1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 #include "mitkTransformFactory.h"
18 #include "mitkTransformParameters.h"
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>
44 template <class TPixelType, unsigned int VImageDimension>
45 TransformFactory<TPixelType, VImageDimension>::TransformFactory()
46 : m_TransformParameters(NULL),
50 m_MovingImage2D(NULL),
56 template <class TPixelType, unsigned int VImageDimension>
57 void TransformFactory<TPixelType, VImageDimension>::SetFixedImage(FixedImageType *fixed)
59 if (VImageDimension == 2)
61 m_FixedImage2D = reinterpret_cast<FixedImage2DType *>(fixed);
66 m_FixedImage3D = reinterpret_cast<FixedImage3DType *>(fixed);
72 template <class TPixelType, unsigned int VImageDimension>
73 void TransformFactory<TPixelType, VImageDimension>::SetMovingImage(MovingImageType *moving)
75 if (VImageDimension == 2)
77 m_MovingImage2D = reinterpret_cast<MovingImage2DType *>(moving);
78 m_MovingImage = moving;
82 m_MovingImage3D = reinterpret_cast<MovingImage3DType *>(moving);
83 m_MovingImage = moving;
87 /*template < class TPixelType, unsigned int VImageDimension >
88 typename ::itk::Transform< double, VImageDimension, VImageDimension >::Pointer
91 template <class TPixelType>
92 typename ::itk::Transform< double, 3, 3 >::Pointer
93 MakeRigidTransform<TPixelType, 3>()
95 itk::Rigid3DTransform< double >::Pointer transform = itk::Rigid3DTransform< double >::New();
96 transform->SetIdentity();
97 return transform.GetPointer();
100 template <class TPixelType>
101 typename ::itk::Transform< double, 2, 2 >::Pointer
102 MakeRigidTransform<TPixelType, 2>()
107 /*template < unsigned int VImageDimension >
108 typename ::itk::Transform< double, VImageDimension, VImageDimension >::Pointer
109 MakeRigidTransform();
112 typename ::itk::Transform< double, 3, 3 >::Pointer
113 MakeRigidTransform<3>()
115 itk::Rigid3DTransform< double >::Pointer transform = itk::Rigid3DTransform< double >::New();
116 transform->SetIdentity();
117 return transform.GetPointer();
121 typename ::itk::Transform< double, 2, 2 >::Pointer
122 MakeRigidTransform<2>()
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;
134 // template < class TPixelType > class TransformFactory<TPixelType, 3>
136 // typedef typename itk::Transform< double, 3, 3 > TransformType;
137 // typedef typename TransformType::Pointer TransformPointer;
139 // typename TransformPointer MakeRigidTransform()
145 // typename TransformFactory< double, 3 >::TransformPointer
146 // TransformFactory< double, 3 >::MakeRigidTransform()
148 // itk::Rigid3DTransform< double >::Pointer transform = itk::Rigid3DTransform< double >::New();
149 // transform->SetIdentity();
150 // return transform.GetPointer();
154 // typename TransformFactory< double, 2 >::TransformPointer
155 // TransformFactory< double, 2 >::MakeRigidTransform()
160 template <class TPixelType, unsigned int VImageDimension>
161 typename TransformFactory<TPixelType, VImageDimension>::TransformPointer
162 TransformFactory<TPixelType, VImageDimension>::GetTransform()
164 int transform = m_TransformParameters->GetTransform();
165 if (transform == TransformParameters::TRANSLATIONTRANSFORM)
167 typename itk::TranslationTransform<double, VImageDimension>::Pointer transformPointer =
168 itk::TranslationTransform<double, VImageDimension>::New();
169 transformPointer->SetIdentity();
170 return transformPointer.GetPointer();
172 else if (transform == TransformParameters::SCALETRANSFORM)
174 typename itk::ScaleTransform<double, VImageDimension>::Pointer transformPointer =
175 itk::ScaleTransform<double, VImageDimension>::New();
176 transformPointer->SetIdentity();
177 return transformPointer.GetPointer();
179 else if (transform == TransformParameters::SCALELOGARITHMICTRANSFORM)
181 typename itk::ScaleLogarithmicTransform<double, VImageDimension>::Pointer transformPointer =
182 itk::ScaleLogarithmicTransform<double, VImageDimension>::New();
183 transformPointer->SetIdentity();
184 return transformPointer.GetPointer();
186 else if (transform == TransformParameters::AFFINETRANSFORM)
188 typename itk::AffineTransform<double, VImageDimension>::Pointer transformPointer =
189 itk::AffineTransform<double, VImageDimension>::New();
190 transformPointer->SetIdentity();
191 if (m_TransformParameters->GetTransformInitializerOn())
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())
202 transformInitializer->MomentsOn();
206 transformInitializer->GeometryOn();
208 transformInitializer->InitializeTransform();
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();
215 else if (transform == TransformParameters::FIXEDCENTEROFROTATIONAFFINETRANSFORM)
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())
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())
234 transformInitializer->MomentsOn();
238 transformInitializer->GeometryOn();
240 m_TransformParameters->SetTransformCenterX(transformPointer->GetCenter()[0]);
241 m_TransformParameters->SetTransformCenterY(transformPointer->GetCenter()[1]);
242 m_TransformParameters->SetTransformCenterZ(transformPointer->GetCenter()[2]);
243 transformInitializer->InitializeTransform();
245 return transformPointer.GetPointer();
247 // TODO remove rigid3dTransform
248 // else if (transform == TransformParameters::RIGID3DTRANSFORM)
250 // //return MakeRigidTransform<VImageDimension>();
251 // if (VImageDimension == 3)
253 // typename itk::Rigid3DTransform< double >::Pointer transformPointer = itk::Rigid3DTransform< double
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());
262 else if (transform == TransformParameters::EULER3DTRANSFORM)
264 if (VImageDimension == 3)
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());
274 else if (transform == TransformParameters::CENTEREDEULER3DTRANSFORM)
276 if (VImageDimension == 3)
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());
287 else if (transform == TransformParameters::QUATERNIONRIGIDTRANSFORM)
289 if (VImageDimension == 3)
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());
300 else if (transform == TransformParameters::VERSORTRANSFORM)
302 if (VImageDimension == 3)
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());
312 else if (transform == TransformParameters::VERSORRIGID3DTRANSFORM)
314 if (VImageDimension == 3)
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())
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())
331 transformInitializer->MomentsOn();
335 transformInitializer->GeometryOn();
337 transformInitializer->InitializeTransform();
339 typedef VersorRigid3DTransformType::VersorType VersorType;
340 typedef VersorType::VectorType VectorType;
349 const double angle = 0;
351 rotation.Set(axis, angle);
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());
360 else if (transform == TransformParameters::SCALESKEWVERSOR3DTRANSFORM)
362 if (VImageDimension == 3)
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());
373 else if (transform == TransformParameters::SIMILARITY3DTRANSFORM)
375 if (VImageDimension == 3)
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());
386 else if (transform == TransformParameters::RIGID2DTRANSFORM)
388 if (VImageDimension == 2)
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());
398 else if (transform == TransformParameters::CENTEREDRIGID2DTRANSFORM)
400 if (VImageDimension == 2)
402 typename itk::CenteredRigid2DTransform<double>::Pointer transformPointer =
403 itk::CenteredRigid2DTransform<double>::New();
404 transformPointer->SetIdentity();
405 if (m_TransformParameters->GetTransformInitializerOn())
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())
417 transformInitializer->MomentsOn();
421 transformInitializer->GeometryOn();
423 transformInitializer->InitializeTransform();
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());
431 else if (transform == TransformParameters::EULER2DTRANSFORM)
433 if (VImageDimension == 2)
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());
442 else if (transform == TransformParameters::SIMILARITY2DTRANSFORM)
444 if (VImageDimension == 2)
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());
456 else if (transform == TransformParameters::CENTEREDSIMILARITY2DTRANSFORM)
458 if (VImageDimension == 2)
460 typename itk::CenteredSimilarity2DTransform<double>::Pointer transformPointer =
461 itk::CenteredSimilarity2DTransform<double>::New();
462 transformPointer->SetIdentity();
463 if (m_TransformParameters->GetTransformInitializerOn())
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())
475 transformInitializer->MomentsOn();
479 transformInitializer->GeometryOn();
481 transformInitializer->InitializeTransform();
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());