Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
itkMultiOutputNaryFunctorImageFilterTest.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 "itkImage.h"
14 #include "itkImageRegionIterator.h"
15 
17 
18 #include "mitkTestingMacros.h"
19 #include "mitkVector.h"
20 
22 
23 class TestFunctor
24 {
25 public:
26  typedef std::vector<int> InputPixelArrayType;
27  typedef std::vector<int> OutputPixelArrayType;
28  typedef itk::Index<2> IndexType;
29 
30  TestFunctor()
31  {
32  secondOutputSelection = 0;
33  };
34 
35  ~TestFunctor() {};
36 
37  int secondOutputSelection;
38 
39  unsigned int GetNumberOfOutputs() const
40  {
41  return 4;
42  }
43 
44  bool operator!=( const TestFunctor & other) const
45  {
46  return !(*this == other);
47  }
48 
49  bool operator==( const TestFunctor & other ) const
50  {
51  return secondOutputSelection == other.secondOutputSelection;
52  }
53 
54  inline OutputPixelArrayType operator()( const InputPixelArrayType & value, const IndexType& currentIndex ) const
55  {
56  OutputPixelArrayType result;
57 
58  int sum = 0;
59  for (InputPixelArrayType::const_iterator pos = value.begin(); pos != value.end(); ++pos)
60  {
61  sum += *pos;
62  }
63 
64  result.push_back(sum);
65  result.push_back(value[secondOutputSelection]);
66  result.push_back(currentIndex[0]);
67  result.push_back(currentIndex[1]);
68 
69  return result;
70  }
71 };
72 
73 int itkMultiOutputNaryFunctorImageFilterTest(int /*argc*/, char*[] /*argv[]*/)
74 {
75  // always start with this!
76  MITK_TEST_BEGIN("itkMultiOutputNaryFunctorImageFilter")
77 
78  //Prepare test artifacts and helper
79 
80  mitk::TestImageType::Pointer img1 = mitk::GenerateTestImage();
81  mitk::TestImageType::Pointer img2 = mitk::GenerateTestImage(10);
82  mitk::TestImageType::Pointer img3 = mitk::GenerateTestImage(100);
83 
84  mitk::TestImageType::IndexType testIndex1;
85  testIndex1[0] = 0;
86  testIndex1[1] = 0;
87 
88  mitk::TestImageType::IndexType testIndex2;
89  testIndex2[0] = 2;
90  testIndex2[1] = 0;
91 
92  mitk::TestImageType::IndexType testIndex3;
93  testIndex3[0] = 0;
94  testIndex3[1] = 1;
95 
96  mitk::TestImageType::IndexType testIndex4;
97  testIndex4[0] = 1;
98  testIndex4[1] = 1;
99 
100  mitk::TestImageType::IndexType testIndex5;
101  testIndex5[0] = 2;
102  testIndex5[1] = 2;
103 
104  //Test default usage of filter
106  FilterType::Pointer testFilter = FilterType::New();
107 
108  testFilter->SetInput(0,img1);
109  testFilter->SetInput(1,img2);
110  testFilter->SetInput(2,img3);
111 
112  testFilter->SetNumberOfThreads(2);
113 
114  testFilter->Update();
115 
116  mitk::TestImageType::Pointer out1 = testFilter->GetOutput(0);
117  mitk::TestImageType::Pointer out2 = testFilter->GetOutput(1);
118  mitk::TestImageType::Pointer out3 = testFilter->GetOutput(2);
119  mitk::TestImageType::Pointer out4 = testFilter->GetOutput(3);
120 
121  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #1 (functor #1)",111 == out1->GetPixel(testIndex1));
122  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #2 (functor #1)",333 == out1->GetPixel(testIndex2));
123  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #3 (functor #1)",444 == out1->GetPixel(testIndex3));
124  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #4 (functor #1)",555 == out1->GetPixel(testIndex4));
125  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #5 (functor #1)",999 == out1->GetPixel(testIndex5));
126 
127  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #1 (functor #1)",1 == out2->GetPixel(testIndex1));
128  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #2 (functor #1)",3 == out2->GetPixel(testIndex2));
129  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #3 (functor #1)",4 == out2->GetPixel(testIndex3));
130  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #4 (functor #1)",5 == out2->GetPixel(testIndex4));
131  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #5 (functor #1)",9 == out2->GetPixel(testIndex5));
132 
133  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #1 (functor #1)",0 == out3->GetPixel(testIndex1));
134  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #2 (functor #1)",2 == out3->GetPixel(testIndex2));
135  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #3 (functor #1)",0 == out3->GetPixel(testIndex3));
136  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #4 (functor #1)",1 == out3->GetPixel(testIndex4));
137  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #5 (functor #1)",2 == out3->GetPixel(testIndex5));
138 
139  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #1 (functor #1)",0 == out4->GetPixel(testIndex1));
140  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #2 (functor #1)",0 == out4->GetPixel(testIndex2));
141  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #3 (functor #1)",1 == out4->GetPixel(testIndex3));
142  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #4 (functor #1)",1 == out4->GetPixel(testIndex4));
143  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #5 (functor #1)",2 == out4->GetPixel(testIndex5));
144 
145  //Test with functor set by user
146  TestFunctor funct2;
147  funct2.secondOutputSelection = 1;
148 
149  testFilter->SetFunctor(funct2);
150 
151  testFilter->Update();
152 
153  out1 = testFilter->GetOutput(0);
154  out2 = testFilter->GetOutput(1);
155  out3 = testFilter->GetOutput(2);
156  out4 = testFilter->GetOutput(3);
157 
158  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #1 (functor #2)",111 == out1->GetPixel(testIndex1));
159  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #2 (functor #2)",333 == out1->GetPixel(testIndex2));
160  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #3 (functor #2)",444 == out1->GetPixel(testIndex3));
161  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #4 (functor #2)",555 == out1->GetPixel(testIndex4));
162  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #1 index #5 (functor #2)",999 == out1->GetPixel(testIndex5));
163 
164  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #1 (functor #2)",10 == out2->GetPixel(testIndex1));
165  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #2 (functor #2)",30 == out2->GetPixel(testIndex2));
166  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #3 (functor #2)",40 == out2->GetPixel(testIndex3));
167  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #4 (functor #2)",50 == out2->GetPixel(testIndex4));
168  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #2 index #5 (functor #2)",90 == out2->GetPixel(testIndex5));
169 
170  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #1 (functor #2)",0 == out3->GetPixel(testIndex1));
171  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #2 (functor #2)",2 == out3->GetPixel(testIndex2));
172  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #3 (functor #2)",0 == out3->GetPixel(testIndex3));
173  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #4 (functor #2)",1 == out3->GetPixel(testIndex4));
174  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #3 index #5 (functor #2)",2 == out3->GetPixel(testIndex5));
175 
176  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #1 (functor #2)",0 == out4->GetPixel(testIndex1));
177  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #2 (functor #2)",0 == out4->GetPixel(testIndex2));
178  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #3 (functor #2)",1 == out4->GetPixel(testIndex3));
179  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #4 (functor #2)",1 == out4->GetPixel(testIndex4));
180  CPPUNIT_ASSERT_MESSAGE("Check pixel of output #4 index #5 (functor #2)",2 == out4->GetPixel(testIndex5));
181 
182  //Test with mask set
183  mitk::TestMaskType::Pointer mask = mitk::GenerateTestMask();
184  testFilter->SetMask(mask);
185 
186  testFilter->Update();
187 
188  out1 = testFilter->GetOutput(0);
189  out2 = testFilter->GetOutput(1);
190  out3 = testFilter->GetOutput(2);
191  out4 = testFilter->GetOutput(3);
192 
193  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #1 index #1 (functor #2)",0 == out1->GetPixel(testIndex1));
194  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #1 index #2 (functor #2)",333 == out1->GetPixel(testIndex2));
195  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #1 index #3 (functor #2)",444 == out1->GetPixel(testIndex3));
196  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #1 index #4 (functor #2)",0 == out1->GetPixel(testIndex4));
197  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #1 index #5 (functor #2)",0 == out1->GetPixel(testIndex5));
198 
199  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #2 index #1 (functor #2)",0 == out2->GetPixel(testIndex1));
200  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #2 index #2 (functor #2)",30 == out2->GetPixel(testIndex2));
201  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #2 index #3 (functor #2)",40 == out2->GetPixel(testIndex3));
202  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #2 index #4 (functor #2)",0 == out2->GetPixel(testIndex4));
203  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #2 index #5 (functor #2)",0 == out2->GetPixel(testIndex5));
204 
205  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #3 index #1 (functor #2)",0 == out3->GetPixel(testIndex1));
206  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #3 index #2 (functor #2)",2 == out3->GetPixel(testIndex2));
207  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #3 index #3 (functor #2)",0 == out3->GetPixel(testIndex3));
208  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #3 index #4 (functor #2)",0 == out3->GetPixel(testIndex4));
209  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #3 index #5 (functor #2)",0 == out3->GetPixel(testIndex5));
210 
211  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #4 index #1 (functor #2)",0 == out4->GetPixel(testIndex1));
212  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #4 index #2 (functor #2)",0 == out4->GetPixel(testIndex2));
213  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #4 index #3 (functor #2)",1 == out4->GetPixel(testIndex3));
214  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #4 index #4 (functor #2)",0 == out4->GetPixel(testIndex4));
215  CPPUNIT_ASSERT_MESSAGE("Check pixel of masked output #4 index #5 (functor #2)",0 == out4->GetPixel(testIndex5));
216 
217  MITK_TEST_END()
218 }
MITKCORE_EXPORT bool operator!=(const InteractionEvent &a, const InteractionEvent &b)
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
TestImageType::Pointer MITKTESTINGHELPER_EXPORT GenerateTestImage(int factor=1)
Perform a generic pixel-wise operation on N images and produces m output images.
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
int itkMultiOutputNaryFunctorImageFilterTest(int, char *[])
mitk::Image::Pointer mask
and MITK_TEST_END()
TestMaskType::Pointer MITKTESTINGHELPER_EXPORT GenerateTestMask()