Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPlanarSubdivisionPolygonTest.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 
18 #include "mitkPlaneGeometry.h"
19 #include "mitkProperties.h"
20 #include "mitkTestingMacros.h"
21 
22 class mitkPlanarSubdivisionPolygonTestClass
23 {
24 public:
25  static void TestPlanarSubdivisionPolygonPlacement(mitk::PlanarSubdivisionPolygon::Pointer planarSubdivisionPolygon)
26  {
27  // Test for correct minimum number of control points in cross-mode
28  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetMinimumNumberOfControlPoints() == 3,
29  "Minimum number of control points");
30 
31  // Test for correct maximum number of control points in cross-mode
32  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetMaximumNumberOfControlPoints() == 1000,
33  "Maximum number of control points");
34 
35  // Test for correct rounds of subdivisionPoints
36  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetSubdivisionRounds() == 5, "Subdivision point generation depth");
37 
38  // Test for correct tension parameter
39  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetTensionParameter() == 0.0625, "Tension parameter");
40 
41  planarSubdivisionPolygon->SetProperty("initiallyplaced", mitk::BoolProperty::New(true));
42 
43  // Initial placement of planarSubdivisionPolygon
44  mitk::Point2D p0;
45  p0[0] = 25.0;
46  p0[1] = 25.0;
47  planarSubdivisionPolygon->PlaceFigure(p0);
48 
49  // Add second control point
50  mitk::Point2D p1;
51  p1[0] = 75.0;
52  p1[1] = 25.0;
53  planarSubdivisionPolygon->SetControlPoint(1, p1);
54 
55  // Add third control point
56  mitk::Point2D p2;
57  p2[0] = 75.0;
58  p2[1] = 75.0;
59  planarSubdivisionPolygon->AddControlPoint(p2);
60 
61  // Add fourth control point
62  mitk::Point2D p3;
63  p3[0] = 25.0;
64  p3[1] = 75.0;
65  planarSubdivisionPolygon->AddControlPoint(p3);
66 
67  // Test for number of control points
68  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetNumberOfControlPoints() == 4,
69  "Number of control points after placement");
70 
71  // Test if PlanarFigure is closed
72  MITK_TEST_CONDITION(planarSubdivisionPolygon->IsClosed(), "Test if property 'closed' is set by default");
73 
74  // Test for number of polylines
75  const mitk::PlanarFigure::PolyLineType polyLine0 = planarSubdivisionPolygon->GetPolyLine(0);
76  auto iter = polyLine0.begin();
77  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetPolyLinesSize() == 1, "Number of polylines after placement");
78 
79  // Test if subdivision point count is correct
80  MITK_TEST_CONDITION(polyLine0.size() == 128, "correct number of subdivision points for this depth level");
81 
82  // Test if control points are in correct order between subdivision points
83  bool correctPoint = true;
84  iter = polyLine0.begin();
85 
86  if (static_cast<mitk::Point2D>(*iter) != p0)
87  {
88  correctPoint = false;
89  }
90  advance(iter, 32);
91  if (static_cast<mitk::Point2D>(*iter) != p1)
92  {
93  correctPoint = false;
94  }
95  advance(iter, 32);
96  if (static_cast<mitk::Point2D>(*iter) != p2)
97  {
98  correctPoint = false;
99  }
100  advance(iter, 32);
101  if (static_cast<mitk::Point2D>(*iter) != p3)
102  {
103  correctPoint = false;
104  }
105  MITK_TEST_CONDITION(correctPoint, "Test if control points are in correct order in polyline");
106 
107  // Test if a picked point has the correct coordinates
108  correctPoint = true;
109 
110  mitk::Point2D testPoint;
111  testPoint[0] = 81.25;
112  testPoint[1] = 48.243;
113  iter = polyLine0.begin();
114  advance(iter, 47);
115 
116  mitk::ScalarType testEps = 1E-5;
117 
118  if ((static_cast<mitk::Point2D>(*iter)[0] - testPoint[0]) + (static_cast<mitk::Point2D>(*iter)[1] - testPoint[1]) >
119  testEps)
120  {
121  correctPoint = false;
122  }
123 
124  testPoint[0] = 39.624;
125  testPoint[1] = 19.3268;
126  iter = polyLine0.begin();
127  advance(iter, 10);
128  if ((static_cast<mitk::Point2D>(*iter)[0] - testPoint[0]) + (static_cast<mitk::Point2D>(*iter)[1] - testPoint[1]) >
129  testEps)
130  {
131  correctPoint = false;
132  }
133 
134  testPoint[0] = 71.2887;
135  testPoint[1] = 77.5248;
136  iter = polyLine0.begin();
137  advance(iter, 67);
138  if ((static_cast<mitk::Point2D>(*iter)[0] - testPoint[0]) + (static_cast<mitk::Point2D>(*iter)[1] - testPoint[1]) >
139  testEps)
140  {
141  correctPoint = false;
142  }
143 
144  MITK_TEST_CONDITION(correctPoint, "Test if subdivision points are calculated correctly")
145 
146  // Test for number of measurement features
147  /*
148  Does not work yet
149 
150  planarSubdivisionPolygon->EvaluateFeatures();
151  MITK_TEST_CONDITION( planarSubdivisionPolygon->GetNumberOfFeatures() == 2, "Number of measurement features" );
152 
153  // Test for correct feature evaluation
154  double length0 = 4 * 50.0; // circumference
155  MITK_TEST_CONDITION( fabs( planarSubdivisionPolygon->GetQuantity( 0 ) - length0) < mitk::eps, "Size of longest
156  diameter" );
157 
158  double length1 = 50.0 * 50.0 ; // area
159  MITK_TEST_CONDITION( fabs( planarSubdivisionPolygon->GetQuantity( 1 ) - length1) < mitk::eps, "Size of short axis
160  diameter" );
161  */
162  }
163 
164  static void TestPlanarSubdivisionPolygonEditing(mitk::PlanarSubdivisionPolygon::Pointer planarSubdivisionPolygon)
165  {
166  unsigned int initialNumberOfControlPoints = planarSubdivisionPolygon->GetNumberOfControlPoints();
167 
168  mitk::Point2D pnt;
169  pnt[0] = 75.0;
170  pnt[1] = 25.0;
171  planarSubdivisionPolygon->AddControlPoint(pnt);
172 
173  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetNumberOfControlPoints() == initialNumberOfControlPoints + 1,
174  "A new control-point shall be added");
176  planarSubdivisionPolygon->GetControlPoint(planarSubdivisionPolygon->GetNumberOfControlPoints() - 1) == pnt,
177  "Control-point shall be added at the end.");
178 
179  planarSubdivisionPolygon->RemoveControlPoint(3);
180  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetNumberOfControlPoints() == initialNumberOfControlPoints,
181  "A control-point has been removed");
182  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetControlPoint(3) == pnt,
183  "It shall be possible to remove any control-point.");
184 
185  planarSubdivisionPolygon->RemoveControlPoint(0);
186  planarSubdivisionPolygon->RemoveControlPoint(0);
187  planarSubdivisionPolygon->RemoveControlPoint(0);
188  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetNumberOfControlPoints() == 3,
189  "Control-points cannot be removed if only three points remain.");
190 
191  mitk::Point2D pnt1;
192  pnt1[0] = 33.0;
193  pnt1[1] = 33.0;
194  planarSubdivisionPolygon->AddControlPoint(pnt1, 0);
195  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetNumberOfControlPoints() == 4, "A control-point has been added");
196  MITK_TEST_CONDITION(planarSubdivisionPolygon->GetControlPoint(0) == pnt1,
197  "It shall be possible to insert a control-point at any position.");
198  }
199 };
206 int mitkPlanarSubdivisionPolygonTest(int /* argc */, char * /*argv*/ [])
207 {
208  // always start with this!
209  MITK_TEST_BEGIN("planarSubdivisionPolygon")
210 
211  // create PlaneGeometry on which to place the planarSubdivisionPolygon
213  planeGeometry->InitializeStandardPlane(100.0, 100.0);
214 
215  // **************************************************************************
216  // 1. Instantiation and basic tests, including feature evaluation
218  planarSubdivisionPolygon->SetPlaneGeometry(planeGeometry);
219 
220  // first test: did this work?
221  MITK_TEST_CONDITION_REQUIRED(planarSubdivisionPolygon.IsNotNull(), "Testing instantiation");
222 
223  // Test placement of planarSubdivisionPolygon by control points
224  mitkPlanarSubdivisionPolygonTestClass::TestPlanarSubdivisionPolygonPlacement(planarSubdivisionPolygon);
225 
226  mitkPlanarSubdivisionPolygonTestClass::TestPlanarSubdivisionPolygonEditing(planarSubdivisionPolygon);
227 
228  // always end with this!
229  MITK_TEST_END();
230 }
double ScalarType
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static Pointer New()
#define MITK_TEST_CONDITION(COND, MSG)
int mitkPlanarSubdivisionPolygonTest(int, char *[])
static const mitk::ScalarType testEps
static Pointer New()
std::vector< PolyLineElement > PolyLineType
and MITK_TEST_END()