ITK  5.2.0
Insight Toolkit
itkMeshConvertPixelTraits.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 
22 #include "itkArray.h"
23 #include "itkMatrix.h"
24 #include "itkOffset.h"
25 #include "itkCovariantVector.h"
27 #include "itkVector.h"
28 #include "itkPoint.h"
29 
30 namespace itk
31 {
32 
45 template <typename PixelType>
47 {
48 public:
49 
51  using ComponentType = typename PixelType::ComponentType;
52 
54  static unsigned int
56  {
57  return PixelType::GetNumberOfComponents();
58  }
59 
60  static unsigned int
61  GetNumberOfComponents(const PixelType &)
62  {
63  return PixelType::GetNumberOfComponents();
64  }
65 
67  static ComponentType
68  GetNthComponent(int c, const PixelType & pixel)
69  {
70  return pixel.GetNthComponent(c);
71  }
72 
74  static void
75  SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
76  {
77  pixel.SetNthComponent(c, v);
78  }
79 
81  static ComponentType
82  GetScalarValue(const PixelType & pixel)
83  {
84  return pixel.GetScalarValue();
85  }
86 };
88 
89 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
90  template <> \
91  class MeshConvertPixelTraits<type> \
92  { \
93  public: \
94  using ComponentType = type; \
95  static unsigned int \
96  GetNumberOfComponents() \
97  { \
98  return 1; \
99  } \
100  static unsigned int \
101  GetNumberOfComponents(const type & itkNotUsed(pixel)) \
102  { \
103  return 1; \
104  } \
105  static ComponentType \
106  GetNthComponent(int itkNotUsed(c), const type & pixel) \
107  { \
108  return pixel; \
109  } \
110  static void \
111  SetNthComponent(int, type & pixel, const ComponentType & v) \
112  { \
113  pixel = v; \
114  } \
115  static type \
116  GetScalarValue(const type & pixel) \
117  { \
118  return pixel; \
119  } \
120  }
121 
126 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned short int);
130 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long int);
132 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long long int);
136 
137 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
138 
139 //
140 // Default traits for the Offset<> pixel type
141 //
142 
143 #define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension) \
144  template <> \
145  class MeshConvertPixelTraits<Offset<dimension>> \
146  { \
147  public: \
148  using TargetType = Offset<dimension>; \
149  using ComponentType = TargetType::OffsetValueType; \
150  static unsigned int \
151  GetNumberOfComponents() \
152  { \
153  return dimension; \
154  } \
155  static unsigned int \
156  GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
157  { \
158  return dimension; \
159  } \
160  static ComponentType \
161  GetNthComponent(int c, const TargetType & pixel) \
162  { \
163  return pixel[c]; \
164  } \
165  static void \
166  SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
167  { \
168  pixel[i] = v; \
169  } \
170  static ComponentType \
171  GetScalarValue(const TargetType & pixel) \
172  { \
173  return pixel[0]; \
174  } \
175  };
176 
177 
178 // Define traits for Offset<> from dimensions 1 to 5
184 
185 //
186 // Default traits for the pixel types deriving from FixedArray<>
187 //
188 
189 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type, componenttype, dimension) \
190  template <> \
191  class MeshConvertPixelTraits<type<componenttype, dimension>> \
192  { \
193  public: \
194  using TargetType = type<componenttype, dimension>; \
195  using ComponentType = componenttype; \
196  static unsigned int \
197  GetNumberOfComponents() \
198  { \
199  return dimension; \
200  } \
201  static unsigned int \
202  GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
203  { \
204  return dimension; \
205  } \
206  static ComponentType \
207  GetNthComponent(int c, const TargetType & pixel) \
208  { \
209  return pixel[c]; \
210  } \
211  static void \
212  SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
213  { \
214  pixel[i] = v; \
215  } \
216  static ComponentType \
217  GetScalarValue(const TargetType & pixel) \
218  { \
219  return pixel[0]; \
220  } \
221  };
222 
223 //
224 //
225 // Define traits for Classed deriving from FixedArray from dimensions 1 to 6
226 // These classes include: Vector, CovariantVector and Point.
227 //
228 //
229 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, Type) \
230  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 1) \
231  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 2) \
232  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 3) \
233  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 4) \
234  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 5) \
235  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 6)
236 
237 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
238  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, char); \
239  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, signed char); \
240  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned char); \
241  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, short int); \
242  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned short int); \
243  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, int); \
244  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned int); \
245  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long int); \
246  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long int); \
247  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long long int); \
248  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long long int); \
249  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, float); \
250  ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, double);
251 
256 
257 //
258 // End of Traits for the classes deriving from FixedArray.
259 //
260 //
261 
262 
263 //
264 // Default traits for the pixel types deriving from Matrix<>
265 //
266 
267 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(type, componenttype, rows, cols) \
268  template <> \
269  class MeshConvertPixelTraits<type<componenttype, rows, cols>> \
270  { \
271  public: \
272  using TargetType = type<componenttype, rows, cols>; \
273  using ComponentType = componenttype; \
274  static unsigned int \
275  GetNumberOfComponents() \
276  { \
277  return rows * cols; \
278  } \
279  static unsigned int \
280  GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
281  { \
282  return rows * cols; \
283  } \
284  static ComponentType \
285  GetNthComponent(int c, const TargetType & pixel) \
286  { \
287  const unsigned int row = c / cols; \
288  const unsigned int col = c % cols; \
289  return pixel[row][col]; \
290  } \
291  static void \
292  SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
293  { \
294  const unsigned int row = i / cols; \
295  const unsigned int col = i % cols; \
296  pixel[row][col] = v; \
297  } \
298  static ComponentType \
299  GetScalarValue(const TargetType & pixel) \
300  { \
301  return pixel[0][0]; \
302  } \
303  };
304 
305 //
306 //
307 // Define traits for Classed deriving from Matrix from dimensions 1 to 6
308 //
309 //
310 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, Type) \
311  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 1, 1) \
312  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 2, 2) \
313  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 3, 3) \
314  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 4, 4) \
315  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 5, 5) \
316  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 6, 6)
317 
318 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(ArrayType) \
319  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, char); \
320  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, signed char); \
321  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned char); \
322  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, short int); \
323  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned short int); \
324  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, int); \
325  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned int); \
326  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long int); \
327  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long int); \
328  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long long int); \
329  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long long int); \
330  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, float); \
331  ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, double);
332 
333 //
334 // Add here other classes that derive from Matrix or that have the same API
335 //
337 
338 //
339 // End of Traits for the classes deriving from Matrix.
340 //
341 //
342 
343 
344 //
345 // Default traits for the pixel types deriving from std::complex<>
346 //
347 
348 #define ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(componenttype) \
349  template <> \
350  class MeshConvertPixelTraits<::std::complex<componenttype>> \
351  { \
352  public: \
353  using TargetType = ::std::complex<componenttype>; \
354  using ComponentType = componenttype; \
355  static unsigned int \
356  GetNumberOfComponents() \
357  { \
358  return 2; \
359  } \
360  static unsigned int \
361  GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
362  { \
363  return 2; \
364  } \
365  static ComponentType \
366  GetNthComponent(int i, TargetType & pixel) \
367  { \
368  if (i == 0) \
369  { \
370  return pixel.imag(); \
371  } \
372  else \
373  { \
374  return pixel.real(); \
375  } \
376  } \
377  static void \
378  SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
379  { \
380  if (i == 0) \
381  { \
382  pixel = TargetType(v, pixel.imag()); \
383  } \
384  else \
385  { \
386  pixel = TargetType(pixel.real(), v); \
387  } \
388  } \
389  static ComponentType \
390  GetScalarValue(const TargetType & pixel) \
391  { \
392  return std::norm(pixel); \
393  } \
394  };
395 
398 
399 #define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(type, componenttype) \
400  template <> \
401  class MeshConvertPixelTraits<type<componenttype>> \
402  { \
403  public: \
404  using TargetType = type<componenttype>; \
405  using ComponentType = componenttype; \
406  static unsigned int \
407  GetNumberOfComponents() \
408  { \
409  return 0; \
410  } \
411  static unsigned int \
412  GetNumberOfComponents(const TargetType & pixel) \
413  { \
414  return pixel.Size(); \
415  } \
416  static ComponentType \
417  GetNthComponent(int c, const TargetType & pixel) \
418  { \
419  return pixel[c]; \
420  } \
421  static void \
422  SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
423  { \
424  pixel[i] = v; \
425  } \
426  static ComponentType \
427  GetScalarValue(const TargetType & pixel) \
428  { \
429  return pixel[0]; \
430  } \
431  };
432 
433 #define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
434  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, char); \
435  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, signed char); \
436  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned char); \
437  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, short int); \
438  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned short int); \
439  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, int); \
440  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned int); \
441  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long int); \
442  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long int); \
443  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long long int); \
444  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long long int); \
445  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, float); \
446  ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, double);
447 
450 //
451 // End of Traits for the classes deriving from std::complex.
452 //
453 //
454 
455 } // end namespace itk
456 #endif
itk::MeshConvertPixelTraits::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkMeshConvertPixelTraits.h:55
itk::MeshConvertPixelTraits::GetScalarValue
static ComponentType GetScalarValue(const PixelType &pixel)
Definition: itkMeshConvertPixelTraits.h:82
itkCovariantVector.h
itkMatrix.h
itkOffset.h
itkPoint.h
itkVariableLengthVector.h
itk::MeshConvertPixelTraits::GetNumberOfComponents
static unsigned int GetNumberOfComponents(const PixelType &)
Definition: itkMeshConvertPixelTraits.h:61
itk::ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO
ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(Array)
ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE
#define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension)
Definition: itkMeshConvertPixelTraits.h:143
itk::MeshConvertPixelTraits::ComponentType
typename PixelType::ComponentType ComponentType
Definition: itkMeshConvertPixelTraits.h:51
itk::MeshConvertPixelTraits
Traits class used to by ConvertPixels to convert blocks of pixels.
Definition: itkMeshConvertPixelTraits.h:46
itk::ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE
ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(float)
itk::ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO
ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(Vector)
itk::ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO
ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(Matrix)
itkArray.h
itk::MeshConvertPixelTraits::SetNthComponent
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
Definition: itkMeshConvertPixelTraits.h:75
itk::ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itkVector.h
itk::MeshConvertPixelTraits::GetNthComponent
static ComponentType GetNthComponent(int c, const PixelType &pixel)
Definition: itkMeshConvertPixelTraits.h:68