ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkTransformJSON.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 itkTransformJSON_h
19#define itkTransformJSON_h
20
21#include "itkFloatTypesJSON.h"
22
23#include <vector>
24
27
28#include "glaze/glaze.hpp"
29
30namespace itk
31{
63
77
85{
88 uint64_t numberOfParameters{ 0 };
89
90 std::string name{ "Transform" };
91
92 std::string inputSpaceName;
93 std::string outputSpaceName;
94
95 std::string fixedParameters;
96 std::string parameters;
97
99};
100
107using TransformListJSON = std::list<TransformJSON>;
108
109template <typename TTransformBase>
110auto
111transformListToTransformListJSON(std::list<typename TTransformBase::ConstPointer> & transformList, bool inMemory)
113{
114 TransformListJSON transformListJSON;
115
116 std::string compositeTransformType = transformList.front()->GetTransformTypeAsString();
117 using TransformBaseType = TTransformBase;
118 using ParametersValueType = typename TransformBaseType::ParametersValueType;
120
121 using ConstTransformListType = std::list<typename TransformBaseType::ConstPointer>;
122 ConstTransformListType usedTransformList = transformList;
123
124 //
125 // if the first transform in the list is a
126 // composite transform, use its internal list
127 // instead of the IO
128 if (compositeTransformType.find("CompositeTransform") != std::string::npos)
129 {
130 usedTransformList = helper.GetTransformList(transformList.front().GetPointer());
131 }
132
133 unsigned int count = 0;
134 typename ConstTransformListType::const_iterator end = usedTransformList.end();
135 for (typename ConstTransformListType::const_iterator it = usedTransformList.begin(); it != end; ++it)
136 {
137 TransformJSON transformJSON;
138 const TransformBaseType * currentTransform = it->GetPointer();
139 const std::string transformType = currentTransform->GetTransformTypeAsString();
140 const std::string delim = "_";
141 std::vector<std::string> tokens;
142 size_t start = 0;
143 size_t end = 0;
144 while ((end = transformType.find(delim, start)) != std::string::npos)
145 {
146 tokens.push_back(transformType.substr(start, end - start));
147 start = end + delim.length();
148 }
149 tokens.push_back(transformType.substr(start));
150 const std::string pString = tokens[0];
151 if (pString == "CompositeTransform")
152 {
154 }
155 else if (pString == "IdentityTransform")
156 {
158 }
159 else if (pString == "TranslationTransform")
160 {
162 }
163 else if (pString == "Euler2DTransform")
164 {
166 }
167 else if (pString == "Euler3DTransform")
168 {
170 }
171 else if (pString == "Rigid2DTransform")
172 {
174 }
175 else if (pString == "Rigid3DTransform")
176 {
178 }
179 else if (pString == "Rigid3DPerspectiveTransform")
180 {
182 }
183 else if (pString == "VersorRigid3DTransform")
184 {
186 }
187 else if (pString == "Versor")
188 {
190 }
191 else if (pString == "ScaleTransform")
192 {
194 }
195 else if (pString == "ScaleLogarithmicTransform")
196 {
198 }
199 else if (pString == "ScaleSkewVersor3DTransform")
200 {
202 }
203 else if (pString == "Similarity2DTransform")
204 {
206 }
207 else if (pString == "Similarity3DTransform")
208 {
210 }
211 else if (pString == "QuaternionRigidTransform")
212 {
214 }
215 else if (pString == "AffineTransform")
216 {
218 }
219 else if (pString == "ScalableAffineTransform")
220 {
222 }
223 else if (pString == "AzimuthElevationToCartesianTransform")
224 {
227 }
228 else if (pString == "BSplineTransform")
229 {
231 }
232 else if (pString == "BSplineSmoothingOnUpdateDisplacementFieldTransform")
233 {
236 }
237 else if (pString == "ConstantVelocityFieldTransform")
238 {
240 }
241 else if (pString == "DisplacementFieldTransform")
242 {
244 }
245 else if (pString == "GaussianExponentialDiffeomorphicTransform")
246 {
249 }
250 else if (pString == "GaussianSmoothingOnUpdateDisplacementFieldTransform")
251 {
254 }
255 else if (pString == "GaussianSmoothingOnUpdateTimeVaryingVelocityFieldTransform")
256 {
259 }
260 else if (pString == "TimeVaryingVelocityFieldTransform")
261 {
264 }
265 else if (pString == "VelocityFieldTransform")
266 {
268 }
269 else
270 {
271 throw std::logic_error("Unknown transform type: " + pString);
272 }
273
274 constexpr size_t parametersSize = sizeof(ParametersValueType);
275 if (parametersSize == 4)
276 {
278 }
279 else if (parametersSize == 8)
280 {
282 }
283 else
284 {
285 throw std::logic_error("Unknown parameters value type");
286 }
287
288 transformJSON.transformType.inputDimension = currentTransform->GetInputSpaceDimension();
289 transformJSON.transformType.outputDimension = currentTransform->GetOutputSpaceDimension();
290
291 transformJSON.numberOfFixedParameters = currentTransform->GetFixedParameters().Size();
292 transformJSON.numberOfParameters = currentTransform->GetParameters().Size();
293 transformJSON.name = currentTransform->GetObjectName();
294 // Todo: needs to be pushed from itk::Transform to itk::TransformBase
295 // Available in ITK 5.4.1 and later
296 // https://github.com/InsightSoftwareConsortium/ITK/pull/4734
297 // transformJSON.inputSpaceName = currentTransform->GetInputSpaceName();
298 // transformJSON.inputSpaceName = currentTransform->GetOutputSpaceName();
299 if (inMemory)
300 {
301 if (pString == "CompositeTransform")
302 {
303 // For composite transforms, we don't store the parameters in memory directly
304 transformJSON.fixedParameters = "data:application/vnd.itk.address,0:0";
305 transformJSON.parameters = "data:application/vnd.itk.address,0:0";
306 }
307 else
308 {
309 std::ostringstream fixedParametersStream;
310 fixedParametersStream << "data:application/vnd.itk.address,0:";
311 const auto fixedParametersAddr = reinterpret_cast<size_t>(currentTransform->GetFixedParameters().data_block());
312 fixedParametersStream << fixedParametersAddr;
313 transformJSON.fixedParameters = fixedParametersStream.str();
314
315 std::ostringstream parametersStream;
316 parametersStream << "data:application/vnd.itk.address,0:";
317 const auto parametersAddr = reinterpret_cast<size_t>(currentTransform->GetParameters().data_block());
318 parametersStream << parametersAddr;
319 transformJSON.parameters = parametersStream.str();
320 }
321 }
322 else
323 {
324 transformJSON.fixedParameters =
325 "data:application/vnd.itk.path,data/" + std::to_string(count) + "/fixed-parameters.raw";
326 transformJSON.parameters = "data:application/vnd.itk.path,data/" + std::to_string(count) + "/parameters.raw";
327 }
328
329 auto dictionary = currentTransform->GetMetaDataDictionary();
330 metaDataDictionaryToJSON(dictionary, transformJSON.metadata);
331
332 transformListJSON.push_back(transformJSON);
333 ++count;
334 }
335
336 return transformListJSON;
337}
338
339} // end namespace itk
340
341template <>
342struct glz::meta<itk::JSONTransformParameterizationEnum>
343{
345 static constexpr auto value = glz::enumerate(Composite,
346 Identity,
347 Translation,
348 Euler2D,
349 Euler3D,
350 Rigid2D,
351 Rigid3D,
352 Rigid3DPerspective,
353 Versor,
354 VersorRigid3D,
355 ScaleLogarithmic,
356 ScaleSkewVersor3D,
357 Scale,
358 Similarity2D,
359 Similarity3D,
360 QuaternionRigid,
361 Affine,
362 ScalableAffine,
363 AzimuthElevationToCartesian,
364 BSpline,
365 BSplineSmoothingOnUpdateDisplacementField,
366 ConstantVelocityField,
367 DisplacementField,
368 GaussianSmoothingOnUpdateDisplacementField,
369 GaussianExponentialDiffeomorphic,
370 VelocityField,
371 TimeVaryingVelocityField,
372 GaussianSmoothingOnUpdateTimeVaryingVelocityField);
373};
374
375#endif // itkTransformJSON_h
An adapter that adapts CompositeTransform into the TransformReader/Writer.
ConstTransformListType & GetTransformList(const TransformType *transform)
A templated class holding a unit quaternion.
Definition itkVersor.h:53
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
WebAssemblyInterface_EXPORT void metaDataDictionaryToJSON(const itk::MetaDataDictionary &dictionary, MetadataJSON &metadataJson)
std::list< TransformJSON > TransformListJSON
std::vector< MetadataEntryJSON > MetadataJSON
JSONTransformParameterizationEnum
auto transformListToTransformListJSON(std::list< typename TTransformBase::ConstPointer > &transformList, bool inMemory) -> TransformListJSON
Transform JSON representation data structure.
std::string inputSpaceName
uint64_t numberOfFixedParameters
TransformTypeJSON transformType
std::string outputSpaceName
std::string fixedParameters
Transform type JSON representation data structure.
JSONFloatTypesEnum parametersValueType
JSONTransformParameterizationEnum transformParameterization