ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkDefaultConvertPixelTraits.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkDefaultConvertPixelTraits_h
19 #define __itkDefaultConvertPixelTraits_h
20 
21 #include "itkOffset.h"
22 #include "itkVector.h"
23 #include "itkMatrix.h"
25 #include "itkVariableSizeMatrix.h"
26 
27 namespace itk
28 {
40 template< typename PixelType >
42 {
43 public:
45  typedef typename PixelType::ComponentType ComponentType;
46 
48  static unsigned int GetNumberOfComponents()
49  { return PixelType::GetNumberOfComponents(); }
50 
51  static unsigned int GetNumberOfComponents( const PixelType itkNotUsed(pixel) )
52  { return PixelType::GetNumberOfComponents( ); }
53 
55  static ComponentType GetNthComponent(int c, const PixelType & pixel)
56  { return pixel.GetNthComponent(c); }
57 
59  static void SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
60  { pixel.SetNthComponent(c, v); }
61 
63  static ComponentType GetScalarValue(const PixelType & pixel)
64  { return pixel.GetScalarValue(); }
65 };
67 
68 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
69  template< > \
70  class DefaultConvertPixelTraits< type > \
71  { \
72 public: \
73  typedef type ComponentType; \
74  static unsigned int GetNumberOfComponents() \
75  { \
76  return 1; \
77  } \
78  static unsigned int GetNumberOfComponents(const type) \
79  { \
80  return 1; \
81  } \
82  static void SetNthComponent(int, type & pixel, const ComponentType &v) \
83  { \
84  pixel = v; \
85  } \
86  static type GetNthComponent(int, const type pixel) \
87  { \
88  return pixel; \
89  } \
90  static type GetScalarValue(const type &pixel) \
91  { \
92  return pixel; \
93  } \
94  };
95 
108 
109 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
110 
111 //
112 // Default traits for the Offset<> pixel type
113 //
114 
115 template<unsigned int VDimension>
116 class DefaultConvertPixelTraits< Offset< VDimension > >
117 {
118 public:
121  static unsigned int GetNumberOfComponents()
122  {
123  return VDimension;
124  }
125  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
126  {
127  pixel[i] = v;
128  }
130  {
131  return pixel[0];
132  }
133 };
134 
135 //
136 // Default traits for the pixel types deriving from FixedArray<>
137 //
138 
139 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type) \
140  template<typename TComponentType, unsigned VDimension > \
141  class DefaultConvertPixelTraits< type< TComponentType, VDimension > > \
142  { \
143  public: \
144  typedef type< TComponentType, VDimension > TargetType; \
145  typedef TComponentType ComponentType; \
146  static unsigned int GetNumberOfComponents() \
147  { \
148  return VDimension; \
149  } \
150  static unsigned int GetNumberOfComponents( const TargetType ) \
151  { \
152  return VDimension; \
153  } \
154  static void SetNthComponent(int i, TargetType & pixel, \
155  const ComponentType &v) \
156  { \
157  pixel[i] = v; \
158  } \
159  static ComponentType GetNthComponent(int i, const TargetType pixel) \
160  { \
161  return pixel[i]; \
162  } \
163  static ComponentType GetScalarValue(const TargetType &pixel) \
164  { \
165  return pixel[0]; \
166  } \
167  } \
168 
173 
174 //
175 // End of Traits for the classes deriving from FixedArray.
176 //
177 //
178 
179 //
180 // Default traits for pixel types deriving from VariableLengthVector<>
181 //
182 template<typename VComponent>
184 {
185 public:
187  typedef VComponent ComponentType;
188  static unsigned int GetNumberOfComponents()
189  {
190  return 0;
191  }
192  static unsigned int GetNumberOfComponents( const TargetType pixel )
193  {
194  return pixel.Size();
195  }
196  static void SetNthComponent(int i, TargetType & pixel,
197  const ComponentType &v)
198  {
199  pixel[i] = v;
200  }
201  static ComponentType GetNthComponent(int i, const TargetType & pixel)
202  {
203  return pixel[i];
204  }
206  {
207  return pixel.GetNorm();
208  }
209 };
210 
211 
212 //
213 // Default traits for pixel types deriving from VariableSizeMatrix<>
214 //
215 template<typename VComponent>
217 {
218 public:
220  typedef VComponent ComponentType;
221  static unsigned int GetNumberOfComponents()
222  {
223  return 0;
224  }
225  static unsigned int GetNumberOfComponents( const TargetType pixel )
226  {
227  return pixel.Cols() * pixel.Rows();
228  }
229  static void SetNthComponent(int i, TargetType & pixel,
230  const ComponentType &v)
231  {
232  const unsigned int row = i / pixel.Cols();
233  const unsigned int col = i % pixel.Cols();
234  pixel(row,col) = v;
235  }
236  static ComponentType GetNthComponent(int i, const TargetType & pixel)
237  {
238  const unsigned int row = i / pixel.Cols();
239  const unsigned int col = i % pixel.Cols();
240  return pixel(row,col);
241  }
243  {
244  return 0.0;
245  }
246 };
247 
248 
249 //
250 // End of Traits for the classes deriving from FixedArray.
251 //
252 //
253 
254 //
255 // Default traits for the pixel types deriving from Matrix<>
256 //
257 
258 template<typename VComponent, unsigned VRows, unsigned VCols >
259 class DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >
260 {
261 public:
263  typedef VComponent ComponentType;
264  static unsigned int GetNumberOfComponents()
265  {
266  return VRows * VCols;
267  }
268  static void SetNthComponent(int i, TargetType & pixel,
269  const ComponentType &v)
270  {
271  const unsigned int row = i / VCols;
272  const unsigned int col = i % VCols;
273  pixel[row][col] = v;
274  }
275  static ComponentType GetNthComponent(int i, const TargetType & pixel)
276  {
277  const unsigned int row = i / VCols;
278  const unsigned int col = i % VCols;
279  return pixel[row][col];
280  }
282  {
283  return pixel[0][0];
284  }
285 };
286 
287 //
288 // Default traits for the pixel types deriving from std::complex<>
289 //
290 
291 template<typename TComponent >
292 class DefaultConvertPixelTraits< ::std::complex< TComponent > >
293 {
294 public:
295  typedef::std::complex< TComponent > TargetType;
296  typedef TComponent ComponentType;
297  static unsigned int GetNumberOfComponents()
298  {
299  return 2;
300  }
301  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
302  {
303  if ( i == 0 )
304  {
305  pixel = TargetType( v, pixel.imag() );
306  }
307  else
308  {
309  pixel = TargetType(pixel.real(), v);
310  }
311  }
312  static ComponentType GetScalarValue(const TargetType &pixel)
313  {
314  return std::norm(pixel);
315  }
316 };
317 
318 
319 //
320 // End of Traits for the classes deriving from std::complex.
321 //
322 //
323 } // end namespace itk
324 #endif
325