ITK  6.0.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 * https://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
30namespace itk
31{
32
45template <typename PixelType>
47{
48public:
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};
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
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 ITK_MACROEND_NOOP_STATEMENT
237
238#define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
239 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, char); \
240 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, signed char); \
241 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned char); \
242 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, short); \
243 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned short); \
244 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, int); \
245 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned int); \
246 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long); \
247 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long); \
248 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long long); \
249 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long long); \
250 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, float); \
251 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, double); \
252 ITK_MACROEND_NOOP_STATEMENT
253
258
259//
260// End of Traits for the classes deriving from FixedArray.
261//
262//
263
264
265//
266// Default traits for the pixel types deriving from Matrix<>
267//
268
269#define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(type, componenttype, rows, cols) \
270 template <> \
271 class MeshConvertPixelTraits<type<componenttype, rows, cols>> \
272 { \
273 public: \
274 using TargetType = type<componenttype, rows, cols>; \
275 using ComponentType = componenttype; \
276 static unsigned int \
277 GetNumberOfComponents() \
278 { \
279 return rows * cols; \
280 } \
281 static unsigned int \
282 GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
283 { \
284 return rows * cols; \
285 } \
286 static ComponentType \
287 GetNthComponent(int c, const TargetType & pixel) \
288 { \
289 const unsigned int row = c / cols; \
290 const unsigned int col = c % cols; \
291 return pixel[row][col]; \
292 } \
293 static void \
294 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
295 { \
296 const unsigned int row = i / cols; \
297 const unsigned int col = i % cols; \
298 pixel[row][col] = v; \
299 } \
300 static ComponentType \
301 GetScalarValue(const TargetType & pixel) \
302 { \
303 return pixel[0][0]; \
304 } \
305 }
306
307//
308//
309// Define traits for Classed deriving from Matrix from dimensions 1 to 6
310//
311//
312#define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, Type) \
313 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 1, 1); \
314 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 2, 2); \
315 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 3, 3); \
316 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 4, 4); \
317 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 5, 5); \
318 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType, Type, 6, 6); \
319 ITK_MACROEND_NOOP_STATEMENT
320
321#define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(ArrayType) \
322 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, char); \
323 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, signed char); \
324 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned char); \
325 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, short); \
326 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned short); \
327 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, int); \
328 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned int); \
329 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long); \
330 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long); \
331 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long long); \
332 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long long); \
333 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, float); \
334 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, double); \
335 ITK_MACROEND_NOOP_STATEMENT
336
337//
338// Add here other classes that derive from Matrix or that have the same API
339//
341
342//
343// End of Traits for the classes deriving from Matrix.
344//
345//
346
347
348//
349// Default traits for the pixel types deriving from std::complex<>
350//
351
352#define ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(componenttype) \
353 template <> \
354 class MeshConvertPixelTraits<std::complex<componenttype>> \
355 { \
356 public: \
357 using TargetType = std::complex<componenttype>; \
358 using ComponentType = componenttype; \
359 static unsigned int \
360 GetNumberOfComponents() \
361 { \
362 return 2; \
363 } \
364 static unsigned int \
365 GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
366 { \
367 return 2; \
368 } \
369 static ComponentType \
370 GetNthComponent(int i, TargetType & pixel) \
371 { \
372 if (i == 0) \
373 { \
374 return pixel.imag(); \
375 } \
376 else \
377 { \
378 return pixel.real(); \
379 } \
380 } \
381 static void \
382 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
383 { \
384 if (i == 0) \
385 { \
386 pixel = TargetType(v, pixel.imag()); \
387 } \
388 else \
389 { \
390 pixel = TargetType(pixel.real(), v); \
391 } \
392 } \
393 static ComponentType \
394 GetScalarValue(const TargetType & pixel) \
395 { \
396 return std::norm(pixel); \
397 } \
398 }
399
402
403#define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(type, componenttype) \
404 template <> \
405 class MeshConvertPixelTraits<type<componenttype>> \
406 { \
407 public: \
408 using TargetType = type<componenttype>; \
409 using ComponentType = componenttype; \
410 static unsigned int \
411 GetNumberOfComponents() \
412 { \
413 return 0; \
414 } \
415 static unsigned int \
416 GetNumberOfComponents(const TargetType & pixel) \
417 { \
418 return pixel.Size(); \
419 } \
420 static ComponentType \
421 GetNthComponent(int c, const TargetType & pixel) \
422 { \
423 return pixel[c]; \
424 } \
425 static void \
426 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
427 { \
428 pixel[i] = v; \
429 } \
430 static ComponentType \
431 GetScalarValue(const TargetType & pixel) \
432 { \
433 return pixel[0]; \
434 } \
435 }
436
437#define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
438 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, char); \
439 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, signed char); \
440 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned char); \
441 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, short); \
442 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned short); \
443 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, int); \
444 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned int); \
445 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long); \
446 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long); \
447 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long long); \
448 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long long); \
449 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, float); \
450 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, double); \
451 ITK_MACROEND_NOOP_STATEMENT
452
455//
456// End of Traits for the classes deriving from std::complex.
457//
458//
459
460} // end namespace itk
461#endif
Array class with size defined at construction time.
Definition: itkArray.h:48
A templated class holding a n-Dimensional covariant vector.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
Traits class used to by ConvertPixels to convert blocks of pixels.
static unsigned int GetNumberOfComponents(const PixelType &)
static unsigned int GetNumberOfComponents()
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
static ComponentType GetNthComponent(int c, const PixelType &pixel)
typename PixelType::ComponentType ComponentType
static ComponentType GetScalarValue(const PixelType &pixel)
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:54
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:63
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(1)
ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(Array)
ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(Matrix)
ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(float)
ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(Vector)