ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkOutputTransform.h
Go to the documentation of this file.
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 itkOutputTransform_h
19#define itkOutputTransform_h
20
21#include "itkPipeline.h"
22
23#ifndef ITK_WASM_NO_MEMORY_IO
24# include "itkWasmExports.h"
25# include "itkWasmTransform.h"
29#endif
30#ifndef ITK_WASM_NO_FILESYSTEM_IO
32#endif
33
34namespace itk
35{
36namespace wasm
37{
48template <typename TTransform>
49class ITK_TEMPLATE_EXPORT OutputTransform
50{
51public:
52 using TransformType = TTransform;
53
54 void
55 Set(const TransformType * transform)
56 {
57 this->m_Transform = transform;
58 }
59
60 const TransformType *
61 Get() const
62 {
63 return this->m_Transform.GetPointer();
64 }
65
67 void
68 SetIdentifier(const std::string & identifier)
69 {
70 this->m_Identifier = identifier;
71 }
72 const std::string &
74 {
75 return this->m_Identifier;
76 }
77
78 OutputTransform() = default;
80 {
82 {
83#ifndef ITK_WASM_NO_MEMORY_IO
84 if (!this->m_Transform.IsNull() && !this->m_Identifier.empty())
85 {
86 using TransformToWasmTransformFilterType = TransformToWasmTransformFilter<TransformType>;
87 auto transformToWasmTransformFilter = TransformToWasmTransformFilterType::New();
88 transformToWasmTransformFilter->SetTransform(this->m_Transform.GetPointer());
89 transformToWasmTransformFilter->Update();
90 auto wasmTransform = transformToWasmTransformFilter->GetOutput();
91 const auto index = std::stoi(this->m_Identifier);
92 setMemoryStoreOutputDataObject(0, index, wasmTransform);
93
94 using ParametersValueType = typename TransformType::ParametersValueType;
95 using FixedParametersValueType = typename TransformType::FixedParametersValueType;
97 auto compositeTransform = dynamic_cast<const CompositeTransformType *>(wasmTransform->GetTransform());
98 if (compositeTransform)
99 {
100 unsigned int dataCount = 0;
102 auto transformList = helper.GetTransformList(compositeTransform);
103 // composite transform
104 transformList.pop_front();
105 // iterate through the transform list and store each transfrom's fixed parameters and parameters
106 for (const auto & transform : transformList)
107 {
108 const auto fixedParamsAddress = reinterpret_cast<size_t>(transform->GetFixedParameters().data_block());
109 const auto fixedParamsSize = transform->GetFixedParameters().Size() * sizeof(FixedParametersValueType);
110 setMemoryStoreOutputArray(0, index, dataCount, fixedParamsAddress, fixedParamsSize);
111 ++dataCount;
112
113 const auto paramsAddress = reinterpret_cast<size_t>(transform->GetParameters().data_block());
114 const auto paramsSize = transform->GetParameters().Size() * sizeof(ParametersValueType);
115 setMemoryStoreOutputArray(0, index, dataCount, paramsAddress, paramsSize);
116 ++dataCount;
117 }
118 }
119 else
120 {
121 const auto fixedParamsAddress =
122 reinterpret_cast<size_t>(wasmTransform->GetTransform()->GetFixedParameters().data_block());
123 const auto fixedParamsSize =
124 wasmTransform->GetTransform()->GetFixedParameters().Size() * sizeof(FixedParametersValueType);
125 setMemoryStoreOutputArray(0, index, 0, fixedParamsAddress, fixedParamsSize);
126
127 const auto paramsAddress =
128 reinterpret_cast<size_t>(wasmTransform->GetTransform()->GetParameters().data_block());
129 const auto paramsSize = wasmTransform->GetTransform()->GetParameters().Size() * sizeof(ParametersValueType);
130 setMemoryStoreOutputArray(0, index, 1, paramsAddress, paramsSize);
131 }
132 }
133#else
134 std::cerr << "Memory IO not supported" << std::endl;
135 abort();
136#endif
137 }
138 else
139 {
140#ifndef ITK_WASM_NO_FILESYSTEM_IO
141 if (!this->m_Transform.IsNull() && !this->m_Identifier.empty())
142 {
143 using ParametersValueType = typename TransformType::ParametersValueType;
145 auto writer = WriterType::New();
146 writer->SetFileName(this->m_Identifier);
147 writer->SetInput(this->m_Transform);
148 writer->Update();
149 }
150#else
151 std::cerr << "Filesystem IO not supported" << std::endl;
152 abort();
153#endif
154 }
155 }
156
157protected:
158 typename TTransform::ConstPointer m_Transform;
159
160 std::string m_Identifier;
161};
162
163template <typename TTransform>
164bool
165lexical_cast(const std::string & input, OutputTransform<TTransform> & outputTransform)
166{
167 outputTransform.SetIdentifier(input);
168 return true;
169}
170
171} // namespace wasm
172} // namespace itk
173
174#endif
An adapter that adapts CompositeTransform into the TransformReader/Writer.
ConstTransformListType & GetTransformList(const TransformType *transform)
This class contains a list of transforms and concatenates them by composition.
Convert an Transform to an WasmTransform object.
Output transform for an itk::wasm::Pipeline.
TTransform::ConstPointer m_Transform
const TransformType * Get() const
void SetIdentifier(const std::string &identifier)
const std::string & GetIdentifier() const
void Set(const TransformType *transform)
static auto get_use_memory_io()
WebAssemblyInterface_EXPORT void setMemoryStoreOutputDataObject(uint32_t memoryIndex, uint32_t index, const WasmDataObject *dataObject)
WebAssemblyInterface_EXPORT void setMemoryStoreOutputArray(uint32_t memoryIndex, uint32_t index, uint32_t subIndex, vcl_size_t address, vcl_size_t size)
bool lexical_cast(const std::string &input, InputImage< TImage > &inputImage)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....