Skip to content

Commit 5aee1ae

Browse files
authored
Merge pull request #55 from PaulHax/wasm
ENH: Add package with itk-wasm build
2 parents d962330 + 6af3e34 commit 5aee1ae

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
3+
emscripten-build/
4+
wasi-build/
5+
6+
package-lock.json
7+
pnpm-lock.yaml

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ if(NOT ITK_SOURCE_DIR)
55
find_package(ITK REQUIRED)
66
list(APPEND CMAKE_MODULE_PATH ${ITK_CMAKE_DIR})
77
include(ITKModuleExternal)
8+
if(WASI OR EMSCRIPTEN)
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
10+
add_subdirectory(wasm)
11+
endif()
812
else()
913
set(ITK_DIR ${CMAKE_BINARY_DIR})
1014
itk_module_impl()

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "wasm",
3+
"version": "1.0.0",
4+
"description": "npm scripts to generate itk-wasm artifacts.",
5+
"type": "module",
6+
"scripts": {
7+
"build": "npm run emscripten && npm run bindgen",
8+
"emscripten": "npx itk-wasm -b emscripten-build build",
9+
"bindgen": "npx itk-wasm -b emscripten-build bindgen --output-dir wasm/typescript --package-name mesh-to-poly-data --package-description \"Convert an ITK Mesh to a simple data structure compatible with vtkPolyData.\" --repository 'https://github.com/InsightSoftwareConsortium/ITKMeshToPolyData'",
10+
"wasi": "npx itk-wasm -b wasi-build -i itkwasm/wasi build",
11+
"clean": "git clean -fdx -e node_modules"
12+
},
13+
"license": "Apache-2.0",
14+
"devDependencies": {
15+
"itk-wasm": "^1.0.0-b.73"
16+
}
17+
}

wasm/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
emscripten-build
3+
node_modules
4+
typescript

wasm/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(mesh-to-poly-data)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
find_package(ITK REQUIRED
7+
COMPONENTS
8+
WebAssemblyInterface
9+
MeshToPolyData
10+
)
11+
include(${ITK_USE_FILE})
12+
13+
add_executable(mesh-to-poly-data mesh-to-poly-data.cxx)
14+
target_link_libraries(mesh-to-poly-data PUBLIC ${ITK_LIBRARIES})
15+
16+
add_executable(poly-data-to-mesh poly-data-to-mesh.cxx)
17+
target_link_libraries(poly-data-to-mesh PUBLIC ${ITK_LIBRARIES})

wasm/mesh-to-poly-data.cxx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
#include "itkMesh.h"
19+
#include "itkPolyData.h"
20+
#include "itkInputMesh.h"
21+
#include "itkOutputPolyData.h"
22+
#include "itkPipeline.h"
23+
#include "itkSupportInputMeshTypes.h"
24+
#include "itkWasmMeshIOFactory.h"
25+
#include "itkMeshToPolyDataFilter.h"
26+
#include "itkVector.h"
27+
28+
template<typename TMesh>
29+
class PipelineFunctor
30+
{
31+
public:
32+
int operator()(itk::wasm::Pipeline & pipeline)
33+
{
34+
using MeshType = TMesh;
35+
36+
using InputMeshType = itk::wasm::InputMesh<MeshType>;
37+
InputMeshType inputMesh;
38+
pipeline.add_option("input-mesh", inputMesh, "Input mesh")->required()->type_name("INPUT_MESH");
39+
40+
using PolyDataType = itk::PolyData<typename MeshType::PixelType>;
41+
using OutputPolyDataType = itk::wasm::OutputPolyData<PolyDataType>;
42+
OutputPolyDataType outputPolyData;
43+
pipeline.add_option("output-polydata", outputPolyData, "Output polydata")->required()->type_name("OUTPUT_POLYDATA");
44+
45+
ITK_WASM_PARSE(pipeline);
46+
47+
using MeshToPolyDataFilterType = itk::MeshToPolyDataFilter<MeshType>;
48+
auto meshToPolyDataFilter = MeshToPolyDataFilterType::New();
49+
meshToPolyDataFilter->SetInput(inputMesh.Get());
50+
meshToPolyDataFilter->Update();
51+
52+
outputPolyData.Set(meshToPolyDataFilter->GetOutput());
53+
54+
return EXIT_SUCCESS;
55+
}
56+
};
57+
58+
int main (int argc, char * argv[])
59+
{
60+
itk::wasm::Pipeline pipeline("mesh-to-poly-data", "Convert an itk::Mesh to an itk::PolyData", argc, argv);
61+
62+
itk::WasmMeshIOFactory::RegisterOneFactory();
63+
64+
return itk::wasm::SupportInputMeshTypes<PipelineFunctor,
65+
uint8_t,
66+
int8_t,
67+
float,
68+
double,
69+
itk::Vector<uint8_t, 3>,
70+
itk::Vector<float, 3>
71+
>
72+
::Dimensions<2U,3U>("input-mesh", pipeline);
73+
}

wasm/poly-data-to-mesh.cxx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
#include "itkMesh.h"
19+
#include "itkPolyData.h"
20+
#include "itkInputPolyData.h"
21+
#include "itkOutputMesh.h"
22+
#include "itkPipeline.h"
23+
#include "itkSupportInputPolyDataTypes.h"
24+
#include "itkWasmMeshIOFactory.h"
25+
#include "itkPolyDataToMeshFilter.h"
26+
27+
template<typename TPolyData>
28+
class PipelineFunctor
29+
{
30+
public:
31+
int operator()(itk::wasm::Pipeline & pipeline)
32+
{
33+
using PolyDataType = TPolyData;
34+
35+
using InputPolyDataType = itk::wasm::InputPolyData<PolyDataType>;
36+
InputPolyDataType inputPolyData;
37+
pipeline.add_option("input-polydata", inputPolyData, "Input polydata")->required()->type_name("INPUT_POLYDATA");
38+
39+
using MeshType = itk::Mesh<typename PolyDataType::PixelType, 3>;
40+
using OutputMeshType = itk::wasm::OutputMesh<MeshType>;
41+
OutputMeshType outputMesh;
42+
pipeline.add_option("output-mesh", outputMesh, "Output mesh")->required()->type_name("OUTPUT_MESH");
43+
44+
ITK_WASM_PARSE(pipeline);
45+
46+
using PolyDataToMeshFilterType = itk::PolyDataToMeshFilter<PolyDataType>;
47+
auto polyDataToMeshFilter = PolyDataToMeshFilterType::New();
48+
polyDataToMeshFilter->SetInput(inputPolyData.Get());
49+
polyDataToMeshFilter->Update();
50+
51+
outputMesh.Set(polyDataToMeshFilter->GetOutput());
52+
53+
return EXIT_SUCCESS;
54+
}
55+
};
56+
57+
int main (int argc, char * argv[])
58+
{
59+
itk::wasm::Pipeline pipeline("poly-data-to-mesh", "Convert an itk::PolyData to an itk::Mesh", argc, argv);
60+
61+
itk::WasmMeshIOFactory::RegisterOneFactory();
62+
63+
return itk::wasm::SupportInputPolyDataTypes<PipelineFunctor>
64+
::PixelTypes<uint8_t,int8_t,float,double>("input-polydata", pipeline);
65+
}

0 commit comments

Comments
 (0)