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