Medical Imaging Interaction Toolkit  2016.11.0
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,
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 #include "mitkBaseProperty.h"
17 #include "mitkDataNode.h"
18 #include "mitkGeometry3D.h"
19 #include "mitkImage.h"
20 #include "mitkInteractionConst.h"
21 #include "mitkPlaneGeometry.h"
23 #include "mitkRotationOperation.h"
26 #include "mitkStringProperty.h"
27 #include "mitkSurface.h"
28 #include "mitkTestingMacros.h"
29 #include "usGetModuleContext.h"
30 #include "usModuleContext.h"
31 #include "usServiceReference.h"
32 #include "vnl/vnl_vector.h"
33 #include <itkAffineGeometryFrame.h>
34 
35 std::vector<mitk::PlaneGeometry::Pointer> m_Geometries;
36 std::vector<unsigned int> m_SliceIndices;
37 
39 
41 {
42  // Getting Service
45  m_Service = us::GetModuleContext()->GetService(serviceRef);
46 
47  if (m_Service == 0)
48  return EXIT_FAILURE;
49 
50  // Creating different Geometries
51  m_Geometries.reserve(100);
54  for (unsigned int i = 0; i < 100; ++i)
55  {
57 
58  mitk::ScalarType width = 256 + (0.01 * i);
59  mitk::ScalarType height = 256 + (0.002 * i);
60 
61  mitk::Vector3D right;
62  mitk::Vector3D down;
63  right[0] = 1;
64  right[1] = i;
65  right[2] = 0.5;
66  down[0] = i * 0.02;
67  down[1] = 1;
68  down[2] = i * 0.03;
69 
70  mitk::Vector3D spacing;
71  mitk::FillVector3D(spacing, 1.0 * 0.02 * i, 1.0 * 0.15 * i, 1.0);
72 
73  mitk::Vector3D rightVector;
74  mitk::FillVector3D(rightVector, 0.02 * (i + 1), 0 + (0.05 * i), 1.0);
75 
76  mitk::Vector3D downVector;
77  mitk::FillVector3D(downVector, 1, 3 - 0.01 * i, 0.0345 * i);
78 
79  vnl_vector<mitk::ScalarType> normal = vnl_cross_3d(rightVector.GetVnlVector(), downVector.GetVnlVector());
80  normal.normalize();
81  normal *= 1.5;
82 
83  mitk::Vector3D origin;
84  origin.Fill(1);
85  origin[0] = 12 + 0.03 * i;
86 
88  mitk::Matrix3D matrix;
89  matrix.GetVnlMatrix().set_column(0, rightVector.GetVnlVector());
90  matrix.GetVnlMatrix().set_column(1, downVector.GetVnlVector());
91  matrix.GetVnlMatrix().set_column(2, normal);
92  transform->SetMatrix(matrix);
93  transform->SetOffset(origin);
94 
95  plane->InitializeStandardPlane(width, height, transform, views[i % 3], i, true, false);
96 
97  m_Geometries.push_back(plane);
98  }
99 
100  return EXIT_SUCCESS;
101 }
102 
104 {
105  MITK_TEST_OUTPUT(<< "Starting Test: ######### A d d P l a n e P o s i t i o n #########");
106 
107  MITK_TEST_CONDITION(m_Service != NULL, "Testing getting of PlanePositionManagerService");
108 
109  unsigned int currentID(m_Service->AddNewPlanePosition(m_Geometries.at(0), 0));
110 
111  bool error = ((m_Service->GetNumberOfPlanePositions() != 1) || (currentID != 0));
112 
113  if (error)
114  {
115  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == 1, "Checking for correct number of planepositions");
116  MITK_TEST_CONDITION(currentID == 0, "Testing for correct ID");
117  return EXIT_FAILURE;
118  }
119 
120  // Adding new planes
121  for (unsigned int i = 1; i < m_Geometries.size(); ++i)
122  {
123  unsigned int newID = m_Service->AddNewPlanePosition(m_Geometries.at(i), i);
124  error = ((m_Service->GetNumberOfPlanePositions() != i + 1) || (newID != (currentID + 1)));
125 
126  if (error)
127  {
128  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == i + 1,
129  "Checking for correct number of planepositions");
130  MITK_TEST_CONDITION(newID == (currentID + 1), "Testing for correct ID");
131  MITK_TEST_OUTPUT(<< "New: " << newID << " Last: " << currentID);
132  return EXIT_FAILURE;
133  }
134  currentID = newID;
135  }
136 
137  unsigned int numberOfPlanePos = m_Service->GetNumberOfPlanePositions();
138 
139  // Adding existing planes -> nothing should change
140  for (unsigned int i = 0; i < (m_Geometries.size() - 1) * 0.5; ++i)
141  {
142  unsigned int newID = m_Service->AddNewPlanePosition(m_Geometries.at(i * 2), i * 2);
143  error = ((m_Service->GetNumberOfPlanePositions() != numberOfPlanePos) || (newID != i * 2));
144  if (error)
145  {
146  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == numberOfPlanePos,
147  "Checking for correct number of planepositions");
148  MITK_TEST_CONDITION(newID == i * 2, "Testing for correct ID");
149  return EXIT_FAILURE;
150  }
151  }
152 
153  return EXIT_SUCCESS;
154 }
155 
157 {
158  bool error(true);
159 
160  MITK_TEST_OUTPUT(<< "Starting Test: ######### G e t P l a n e P o s i t i o n #########");
161 
162  // Testing for existing planepositions
163  for (unsigned int i = 0; i < m_Geometries.size(); ++i)
164  {
165  auto plane = m_Geometries.at(i);
166  auto op = m_Service->GetPlanePosition(i);
167  error =
168  (!mitk::Equal(op->GetHeight(), plane->GetExtent(1)) || !mitk::Equal(op->GetWidth(), plane->GetExtent(0)) ||
169  !mitk::Equal(op->GetSpacing(), plane->GetSpacing()) ||
170  !mitk::Equal(op->GetTransform()->GetOffset(), plane->GetIndexToWorldTransform()->GetOffset()) ||
171  !mitk::Equal(op->GetDirectionVector().GetVnlVector(),
172  plane->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2).normalize()) ||
173  !mitk::MatrixEqualElementWise(op->GetTransform()->GetMatrix(), plane->GetIndexToWorldTransform()->GetMatrix()));
174 
175  if (error)
176  {
177  MITK_TEST_OUTPUT(<< "Iteration: " << i)
179  mitk::Equal(op->GetHeight(), plane->GetExtent(1)) && mitk::Equal(op->GetWidth(), plane->GetExtent(0)),
180  "Checking for correct extent");
181  MITK_TEST_CONDITION(mitk::Equal(op->GetSpacing(), plane->GetSpacing()), "Checking for correct spacing");
182  MITK_TEST_CONDITION(mitk::Equal(op->GetTransform()->GetOffset(), plane->GetIndexToWorldTransform()->GetOffset()),
183  "Checking for correct offset");
184  MITK_INFO << "Op: " << op->GetDirectionVector()
185  << " plane: " << plane->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2) << "\n";
186  MITK_TEST_CONDITION(mitk::Equal(op->GetDirectionVector().GetVnlVector(),
187  plane->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix().get_column(2)),
188  "Checking for correct direction");
190  mitk::MatrixEqualElementWise(op->GetTransform()->GetMatrix(), plane->GetIndexToWorldTransform()->GetMatrix()),
191  "Checking for correct matrix");
192  return EXIT_FAILURE;
193  }
194  }
195 
196  // Testing for not existing planepositions
197  error = (m_Service->GetPlanePosition(100000000) != 0 || m_Service->GetPlanePosition(-1) != 0);
198 
199  if (error)
200  {
201  MITK_TEST_CONDITION(m_Service->GetPlanePosition(100000000) == 0, "Trying to get non existing pos");
202  MITK_TEST_CONDITION(m_Service->GetPlanePosition(-1) == 0, "Trying to get non existing pos");
203  return EXIT_FAILURE;
204  }
205 
206  return EXIT_SUCCESS;
207 }
208 
210 {
211  MITK_TEST_OUTPUT(<< "Starting Test: ######### R e m o v e P l a n e P o s i t i o n #########");
212  unsigned int size = m_Service->GetNumberOfPlanePositions();
213 
214  // Testing for invalid IDs
215  bool removed = m_Service->RemovePlanePosition(-1);
216  removed = m_Service->RemovePlanePosition(1000000);
217  unsigned int size2 = m_Service->GetNumberOfPlanePositions();
218 
219  if (removed)
220  {
221  MITK_TEST_CONDITION(removed == false, "Testing remove not existing planepositions");
222  MITK_TEST_CONDITION(size == size2, "Testing remove not existing planepositions");
223  return EXIT_FAILURE;
224  }
225 
226  // Testing for valid IDs
227  for (unsigned int i = 0; i < m_Geometries.size() * 0.5; i++)
228  {
229  removed = m_Service->RemovePlanePosition(i);
230  unsigned int size2 = m_Service->GetNumberOfPlanePositions();
231  removed = (size2 == (size - (i + 1)));
232  if (!removed)
233  {
234  MITK_TEST_CONDITION(removed == true, "Testing remove existing planepositions");
235  MITK_TEST_CONDITION(size == (size - i + 1), "Testing remove existing planepositions");
236  return EXIT_FAILURE;
237  }
238  }
239 
240  return EXIT_SUCCESS;
241 }
242 
244 {
245  MITK_TEST_OUTPUT(<< "Starting Test: ######### R e m o v e A l l #########");
246 
247  unsigned int numPos = m_Service->GetNumberOfPlanePositions();
248  MITK_INFO << numPos;
249 
250  m_Service->RemoveAllPlanePositions();
251 
252  bool error(true);
253 
254  error = (m_Service->GetNumberOfPlanePositions() != 0 || m_Service->GetPlanePosition(60) != 0);
255 
256  if (error)
257  {
258  MITK_TEST_CONDITION(m_Service->GetNumberOfPlanePositions() == 0, "Testing remove all pos");
259  MITK_TEST_CONDITION(m_Service->GetPlanePosition(60) == 0, "Testing remove all pos");
260  return EXIT_FAILURE;
261  }
262  return EXIT_SUCCESS;
263 }
264 
266 {
267  MITK_TEST_BEGIN("PlanePositionManager");
268 
269  SetUpBeforeTest();
270  int result;
271  MITK_TEST_CONDITION_REQUIRED((result = testAddPlanePosition()) == EXIT_SUCCESS, "");
272  MITK_TEST_CONDITION_REQUIRED((result = testGetPlanePosition()) == EXIT_SUCCESS, "");
273  MITK_TEST_CONDITION_REQUIRED((result = testRemovePlanePosition()) == EXIT_SUCCESS, "");
274  MITK_TEST_CONDITION_REQUIRED((result = testRemoveAll()) == EXIT_SUCCESS, "");
275 
276  MITK_TEST_END();
277 }
ServiceReferenceU GetServiceReference(const std::string &clazz)
itk::SmartPointer< Self > Pointer
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
#define MITK_INFO
Definition: mitkLogMacros.h:22
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:110
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.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.