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