Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPointSetOnEmptyTest.cpp
Go to the documentation of this file.
1 /*
2  * mitkPointSetTestOnEmpty.cpp
3  *
4  * Created on: Apr 23, 2014
5  * Author: wirkert
6  */
11 #include "mitkTestFixture.h"
12 #include "mitkTestingMacros.h"
13 
14 #include <mitkInteractionConst.h>
15 #include <mitkPointOperation.h>
16 #include <mitkPointSet.h>
17 #include <mitkVector.h>
18 
19 #include <fstream>
20 
21 class mitkPointSetOnEmptyTestSuite : public mitk::TestFixture
22 {
23  CPPUNIT_TEST_SUITE(mitkPointSetOnEmptyTestSuite);
24 
25  MITK_TEST(TestInstantiation);
26  MITK_TEST(TestIsEmpty);
27  MITK_TEST(TestGetITKPointSet);
28  MITK_TEST(TestGetSizeIsZero);
29  MITK_TEST(TestAddPointDirectly);
30  MITK_TEST(TestPointSetClone);
31  MITK_TEST(TestPointDataContainerCorrectAfterPointSetManipulation);
32 
33  CPPUNIT_TEST_SUITE_END();
34 
35 private:
36  mitk::PointSet::Pointer pointSet;
37 
38  void TestPointContainerPointDataContainer(mitk::PointSet *ps)
39  {
40  mitk::PointSet::PointsContainer *pc = ps->GetPointSet()->GetPoints();
41  mitk::PointSet::PointDataContainer *pd = ps->GetPointSet()->GetPointData();
42  CPPUNIT_ASSERT_EQUAL_MESSAGE(
43  "PointContainer and PointDataContainer have same size", true, pc->Size() == pd->Size());
44  mitk::PointSet::PointsContainer::ConstIterator pIt = pc->Begin();
45  mitk::PointSet::PointDataContainer::ConstIterator dIt = pd->Begin();
46  bool failed = false;
47  for (; pIt != pc->End(); ++pIt, ++dIt)
48  if (pIt->Index() != dIt->Index())
49  {
50  failed = true;
51  break;
52  }
53  CPPUNIT_ASSERT_EQUAL_MESSAGE("Indices in PointContainer and PointDataContainer are equal", false, failed);
54  }
55 
56 public:
57  void setUp() override
58  {
59  // Create PointSet
60  pointSet = mitk::PointSet::New();
61  }
62 
63  void tearDown() override { pointSet = nullptr; }
64  void TestInstantiation() { CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing instantiation", true, pointSet.IsNotNull()); }
65  void TestGetITKPointSet()
66  {
67  // try to get the itkPointSet
68  mitk::PointSet::DataType::Pointer itkdata = nullptr;
69  itkdata = pointSet->GetPointSet();
70  CPPUNIT_ASSERT_EQUAL_MESSAGE("try to get the itkPointSet from a newly created PointSet", true, itkdata.IsNotNull());
71  }
72 
73  void TestGetSizeIsZero()
74  {
75  // fresh PointSet has to be empty!
76  CPPUNIT_ASSERT_EQUAL_MESSAGE("check if the PointSet size is 0 ", true, pointSet->GetSize() == 0);
77  }
78 
79  void TestIsEmpty()
80  {
81  CPPUNIT_ASSERT_EQUAL_MESSAGE("check if the PointSet is empty", true, pointSet->IsEmptyTimeStep(0));
82  }
83 
84  void TestAddPointDirectly()
85  {
86  // add a point directly
87  int id = 0;
88  mitk::Point3D point;
89  mitk::FillVector3D(point, 1.0, 2.0, 3.0);
90  ++id;
91  pointSet->GetPointSet()->GetPoints()->InsertElement(id, point);
92 
93  CPPUNIT_ASSERT_EQUAL_MESSAGE(
94  "check if added points exists", true, pointSet->GetSize() == 1 || pointSet->IndexExists(id));
95 
96  mitk::Point3D tempPoint;
97 
98  tempPoint.Fill(0);
99  tempPoint = pointSet->GetPoint(id);
100 
101  CPPUNIT_ASSERT_EQUAL_MESSAGE("check if added point contains real value", true, point == tempPoint);
102  }
103 
104  void TestPointSetClone(void)
105  {
106  pointSet->Expand(3);
107  mitk::Point3D new0, new1, new2;
108  new0.Fill(0);
109  new1.Fill(1);
110  new2.Fill(2);
111  pointSet->InsertPoint(5, new0, mitk::PTCORNER, 0);
112  pointSet->InsertPoint(112, new1, 0);
113  pointSet->InsertPoint(2, new2, 0);
114  pointSet->InsertPoint(2, new0, 1);
115  pointSet->InsertPoint(1, new1, 1);
116  pointSet->InsertPoint(0, new2, 1);
117  pointSet->InsertPoint(0, new0, 2);
118  pointSet->InsertPoint(2, new1, 2);
119  pointSet->InsertPoint(1, new2, 2);
120 
121  MITK_TEST_OUTPUT(<< "... pointset ts: " << pointSet->GetTimeSteps())
122  mitk::PointSet::Pointer clonePS = pointSet->Clone();
123  MITK_TEST_OUTPUT(<< "... clone pointset ts: " << clonePS->GetTimeSteps())
124 
125  for (unsigned int t = 0; t < pointSet->GetTimeSteps(); t++)
126  {
127  MITK_TEST_OUTPUT(<< "testing timestep: " << t)
128  CPPUNIT_ASSERT_EQUAL_MESSAGE("Clone has same size", true, pointSet->GetSize(t) == clonePS->GetSize(t));
129 
130  // test for equal point coordinates
131  for (mitk::PointSet::PointsConstIterator i = pointSet->Begin(), j = clonePS->Begin();
132  i != pointSet->End() && j != clonePS->End();
133  ++i, ++j)
134  {
135  CPPUNIT_ASSERT_EQUAL_MESSAGE(
136  "Cloned PS and PS have same points", true, i.Index() == j.Index() && mitk::Equal(i.Value(), j.Value()));
137  }
138 
139  // test for equal point data
140  mitk::PointSet::PointDataContainer *pointDataCont = pointSet->GetPointSet(t)->GetPointData();
141  mitk::PointSet::PointDataContainer *clonePointDataCont = clonePS->GetPointSet(t)->GetPointData();
142  CPPUNIT_ASSERT_EQUAL_MESSAGE("Valid point data container", true, pointDataCont && clonePointDataCont);
143  CPPUNIT_ASSERT_EQUAL_MESSAGE(
144  "Cloned point data container has same size", true, pointDataCont->Size() == clonePointDataCont->Size());
145  for (mitk::PointSet::PointDataConstIterator i = pointDataCont->Begin(), j = clonePointDataCont->Begin();
146  i != pointDataCont->End() && j != clonePointDataCont->End();
147  ++i, ++j)
148  {
149  CPPUNIT_ASSERT_EQUAL_MESSAGE(
150  "Cloned PS and PS have same point data", true, i.Index() == j.Index() && i.Value() == j.Value());
151  }
152  }
153 
154  CPPUNIT_ASSERT_EQUAL_MESSAGE("check if the PointSet is not empty", false, clonePS->IsEmptyTimeStep(0));
155 
156  CPPUNIT_ASSERT_EQUAL_MESSAGE(
157  "Testing cloned point set's size!", true, clonePS->GetPointSetSeriesSize() == pointSet->GetPointSetSeriesSize());
158  CPPUNIT_ASSERT_EQUAL_MESSAGE(
159  "Testing that the clone is not the source PS!", true, clonePS.GetPointer() != pointSet.GetPointer());
160  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing if the geometry is cloned correctly!",
161  true,
162  clonePS->GetGeometry()->GetCenter() == pointSet->GetGeometry()->GetCenter());
163  CPPUNIT_ASSERT_EQUAL_MESSAGE(
164  "Testing if the property list is cloned correctly!",
165  true,
166  clonePS->GetPropertyList()->GetMap()->size() == pointSet->GetPropertyList()->GetMap()->size());
167  // Also testing, that clone is independent from original
168  mitk::Point3D p, p2;
169  p.Fill(42);
170  p2.Fill(84);
171  clonePS->InsertPoint(0, p);
172  pointSet->InsertPoint(0, p2);
173  p = clonePS->GetPoint(0);
174  p2 = pointSet->GetPoint(0);
175  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing that the clone is independent from source!", true, p != p2);
176  }
177 
178  void TestPointDataContainerCorrectAfterPointSetManipulation(void)
179  {
180  /* call everything that might modify PointContainer and PointDataContainer */
181  MITK_TEST_OUTPUT(<< "Test InsertPoint(), SetPoint() and SwapPointPosition()");
183  mitk::FillVector3D(point, 2.2, 3.3, -4.4);
184  pointSet->InsertPoint(17, point);
185  pointSet->SetPoint(4, point);
186  pointSet->SetPoint(7, point);
187  pointSet->SetPoint(2, point);
188  pointSet->SwapPointPosition(7, true);
189  pointSet->SwapPointPosition(3, true);
190  pointSet->SwapPointPosition(2, false);
191  TestPointContainerPointDataContainer(pointSet);
192 
193  MITK_TEST_OUTPUT(<< "Test OpREMOVE");
194  mitk::PointOperation op1(mitk::OpREMOVE, mitk::Point3D(), 2); // existing index
195  pointSet->ExecuteOperation(&op1);
196  mitk::PointOperation op1b(mitk::OpREMOVE, mitk::Point3D(), 112); // non existing index
197  pointSet->ExecuteOperation(&op1b);
198  TestPointContainerPointDataContainer(pointSet);
199 
200  MITK_TEST_OUTPUT(<< "Test OpMove");
201  mitk::PointOperation op2(mitk::OpMOVE, mitk::Point3D(), 4); // existing index
202  pointSet->ExecuteOperation(&op2);
203  mitk::PointOperation op3(mitk::OpMOVE, mitk::Point3D(), 34); // non existing index
204  pointSet->ExecuteOperation(&op3);
205  TestPointContainerPointDataContainer(pointSet);
206 
207  MITK_TEST_OUTPUT(<< "Test OpINSERT");
208  mitk::PointOperation op4(mitk::OpINSERT, mitk::Point3D(), 38); // non existing index
209  pointSet->ExecuteOperation(&op4);
210  mitk::PointOperation op5(mitk::OpINSERT, mitk::Point3D(), 17); // existing index
211  pointSet->ExecuteOperation(&op5);
212  TestPointContainerPointDataContainer(pointSet);
213  }
214 };
215 
216 MITK_TEST_SUITE_REGISTRATION(mitkPointSetOnEmpty)
itk::SmartPointer< Self > Pointer
DataType::PointDataContainer PointDataContainer
Definition: mitkPointSet.h:139
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
static Pointer New()
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
DataCollection - Class to facilitate loading/accessing structured data.
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::PointDataContainerIterator PointDataConstIterator
Definition: mitkPointSet.h:141
#define MITK_TEST_OUTPUT(x)
Output some text.
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:79
virtual DataType::Pointer GetPointSet(int t=0) const
returns the pointset
Test fixture for parameterized tests.
Operation that handles all actions on one Point.
DataType::PointsContainer PointsContainer
Definition: mitkPointSet.h:136
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 mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)