Skip to content

Commit

Permalink
feat: migrate mesh-io to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 25, 2023
1 parent bd85277 commit c58da03
Show file tree
Hide file tree
Showing 12 changed files with 751 additions and 10 deletions.
17 changes: 17 additions & 0 deletions include/itkOutputMeshIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ namespace wasm
class OutputMeshIO
{
public:
/** Set whether to only read mesh metadata. Do not read the points, points data. */
void SetInformationOnly(bool informationOnly) {
this->m_InformationOnly = informationOnly;
}

void Set(MeshIOBase * imageIO) {
this->m_MeshIO = imageIO;
}
Expand Down Expand Up @@ -76,6 +81,11 @@ class OutputMeshIO
wasmMeshIOBase->SetMeshIO(this->m_MeshIO);
setMemoryStoreOutputDataObject(0, index, wasmMeshIOBase);

if (this->m_InformationOnly)
{
return;
}

const auto pointsSize = wasmMeshIOBase->GetPointsContainer()->size();
if (pointsSize)
{
Expand Down Expand Up @@ -138,6 +148,11 @@ class OutputMeshIO

wasmMeshIO->WriteMeshInformation();

if (this->m_InformationOnly)
{
return;
}

SizeValueType numberOfBytes = this->m_MeshIO->GetNumberOfPoints() * this->m_MeshIO->GetPointDimension() * WasmMeshIO::ITKComponentSize( this->m_MeshIO->GetPointComponentType() );
std::vector<char> loadBuffer(numberOfBytes);
if (numberOfBytes)
Expand Down Expand Up @@ -188,6 +203,8 @@ class OutputMeshIO
typename MeshIOBase::Pointer m_MeshIO;

std::string m_Identifier;

bool m_InformationOnly{ false };
};

bool lexical_cast(const std::string &input, OutputMeshIO &outputMeshIO)
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"build:testData": "npm run build:testData:dicom && npm run build:testData:image-io",
"build:testData:dicom": "dam download packages/dicom/test/data packages/dicom/test/data.tar.gz bafybeicskxufnvuem6342pkfwgeo3siiozgzmfo5f34woge6aptuzuwzzu https://github.com/InsightSoftwareConsortium/itk-wasm/releases/download/itk-wasm-v1.0.0-b.119/dicom-test-data.tar.gz https://w3s.link/ipfs/bafybeiby67winzvozowf4moqthwunuxxscssitnb6wahxv4ugvfxhu2vki/data.tar.gz",
"build:testData:image-io": "dam download packages/image-io/test/data packages/image-io/test/data.tar.gz bafybeibyjhkcrinl2lotw5g2vngjs23aaenv3tjzxssm35jxaci5ylsqia https://github.com/InsightSoftwareConsortium/itk-wasm/releases/download/itk-wasm-v1.0.0-b.155/image-io-test-data.tar.gz https://w3s.link/ipfs/bafybeiewe4rankwwe7nw7qm2g3qclflhci2e53wthn3gukdlhbff64pua4/data.tar.gz",
"build:testData:mesh-io": "dam download packages/mesh-io/test/data packages/mesh-io/test/data.tar.gz bafybeibyjhkcrinl2lotw5g2vngjs23aaenv3tjzxssm35jxaci5ylsqia https://github.com/InsightSoftwareConsortium/itk-wasm/releases/download/itk-wasm-v1.0.0-b.155/mesh-io-test-data.tar.gz https://w3s.link/ipfs/bafybeiewe4rankwwe7nw7qm2g3qclflhci2e53wthn3gukdlhbff64pua4/data.tar.gz",
"build:debug": "npm run build:emscripten -- --debug && npm run build:tsc && npm run build:tscWorkersModuleLoader && npm run build:tscWebWorkers && npm run build:workerBundle && npm run build:workerMinBundle && npm run build:webpack -- --mode development",
"build:tsc": "tsc --pretty",
"build:tscWorkersModuleLoader": "tsc --types --lib es2017,webworker --rootDir ./src/ --outDir ./dist/ --moduleResolution node --target es2017 --module es2020 --strict --forceConsistentCasingInFileNames --declaration ./src/core/internal/loadEmscriptenModuleWebWorker.ts",
Expand All @@ -50,12 +51,16 @@
"build:emscripten:image-io": "node ./src/itk-wasm-cli.js -s packages/image-io -b emscripten-build build",
"build:bindgen:typescript:image-io": "./src/itk-wasm-cli.js -s packages/image-io -b emscripten-build bindgen --package-version 0.5.0 --package-name @itk-wasm/image-io --package-description \"Input and output for scientific and medical image file formats.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:image-io": "./src/itk-wasm-cli.js -s packages/image-io -b wasi-build bindgen --interface python --package-name itkwasm-image-io --package-description \"Input and output for scientific and medical image file formats.\" --package-version 0.5.0 --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:mesh-io": "node ./src/itk-wasm-cli.js -s packages/mesh-io -b emscripten-build build",
"build:bindgen:typescript:mesh-io": "./src/itk-wasm-cli.js -s packages/mesh-io -b emscripten-build bindgen --package-version 0.1.0 --package-name @itk-wasm/mesh-io --package-description \"Input and output for scientific and medical image file formats.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:mesh-io": "./src/itk-wasm-cli.js -s packages/mesh-io -b wasi-build bindgen --interface python --package-name itkwasm-mesh-io --package-description \"Input and output for scientific and medical image file formats.\" --package-version 0.1.0 --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:packages": "npm run build:emscripten:compress-stringify && npm run build:bindgen:typescript:compress-stringify && npm run build:emscripten:dicom && npm run build:bindgen:typescript:dicom && npm run build:emscripten:compare-images && npm run build:bindgen:typescript:compare-images && npm run build:emscripten:image-io && npm run build:bindgen:typescript:image-io",
"build:wasi": "node ./src/build-wasi.js && npm run build:wasi:packages",
"build:wasi:compress-stringify": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/compress-stringify -b wasi-build build",
"build:wasi:dicom": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/dicom -b wasi-build build",
"build:wasi:compare-images": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/compare-images -b wasi-build build",
"build:wasi:image-io": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/image-io -b wasi-build build",
"build:wasi:mesh-io": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/mesh-io -b wasi-build build",
"build:wasi:packages": "npm run build:wasi:compress-stringify && npm run build:bindgen:python:compress-stringify && npm run build:wasi:compare-images && npm run build:bindgen:python:compare-images && npm run build:wasi:dicom && npm run build:bindgen:python:dicom && npm run build:wasi:image-io && npm run build:bindgen:python:image-io",
"cypress:open": "npx cypress open",
"cypress:run": "npx cypress run --config defaultCommandTimeout=8000",
Expand Down
2 changes: 1 addition & 1 deletion packages/compare-images/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
"type": "git",
"url": "https://github.com/InsightSoftwareConsortium/itk-wasm"
}
}
}
1 change: 0 additions & 1 deletion packages/image-io/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ImageIOIndex.ts.in
dist
emscripten-build/
wasi-build/
Expand Down
16 changes: 8 additions & 8 deletions packages/image-io/itkWasmZstdImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace itk
WasmZstdImageIO
::WasmZstdImageIO()
{
this->AddSupportedWriteExtension(".iwi.cbor.zstd");
this->AddSupportedReadExtension(".iwi.cbor.zstd");
this->AddSupportedWriteExtension(".iwi.cbor.zst");
this->AddSupportedReadExtension(".iwi.cbor.zst");
}


Expand Down Expand Up @@ -68,9 +68,9 @@ ::ReadImageInformation()

const std::string path = this->GetFileName();

std::string::size_type zstdPos = path.rfind(".zstd");
std::string::size_type zstdPos = path.rfind(".zst");
if ( ( zstdPos != std::string::npos )
&& ( zstdPos == path.length() - 5 ) )
&& ( zstdPos == path.length() - 4 ) )
{
std::ifstream dataStream;
this->OpenFileForReading( dataStream, this->GetFileName() );
Expand Down Expand Up @@ -98,9 +98,9 @@ WasmZstdImageIO
::Read( void *buffer )
{
const std::string path = this->GetFileName();
std::string::size_type zstdPos = path.rfind(".zstd");
std::string::size_type zstdPos = path.rfind(".zst");
if ( ( zstdPos != std::string::npos )
&& ( zstdPos == path.length() - 5 ) )
&& ( zstdPos == path.length() - 4 ) )
{
std::ifstream dataStream;
this->OpenFileForReading( dataStream, this->GetFileName() );
Expand Down Expand Up @@ -166,9 +166,9 @@ ::Write( const void *buffer )
{
const std::string path(this->GetFileName());

std::string::size_type cborPos = path.rfind(".zstd");
std::string::size_type cborPos = path.rfind(".zst");
if ( ( cborPos != std::string::npos )
&& ( cborPos == path.length() - 5 ) )
&& ( cborPos == path.length() - 4 ) )
{
unsigned char * inputBinary;
const size_t inputBinarySize = this->WriteCBOR(buffer, &inputBinary, true);
Expand Down
6 changes: 6 additions & 0 deletions packages/mesh-io/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist
emscripten-build/
wasi-build/
package-lock.json
test/data/
test/data.tar.gz
25 changes: 25 additions & 0 deletions packages/mesh-io/BuildZstd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include(FetchContent)
option(ZSTD_BUILD_CONTRIB "BUILD_CONTRIB" OFF)
option(ZSTD_BUILD_PROGRAMS "BUILD_PROGRAMS" OFF)
option(ZSTD_BUILD_SHARED "BUILD_SHARED" OFF)
option(ZSTD_BUILD_STATIC "BUILD_STATIC" ON)
option(ZSTD_BUILD_TESTS "BUILD_TESTS" OFF)
option(ZSTD_BUILD_LEGACY_SUPPORT "BUILD_LEGACY_SUPPORT" OFF)
option(ZSTD_MULTITHREAD_SUPPORT "BUILD_MULTITHREAD_SUPPORT" OFF)
option(ZSTD_BUILD_PROGRAMS_LINK_SHARED "BUILD_PROGRAMS_LINK_SHARED" OFF)
option(ZSTD_BUILD_LZ4 "BUILD_LZ4" OFF)
option(ZSTD_BUILD_LZMA "BUILD_LZMA" OFF)
option(ZSTD_BUILD_ZLIB "BUILD_ZLIB" OFF)
set(zstd_GIT_REPOSITORY "https://github.com/facebook/zstd.git")
# v1.5.5
set(zstd_GIT_TAG 17ecb1e2df45a45be908b34aba34955199bf11b4)
FetchContent_Declare(
zstd_lib
GIT_REPOSITORY ${zstd_GIT_REPOSITORY}
GIT_TAG ${zstd_GIT_TAG}
)

FetchContent_MakeAvailable(zstd_lib)
set(zstd_lib_INCLUDE_DIR "${zstd_lib_SOURCE_DIR}/lib")
include_directories(${zstd_lib_INCLUDE_DIR})
add_subdirectory("${zstd_lib_SOURCE_DIR}/build/cmake" "${zstd_lib_BINARY_DIR}")
131 changes: 131 additions & 0 deletions packages/mesh-io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
cmake_minimum_required(VERSION 3.16)
project(itkwasm-mesh-io)

set(CMAKE_CXX_STANDARD 17)

if (NOT TARGET libzstd_static)
include(${CMAKE_CURRENT_SOURCE_DIR}/BuildZstd.cmake)
endif()

# This list should be ordered from approximately most commonly to least
# commonly used modules.
set(WebAssemblyInterface_MeshIOModules
"ITKIOMeshBYU"
"ITKIOMeshFreeSurfer"
"ITKIOMeshOBJ"
"ITKIOMeshOFF"
"IOMeshSTL"
"IOMeshSWC"
"ITKIOMeshVTK"
CACHE STRING
"String delimited list of ITK mesh IO modules to support.")

set(meshios_ITKIOMeshBYU itkBYUMeshIO)
set(meshio_id_itkBYUMeshIO 0)
set(meshio_kebab_itkBYUMeshIO "byu")

set(meshios_ITKIOMeshFreeSurfer itkFreeSurferAsciiMeshIO itkFreeSurferBinaryMeshIO)
set(meshio_id_itkFreeSurferAsciiMeshIO 1)
set(meshio_kebab_itkFreeSurferAsciiMeshIO "free-surfer-ascii")
set(meshio_id_itkFreeSurferBinaryMeshIO 2)
set(meshio_kebab_itkFreeSurferBinaryMeshIO "free-surfer-binary")

set(meshios_ITKIOMeshVTK itkVTKPolyDataMeshIO)
set(meshio_id_itkVTKPolyDataMeshIO 3)
set(meshio_kebab_itkVTKPolyDataMeshIO "vtk-poly-data")

set(meshios_ITKIOMeshOBJ itkOBJMeshIO)
set(meshio_id_itkOBJMeshIO 4)
set(meshio_kebab_itkOBJMeshIO "obj")

set(meshios_ITKIOMeshOFF itkOFFMeshIO)
set(meshio_id_itkOFFMeshIO 5)
set(meshio_kebab_itkOFFMeshIO "off")

set(meshios_IOMeshSTL itkSTLMeshIO)
set(meshio_id_itkSTLMeshIO 6)
set(meshio_kebab_itkSTLMeshIO "stl")

set(meshios_IOMeshSWC itkSWCMeshIO)
set(meshio_id_itkSWCMeshIO 7)
set(meshio_kebab_itkSWCMeshIO "swc")

set(meshios_WebAssemblyInterface itkWasmMeshIO itkWasmZstdMeshIO)
set(meshio_id_itkWasmMeshIO 8)
set(meshio_kebab_itkWasmMeshIO "wasm")
set(meshio_id_itkWasmZstdMeshIO 9)
set(meshio_kebab_itkWasmZstdMeshIO "wasm-ztd")

set(ITK_NO_MESHIO_FACTORY_REGISTER_MANAGER 1)
set(MeshIOIndex_ARRAY "")
foreach(io_module ${WebAssemblyInterface_MeshIOModules} WebAssemblyInterface)
if (DEFINED WebAssemblyInterface_INCLUDE_DIRS)
if(${io_module} STREQUAL "WebAssemblyInterface")
find_package(ITK REQUIRED COMPONENTS ITKIOMeshBase)
include(${ITK_USE_FILE})
include_directories(${WebAssemblyInterface_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
list(APPEND ITK_LIBRARIES ${WebAssemblyInterface_LIBRARIES})
else()
find_package(ITK REQUIRED COMPONENTS ${io_module} WebAssemblyInterface)
include(${ITK_USE_FILE})
endif()
else()
find_package(ITK REQUIRED COMPONENTS ${io_module} WebAssemblyInterface)
include(${ITK_USE_FILE})
endif()

if(NOT DEFINED meshios_${io_module})
message(FATAL_ERROR "Unknown MeshIOBase classes for module ${io_module}")
endif()
foreach(meshio ${meshios_${io_module}})
string(SUBSTRING "${meshio}" 3 -1 ioname)
if(NOT DEFINED meshio_kebab_${meshio})
message(FATAL_ERROR "Unknown kebab name for meshio ${meshio}")
endif()
set(ioname ${meshio_kebab_${meshio}})
set(read_binary "${ioname}-read-mesh")
set(write_binary "${ioname}-write-mesh")
set(MeshIOIndex_ARRAY "${MeshIOIndex_ARRAY}'${ioname}', ")
set(extra_srcs)
if(${meshio} STREQUAL "itkWasmZstdMeshIO")
list(APPEND ITK_LIBRARIES libzstd_static)
list(APPEND extra_srcs itkWasmZstdMeshIO.cxx)
endif()

add_executable(${read_binary} read-mesh.cxx ${extra_srcs})
target_link_libraries(${read_binary} PUBLIC ${ITK_LIBRARIES})
target_compile_definitions(${read_binary} PUBLIC -DMESH_IO_CLASS=${meshio_id_${meshio}} -DMESH_IO_KEBAB_NAME=${ioname})
add_executable(${write_binary} write-mesh.cxx ${extra_srcs})
target_link_libraries(${write_binary} PUBLIC ${ITK_LIBRARIES})
target_compile_definitions(${write_binary} PUBLIC -DMESH_IO_CLASS=${meshio_id_${meshio}} -DMESH_IO_KEBAB_NAME=${ioname})
if (EMSCRIPTEN)
set(target_esm_read "${read_binary}")
set(target_esm_write "${write_binary}")
foreach(target ${target_esm_read} ${target_esm_write})
set(exception_catching )
set(meshio_common_link_flags " -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s SUPPORT_LONGJMP=1")
get_property(link_flags TARGET ${target} PROPERTY LINK_FLAGS)
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " ${meshio_common_link_flags} ${link_flags}")
endforeach()
endif()
endforeach()
endforeach()

enable_testing()

set(input_dir ${CMAKE_CURRENT_SOURCE_DIR}/test/data/input)
set(baseline_dir ${CMAKE_CURRENT_SOURCE_DIR}/test/data/baseline)
set(output_dir ${CMAKE_CURRENT_BINARY_DIR})

add_test(NAME byu-read-mesh-test
COMMAND byu-read-mesh
${input_dir}/cube.byu
${output_dir}/byu-read-mesh-test.could-read.json
${output_dir}/byu-read-mesh-test.iwm.cbor)

add_test(NAME byu-write-mesh-test
COMMAND byu-write-mesh
${baseline_dir}/byu-read-mesh-test.iwm.cbor
${output_dir}/byu-write-mesh-test.could-write.json
${output_dir}/byu-write-mesh-test.byu)
Loading

0 comments on commit c58da03

Please sign in to comment.