Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Attempt to cross-compile PolySeg for use on the Web #4

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions web/Docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#FROM kitware/itk-js-vtk:latest
FROM insighttoolkit/itk-js:latest
MAINTAINER PerkLab

WORKDIR /

#2019-01-03 + Emscripten patches
ENV VTK_GIT_TAG acd6ccd796c511acdafc9c2ff2f2135f1a8900cc
RUN git clone https://github.com/swederik/VTK.git && \
cd VTK && \
git checkout ${VTK_GIT_TAG} && \
cd ../ && \
mkdir VTK-build && \
cd VTK-build && \
cmake \
-G Ninja \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
-DCMAKE_INSTALL_PREFIX:PATH=/install-prefix \
-DBUILD_EXAMPLES:BOOL=OFF \
-DBUILD_TESTING:BOOL=OFF \
-DVTK_RENDERING_BACKEND:STRING=None \
-DModule_vtkIOExport:BOOL=ON \
-DVTK_NO_PLATFORM_SOCKETS:BOOL=ON \
-DCMAKE_USE_PTHREADS_INIT:BOOL=OFF \
../VTK && \
ninja -j7 && \
find . -name '*.o' -delete && \
cd .. && chmod -R 777 VTK-build

# 2019-01-27
ENV ITKVtkGlue_GIT_TAG 6f3cd6fd697ec19ee12b65041537594bf295c0ca
RUN git clone https://github.com/InsightSoftwareConsortium/ITKVtkGlue.git && \
cd ITKVtkGlue && \
git checkout ${ITKVtkGlue_GIT_TAG} && \
cd ../ && \
mkdir ITKVtkGlue-build && \
cd ITKVtkGlue-build && \
cmake \
-G Ninja \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
-DCMAKE_INSTALL_PREFIX:PATH=/install-prefix \
-DBUILD_TESTING:BOOL=OFF \
-DITK_DIR:PATH=/ITK-build \
-DVTK_DIR:PATH=/VTK-build \
../ITKVtkGlue && \
ninja -j7 && \
find . -name '*.o' -delete && \
cd .. && chmod -R 777 ITK-build

# 2019-03-11
ENV PolySeg_GIT_TAG 74f1665a5c2d15e22d57e3a44821ad1c15b739aa
RUN git clone https://github.com/PerkLab/PolySeg.git && \
cd PolySeg && \
git checkout ${PolySeg_GIT_TAG} && \
cd ../ && \
mkdir PolySeg-build && \
cd PolySeg-build && \
cmake \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
-DCMAKE_INSTALL_PREFIX:PATH=/install-prefix \
-DBUILD_TESTING:BOOL=OFF \
-DVTK_DIR:PATH=/VTK-build \
../PolySeg && \
make && \
make install && \
cd .. && chmod -R 777 PolySeg-build

COPY ITKBridgeJavaScript.cmake /usr/share/cmake-3.13/Modules/

RUN ls /install-prefix/lib/PolySeg/

ARG IMAGE=ohif/polyseg-web
ARG VERSION=latest
ENV DEFAULT_DOCKCROSS_IMAGE ${IMAGE}:${VERSION}
WORKDIR /work
28 changes: 28 additions & 0 deletions web/Docker/ITKBridgeJavaScript.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function(web_add_executable target_name)
add_executable(${target_name} ${ARGN})
set_property(TARGET ${target_name} APPEND_STRING
PROPERTY LINK_FLAGS " -s WASM=0 -s ASSERTIONS=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=1 -s INVOKE_RUN=0 --pre-js /ITKBridgeJavaScript/src/EmscriptenModule/itkJSPipelinePre.js --post-js /ITKBridgeJavaScript/src/EmscriptenModule/itkJSPost.js"
)
set_property(TARGET ${target_name} APPEND_STRING
PROPERTY LINK_FLAGS_DEBUG " -s DISABLE_EXCEPTION_CATCHING=0"
)

set(wasm_target_name ${target_name}Wasm)
add_executable(${wasm_target_name} ${ARGN})
set(pre_js ${CMAKE_CURRENT_BINARY_DIR}/itkJSPipelinePre${target_name}.js)
configure_file(/ITKBridgeJavaScript/src/EmscriptenModule/itkJSPipelinePre.js.in
${pre_js} @ONLY)
set_property(TARGET ${wasm_target_name} APPEND_STRING
PROPERTY LINK_FLAGS " -s BINARYEN_ASYNC_COMPILATION=0 -s SINGLE_FILE=1 -s WASM=1 -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s NO_EXIT_RUNTIME=1 -s INVOKE_RUN=0 --pre-js ${pre_js} --post-js /ITKBridgeJavaScript/src/EmscriptenModule/itkJSPost.js"
)
set_property(TARGET ${wasm_target_name} APPEND_STRING
PROPERTY LINK_FLAGS_DEBUG " -s DISABLE_EXCEPTION_CATCHING=0"
)
endfunction()

function(web_target_link_libraries target_name)
target_link_libraries(${target_name} ${ARGN})

set(wasm_target_name ${target_name}Wasm)
target_link_libraries(${wasm_target_name} ${ARGN})
endfunction()
19 changes: 19 additions & 0 deletions web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Build the Docker image for cross-compilation

The image is based on `kitware/itk-js-vtk`

```bash
# Docker needs to be running...
docker build -t ohif/polyseg-web .
```

### Run the build

```bash
cd helloPolySeg
npm install
npx itk-js build --image ohif/polyseg-web:latest .
```

#### TODO:
- [ ] Weird that I have to clone the src into /src under helloPolySegsub
58 changes: 58 additions & 0 deletions web/binaryLabelmapToClosedSurface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cmake_minimum_required(VERSION 3.10)
project(BinaryLabelmapToClosedSurface)

if(EMSCRIPTEN)
find_package(VTK REQUIRED
COMPONENTS
vtkCommonCore
vtkCommonDataModel
vtkImagingCore
vtkImagingSources
vtkFiltersGeometry
vtkImagingStatistics
vtkImagingStencil
vtkIOExport
)
else()
find_package(VTK REQUIRED)
endif()

include(${VTK_USE_FILE})

set(io_components ITKImageIO)
if(EMSCRIPTEN)
set(io_components BridgeJavaScript)
endif()

if(EMSCRIPTEN)
find_package(ITK REQUIRED
COMPONENTS
ITKIOMeshBase
ITKVtkGlue
${io_components}
)
else()
find_package(ITK REQUIRED
COMPONENTS
${io_components}
)
endif()

include(${ITK_USE_FILE})

set(hello_SRCS binaryLabelmapToClosedSurface.cxx)

if(EMSCRIPTEN)
include(ITKBridgeJavaScript)
web_add_executable(hello ${hello_SRCS})
web_target_link_libraries(hello ${ITK_LIBRARIES} ${VTK_LIBRARIES} /install-prefix/lib/PolySeg/libPolySeg.a)
else()
add_executable(hello ${hello_SRCS})
target_link_libraries(hello ${ITK_LIBRARIES} ${VTK_LIBRARIES} /install-prefix/lib/PolySeg/libPolySeg.a)
endif()

enable_testing()
add_test(NAME binaryLabelmapToClosedSurface
COMMAND binaryLabelmapToClosedSurface ${CMAKE_CURRENT_BINARY_DIR}/Segmentation-label.nii
${CMAKE_CURRENT_BINARY_DIR}/Segmentation.vtk.json
)
21 changes: 21 additions & 0 deletions web/binaryLabelmapToClosedSurface/PolySegConfigure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Here is where system computed values get stored.
* These values should only change when the target compile platform changes.
*/

/* #undef BUILD_SHARED_LIBS */
#ifndef BUILD_SHARED_LIBS
#define PolySeg_STATIC
#endif

#if defined(WIN32) && !defined(PolySeg_STATIC)
#pragma warning ( disable : 4275 )

#if defined(PolySeg_EXPORT)
#define PolySeg_EXPORT __declspec( dllexport )
#else
#define PolySeg_EXPORT __declspec( dllimport )
#endif
#else
#define PolySeg_EXPORT
#endif
Binary file not shown.
141 changes: 141 additions & 0 deletions web/binaryLabelmapToClosedSurface/binaryLabelmapToClosedSurface.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*==============================================================================

Copyright (c) Laboratory for Percutaneous Surgery (PerkLab)
Queen's University, Kingston, ON, Canada. All Rights Reserved.

See https://opensource.org/licenses/BSD-2-Clause for details.

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.

This file was originally developed by Csaba Pinter, PerkLab, Queen's University
and was supported through the Applied Cancer Research Unit program of Cancer Care
Ontario with funds provided by the Ontario Ministry of Health and Long-Term Care

==============================================================================*/
#include <iostream>
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"

// VTK includes
#include "vtkPolyData.h"
#include "vtkImageData.h"
#include "vtkJSONDataSetWriter.h"
#include "vtkNew.h"
#include "vtkSphereSource.h"

// SegmentationCore includes
#include "vtkSegmentation.h"
#include "vtkSegment.h"
#include "vtkSegmentationConverter.h"
#include "vtkOrientedImageData.h"
#include "vtkSegmentationConverterFactory.h"
#include "vtkBinaryLabelmapToClosedSurfaceConversionRule.h"
#include "vtkClosedSurfaceToBinaryLabelmapConversionRule.h"

//----------------------------------------------------------------------------
int main( int argc, char * argv[] )
{
std::cout << __LINE__ << argv << std::endl;
std::cout << __LINE__ << argc << std::endl;
if( argc < 2 )
{
std::cerr << "Usage: " << argv[0] << " <inputImage> <outputPolyData>" << std::endl;
//return EXIT_FAILURE;
}
const char * inputFilename = argv[1];
const char * outputFilename = argv[2];

using PixelType = unsigned char;
constexpr unsigned int Dimension = 3;
using ImageType = itk::Image< PixelType, Dimension >;

using ReaderType = itk::ImageFileReader< ImageType >;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( inputFilename );

using FilterType = itk::ImageToVTKImageFilter< ImageType >;
FilterType::Pointer filter = FilterType::New();
filter->SetInput( reader->GetOutput() );

vtkSegmentationConverterFactory::GetInstance()->RegisterConverterRule(
vtkSmartPointer<vtkBinaryLabelmapToClosedSurfaceConversionRule>::New() );
vtkSegmentationConverterFactory::GetInstance()->RegisterConverterRule(
vtkSmartPointer<vtkClosedSurfaceToBinaryLabelmapConversionRule>::New() );

// Create new one because by default the direction is identity, origin is zeros and spacing is ones
vtkImageData * myvtkImageData = filter->GetOutput();
vtkNew<vtkOrientedImageData> imageData;
//imageData->SetExtent(vtkImageData->GetExtent);
imageData->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
imageData->DeepCopy(myvtkImageData);
imageData->Print(std::cout);


//imageData->SetExtent(0,size-1,0,size,0,size-1);
//imageData->AllocateScalars(VTK_UNSIGNED_CHAR, 1);

// Create a segment
vtkNew<vtkSegment> segment;
segment->SetName("inputImage");
segment->AddRepresentation(
vtkSegmentationConverter::GetSegmentationBinaryLabelmapRepresentationName(), imageData.GetPointer());
if (!segment->GetRepresentation(vtkSegmentationConverter::GetSegmentationBinaryLabelmapRepresentationName()))
{
std::cerr << __LINE__ << ": Failed to add binary labelmap representation to segment!" << std::endl;
return EXIT_FAILURE;
}

// Create segmentation with segment
vtkNew<vtkSegmentation> segmentation;
segmentation->SetMasterRepresentationName(
vtkSegmentationConverter::GetSegmentationBinaryLabelmapRepresentationName() );
segmentation->AddSegment(segment.GetPointer());
if (segmentation->GetNumberOfSegments() != 1)
{
std::cerr << __LINE__ << ": Failed to add segment to segmentation!" << std::endl;
return EXIT_FAILURE;
}

// Convert to closed surface model
segmentation->CreateRepresentation(vtkSegmentationConverter::GetSegmentationClosedSurfaceRepresentationName());
vtkPolyData* closedSurfaceModel = vtkPolyData::SafeDownCast(
segment->GetRepresentation(vtkSegmentationConverter::GetSegmentationClosedSurfaceRepresentationName()) );
if (!closedSurfaceModel)
{
std::cerr << __LINE__ << ": Failed to convert binary labelmap representation to closed surface model!" << std::endl;
return EXIT_FAILURE;
}

std::cout << "ClosedSurfaceModel: " << closedSurfaceModel->GetBounds() << std::endl;
closedSurfaceModel->Print(std::cout);

vtkNew<vtkSphereSource> sphere;
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(5.0);

// Create the output writer
vtkNew<vtkJSONDataSetWriter> writer;
writer->SetFileName(outputFilename);
//writer->SetInputData(sphere);
//writer->SetInputConnection(sphere->GetOutputPort());
writer->SetInputData(closedSurfaceModel);

try
{
writer->Update();
std::cout << "Written to: " << outputFilename << std::endl;
}
catch( const std::exception & error )
{
std::cerr << "Error: " << error.what() << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

28 changes: 28 additions & 0 deletions web/binaryLabelmapToClosedSurface/example/Pipelines/hello.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions web/binaryLabelmapToClosedSurface/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
itk-webpack-example
===================

This example demonstrates how to configure Webpack to build a project that
uses [itk.js](https://insightsoftwareconsortium.github.io/itk-js/).
More information can be found in the [example
documentation](https://insightsoftwareconsortium.github.io/itk-js/examples/webpack.html).

## Run Locally

```
npm install
npm run start
```

And visit [http://localhost:8080/](http://localhost:8080/).

## Development

```
npm install
npm run build
npm test
```
Binary file not shown.
Loading