Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTransformFactory.txx
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkTransformFactory.h"
18 #include "mitkTransformParameters.h"
19 
20 #include "itkAffineTransform.h"
21 #include "itkAzimuthElevationToCartesianTransform.h"
22 #include "itkCenteredAffineTransform.h"
23 #include "itkCenteredTransformInitializer.h"
24 #include "itkFixedCenterOfRotationAffineTransform.h"
25 #include "itkScaleLogarithmicTransform.h"
26 #include "itkScaleTransform.h"
27 #include "itkTranslationTransform.h"
28 #include <itkCenteredEuler3DTransform.h>
29 #include <itkCenteredRigid2DTransform.h>
30 #include <itkCenteredSimilarity2DTransform.h>
31 #include <itkEuler2DTransform.h>
32 #include <itkEuler3DTransform.h>
33 #include <itkQuaternionRigidTransform.h>
34 #include <itkRigid2DTransform.h>
35 #include <itkRigid3DTransform.h>
36 #include <itkScaleSkewVersor3DTransform.h>
37 #include <itkSimilarity2DTransform.h>
38 #include <itkSimilarity3DTransform.h>
39 #include <itkVersorRigid3DTransform.h>
40 #include <itkVersorTransform.h>
41 
42 namespace mitk
43 {
44  template <class TPixelType, unsigned int VImageDimension>
45  TransformFactory<TPixelType, VImageDimension>::TransformFactory()
46  : m_TransformParameters(NULL),
47  m_FixedImage(NULL),
48  m_MovingImage(NULL),
49  m_FixedImage2D(NULL),
50  m_MovingImage2D(NULL),
51  m_FixedImage3D(NULL),
52  m_MovingImage3D(NULL)
53  {
54  }
55 
56  template <class TPixelType, unsigned int VImageDimension>
57  void TransformFactory<TPixelType, VImageDimension>::SetFixedImage(FixedImageType *fixed)
58  {
59  if (VImageDimension == 2)
60  {
61  m_FixedImage2D = reinterpret_cast<FixedImage2DType *>(fixed);
62  m_FixedImage = fixed;
63  }
64  else
65  {
66  m_FixedImage3D = reinterpret_cast<FixedImage3DType *>(fixed);
67  ;
68  m_FixedImage = fixed;
69  }
70  }
71 
72  template <class TPixelType, unsigned int VImageDimension>
73  void TransformFactory<TPixelType, VImageDimension>::SetMovingImage(MovingImageType *moving)
74  {
75  if (VImageDimension == 2)
76  {
77  m_MovingImage2D = reinterpret_cast<MovingImage2DType *>(moving);
78  m_MovingImage = moving;
79  }
80  else
81  {
82  m_MovingImage3D = reinterpret_cast<MovingImage3DType *>(moving);
83  m_MovingImage = moving;
84  }
85  }
86 
87  /*template < class TPixelType, unsigned int VImageDimension >
88  typename ::itk::Transform< double, VImageDimension, VImageDimension >::Pointer
89  MakeRigidTransform();
90 
91  template <class TPixelType>
92  typename ::itk::Transform< double, 3, 3 >::Pointer
93  MakeRigidTransform<TPixelType, 3>()
94  {
95  itk::Rigid3DTransform< double >::Pointer transform = itk::Rigid3DTransform< double >::New();
96  transform->SetIdentity();
97  return transform.GetPointer();
98  }
99 
100  template <class TPixelType>
101  typename ::itk::Transform< double, 2, 2 >::Pointer
102  MakeRigidTransform<TPixelType, 2>()
103  {
104  return NULL;
105  }*/
106 
107  /*template < unsigned int VImageDimension >
108  typename ::itk::Transform< double, VImageDimension, VImageDimension >::Pointer
109  MakeRigidTransform();
110 
111  template <>
112  typename ::itk::Transform< double, 3, 3 >::Pointer
113  MakeRigidTransform<3>()
114  {
115  itk::Rigid3DTransform< double >::Pointer transform = itk::Rigid3DTransform< double >::New();
116  transform->SetIdentity();
117  return transform.GetPointer();
118  }
119 
120  template <>
121  typename ::itk::Transform< double, 2, 2 >::Pointer
122  MakeRigidTransform<2>()
123  {
124  return NULL;
125  }*/
126  //
127  // template<class T, class U, int I> class X
128  // { void f() { cout << "Primary template" << endl; } };
129  // template<class T> class X<int, T*, 10>
130  // { void f() { cout << "Partial specialization 3" << endl;
131  // } };
132  //
133  //
134  // template < class TPixelType > class TransformFactory<TPixelType, 3>
135  //{
136  // typedef typename itk::Transform< double, 3, 3 > TransformType;
137  // typedef typename TransformType::Pointer TransformPointer;
138  //
139  // typename TransformPointer MakeRigidTransform()
140  // {
141  // }
142  //};
143 
144  // template <>
145  // typename TransformFactory< double, 3 >::TransformPointer
146  // TransformFactory< double, 3 >::MakeRigidTransform()
147  //{
148  // itk::Rigid3DTransform< double >::Pointer transform = itk::Rigid3DTransform< double >::New();
149  // transform->SetIdentity();
150  // return transform.GetPointer();
151  //}
152 
153  // template <>
154  // typename TransformFactory< double, 2 >::TransformPointer
155  // TransformFactory< double, 2 >::MakeRigidTransform()
156  //{
157  // return NULL;
158  //}
159 
160  template <class TPixelType, unsigned int VImageDimension>
161  typename TransformFactory<TPixelType, VImageDimension>::TransformPointer
162  TransformFactory<TPixelType, VImageDimension>::GetTransform()
163  {
164  int transform = m_TransformParameters->GetTransform();
165  if (transform == TransformParameters::TRANSLATIONTRANSFORM)
166  {
167  typename itk::TranslationTransform<double, VImageDimension>::Pointer transformPointer =
168  itk::TranslationTransform<double, VImageDimension>::New();
169  transformPointer->SetIdentity();
170  return transformPointer.GetPointer();
171  }
172  else if (transform == TransformParameters::SCALETRANSFORM)
173  {
174  typename itk::ScaleTransform<double, VImageDimension>::Pointer transformPointer =
175  itk::ScaleTransform<double, VImageDimension>::New();
176  transformPointer->SetIdentity();
177  return transformPointer.GetPointer();
178  }
179  else if (transform == TransformParameters::SCALELOGARITHMICTRANSFORM)
180  {
181  typename itk::ScaleLogarithmicTransform<double, VImageDimension>::Pointer transformPointer =
182  itk::ScaleLogarithmicTransform<double, VImageDimension>::New();
183  transformPointer->SetIdentity();
184  return transformPointer.GetPointer();
185  }
186  else if (transform == TransformParameters::AFFINETRANSFORM)
187  {
188  typename itk::AffineTransform<double, VImageDimension>::Pointer transformPointer =
189  itk::AffineTransform<double, VImageDimension>::New();
190  transformPointer->SetIdentity();
191  if (m_TransformParameters->GetTransformInitializerOn())
192  {
193  typedef typename itk::AffineTransform<double, VImageDimension> AffineTransformType;
194  typedef typename itk::CenteredTransformInitializer<AffineTransformType, FixedImageType, MovingImageType>
195  TransformInitializerType;
196  typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
197  transformInitializer->SetFixedImage(m_FixedImage);
198  transformInitializer->SetMovingImage(m_MovingImage);
199  transformInitializer->SetTransform(transformPointer);
200  if (m_TransformParameters->GetMomentsOn())
201  {
202  transformInitializer->MomentsOn();
203  }
204  else
205  {
206  transformInitializer->GeometryOn();
207  }
208  transformInitializer->InitializeTransform();
209  }
210  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
211  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
212  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
213  return transformPointer.GetPointer();
214  }
215  else if (transform == TransformParameters::FIXEDCENTEROFROTATIONAFFINETRANSFORM)
216  {
217  typedef typename itk::FixedCenterOfRotationAffineTransform<double, VImageDimension> CenteredAffineTransformType;
218  typename itk::FixedCenterOfRotationAffineTransform<double, VImageDimension>::Pointer transformPointer =
219  itk::FixedCenterOfRotationAffineTransform<double, VImageDimension>::New();
220  transformPointer->SetIdentity();
221  if (m_TransformParameters->GetTransformInitializerOn())
222  {
223  typedef typename itk::FixedCenterOfRotationAffineTransform<double, VImageDimension>
224  FixedCenterOfRotationAffineTransformType;
225  typedef typename itk::
226  CenteredTransformInitializer<FixedCenterOfRotationAffineTransformType, FixedImageType, MovingImageType>
227  TransformInitializerType;
228  typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
229  transformInitializer->SetFixedImage(m_FixedImage);
230  transformInitializer->SetMovingImage(m_MovingImage);
231  transformInitializer->SetTransform(transformPointer);
232  if (m_TransformParameters->GetMomentsOn())
233  {
234  transformInitializer->MomentsOn();
235  }
236  else
237  {
238  transformInitializer->GeometryOn();
239  }
240  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
241  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
242  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
243  transformInitializer->InitializeTransform();
244  }
245  return transformPointer.GetPointer();
246  }
247  // TODO remove rigid3dTransform
248  // else if (transform == TransformParameters::RIGID3DTRANSFORM)
249  // {
250  // //return MakeRigidTransform<VImageDimension>();
251  // if (VImageDimension == 3)
252  // {
253  // typename itk::Rigid3DTransform< double >::Pointer transformPointer = itk::Rigid3DTransform< double
254  // >::New();
255  // transformPointer->SetIdentity();
256  // m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
257  // m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
258  // m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
259  // return reinterpret_cast<TransformType*>(transformPointer.GetPointer());
260  // }
261  // }
262  else if (transform == TransformParameters::EULER3DTRANSFORM)
263  {
264  if (VImageDimension == 3)
265  {
266  typename itk::Euler3DTransform<double>::Pointer transformPointer = itk::Euler3DTransform<double>::New();
267  transformPointer->SetIdentity();
268  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
269  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
270  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
271  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
272  }
273  }
274  else if (transform == TransformParameters::CENTEREDEULER3DTRANSFORM)
275  {
276  if (VImageDimension == 3)
277  {
278  typename itk::CenteredEuler3DTransform<double>::Pointer transformPointer =
279  itk::CenteredEuler3DTransform<double>::New();
280  transformPointer->SetIdentity();
281  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
282  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
283  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
284  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
285  }
286  }
287  else if (transform == TransformParameters::QUATERNIONRIGIDTRANSFORM)
288  {
289  if (VImageDimension == 3)
290  {
291  typename itk::QuaternionRigidTransform<double>::Pointer transformPointer =
292  itk::QuaternionRigidTransform<double>::New();
293  transformPointer->SetIdentity();
294  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
295  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
296  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
297  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
298  }
299  }
300  else if (transform == TransformParameters::VERSORTRANSFORM)
301  {
302  if (VImageDimension == 3)
303  {
304  typename itk::VersorTransform<double>::Pointer transformPointer = itk::VersorTransform<double>::New();
305  transformPointer->SetIdentity();
306  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
307  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
308  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
309  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
310  }
311  }
312  else if (transform == TransformParameters::VERSORRIGID3DTRANSFORM)
313  {
314  if (VImageDimension == 3)
315  {
316  typename itk::VersorRigid3DTransform<double>::Pointer transformPointer =
317  itk::VersorRigid3DTransform<double>::New();
318  transformPointer->SetIdentity();
319  typedef typename itk::VersorRigid3DTransform<double> VersorRigid3DTransformType;
320  if (m_TransformParameters->GetTransformInitializerOn())
321  {
322  typedef
323  typename itk::CenteredTransformInitializer<VersorRigid3DTransformType, FixedImage3DType, MovingImage3DType>
324  TransformInitializerType;
325  typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
326  transformInitializer->SetFixedImage(m_FixedImage3D);
327  transformInitializer->SetMovingImage(m_MovingImage3D);
328  transformInitializer->SetTransform(transformPointer);
329  if (m_TransformParameters->GetMomentsOn())
330  {
331  transformInitializer->MomentsOn();
332  }
333  else
334  {
335  transformInitializer->GeometryOn();
336  }
337  transformInitializer->InitializeTransform();
338  }
339  typedef VersorRigid3DTransformType::VersorType VersorType;
340  typedef VersorType::VectorType VectorType;
341 
342  VersorType rotation;
343  VectorType axis;
344 
345  axis[0] = 0.0;
346  axis[1] = 0.0;
347  axis[2] = 1.0;
348 
349  const double angle = 0;
350 
351  rotation.Set(axis, angle);
352 
353  transformPointer->SetRotation(rotation);
354  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
355  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
356  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
357  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
358  }
359  }
360  else if (transform == TransformParameters::SCALESKEWVERSOR3DTRANSFORM)
361  {
362  if (VImageDimension == 3)
363  {
364  typename itk::ScaleSkewVersor3DTransform<double>::Pointer transformPointer =
365  itk::ScaleSkewVersor3DTransform<double>::New();
366  transformPointer->SetIdentity();
367  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
368  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
369  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
370  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
371  }
372  }
373  else if (transform == TransformParameters::SIMILARITY3DTRANSFORM)
374  {
375  if (VImageDimension == 3)
376  {
377  typename itk::Similarity3DTransform<double>::Pointer transformPointer =
378  itk::Similarity3DTransform<double>::New();
379  transformPointer->SetIdentity();
380  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
381  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
382  m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
383  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
384  }
385  }
386  else if (transform == TransformParameters::RIGID2DTRANSFORM)
387  {
388  if (VImageDimension == 2)
389  {
390  typename itk::Rigid2DTransform<double>::Pointer transformPointer = itk::Rigid2DTransform<double>::New();
391  transformPointer->SetIdentity();
392  transformPointer->SetAngle(m_TransformParameters->GetAngle());
393  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
394  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
395  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
396  }
397  }
398  else if (transform == TransformParameters::CENTEREDRIGID2DTRANSFORM)
399  {
400  if (VImageDimension == 2)
401  {
402  typename itk::CenteredRigid2DTransform<double>::Pointer transformPointer =
403  itk::CenteredRigid2DTransform<double>::New();
404  transformPointer->SetIdentity();
405  if (m_TransformParameters->GetTransformInitializerOn())
406  {
407  typedef typename itk::CenteredRigid2DTransform<double> CenteredRigid2DTransformType;
408  typedef typename itk::
409  CenteredTransformInitializer<CenteredRigid2DTransformType, FixedImage2DType, MovingImage2DType>
410  TransformInitializerType;
411  typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
412  transformInitializer->SetFixedImage(m_FixedImage2D);
413  transformInitializer->SetMovingImage(m_MovingImage2D);
414  transformInitializer->SetTransform(transformPointer);
415  if (m_TransformParameters->GetMomentsOn())
416  {
417  transformInitializer->MomentsOn();
418  }
419  else
420  {
421  transformInitializer->GeometryOn();
422  }
423  transformInitializer->InitializeTransform();
424  }
425  transformPointer->SetAngle(m_TransformParameters->GetAngle());
426  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
427  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
428  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
429  }
430  }
431  else if (transform == TransformParameters::EULER2DTRANSFORM)
432  {
433  if (VImageDimension == 2)
434  {
435  typename itk::Euler2DTransform<double>::Pointer transformPointer = itk::Euler2DTransform<double>::New();
436  transformPointer->SetIdentity();
437  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
438  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
439  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
440  }
441  }
442  else if (transform == TransformParameters::SIMILARITY2DTRANSFORM)
443  {
444  if (VImageDimension == 2)
445  {
446  typename itk::Similarity2DTransform<double>::Pointer transformPointer =
447  itk::Similarity2DTransform<double>::New();
448  transformPointer->SetIdentity();
449  transformPointer->SetScale(m_TransformParameters->GetScale());
450  transformPointer->SetAngle(m_TransformParameters->GetAngle());
451  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
452  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
453  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
454  }
455  }
456  else if (transform == TransformParameters::CENTEREDSIMILARITY2DTRANSFORM)
457  {
458  if (VImageDimension == 2)
459  {
460  typename itk::CenteredSimilarity2DTransform<double>::Pointer transformPointer =
461  itk::CenteredSimilarity2DTransform<double>::New();
462  transformPointer->SetIdentity();
463  if (m_TransformParameters->GetTransformInitializerOn())
464  {
465  typedef typename itk::CenteredSimilarity2DTransform<double> CenteredSimilarity2DTransformType;
466  typedef typename itk::
467  CenteredTransformInitializer<CenteredSimilarity2DTransformType, FixedImage2DType, MovingImage2DType>
468  TransformInitializerType;
469  typename TransformInitializerType::Pointer transformInitializer = TransformInitializerType::New();
470  transformInitializer->SetFixedImage(m_FixedImage2D);
471  transformInitializer->SetMovingImage(m_MovingImage2D);
472  transformInitializer->SetTransform(transformPointer);
473  if (m_TransformParameters->GetMomentsOn())
474  {
475  transformInitializer->MomentsOn();
476  }
477  else
478  {
479  transformInitializer->GeometryOn();
480  }
481  transformInitializer->InitializeTransform();
482  }
483  transformPointer->SetScale(m_TransformParameters->GetScale());
484  transformPointer->SetAngle(m_TransformParameters->GetAngle());
485  m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
486  m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
487  return reinterpret_cast<TransformType *>(transformPointer.GetPointer());
488  }
489  }
490  return NULL;
491  }
492 } // end namespace