Medical Imaging Interaction Toolkit  2018.4.99-07c45cb1
Medical Imaging Interaction Toolkit
mitkBaseGeometryTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkTestingMacros.h"
14 #include <mitkTestFixture.h>
15 #include <mitkTestingConfig.h>
16 
17 #include <MitkCoreExports.h>
18 #include <mitkBaseGeometry.h>
19 #include <mitkCommon.h>
20 #include <mitkOperationActor.h>
21 
22 #include <itkAffineGeometryFrame.h>
23 #include <itkBoundingBox.h>
24 #include <itkIndex.h>
25 #include <itkQuaternionRigidTransform.h>
26 #include <itkScalableAffineTransform.h>
27 #include <mitkVector.h>
28 #include <vtkMatrix4x4.h>
29 #include <vtkMatrixToLinearTransform.h>
30 
31 #include <mitkImageCast.h>
32 #include <mitkInteractionConst.h>
33 #include <mitkMatrixConvert.h>
34 #include <mitkRotationOperation.h>
35 #include <mitkScaleOperation.h>
36 
37 class vtkMatrix4x4;
38 class vtkMatrixToLinearTransform;
39 class vtkLinearTransform;
40 
41 typedef itk::BoundingBox<unsigned long, 3, mitk::ScalarType> BoundingBox;
42 typedef itk::BoundingBox<unsigned long, 3, mitk::ScalarType> BoundingBoxType;
44 typedef BoundingBoxType::Pointer BoundingBoxPointer;
45 
46 // Dummy instance of abstract base class
47 class DummyTestClass : public mitk::BaseGeometry
48 {
49 public:
50  DummyTestClass(){};
51  DummyTestClass(const DummyTestClass &other) : BaseGeometry(other){};
52  ~DummyTestClass() override{};
53 
54  mitkClassMacro(DummyTestClass, mitk::BaseGeometry);
55  itkNewMacro(Self);
56  mitkNewMacro1Param(Self, const Self &);
57 
58  itk::LightObject::Pointer InternalClone() const override
59  {
60  Self::Pointer newGeometry = new Self(*this);
61  newGeometry->UnRegister();
62  return newGeometry.GetPointer();
63  }
64 
65 protected:
66  void PrintSelf(std::ostream & /*os*/, itk::Indent /*indent*/) const override{};
67  //##Documentation
68  //## @brief Pre- and Post-functions are empty in BaseGeometry
69  //##
70  //## These virtual functions allow for a different beahiour in subclasses.
71  //## Do implement them in every subclass of BaseGeometry. If not needed, use {}.
72  //## If this class is inherited from a subclass of BaseGeometry, call {Superclass::Pre...();};, example:
73  // SlicedGeometry3D class
74  void PreSetSpacing(const mitk::Vector3D &/*aSpacing*/) override{};
75 };
76 
77 class mitkBaseGeometryTestSuite : public mitk::TestFixture
78 {
79  // List of Tests
80  CPPUNIT_TEST_SUITE(mitkBaseGeometryTestSuite);
81 
82  // Constructor
83  MITK_TEST(TestConstructors);
84  MITK_TEST(TestInitialize);
85 
86  // Set
87  MITK_TEST(TestSetOrigin);
88  MITK_TEST(TestSetBounds);
89  MITK_TEST(TestSetFloatBounds);
90  MITK_TEST(TestSetFloatBoundsDouble);
91  MITK_TEST(TestSetFrameOfReferenceID);
92  MITK_TEST(TestSetIndexToWorldTransform);
93  MITK_TEST(TestSetIndexToWorldTransformWithoutChangingSpacing);
94  MITK_TEST(TestSetIndexToWorldTransform_WithPointerToSameTransform);
95  MITK_TEST(TestSetSpacing);
96  MITK_TEST(TestTransferItkToVtkTransform);
97  MITK_TEST(TestSetIndexToWorldTransformByVtkMatrix);
98  MITK_TEST(TestSetIdentity);
99  MITK_TEST(TestSetImageGeometry);
100  // Equal
101  MITK_TEST(Equal_CloneAndOriginal_ReturnsTrue);
102  MITK_TEST(Equal_DifferentOrigin_ReturnsFalse);
103  MITK_TEST(Equal_DifferentIndexToWorldTransform_ReturnsFalse);
104  MITK_TEST(Equal_DifferentSpacing_ReturnsFalse);
105  MITK_TEST(Equal_InputIsNull_ReturnsFalse);
106  MITK_TEST(Equal_DifferentBoundingBox_ReturnsFalse);
107  MITK_TEST(Equal_Transforms_MinorDifferences_And_Eps);
108  // other Functions
109  MITK_TEST(TestComposeTransform);
110  MITK_TEST(TestComposeVtkMatrix);
111  MITK_TEST(TestTranslate);
112  MITK_TEST(TestIndexToWorld);
113  MITK_TEST(TestExecuteOperation);
114  MITK_TEST(TestCalculateBoundingBoxRelToTransform);
115  // MITK_TEST(TestSetTimeBounds);
116  MITK_TEST(TestIs2DConvertable);
117  MITK_TEST(TestGetCornerPoint);
118  MITK_TEST(TestExtentInMM);
119  MITK_TEST(TestGetAxisVector);
120  MITK_TEST(TestGetCenter);
121  MITK_TEST(TestGetDiagonalLength);
122  MITK_TEST(TestGetExtent);
123  MITK_TEST(TestIsInside);
124  MITK_TEST(TestGetMatrixColumn);
125 
126  CPPUNIT_TEST_SUITE_END();
127 
128  // Used Variables
129 private:
130  mitk::Point3D aPoint;
131  float aFloatSpacing[3];
132  mitk::Vector3D aSpacing;
133  mitk::AffineTransform3D::Pointer aTransform;
134  BoundingBoxPointer aBoundingBox;
135  mitk::AffineTransform3D::MatrixType aMatrix;
136 
137  mitk::Point3D anotherPoint;
138  mitk::Vector3D anotherSpacing;
139  BoundingBoxPointer anotherBoundingBox;
140  BoundingBoxPointer aThirdBoundingBox;
141  mitk::AffineTransform3D::Pointer anotherTransform;
142  mitk::AffineTransform3D::Pointer aThirdTransform;
143  mitk::AffineTransform3D::MatrixType anotherMatrix;
144  mitk::AffineTransform3D::MatrixType aThirdMatrix;
145 
146  DummyTestClass::Pointer aDummyGeometry;
147  DummyTestClass::Pointer anotherDummyGeometry;
148 
149 public:
150  // Set up for variables
151  void setUp() override
152  {
153  mitk::FillVector3D(aFloatSpacing, 1, 1, 1);
154  mitk::FillVector3D(aSpacing, 1, 1, 1);
155  mitk::FillVector3D(aPoint, 0, 0, 0);
156 
157  // Transform
158  aTransform = mitk::AffineTransform3D::New();
159  aTransform->SetIdentity();
160 
161  aMatrix.SetIdentity();
162 
163  anotherTransform = mitk::AffineTransform3D::New();
164 
165  anotherMatrix.SetIdentity();
166  anotherMatrix(1, 1) = 2;
167  anotherTransform->SetMatrix(anotherMatrix);
168 
169  aThirdTransform = mitk::AffineTransform3D::New();
170 
171  aThirdMatrix.SetIdentity();
172  aThirdMatrix(1, 1) = 7;
173  aThirdTransform->SetMatrix(aThirdMatrix);
174 
175  // Bounding Box
176  float bounds[6] = {0, 1, 0, 1, 0, 1};
178  const float *input = bounds;
179  int j = 0;
180  for (mitk::BoundingBox::BoundsArrayType::Iterator it = b.Begin(); j < 6; ++j)
181  *it++ = (mitk::ScalarType)*input++;
182 
183  aBoundingBox = BoundingBoxType::New();
184 
185  BoundingBoxType::PointsContainer::Pointer pointscontainer = BoundingBoxType::PointsContainer::New();
186  BoundingBoxType::PointType p;
187  BoundingBoxType::PointIdentifier pointid;
188  for (pointid = 0; pointid < 2; ++pointid)
189  {
190  unsigned int i;
191  for (i = 0; i < 3; ++i)
192  {
193  p[i] = bounds[2 * i + pointid];
194  }
195  pointscontainer->InsertElement(pointid, p);
196  }
197 
198  aBoundingBox->SetPoints(pointscontainer);
199  aBoundingBox->ComputeBoundingBox();
200 
201  anotherBoundingBox = BoundingBoxType::New();
202  p[0] = 11;
203  p[1] = 12;
204  p[2] = 13;
205  pointscontainer->InsertElement(1, p);
206  anotherBoundingBox->SetPoints(pointscontainer);
207  anotherBoundingBox->ComputeBoundingBox();
208 
209  aThirdBoundingBox = BoundingBoxType::New();
210  p[0] = 22;
211  p[1] = 23;
212  p[2] = 24;
213  pointscontainer->InsertElement(1, p);
214  aThirdBoundingBox->SetPoints(pointscontainer);
215  aThirdBoundingBox->ComputeBoundingBox();
216 
217  mitk::FillVector3D(anotherPoint, 2, 3, 4);
218  mitk::FillVector3D(anotherSpacing, 5, 6.5, 7);
219 
220  aDummyGeometry = DummyTestClass::New();
221  aDummyGeometry->Initialize();
222  anotherDummyGeometry = aDummyGeometry->Clone();
223  }
224 
225  void tearDown() override
226  {
227  aDummyGeometry = nullptr;
228  anotherDummyGeometry = nullptr;
229  }
230 
231  // Test functions
232 
233  void TestSetOrigin()
234  {
235  DummyTestClass::Pointer dummy = DummyTestClass::New();
236  dummy->SetOrigin(anotherPoint);
237  CPPUNIT_ASSERT(mitk::Equal(anotherPoint, dummy->GetOrigin()));
238 
239  // undo changes, new and changed object need to be the same!
240  dummy->SetOrigin(aPoint);
241  DummyTestClass::Pointer newDummy = DummyTestClass::New();
242  MITK_ASSERT_EQUAL(dummy, newDummy, "TestSetOrigin");
243  }
244 
245  void TestSetImageGeometry()
246  {
247  DummyTestClass::Pointer dummy = DummyTestClass::New();
248  dummy->SetImageGeometry(true);
249  CPPUNIT_ASSERT(dummy->GetImageGeometry());
250 
251  // undo changes, new and changed object need to be the same!
252  dummy->SetImageGeometry(false);
253  CPPUNIT_ASSERT(dummy->GetImageGeometry() == false);
254  DummyTestClass::Pointer newDummy = DummyTestClass::New();
255  MITK_ASSERT_EQUAL(dummy, newDummy, "TestSetImageGeometry");
256  }
257 
258  void TestSetFloatBounds()
259  {
260  float bounds[6] = {0, 11, 0, 12, 0, 13};
261  DummyTestClass::Pointer dummy = DummyTestClass::New();
262  dummy->SetFloatBounds(bounds);
263  MITK_ASSERT_EQUAL(BoundingBox::ConstPointer(dummy->GetBoundingBox()), anotherBoundingBox, "BoundingBox equality");
264 
265  // Wrong bounds, test needs to fail
266  bounds[1] = 7;
267  dummy->SetFloatBounds(bounds);
269  BoundingBox::ConstPointer(dummy->GetBoundingBox()), anotherBoundingBox, "BoundingBox not equal");
270 
271  // undo changes, new and changed object need to be the same!
272  float originalBounds[6] = {0, 1, 0, 1, 0, 1};
273  dummy->SetFloatBounds(originalBounds);
274  DummyTestClass::Pointer newDummy = DummyTestClass::New();
275  MITK_ASSERT_EQUAL(dummy, newDummy, "Undo and equal");
276  }
277 
278  void TestSetBounds()
279  {
280  DummyTestClass::Pointer dummy = DummyTestClass::New();
281  dummy->SetBounds(anotherBoundingBox->GetBounds());
282  MITK_ASSERT_EQUAL(BoundingBox::ConstPointer(dummy->GetBoundingBox()), anotherBoundingBox, "Setting bounds");
283 
284  // Test needs to fail now
285  dummy->SetBounds(aThirdBoundingBox->GetBounds());
287  BoundingBox::ConstPointer(dummy->GetBoundingBox()), anotherBoundingBox, "Setting unequal bounds");
288 
289  // undo changes, new and changed object need to be the same!
290  dummy->SetBounds(aBoundingBox->GetBounds());
291  DummyTestClass::Pointer newDummy = DummyTestClass::New();
292  MITK_ASSERT_EQUAL(dummy, newDummy, "Undo set bounds");
293  }
294 
295  void TestSetFloatBoundsDouble()
296  {
297  double bounds[6] = {0, 11, 0, 12, 0, 13};
298  DummyTestClass::Pointer dummy = DummyTestClass::New();
299  dummy->SetFloatBounds(bounds);
300  MITK_ASSERT_EQUAL(BoundingBox::ConstPointer(dummy->GetBoundingBox()), anotherBoundingBox, "Float bounds");
301 
302  // Test needs to fail now
303  bounds[3] = 7;
304  dummy->SetFloatBounds(bounds);
306  BoundingBox::ConstPointer(dummy->GetBoundingBox()), anotherBoundingBox, "Float bounds unequal");
307 
308  // undo changes, new and changed object need to be the same!
309  double originalBounds[6] = {0, 1, 0, 1, 0, 1};
310  dummy->SetFloatBounds(originalBounds);
311  DummyTestClass::Pointer newDummy = DummyTestClass::New();
312  MITK_ASSERT_EQUAL(dummy, newDummy, "Undo set float bounds");
313  }
314 
315  void TestSetFrameOfReferenceID()
316  {
317  DummyTestClass::Pointer dummy = DummyTestClass::New();
318  dummy->SetFrameOfReferenceID(5);
319  CPPUNIT_ASSERT(dummy->GetFrameOfReferenceID() == 5);
320 
321  // undo changes, new and changed object need to be the same!
322  dummy->SetFrameOfReferenceID(0);
323  DummyTestClass::Pointer newDummy = DummyTestClass::New();
324  MITK_ASSERT_EQUAL(dummy, newDummy, "Undo set frame of reference");
325  }
326 
327  void TestSetIndexToWorldTransform()
328  {
329  DummyTestClass::Pointer dummy = DummyTestClass::New();
330  dummy->SetIndexToWorldTransform(anotherTransform);
331  MITK_ASSERT_EQUAL(anotherTransform,
332  mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()),
333  "Compare IndexToWorldTransform 1");
334 
335  // Test needs to fail now
336  dummy->SetIndexToWorldTransform(aThirdTransform);
337  MITK_ASSERT_NOT_EQUAL(anotherTransform,
338  mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()),
339  "Compare IndexToWorldTransform 2");
340 
341  // undo changes, new and changed object need to be the same!
342  dummy->SetIndexToWorldTransform(aTransform);
343  DummyTestClass::Pointer newDummy = DummyTestClass::New();
344  MITK_ASSERT_EQUAL(dummy, newDummy, "Compare IndexToWorldTransform 3");
345  }
346 
347  void TestSetIndexToWorldTransformWithoutChangingSpacing()
348  {
349  DummyTestClass::Pointer dummy = DummyTestClass::New();
350  dummy->SetIndexToWorldTransformWithoutChangingSpacing(anotherTransform);
351  CPPUNIT_ASSERT(mitk::Equal(aSpacing, dummy->GetSpacing(), mitk::eps, true));
352 
353  // calculate a new version of anotherTransform, so that the spacing should be the same as the original spacing of
354  // aTransform.
355  mitk::AffineTransform3D::MatrixType::InternalMatrixType vnlmatrix;
356  vnlmatrix = anotherTransform->GetMatrix().GetVnlMatrix();
357 
358  mitk::VnlVector col;
359  col = vnlmatrix.get_column(0);
360  col.normalize();
361  col *= aSpacing[0];
362  vnlmatrix.set_column(0, col);
363  col = vnlmatrix.get_column(1);
364  col.normalize();
365  col *= aSpacing[1];
366  vnlmatrix.set_column(1, col);
367  col = vnlmatrix.get_column(2);
368  col.normalize();
369  col *= aSpacing[2];
370  vnlmatrix.set_column(2, col);
371 
372  mitk::Matrix3D matrix;
373  matrix = vnlmatrix;
374  anotherTransform->SetMatrix(matrix);
375 
376  CPPUNIT_ASSERT(mitk::Equal(anotherTransform, dummy->GetIndexToWorldTransform(), mitk::eps, true));
377  }
378 
379  void TestSetIndexToWorldTransform_WithPointerToSameTransform()
380  {
381  DummyTestClass::Pointer dummy = DummyTestClass::New();
382 
383  dummy->SetOrigin(anotherPoint);
384  dummy->SetIndexToWorldTransform(anotherTransform);
385  dummy->SetSpacing(anotherSpacing);
386 
387  mitk::AffineTransform3D::Pointer testTransfrom = dummy->GetIndexToWorldTransform();
388 
389  mitk::Vector3D modifiedPoint = anotherPoint.GetVectorFromOrigin() * 2.;
390 
391  testTransfrom->SetOffset(modifiedPoint);
392 
393  dummy->SetIndexToWorldTransform(testTransfrom);
394 
395  CPPUNIT_ASSERT(mitk::Equal(modifiedPoint, dummy->GetOrigin().GetVectorFromOrigin()));
396  }
397 
398  void TestSetIndexToWorldTransformByVtkMatrix()
399  {
400  vtkMatrix4x4 *vtkmatrix;
401  vtkmatrix = vtkMatrix4x4::New();
402  vtkmatrix->Identity();
403  vtkmatrix->SetElement(1, 1, 2);
404  DummyTestClass::Pointer dummy = DummyTestClass::New();
405  dummy->SetIndexToWorldTransformByVtkMatrix(vtkmatrix);
406  MITK_ASSERT_EQUAL(anotherTransform,
407  mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()),
408  "Compare IndexToWorldTransformByVtkMatrix 1");
409 
410  // test needs to fail now
411  vtkmatrix->SetElement(1, 1, 7);
412  dummy->SetIndexToWorldTransformByVtkMatrix(vtkmatrix);
413  MITK_ASSERT_NOT_EQUAL(anotherTransform,
414  mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()),
415  "Compare IndexToWorldTransformByVtkMatrix 2");
416 
417  // undo changes, new and changed object need to be the same!
418  vtkmatrix->SetElement(1, 1, 1);
419  dummy->SetIndexToWorldTransformByVtkMatrix(vtkmatrix);
420  vtkmatrix->Delete();
421  DummyTestClass::Pointer newDummy = DummyTestClass::New();
422  MITK_ASSERT_EQUAL(dummy, newDummy, "Compare IndexToWorldTransformByVtkMatrix 3");
423  }
424 
425  void TestSetIdentity()
426  {
427  DummyTestClass::Pointer dummy = DummyTestClass::New();
428  // Change IndextoWorldTransform and Origin
429  dummy->SetIndexToWorldTransform(anotherTransform);
430  dummy->SetOrigin(anotherPoint);
431 
432  // Set Identity should reset ITWT and Origin
433  dummy->SetIdentity();
434 
436  aTransform, mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()), "Test set identity 1");
437  CPPUNIT_ASSERT(mitk::Equal(aPoint, dummy->GetOrigin()));
438  CPPUNIT_ASSERT(mitk::Equal(aSpacing, dummy->GetSpacing()));
439 
440  // new and changed object need to be the same!
441  DummyTestClass::Pointer newDummy = DummyTestClass::New();
442  MITK_ASSERT_EQUAL(dummy, newDummy, "Test set identity 2");
443  }
444 
445  void TestSetSpacing()
446  {
447  DummyTestClass::Pointer dummy = DummyTestClass::New();
448  dummy->SetSpacing(anotherSpacing);
449  CPPUNIT_ASSERT(mitk::Equal(anotherSpacing, dummy->GetSpacing()));
450 
451  // undo changes, new and changed object need to be the same!
452  dummy->SetSpacing(aSpacing);
453  DummyTestClass::Pointer newDummy = DummyTestClass::New();
454  MITK_ASSERT_EQUAL(dummy, newDummy, "Dummy spacing");
455  }
456 
457  void TestTransferItkToVtkTransform()
458  {
459  DummyTestClass::Pointer dummy = DummyTestClass::New();
460  dummy->SetIndexToWorldTransform(anotherTransform); // calls TransferItkToVtkTransform
461  mitk::AffineTransform3D::Pointer dummyTransform = dummy->GetIndexToWorldTransform();
462  CPPUNIT_ASSERT(mitk::MatrixEqualElementWise(anotherMatrix, dummyTransform->GetMatrix()));
463  }
464 
465  void TestConstructors()
466  {
467  // test standard constructor
468  DummyTestClass::Pointer dummy1 = DummyTestClass::New();
469  bool test = dummy1->IsValid();
470  CPPUNIT_ASSERT(test == true);
471  CPPUNIT_ASSERT(dummy1->GetFrameOfReferenceID() == 0);
472  CPPUNIT_ASSERT(dummy1->GetIndexToWorldTransformLastModified() == 0);
473 
474  CPPUNIT_ASSERT(mitk::Equal(dummy1->GetSpacing(), aSpacing));
475  CPPUNIT_ASSERT(mitk::Equal(dummy1->GetOrigin(), aPoint));
476 
477  CPPUNIT_ASSERT(dummy1->GetImageGeometry() == false);
478 
480  mitk::AffineTransform3D::Pointer(dummy1->GetIndexToWorldTransform()), aTransform, "Contructor test 1");
481 
483  mitk::BaseGeometry::BoundingBoxType::ConstPointer(dummy1->GetBoundingBox()), aBoundingBox, "Constructor test 2");
484 
485  DummyTestClass::Pointer dummy2 = DummyTestClass::New();
486  dummy2->SetOrigin(anotherPoint);
487  float bounds[6] = {0, 11, 0, 12, 0, 13};
488  dummy2->SetFloatBounds(bounds);
489  dummy2->SetIndexToWorldTransform(anotherTransform);
490  dummy2->SetSpacing(anotherSpacing);
491 
492  DummyTestClass::Pointer dummy3 = DummyTestClass::New(*dummy2);
493  MITK_ASSERT_EQUAL(dummy3, dummy2, "Dummy contructor");
494  }
495 
496  // Equal Tests
497 
498  void Equal_CloneAndOriginal_ReturnsTrue() { MITK_ASSERT_EQUAL(aDummyGeometry, anotherDummyGeometry, "Clone test"); }
499  void Equal_DifferentOrigin_ReturnsFalse()
500  {
501  anotherDummyGeometry->SetOrigin(anotherPoint);
502 
503  MITK_ASSERT_NOT_EQUAL(aDummyGeometry, anotherDummyGeometry, "Different origin test");
504  }
505 
506  void Equal_DifferentIndexToWorldTransform_ReturnsFalse()
507  {
508  anotherDummyGeometry->SetIndexToWorldTransform(anotherTransform);
509 
510  MITK_ASSERT_NOT_EQUAL(aDummyGeometry, anotherDummyGeometry, "Different index to world");
511  }
512 
513  void Equal_DifferentSpacing_ReturnsFalse()
514  {
515  anotherDummyGeometry->SetSpacing(anotherSpacing);
516 
517  MITK_ASSERT_NOT_EQUAL(aDummyGeometry, anotherDummyGeometry, "Different spacing");
518  }
519 
520  void Equal_InputIsNull_ReturnsFalse()
521  {
522  DummyTestClass::Pointer geometryNull = nullptr;
523  CPPUNIT_ASSERT_THROW(MITK_ASSERT_EQUAL(geometryNull, anotherDummyGeometry, "Input is null"), mitk::Exception);
524  }
525 
526  void Equal_DifferentBoundingBox_ReturnsFalse()
527  {
528  // create different bounds to make the comparison false
529  mitk::ScalarType bounds[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
530  anotherDummyGeometry->SetBounds(bounds);
531 
532  MITK_ASSERT_NOT_EQUAL(aDummyGeometry, anotherDummyGeometry, "Different bounding box");
533  }
534 
535  void Equal_Transforms_MinorDifferences_And_Eps()
536  {
537  // Verifies that the eps parameter is evaluated properly
538  // when comparing two mitk::BaseGeometry::TransformTypes
539  aMatrix.SetIdentity();
540  anotherMatrix.SetIdentity();
541 
542  aMatrix(0, 1) = 0.0002;
543  aTransform->SetMatrix(aMatrix);
544  anotherMatrix(0, 1) = 0.0002;
545  anotherTransform->SetMatrix(anotherMatrix);
546  anotherTransform->SetMatrix(aMatrix);
547  CPPUNIT_ASSERT_MESSAGE("Exact same transforms are mitk::Equal() for eps=mitk::eps",
548  mitk::Equal(aTransform, anotherTransform, mitk::eps, true));
549  CPPUNIT_ASSERT_MESSAGE("Exact same transforms are mitk::Equal() for eps=vnl_math::eps",
550  mitk::Equal(aTransform, anotherTransform, vnl_math::eps, true));
551 
552  anotherMatrix(0, 1) = 0.0002 + mitk::eps;
553  anotherTransform->SetMatrix(anotherMatrix);
554  CPPUNIT_ASSERT_MESSAGE("Transforms of diff mitk::eps are !mitk::Equal() for eps=vnl_math::eps",
555  !mitk::Equal(aTransform, anotherTransform, vnl_math::eps, true));
556  CPPUNIT_ASSERT_MESSAGE("Transforms of diff mitk::eps are !mitk::Equal() for eps=mitk::eps-1%",
557  !mitk::Equal(aTransform, anotherTransform, mitk::eps * 0.99, true));
558  CPPUNIT_ASSERT_MESSAGE("Transforms of diff mitk::eps _are_ mitk::Equal() for eps=mitk::eps+1%",
559  mitk::Equal(aTransform, anotherTransform, mitk::eps * 1.01, true));
560  }
561 
562  void TestComposeTransform()
563  {
564  // Create Transformations to set and compare
565  mitk::AffineTransform3D::Pointer transform1;
566  transform1 = mitk::AffineTransform3D::New();
567  mitk::AffineTransform3D::MatrixType matrix1;
568  matrix1.SetIdentity();
569  matrix1(1, 1) = 2;
570  transform1->SetMatrix(matrix1); // Spacing = 2
571 
572  mitk::AffineTransform3D::Pointer transform2;
573  transform2 = mitk::AffineTransform3D::New();
574  mitk::AffineTransform3D::MatrixType matrix2;
575  matrix2.SetIdentity();
576  matrix2(1, 1) = 2;
577  transform2->SetMatrix(matrix2); // Spacing = 2
578 
579  mitk::AffineTransform3D::Pointer transform3;
580  transform3 = mitk::AffineTransform3D::New();
581  mitk::AffineTransform3D::MatrixType matrix3;
582  matrix3.SetIdentity();
583  matrix3(1, 1) = 4;
584  transform3->SetMatrix(matrix3); // Spacing = 4
585 
586  mitk::AffineTransform3D::Pointer transform4;
587  transform4 = mitk::AffineTransform3D::New();
588  mitk::AffineTransform3D::MatrixType matrix4;
589  matrix4.SetIdentity();
590  matrix4(1, 1) = 0.25;
591  transform4->SetMatrix(matrix4); // Spacing = 0.25
592 
593  // Vector to compare spacing
594  mitk::Vector3D expectedSpacing;
595  expectedSpacing.Fill(1.0);
596  expectedSpacing[1] = 4;
597 
598  DummyTestClass::Pointer dummy = DummyTestClass::New();
599  dummy->SetIndexToWorldTransform(transform1); // Spacing = 2
600  dummy->Compose(transform2); // Spacing = 4
601  CPPUNIT_ASSERT(mitk::Equal(dummy->GetSpacing(), expectedSpacing));
603  transform3, mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()), "Compose transform 2"); // 4=4
604 
605  // undo changes, new and changed object need to be the same!
606  dummy->Compose(transform4); // Spacing = 1
607  DummyTestClass::Pointer newDummy = DummyTestClass::New();
608  MITK_ASSERT_EQUAL(dummy, newDummy, "Compose transform 3"); // 1=1
609  }
610 
611  void TestComposeVtkMatrix()
612  {
613  // Create Transformations to set and compare
614  mitk::AffineTransform3D::Pointer transform1;
615  transform1 = mitk::AffineTransform3D::New();
616  mitk::AffineTransform3D::MatrixType matrix1;
617  matrix1.SetIdentity();
618  matrix1(1, 1) = 2;
619  transform1->SetMatrix(matrix1); // Spacing = 2
620 
621  vtkMatrix4x4 *vtkmatrix2;
622  vtkmatrix2 = vtkMatrix4x4::New();
623  vtkmatrix2->Identity();
624  vtkmatrix2->SetElement(1, 1, 2); // Spacing = 2
625 
626  mitk::AffineTransform3D::Pointer transform3;
627  transform3 = mitk::AffineTransform3D::New();
628  mitk::AffineTransform3D::MatrixType matrix3;
629  matrix3.SetIdentity();
630  matrix3(1, 1) = 4;
631  transform3->SetMatrix(matrix3); // Spacing = 4
632 
633  vtkMatrix4x4 *vtkmatrix4;
634  vtkmatrix4 = vtkMatrix4x4::New();
635  vtkmatrix4->Identity();
636  vtkmatrix4->SetElement(1, 1, 0.25); // Spacing = 0.25
637 
638  // Vector to compare spacing
639  mitk::Vector3D expectedSpacing;
640  expectedSpacing.Fill(1.0);
641  expectedSpacing[1] = 4;
642 
643  DummyTestClass::Pointer dummy = DummyTestClass::New();
644  dummy->SetIndexToWorldTransform(transform1); // Spacing = 2
645  dummy->Compose(vtkmatrix2); // Spacing = 4
646  vtkmatrix2->Delete();
647 
649  transform3, mitk::AffineTransform3D::Pointer(dummy->GetIndexToWorldTransform()), "Compose vtk matrix"); // 4=4
650  CPPUNIT_ASSERT(mitk::Equal(dummy->GetSpacing(), expectedSpacing));
651 
652  // undo changes, new and changed object need to be the same!
653  dummy->Compose(vtkmatrix4); // Spacing = 1
654  vtkmatrix4->Delete();
655  DummyTestClass::Pointer newDummy = DummyTestClass::New();
656  MITK_ASSERT_EQUAL(dummy, newDummy, "Compose vtk"); // 1=1
657  }
658 
659  void TestTranslate()
660  {
661  DummyTestClass::Pointer dummy = DummyTestClass::New();
662  dummy->SetOrigin(anotherPoint);
663  CPPUNIT_ASSERT(mitk::Equal(anotherPoint, dummy->GetOrigin()));
664 
665  // use some random values for translation
666  mitk::Vector3D translationVector;
667  translationVector.SetElement(0, 17.5f);
668  translationVector.SetElement(1, -32.3f);
669  translationVector.SetElement(2, 4.0f);
670 
671  // compute ground truth
672  mitk::Point3D tmpResult = anotherPoint + translationVector;
673  dummy->Translate(translationVector);
674  CPPUNIT_ASSERT(mitk::Equal(dummy->GetOrigin(), tmpResult));
675 
676  // undo changes
677  translationVector *= -1;
678  dummy->Translate(translationVector);
679  CPPUNIT_ASSERT(mitk::Equal(dummy->GetOrigin(), anotherPoint));
680 
681  // undo changes, new and changed object need to be the same!
682  translationVector.SetElement(0, -1 * anotherPoint[0]);
683  translationVector.SetElement(1, -1 * anotherPoint[1]);
684  translationVector.SetElement(2, -1 * anotherPoint[2]);
685  dummy->Translate(translationVector);
686 
687  DummyTestClass::Pointer newDummy = DummyTestClass::New();
688  MITK_ASSERT_EQUAL(dummy, newDummy, "Translate test");
689  }
690 
691  // a part of the test requires axis-parallel coordinates
693  {
694  // Testing consistency of index and world coordinate systems
695  mitk::Point3D origin = dummyGeometry->GetOrigin();
696  mitk::Point3D dummyPoint;
697 
698  // Testing index->world->index conversion consistency
699  dummyGeometry->WorldToIndex(origin, dummyPoint);
700  dummyGeometry->IndexToWorld(dummyPoint, dummyPoint);
701  CPPUNIT_ASSERT(mitk::EqualArray(dummyPoint, origin, 3, mitk::eps, true));
702 
703  // Testing WorldToIndex(origin, mitk::Point3D)==(0,0,0)
704  mitk::Point3D globalOrigin;
705  mitk::FillVector3D(globalOrigin, 0, 0, 0);
706 
707  mitk::Point3D originContinuousIndex;
708  dummyGeometry->WorldToIndex(origin, originContinuousIndex);
709  CPPUNIT_ASSERT(mitk::EqualArray(originContinuousIndex, globalOrigin, 3, mitk::eps, true));
710 
711  // Testing WorldToIndex(origin, itk::Index)==(0,0,0)
712  itk::Index<3> itkindex;
713  dummyGeometry->WorldToIndex(origin, itkindex);
714  itk::Index<3> globalOriginIndex;
715  mitk::vtk2itk(globalOrigin, globalOriginIndex);
716  CPPUNIT_ASSERT(mitk::EqualArray(itkindex, globalOriginIndex, 3, mitk::eps, true));
717 
718  // Testing WorldToIndex(origin-0.5*spacing, itk::Index)==(0,0,0)
719  mitk::Vector3D halfSpacingStep = dummyGeometry->GetSpacing() * 0.5;
721  mitk::Point3D originOffCenter = origin - halfSpacingStep;
722  dummyGeometry->WorldToIndex(originOffCenter, itkindex);
723  CPPUNIT_ASSERT(mitk::EqualArray(itkindex, globalOriginIndex, 3, mitk::eps, true));
724 
725  // Testing WorldToIndex(origin+0.5*spacing-eps, itk::Index)==(0,0,0)
726  originOffCenter = origin + halfSpacingStep;
727  originOffCenter -= 0.0001;
728  dummyGeometry->WorldToIndex(originOffCenter, itkindex);
729  CPPUNIT_ASSERT(mitk::EqualArray(itkindex, globalOriginIndex, 3, mitk::eps, true));
730 
731  // Testing WorldToIndex(origin+0.5*spacing, itk::Index)==(1,1,1)");
732  originOffCenter = origin + halfSpacingStep;
733  itk::Index<3> global111;
734  mitk::FillVector3D(global111, 1, 1, 1);
735  dummyGeometry->WorldToIndex(originOffCenter, itkindex);
736  CPPUNIT_ASSERT(mitk::EqualArray(itkindex, global111, 3, mitk::eps, true));
737 
738  // Testing WorldToIndex(GetCenter())==BoundingBox.GetCenter
739  mitk::Point3D center = dummyGeometry->GetCenter();
740  mitk::Point3D centerContIndex;
741  dummyGeometry->WorldToIndex(center, centerContIndex);
742  mitk::BoundingBox::ConstPointer boundingBox = dummyGeometry->GetBoundingBox();
743  mitk::BoundingBox::PointType centerBounds = boundingBox->GetCenter();
744  CPPUNIT_ASSERT(mitk::Equal(centerContIndex, centerBounds));
745 
746  // Testing GetCenter()==IndexToWorld(BoundingBox.GetCenter)
747  center = dummyGeometry->GetCenter();
748  mitk::Point3D centerBoundsInWorldCoords;
749  dummyGeometry->IndexToWorld(centerBounds, centerBoundsInWorldCoords);
750  CPPUNIT_ASSERT(mitk::Equal(center, centerBoundsInWorldCoords));
751 
752  // Test using random point,
753  // Testing consistency of index and world coordinate systems
754  mitk::Point3D point;
755  mitk::FillVector3D(point, 3.5, -2, 4.6);
756 
757  // Testing index->world->index conversion consistency
758  dummyGeometry->WorldToIndex(point, dummyPoint);
759  dummyGeometry->IndexToWorld(dummyPoint, dummyPoint);
760  CPPUNIT_ASSERT(mitk::EqualArray(dummyPoint, point, 3, mitk::eps, true));
761 
762  return EXIT_SUCCESS;
763  }
764 
766  {
767  // Testing consistency of index and world coordinate systems for vectors
768  mitk::Vector3D xAxisMM = dummyGeometry->GetAxisVector(0);
769  mitk::Vector3D xAxisContinuousIndex;
770 
771  mitk::Point3D p, pIndex, origin;
772  origin = dummyGeometry->GetOrigin();
773  p[0] = xAxisMM[0] + origin[0];
774  p[1] = xAxisMM[1] + origin[1];
775  p[2] = xAxisMM[2] + origin[2];
776 
777  dummyGeometry->WorldToIndex(p, pIndex);
778 
779  dummyGeometry->WorldToIndex(xAxisMM, xAxisContinuousIndex);
780  CPPUNIT_ASSERT(mitk::Equal(xAxisContinuousIndex[0], pIndex[0]));
781  CPPUNIT_ASSERT(mitk::Equal(xAxisContinuousIndex[1], pIndex[1]));
782  CPPUNIT_ASSERT(mitk::Equal(xAxisContinuousIndex[2], pIndex[2]));
783 
784  dummyGeometry->IndexToWorld(xAxisContinuousIndex, xAxisContinuousIndex);
785 
786  dummyGeometry->IndexToWorld(pIndex, p);
787 
788  CPPUNIT_ASSERT(xAxisContinuousIndex == xAxisMM);
789  CPPUNIT_ASSERT(mitk::Equal(xAxisContinuousIndex[0], p[0] - origin[0]));
790  CPPUNIT_ASSERT(mitk::Equal(xAxisContinuousIndex[1], p[1] - origin[1]));
791  CPPUNIT_ASSERT(mitk::Equal(xAxisContinuousIndex[2], p[2] - origin[2]));
792 
793  // Test consictency for random vector
794  mitk::Vector3D vector;
795  mitk::FillVector3D(vector, 2.5, -3.2, 8.1);
796  mitk::Vector3D vectorContinuousIndex;
797 
798  p[0] = vector[0] + origin[0];
799  p[1] = vector[1] + origin[1];
800  p[2] = vector[2] + origin[2];
801 
802  dummyGeometry->WorldToIndex(p, pIndex);
803 
804  dummyGeometry->WorldToIndex(vector, vectorContinuousIndex);
805  CPPUNIT_ASSERT(mitk::Equal(vectorContinuousIndex[0], pIndex[0]));
806  CPPUNIT_ASSERT(mitk::Equal(vectorContinuousIndex[1], pIndex[1]));
807  CPPUNIT_ASSERT(mitk::Equal(vectorContinuousIndex[2], pIndex[2]));
808 
809  dummyGeometry->IndexToWorld(vectorContinuousIndex, vectorContinuousIndex);
810 
811  dummyGeometry->IndexToWorld(pIndex, p);
812 
813  CPPUNIT_ASSERT(vectorContinuousIndex == vector);
814  CPPUNIT_ASSERT(mitk::Equal(vectorContinuousIndex[0], p[0] - origin[0]));
815  CPPUNIT_ASSERT(mitk::Equal(vectorContinuousIndex[1], p[1] - origin[1]));
816  CPPUNIT_ASSERT(mitk::Equal(vectorContinuousIndex[2], p[2] - origin[2]));
817 
818  return EXIT_SUCCESS;
819  }
820 
822  {
823  // Testing consistency of index and world coordinate systems
824 
825  // creating testing data
826  itk::Index<4> itkIndex4, itkIndex4b;
827  itk::Index<3> itkIndex3, itkIndex3b;
828  itk::Index<2> itkIndex2, itkIndex2b;
829  itk::Index<3> mitkIndex, mitkIndexb;
830 
831  itkIndex4[0] = itkIndex4[1] = itkIndex4[2] = itkIndex4[3] = 4;
832  itkIndex3[0] = itkIndex3[1] = itkIndex3[2] = 6;
833  itkIndex2[0] = itkIndex2[1] = 2;
834  mitkIndex[0] = mitkIndex[1] = mitkIndex[2] = 13;
835 
836  // check for constistency
837  mitk::Point3D point;
838  dummyGeometry->IndexToWorld(itkIndex2, point);
839  dummyGeometry->WorldToIndex(point, itkIndex2b);
840 
841  CPPUNIT_ASSERT(((itkIndex2b[0] == itkIndex2[0]) && (itkIndex2b[1] == itkIndex2[1])));
842  // Testing itk::index<2> for IndexToWorld/WorldToIndex consistency
843 
844  dummyGeometry->IndexToWorld(itkIndex3, point);
845  dummyGeometry->WorldToIndex(point, itkIndex3b);
846 
847  CPPUNIT_ASSERT(
848  ((itkIndex3b[0] == itkIndex3[0]) && (itkIndex3b[1] == itkIndex3[1]) && (itkIndex3b[2] == itkIndex3[2])));
849  // Testing itk::index<3> for IndexToWorld/WorldToIndex consistency
850 
851  dummyGeometry->IndexToWorld(itkIndex4, point);
852  dummyGeometry->WorldToIndex(point, itkIndex4b);
853 
854  CPPUNIT_ASSERT(((itkIndex4b[0] == itkIndex4[0]) && (itkIndex4b[1] == itkIndex4[1]) &&
855  (itkIndex4b[2] == itkIndex4[2]) && (itkIndex4b[3] == 0)));
856  // Testing itk::index<3> for IndexToWorld/WorldToIndex consistency
857 
858  dummyGeometry->IndexToWorld(mitkIndex, point);
859  dummyGeometry->WorldToIndex(point, mitkIndexb);
860 
861  CPPUNIT_ASSERT(
862  ((mitkIndexb[0] == mitkIndex[0]) && (mitkIndexb[1] == mitkIndex[1]) && (mitkIndexb[2] == mitkIndex[2])));
863  // Testing mitk::Index for IndexToWorld/WorldToIndex consistency
864 
865  return EXIT_SUCCESS;
866  }
867 
868  void TestIndexToWorld()
869  {
870  DummyTestClass::Pointer dummy = DummyTestClass::New();
871 
875 
876  // Geometry must not have changed
877  DummyTestClass::Pointer newDummy = DummyTestClass::New();
878  MITK_ASSERT_EQUAL(dummy, newDummy, "Dummy index to world");
879 
880  // Test with other geometries
881  dummy->SetOrigin(anotherPoint);
885 
886  dummy->SetIndexToWorldTransform(anotherTransform);
890 
891  dummy->SetOrigin(anotherPoint);
895 
896  dummy->SetSpacing(anotherSpacing);
900  }
901 
902  void TestExecuteOperation()
903  {
904  DummyTestClass::Pointer dummy = DummyTestClass::New();
905 
906  // Do same Operations with new Dummy and compare
907  DummyTestClass::Pointer newDummy = DummyTestClass::New();
908 
909  // Test operation Nothing
910  auto opN = new mitk::Operation(mitk::OpNOTHING);
911  dummy->ExecuteOperation(opN);
912  MITK_ASSERT_EQUAL(dummy, newDummy, "Dummy execute operation 1");
913 
914  // Test operation Move
915  auto opP = new mitk::PointOperation(mitk::OpMOVE, anotherPoint);
916  dummy->ExecuteOperation(opP);
917  CPPUNIT_ASSERT(mitk::Equal(anotherPoint, dummy->GetOrigin()));
918  newDummy->SetOrigin(anotherPoint);
919  MITK_ASSERT_EQUAL(dummy, newDummy, "Dummy execute operation 2");
920 
921  // Test operation Scale, Scale sets spacing to scale+1
922  mitk::Point3D spacing;
923  spacing[0] = anotherSpacing[0] - 1.;
924  spacing[1] = anotherSpacing[1] - 1.;
925  spacing[2] = anotherSpacing[2] - 1.;
926 
927  auto opS = new mitk::ScaleOperation(mitk::OpSCALE, spacing, anotherPoint);
928  dummy->ExecuteOperation(opS);
929  CPPUNIT_ASSERT(mitk::Equal(anotherSpacing, dummy->GetSpacing()));
930  newDummy->SetSpacing(anotherSpacing);
931  MITK_ASSERT_EQUAL(dummy, newDummy, "Dummy execute operation 3");
932 
933  // change Geometry to test more cases
934  dummy->SetIndexToWorldTransform(anotherTransform);
935  dummy->SetSpacing(anotherSpacing);
936 
937  // Testing a rotation of the geometry
938  double angle = 35.0;
939  mitk::Vector3D rotationVector;
940  mitk::FillVector3D(rotationVector, 1, 0, 0);
941  mitk::Point3D center = dummy->GetCenter();
942  auto opR = new mitk::RotationOperation(mitk::OpROTATE, center, rotationVector, angle);
943  dummy->ExecuteOperation(opR);
944 
946  mitk::GetRotation(dummy, rotation);
947  mitk::Vector3D voxelStep = rotation * anotherSpacing;
948  mitk::Vector3D voxelStepIndex;
949  dummy->WorldToIndex(voxelStep, voxelStepIndex);
950  mitk::Vector3D expectedVoxelStepIndex;
951  expectedVoxelStepIndex.Fill(1);
952  CPPUNIT_ASSERT(mitk::Equal(voxelStepIndex, expectedVoxelStepIndex));
953 
954  delete opR;
955  delete opN;
956  delete opS;
957  delete opP;
958  }
959 
960  void TestCalculateBoundingBoxRelToTransform()
961  {
962  DummyTestClass::Pointer dummy = DummyTestClass::New();
963  dummy->SetExtentInMM(0, 15);
964  dummy->SetExtentInMM(1, 20);
965  dummy->SetExtentInMM(2, 8);
966 
967  mitk::BoundingBox::Pointer dummyBoundingBox = dummy->CalculateBoundingBoxRelativeToTransform(anotherTransform);
968 
969  mitk::BoundingBox::PointsContainer::Pointer pointscontainer = mitk::BoundingBox::PointsContainer::New();
970  mitk::BoundingBox::PointIdentifier pointid = 0;
971  unsigned char i;
972  mitk::AffineTransform3D::Pointer inverse = mitk::AffineTransform3D::New();
973  anotherTransform->GetInverse(inverse);
974  for (i = 0; i < 8; ++i)
975  pointscontainer->InsertElement(pointid++, inverse->TransformPoint(dummy->GetCornerPoint(i)));
976  mitk::BoundingBox::Pointer result = mitk::BoundingBox::New();
977  result->SetPoints(pointscontainer);
978  result->ComputeBoundingBox();
979 
980  MITK_ASSERT_EQUAL(result, dummyBoundingBox, "BBox rel to transform");
981 
982  // dummy still needs to be unchanged, except for extend
983  DummyTestClass::Pointer newDummy = DummyTestClass::New();
984  newDummy->SetExtentInMM(0, 15);
985  newDummy->SetExtentInMM(1, 20);
986  newDummy->SetExtentInMM(2, 8);
987  MITK_ASSERT_EQUAL(dummy, newDummy, "Dummy BBox");
988  }
989 
990  // void TestSetTimeBounds(){
991  // mitk::TimeBounds timeBounds;
992  // timeBounds[0] = 1;
993  // timeBounds[1] = 9;
994 
995  // DummyTestClass::Pointer dummy = DummyTestClass::New();
996  // dummy->SetTimeBounds(timeBounds);
997  // mitk::TimeBounds timeBounds2 = dummy->GetTimeBounds();
998 
999  // CPPUNIT_ASSERT(timeBounds[0]==timeBounds2[0]);
1000  // CPPUNIT_ASSERT(timeBounds[1]==timeBounds2[1]);
1001 
1002  // //undo changes, new and changed object need to be the same!
1003  // timeBounds[0]=mitk::ScalarTypeNumericTraits::NonpositiveMin();
1004  // timeBounds[1]=mitk::ScalarTypeNumericTraits::max();
1005 
1006  // DummyTestClass::Pointer newDummy = DummyTestClass::New();
1007  // CPPUNIT_ASSERT(mitk::Equal(dummy,newDummy,mitk::eps,true));
1008  //}
1009 
1010  void TestIs2DConvertable()
1011  {
1012  DummyTestClass::Pointer dummy = DummyTestClass::New();
1013 
1014  // new initialized geometry is 2D convertable
1015  CPPUNIT_ASSERT(dummy->Is2DConvertable());
1016 
1017  // Wrong Spacing needs to fail
1018  dummy->SetSpacing(anotherSpacing);
1019  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1020  // undo
1021  dummy->SetSpacing(aSpacing);
1022  CPPUNIT_ASSERT(dummy->Is2DConvertable());
1023 
1024  // Wrong Origin needs to fail
1025  dummy->SetOrigin(anotherPoint);
1026  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1027  // undo
1028  dummy->SetOrigin(aPoint);
1029  CPPUNIT_ASSERT(dummy->Is2DConvertable());
1030 
1031  // third dimension must not be transformed
1032  mitk::AffineTransform3D::Pointer dummyTransform = mitk::AffineTransform3D::New();
1033  mitk::AffineTransform3D::MatrixType dummyMatrix;
1034  dummyMatrix.SetIdentity();
1035  dummyTransform->SetMatrix(dummyMatrix);
1036  dummy->SetIndexToWorldTransform(dummyTransform);
1037 
1038  // identity matrix is 2DConvertable
1039  CPPUNIT_ASSERT(dummy->Is2DConvertable());
1040 
1041  dummyMatrix(0, 2) = 3;
1042  dummyTransform->SetMatrix(dummyMatrix);
1043  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1044 
1045  dummyMatrix.SetIdentity();
1046  dummyMatrix(1, 2) = 0.4;
1047  dummyTransform->SetMatrix(dummyMatrix);
1048  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1049 
1050  dummyMatrix.SetIdentity();
1051  dummyMatrix(2, 2) = 3;
1052  dummyTransform->SetMatrix(dummyMatrix);
1053  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1054 
1055  dummyMatrix.SetIdentity();
1056  dummyMatrix(2, 1) = 3;
1057  dummyTransform->SetMatrix(dummyMatrix);
1058  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1059 
1060  dummyMatrix.SetIdentity();
1061  dummyMatrix(2, 0) = 3;
1062  dummyTransform->SetMatrix(dummyMatrix);
1063  CPPUNIT_ASSERT(dummy->Is2DConvertable() == false);
1064 
1065  // undo changes, new and changed object need to be the same!
1066  dummyMatrix.SetIdentity();
1067  dummyTransform->SetMatrix(dummyMatrix);
1068  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1069  MITK_ASSERT_EQUAL(dummy, newDummy, "Is 2D convertable");
1070  }
1071 
1072  void TestGetCornerPoint()
1073  {
1074  DummyTestClass::Pointer dummy = DummyTestClass::New();
1075  dummy->SetIndexToWorldTransform(anotherTransform);
1076  double bounds[6] = {0, 11, 0, 12, 0, 13};
1077  dummy->SetFloatBounds(bounds);
1078 
1079  mitk::Point3D corner, refCorner;
1080 
1081  // Corner 0
1082  mitk::FillVector3D(refCorner, bounds[0], bounds[2], bounds[4]);
1083  refCorner = anotherTransform->TransformPoint(refCorner);
1084  corner = dummy->GetCornerPoint(0);
1085  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1086  corner = dummy->GetCornerPoint(true, true, true);
1087  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1088 
1089  // Corner 1
1090  mitk::FillVector3D(refCorner, bounds[0], bounds[2], bounds[5]);
1091  refCorner = anotherTransform->TransformPoint(refCorner);
1092  corner = dummy->GetCornerPoint(1);
1093  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1094  corner = dummy->GetCornerPoint(true, true, false);
1095  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1096 
1097  // Corner 2
1098  mitk::FillVector3D(refCorner, bounds[0], bounds[3], bounds[4]);
1099  refCorner = anotherTransform->TransformPoint(refCorner);
1100  corner = dummy->GetCornerPoint(2);
1101  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1102  corner = dummy->GetCornerPoint(true, false, true);
1103  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1104 
1105  // Corner 3
1106  mitk::FillVector3D(refCorner, bounds[0], bounds[3], bounds[5]);
1107  refCorner = anotherTransform->TransformPoint(refCorner);
1108  corner = dummy->GetCornerPoint(3);
1109  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1110  corner = dummy->GetCornerPoint(true, false, false);
1111  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1112 
1113  // Corner 4
1114  mitk::FillVector3D(refCorner, bounds[1], bounds[2], bounds[4]);
1115  refCorner = anotherTransform->TransformPoint(refCorner);
1116  corner = dummy->GetCornerPoint(4);
1117  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1118  corner = dummy->GetCornerPoint(false, true, true);
1119  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1120 
1121  // Corner 5
1122  mitk::FillVector3D(refCorner, bounds[1], bounds[2], bounds[5]);
1123  refCorner = anotherTransform->TransformPoint(refCorner);
1124  corner = dummy->GetCornerPoint(5);
1125  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1126  corner = dummy->GetCornerPoint(false, true, false);
1127  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1128 
1129  // Corner 6
1130  mitk::FillVector3D(refCorner, bounds[1], bounds[3], bounds[4]);
1131  refCorner = anotherTransform->TransformPoint(refCorner);
1132  corner = dummy->GetCornerPoint(6);
1133  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1134  corner = dummy->GetCornerPoint(false, false, true);
1135  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1136 
1137  // Corner 7
1138  mitk::FillVector3D(refCorner, bounds[1], bounds[3], bounds[5]);
1139  refCorner = anotherTransform->TransformPoint(refCorner);
1140  corner = dummy->GetCornerPoint(7);
1141  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1142  corner = dummy->GetCornerPoint(false, false, false);
1143  CPPUNIT_ASSERT(mitk::Equal(refCorner, corner));
1144 
1145  // Wrong Corner needs to fail
1146  CPPUNIT_ASSERT_THROW(dummy->GetCornerPoint(20), itk::ExceptionObject);
1147 
1148  // dummy geometry must not have changed!
1149  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1150  newDummy->SetIndexToWorldTransform(anotherTransform);
1151  newDummy->SetFloatBounds(bounds);
1152  MITK_ASSERT_EQUAL(dummy, newDummy, "Corner point");
1153  }
1154 
1155  void TestExtentInMM()
1156  {
1157  DummyTestClass::Pointer dummy = DummyTestClass::New();
1158  dummy->SetExtentInMM(0, 50);
1159  CPPUNIT_ASSERT(mitk::Equal(50., dummy->GetExtentInMM(0)));
1160  // Vnl Matrix has changed. The next line only works because the spacing is 1!
1161  CPPUNIT_ASSERT(
1162  mitk::Equal(50., dummy->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(0).magnitude()));
1163 
1164  // Smaller extent than original
1165  dummy->SetExtentInMM(0, 5);
1166  CPPUNIT_ASSERT(mitk::Equal(5., dummy->GetExtentInMM(0)));
1167  CPPUNIT_ASSERT(
1168  mitk::Equal(5., dummy->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(0).magnitude()));
1169 
1170  dummy->SetExtentInMM(1, 4);
1171  CPPUNIT_ASSERT(mitk::Equal(4., dummy->GetExtentInMM(1)));
1172  CPPUNIT_ASSERT(
1173  mitk::Equal(4., dummy->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(1).magnitude()));
1174 
1175  dummy->SetExtentInMM(2, 2.5);
1176  CPPUNIT_ASSERT(mitk::Equal(2.5, dummy->GetExtentInMM(2)));
1177  CPPUNIT_ASSERT(
1178  mitk::Equal(2.5, dummy->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2).magnitude()));
1179  }
1180 
1181  void TestGetAxisVector()
1182  {
1183  DummyTestClass::Pointer dummy = DummyTestClass::New();
1184  dummy->SetIndexToWorldTransform(anotherTransform);
1185  double bounds[6] = {0, 11, 0, 12, 0, 13};
1186  dummy->SetFloatBounds(bounds);
1187 
1188  mitk::Vector3D vector;
1189  mitk::FillVector3D(vector, bounds[1], 0, 0);
1190  dummy->IndexToWorld(vector, vector);
1191  CPPUNIT_ASSERT(mitk::Equal(dummy->GetAxisVector(0), vector));
1192 
1193  mitk::FillVector3D(vector, 0, bounds[3], 0);
1194  dummy->IndexToWorld(vector, vector);
1195  CPPUNIT_ASSERT(mitk::Equal(dummy->GetAxisVector(1), vector));
1196 
1197  mitk::FillVector3D(vector, 0, 0, bounds[5]);
1198  dummy->IndexToWorld(vector, vector);
1199  CPPUNIT_ASSERT(mitk::Equal(dummy->GetAxisVector(2), vector));
1200  }
1201 
1202  void TestGetCenter()
1203  {
1204  DummyTestClass::Pointer dummy = DummyTestClass::New();
1205  dummy->SetIndexToWorldTransform(anotherTransform);
1206  double bounds[6] = {0, 11, 2, 12, 1, 13};
1207  dummy->SetFloatBounds(bounds);
1208 
1209  mitk::Point3D refCenter;
1210  for (int i = 0; i < 3; i++)
1211  refCenter.SetElement(i, (bounds[2 * i] + bounds[2 * i + 1]) / 2.0);
1212 
1213  dummy->IndexToWorld(refCenter, refCenter);
1214 
1215  CPPUNIT_ASSERT(mitk::Equal(dummy->GetCenter(), refCenter));
1216  }
1217 
1218  void TestGetDiagonalLength()
1219  {
1220  DummyTestClass::Pointer dummy = DummyTestClass::New();
1221  double bounds[6] = {1, 3, 5, 8, 7.5, 11.5};
1222  dummy->SetFloatBounds(bounds);
1223  // 3-1=2, 8-5=3, 11.5-7.5=4; 2^2+3^2+4^2 = 29
1224  double expectedLength = sqrt(29.);
1225 
1226  CPPUNIT_ASSERT(mitk::Equal(expectedLength, dummy->GetDiagonalLength(), mitk::eps, true));
1227  CPPUNIT_ASSERT(mitk::Equal(29., dummy->GetDiagonalLength2(), mitk::eps, true));
1228 
1229  // dummy must not have changed
1230  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1231  newDummy->SetFloatBounds(bounds);
1232  MITK_ASSERT_EQUAL(dummy, newDummy, "Diagonal length");
1233  }
1234 
1235  void TestGetExtent()
1236  {
1237  DummyTestClass::Pointer dummy = DummyTestClass::New();
1238  double bounds[6] = {1, 3, 5, 8, 7.5, 11.5};
1239  dummy->SetFloatBounds(bounds);
1240 
1241  CPPUNIT_ASSERT(mitk::Equal(2., dummy->GetExtent(0)));
1242  CPPUNIT_ASSERT(mitk::Equal(3., dummy->GetExtent(1)));
1243  CPPUNIT_ASSERT(mitk::Equal(4., dummy->GetExtent(2)));
1244 
1245  // dummy must not have changed
1246  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1247  newDummy->SetFloatBounds(bounds);
1248  MITK_ASSERT_EQUAL(dummy, newDummy, "Extend");
1249  }
1250 
1251  void TestIsInside()
1252  {
1253  DummyTestClass::Pointer dummy = DummyTestClass::New();
1254  double bounds[6] = {1, 3, 5, 8, 7.5, 11.5};
1255  dummy->SetFloatBounds(bounds);
1256 
1257  mitk::Point3D insidePoint;
1258  mitk::Point3D outsidePoint;
1259 
1260  mitk::FillVector3D(insidePoint, 2, 6, 7.6);
1261  mitk::FillVector3D(outsidePoint, 0, 9, 8.2);
1262 
1263  CPPUNIT_ASSERT(dummy->IsIndexInside(insidePoint));
1264  CPPUNIT_ASSERT(false == dummy->IsIndexInside(outsidePoint));
1265 
1266  dummy->IndexToWorld(insidePoint, insidePoint);
1267  dummy->IndexToWorld(outsidePoint, outsidePoint);
1268 
1269  CPPUNIT_ASSERT(dummy->IsInside(insidePoint));
1270  CPPUNIT_ASSERT(false == dummy->IsInside(outsidePoint));
1271 
1272  // dummy must not have changed
1273  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1274  newDummy->SetFloatBounds(bounds);
1275  MITK_ASSERT_EQUAL(dummy, newDummy, "Is inside");
1276  }
1277 
1278  void TestInitialize()
1279  {
1280  // test standard constructor
1281  DummyTestClass::Pointer dummy1 = DummyTestClass::New();
1282 
1283  DummyTestClass::Pointer dummy2 = DummyTestClass::New();
1284  dummy2->SetOrigin(anotherPoint);
1285  dummy2->SetBounds(anotherBoundingBox->GetBounds());
1286  // mitk::TimeBounds timeBounds;
1287  // timeBounds[0] = 1;
1288  // timeBounds[1] = 9;
1289  // dummy2->SetTimeBounds(timeBounds);
1290  dummy2->SetIndexToWorldTransform(anotherTransform);
1291  dummy2->SetSpacing(anotherSpacing);
1292 
1293  dummy1->InitializeGeometry(dummy2);
1294 
1295  MITK_ASSERT_EQUAL(dummy1, dummy2, "Initialize 1");
1296 
1297  dummy1->Initialize();
1298 
1299  DummyTestClass::Pointer dummy3 = DummyTestClass::New();
1300  MITK_ASSERT_EQUAL(dummy3, dummy1, "Initialize 2");
1301  }
1302 
1303  void TestGetMatrixColumn()
1304  {
1305  DummyTestClass::Pointer dummy = DummyTestClass::New();
1306  dummy->SetIndexToWorldTransform(anotherTransform);
1307  mitk::Vector3D testVector, refVector;
1308 
1309  testVector.SetVnlVector(dummy->GetMatrixColumn(0));
1310  mitk::FillVector3D(refVector, 1, 0, 0);
1311  CPPUNIT_ASSERT(testVector == refVector);
1312 
1313  testVector.SetVnlVector(dummy->GetMatrixColumn(1));
1314  mitk::FillVector3D(refVector, 0, 2, 0);
1315  CPPUNIT_ASSERT(testVector == refVector);
1316 
1317  testVector.SetVnlVector(dummy->GetMatrixColumn(2));
1318  mitk::FillVector3D(refVector, 0, 0, 1);
1319  CPPUNIT_ASSERT(testVector == refVector);
1320 
1321  // dummy must not have changed
1322  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1323  newDummy->SetIndexToWorldTransform(anotherTransform);
1324  MITK_ASSERT_EQUAL(dummy, newDummy, "GetMatrixColumn");
1325  }
1326 
1327  /*
1328 
1329  void (){
1330  DummyTestClass::Pointer dummy = DummyTestClass::New();
1331 
1332  CPPUNIT_ASSERT();
1333 
1334  //undo changes, new and changed object need to be the same!
1335 
1336  DummyTestClass::Pointer newDummy = DummyTestClass::New();
1337  CPPUNIT_ASSERT(mitk::Equal(dummy,newDummy,mitk::eps,true));
1338  }
1339 
1340  */
1341 }; // end class mitkBaseGeometryTestSuite
1342 
1343 MITK_TEST_SUITE_REGISTRATION(mitkBaseGeometry)
bool EqualArray(TArrayType1 &arrayType1, TArrayType2 &arrayType2, int size, ScalarType eps=mitk::eps, bool verbose=false)
Definition: mitkArray.h:128
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
void GetRotation(const mitk::BaseGeometry *geometry, TMatrixType &itkmatrix)
#define mitkNewMacro1Param(classname, type)
Definition: mitkCommon.h:72
bool MatrixEqualElementWise(const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix1, const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix2, mitk::ScalarType epsilon=mitk::eps)
Check for element-wise matrix equality with a user defined accuracy.
Definition: mitkMatrix.h:140
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Base class of all Operation-classes.
Definition: mitkOperation.h:29
vnl_vector< ScalarType > VnlVector
Definition: mitkVector.h:134
double ScalarType
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:28
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Constants for most interaction classes, due to the generic StateMachines.
int testIndexAndWorldConsistencyForVectors(mitk::Geometry3D *geometry3d)
static Matrix3D rotation
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
itk::SmartPointer< Self > Pointer
#define MITK_ASSERT_NOT_EQUAL(OBJ1, OBJ2, MSG)
Testing macro to test if two objects are not equal.
virtual void PreSetSpacing(const mitk::Vector3D &)
PreSetSpacing.
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:40
itk::BoundingBox< unsigned long, 3, mitk::ScalarType > BoundingBoxType
Test fixture for parameterized tests.
Operation that handles all actions on one Point.
#define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG)
Testing macro to test if two objects are equal.
void vtk2itk(const Tin &in, Tout &out)
int testIndexAndWorldConsistencyForIndex(mitk::Geometry3D *geometry3d)
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.
The ScaleOperation is an operation to scale any mitk::BaseGeometry.
itk::BoundingBox< unsigned long, 3, mitk::ScalarType > BoundingBox
MITKCORE_EXPORT const ScalarType eps
int testIndexAndWorldConsistency(mitk::Geometry3D *geometry3d)
itk::LightObject::Pointer InternalClone() const override=0
clones the geometry
Operation, that holds everything necessary for an rotation operation on mitk::BaseData.
BoundingBoxType::Pointer BoundingBoxPointer
BaseGeometry Describes the geometry of a data object.
BoundingBoxType::BoundsArrayType BoundsArrayType