Medical Imaging Interaction Toolkit  2018.4.99-c7ee88da
Medical Imaging Interaction Toolkit
mitkLabeledImageToSurfaceFilterTest.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 
15 #include <itksys/SystemTools.hxx>
16 
17 #include "mitkIOUtil.h"
18 
19 #include <cmath>
20 
21 bool equals(const mitk::ScalarType &val1, const mitk::ScalarType &val2, mitk::ScalarType epsilon = mitk::eps)
22 {
23  return (std::fabs(val1 - val2) <= epsilon);
24 }
25 
26 int mitkLabeledImageToSurfaceFilterTest(int argc, char *argv[])
27 {
28  if (argc < 2)
29  {
30  std::cout << "no path to testing specified [FAILED]" << std::endl;
31  return EXIT_FAILURE;
32  }
33 
34  std::string fileIn = argv[1];
35  std::cout << "Eingabe Datei: " << fileIn << std::endl;
36  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(fileIn);
37  if (image->GetPixelType() != mitk::PixelType(mitk::MakeScalarPixelType<char>()) ||
38  image->GetPixelType() != mitk::PixelType(mitk::MakeScalarPixelType<unsigned char>()))
39  {
40  std::cout << "file not a char or unsigned char image - test will not be applied [PASSED]" << std::endl;
41  std::cout << "[TEST DONE]" << std::endl;
42  return EXIT_SUCCESS;
43  }
44 
45  std::cout << "Testing instantiation: ";
47  if (filter.IsNull())
48  {
49  std::cout << "[FAILED]" << std::endl;
50  return EXIT_FAILURE;
51  }
52  else
53  {
54  std::cout << "[PASSED]" << std::endl;
55  }
56 
57  std::cout << "Create surface with default settings: ";
58  filter->SetInput(image);
59  filter->Update();
60 
61  if (filter->GetNumberOfOutputs() != 1)
62  {
63  std::cout << "Wrong number of outputs, [FAILED]" << std::endl;
64  return EXIT_FAILURE;
65  }
66  else if (filter->GetOutput() == nullptr)
67  {
68  std::cout << "Output is nullptr, [FAILED]" << std::endl;
69  return EXIT_FAILURE;
70  }
71  else if (filter->GetOutput()->GetVtkPolyData() == nullptr)
72  {
73  std::cout << "PolyData of surface is nullptr, [FAILED]" << std::endl;
74  return EXIT_FAILURE;
75  }
76  else
77  {
78  std::cout << "[PASSED]" << std::endl;
79  }
80 
81  std::cout << "Testing index to label conversion: ";
82  if (filter->GetLabelForNthOutput(0) != 257)
83  {
84  std::cout << "[FAILED]" << std::endl;
85  return EXIT_FAILURE;
86  }
87  else
88  {
89  std::cout << "[PASSED]" << std::endl;
90  }
91 
92  std::cout << "Testing volume for label calculation: ";
93  if (!equals(filter->GetVolumeForLabel(257), 14.328))
94  {
95  std::cout << filter->GetVolumeForLabel(257) << "[FAILED]" << std::endl;
96  return EXIT_FAILURE;
97  }
98  else
99  {
100  std::cout << "[PASSED]" << std::endl;
101  }
102 
103  std::cout << "Testing volume for index calculation: ";
104  if (!equals(filter->GetVolumeForNthOutput(0), 14.328))
105  {
106  std::cout << "[FAILED]" << std::endl;
107  return EXIT_FAILURE;
108  }
109  else
110  {
111  std::cout << "[PASSED]" << std::endl;
112  }
113 
114  std::cout << "Create surface using optimised settings: ";
115  filter->GenerateAllLabelsOn();
116  filter->SetGaussianStandardDeviation(1.5);
117  filter->SetSmooth(true); // smooth wireframe
118  filter->SetDecimate(mitk::ImageToSurfaceFilter::DecimatePro);
119  filter->SetTargetReduction(0.8);
120  if (filter->GetNumberOfOutputs() != 1)
121  {
122  std::cout << "[FAILED]" << std::endl;
123  return EXIT_FAILURE;
124  }
125  else
126  {
127  std::cout << "[PASSED]" << std::endl;
128  }
129 
130  std::cout << "Create surface for label 257: ";
131  filter->GenerateAllLabelsOff();
132  filter->SetLabel(257);
133  filter->Update();
134  if (filter->GetNumberOfOutputs() != 1)
135  {
136  std::cout << "Wrong number of outputs, [FAILED]" << std::endl;
137  return EXIT_FAILURE;
138  }
139  else if (filter->GetOutput() == nullptr)
140  {
141  std::cout << "Output is nullptr, [FAILED]" << std::endl;
142  return EXIT_FAILURE;
143  }
144  else if (filter->GetOutput()->GetVtkPolyData() == nullptr)
145  {
146  std::cout << "PolyData of surface is nullptr, [FAILED]" << std::endl;
147  return EXIT_FAILURE;
148  }
149  else
150  {
151  std::cout << "[PASSED]" << std::endl;
152  }
153 
154  std::cout << "Testing volume for label calculation: ";
155  if (!equals(filter->GetVolumeForLabel(257), 14.328))
156  {
157  std::cout << "[FAILED]" << std::endl;
158  return EXIT_FAILURE;
159  }
160  else
161  {
162  std::cout << "[PASSED]" << std::endl;
163  }
164 
165  std::cout << "Testing volume for index calculation: ";
166  if (!equals(filter->GetVolumeForNthOutput(0), 14.328))
167  {
168  std::cout << "[FAILED]" << std::endl;
169  return EXIT_FAILURE;
170  }
171  else
172  {
173  std::cout << "[PASSED]" << std::endl;
174  }
175 
176  std::cout << "Create surface for multiple labels: ";
177  filter->GenerateAllLabelsOn();
178  filter->SetBackgroundLabel(32000);
179  filter->Update();
180  if (filter->GetNumberOfOutputs() != 2)
181  {
182  std::cout << "Wrong number of outputs, [FAILED]" << std::endl;
183  return EXIT_FAILURE;
184  }
185  else if (filter->GetOutput() == nullptr)
186  {
187  std::cout << "Output 0 is nullptr, [FAILED]" << std::endl;
188  return EXIT_FAILURE;
189  }
190  else if (filter->GetOutput()->GetVtkPolyData() == nullptr)
191  {
192  std::cout << "PolyData of output 0 is nullptr, [FAILED]" << std::endl;
193  return EXIT_FAILURE;
194  }
195  else if (filter->GetOutput(1) == nullptr)
196  {
197  std::cout << "Output 1 is nullptr, [FAILED]" << std::endl;
198  return EXIT_FAILURE;
199  }
200  else if (filter->GetOutput(1)->GetVtkPolyData() == nullptr)
201  {
202  std::cout << "PolyData of output 1 is nullptr, [FAILED]" << std::endl;
203  return EXIT_FAILURE;
204  }
205  else
206  {
207  std::cout << "[PASSED]" << std::endl;
208  }
209 
210  std::cout << "Testing volume for label calculation: ";
211  if (!equals(filter->GetVolumeForLabel(257), 14.328))
212  {
213  std::cout << "[FAILED]" << std::endl;
214  return EXIT_FAILURE;
215  }
216  else if (!equals(filter->GetVolumeForLabel(0), 12.672))
217  {
218  std::cout << "[FAILED]" << std::endl;
219  return EXIT_FAILURE;
220  }
221  else
222  {
223  std::cout << "[PASSED]" << std::endl;
224  }
225 
226  std::cout << "Testing volume for index calculation: ";
227  if (!equals(filter->GetVolumeForNthOutput(1), 14.328))
228  {
229  std::cout << "[FAILED]" << std::endl;
230  return EXIT_FAILURE;
231  }
232  else if (!equals(filter->GetVolumeForNthOutput(0), 12.672))
233  {
234  std::cout << "[FAILED]" << std::endl;
235  return EXIT_FAILURE;
236  }
237  else
238  {
239  std::cout << "[PASSED]" << std::endl;
240  }
241 
242  mitk::ReferenceCountWatcher::Pointer outputSurface1Watcher =
243  new mitk::ReferenceCountWatcher(filter->GetOutput(1), "outputSurface1");
244  mitk::ReferenceCountWatcher::Pointer filterWatcher = new mitk::ReferenceCountWatcher(filter, "filter");
245 
246  std::cout << "Create surface for background (label 0): " << std::flush;
247  filter->GenerateAllLabelsOff();
248  filter->SetLabel(0);
249  filter->SetBackgroundLabel(257);
250  // mitk::Surface::Pointer surface = filter->GetOutput(1);
251  // std::cout<< surface->GetReferenceCount() << std::endl;
252  filter->Update();
253  // surface = nullptr;
254 
255  if (filter->GetNumberOfOutputs() != 1)
256  {
257  std::cout << "Wrong number of outputs, [FAILED]" << std::endl;
258  return EXIT_FAILURE;
259  }
260  else if (filter->GetOutput() == nullptr)
261  {
262  std::cout << "Output is nullptr, [FAILED]" << std::endl;
263  return EXIT_FAILURE;
264  }
265  else if (filter->GetOutput()->GetVtkPolyData() == nullptr)
266  {
267  std::cout << "PolyData of surface is nullptr, [FAILED]" << std::endl;
268  return EXIT_FAILURE;
269  }
270  else
271  {
272  std::cout << "[PASSED]" << std::endl;
273  }
274  std::cout << "Testing reference count correctness of old output 1: " << std::flush;
275  if (outputSurface1Watcher->GetReferenceCount() != 0)
276  {
277  std::cout << "outputSurface1Watcher->GetReferenceCount()==" << outputSurface1Watcher->GetReferenceCount()
278  << "!=0, [FAILED]" << std::endl;
279  return EXIT_FAILURE;
280  }
281  std::cout << "[PASSED]" << std::endl;
282  std::cout << "Testing reference count correctness of filter: " << std::flush;
283  if (filterWatcher->GetReferenceCount() != 2)
284  {
285  std::cout << "filterWatcher->GetReferenceCount()==" << outputSurface1Watcher->GetReferenceCount() << "!=2, [FAILED]"
286  << std::endl;
287  return EXIT_FAILURE;
288  }
289  std::cout << "[PASSED]" << std::endl;
290 
291  std::cout << "Testing index to label conversion: ";
292  if (filter->GetLabelForNthOutput(0) != 0)
293  {
294  std::cout << "[FAILED]" << std::endl;
295  return EXIT_FAILURE;
296  }
297  else
298  {
299  std::cout << "[PASSED]" << std::endl;
300  }
301 
302  std::cout << "Testing volume for label calculation: ";
303  if (!equals(filter->GetVolumeForLabel(filter->GetLabel()), 12.672))
304  {
305  std::cout << "[FAILED]" << std::endl;
306  return EXIT_FAILURE;
307  }
308  else
309  {
310  std::cout << "[PASSED]" << std::endl;
311  }
312 
313  std::cout << "Testing volume for index calculation: ";
314  if (!equals(filter->GetVolumeForNthOutput(0), 12.672))
315  {
316  std::cout << "[FAILED]" << std::endl;
317  return EXIT_FAILURE;
318  }
319  else
320  {
321  std::cout << "[PASSED]" << std::endl;
322  }
323 
324  std::cout << "Create surface for invalid label: ";
325  filter->GenerateAllLabelsOff();
326  filter->SetLabel(1);
327  filter->Update();
328  if (filter->GetNumberOfOutputs() != 1)
329  {
330  std::cout << "Number of outputs != 1, [FAILED]" << std::endl;
331  return EXIT_FAILURE;
332  }
333  else if (filter->GetOutput()->GetVtkPolyData()->GetNumberOfPoints() != 0)
334  {
335  std::cout << "PolyData is not empty (" << filter->GetOutput()->GetVtkPolyData()->GetNumberOfPoints()
336  << "), [FAILED]" << std::endl;
337  return EXIT_FAILURE;
338  }
339  else
340  {
341  std::cout << "[PASSED]" << std::endl;
342  }
343 
344  std::cout << "[TEST DONE]" << std::endl;
345  return EXIT_SUCCESS;
346 }
double ScalarType
Keeps track of the reference count of an object even if it is destroyed.
int mitkLabeledImageToSurfaceFilterTest(int argc, char *argv[])
mitk::Image::Pointer image
bool equals(const mitk::ScalarType &val1, const mitk::ScalarType &val2, mitk::ScalarType epsilon=mitk::eps)
MITKCORE_EXPORT const ScalarType eps
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51