ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkMeshConvertPixelTraits.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 
19 #ifndef __itkMeshConvertPixelTraits_h
20 #define __itkMeshConvertPixelTraits_h
21 #include "ITKIOMeshExport.h"
22 
23 #include "itkArray.h"
24 #include "itkMatrix.h"
25 #include "itkOffset.h"
26 #include "itkCovariantVector.h"
28 #include "itkVector.h"
29 #include "itkPoint.h"
30 
31 namespace itk
32 {
33 
45 template<typename PixelType>
47 {
48 public:
50  typedef typename PixelType::ComponentType ComponentType;
51 
53  static unsigned int GetNumberOfComponents()
54  { return PixelType::GetNumberOfComponents();}
55 
56  static unsigned int GetNumberOfComponents(const PixelType& )
57  { return PixelType::GetNumberOfComponents();}
58 
60  static ComponentType GetNthComponent(int c, const PixelType& pixel)
61  { return pixel.GetNthComponent(c); }
62 
64  static void SetNthComponent(int c, PixelType& pixel, const ComponentType& v)
65  { pixel.SetNthComponent(c, v); }
66 
68  static ComponentType GetScalarValue(const PixelType& pixel)
69  { return pixel.GetScalarValue(); }
70 
71 };
72 
73 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
74 template<> \
75  class MeshConvertPixelTraits<type> \
76  { \
77  public: \
78  typedef type ComponentType; \
79  static unsigned int GetNumberOfComponents() \
80  { \
81  return 1; \
82  } \
83  static unsigned int GetNumberOfComponents(const type& itkNotUsed(pixel))\
84  { \
85  return 1; \
86  } \
87  static ComponentType GetNthComponent(int itkNotUsed(c), const type& pixel)\
88  { \
89  return pixel; \
90  } \
91  static void SetNthComponent(int , type& pixel, const ComponentType& v) \
92  { \
93  pixel = v; \
94  } \
95  static type GetScalarValue(const type& pixel) \
96  { \
97  return pixel; \
98  } \
99  };
100 
113 
114 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
115 
116 //
117 // Default traits for the Offset<> pixel type
118 //
119 
120 #define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension) \
121  template<> \
122  class MeshConvertPixelTraits< Offset<dimension> > \
123  { \
124  public: \
125  typedef Offset<dimension> TargetType; \
126  typedef TargetType::OffsetValueType ComponentType; \
127  static unsigned int GetNumberOfComponents() \
128  { \
129  return dimension; \
130  } \
131  static unsigned int GetNumberOfComponents(const TargetType& itkNotUsed(pixel)) \
132  { \
133  return dimension; \
134  } \
135  static ComponentType GetNthComponent(int c, const TargetType& pixel) \
136  { \
137  return pixel[c]; \
138  } \
139  static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v) \
140  { \
141  pixel[i] = v; \
142  } \
143  static ComponentType GetScalarValue(const TargetType& pixel) \
144  { \
145  return pixel[0]; \
146  } \
147  }; \
148 
149 
150 // Define traits for Offset<> from dimensions 1 to 5
156 
157 //
158 // Default traits for the pixel types deriving from FixedArray<>
159 //
160 
161 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type,componenttype, dimension) \
162  template<> \
163  class MeshConvertPixelTraits< type< componenttype, dimension> > \
164  { \
165  public: \
166  typedef type< componenttype, dimension > TargetType; \
167  typedef componenttype ComponentType; \
168  static unsigned int GetNumberOfComponents() \
169  { \
170  return dimension; \
171  } \
172  static unsigned int GetNumberOfComponents(const TargetType& itkNotUsed(pixel)) \
173  { \
174  return dimension; \
175  } \
176  static ComponentType GetNthComponent(int c, const TargetType& pixel) \
177  { \
178  return pixel[c]; \
179  } \
180  static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v) \
181  { \
182  pixel[i] = v; \
183  } \
184  static ComponentType GetScalarValue(const TargetType& pixel) \
185  { \
186  return pixel[0]; \
187  } \
188  }; \
189 
190 //
191 //
192 // Define traits for Classed deriving from FixedArray from dimensions 1 to 6
193 // These classes include: Vector, CovariantVector and Point.
194 //
195 //
196 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, Type) \
197  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,1) \
198  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,2) \
199  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,3) \
200  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,4) \
201  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,5) \
202  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,6)
203 
204 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
205  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, char); \
206  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, signed char); \
207  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned char); \
208  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, short); \
209  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned short); \
210  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, int); \
211  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned int); \
212  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long); \
213  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long); \
214  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, float); \
215  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, double);
216 
221 
222 //
223 // End of Traits for the classes deriving from FixedArray.
224 //
225 //
226 
227 
228 //
229 // Default traits for the pixel types deriving from Matrix<>
230 //
231 
232 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(type,componenttype,rows,cols) \
233 template<> \
234  class MeshConvertPixelTraits< type< componenttype, rows, cols > > \
235  { \
236  public: \
237  typedef type< componenttype, rows, cols > TargetType; \
238  typedef componenttype ComponentType; \
239  static unsigned int GetNumberOfComponents() \
240  { \
241  return rows * cols; \
242  } \
243  static unsigned int GetNumberOfComponents(const TargetType& itkNotUsed(pixel)) \
244  { \
245  return rows * cols; \
246  } \
247  static ComponentType GetNthComponent(int c, const TargetType& pixel) \
248  { \
249  const unsigned int row = c / cols; \
250  const unsigned int col = c % cols; \
251  return pixel[row][col]; \
252  } \
253  static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v) \
254  { \
255  const unsigned int row = i / cols; \
256  const unsigned int col = i % cols; \
257  pixel[row][col] = v; \
258  } \
259  static ComponentType GetScalarValue(const TargetType& pixel) \
260  { \
261  return pixel[0][0]; \
262  } \
263  }; \
264 
265 //
266 //
267 // Define traits for Classed deriving from Matrix from dimensions 1 to 6
268 //
269 //
270 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, Type) \
271  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,1,1) \
272  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,2,2) \
273  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,3,3) \
274  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,4,4) \
275  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,5,5) \
276  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,6,6)
277 
278 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(ArrayType) \
279  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, char); \
280  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, signed char); \
281  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned char); \
282  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, short); \
283  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned short); \
284  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, int); \
285  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned int); \
286  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long); \
287  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long); \
288  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, float); \
289  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, double);
290 
291 //
292 // Add here other classes that derive from Matrix or that have the same API
293 //
295 
296 //
297 // End of Traits for the classes deriving from Matrix.
298 //
299 //
300 
301 
302 //
303 // Default traits for the pixel types deriving from std::complex<>
304 //
305 
306 #define ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE( componenttype ) \
307 template<> \
308  class MeshConvertPixelTraits< ::std::complex< componenttype > > \
309  { \
310  public: \
311  typedef ::std::complex< componenttype> TargetType; \
312  typedef componenttype ComponentType; \
313  static unsigned int GetNumberOfComponents() \
314  { \
315  return 2; \
316  } \
317  static unsigned int GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
318  { \
319  return 2; \
320  } \
321  static ComponentType GetNthComponent(int i, TargetType & pixel) \
322  { \
323  if( i == 0 ) \
324  { \
325  return pixel.imag(); \
326  } \
327  else \
328  { \
329  return pixel.real(); \
330  } \
331  } \
332  static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v) \
333  { \
334  if( i == 0 ) \
335  { \
336  pixel = TargetType( v, pixel.imag() ); \
337  } \
338  else \
339  { \
340  pixel = TargetType( pixel.real(), v ); \
341  } \
342  } \
343  static ComponentType GetScalarValue(const TargetType& pixel) \
344  { \
345  return std::norm(pixel); \
346  } \
347  }; \
348 
351 
352 #define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(type,componenttype) \
353 template<> \
354  class MeshConvertPixelTraits< type< componenttype> > \
355  { \
356  public: \
357  typedef type< componenttype > TargetType; \
358  typedef componenttype ComponentType; \
359  static unsigned int GetNumberOfComponents() \
360  { \
361  return 0; \
362  } \
363  static unsigned int GetNumberOfComponents(const TargetType& pixel) \
364  { \
365  return pixel.Size(); \
366  } \
367  static ComponentType GetNthComponent(int c, const TargetType& pixel) \
368  { \
369  return pixel[c]; \
370  } \
371  static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v) \
372  { \
373  pixel[i] = v; \
374  } \
375  static ComponentType GetScalarValue(const TargetType& pixel) \
376  { \
377  return pixel[0]; \
378  } \
379  }; \
380 
381 #define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
382  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, char); \
383  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, signed char); \
384  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned char); \
385  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, short); \
386  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned short); \
387  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, int); \
388  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned int); \
389  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long); \
390  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long); \
391  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, float); \
392  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, double);
393 
396 //
397 // End of Traits for the classes deriving from std::complex.
398 //
399 //
400 
401 } // end namespace itk
402 #endif
static unsigned int GetNumberOfComponents(const PixelType &)
Traits class used to by ConvertPixels to convert blocks of pixels.
ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(float)
#define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension)
ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(Array)
static ComponentType GetNthComponent(int c, const PixelType &pixel)
ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(Vector)
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(Matrix)
static ComponentType GetScalarValue(const PixelType &pixel)
static unsigned int GetNumberOfComponents()
PixelType::ComponentType ComponentType
#define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)