Medical Imaging Interaction Toolkit  2018.4.99-936b789b
Medical Imaging Interaction Toolkit
mitkPlanePositionManagerTest.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 #include "mitkBaseProperty.h"
13 #include "mitkDataNode.h"
14 #include "mitkGeometry3D.h"
15 #include "mitkImage.h"
16 #include "mitkInteractionConst.h"
17 #include "mitkPlaneGeometry.h"
19 #include "mitkRotationOperation.h"
22 #include "mitkStringProperty.h"
23 #include "mitkSurface.h"
24 #include "mitkTestingMacros.h"
25 #include "usGetModuleContext.h"
26 #include "usModuleContext.h"
27 #include "usServiceReference.h"
28 #include "vnl/vnl_vector.h"
29 #include <itkAffineGeometryFrame.h>
30 
31 std::vector<mitk::PlaneGeometry::Pointer> m_Geometries;
32 std::vector<unsigned int> m_SliceIndices;
33 
35 
37 {
38  // Getting Service
41  m_Service = us::GetModuleContext()->GetService(serviceRef);
42 
43  if (m_Service == nullptr)
44  return EXIT_FAILURE;
45 
46  // Creating different Geometries
47  m_Geometries.reserve(100);
50  for (unsigned int i = 0; i < 100; ++i)
51  {
53 
54  mitk::ScalarType width = 256 + (0.01 * i);
55  mitk::ScalarType height = 256 + (0.002 * i);
56 
57  mitk::Vector3D right;
58  mitk::Vector3D down;
59  right[0] = 1;
60  right[1] = i;
61  right[2] = 0.5;
62  down[0] = i * 0.02;
63  down[1] = 1;
64  down[2] = i * 0.03;
65 
66  mitk::Vector3D spacing;
67  mitk::FillVector3D(spacing, 1.0 * 0.02 * i, 1.0 * 0.15 * i, 1.0);
68 
69  mitk::Vector3D rightVector;
70  mitk::FillVector3D(rightVector, 0.02 * (i + 1), 0 + (0.05 * i), 1.0);
71 
72  mitk::Vector3D downVector;
73  mitk::FillVector3D(downVector, 1, 3 - 0.01 * i, 0.0345 * i);
74 
75  vnl_vector<mitk::ScalarType> normal = vnl_cross_3d(rightVector.GetVnlVector(), downVector.GetVnlVector());
76  normal.normalize();
77  normal *= 1.5;
78 
79  mitk::Vector3D origin;
80  origin.Fill(1);
81  origin[0] = 12 + 0.03 * i;
82 
83  mitk::AffineTransform3D::Pointer transform = mitk::AffineTransform3D::New();
84  mitk::Matrix3D matrix;
85  matrix.GetVnlMatrix().set_column(0, rightVector.GetVnlVector());
86  matrix.GetVnlMatrix().set_column(1, downVector.GetVnlVector());
87  matrix.GetVnlMatrix().set_column(2, normal);
88  transform->SetMatrix(matrix);
89  transform->SetOffset(origin);
90 
91  plane->InitializeStandardPlane(width, height, transform, views[i % 3], i, true, false);
92 
93  m_Geometries.push_back(plane);
94  }
95 
96  return EXIT_SUCCESS;
97 }
98 
100 {
101  MITK_TEST_OUTPUT(<< "Starting Test: ######### A d d P l a n e P o s i t i o n #########");
102 
103  MITK_TEST_CONDITION(m_Service != nullptr, "Testing getting of PlanePositionManagerService");
104 
105  unsigned int currentID(m_Service->AddNewPlanePosition(m_Geometries.at(0), 0));
106 
107  bool error = ((m_Service->GetNumberOfPlanePositions() != 1) || (currentID != 0));
108 
109  if (error)
110  {
111  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == 1, "Checking for correct number of planepositions");
112  MITK_TEST_CONDITION(currentID == 0, "Testing for correct ID");
113  return EXIT_FAILURE;
114  }
115 
116  // Adding new planes
117  for (unsigned int i = 1; i < m_Geometries.size(); ++i)
118  {
119  unsigned int newID = m_Service->AddNewPlanePosition(m_Geometries.at(i), i);
120  error = ((m_Service->GetNumberOfPlanePositions() != i + 1) || (newID != (currentID + 1)));
121 
122  if (error)
123  {
124  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == i + 1,
125  "Checking for correct number of planepositions");
126  MITK_TEST_CONDITION(newID == (currentID + 1), "Testing for correct ID");
127  MITK_TEST_OUTPUT(<< "New: " << newID << " Last: " << currentID);
128  return EXIT_FAILURE;
129  }
130  currentID = newID;
131  }
132 
133  unsigned int numberOfPlanePos = m_Service->GetNumberOfPlanePositions();
134 
135  // Adding existing planes -> nothing should change
136  for (unsigned int i = 0; i < (m_Geometries.size() - 1) * 0.5; ++i)
137  {
138  unsigned int newID = m_Service->AddNewPlanePosition(m_Geometries.at(i * 2), i * 2);
139  error = ((m_Service->GetNumberOfPlanePositions() != numberOfPlanePos) || (newID != i * 2));
140  if (error)
141  {
142  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == numberOfPlanePos,
143  "Checking for correct number of planepositions");
144  MITK_TEST_CONDITION(newID == i * 2, "Testing for correct ID");
145  return EXIT_FAILURE;
146  }
147  }
148 
149  return EXIT_SUCCESS;
150 }
151 
153 {
154  bool error(true);
155 
156  MITK_TEST_OUTPUT(<< "Starting Test: ######### G e t P l a n e P o s i t i o n #########");
157 
158  // Testing for existing planepositions
159  for (unsigned int i = 0; i < m_Geometries.size(); ++i)
160  {
161  auto plane = m_Geometries.at(i);
162  auto op = m_Service->GetPlanePosition(i);
163  error =
164  (!mitk::Equal(op->GetHeight(), plane->GetExtent(1)) || !mitk::Equal(op->GetWidth(), plane->GetExtent(0)) ||
165  !mitk::Equal(op->GetSpacing(), plane->GetSpacing()) ||
166  !mitk::Equal(op->GetTransform()->GetOffset(), plane->GetIndexToWorldTransform()->GetOffset()) ||
167  !mitk::Equal(op->GetDirectionVector().GetVnlVector(),
168  plane->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2).normalize()) ||
169  !mitk::MatrixEqualElementWise(op->GetTransform()->GetMatrix(), plane->GetIndexToWorldTransform()->GetMatrix()));
170 
171  if (error)
172  {
173  MITK_TEST_OUTPUT(<< "Iteration: " << i)
175  mitk::Equal(op->GetHeight(), plane->GetExtent(1)) && mitk::Equal(op->GetWidth(), plane->GetExtent(0)),
176  "Checking for correct extent");
177  MITK_TEST_CONDITION(mitk::Equal(op->GetSpacing(), plane->GetSpacing()), "Checking for correct spacing");
178  MITK_TEST_CONDITION(mitk::Equal(op->GetTransform()->GetOffset(), plane->GetIndexToWorldTransform()->GetOffset()),
179  "Checking for correct offset");
180  MITK_INFO << "Op: " << op->GetDirectionVector()
181  << " plane: " << plane->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2) << "\n";
182  MITK_TEST_CONDITION(mitk::Equal(op->GetDirectionVector().GetVnlVector(),
183  plane->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2)),
184  "Checking for correct direction");
186  mitk::MatrixEqualElementWise(op->GetTransform()->GetMatrix(), plane->GetIndexToWorldTransform()->GetMatrix()),
187  "Checking for correct matrix");
188  return EXIT_FAILURE;
189  }
190  }
191 
192  // Testing for not existing planepositions
193  error = (m_Service->GetPlanePosition(100000000) != nullptr || m_Service->GetPlanePosition(-1) != nullptr);
194 
195  if (error)
196  {
197  MITK_TEST_CONDITION(m_Service->GetPlanePosition(100000000) == nullptr, "Trying to get non existing pos");
198  MITK_TEST_CONDITION(m_Service->GetPlanePosition(-1) == nullptr, "Trying to get non existing pos");
199  return EXIT_FAILURE;
200  }
201 
202  return EXIT_SUCCESS;
203 }
204 
206 {
207  MITK_TEST_OUTPUT(<< "Starting Test: ######### R e m o v e P l a n e P o s i t i o n #########");
208  unsigned int size = m_Service->GetNumberOfPlanePositions();
209 
210  // Testing for invalid IDs
211  bool removed = m_Service->RemovePlanePosition(-1);
212  removed = m_Service->RemovePlanePosition(1000000);
213  unsigned int size2 = m_Service->GetNumberOfPlanePositions();
214 
215  if (removed)
216  {
217  MITK_TEST_CONDITION(removed == false, "Testing remove not existing planepositions");
218  MITK_TEST_CONDITION(size == size2, "Testing remove not existing planepositions");
219  return EXIT_FAILURE;
220  }
221 
222  // Testing for valid IDs
223  for (unsigned int i = 0; i < m_Geometries.size() * 0.5; i++)
224  {
225  removed = m_Service->RemovePlanePosition(i);
226  unsigned int size2 = m_Service->GetNumberOfPlanePositions();
227  removed = (size2 == (size - (i + 1)));
228  if (!removed)
229  {
230  MITK_TEST_CONDITION(removed == true, "Testing remove existing planepositions");
231  MITK_TEST_CONDITION(size == (size - i + 1), "Testing remove existing planepositions");
232  return EXIT_FAILURE;
233  }
234  }
235 
236  return EXIT_SUCCESS;
237 }
238 
240 {
241  MITK_TEST_OUTPUT(<< "Starting Test: ######### R e m o v e A l l #########");
242 
243  unsigned int numPos = m_Service->GetNumberOfPlanePositions();
244  MITK_INFO << numPos;
245 
246  m_Service->RemoveAllPlanePositions();
247 
248  bool error(true);
249 
250  error = (m_Service->GetNumberOfPlanePositions() != 0 || m_Service->GetPlanePosition(60) != nullptr);
251 
252  if (error)
253  {
254  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == 0, "Testing remove all pos");
255  MITK_TEST_CONDITION(m_Service->GetPlanePosition(60) == nullptr, "Testing remove all pos");
256  return EXIT_FAILURE;
257  }
258  return EXIT_SUCCESS;
259 }
260 
262 {
263  MITK_TEST_BEGIN("PlanePositionManager");
264 
265  SetUpBeforeTest();
266  int result;
267  MITK_TEST_CONDITION_REQUIRED((result = testAddPlanePosition()) == EXIT_SUCCESS, "");
268  MITK_TEST_CONDITION_REQUIRED((result = testGetPlanePosition()) == EXIT_SUCCESS, "");
269  MITK_TEST_CONDITION_REQUIRED((result = testRemovePlanePosition()) == EXIT_SUCCESS, "");
270  MITK_TEST_CONDITION_REQUIRED((result = testRemoveAll()) == EXIT_SUCCESS, "");
271 
272  MITK_TEST_END();
273 }
ServiceReferenceU GetServiceReference(const std::string &clazz)
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
#define MITK_INFO
Definition: mitkLogMacros.h:18
double ScalarType
int testGetPlanePosition()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
Constants for most interaction classes, due to the generic StateMachines.
mitk::RestorePlanePositionOperation * GetPlanePosition(unsigned int ID)
Getter for a specific plane position with a given ID.
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
void * GetService(const ServiceReferenceBase &reference)
std::vector< unsigned int > m_SliceIndices
int testRemovePlanePosition()
#define MITK_TEST_OUTPUT(x)
Output some text.
#define MITK_TEST_CONDITION(COND, MSG)
bool RemovePlanePosition(unsigned int ID)
Removes the plane at the position ID from the list.
mitk::PlanePositionManagerService * m_Service
std::vector< mitk::PlaneGeometry::Pointer > m_Geometries
int mitkPlanePositionManagerTest(int, char *[])
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.
int testAddPlanePosition()
unsigned int GetNumberOfPlanePositions()
Getting the number of all stored planes.
static Pointer New()
unsigned int AddNewPlanePosition(const mitk::PlaneGeometry *plane, unsigned int sliceIndex=0)
Adds a new plane position to the list. If this geometry is identical to one of the list nothing will ...
and MITK_TEST_END()
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void RemoveAllPlanePositions()
Clears the complete positionlist.