Medical Imaging Interaction Toolkit  2021.02.99-121d8b57
Medical Imaging Interaction Toolkit
mitkPixelTypeList.h
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 <stdexcept>
14 
15 namespace mitk
16 {
17  struct EmptyType
18  {
19  };
20 
21  template <typename T0 = EmptyType,
22  typename T1 = EmptyType,
23  typename T2 = EmptyType,
24  typename T3 = EmptyType,
25  typename T4 = EmptyType,
26  typename T5 = EmptyType,
27  typename T6 = EmptyType,
28  typename T7 = EmptyType,
29  typename T8 = EmptyType,
30  typename T9 = EmptyType>
31  struct PixelTypeList;
32 
33  template <typename T0,
34  typename T1,
35  typename T2,
36  typename T3,
37  typename T4,
38  typename T5,
39  typename T6,
40  typename T7,
41  typename T8,
42  typename T9>
43  struct PixelTypeList
44  {
45  typedef T0 head;
47  enum
48  {
49  length = tail::length + 1
50  };
51  };
52 
53  template <>
55  EmptyType,
56  EmptyType,
57  EmptyType,
58  EmptyType,
59  EmptyType,
60  EmptyType,
61  EmptyType,
62  EmptyType,
63  EmptyType>
64  {
65  enum
66  {
67  length = 0
68  };
69  };
70 
71  template <typename TypeList>
73  {
74  enum
75  {
76  value = TypeList::length
77  };
78  };
79 
80  template <typename TypeList,
81  int Index, // requested element index
82  int Step = 0, // current recusion step
83  bool Stop = (Index == Step), // stop recusion flag
84  bool OutOfRange = PixelTypeLength<TypeList>::value == 0 // out of range flag
85  >
86  struct GetPixelType
87  {
89  };
90 
91  //"out of range" specialization
92  template <typename TypeList, int Index, int Step, bool Stop>
94  {
95  // if OutOfRange is 'true' the 'type' is undefined
96  // so we'll get a compile-time error
97  };
98 
99  //"element found" specialization
100  template <typename TypeList, int Index, int Step, bool OutOfRange>
102  {
103  // the index is equal to the recursion step
104  // so the result type is the head of the Typlist and stop!
105  typedef typename TypeList::head type;
106  };
107 
109  // run-time type switch
110  template <typename TypeList, int Index = 0, bool Stop = (Index == PixelTypeLength<TypeList>::value)>
112 
113  template <typename TypeList, int Index, bool Stop>
114  struct PixelTypeSwitch
115  {
116  template <typename F>
117  bool operator()(int i, F &f)
118  {
119  if (i == Index)
120  {
121  return f.operator()<typename GetPixelType<TypeList, Index>::type>();
122  }
123  else
124  {
126  return next(i, f);
127  }
128  }
129  };
130 
131  template <typename TypeList, int Index>
132  struct PixelTypeSwitch<TypeList, Index, true>
133  {
134  template <typename F>
135  bool operator()(int, F &)
136  {
137  throw std::out_of_range("Index out of range");
138  }
139  };
140 
141  template <typename X, int VDimension, typename T1 = EmptyType, typename T2 = EmptyType, typename T3 = EmptyType>
143  {
144  typedef void (*CallBack)(T1, T2, T3);
145 
147  X *cl, CallBack callBack, const mitk::Image *mitkImage, T1 t1 = T1(), T2 t2 = T2(), T3 t3 = T3())
148  : cl(cl), callBack(callBack), mitkImage(mitkImage), pixelType(mitkImage->GetPixelType()), t1(t1), t2(t2), t3(t3)
149  {
150  }
151 
152  template <typename PixelType>
153  bool operator()()
154  {
155  if (pixelType != typeid(PixelType))
156  return false;
157  if (mitkImage->GetDimension() != VDimension)
158  return false;
159 
160  const_cast<mitk::Image *>(mitkImage)->Update();
161 
162  typedef itk::Image<PixelType, VDimension> ImageType;
163  typedef mitk::ImageToItk<ImageType> ImageToItkType;
164  itk::SmartPointer<ImageToItkType> imagetoitk = ImageToItkType::New();
165  imagetoitk->SetInput(mitkImage);
166  imagetoitk->Update();
167  cl->*callBack(imagetoitk->GetOutput(), t1, t2, t3);
168  return true;
169  }
170 
171  private:
172  X *cl;
173  CallBack callBack;
174 
175  const mitk::Image *mitkImage;
176  const mitk::PixelType &pixelType;
177  T1 t1;
178  T2 t2;
179  T3 t3;
180  };
181 
182 } // namespace mitk
PixelTypeList< T1, T2, T3, T4, T5, T6, T7, T8, T9 > tail
DataCollection - Class to facilitate loading/accessing structured data.
bool operator()(int i, F &f)
GetPixelType< typename TypeList::tail, Index, Step+1 >::type type
unsigned int GetDimension() const
Get dimension of the image.
Image class for storing images.
Definition: mitkImage.h:73
AccessItkImageFunctor(X *cl, CallBack callBack, const mitk::Image *mitkImage, T1 t1=T1(), T2 t2=T2(), T3 t3=T3())
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51