Skip to content

Commit

Permalink
ENH: Add WebAssembly interface
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 15, 2022
1 parent 4427e06 commit 5a0de71
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
# macOS Azure VM Warning
"ld: warning: text-based stub file"
"libcleaver.a.*has no symbols"
"libitkcleaver.*has no symbols"
)
set(dashboard_no_clean 1)
set(ENV{CC} ${{ matrix.c-compiler }})
Expand Down
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ project(Cleaver)

set(CMAKE_CXX_STANDARD 17)

set(Cleaver_LIBRARIES cleaver)

include(FetchContent)
set(_itk_build_testing ${BUILD_TESTING})
#set(BUILD_TESTING OFF)
Expand All @@ -29,7 +27,11 @@ endif()
set(BUILD_TESTING ${_itk_build_testing})
set(BUILD_SHARED_LIBS ${_itk_build_shared})

include_directories(${cleaver_lib_SOURCE_DIR}/src/lib)
set(Cleaver_LIBRARIES cleaver)
set(Cleaver_INCLUDE_DIRS
${Cleaver_SOURCE_DIR}/include
${cleaver_lib_SOURCE_DIR}/src/lib
)

if(NOT ITK_SOURCE_DIR)
find_package(ITK REQUIRED)
Expand All @@ -39,3 +41,5 @@ else()
set(ITK_DIR ${CMAKE_BINARY_DIR})
itk_module_impl()
endif()

itk_module_target(cleaver NO_INSTALL)
49 changes: 49 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.10)
project(ITKCleaverWebAssembly)

set(CMAKE_CXX_STANDARD 17)

if(EMSCRIPTEN)
set(io_components
)
elseif(WASI)
set(io_components
ITKIONRRD
ITKIOMeshVTK
)
else()
set(io_components
ITKMeshIO
ITKImageIO
)
endif()
find_package(ITK REQUIRED
COMPONENTS
${io_components}
WebAssemblyInterface
Cleaver
)
include(${ITK_USE_FILE})

add_executable(ITKCleaverWasm ITKCleaverWasm.cxx)
target_link_libraries(ITKCleaverWasm PUBLIC ${ITK_LIBRARIES})

enable_testing()
add_test(NAME ITKCleaverWasmLabelImageTest
COMMAND ITKCleaverWasm
--input
${CMAKE_CURRENT_SOURCE_DIR}/mickey.nrrd
--triangle
${CMAKE_CURRENT_BINARY_DIR}/mickey-triangle.vtk
)

add_test(NAME ITKCleaverIndicatorFunctionTest
COMMAND ITKCleaverWasm
--input
${CMAKE_CURRENT_SOURCE_DIR}/spheres1.nrrd
${CMAKE_CURRENT_SOURCE_DIR}/spheres2.nrrd
${CMAKE_CURRENT_SOURCE_DIR}/spheres3.nrrd
${CMAKE_CURRENT_SOURCE_DIR}/spheres4.nrrd
--triangle
${CMAKE_CURRENT_BINARY_DIR}/indicator-triangle.vtk
)
113 changes: 113 additions & 0 deletions examples/ITKCleaverWasm.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkCleaverImageToMeshFilter.h"
#include "itkPipeline.h"
#include "itkInputImage.h"
#include "itkOutputMesh.h"
#include "itkSupportInputImageTypesNoVectorImage.h"
#include "itkMesh.h"

template<typename TImage>
int
Mesher(itk::wasm::Pipeline & pipeline, std::vector<const TImage *> & inputImages)
{
using ImageType = TImage;

using MeshType = itk::Mesh<typename ImageType::PixelType, 3>;
using OutputMeshType = itk::wasm::OutputMesh<MeshType>;
OutputMeshType outputTriangleMesh;
pipeline.add_option("-t,--triangle", outputTriangleMesh, "Output triangle mesh");

double sigma = 1.0;
pipeline.add_option("-s,--sigma", sigma, "Blending function sigma for input(s) to remove alias artifacts.");

double samplingRate = 1.0;
pipeline.add_option("-r,--sampling-rate", samplingRate, "Sizing field sampling rate. The default sample rate will be the dimensions of the volume. Smaller sampling creates coarser meshes.");

double lipschitz = 0.2;
pipeline.add_option("-l,--lipschitz", lipschitz, "Sizing field rate of change. the maximum rate of change of element size throughout a mesh.");

double featureScaling = 1.0;
pipeline.add_option("-f,--feature-scaling", featureScaling, "Sizing field feature scaling. Scales features of the mesh effecting element size. Higher feature scaling creates coaser meshes.");

int padding = 0;
pipeline.add_option("-p,--padding", padding, "Sizing field padding. Adds a volume buffer around the data. Useful when volumes intersect near the boundary.");

ITK_WASM_PARSE(pipeline);

using FilterType = itk::CleaverImageToMeshFilter<ImageType, MeshType>;
typename FilterType::Pointer filter = FilterType::New();

for(size_t ii = 0; ii < inputImages.size(); ii++)
{
filter->SetInput(ii, inputImages[ii]);
}

filter->SetSigma(sigma);
filter->SetSamplingRate(samplingRate);
filter->SetLipschitz(lipschitz);
filter->SetFeatureScaling(featureScaling);
filter->SetPadding(padding);

ITK_WASM_CATCH_EXCEPTION(pipeline, filter->Update());

outputTriangleMesh.Set(filter->GetOutput(1));

return EXIT_SUCCESS;
}

template<typename TImage>
class PipelineFunctor
{
public:
int operator()(itk::wasm::Pipeline & pipeline)
{
using ImageType = TImage;

using InputImageType = itk::wasm::InputImage<ImageType>;
std::vector<InputImageType> inputImages;
pipeline.add_option("-i,--input", inputImages, "Input label image or multiple indicator function images");

ITK_WASM_PRE_PARSE(pipeline);

std::vector<const ImageType *> loadedInputImages;
for(auto image: inputImages)
{
loadedInputImages.push_back(image.Get());
}

int result = Mesher<ImageType>(pipeline, loadedInputImages);
return result;
}
};

int main( int argc, char * argv[] )
{
itk::wasm::Pipeline pipeline("Create a multi-material mesh suitable for simulation/modeling from an input label image or indicator function images", argc, argv);

return itk::wasm::SupportInputImageTypesNoVectorImage<PipelineFunctor,
//uint8_t,
//int8_t,
uint16_t,
//int16_t,
//float,
//double
float
>
::Dimensions<3U>("Input", pipeline);
}
Binary file added examples/mickey.nrrd
Binary file not shown.
Binary file added examples/spheres1.nrrd
Binary file not shown.
Binary file added examples/spheres2.nrrd
Binary file not shown.
Binary file added examples/spheres3.nrrd
Binary file not shown.
Binary file added examples/spheres4.nrrd
Binary file not shown.

0 comments on commit 5a0de71

Please sign in to comment.