Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkAccessByItkTest.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 <stdexcept>
18 
19 #include "mitkTestingMacros.h"
20 
21 #include <mitkITKImageImport.h>
22 #include <mitkImageAccessByItk.h>
23 
24 #define TestImageType(type, dim) \
25  MITK_TEST_CONDITION(typeid(type) == typeid(TPixel) && dim == VDimension, \
26  "Checking for correct type itk::Image<" #type "," #dim ">")
27 
28 #define TestVectorImageType(type, dim) \
29  MITK_TEST_CONDITION(typeid(type) == typeid(TPixel) && dim == VDimension && \
30  typeid(itk::VariableLengthVector<type>) == typeid(typename ImageType::PixelType), \
31  "Checking for correct type itk::VectorImage<" #type "," #dim ">")
32 
33 class AccessByItkTest
34 {
35 public:
36  typedef AccessByItkTest Self;
37 
38  typedef itk::Image<int, 2> IntImage2D;
39  typedef itk::Image<int, 3> IntImage3D;
40  typedef itk::Image<float, 2> FloatImage2D;
41  typedef itk::Image<float, 3> FloatImage3D;
42 
43  typedef itk::VectorImage<int, 3> IntVectorImage3D;
44 
45  enum EImageType
46  {
47  Unknown = 0,
48  Int2D,
49  Int3D,
50  Float2D,
51  Float3D
52  };
53 
54  void testAccessByItk()
55  {
56  mitk::Image::Pointer mitkIntImage2D = createMitkImage<IntImage2D>();
57  mitk::Image::ConstPointer mitkIntImage3D(createMitkImage<IntImage3D>());
58  mitk::Image::ConstPointer mitkFloatImage2D(createMitkImage<FloatImage2D>());
59  mitk::Image::Pointer mitkFloatImage3D = createMitkImage<FloatImage3D>();
60 
61  AccessByItk(mitkIntImage2D, AccessItkImage);
62  AccessByItk(mitkIntImage3D, AccessItkImage);
63  AccessByItk(mitkFloatImage2D, AccessItkImage);
64  AccessByItk(mitkFloatImage3D, AccessItkImage);
65 
66  AccessByItk_n(mitkIntImage2D, AccessItkImage, (Int2D, 2));
67  AccessByItk_n(mitkIntImage3D, AccessItkImage, (Int3D, 2));
68  AccessByItk_n(mitkFloatImage2D, AccessItkImage, (Float2D, 2));
69  AccessByItk_n(mitkFloatImage3D, AccessItkImage, (Float3D, 2));
70 
71  mitk::Image::Pointer mitkIntVectorImage3D = createMitkImage<IntVectorImage3D>(2);
72 
73  // Test for wrong pixel type (the AccessByItk macro multi-plexes integral
74  // types only by default)
76  AccessByItk(mitkIntVectorImage3D, AccessItkImage);
78 
79  // Test for correct handling of vector images
80  AccessVectorPixelTypeByItk(mitkIntVectorImage3D, AccessItkImage);
81  AccessVectorPixelTypeByItk_n(mitkIntVectorImage3D, AccessItkImage, (Int3D, 2));
82  }
83 
84  void testAccessFixedDimensionByItk()
85  {
86  mitk::Image::Pointer mitkIntImage2D = createMitkImage<IntImage2D>();
87  mitk::Image::ConstPointer mitkIntImage3D(createMitkImage<IntImage3D>());
88  mitk::Image::ConstPointer mitkFloatImage2D(createMitkImage<FloatImage2D>());
89  mitk::Image::Pointer mitkFloatImage3D = createMitkImage<FloatImage3D>();
90 
91  AccessFixedDimensionByItk(mitkIntImage2D, AccessItkImage, 2);
92  AccessFixedDimensionByItk(mitkIntImage3D, AccessItkImage, 3);
93  AccessFixedDimensionByItk(mitkFloatImage2D, AccessItkImage, 2);
94  AccessFixedDimensionByItk(mitkFloatImage3D, AccessItkImage, 3);
95 
96  AccessFixedDimensionByItk_n(mitkIntImage2D, AccessItkImage, 2, (Int2D, 2));
97  AccessFixedDimensionByItk_n(mitkIntImage3D, AccessItkImage, 3, (Int3D, 2));
98  AccessFixedDimensionByItk_n(mitkFloatImage2D, AccessItkImage, 2, (Float2D, 2));
99  AccessFixedDimensionByItk_n(mitkFloatImage3D, AccessItkImage, 3, (Float3D, 2));
100 
101  // Test for wrong dimension
103  AccessFixedDimensionByItk(mitkFloatImage3D, AccessItkImage, 2);
105 
107  AccessFixedDimensionByItk_n(mitkFloatImage3D, AccessItkImage, 2, (Float3D, 2));
109  }
110 
111  void testAccessFixedPixelTypeByItk()
112  {
113  mitk::Image::Pointer mitkIntImage2D = createMitkImage<IntImage2D>();
114  mitk::Image::ConstPointer mitkIntImage3D(createMitkImage<IntImage3D>());
115  mitk::Image::ConstPointer mitkFloatImage2D(createMitkImage<FloatImage2D>());
116  mitk::Image::Pointer mitkFloatImage3D = createMitkImage<FloatImage3D>();
117 
118  AccessFixedPixelTypeByItk(mitkIntImage2D, AccessItkImage, (int)(float));
119  AccessFixedPixelTypeByItk(mitkIntImage3D, AccessItkImage, (int)(float));
120  AccessFixedPixelTypeByItk(mitkFloatImage2D, AccessItkImage, (int)(float));
121  AccessFixedPixelTypeByItk(mitkFloatImage3D, AccessItkImage, (int)(float));
122 
123  AccessFixedPixelTypeByItk_n(mitkIntImage2D, AccessItkImage, (int)(float), (Int2D, 2));
124  AccessFixedPixelTypeByItk_n(mitkIntImage3D, AccessItkImage, (int)(float), (Int3D, 2));
125  AccessFixedPixelTypeByItk_n(mitkFloatImage2D, AccessItkImage, (int)(float), (Float2D, 2));
126  AccessFixedPixelTypeByItk_n(mitkFloatImage3D, AccessItkImage, (int)(float), (Float3D, 2));
127 
128  // Test for wrong pixel type
130  AccessFixedPixelTypeByItk(mitkFloatImage3D, AccessItkImage, (int));
132 
134  AccessFixedPixelTypeByItk_n(mitkFloatImage3D, AccessItkImage, (int), (Float3D, 2));
136  }
137 
138  void testAccessFixedTypeByItk()
139  {
140  mitk::Image::Pointer mitkIntImage2D = createMitkImage<IntImage2D>();
141  mitk::Image::ConstPointer mitkIntImage3D(createMitkImage<IntImage3D>());
142  mitk::Image::ConstPointer mitkFloatImage2D(createMitkImage<FloatImage2D>());
143  mitk::Image::Pointer mitkFloatImage3D = createMitkImage<FloatImage3D>();
144 
145  AccessFixedTypeByItk(mitkIntImage2D, AccessItkImage, (int)(float), (2)(3));
146  AccessFixedTypeByItk(mitkIntImage3D, AccessItkImage, (int)(float), (2)(3));
147  AccessFixedTypeByItk(mitkFloatImage2D, AccessItkImage, (int)(float), (2)(3));
148  AccessFixedTypeByItk(mitkFloatImage3D, AccessItkImage, (int)(float), (2)(3));
149 
150  AccessFixedTypeByItk_n(mitkIntImage2D, AccessItkImage, (int)(float), (2)(3), (Int2D, 2));
151  AccessFixedTypeByItk_n(mitkIntImage3D, AccessItkImage, (int)(float), (2)(3), (Int3D, 2));
152  AccessFixedTypeByItk_n(mitkFloatImage2D, AccessItkImage, (int)(float), (2)(3), (Float2D, 2));
153  AccessFixedTypeByItk_n(mitkFloatImage3D, AccessItkImage, (int)(float), (2)(3), (Float3D, 2));
154 
155  // Test for wrong dimension
157  AccessFixedTypeByItk(mitkFloatImage3D, AccessItkImage, (float), (2));
159 
161  AccessFixedTypeByItk_n(mitkFloatImage3D, AccessItkImage, (float), (2), (Float3D, 2));
163 
164  // Test for wrong pixel type
166  AccessFixedTypeByItk(mitkFloatImage3D, AccessItkImage, (int), (3));
168 
170  AccessFixedTypeByItk_n(mitkFloatImage3D, AccessItkImage, (int), (3), (Float3D, 2));
172  }
173 
174  void testAccessTwoImagesFixedDimensionByItk()
175  {
176  mitk::Image::Pointer mitkIntImage2D = createMitkImage<IntImage2D>();
177  mitk::Image::ConstPointer mitkFloatImage2D(createMitkImage<FloatImage2D>());
178 
179  AccessTwoImagesFixedDimensionByItk(mitkIntImage2D, mitkFloatImage2D, AccessTwoItkImages, 2);
180  }
181 
182  template <typename TPixel, unsigned int VDimension>
183  void AccessItkImage(const itk::Image<TPixel, VDimension> *,
184  EImageType param1 = Unknown,
185  int param2 = 0,
186  int param3 = 0)
187  {
188  switch (param1)
189  {
190  case Int2D:
191  TestImageType(int, 2) break;
192  case Int3D:
193  TestImageType(int, 3) break;
194  case Float2D:
195  TestImageType(float, 2) break;
196  case Float3D:
197  TestImageType(float, 3) break;
198  default:
199  break;
200  }
201 
202  if (param2)
203  {
204  MITK_TEST_CONDITION(param2 == 2, "Checking for correct second parameter")
205  }
206  if (param3)
207  {
208  MITK_TEST_CONDITION(param3 == 3, "Checking for correct third parameter")
209  }
210  }
211 
212  template <typename TPixel, unsigned int VDimension>
213  void AccessItkImage(itk::VectorImage<TPixel, VDimension> *,
214  EImageType param1 = Unknown,
215  int param2 = 0,
216  int param3 = 0)
217  {
218  typedef itk::VectorImage<TPixel, VDimension> ImageType;
219  switch (param1)
220  {
221  case Int2D:
222  TestVectorImageType(int, 2) break;
223  case Int3D:
224  TestVectorImageType(int, 3) break;
225  case Float2D:
226  TestVectorImageType(float, 2) break;
227  case Float3D:
228  TestVectorImageType(float, 3) break;
229  default:
230  break;
231  }
232 
233  if (param2)
234  {
235  MITK_TEST_CONDITION(param2 == 2, "Checking for correct second parameter")
236  }
237  if (param3)
238  {
239  MITK_TEST_CONDITION(param3 == 3, "Checking for correct third parameter")
240  }
241  }
242 
243 private:
244  template <typename TPixel1, unsigned int VDimension1, typename TPixel2, unsigned int VDimension2>
245  void AccessTwoItkImages(itk::Image<TPixel1, VDimension1> * /*itkImage1*/,
246  itk::Image<TPixel2, VDimension2> * /*itkImage2*/)
247  {
248  if (!(typeid(int) == typeid(TPixel1) && typeid(float) == typeid(TPixel2) && VDimension1 == 2 && VDimension2 == 2))
249  {
250  throw std::runtime_error("Image type mismatch");
251  }
252  }
253 
254  template <typename ImageType>
255  mitk::Image::Pointer createMitkImage()
256  {
257  typename ImageType::Pointer itkImage = ImageType::New();
258  typename ImageType::IndexType start;
259  start.Fill(0);
260  typename ImageType::SizeType size;
261  size.Fill(3);
262  typename ImageType::RegionType region;
263  region.SetSize(size);
264  region.SetIndex(start);
265  itkImage->SetRegions(region);
266  itkImage->Allocate();
267  return mitk::GrabItkImageMemory(itkImage);
268  }
269 
270  template <typename ImageType>
271  mitk::Image::Pointer createMitkImage(std::size_t vectorLength)
272  {
273  typename ImageType::Pointer itkImage = ImageType::New();
274  typename ImageType::IndexType start;
275  start.Fill(0);
276  typename ImageType::SizeType size;
277  size.Fill(3);
278  typename ImageType::RegionType region;
279  region.SetSize(size);
280  region.SetIndex(start);
281  itkImage->SetRegions(region);
282  itkImage->SetVectorLength(vectorLength);
283  itkImage->Allocate();
284  return mitk::GrabItkImageMemory(itkImage);
285  }
286 };
287 
288 int mitkAccessByItkTest(int /*argc*/, char * /*argv*/ [])
289 {
290  MITK_TEST_BEGIN("AccessByItk")
291 
292  AccessByItkTest accessTest;
293 
294  MITK_TEST_OUTPUT(<< "Testing AccessByItk macro")
295  accessTest.testAccessByItk();
296 
297  MITK_TEST_OUTPUT(<< "Testing AccessFixedDimensionByItk macro")
298  accessTest.testAccessFixedDimensionByItk();
299 
300  MITK_TEST_OUTPUT(<< "Testing AccessFixedTypeByItk macro")
301  accessTest.testAccessFixedTypeByItk();
302 
303  MITK_TEST_OUTPUT(<< "Testing AccessFixedPixelTypeByItk macro")
304  accessTest.testAccessFixedPixelTypeByItk();
305 
306  MITK_TEST_OUTPUT(<< "Testing AccessTwoImagesFixedDimensionByItk macro")
307  accessTest.testAccessTwoImagesFixedDimensionByItk();
308 
309  MITK_TEST_END()
310 }
#define AccessFixedTypeByItk_n(mitkImage, itkImageTypeFunction, pixelTypeSeq, dimSeq, va_tuple)
Access a mitk-image with known type (pixel type and dimension) by an itk-image with one or more param...
itk::SmartPointer< Self > Pointer
#define MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS)
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
itk::Image< unsigned char, 3 > ImageType
#define AccessFixedDimensionByItk_n(mitkImage, itkImageTypeFunction, dimension, va_tuple)
Access a mitk-image with known dimension by an itk-image with one or more parameters.
#define AccessFixedPixelTypeByItk_n(mitkImage, itkImageTypeFunction, pixelTypeSeq, va_tuple)
Access a mitk-image with known pixeltype (but unknown dimension) by an itk-image with one or more par...
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
#define AccessByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a MITK image by an ITK image with one or more parameters.
#define TestImageType(type, dim)
int mitkAccessByItkTest(int, char *[])
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
#define MITK_TEST_OUTPUT(x)
Output some text.
#define AccessFixedTypeByItk(mitkImage, itkImageTypeFunction, pixelTypeSeq, dimSeq)
Access a mitk-image with known type (pixel type and dimension) by an itk-image.
#define AccessVectorPixelTypeByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a vector mitk::Image by an ITK vector image with one or more parameters.
#define TestVectorImageType(type, dim)
#define MITK_TEST_CONDITION(COND, MSG)
#define MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS)
Begin block which should be checked for exceptions.
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
#define AccessFixedPixelTypeByItk(mitkImage, itkImageTypeFunction, pixelTypeSeq)
Access a mitk-image with known pixeltype (but unknown dimension) by an itk-image. ...
Exception class thrown in AccessByItk macros.
#define AccessVectorPixelTypeByItk(mitkImage, itkImageTypeFunction)
#define AccessTwoImagesFixedDimensionByItk(mitkImage1, mitkImage2, itkImageTypeFunction, dimension)
Access two mitk-images with known dimension by itk-images.
and MITK_TEST_END()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.