~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Code/Common/include/sitkTransform.h

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
*
 
3
*  Copyright Insight Software Consortium
 
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 sitkTransform_h
 
19
#define sitkTransform_h
 
20
 
 
21
#include "sitkCommon.h"
 
22
#include "sitkExceptionObject.h"
 
23
#include "sitkImage.h"
 
24
#include <vector>
 
25
 
 
26
 
 
27
namespace itk
 
28
{
 
29
 
 
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
 
33
// differently.
 
34
#if ( ( SITK_ITK_VERSION_MAJOR == 4 ) && ( SITK_ITK_VERSION_MINOR < 5 ) )
 
35
class TransformBase;
 
36
#else
 
37
template< typename TScalar > class TransformBaseTemplate;
 
38
typedef TransformBaseTemplate<double> TransformBase;
 
39
#endif
 
40
 
 
41
template< typename TScalar, unsigned int NDimension> class CompositeTransform;
 
42
 
 
43
namespace simple
 
44
{
 
45
 
 
46
class PimpleTransformBase;
 
47
 
 
48
 
 
49
enum TransformEnum { sitkIdentity,
 
50
                     sitkTranslation,
 
51
                     sitkScale,
 
52
                     sitkScaleLogarithmic,
 
53
                     sitkEuler,
 
54
                     sitkSimilarity,
 
55
                     sitkQuaternionRigid,
 
56
                     sitkVersor,
 
57
                     sitkVersorRigid,
 
58
                     sitkScaleSkewVersor,
 
59
                     sitkAffine,
 
60
                     sitkComposite,
 
61
                     sitkDisplacementField,
 
62
                     sitkBSplineTransform
 
63
};
 
64
 
 
65
 
 
66
/** \brief A simplified wrapper around a variety of ITK transforms.
 
67
 *
 
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.
 
72
 *
 
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
 
76
 * object.
 
77
 *
 
78
 * Additionally, this class provides a basic interface to a composite
 
79
 * transforms.
 
80
 *
 
81
 * \sa itk::CompositeTransform
 
82
 */
 
83
class SITKCommon_EXPORT Transform
 
84
{
 
85
public:
 
86
  typedef Transform Self;
 
87
 
 
88
  /** \brief By default a 3-d identity transform is constructed
 
89
   */
 
90
  Transform( void );
 
91
 
 
92
  /** \brief Construct a SimpleITK Transform from a pointer to an ITK
 
93
   * composite transform.
 
94
   *
 
95
   */
 
96
  template<unsigned int NDimension>
 
97
  explicit Transform( itk::CompositeTransform< double, NDimension >* compositeTransform )
 
98
    : m_PimpleTransform( NULL )
 
99
    {
 
100
      sitkStaticAssert( NDimension == 2 || NDimension == 3, "Only 2D and 3D transforms are supported" );
 
101
      if ( compositeTransform == NULL )
 
102
        {
 
103
        sitkExceptionMacro( "Unable to construct a null transform!" );
 
104
        }
 
105
      this->InternalInitialization<NDimension>( sitkComposite, compositeTransform );
 
106
    }
 
107
 
 
108
  explicit Transform( itk::TransformBase *transform );
 
109
 
 
110
  /** \brief Construct a specific transformation
 
111
   *
 
112
   * \deprecated This constructor will be removed in future releases.
 
113
   */
 
114
  Transform( unsigned int dimensions, TransformEnum type);
 
115
 
 
116
  /** \brief Use an image to construct a transform.
 
117
   *
 
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.
 
121
   *
 
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.
 
125
   *
 
126
   * \deprecated This constructor will be removed in future releases.
 
127
   */
 
128
  Transform( Image &displacement, TransformEnum type = sitkDisplacementField );
 
129
 
 
130
  virtual ~Transform( void );
 
131
 
 
132
  /** \brief Copy constructor and assignment operator
 
133
   *
 
134
   * Performs a shallow copy of the internal ITK transform. A deep
 
135
   * copy will be done if the transform in modified.
 
136
   * @{
 
137
   */
 
138
  Transform &operator=( const Transform & );
 
139
  Transform( const Transform & );
 
140
  /**@}*/
 
141
 
 
142
 
 
143
  /** Get access to internal ITK data object.
 
144
   *
 
145
   * The return value should imediately be assigned to as
 
146
   * itk::SmartPointer.
 
147
   *
 
148
   * In many cases the value may need to be dynamically casted to
 
149
   * the the actual transform type.
 
150
   *
 
151
   * @{
 
152
   */
 
153
  itk::TransformBase* GetITKBase( void );
 
154
  const itk::TransformBase* GetITKBase( void ) const;
 
155
  /**@}*/
 
156
 
 
157
  /** Return the dimension of the Transform ( 2D or 3D )
 
158
   */
 
159
  unsigned int GetDimension( void ) const;
 
160
 
 
161
  // todo get transform type
 
162
 
 
163
  /** Set/Get Transform Parameter
 
164
   * @{
 
165
   */
 
166
  void SetParameters ( const std::vector<double>& parameters );
 
167
  std::vector<double> GetParameters( void ) const ;
 
168
  /**@}*/
 
169
 
 
170
  /** Set/Get Fixed Transform Parameter
 
171
   * @{
 
172
   */
 
173
  void SetFixedParameters ( const std::vector<double>& parameters );
 
174
  std::vector<double> GetFixedParameters( void ) const ;
 
175
  /**@}*/
 
176
 
 
177
  // Make composition
 
178
  SITK_RETURN_SELF_TYPE_HEADER AddTransform( Transform t );
 
179
 
 
180
  std::vector< double > TransformPoint( const std::vector< double > &point ) const;
 
181
 
 
182
  // write
 
183
  void WriteTransform( const std::string &filename ) const;
 
184
 
 
185
  virtual bool IsLinear() const;
 
186
 
 
187
  virtual void SetIdentity();
 
188
 
 
189
  /** \brief Try to change the current transform to it's inverse.
 
190
   *
 
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.
 
194
   *
 
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.
 
198
   */
 
199
  virtual bool SetInverse();
 
200
 
 
201
  /** \brief Return a new inverse transform of the same type as this.
 
202
   *
 
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.
 
207
   */
 
208
  Transform GetInverse() const;
 
209
 
 
210
  std::string ToString( void ) const;
 
211
 
 
212
 
 
213
  /** return user readable name for the SimpleITK transform */
 
214
  virtual std::string GetName() const;
 
215
 
 
216
  /** \brief Performs actually coping if needed to make object unique.
 
217
   *
 
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
 
221
   * object.
 
222
   */
 
223
  void MakeUnique( void );
 
224
 
 
225
protected:
 
226
 
 
227
 
 
228
  explicit Transform( PimpleTransformBase *pimpleTransform );
 
229
 
 
230
  // this method is called to set the private pimpleTransfrom outside
 
231
  // of the constructor, derived classes can override it of update the
 
232
  // state.
 
233
  virtual void SetPimpleTransform( PimpleTransformBase *pimpleTransform );
 
234
 
 
235
private:
 
236
 
 
237
  template< unsigned int VDimension>
 
238
  void InternalInitialization( TransformEnum type, itk::TransformBase *base = NULL );
 
239
 
 
240
  struct TransformTryCastVisitor
 
241
  {
 
242
    itk::TransformBase *transform;
 
243
    Transform *that;
 
244
    template< typename TransformType >
 
245
    void operator() ( void ) const
 
246
      {
 
247
        TransformType *t = dynamic_cast<TransformType*>(transform);
 
248
        if (t)
 
249
          {
 
250
          that->InternalInitialization<TransformType>(t);
 
251
          }
 
252
      }
 
253
  };
 
254
 
 
255
 
 
256
  template< class TransformType>
 
257
  void InternalInitialization( TransformType *t );
 
258
  void InternalInitialization( itk::TransformBase *base );
 
259
 
 
260
 
 
261
  template< unsigned int >
 
262
    void InternalBSplineInitialization( Image & img );
 
263
 
 
264
  template< typename TDisplacementType >
 
265
    void InternalDisplacementInitialization( Image & img );
 
266
 
 
267
  template < class TMemberFunctionPointer >
 
268
    struct DisplacementInitializationMemberFunctionAddressor
 
269
  {
 
270
    typedef typename ::detail::FunctionTraits<TMemberFunctionPointer>::ClassType ObjectType;
 
271
 
 
272
    template< typename TImageType >
 
273
    TMemberFunctionPointer operator() ( void ) const
 
274
      {
 
275
        return &ObjectType::template InternalDisplacementInitialization< TImageType >;
 
276
      }
 
277
  };
 
278
 
 
279
  // As is the architecture of all SimpleITK pimples,
 
280
  // this pointer should never be null and should always point to a
 
281
  // valid object
 
282
  PimpleTransformBase *m_PimpleTransform;
 
283
};
 
284
 
 
285
 
 
286
// read
 
287
SITKCommon_EXPORT Transform ReadTransform( const std::string &filename );
 
288
 
 
289
// write
 
290
SITKCommon_EXPORT void WriteTransform( const Transform &transform, const std::string &filename);
 
291
 
 
292
}
 
293
}
 
294
 
 
295
#endif // sitkTransform_h