ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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:
50 using ComponentType = typename PixelType::ComponentType;
51
53 static unsigned int
55 {
56 return PixelType::GetNumberOfComponents();
57 }
58
59 static unsigned int
60 GetNumberOfComponents(const PixelType &)
61 {
62 return PixelType::GetNumberOfComponents();
63 }
64
66 static ComponentType
67 GetNthComponent(int c, const PixelType & pixel)
68 {
69 return pixel.GetNthComponent(c);
70 }
71
73 static void
74 SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
75 {
76 pixel.SetNthComponent(c, v);
77 }
78
80 static ComponentType
81 GetScalarValue(const PixelType & pixel)
82 {
83 return pixel.GetScalarValue();
84 }
85};
86
87#define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
88 template <> \
89 class MeshConvertPixelTraits<type> \
90 { \
91 public: \
92 using ComponentType = type; \
93 static unsigned int \
94 GetNumberOfComponents() \
95 { \
96 return 1; \
97 } \
98 static unsigned int \
99 GetNumberOfComponents(const type & itkNotUsed(pixel)) \
100 { \
101 return 1; \
102 } \
103 static ComponentType \
104 GetNthComponent(int itkNotUsed(c), const type & pixel) \
105 { \
106 return pixel; \
107 } \
108 static void \
109 SetNthComponent(int, type & pixel, const ComponentType & v) \
110 { \
111 pixel = v; \
112 } \
113 static type \
114 GetScalarValue(const type & pixel) \
115 { \
116 return pixel; \
117 } \
118 }
119
134
135#undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
136
137//
138// Default traits for the Offset<> pixel type
139//
140
141#define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension) \
142 template <> \
143 class MeshConvertPixelTraits<Offset<dimension>> \
144 { \
145 public: \
146 using TargetType = Offset<dimension>; \
147 using ComponentType = TargetType::OffsetValueType; \
148 static unsigned int \
149 GetNumberOfComponents() \
150 { \
151 return dimension; \
152 } \
153 static unsigned int \
154 GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
155 { \
156 return dimension; \
157 } \
158 static ComponentType \
159 GetNthComponent(int c, const TargetType & pixel) \
160 { \
161 return pixel[c]; \
162 } \
163 static void \
164 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
165 { \
166 pixel[i] = v; \
167 } \
168 static ComponentType \
169 GetScalarValue(const TargetType & pixel) \
170 { \
171 return pixel[0]; \
172 } \
173 }
174
175
176// Define traits for Offset<> from dimensions 1 to 5
182
183//
184// Default traits for the pixel types deriving from FixedArray<>
185//
186
187#define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type, componenttype, dimension) \
188 template <> \
189 class MeshConvertPixelTraits<type<componenttype, dimension>> \
190 { \
191 public: \
192 using TargetType = type<componenttype, dimension>; \
193 using ComponentType = componenttype; \
194 static unsigned int \
195 GetNumberOfComponents() \
196 { \
197 return dimension; \
198 } \
199 static unsigned int \
200 GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
201 { \
202 return dimension; \
203 } \
204 static ComponentType \
205 GetNthComponent(int c, const TargetType & pixel) \
206 { \
207 return pixel[c]; \
208 } \
209 static void \
210 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
211 { \
212 pixel[i] = v; \
213 } \
214 static ComponentType \
215 GetScalarValue(const TargetType & pixel) \
216 { \
217 return pixel[0]; \
218 } \
219 }
220
221//
222//
223// Define traits for Classed deriving from FixedArray from dimensions 1 to 6
224// These classes include: Vector, CovariantVector and Point.
225//
226//
227#define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, Type) \
228 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 1); \
229 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 2); \
230 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 3); \
231 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 4); \
232 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 5); \
233 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType, Type, 6); \
234 ITK_MACROEND_NOOP_STATEMENT
235
236#define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
237 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, char); \
238 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, signed char); \
239 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned char); \
240 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, short); \
241 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned short); \
242 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, int); \
243 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned int); \
244 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long); \
245 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long); \
246 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long long); \
247 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long long); \
248 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, float); \
249 ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, double); \
250 ITK_MACROEND_NOOP_STATEMENT
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 ITK_MACROEND_NOOP_STATEMENT
318
319#define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(ArrayType) \
320 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, char); \
321 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, signed char); \
322 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned char); \
323 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, short); \
324 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned short); \
325 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, int); \
326 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned int); \
327 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long); \
328 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long); \
329 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long long); \
330 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long long); \
331 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, float); \
332 ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, double); \
333 ITK_MACROEND_NOOP_STATEMENT
334
335//
336// Add here other classes that derive from Matrix or that have the same API
337//
339
340//
341// End of Traits for the classes deriving from Matrix.
342//
343//
344
345
346//
347// Default traits for the pixel types deriving from std::complex<>
348//
349
350#define ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(componenttype) \
351 template <> \
352 class MeshConvertPixelTraits<std::complex<componenttype>> \
353 { \
354 public: \
355 using TargetType = std::complex<componenttype>; \
356 using ComponentType = componenttype; \
357 static unsigned int \
358 GetNumberOfComponents() \
359 { \
360 return 2; \
361 } \
362 static unsigned int \
363 GetNumberOfComponents(const TargetType & itkNotUsed(pixel)) \
364 { \
365 return 2; \
366 } \
367 static ComponentType \
368 GetNthComponent(int i, TargetType & pixel) \
369 { \
370 if (i == 0) \
371 { \
372 return pixel.imag(); \
373 } \
374 else \
375 { \
376 return pixel.real(); \
377 } \
378 } \
379 static void \
380 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
381 { \
382 if (i == 0) \
383 { \
384 pixel = TargetType(v, pixel.imag()); \
385 } \
386 else \
387 { \
388 pixel = TargetType(pixel.real(), v); \
389 } \
390 } \
391 static ComponentType \
392 GetScalarValue(const TargetType & pixel) \
393 { \
394 return std::norm(pixel); \
395 } \
396 }
397
400
401#define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(type, componenttype) \
402 template <> \
403 class MeshConvertPixelTraits<type<componenttype>> \
404 { \
405 public: \
406 using TargetType = type<componenttype>; \
407 using ComponentType = componenttype; \
408 static unsigned int \
409 GetNumberOfComponents() \
410 { \
411 return 0; \
412 } \
413 static unsigned int \
414 GetNumberOfComponents(const TargetType & pixel) \
415 { \
416 return pixel.Size(); \
417 } \
418 static ComponentType \
419 GetNthComponent(int c, const TargetType & pixel) \
420 { \
421 return pixel[c]; \
422 } \
423 static void \
424 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
425 { \
426 pixel[i] = v; \
427 } \
428 static ComponentType \
429 GetScalarValue(const TargetType & pixel) \
430 { \
431 return pixel[0]; \
432 } \
433 }
434
435#define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(ArrayType) \
436 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, char); \
437 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, signed char); \
438 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned char); \
439 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, short); \
440 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned short); \
441 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, int); \
442 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned int); \
443 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long); \
444 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long); \
445 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long long); \
446 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long long); \
447 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, float); \
448 ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, double); \
449 ITK_MACROEND_NOOP_STATEMENT
450
453//
454// End of Traits for the classes deriving from std::complex.
455//
456//
457
458} // end namespace itk
459#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.
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
#define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)
#define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension)
#define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(ArrayType)
#define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(ArrayType)
#define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(ArrayType)
#define ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(componenttype)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....