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