Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSurfaceTest.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 // Testing
14 #include "mitkTestFixture.h"
15 #include <mitkTestingMacros.h>
16 
17 // std includes
18 #include <string>
19 
20 // MITK includes
21 #include "mitkCommon.h"
22 #include "mitkNumericTypes.h"
23 #include "mitkSurface.h"
24 
25 // MITK includes
26 #include <mitkWeakPointer.h>
27 
28 // VTK includes
29 #include "vtkPolyData.h"
30 #include "vtkSphereSource.h"
31 
32 // stream includes
33 #include <fstream>
34 
35 class mitkSurfaceTestSuite : public mitk::TestFixture
36 {
37  CPPUNIT_TEST_SUITE(mitkSurfaceTestSuite);
38 
39  MITK_TEST(InitializationSurfacePointer_Success);
40  MITK_TEST(InitializationCloneSurfacePointer_Success);
41 
42  MITK_TEST(StateOfVtkPolyDataEqualNullPointer_Success);
43 
44  MITK_TEST(SetVtkPolyDataNotNullPointer_Failure);
45  MITK_TEST(SetClonedVtkPolyDataNotNullPointer_Failure);
46 
47  MITK_TEST(GetBoundingBox_Success);
48 
49  MITK_TEST(SurfaceExpandTimestepsAreFive_Success);
50  MITK_TEST(Surface4DDataCreation_Success);
51 
52  MITK_TEST(TimeGeometrySurface_Success);
53  MITK_TEST(ChangingDataOfSpecificTimestepSurface_Success);
54  MITK_TEST(SurfaceCopyWithGraft_Failure);
55  MITK_TEST(CopyingNumberOfTimesteps_Success);
56 
57  MITK_TEST(DestructionOfSurface_Success);
58 
59  CPPUNIT_TEST_SUITE_END();
60 
61 private:
62  mitk::Surface::Pointer m_Surface;
63  mitk::Surface::Pointer m_CloneSurface;
64  vtkSmartPointer<vtkSphereSource> m_SphereSource;
65  const mitk::TimeGeometry *m_InputTimeGeometry;
66 
67  int m_Time;
68  int m_Timestep;
69 
70 public:
71  void setUp() override
72  {
73  m_Surface = mitk::Surface::New();
74  m_CloneSurface = m_Surface->Clone();
75  m_SphereSource = vtkSmartPointer<vtkSphereSource>::New();
76  m_InputTimeGeometry = m_Surface->GetUpdatedTimeGeometry();
77 
78  m_SphereSource->SetCenter(0, 0, 0);
79  m_SphereSource->SetRadius(5.0);
80  m_SphereSource->SetThetaResolution(10);
81  m_SphereSource->SetPhiResolution(10);
82  m_SphereSource->Update();
83 
84  m_Time = 3;
85  m_Timestep = 0;
86  }
87 
88  void tearDown() override
89  {
90  m_Surface = nullptr;
91  m_CloneSurface = nullptr;
92  }
93 
94  void InitializationSurfacePointer_Success()
95  {
96  CPPUNIT_ASSERT_MESSAGE("Testing initialization", m_Surface.GetPointer());
97  }
98 
99  void InitializationCloneSurfacePointer_Success()
100  {
101  CPPUNIT_ASSERT_MESSAGE("Testing clone surface initialization", m_CloneSurface.GetPointer());
102  }
103 
104  void StateOfVtkPolyDataEqualNullPointer_Success()
105  {
106  CPPUNIT_ASSERT_MESSAGE("Testing initial state of vtkPolyData", m_Surface->GetVtkPolyData() == nullptr);
107  }
108 
109  void SetVtkPolyDataNotNullPointer_Failure()
110  {
111  vtkSmartPointer<vtkPolyData> polys = m_SphereSource->GetOutput();
112  m_Surface->SetVtkPolyData(polys);
113  CPPUNIT_ASSERT_MESSAGE("Testing set vtkPolyData", m_Surface->GetVtkPolyData() != nullptr);
114  }
115 
116  void SetClonedVtkPolyDataNotNullPointer_Failure()
117  {
118  vtkSmartPointer<vtkPolyData> polys = m_SphereSource->GetOutput();
119  m_Surface->SetVtkPolyData(polys);
120  m_CloneSurface = m_Surface->Clone();
121  CPPUNIT_ASSERT_MESSAGE("Testing set vtkPolyData of cloned surface!", m_CloneSurface->GetVtkPolyData() != nullptr);
122  }
123 
124  void GetBoundingBox_Success()
125  {
126  vtkSmartPointer<vtkPolyData> polys = m_SphereSource->GetOutput();
127  m_Surface->SetVtkPolyData(polys);
128 
129  double bounds[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
130  polys->ComputeBounds();
131  polys->GetBounds(bounds);
132 
133  m_Surface->UpdateOutputInformation();
134  m_Surface->SetRequestedRegionToLargestPossibleRegion();
135  auto *bb = const_cast<mitk::BoundingBox *>(m_Surface->GetGeometry()->GetBoundingBox());
136  mitk::BoundingBox::BoundsArrayType surfBounds = bb->GetBounds();
137 
138  bool passed = false;
139  if (bounds[0] == surfBounds[0] && bounds[1] == surfBounds[1] && bounds[2] == surfBounds[2] &&
140  bounds[3] == surfBounds[3] && bounds[4] == surfBounds[4] && bounds[5] == surfBounds[5])
141  {
142  passed = true;
143  }
144 
145  CPPUNIT_ASSERT_MESSAGE("Testing GetBoundingBox()", passed);
146  }
147 
148  void SurfaceExpandTimestepsAreFive_Success()
149  {
150  m_Surface->Expand(5);
151  m_Surface->Update();
152  m_Surface->SetRequestedRegionToLargestPossibleRegion();
153  mitk::Surface::RegionType requestedRegion = m_Surface->GetRequestedRegion();
154  CPPUNIT_ASSERT_MESSAGE("Testing mitk::Surface::Expand( timesteps ): ", requestedRegion.GetSize(3) == 5);
155  }
156 
157  void Surface4DDataCreation_Success()
158  {
159  double boundsMat[5][6];
160 
161  for (int i = 0; i < 5; i++)
162  {
163  vtkNew<vtkSphereSource> sphereSource;
164  sphereSource->SetCenter(0, 0, 0);
165  sphereSource->SetRadius(1.0 * (i + 1.0));
166  sphereSource->SetThetaResolution(10);
167  sphereSource->SetPhiResolution(10);
168  sphereSource->Update();
169  sphereSource->GetOutput()->ComputeBounds();
170  sphereSource->GetOutput()->GetBounds(boundsMat[i]);
171  m_Surface->SetVtkPolyData(sphereSource->GetOutput(), i);
172  }
173 
174  m_Surface->UpdateOutputInformation();
175  m_Surface->SetRequestedRegionToLargestPossibleRegion();
176 
177  bool passed = true;
178  for (int i = 0; i < 5; i++)
179  {
181  (const_cast<mitk::BoundingBox *>(m_Surface->GetTimeGeometry()->GetGeometryForTimeStep(i)->GetBoundingBox()))
182  ->GetBounds();
183 
184  if (boundsMat[i][0] != surfBounds[0] || boundsMat[i][1] != surfBounds[1] || boundsMat[i][2] != surfBounds[2] ||
185  boundsMat[i][3] != surfBounds[3] || boundsMat[i][4] != surfBounds[4] || boundsMat[i][5] != surfBounds[5])
186  {
187  passed = false;
188  break;
189  }
190  }
191  CPPUNIT_ASSERT_MESSAGE("Testing mitk::Surface::Testing 4D surface data creation", passed);
192  }
193 
194  void TimeGeometrySurface_Success()
195  {
196  m_Timestep = m_InputTimeGeometry->TimePointToTimeStep(m_Time);
197  CPPUNIT_ASSERT_MESSAGE("Testing correctness of geometry for surface->GetUpdatedTimeGeometry()",
198  m_Time == m_Timestep);
199  }
200 
201  void ChangingDataOfSpecificTimestepSurface_Success()
202  {
203  vtkNew<vtkSphereSource> sphereSource;
204  sphereSource->SetCenter(0, 0, 0);
205  sphereSource->SetRadius(100.0);
206  sphereSource->SetThetaResolution(10);
207  sphereSource->SetPhiResolution(10);
208  sphereSource->Update();
209  m_Surface->SetVtkPolyData(sphereSource->GetOutput(), 3);
210 
211  m_Timestep = m_InputTimeGeometry->TimePointToTimeStep(m_Time);
212  CPPUNIT_ASSERT_MESSAGE(
213  "Explicitly changing the data of timestep 3 and checking for timebounds correctness of surface's geometry again",
214  m_Time == m_Timestep);
215  }
216 
217  void SurfaceCopyWithGraft_Failure()
218  {
219  double boundsMat[5][6];
220 
221  for (int i = 0; i < 5; i++)
222  {
223  vtkNew<vtkSphereSource> sphereSource;
224  sphereSource->SetCenter(0, 0, 0);
225  sphereSource->SetRadius(1.0 * (i + 1.0));
226  sphereSource->SetThetaResolution(10);
227  sphereSource->SetPhiResolution(10);
228  sphereSource->Update();
229  sphereSource->GetOutput()->ComputeBounds();
230  sphereSource->GetOutput()->GetBounds(boundsMat[i]);
231  m_Surface->SetVtkPolyData(sphereSource->GetOutput(), i);
232  }
233 
234  m_Surface->UpdateOutputInformation();
235  m_Surface->SetRequestedRegionToLargestPossibleRegion();
236 
238  dummy->Graft(m_Surface);
239  CPPUNIT_ASSERT_MESSAGE("Testing copying a Surface with Graft()", dummy->GetVtkPolyData() != nullptr);
240  }
241 
242  void CopyingNumberOfTimesteps_Success()
243  {
244  double boundsMat[5][6];
245 
246  for (int i = 0; i < 5; i++)
247  {
248  vtkNew<vtkSphereSource> sphereSource;
249  sphereSource->SetCenter(0, 0, 0);
250  sphereSource->SetRadius(1.0 * (i + 1.0));
251  sphereSource->SetThetaResolution(10);
252  sphereSource->SetPhiResolution(10);
253  sphereSource->Update();
254  sphereSource->GetOutput()->ComputeBounds();
255  sphereSource->GetOutput()->GetBounds(boundsMat[i]);
256  m_Surface->SetVtkPolyData(sphereSource->GetOutput(), i);
257  }
258 
259  m_Surface->UpdateOutputInformation();
260  m_Surface->SetRequestedRegionToLargestPossibleRegion();
261 
262  unsigned int numberoftimesteps = m_Surface->GetTimeSteps();
264  dummy->Graft(m_Surface);
265 
266  CPPUNIT_ASSERT_MESSAGE(" Old timesteps == copy of timesteps ", dummy->GetTimeSteps() == numberoftimesteps);
267  }
268 
269  void DestructionOfSurface_Success()
270  {
271  m_Surface = nullptr;
272  CPPUNIT_ASSERT_MESSAGE("Testing destruction of surface", m_Surface.IsNull());
273  }
274 };
275 MITK_TEST_SUITE_REGISTRATION(mitkSurface)
itk::BoundingBox< unsigned long, 3, ScalarType > BoundingBox
Standard 3D-BoundingBox typedef.
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
virtual TimeStepType TimePointToTimeStep(TimePointType timePoint) const =0
Converts a time point to the corresponding time step.
Test fixture for parameterized tests.
itk::ImageRegion< 5 > RegionType
Definition: mitkSurface.h:31
static Pointer New()
BoundingBoxType::BoundsArrayType BoundsArrayType