go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
elxTransformIO.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright UMC Utrecht and contributors
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 * http://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 elxTransformIO_h
19#define elxTransformIO_h
20
23
24#include <itkCompositeTransform.h>
25#include <itkTransform.h>
26#include <itkTransformBase.h>
27
28#include <cassert>
29#include <string>
30
31namespace elastix
32{
33class BaseComponent;
34class Configuration;
35
37{
38public:
40 GetParameters(const bool fixed, const itk::TransformBase & transform)
41 {
42 return fixed ? transform.GetFixedParameters() : transform.GetParameters();
43 }
44
45 static void
46 SetParameters(const bool fixed, itk::TransformBase & transform, const itk::OptimizerParameters<double> & parameters)
47 {
48 fixed ? transform.SetFixedParameters(parameters) : transform.SetParameters(parameters);
49 }
50
51
55 static std::string
56 ConvertITKNameOfClassToElastixClassName(const std::string & itkNameOfClass);
57
58
61 template <unsigned NDimension>
62 static itk::SmartPointer<itk::CompositeTransform<double, NDimension>>
64 const itk::AdvancedCombinationTransform<double, NDimension> & advancedCombinationTransform)
65 {
66 const auto numberOfTransforms = advancedCombinationTransform.GetNumberOfTransforms();
67
68 if ((numberOfTransforms > 1) && (!advancedCombinationTransform.GetUseComposition()))
69 {
70 // A combination of multiple transforms can only be converted to CompositeTransform when the original combination
71 // uses composition.
72 return nullptr;
73 }
74
75 const auto compositeTransform = itk::CompositeTransform<double, NDimension>::New();
76
77 for (itk::SizeValueType n{}; n < numberOfTransforms; ++n)
78 {
79 const auto nthTransform = advancedCombinationTransform.GetNthTransform(n);
80 const auto singleItkTransform = ConvertToSingleItkTransform(*nthTransform);
81 compositeTransform->AddTransform((singleItkTransform == nullptr) ? nthTransform : singleItkTransform);
82 }
83 return compositeTransform;
84 }
85
86
90 template <unsigned NDimension>
91 static itk::SmartPointer<itk::CompositeTransform<double, NDimension>>
93 const itk::AdvancedCombinationTransform<double, NDimension> & advancedCombinationTransform)
94 {
95 const auto numberOfTransforms = advancedCombinationTransform.GetNumberOfTransforms();
96
97 if ((numberOfTransforms > 1) && (!advancedCombinationTransform.GetUseComposition()))
98 {
99 // A combination of multiple transforms can only be converted to CompositeTransform when the original combination
100 // uses composition.
101 return nullptr;
102 }
103
104 const auto compositeTransform = itk::CompositeTransform<double, NDimension>::New();
105
106 for (itk::SizeValueType n{}; n < numberOfTransforms; ++n)
107 {
108 const auto nthTransform = advancedCombinationTransform.GetNthTransform(n);
109 const auto singleItkTransform = ConvertToSingleItkTransform(*nthTransform);
110
111 if (singleItkTransform == nullptr)
112 {
113 return nullptr;
114 }
115 compositeTransform->AddTransform(singleItkTransform);
116 }
117 return compositeTransform;
118 }
119
120
123 template <unsigned NDimension>
124 static itk::SmartPointer<itk::Transform<double, NDimension, NDimension>>
125 ConvertToSingleItkTransform(const itk::Transform<double, NDimension, NDimension> & elxTransform)
126 {
127 // Do not use this function for elastix combination transforms!
128 using CombinationTransformType = itk::AdvancedCombinationTransform<double, NDimension>;
129 assert(dynamic_cast<const CombinationTransformType *>(&elxTransform) == nullptr);
130
131 if (const auto transformAdapter = dynamic_cast<const AdvancedTransformAdapter<double, NDimension> *>(&elxTransform))
132 {
133 return transformAdapter->GetModifiableExternalTransform();
134 }
135 return dynamic_cast<itk::Transform<double, NDimension, NDimension> *>(
136 ConvertItkTransformBaseToSingleItkTransform(elxTransform).GetPointer());
137 }
138
139
144 static void
145 Write(const itk::Object & itkTransform, const std::string & fileName);
146
147 static itk::TransformBase::Pointer
148 Read(const std::string & fileName);
149
151 template <typename TElastixTransform>
152 static std::string
153 MakeDeformationFieldFileName(const TElastixTransform & elxTransform)
154 {
155 return MakeDeformationFieldFileName(*(elxTransform.GetConfiguration()),
156 elxTransform.GetElastix()->GetCurrentTransformParameterFileName());
157 }
158
159private:
160 static itk::TransformBase::Pointer
161 ConvertItkTransformBaseToSingleItkTransform(const itk::TransformBase & elxTransform);
162
163 static std::string
164 MakeDeformationFieldFileName(const Configuration & configuration, const std::string & transformParameterFileName);
165};
166} // namespace elastix
167
168#endif
Adapts the ITK transform that is specified by AdvancedTransformAdapter::SetExternalTransform to the e...
A class that deals with user given parameters and command line arguments.
static itk::SmartPointer< itk::Transform< double, NDimension, NDimension > > ConvertToSingleItkTransform(const itk::Transform< double, NDimension, NDimension > &elxTransform)
static std::string MakeDeformationFieldFileName(const TElastixTransform &elxTransform)
Makes the deformation field file name, as used by BSplineTransformWithDiffusion and DeformationFieldT...
static void Write(const itk::Object &itkTransform, const std::string &fileName)
static std::string MakeDeformationFieldFileName(const Configuration &configuration, const std::string &transformParameterFileName)
static itk::SmartPointer< itk::CompositeTransform< double, NDimension > > ConvertToCompositionOfItkTransforms(const itk::AdvancedCombinationTransform< double, NDimension > &advancedCombinationTransform)
static itk::TransformBase::Pointer Read(const std::string &fileName)
static itk::SmartPointer< itk::CompositeTransform< double, NDimension > > ConvertToItkCompositeTransform(const itk::AdvancedCombinationTransform< double, NDimension > &advancedCombinationTransform)
static void SetParameters(const bool fixed, itk::TransformBase &transform, const itk::OptimizerParameters< double > &parameters)
static itk::OptimizerParameters< double > GetParameters(const bool fixed, const itk::TransformBase &transform)
static std::string ConvertITKNameOfClassToElastixClassName(const std::string &itkNameOfClass)
static itk::TransformBase::Pointer ConvertItkTransformBaseToSingleItkTransform(const itk::TransformBase &elxTransform)
This class combines two transforms: an 'initial transform' with a 'current transform'.
SizeValueType GetNumberOfTransforms() const
const TransformTypePointer GetNthTransform(SizeValueType n) const
virtual bool GetUseComposition() const
class ITK_TEMPLATE_EXPORT OptimizerParameters


Generated on 2024-07-17 for elastix by doxygen 1.11.0 (9b424b03c9833626cd435af22a444888fbbb192d) elastix logo