Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSurfaceEqualTest.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 "mitkSurface.h"
14 #include "mitkTestFixture.h"
15 #include "mitkTestingMacros.h"
16 
17 #include <vtkCellArray.h>
18 #include <vtkPolyData.h>
19 #include <vtkPolyLine.h>
20 #include <vtkPolygon.h>
21 #include <vtkSmartPointer.h>
22 
27 class mitkSurfaceEqualTestSuite : public mitk::TestFixture
28 {
29  CPPUNIT_TEST_SUITE(mitkSurfaceEqualTestSuite);
30  MITK_TEST(Equal_CloneAndOriginalOneTimestep_ReturnsTrue);
31  MITK_TEST(Equal_CloneAndOriginalTwoTimesteps_ReturnsTrue);
32  MITK_TEST(Equal_OneTimeStepVSTwoTimeStep_ReturnsFalse);
33  MITK_TEST(Equal_TwoTimeStepsDifferentPoints_ReturnsFalse);
34  MITK_TEST(Equal_DifferentPoints_ReturnsFalse);
35  MITK_TEST(Equal_SurfaceWithPolygonSurfaceWithPolyLine_ReturnsFalse);
36  CPPUNIT_TEST_SUITE_END();
37 
38 private:
40  mitk::Surface::Pointer m_Surface3D;
41  mitk::Surface::Pointer m_Surface3DTwoTimeSteps;
42 
43  vtkSmartPointer<vtkPoints> m_PointsOne;
44  vtkSmartPointer<vtkPoints> m_PointsTwo;
45  vtkSmartPointer<vtkCellArray> m_PolygonArrayTwo;
46  vtkSmartPointer<vtkPolyData> m_PolyDataOne;
47 
48 public:
53  void setUp() override
54  {
55  // generate two sets of points
56  m_PointsOne = vtkSmartPointer<vtkPoints>::New();
57  m_PointsOne->InsertNextPoint(0.0, 0.0, 0.0);
58  m_PointsOne->InsertNextPoint(1.0, 0.0, 0.0);
59  m_PointsOne->InsertNextPoint(0.0, 1.0, 0.0);
60  m_PointsOne->InsertNextPoint(1.0, 1.0, 0.0);
61 
62  m_PointsTwo = vtkSmartPointer<vtkPoints>::New();
63  m_PointsTwo->InsertNextPoint(0.0, 0.0, 0.0);
64  m_PointsTwo->InsertNextPoint(0.0, 0.0, 2.0);
65  m_PointsTwo->InsertNextPoint(0.0, 1.0, 0.0);
66  m_PointsTwo->InsertNextPoint(0.0, 1.0, 2.0);
67 
68  // generate two polygons
69  vtkSmartPointer<vtkPolygon> polygonOne = vtkSmartPointer<vtkPolygon>::New();
70  polygonOne->GetPointIds()->SetNumberOfIds(4);
71  polygonOne->GetPointIds()->SetId(0, 0);
72  polygonOne->GetPointIds()->SetId(1, 1);
73  polygonOne->GetPointIds()->SetId(2, 2);
74  polygonOne->GetPointIds()->SetId(3, 3);
75 
76  vtkSmartPointer<vtkPolygon> polygonTwo = vtkSmartPointer<vtkPolygon>::New();
77  polygonTwo->GetPointIds()->SetNumberOfIds(4);
78  polygonTwo->GetPointIds()->SetId(0, 3);
79  polygonTwo->GetPointIds()->SetId(1, 2);
80  polygonTwo->GetPointIds()->SetId(2, 0);
81  polygonTwo->GetPointIds()->SetId(3, 1);
82 
83  // generate polydatas
84  vtkSmartPointer<vtkCellArray> polygonArrayOne = vtkSmartPointer<vtkCellArray>::New();
85  polygonArrayOne->InsertNextCell(polygonOne);
86 
87  m_PolyDataOne = vtkSmartPointer<vtkPolyData>::New();
88  m_PolyDataOne->SetPoints(m_PointsOne);
89  m_PolyDataOne->SetPolys(polygonArrayOne);
90 
91  m_PolygonArrayTwo = vtkSmartPointer<vtkCellArray>::New();
92  m_PolygonArrayTwo->InsertNextCell(polygonTwo);
93 
94  vtkSmartPointer<vtkPolyData> polyDataTwo = vtkSmartPointer<vtkPolyData>::New();
95  polyDataTwo->SetPoints(m_PointsOne);
96  polyDataTwo->SetPolys(m_PolygonArrayTwo);
97 
98  // generate surfaces
99  m_Surface3D = mitk::Surface::New();
100  m_Surface3D->SetVtkPolyData(m_PolyDataOne);
101 
102  m_Surface3DTwoTimeSteps = mitk::Surface::New();
103  m_Surface3DTwoTimeSteps->SetVtkPolyData(m_PolyDataOne, 0);
104  m_Surface3DTwoTimeSteps->SetVtkPolyData(polyDataTwo, 1);
105  }
106 
107  void tearDown() override
108  {
109  m_Surface3D = nullptr;
110  m_Surface3DTwoTimeSteps = nullptr;
111  m_PolyDataOne = nullptr;
112  m_PolygonArrayTwo = nullptr;
113  m_PointsOne = nullptr;
114  m_PointsTwo = nullptr;
115  }
116 
117  void Equal_CloneAndOriginalOneTimestep_ReturnsTrue()
118  {
119  MITK_ASSERT_EQUAL(m_Surface3D, m_Surface3D->Clone(), "A one timestep clone should be equal to its original.");
120  }
121 
122  void Equal_CloneAndOriginalTwoTimesteps_ReturnsTrue()
123  {
124  MITK_ASSERT_EQUAL(m_Surface3DTwoTimeSteps,
125  m_Surface3DTwoTimeSteps->Clone(),
126  "A two timestep clone should be equal to its original.");
127  }
128 
129  void Equal_OneTimeStepVSTwoTimeStep_ReturnsFalse()
130  {
132  m_Surface3D, m_Surface3DTwoTimeSteps, "A one timestep and two timestep surface should not be equal.");
133  }
134 
135  void Equal_TwoTimeStepsDifferentPoints_ReturnsFalse()
136  {
137  vtkSmartPointer<vtkPolyData> polyDataDifferentPoints = vtkSmartPointer<vtkPolyData>::New();
138  polyDataDifferentPoints->SetPoints(m_PointsTwo);
139  polyDataDifferentPoints->SetPolys(m_PolygonArrayTwo);
140 
141  mitk::Surface::Pointer surface3DTwoTimeStepsDifferentPoints = mitk::Surface::New();
142  surface3DTwoTimeStepsDifferentPoints->SetVtkPolyData(m_PolyDataOne, 0);
143  surface3DTwoTimeStepsDifferentPoints->SetVtkPolyData(polyDataDifferentPoints, 1);
144 
145  // The geometry also changes, because the second pointset has a different geometry/extent.
146  MITK_ASSERT_NOT_EQUAL(surface3DTwoTimeStepsDifferentPoints,
147  m_Surface3DTwoTimeSteps,
148  "A surface with the same timesteps and different points should not be equal.");
149  }
150 
151  void Equal_SurfaceWithPolygonSurfaceWithPolyLine_ReturnsFalse()
152  {
153  // generate a line
154  vtkSmartPointer<vtkPolyLine> polyLineOne = vtkSmartPointer<vtkPolyLine>::New();
155  polyLineOne->GetPointIds()->SetNumberOfIds(2);
156  polyLineOne->GetPointIds()->SetId(0, 0);
157  polyLineOne->GetPointIds()->SetId(1, 1);
158 
159  vtkSmartPointer<vtkCellArray> polyLineArrayOne = vtkSmartPointer<vtkCellArray>::New();
160  polyLineArrayOne->InsertNextCell(polyLineOne);
161 
162  vtkSmartPointer<vtkPolyData> polyDataLine = vtkSmartPointer<vtkPolyData>::New();
163  polyDataLine->SetPoints(m_PointsOne);
164  polyDataLine->SetLines(polyLineArrayOne);
165 
166  mitk::Surface::Pointer surface3DLine = mitk::Surface::New();
167  surface3DLine->SetVtkPolyData(polyDataLine);
168 
169  MITK_ASSERT_NOT_EQUAL(m_Surface3D,
170  surface3DLine,
171  "A surface with the same timesteps and points and the same "
172  "number of cells, but different types of cells should not be "
173  "equal.");
174  }
175 
176  void Equal_DifferentPoints_ReturnsFalse()
177  {
178  mitk::Surface::Pointer surfaceWithADifferentPoint = m_Surface3D->Clone();
179  // modify points. m_Surface3D contains m_PointsOne
180  surfaceWithADifferentPoint->GetVtkPolyData()->SetPoints(m_PointsTwo);
181 
182  MITK_ASSERT_NOT_EQUAL(m_Surface3D,
183  surfaceWithADifferentPoint,
184  "A surface with a single timestep and different points should not be equal.");
185  }
186 };
187 
188 MITK_TEST_SUITE_REGISTRATION(mitkSurfaceEqual)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
#define MITK_ASSERT_NOT_EQUAL(OBJ1, OBJ2, MSG)
Testing macro to test if two objects are not equal.
Test fixture for parameterized tests.
#define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG)
Testing macro to test if two objects are equal.
static Pointer New()