Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkSlicedGeometry3DTest.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 "mitkImage.h"
14 #include "mitkPlaneGeometry.h"
15 #include "mitkSlicedGeometry3D.h"
16 #include "mitkTestingMacros.h"
17 
18 #include <vnl/vnl_quaternion.h>
19 #include <vnl/vnl_quaternion.hxx>
20 
21 #include <fstream>
22 #include <array>
23 
24 static const mitk::ScalarType slicedGeometryEps = 1e-9; // Set epsilon to float precision for this test
25 
27 {
28  auto planeGeometry = mitk::PlaneGeometry::New();
29  planeGeometry->Initialize();
30  return planeGeometry;
31 }
32 
33 static mitk::SlicedGeometry3D::Pointer createSlicedGeometry(const mitk::Point3D &origin, const mitk::Vector3D &spacing, int numberOfSlices)
34 {
35  auto slicedGeometry = mitk::SlicedGeometry3D::New();
36  slicedGeometry->InitializeSlicedGeometry(static_cast<unsigned int>(numberOfSlices));
37  slicedGeometry->SetOrigin(origin);
38  slicedGeometry->SetSpacing(spacing);
39 
40  for (int i = 0; i < numberOfSlices; ++i)
41  {
42  auto planeGeometry = createPlaneGeometry();
43  slicedGeometry->SetPlaneGeometry(planeGeometry, i);
44  }
45 
46  return slicedGeometry;
47 }
48 
50 {
51  auto slicedGeometry = mitk::SlicedGeometry3D::New();
52  slicedGeometry->InitializeEvenlySpaced(planeGeometry, spacing, numberOfSlices);
53  return slicedGeometry;
54 }
55 
57 {
58  T array;
59  mitk::FillVector3D(array, x, y, z);
60  return array;
61 }
62 
64 {
65  return createArray<mitk::Point3D>(x, y, z);
66 }
67 
69 {
70  return createArray<mitk::Vector3D>(x, y, z);
71 }
72 
74 {
75  MITK_TEST_OUTPUT(<< "====== mitkSlicedGeometry3D_ChangeImageGeometryConsideringOriginOffset_Test() ======");
76 
77  auto origin = createPoint(91.3, -13.3, 0);
78  auto spacing = createVector(1.0, 0.9, 0.3);
79  auto numberOfSlices = 5;
80  auto slicedGeometry = createSlicedGeometry(origin, spacing, numberOfSlices);
81 
82  MITK_TEST_OUTPUT(<< "SlicedGeometry3D isn't an image geometry by default");
83  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetImageGeometry() == false, "");
84 
85  MITK_TEST_OUTPUT(<< "First and last PlaneGeometry in SlicedGeometry3D are not image geometries");
86  auto firstPlaneGeometry = slicedGeometry->GetPlaneGeometry(0);
87  auto lastPlaneGeometry = slicedGeometry->GetPlaneGeometry(numberOfSlices - 1);
88  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetImageGeometry() == false, "");
89  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetImageGeometry() == false, "");
90 
91  auto originOfSlicedGeometry = slicedGeometry->GetOrigin();
92  auto originOfFirstPlaneGeometry = firstPlaneGeometry->GetOrigin();
93  auto originOfLastPlaneGeometry = lastPlaneGeometry->GetOrigin();
94  auto firstCornerPointOfSlicedGeometry = slicedGeometry->GetCornerPoint(0);
95  auto secondCornerPointOfFirstPlaneGeometry = firstPlaneGeometry->GetCornerPoint(1);
96  auto thirdCornerPointOfLastPlaneGeometry = lastPlaneGeometry->GetCornerPoint(2);
97 
98  MITK_TEST_OUTPUT(<< "Calling SlicedGeometry3D::ChangeImageGeometryConsideringOriginOffset(true)");
99  slicedGeometry->ChangeImageGeometryConsideringOriginOffset(true);
100  MITK_TEST_OUTPUT(<< "SlicedGeometry3D is an image geometry");
101  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetImageGeometry() == true, "");
102  MITK_TEST_OUTPUT(<< "First and last PlaneGeometry in SlicedGeometry3D are image geometries");
103  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetImageGeometry() == true, "");
104  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetImageGeometry() == true, "");
105 
106  MITK_TEST_OUTPUT(<< "Corner points of geometries didn't change");
107  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetCornerPoint(0) == firstCornerPointOfSlicedGeometry, "");
108  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetCornerPoint(1) == secondCornerPointOfFirstPlaneGeometry, "");
109  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetCornerPoint(2) == thirdCornerPointOfLastPlaneGeometry, "");
110 
111  MITK_TEST_OUTPUT(<< "Offsets were added to geometry origins");
112  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetOrigin() == originOfSlicedGeometry + slicedGeometry->GetSpacing() * 0.5, "");
113  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetOrigin() == originOfFirstPlaneGeometry + firstPlaneGeometry->GetSpacing() * 0.5, "");
114  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetOrigin() == originOfLastPlaneGeometry + lastPlaneGeometry->GetSpacing() * 0.5, "");
115 
116  MITK_TEST_OUTPUT(<< "Calling SlicedGeometry3D::ChangeImageGeometryConsideringOriginOffset(false)");
117  slicedGeometry->ChangeImageGeometryConsideringOriginOffset(false);
118  MITK_TEST_OUTPUT(<< "SlicedGeometry3D isn't an image geometry anymore");
119  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetImageGeometry() == false, "");
120  MITK_TEST_OUTPUT(<< "First and last PlaneGeometry in SlicedGeometry3D are no image geometries anymore");
121  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetImageGeometry() == false, "");
122  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetImageGeometry() == false, "");
123 
124  MITK_TEST_OUTPUT(<< "Corner points of geometries didn't change");
125  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetCornerPoint(0) == firstCornerPointOfSlicedGeometry, "");
126  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetCornerPoint(1) == secondCornerPointOfFirstPlaneGeometry, "");
127  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetCornerPoint(2) == thirdCornerPointOfLastPlaneGeometry, "");
128 
129  MITK_TEST_OUTPUT(<< "Offsets were subtracted from geometry origins");
130  MITK_TEST_CONDITION_REQUIRED(slicedGeometry->GetOrigin() == originOfSlicedGeometry, "");
131  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry->GetOrigin() == originOfFirstPlaneGeometry, "");
132  MITK_TEST_CONDITION_REQUIRED(lastPlaneGeometry->GetOrigin() == originOfLastPlaneGeometry, "");
133 }
134 
135 int mitkSlicedGeometry3DTest(int, char *[])
136 {
137  mitk::ScalarType width = 100.0;
138  mitk::ScalarType widthInMM = width;
139 
140  mitk::ScalarType height = 200.0;
141  mitk::ScalarType heightInMM = height;
142 
143  mitk::ScalarType thicknessInMM = 3.0;
144 
145  auto right = createVector(widthInMM, 0.0, 0.0);
146  auto bottom = createVector(0.0, heightInMM, 0.0);
147  auto spacing = createVector(1.0, 1.0, thicknessInMM);
148 
149  auto planeGeometry = mitk::PlaneGeometry::New();
150  planeGeometry->InitializeStandardPlane(right, bottom, &spacing);
151 
152  auto numberOfSlices = 5;
153  auto slicedGeometry = createEvenlySpacedSlicedGeometry(planeGeometry, thicknessInMM, numberOfSlices);
154  auto firstPlaneGeometry = slicedGeometry->GetPlaneGeometry(0);
155 
156  MITK_TEST_OUTPUT(<< "Check if first PlaneGeometry of evenly spaced SlicedGeometry is valid");
157  MITK_TEST_CONDITION_REQUIRED(firstPlaneGeometry != nullptr, "");
158  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(firstPlaneGeometry->GetAxisVector(0), planeGeometry->GetAxisVector(0), slicedGeometryEps), "");
159  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(firstPlaneGeometry->GetAxisVector(1), planeGeometry->GetAxisVector(1), slicedGeometryEps), "");
160  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(firstPlaneGeometry->GetAxisVector(2), planeGeometry->GetAxisVector(2), slicedGeometryEps), "");
161 
162  auto lastPlaneGeometry = slicedGeometry->GetPlaneGeometry(numberOfSlices - 1);
163  auto expectedOriginOfLastSlice = createPoint(0.0, 0.0, thicknessInMM * (numberOfSlices - 1));
164 
165  MITK_TEST_OUTPUT(<< "Check if origin of last PlaneGeometry is at expected location");
166  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(lastPlaneGeometry->GetOrigin(), expectedOriginOfLastSlice, slicedGeometryEps), "");
167 
169 
170  std::cout << "[TEST DONE]" << std::endl;
171  return EXIT_SUCCESS;
172 }
static const mitk::ScalarType slicedGeometryEps
static T createArray(mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
static Pointer New()
double ScalarType
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
void mitkSlicedGeometry3D_ChangeImageGeometryConsideringOriginOffset_Test()
static mitk::Point3D createPoint(mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
static mitk::Vector3D createVector(mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
static mitk::SlicedGeometry3D::Pointer createSlicedGeometry(const mitk::Point3D &origin, const mitk::Vector3D &spacing, int numberOfSlices)
#define MITK_TEST_OUTPUT(x)
Output some text.
static mitk::SlicedGeometry3D::Pointer createEvenlySpacedSlicedGeometry(mitk::PlaneGeometry::Pointer planeGeometry, mitk::ScalarType spacing, int numberOfSlices)
int mitkSlicedGeometry3DTest(int, char *[])
static mitk::PlaneGeometry::Pointer createPlaneGeometry()
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
static Pointer New()