Skip to content

Commit c7d6676

Browse files
committed
feat: add itk::wasm::InputTransform
1 parent 03b5cd6 commit c7d6676

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

docs/cxx/interface_types.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ For binding generation, set the `type_name` an the options accordingly. The type
6666
- `INPUT_MESH`
6767
- `OUTPUT_MESH`
6868
- `INPUT_POLYDATA`
69-
- `OUTPUT_POLYDATA`
69+
- `OUTPUT_POLYDATA`
70+
- `INPUT_TRANSFORM`
71+
- `OUTPUT_TRANSFORM`

include/itkInputTransform.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
#ifndef itkInputTransform_h
19+
#define itkInputTransform_h
20+
21+
#include "itkPipeline.h"
22+
23+
#ifndef ITK_WASM_NO_MEMORY_IO
24+
#include "itkWasmExports.h"
25+
#include "itkWasmTransform.h"
26+
#include "itkWasmTransformToTransformFilter.h"
27+
#endif
28+
#ifndef ITK_WASM_NO_FILESYSTEM_IO
29+
#include "itkTransformFileReader.h"
30+
#endif
31+
32+
namespace itk
33+
{
34+
namespace wasm
35+
{
36+
37+
/**
38+
*\class InputTransform
39+
* \brief Input transform for an itk::wasm::Pipeline
40+
*
41+
* This transform is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called.
42+
*
43+
* Call `Get()` to get the TTransform * to use an input to a pipeline.
44+
*
45+
* \ingroup WebAssemblyInterface
46+
*/
47+
template <typename TTransform>
48+
class ITK_TEMPLATE_EXPORT InputTransform
49+
{
50+
public:
51+
using TransformType = TTransform;
52+
53+
void Set(const TransformType * transform) {
54+
this->m_Transform = transform;
55+
}
56+
57+
const TransformType * Get() const {
58+
return this->m_Transform.GetPointer();
59+
}
60+
61+
InputTransform() = default;
62+
~InputTransform() = default;
63+
protected:
64+
typename TTransform::ConstPointer m_Transform;
65+
};
66+
67+
68+
template <typename TTransform>
69+
bool lexical_cast(const std::string &input, InputTransform<TTransform> &inputTransform)
70+
{
71+
if (input.empty())
72+
{
73+
return false;
74+
}
75+
76+
if (wasm::Pipeline::get_use_memory_io())
77+
{
78+
#ifndef ITK_WASM_NO_MEMORY_IO
79+
using WasmTransformToTransformFilterType = WasmTransformToTransformFilter<TTransform>;
80+
auto wasmTransformToTransformFilter = WasmTransformToTransformFilterType::New();
81+
auto wasmTransform = WasmTransformToTransformFilterType::WasmTransformType::New();
82+
const unsigned int index = std::stoi(input);
83+
auto json = getMemoryStoreInputJSON(0, index);
84+
wasmTransform->SetJSON(json);
85+
wasmTransformToTransformFilter->SetInput(wasmTransform);
86+
wasmTransformToTransformFilter->Update();
87+
inputTransform.Set(wasmTransformToTransformFilter->GetOutput());
88+
#else
89+
return false;
90+
#endif
91+
}
92+
else
93+
{
94+
#ifndef ITK_WASM_NO_FILESYSTEM_IO
95+
using ParametersValueType = typename TTransform::ParametersValueType;
96+
using ReaderType = itk::TransformFileReaderTemplate<ParametersValueType>;
97+
auto reader = ReaderType::New();
98+
reader->SetFileName(input);
99+
reader->Update();
100+
auto transformList = reader->GetTransformList();
101+
if (transformList->empty())
102+
{
103+
return false;
104+
}
105+
auto transform = dynamic_cast<const TTransform *>(transformList->front().GetPointer());
106+
if (!transform)
107+
{
108+
return false;
109+
}
110+
inputTransform.Set(transform);
111+
#else
112+
return false;
113+
#endif
114+
}
115+
return true;
116+
}
117+
118+
} // end namespace wasm
119+
} // end namespace itk
120+
121+
#endif

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ itk_add_test(NAME itkPipelineTest
214214
${ITK_TEST_OUTPUT_DIR}/itkPipelineTestOutputMesh.vtk
215215
DATA{Input/cow.vtk}
216216
${ITK_TEST_OUTPUT_DIR}/itkPipelineTestOutputPolyData.vtk
217+
DATA{Input/LinearTransform.h5}
218+
# ${ITK_TEST_OUTPUT_DIR}/itkPipelineTestOutputTransform.h5
217219
)
218220

219221
itk_add_test(NAME itkPipelineMemoryIOTest

test/itkPipelineTest.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@
3131
#include "itkPolyData.h"
3232
#include "itkInputPolyData.h"
3333
#include "itkOutputPolyData.h"
34+
#include "itkInputTransform.h"
35+
#include "itkAffineTransform.h"
36+
#include "itkHDF5TransformIOFactory.h"
3437

3538
int
3639
itkPipelineTest(int argc, char * argv[])
3740
{
41+
itk::HDF5TransformIOFactory::RegisterOneFactory();
42+
3843
itk::wasm::Pipeline pipeline("pipeline-test", "A test ITK Wasm Pipeline", argc, argv);
3944
pipeline.set_version("10.8.1");
4045

@@ -58,6 +63,7 @@ itkPipelineTest(int argc, char * argv[])
5863
using ImageType = itk::Image<PixelType, Dimension>;
5964
using MeshType = itk::Mesh<PixelType, 3>;
6065
using PolyDataType = itk::PolyData<PixelType>;
66+
using TransformType = itk::AffineTransform<double, 3>;
6167

6268
using InputImageType = itk::wasm::InputImage<ImageType>;
6369
InputImageType inputImage;
@@ -96,6 +102,10 @@ itkPipelineTest(int argc, char * argv[])
96102
OutputPolyDataType outputPolyData;
97103
pipeline.add_option("output-polydata", outputPolyData, "The output polydata")->required()->type_name("OUTPUT_POLYDATA");
98104

105+
using InputTransformType = itk::wasm::InputTransform<TransformType>;
106+
InputTransformType inputTransform;
107+
pipeline.add_option("input-transform", inputTransform, "The input transform")->required()->type_name("INPUT_TRANSFORM");
108+
99109
ITK_WASM_PARSE(pipeline);
100110

101111
outputImage.Set(inputImage.Get());

0 commit comments

Comments
 (0)