Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkPointSetTest.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 "mitkTestFixture.h"
14 #include "mitkTestingMacros.h"
15 
16 #include <mitkInteractionConst.h>
17 #include <mitkNumericTypes.h>
18 #include <mitkPointOperation.h>
19 #include <mitkPointSet.h>
20 
21 #include <fstream>
22 
26 class mitkPointSetTestSuite : public mitk::TestFixture
27 {
28  CPPUNIT_TEST_SUITE(mitkPointSetTestSuite);
29 
30  MITK_TEST(TestIsNotEmpty);
31  MITK_TEST(TestSetSelectInfo);
32  MITK_TEST(TestGetNumberOfSelected);
33  MITK_TEST(TestSearchSelectedPoint);
34  MITK_TEST(TestGetPointIfExists);
35  MITK_TEST(TestSwapPointPositionUpwards);
36  MITK_TEST(TestSwapPointPositionUpwardsNotPossible);
37  MITK_TEST(TestSwapPointPositionDownwards);
38  MITK_TEST(TestSwapPointPositionDownwardsNotPossible);
39  MITK_TEST(TestCreateHoleInThePointIDs);
40  MITK_TEST(TestInsertPointWithPointSpecification);
41  MITK_TEST(TestRemovePointInterface);
42  MITK_TEST(TestMaxIdAccess);
43  MITK_TEST(TestInsertPointAtEnd);
44 
45  CPPUNIT_TEST_SUITE_END();
46 
47 private:
48  mitk::PointSet::Pointer pointSet;
49  static const mitk::PointSet::PointIdentifier selectedPointId = 2;
50 
51 public:
52  void setUp() override
53  {
54  // Create PointSet
55  pointSet = mitk::PointSet::New();
56 
57  // add some points
58  mitk::Point3D point2, point3, point4;
59  point2.Fill(3);
60  point3.Fill(4);
61  point4.Fill(5);
62  pointSet->InsertPoint(2, point2);
63  pointSet->InsertPoint(3, point3);
64  pointSet->InsertPoint(4, point4);
65 
66  mitk::Point3D point1;
67  mitk::FillVector3D(point1, 1.0, 2.0, 3.0);
68  pointSet->InsertPoint(1, point1);
69 
70  mitk::Point3D point0;
71  point0.Fill(1);
72  pointSet->InsertPoint(0, point0);
73 
74  // select point with id 2
75  pointSet->SetSelectInfo(2, true);
76  }
77 
78  void tearDown() override { pointSet = nullptr; }
79  void TestIsNotEmpty()
80  {
81  // PointSet can not be empty!
82  CPPUNIT_ASSERT_EQUAL_MESSAGE("check if the PointSet is not empty ", true, !pointSet->IsEmptyTimeStep(0));
83 
84  /*
85 std::cout << "check if the PointSet is not empty ";
86 if (pointSet->IsEmpty(0))
87 {
88 std::cout<<"[FAILED]"<<std::endl;
89 return EXIT_FAILURE;
90 }
91 std::cout<<"[PASSED]"<<std::endl;
92  */
93  }
94 
95  void TestSetSelectInfo()
96  {
97  // check SetSelectInfo
98  pointSet->SetSelectInfo(4, true);
99 
100  CPPUNIT_ASSERT_EQUAL_MESSAGE("check SetSelectInfo", true, pointSet->GetSelectInfo(4));
101  /*
102  if (!pointSet->GetSelectInfo(2))
103  {
104  std::cout<<"[FAILED]"<<std::endl;
105  return EXIT_FAILURE;
106  }
107  delete doOp;
108  std::cout<<"[PASSED]"<<std::endl;
109  */
110  }
111 
112  void TestSearchSelectedPoint()
113  {
114  // check SearchSelectedPoint
115  CPPUNIT_ASSERT_EQUAL_MESSAGE(
116  "check SearchSelectedPoint ", true, pointSet->SearchSelectedPoint() == (int)selectedPointId);
117 
118  /*
119 if( pointSet->SearchSelectedPoint() != 4)
120 {
121 std::cout<<"[FAILED]"<<std::endl;
122 return EXIT_FAILURE;
123 }
124 std::cout<<"[PASSED]"<<std::endl;
125  */
126  }
127 
128  void TestGetNumberOfSelected()
129  {
130  // check GetNumeberOfSelected
131  CPPUNIT_ASSERT_EQUAL_MESSAGE("check GetNumeberOfSelected ", true, pointSet->GetNumberOfSelected() == 1);
132 
133  /*
134 if(pointSet->GetNumberOfSelected() != 1)
135 {
136 std::cout<<"[FAILED]"<<std::endl;
137 return EXIT_FAILURE;
138 }
139 std::cout<<"[PASSED]"<<std::endl;
140  */
141  }
142 
143  void TestGetPointIfExists()
144  {
145  // check GetPointIfExists
146  mitk::Point3D point4;
147  mitk::Point3D tempPoint;
148  point4.Fill(5);
149  mitk::PointSet::PointType tmpPoint;
150 
151  pointSet->GetPointIfExists(4, &tmpPoint);
152 
153  CPPUNIT_ASSERT_EQUAL_MESSAGE("check GetPointIfExists: ", true, tmpPoint == point4);
154  /*
155  if (tmpPoint != point5)
156  {
157  std::cout<<"[FAILED]"<<std::endl;
158  return EXIT_FAILURE;
159  }
160  std::cout<<"[PASSED]"<<std::endl;
161  */
162  }
163 
164  void TestSwapPointPositionUpwards()
165  {
166  // Check SwapPointPosition upwards
167  mitk::Point3D point;
168  mitk::Point3D tempPoint;
169  point = pointSet->GetPoint(1);
170  pointSet->SwapPointPosition(1, true);
171  tempPoint = pointSet->GetPoint(0);
172 
173  CPPUNIT_ASSERT_EQUAL_MESSAGE("check SwapPointPosition upwards", true, point == tempPoint);
174 
175  /*
176  if(point != tempPoint)
177  {
178  std::cout<<"[FAILED]"<<std::endl;
179  return EXIT_FAILURE;
180  }
181  std::cout<<"[PASSED]"<<std::endl;
182  */
183  }
184 
185  void TestSwapPointPositionUpwardsNotPossible()
186  {
187  // Check SwapPointPosition upwards not possible
188  CPPUNIT_ASSERT_EQUAL_MESSAGE(
189  "check SwapPointPosition upwards not possible", false, pointSet->SwapPointPosition(0, true));
190 
191  /*
192 if(pointSet->SwapPointPosition(0, true))
193 {
194 std::cout<<"[FAILED]"<<std::endl;
195 return EXIT_FAILURE;
196 }
197 std::cout<<"[PASSED]"<<std::endl;
198  */
199  }
200 
201  void TestSwapPointPositionDownwards()
202  {
203  // Check SwapPointPosition downwards
204  mitk::Point3D point;
205  mitk::Point3D tempPoint;
206  point = pointSet->GetPoint(0);
207  pointSet->SwapPointPosition(0, false);
208  tempPoint = pointSet->GetPoint(1);
209 
210  CPPUNIT_ASSERT_EQUAL_MESSAGE("check SwapPointPosition down", true, point == tempPoint);
211 
212  /*
213 if(point != tempPoint)
214 {
215 std::cout<<"[FAILED]"<<std::endl;
216 return EXIT_FAILURE;
217 }
218 std::cout<<"[PASSED]"<<std::endl;
219  */
220  }
221 
222  void TestSwapPointPositionDownwardsNotPossible()
223  {
225 
226  int id = 0;
227  mitk::Point3D point;
228  point.Fill(1);
229  pointSet2->SetPoint(id, point);
230 
231  // Check SwapPointPosition downwards not possible
232  CPPUNIT_ASSERT_EQUAL_MESSAGE(
233  "check SwapPointPosition downwards not possible", false, pointSet2->SwapPointPosition(id, false));
234 
235  /*
236 if(pointSet->SwapPointPosition(1, false))
237 {
238 std::cout<<"[FAILED]"<<std::endl;
239 return EXIT_FAILURE;
240 }
241 std::cout<<"[PASSED]"<<std::endl;
242  */
243  }
244 
245  void TestCreateHoleInThePointIDs()
246  {
247  // create a hole in the point IDs
248  mitk::Point3D point(0.);
249  mitk::PointSet::PointType p10, p11, p12;
250  p10.Fill(10.0);
251  p11.Fill(11.0);
252  p12.Fill(12.0);
253  pointSet->InsertPoint(10, p10);
254  pointSet->InsertPoint(11, p11);
255  pointSet->InsertPoint(12, p12);
256 
257  CPPUNIT_ASSERT_EQUAL_MESSAGE("add points with id 10, 11, 12: ",
258  true,
259  (pointSet->IndexExists(10) == true) || (pointSet->IndexExists(11) == true) ||
260  (pointSet->IndexExists(12) == true));
261 
262  // check OpREMOVE ExecuteOperation
263  int id = 11;
264  auto doOp = new mitk::PointOperation(mitk::OpREMOVE, point, id);
265  pointSet->ExecuteOperation(doOp);
266 
267  CPPUNIT_ASSERT_EQUAL_MESSAGE("remove point id 11: ", false, pointSet->IndexExists(id));
268 
269  /*
270  if(pointSet->IndexExists(id))
271  {
272  std::cout<<"[FAILED]"<<std::endl;
273  return EXIT_FAILURE;
274  }
275  delete doOp;
276  std::cout<<"[PASSED]"<<std::endl;
277  */
278 
279  // mitk::PointOperation* doOp = new mitk::PointOperation(mitk::OpMOVEPOINTUP, p12, 12);
280  // pointSet->ExecuteOperation(doOp);
281  delete doOp;
282 
283  // check OpMOVEPOINTUP ExecuteOperation
284  doOp = new mitk::PointOperation(mitk::OpMOVEPOINTUP, p12, 12);
285  pointSet->ExecuteOperation(doOp);
286  delete doOp;
287 
288  mitk::PointSet::PointType newP10 = pointSet->GetPoint(10);
289  mitk::PointSet::PointType newP12 = pointSet->GetPoint(12);
290 
291  CPPUNIT_ASSERT_EQUAL_MESSAGE(
292  "check PointOperation OpMOVEPOINTUP for point id 12:", true, ((newP10 == p12) && (newP12 == p10)));
293 
294  // check OpMOVEPOINTDOWN ExecuteOperation
295  doOp = new mitk::PointOperation(mitk::OpMOVEPOINTDOWN, p10, 10);
296  pointSet->ExecuteOperation(doOp);
297  delete doOp;
298  newP10 = pointSet->GetPoint(10);
299  newP12 = pointSet->GetPoint(12);
300 
301  CPPUNIT_ASSERT_EQUAL_MESSAGE(
302  "check PointOperation OpMOVEPOINTDOWN for point id 10: ", true, ((newP10 == p10) && (newP12 == p12)));
303  }
304 
305  void TestInsertPointWithPointSpecification()
306  {
307  // check InsertPoint with PointSpecification
308  mitk::Point3D point5;
309  mitk::Point3D tempPoint;
310  point5.Fill(7);
311 
312  pointSet->SetPoint(5, point5, mitk::PTEDGE);
313  tempPoint = pointSet->GetPoint(5);
314 
315  CPPUNIT_ASSERT_EQUAL_MESSAGE("check InsertPoint with PointSpecification", true, tempPoint == point5);
316  /*
317  if (tempPoint != point5)
318  {
319  std::cout<<"[FAILED]"<<std::endl;
320  return EXIT_FAILURE;
321  }
322  std::cout<<"[PASSED]"<<std::endl;
323  */
324  }
325 
326  void TestRemovePointInterface()
327  {
328  mitk::PointSet::Pointer psClone = pointSet->Clone();
329  mitk::PointSet::Pointer refPsLastRemoved = mitk::PointSet::New();
330  mitk::Point3D point0, point1, point2, point3, point4;
331  point0.Fill(1);
332  refPsLastRemoved->InsertPoint(0, point0);
333  mitk::FillVector3D(point1, 1.0, 2.0, 3.0);
334  refPsLastRemoved->InsertPoint(1, point1);
335  point2.Fill(3);
336  point3.Fill(4);
337  refPsLastRemoved->InsertPoint(2, point2);
338  refPsLastRemoved->InsertPoint(3, point3);
339 
340  mitk::PointSet::Pointer refPsMiddleRemoved = mitk::PointSet::New();
341  refPsMiddleRemoved->InsertPoint(0, point0);
342  refPsMiddleRemoved->InsertPoint(1, point1);
343  refPsMiddleRemoved->InsertPoint(3, point3);
344 
345  // remove non-existent point
346  bool removed = pointSet->RemovePointIfExists(5, 0);
347  CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove non-existent point", false, removed);
348  MITK_ASSERT_EQUAL(pointSet, psClone, "No changes made");
349 
350  // remove point from non-existent time-step
351  removed = pointSet->RemovePointIfExists(1, 1);
352  CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove non-existent point", false, removed);
353  MITK_ASSERT_EQUAL(pointSet, psClone, "No changes made");
354 
355  // remove max id from non-existent time-step
356  mitk::PointSet::PointsIterator maxIt = pointSet->RemovePointAtEnd(2);
357  CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove max id point from non-existent time step", true, maxIt == pointSet->End(2));
358  MITK_ASSERT_EQUAL(pointSet, psClone, "No changes made");
359 
360  // remove max id from empty point set
362  maxIt = emptyPS->RemovePointAtEnd(0);
363  CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove max id point from non-existent time step", true, maxIt == emptyPS->End(0));
364  int size = emptyPS->GetSize(0);
365  unsigned int pointSetSeriesSize = emptyPS->GetPointSetSeriesSize();
366  CPPUNIT_ASSERT_EQUAL_MESSAGE("Nothing added", true, size == 0 && pointSetSeriesSize == 1);
367 
368  // remove max id point
369  maxIt = pointSet->RemovePointAtEnd(0);
370  CPPUNIT_ASSERT_EQUAL_MESSAGE("Point id 4 removed", false, pointSet->IndexExists(4));
371  MITK_ASSERT_EQUAL(pointSet, refPsLastRemoved, "No changes made");
372 
373  mitk::PointSet::PointIdentifier id = maxIt.Index();
375  refPt[0] = 4.0;
376  refPt[1] = 4.0;
377  refPt[2] = 4.0;
378  mitk::PointSet::PointType pt = maxIt.Value();
379  bool equal = mitk::Equal(refPt, pt);
380  CPPUNIT_ASSERT_EQUAL_MESSAGE("Returned iterator pointing at max id", true, id == 3 && equal);
381 
382  // remove middle point
383  removed = pointSet->RemovePointIfExists(2, 0);
384  CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove point id 2", true, removed);
385  MITK_ASSERT_EQUAL(pointSet, refPsMiddleRemoved, "Point removed");
386  }
387 
388  void TestMaxIdAccess()
389  {
390  typedef mitk::PointSet::PointIdentifier IdType;
391  typedef mitk::PointSet::PointsIterator PointsIteratorType;
392  PointsIteratorType empty;
393 
394  mitk::Point3D new1, new2, new3, new4, refMaxPt;
395  new1.Fill(4);
396  new2.Fill(5);
397  new3.Fill(6);
398  new4.Fill(7);
399  refMaxPt.Fill(5);
400 
401  pointSet->SetPoint(0, new1, 2);
402  pointSet->InsertPoint(1, new2, 2);
403  pointSet->InsertPoint(3, new3, 2);
404  pointSet->InsertPoint(6, new4, 2);
405 
406  PointsIteratorType maxIt = pointSet->GetMaxId(1);
407  empty = pointSet->End(1);
408  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check empty time step max id.", true, maxIt == empty);
409 
410  maxIt = pointSet->GetMaxId(3);
411  empty = pointSet->End(3);
412  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check non-existent time step max id.", true, maxIt == empty);
413 
414  maxIt = pointSet->GetMaxId(0);
415  empty = pointSet->End(0);
416  IdType maxId = maxIt.Index();
417  mitk::Point3D maxPt = maxIt.Value();
418  bool equal = mitk::Equal(maxPt, refMaxPt);
419  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check time step 0 max id iterator.", false, maxIt == empty);
420  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check time step 0 max id.", true, maxId == 4);
421  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check time step 0 max id point.", true, equal);
422 
423  maxIt = pointSet->GetMaxId(2);
424  empty = pointSet->End(2);
425  maxId = maxIt.Index();
426  maxPt = maxIt.Value();
427  equal = mitk::Equal(maxPt, new4);
428  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check time step 2 max id iterator.", false, maxIt == empty);
429  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check time step 2 max id.", true, maxId == 6);
430  CPPUNIT_ASSERT_EQUAL_MESSAGE("Check time step 2 max id point.", true, equal);
431  }
432 
433  void TestInsertPointAtEnd()
434  {
435  typedef mitk::PointSet::PointType PointType;
436 
437  PointType new1, new2, new3, new4, refMaxPt;
438  new1.Fill(4);
439  new2.Fill(5);
440  new3.Fill(6);
441  new4.Fill(7);
442 
443  pointSet->SetPoint(1, new1, 2);
444  pointSet->InsertPoint(3, new2, 2);
445  pointSet->InsertPoint(4, new3, 2);
446  pointSet->InsertPoint(6, new4, 2);
447 
448  PointType in1, in2, in3, in4;
449  in1.Fill(8);
450  in2.Fill(9);
451  in3.Fill(10);
452  in4.Fill(11);
453 
454  mitk::PointSet::Pointer refPs1 = pointSet->Clone();
455  refPs1->SetPoint(5, in1, 0);
456  mitk::PointSet::Pointer refPs2 = pointSet->Clone();
457  refPs2->SetPoint(5, in1, 0);
458  refPs2->SetPoint(0, in2, 1);
459  mitk::PointSet::Pointer refPs3 = pointSet->Clone();
460  refPs3->SetPoint(5, in1, 0);
461  refPs3->SetPoint(0, in2, 1);
462  refPs3->SetPoint(7, in3, 2);
463  mitk::PointSet::Pointer refPs4 = pointSet->Clone();
464  refPs4->SetPoint(5, in1, 0);
465  refPs4->SetPoint(0, in2, 1);
466  refPs4->SetPoint(7, in3, 2);
467  refPs4->SetPoint(0, in4, 7);
468 
469  pointSet->InsertPoint(in1, 0);
470  MITK_ASSERT_EQUAL(pointSet, refPs1, "Check point insertion for time step 0.");
471 
472  pointSet->InsertPoint(in2, 1);
473  MITK_ASSERT_EQUAL(pointSet, refPs2, "Check point insertion for time step 1.");
474 
475  pointSet->InsertPoint(in3, 2);
476  MITK_ASSERT_EQUAL(pointSet, refPs3, "Check point insertion for time step 2.");
477 
478  pointSet->InsertPoint(in4, 7);
479  MITK_ASSERT_EQUAL(pointSet, refPs4, "Check point insertion for time step 7.");
480  }
481 };
482 
483 MITK_TEST_SUITE_REGISTRATION(mitkPointSet)
DataType::PointsContainerIterator PointsIterator
Definition: mitkPointSet.h:133
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
static Pointer New()
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Constants for most interaction classes, due to the generic StateMachines.
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:106
DataType::PointIdentifier PointIdentifier
Definition: mitkPointSet.h:131
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.
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.