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