Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3 Medical Imaging Interaction Toolkit
mitkAffineTransformBaseTest.cpp
Go to the documentation of this file.
1
2
3 /*============================================================================
4
5 The Medical Imaging Interaction Toolkit (MITK)
6
7 Copyright (c) German Cancer Research Center (DKFZ)
9
10 Use of this source code is governed by a 3-clause BSD license that can be
11 found in the LICENSE file.
12
13 ============================================================================*/
14
15 #include "itkScalableAffineTransform.h"
16 #include "mitkMatrixConvert.h"
17 #include "mitkNumericTypes.h"
18
19 #include "mitkTestingMacros.h"
20
21 using namespace mitk;
22
26 static double originalPointDouble[4];
27
28 static vtkMatrix4x4 *homogenMatrix = nullptr;
29
30 static vtkMatrix4x4 *expectedHomogenousMatrix = nullptr;
31 static const double expectedPointAfterTransformation[] = {2, 4, 4, 1};
32
33 static void Setup()
34 {
35  originalPoint[0] = 1.0;
36  originalPoint[1] = 0.0;
37  originalPoint[2] = 0.0;
38
39  for (int i = 0; i < 3; i++)
40  originalPointDouble[i] = originalPoint[i]; // same point as the Point3D version
41  originalPointDouble[3] = 1; // homogenous extension
42
43  offset[0] = 2.0;
44  offset[1] = 3.0;
45  offset[2] = 4.0;
46
47  // 90� rotation
48  rotation.Fill(0);
49  rotation[0][1] = -1;
50  rotation[1][0] = 1;
51
52  // prepare a Matrix which shall be set later to a specific
53  // homogen matrix by TransferItkTransformToVtkMatrix
54  // this makes sure, that the initialization to the identity does not
55  // overshadow any bugs in TransferItkTransformToVtkMatrix
56  // (it actually did that by "helping out" TransferItkTransformToVtkMatrix
57  // by setting the (3,3) element to 1).
58  homogenMatrix = vtkMatrix4x4::New();
59  for (int i = 0; i < 4; i++)
60  for (int j = 0; j < 4; j++)
61  homogenMatrix->SetElement(i, j, i + j * j); // just some not trivial value
62
63  // set the expected homogenous matrix result
64  expectedHomogenousMatrix = vtkMatrix4x4::New();
66  expectedHomogenousMatrix->SetElement(0, 1, -1);
67  expectedHomogenousMatrix->SetElement(1, 0, 1);
68  expectedHomogenousMatrix->SetElement(0, 3, 2);
69  expectedHomogenousMatrix->SetElement(1, 3, 3);
70  expectedHomogenousMatrix->SetElement(2, 3, 4);
71  expectedHomogenousMatrix->SetElement(3, 3, 1);
72 }
73
74 static void TearDown()
75 {
76  if (homogenMatrix)
77  homogenMatrix->Delete();
79  expectedHomogenousMatrix->Delete();
80 }
81
87 {
88  Setup();
89
91  AffineTransform3D::Pointer transform = AffineTransform3D::New();
92
93  transform->SetOffset(offset);
94  transform->SetMatrix(rotation);
95
96  TransferItkTransformToVtkMatrix(transform.GetPointer(), homogenMatrix);
97
99  Point3D pointTransformedByAffineTransform3D = transform->TransformPoint(originalPoint);
100
102  bool pointCorrect = true;
103  for (int i = 0; i < 3; i++) // only first three since no homogenous coordinates
104  pointCorrect &= Equal(pointTransformedByAffineTransform3D[i], expectedPointAfterTransformation[i]);
105
106  MITK_TEST_CONDITION(pointCorrect, "Point has been correctly transformed by AffineTranform3D")
107
108  TearDown();
109 }
110
116 {
117  Setup();
118
119  AffineTransform3D::Pointer transform = AffineTransform3D::New();
120
121  transform->SetOffset(offset);
122  transform->SetMatrix(rotation);
123
124  TransferItkTransformToVtkMatrix(transform.GetPointer(), homogenMatrix);
125
126  bool allElementsEqual = true;
127  for (int i = 0; i < 4; i++)
128  for (int j = 0; j < 4; j++)
129  allElementsEqual &= Equal(homogenMatrix->GetElement(i, j), expectedHomogenousMatrix->GetElement(i, j));
130
131  MITK_TEST_CONDITION(allElementsEqual, "Homogenous Matrix is set as expected")
132
133  TearDown();
134 }
135
146 {
147  Setup();
148
154  AffineTransform3D::Pointer transform = AffineTransform3D::New();
155
156  transform->SetOffset(offset);
157  transform->SetMatrix(rotation);
158
159  TransferItkTransformToVtkMatrix(transform.GetPointer(), homogenMatrix);
160
162  Point3D pointTransformedByAffineTransform3D = transform->TransformPoint(originalPoint);
163
164  double *pointTransformedByHomogenous = homogenMatrix->MultiplyDoublePoint(originalPointDouble);
165
166  /* check if results match */
167
168  bool pointsMatch = true;
169  for (int i = 0; i < 3; i++) // only first three since no homogenous coordinates
170  pointsMatch &= Equal(pointTransformedByAffineTransform3D[i], pointTransformedByHomogenous[i]);
171
172  bool homogenousComponentCorrect = Equal(1, pointTransformedByHomogenous[3]);
173
174  MITK_TEST_CONDITION(pointsMatch && homogenousComponentCorrect,
175  "Point transformed by AffineTransform and homogenous coordinates match")
176
177  TearDown();
178 }
179
184 int mitkAffineTransformBaseTest(int /*argc*/, char * /*argv*/ [])
185 {
186  MITK_TEST_BEGIN("AffineTransformationBaseTest");
187
189
191
193
194  MITK_TEST_END();
195 }
static vtkMatrix4x4 * homogenMatrix
static void TearDown()
void TransferItkTransformToVtkMatrix(const TTransformType *itkTransform, vtkMatrix4x4 *vtkmatrix)
int mitkAffineTransformBaseTest(int, char *[])
static void testIfPointIsTransformedAsExpected(void)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static Matrix3D rotation
static void testIfBothTransformationsProduceSameResults(void)
static Vector3D offset
#define MITK_TEST_CONDITION(COND, MSG)
static double originalPointDouble[4]
static void testTransferItkTransformToVtkMatrix(void)
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static Point3D originalPoint
static const double expectedPointAfterTransformation[]
and MITK_TEST_END()
static void Setup()
static vtkMatrix4x4 * expectedHomogenousMatrix