1
/*=========================================================================
3
* Copyright Insight Software Consortium
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
9
* http://www.apache.org/licenses/LICENSE-2.0.txt
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.
17
*=========================================================================*/
18
#ifndef sitkTransform_h
19
#define sitkTransform_h
21
#include "sitkCommon.h"
22
#include "sitkExceptionObject.h"
23
#include "sitkImage.h"
30
// Forward declaration for pointer
31
// After ITK_VERSION 4.5 (Acutally after June 20th, 2013) the ITK Transform
32
// classes are now templated. This requires forward declarations to be defined
34
#if ( ( SITK_ITK_VERSION_MAJOR == 4 ) && ( SITK_ITK_VERSION_MINOR < 5 ) )
37
template< typename TScalar > class TransformBaseTemplate;
38
typedef TransformBaseTemplate<double> TransformBase;
41
template< typename TScalar, unsigned int NDimension> class CompositeTransform;
46
class PimpleTransformBase;
49
enum TransformEnum { sitkIdentity,
61
sitkDisplacementField,
66
/** \brief A simplified wrapper around a variety of ITK transforms.
68
* The interface to ITK transform objects to be used with the
69
* ImageRegistrationMethod, ResampleImageFilter and other SimpleITK
70
* process objects. The transforms are designed to have a serialized
71
* array of parameters to facilitate optimization for registration.
73
* Provides a base class interface to any type of ITK
74
* transform. Objects of this type may have their interface converted
75
* to a derived interface while keeping the same reference to the ITK
78
* Additionally, this class provides a basic interface to a composite
81
* \sa itk::CompositeTransform
83
class SITKCommon_EXPORT Transform
86
typedef Transform Self;
88
/** \brief By default a 3-d identity transform is constructed
92
/** \brief Construct a SimpleITK Transform from a pointer to an ITK
93
* composite transform.
96
template<unsigned int NDimension>
97
explicit Transform( itk::CompositeTransform< double, NDimension >* compositeTransform )
98
: m_PimpleTransform( NULL )
100
sitkStaticAssert( NDimension == 2 || NDimension == 3, "Only 2D and 3D transforms are supported" );
101
if ( compositeTransform == NULL )
103
sitkExceptionMacro( "Unable to construct a null transform!" );
105
this->InternalInitialization<NDimension>( sitkComposite, compositeTransform );
108
explicit Transform( itk::TransformBase *transform );
110
/** \brief Construct a specific transformation
112
* \deprecated This constructor will be removed in future releases.
114
Transform( unsigned int dimensions, TransformEnum type);
116
/** \brief Use an image to construct a transform.
118
* The input displacement image is transferred to the constructed
119
* transform object. The input image is modified to be a default
120
* constructed Image object.
122
* Only the sitkDisplacementField transformation type can currently
123
* be constructed this way. Image must be of sitkVectorFloat64 pixel
124
* type with the number of components equal to the image dimension.
126
* \deprecated This constructor will be removed in future releases.
128
Transform( Image &displacement, TransformEnum type = sitkDisplacementField );
130
virtual ~Transform( void );
132
/** \brief Copy constructor and assignment operator
134
* Performs a shallow copy of the internal ITK transform. A deep
135
* copy will be done if the transform in modified.
138
Transform &operator=( const Transform & );
139
Transform( const Transform & );
143
/** Get access to internal ITK data object.
145
* The return value should imediately be assigned to as
148
* In many cases the value may need to be dynamically casted to
149
* the the actual transform type.
153
itk::TransformBase* GetITKBase( void );
154
const itk::TransformBase* GetITKBase( void ) const;
157
/** Return the dimension of the Transform ( 2D or 3D )
159
unsigned int GetDimension( void ) const;
161
// todo get transform type
163
/** Set/Get Transform Parameter
166
void SetParameters ( const std::vector<double>& parameters );
167
std::vector<double> GetParameters( void ) const ;
170
/** Set/Get Fixed Transform Parameter
173
void SetFixedParameters ( const std::vector<double>& parameters );
174
std::vector<double> GetFixedParameters( void ) const ;
178
SITK_RETURN_SELF_TYPE_HEADER AddTransform( Transform t );
180
std::vector< double > TransformPoint( const std::vector< double > &point ) const;
183
void WriteTransform( const std::string &filename ) const;
185
virtual bool IsLinear() const;
187
virtual void SetIdentity();
189
/** \brief Try to change the current transform to it's inverse.
191
* If the transform has an inverse, i.e. non-singular linear
192
* transforms, then a new ITK transform is created of the same type
193
* and this object is set to it.
195
* However not all transform have a direct inverse, if the inverse
196
* does not exist or fails false will be returned and this transform
197
* will not be modified.
199
virtual bool SetInverse();
201
/** \brief Return a new inverse transform of the same type as this.
203
* Creates a new transform object and tries to set the value to the
204
* inverse. As not all transform types have inverse and some
205
* transforms are not invertable, an exception will be throw is
206
* there is no inverse.
208
Transform GetInverse() const;
210
std::string ToString( void ) const;
213
/** return user readable name for the SimpleITK transform */
214
virtual std::string GetName() const;
216
/** \brief Performs actually coping if needed to make object unique.
218
* The Transform class by default performs lazy coping and
219
* assignment. This method make sure that coping actually happens
220
* to the itk::Transform pointed to is only pointed to by this
223
void MakeUnique( void );
228
explicit Transform( PimpleTransformBase *pimpleTransform );
230
// this method is called to set the private pimpleTransfrom outside
231
// of the constructor, derived classes can override it of update the
233
virtual void SetPimpleTransform( PimpleTransformBase *pimpleTransform );
237
template< unsigned int VDimension>
238
void InternalInitialization( TransformEnum type, itk::TransformBase *base = NULL );
240
struct TransformTryCastVisitor
242
itk::TransformBase *transform;
244
template< typename TransformType >
245
void operator() ( void ) const
247
TransformType *t = dynamic_cast<TransformType*>(transform);
250
that->InternalInitialization<TransformType>(t);
256
template< class TransformType>
257
void InternalInitialization( TransformType *t );
258
void InternalInitialization( itk::TransformBase *base );
261
template< unsigned int >
262
void InternalBSplineInitialization( Image & img );
264
template< typename TDisplacementType >
265
void InternalDisplacementInitialization( Image & img );
267
template < class TMemberFunctionPointer >
268
struct DisplacementInitializationMemberFunctionAddressor
270
typedef typename ::detail::FunctionTraits<TMemberFunctionPointer>::ClassType ObjectType;
272
template< typename TImageType >
273
TMemberFunctionPointer operator() ( void ) const
275
return &ObjectType::template InternalDisplacementInitialization< TImageType >;
279
// As is the architecture of all SimpleITK pimples,
280
// this pointer should never be null and should always point to a
282
PimpleTransformBase *m_PimpleTransform;
287
SITKCommon_EXPORT Transform ReadTransform( const std::string &filename );
290
SITKCommon_EXPORT void WriteTransform( const Transform &transform, const std::string &filename);
295
#endif // sitkTransform_h