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
mitkSurfaceToImageFilterTest.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 <mitkIOUtil.h>
19 #include <mitkImageWriteAccessor.h>
20 #include <mitkTestingMacros.h>
21 
23 
24 #include "mitkTestFixture.h"
25 #include <vtkPolyData.h>
26 
27 class mitkSurfaceToImageFilterTestSuite : public mitk::TestFixture
28 {
29  CPPUNIT_TEST_SUITE(mitkSurfaceToImageFilterTestSuite);
30  MITK_TEST(test3DSurfaceValidOutput);
31  MITK_TEST(test3DSurfaceCorrect);
32  MITK_TEST(test3DSurfaceIn4DImage);
33  CPPUNIT_TEST_SUITE_END();
34 
35 private:
37  mitk::Surface::Pointer m_Surface;
38 
39 public:
44  void setUp() override { m_Surface = mitk::IOUtil::LoadSurface(GetTestDataFilePath("ball.stl")); }
45  void tearDown() override {}
46  void test3DSurfaceValidOutput()
47  {
49 
50  mitk::Image::Pointer additionalInputImage = mitk::Image::New();
51  additionalInputImage->Initialize(mitk::MakeScalarPixelType<unsigned int>(), *m_Surface->GetTimeGeometry());
52 
53  // Arrange the filter
54  surfaceToImageFilter->MakeOutputBinaryOn();
55  surfaceToImageFilter->SetInput(m_Surface);
56  surfaceToImageFilter->SetImage(additionalInputImage);
57  surfaceToImageFilter->Update();
58 
59  CPPUNIT_ASSERT_MESSAGE(
60  "SurfaceToImageFilter_AnyInputImageAndModeSetToBinary_ResultIsImageWithUCHARPixelType",
61  surfaceToImageFilter->GetOutput()->GetPixelType().GetComponentType() == itk::ImageIOBase::UCHAR);
62 
63  surfaceToImageFilter->SetUShortBinaryPixelType(true);
64  surfaceToImageFilter->Update();
65 
66  CPPUNIT_ASSERT_MESSAGE(
67  "SurfaceToImageFilter_AnyInputImageAndModeSetToBinary_ResultIsImageWithUCHARPixelType",
68  surfaceToImageFilter->GetOutput()->GetPixelType().GetComponentType() == itk::ImageIOBase::USHORT);
69  }
70 
71  void test3DSurfaceCorrect()
72  {
74 
75  // todo I don't know if this image is always needed. There is no documentation of the filter. Use git blame and ask
76  // the author.
77  mitk::Image::Pointer additionalInputImage = mitk::Image::New();
78  unsigned int *dims = new unsigned int[3];
79  dims[0] = 32;
80  dims[1] = 32;
81  dims[2] = 32;
82  additionalInputImage->Initialize(mitk::MakeScalarPixelType<unsigned int>(), 3, dims);
83  additionalInputImage->SetOrigin(m_Surface->GetGeometry()->GetOrigin());
84  additionalInputImage->GetGeometry()->SetIndexToWorldTransform(m_Surface->GetGeometry()->GetIndexToWorldTransform());
85  // Arrange the filter
86  // The docu does not really tell if this is always needed. Could we skip SetImage in any case?
87  surfaceToImageFilter->MakeOutputBinaryOn();
88  surfaceToImageFilter->SetInput(m_Surface);
89  surfaceToImageFilter->SetImage(additionalInputImage);
90  surfaceToImageFilter->Update();
91 
92  mitk::ImagePixelReadAccessor<unsigned char, 3> outputReader(surfaceToImageFilter->GetOutput());
93  itk::Index<3> idx;
94  bool valuesCorrect = true;
95  // Values outside the ball should be 0
96  idx[0] = 0;
97  idx[1] = 0, idx[2] = 0;
98  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
99  idx[0] = 0;
100  idx[1] = 15, idx[2] = 15;
101  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
102  idx[0] = 15;
103  idx[1] = 15, idx[2] = 0;
104  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
105  idx[0] = 15;
106  idx[1] = 0, idx[2] = 15;
107  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
108  idx[0] = 5;
109  idx[1] = 9, idx[2] = 23;
110  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
111  // Values inside the ball should be 1
112  idx[0] = 15;
113  idx[1] = 15, idx[2] = 15;
114  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
115  idx[0] = 31;
116  idx[1] = 15, idx[2] = 15;
117  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
118  idx[0] = 2;
119  idx[1] = 15, idx[2] = 15;
120  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
121  idx[0] = 15;
122  idx[1] = 15, idx[2] = 2;
123  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
124  idx[0] = 15;
125  idx[1] = 2, idx[2] = 15;
126  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
127  idx[0] = 6;
128  idx[1] = 9, idx[2] = 23;
129  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
130 
131  CPPUNIT_ASSERT_MESSAGE("SurfaceToImageFilter_BallSurfaceAsInput_OutputCorrect", valuesCorrect == true);
132  }
133 
134  void test3DSurfaceIn4DImage()
135  {
137 
138  mitk::Image::Pointer additionalInputImage = mitk::Image::New();
139  unsigned int *dims = new unsigned int[4];
140  dims[0] = 32;
141  dims[1] = 32;
142  dims[2] = 32;
143  dims[3] = 2;
144  additionalInputImage->Initialize(mitk::MakeScalarPixelType<unsigned int>(), 4, dims);
145  additionalInputImage->SetOrigin(m_Surface->GetGeometry()->GetOrigin());
146  additionalInputImage->GetGeometry()->SetIndexToWorldTransform(m_Surface->GetGeometry()->GetIndexToWorldTransform());
147  mitk::Image::Pointer secondStep = additionalInputImage->Clone();
148  unsigned int size = sizeof(unsigned char);
149  for (unsigned int i = 0; i < secondStep->GetDimension(); ++i)
150  size *= secondStep->GetDimension(i);
151  mitk::ImageWriteAccessor accessor(secondStep);
152  memset(accessor.GetData(), 1, size);
153  additionalInputImage->GetTimeGeometry()->Expand(2);
154  additionalInputImage->GetGeometry(1)->SetSpacing(secondStep->GetGeometry()->GetSpacing());
155  additionalInputImage->GetGeometry(1)->SetOrigin(secondStep->GetGeometry()->GetOrigin());
156  additionalInputImage->GetGeometry(1)->SetIndexToWorldTransform(
157  secondStep->GetGeometry()->GetIndexToWorldTransform());
158  additionalInputImage->SetImportVolume(secondStep->GetData(), 0);
159  additionalInputImage->SetImportVolume(secondStep->GetData(), 1);
160 
161  // Arrange the filter
162  surfaceToImageFilter->MakeOutputBinaryOn();
163  surfaceToImageFilter->SetInput(m_Surface);
164  surfaceToImageFilter->SetImage(additionalInputImage);
165 
166  surfaceToImageFilter->Update();
167 
168  mitk::ImagePixelReadAccessor<unsigned char, 4> outputReader(surfaceToImageFilter->GetOutput());
169  itk::Index<4> idx;
170  bool valuesCorrect = true;
171  // Values outside the ball should be 0
172  idx[0] = 0;
173  idx[1] = 0, idx[2] = 0;
174  idx[3] = 0;
175  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
176  idx[0] = 0;
177  idx[1] = 15, idx[2] = 15;
178  idx[3] = 0;
179  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
180  idx[0] = 15;
181  idx[1] = 15, idx[2] = 0;
182  idx[3] = 0;
183  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
184  idx[0] = 15;
185  idx[1] = 0, idx[2] = 15;
186  idx[3] = 0;
187  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
188  idx[0] = 5;
189  idx[1] = 9, idx[2] = 23;
190  idx[3] = 0;
191  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
192  // Values inside the ball should be 1 hould be 1
193  idx[0] = 15;
194  idx[1] = 15, idx[2] = 15;
195  idx[3] = 0;
196  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
197  idx[0] = 31;
198  idx[1] = 15, idx[2] = 15;
199  idx[3] = 0;
200  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
201  idx[0] = 2;
202  idx[1] = 15, idx[2] = 15;
203  idx[3] = 0;
204  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
205  idx[0] = 15;
206  idx[1] = 15, idx[2] = 2;
207  idx[3] = 0;
208  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
209  idx[0] = 15;
210  idx[1] = 2, idx[2] = 15;
211  idx[3] = 0;
212  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
213  idx[0] = 6;
214  idx[1] = 9, idx[2] = 23;
215  idx[3] = 0;
216  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 1);
217  // Values inside the ball but in the second timestep hould be 0
218  idx[0] = 15;
219  idx[1] = 15, idx[2] = 15;
220  idx[3] = 1;
221  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
222  idx[0] = 31;
223  idx[1] = 15, idx[2] = 15;
224  idx[3] = 1;
225  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
226  idx[0] = 2;
227  idx[1] = 15, idx[2] = 15;
228  idx[3] = 1;
229  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
230  idx[0] = 15;
231  idx[1] = 15, idx[2] = 2;
232  idx[3] = 1;
233  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
234  idx[0] = 15;
235  idx[1] = 2, idx[2] = 15;
236  idx[3] = 1;
237  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
238  idx[0] = 6;
239  idx[1] = 9, idx[2] = 23;
240  idx[3] = 1;
241  valuesCorrect = valuesCorrect && (outputReader.GetPixelByIndex(idx) == 0);
242 
243  CPPUNIT_ASSERT_MESSAGE("SurfaceToImageFilter_BallSurfaceAsInput_Output4DCorrect", valuesCorrect == true);
244  }
245 };
246 MITK_TEST_SUITE_REGISTRATION(mitkSurfaceToImageFilter)
static mitk::Surface::Pointer LoadSurface(const std::string &path)
LoadSurface Convenience method to load an arbitrary mitkSurface.
Definition: mitkIOUtil.cpp:608
Gives locked and index-based read access for a particular image part. The class provides several set-...
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
Test fixture for parameterized tests.
static Pointer New()
ImageWriteAccessor class to get locked write-access for a particular image part.