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