Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPlanarPolygonTest.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 "mitkPlanarPolygon.h"
18 #include "mitkPlaneGeometry.h"
19 #include "mitkTestingMacros.h"
20 
21 class mitkPlanarPolygonTestClass
22 {
23 public:
24  static void TestPlanarPolygonPlacement(mitk::PlanarPolygon::Pointer planarPolygon)
25  {
26  // Test for correct minimum number of control points in cross-mode
27  MITK_TEST_CONDITION(planarPolygon->GetMinimumNumberOfControlPoints() == 3, "Minimum number of control points");
28 
29  // Test for correct maximum number of control points in cross-mode
30  MITK_TEST_CONDITION(planarPolygon->GetMaximumNumberOfControlPoints() == 1000, "Maximum number of control points");
31 
32  // Initial placement of PlanarPolygon
33  mitk::Point2D p0;
34  p0[0] = 00.0;
35  p0[1] = 0.0;
36  planarPolygon->PlaceFigure(p0);
37 
38  // Add second control point
39  mitk::Point2D p1;
40  p1[0] = 50.0;
41  p1[1] = 00.0;
42  planarPolygon->SetControlPoint(1, p1);
43 
44  // Add third control point
45  mitk::Point2D p2;
46  p2[0] = 50.0;
47  p2[1] = 50.0;
48  planarPolygon->AddControlPoint(p2);
49 
50  // Add fourth control point
51  mitk::Point2D p3;
52  p3[0] = 0.0;
53  p3[1] = 50.0;
54  planarPolygon->AddControlPoint(p3);
55 
56  // Test for number of control points
57  MITK_TEST_CONDITION(planarPolygon->GetNumberOfControlPoints() == 4, "Number of control points after placement");
58 
59  // Test if PlanarFigure is closed
60  MITK_TEST_CONDITION(planarPolygon->IsClosed(), "planar polygon should not be closed, yet, right?");
61 
62  planarPolygon->SetClosed(true);
63 
64  MITK_TEST_CONDITION(planarPolygon->IsClosed(), "planar polygon should be closed after function call, right?");
65 
66  // Test for number of polylines
67  const mitk::PlanarFigure::PolyLineType polyLine0 = planarPolygon->GetPolyLine(0);
68  auto iter = polyLine0.begin();
69  MITK_TEST_CONDITION(planarPolygon->GetPolyLinesSize() == 1, "Number of polylines after placement");
70 
71  // Get polylines and check if the generated coordinates are OK
72  const mitk::Point2D &pp0 = *iter;
73  ++iter;
74  const mitk::Point2D &pp1 = *iter;
75  MITK_TEST_CONDITION(((pp0 == p0) && (pp1 == p1)) || ((pp0 == p1) && (pp1 == p0)), "Correct polyline 1");
76 
77  // Test for number of measurement features
78  planarPolygon->EvaluateFeatures();
79  MITK_TEST_CONDITION(planarPolygon->GetNumberOfFeatures() == 2, "Number of measurement features");
80 
81  // Test for correct feature evaluation
82  double length0 = 4 * 50.0; // circumference
83  MITK_TEST_CONDITION(fabs(planarPolygon->GetQuantity(0) - length0) < mitk::eps, "Size of longest diameter");
84 
85  double length1 = 50.0 * 50.0; // area
86  MITK_TEST_CONDITION(fabs(planarPolygon->GetQuantity(1) - length1) < mitk::eps, "Size of short axis diameter");
87  }
88 
89  static void TestPlanarPolygonEditing(mitk::PlanarPolygon::Pointer planarPolygon)
90  {
91  unsigned int initialNumberOfControlPoints = planarPolygon->GetNumberOfControlPoints();
92 
93  mitk::Point2D pnt;
94  pnt[0] = 75.0;
95  pnt[1] = 25.0;
96  planarPolygon->AddControlPoint(pnt);
97 
98  MITK_TEST_CONDITION(planarPolygon->GetNumberOfControlPoints() == initialNumberOfControlPoints + 1,
99  "A new control-point shall be added");
100  MITK_TEST_CONDITION(planarPolygon->GetControlPoint(planarPolygon->GetNumberOfControlPoints() - 1) == pnt,
101  "Control-point shall be added at the end.");
102 
103  planarPolygon->RemoveControlPoint(3);
104  MITK_TEST_CONDITION(planarPolygon->GetNumberOfControlPoints() == initialNumberOfControlPoints,
105  "A control-point has been removed");
106  MITK_TEST_CONDITION(planarPolygon->GetControlPoint(3) == pnt, "It shall be possible to remove any control-point.");
107 
108  planarPolygon->RemoveControlPoint(0);
109  planarPolygon->RemoveControlPoint(0);
110  planarPolygon->RemoveControlPoint(0);
111  MITK_TEST_CONDITION(planarPolygon->GetNumberOfControlPoints() == 3,
112  "Control-points cannot be removed if only three points remain.");
113 
114  mitk::Point2D pnt1;
115  pnt1[0] = 33.0;
116  pnt1[1] = 33.0;
117  planarPolygon->AddControlPoint(pnt1, 0);
118  MITK_TEST_CONDITION(planarPolygon->GetNumberOfControlPoints() == 4, "A control-point has been added");
119  MITK_TEST_CONDITION(planarPolygon->GetControlPoint(0) == pnt1,
120  "It shall be possible to insert a control-point at any position.");
121  }
122 };
129 int mitkPlanarPolygonTest(int /* argc */, char * /*argv*/ [])
130 {
131  // always start with this!
132  MITK_TEST_BEGIN("planarPolygon")
133 
134  // create PlaneGeometry on which to place the planarPolygon
136  planeGeometry->InitializeStandardPlane(100.0, 100.0);
137 
138  // **************************************************************************
139  // 1. Instantiation and basic tests, including feature evaluation
141  planarPolygon->SetPlaneGeometry(planeGeometry);
142 
143  // first test: did this work?
144  MITK_TEST_CONDITION_REQUIRED(planarPolygon.IsNotNull(), "Testing instantiation");
145 
146  // Test placement of planarPolygon by control points
147  mitkPlanarPolygonTestClass::TestPlanarPolygonPlacement(planarPolygon);
148 
149  mitkPlanarPolygonTestClass::TestPlanarPolygonEditing(planarPolygon);
150 
151  // always end with this!
152  MITK_TEST_END();
153 }
int mitkPlanarPolygonTest(int, char *[])
static Pointer New()
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define MITK_TEST_CONDITION(COND, MSG)
static Pointer New()
MITKCORE_EXPORT const ScalarType eps
std::vector< PolyLineElement > PolyLineType
and MITK_TEST_END()