ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMeshConvertPixelTraits.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 
00019 #ifndef __itkMeshConvertPixelTraits_h
00020 #define __itkMeshConvertPixelTraits_h
00021 
00022 #include "itkArray.h"
00023 #include "itkMatrix.h"
00024 #include "itkOffset.h"
00025 #include "itkCovariantVector.h"
00026 #include "itkVariableLengthVector.h"
00027 #include "itkVector.h"
00028 #include "itkPoint.h"
00029 
00030 namespace itk
00031 {
00032 
00044 template<typename PixelType>
00045 class MeshConvertPixelTraits
00046 {
00047 public:
00049   typedef typename PixelType::ComponentType ComponentType;
00050 
00052   static unsigned int GetNumberOfComponents()
00053   { return PixelType::GetNumberOfComponents();}
00054 
00055   static unsigned int GetNumberOfComponents(const PixelType& )
00056   { return PixelType::GetNumberOfComponents();}
00057 
00059   static ComponentType GetNthComponent(int c, const PixelType& pixel)
00060   { return pixel.GetNthComponent(c); }
00061 
00063   static void SetNthComponent(int c, PixelType& pixel, const ComponentType& v)
00064   { pixel.SetNthComponent(c, v); }
00065 
00067   static ComponentType GetScalarValue(const PixelType& pixel)
00068   { return pixel.GetScalarValue(); }
00069 
00070 };
00071 
00072 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)                     \
00073 template<>                                                                \
00074   class MeshConvertPixelTraits<type>                                      \
00075   {                                                                       \
00076   public:                                                                 \
00077   typedef type ComponentType;                                             \
00078   static unsigned int GetNumberOfComponents()                             \
00079   {                                                                       \
00080   return 1;                                                               \
00081   }                                                                       \
00082   static unsigned int GetNumberOfComponents(const type& itkNotUsed(pixel))\
00083   {                                                                       \
00084   return 1;                                                               \
00085   }                                                                       \
00086   static ComponentType GetNthComponent(int itkNotUsed(c), const type& pixel)\
00087   {                                                                       \
00088   return pixel;                                                           \
00089   }                                                                       \
00090   static void SetNthComponent(int , type& pixel, const ComponentType& v)  \
00091   {                                                                       \
00092   pixel = v;                                                              \
00093   }                                                                       \
00094   static type GetScalarValue(const type& pixel)                           \
00095   {                                                                       \
00096   return pixel;                                                           \
00097   }                                                                       \
00098   };
00099 
00100   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(float)
00101   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(double)
00102   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(int)
00103   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
00104   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(short)
00105   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned int)
00106   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(signed char)
00107   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned char)
00108   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned short)
00109   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(long)
00110   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long)
00111   ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(bool)
00112 
00113 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
00114 
00115 //
00116 //  Default traits for the Offset<> pixel type
00117 //
00118 
00119 #define ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(dimension)                       \
00120   template<>                                                                       \
00121   class MeshConvertPixelTraits< Offset<dimension> >                                \
00122   {                                                                                \
00123   public:                                                                          \
00124   typedef Offset<dimension>  TargetType;                                           \
00125   typedef TargetType::OffsetValueType  ComponentType;                              \
00126   static unsigned int GetNumberOfComponents()                                      \
00127   {                                                                                \
00128   return dimension;                                                                \
00129   }                                                                                \
00130   static unsigned int GetNumberOfComponents(const TargetType& itkNotUsed(pixel))   \
00131   {                                                                                \
00132   return dimension;                                                                \
00133   }                                                                                \
00134   static ComponentType GetNthComponent(int c, const TargetType& pixel)             \
00135   {                                                                                \
00136   return pixel[c];                                                                 \
00137   }                                                                                \
00138   static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v)   \
00139   {                                                                                \
00140   pixel[i] = v;                                                                    \
00141   }                                                                                \
00142   static ComponentType GetScalarValue(const TargetType& pixel)                     \
00143   {                                                                                \
00144   return pixel[0];                                                                 \
00145   }                                                                                \
00146   };                                                                               \
00147 
00148 
00149 // Define traits for Offset<> from dimensions 1 to 5
00150   ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(1)
00151   ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(2)
00152   ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(3)
00153   ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(4)
00154   ITK_MESH_DEFAULTCONVERTTRAITS_OFFSET_TYPE(5)
00155 
00156 //
00157 //  Default traits for the pixel types deriving from FixedArray<>
00158 //
00159 
00160 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type,componenttype, dimension) \
00161   template<>                                                                         \
00162   class MeshConvertPixelTraits< type< componenttype, dimension> >                    \
00163   {                                                                                  \
00164   public:                                                                            \
00165   typedef type< componenttype, dimension >  TargetType;                              \
00166   typedef componenttype                     ComponentType;                           \
00167   static unsigned int GetNumberOfComponents()                                        \
00168   {                                                                                  \
00169   return dimension;                                                                  \
00170   }                                                                                  \
00171   static unsigned int GetNumberOfComponents(const TargetType& itkNotUsed(pixel))     \
00172   {                                                                                  \
00173   return dimension;                                                                  \
00174   }                                                                                  \
00175   static ComponentType GetNthComponent(int c, const TargetType& pixel)               \
00176   {                                                                                  \
00177   return pixel[c];                                                                   \
00178   }                                                                                  \
00179   static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v)     \
00180   {                                                                                  \
00181   pixel[i] = v;                                                                      \
00182   }                                                                                  \
00183   static ComponentType GetScalarValue(const TargetType& pixel)                       \
00184   {                                                                                  \
00185   return pixel[0];                                                                   \
00186   }                                                                                  \
00187   };                                                                                 \
00188 
00189 //
00190 //
00191 // Define traits for Classed deriving from FixedArray from dimensions 1 to 6
00192 // These classes include: Vector, CovariantVector and Point.
00193 //
00194 //
00195 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, Type)      \
00196   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,1)                     \
00197   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,2)                     \
00198   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,3)                     \
00199   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,4)                     \
00200   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,5)                     \
00201   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(ArrayType,Type,6)
00202 
00203 #define ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(ArrayType)      \
00204   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, char);           \
00205   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, signed char);    \
00206   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned char);  \
00207   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, short);          \
00208   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned short); \
00209   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, int);            \
00210   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned int);   \
00211   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, long);           \
00212   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, unsigned long);  \
00213   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, float);          \
00214   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_MACRO(ArrayType, double);
00215 
00216   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(Vector);
00217   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(CovariantVector);
00218   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(Point);
00219   ITK_MESH_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE_ALL_TYPES_MACRO(FixedArray);
00220 
00221 //
00222 //  End of Traits for the classes deriving from FixedArray.
00223 //
00224 //
00225 
00226 
00227 //
00228 //  Default traits for the pixel types deriving from Matrix<>
00229 //
00230 
00231 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(type,componenttype,rows,cols)    \
00232 template<>                                                                         \
00233   class MeshConvertPixelTraits< type< componenttype, rows, cols > >                \
00234   {                                                                                \
00235   public:                                                                          \
00236   typedef type< componenttype, rows, cols >  TargetType;                           \
00237   typedef componenttype                     ComponentType;                         \
00238   static unsigned int GetNumberOfComponents()                                      \
00239   {                                                                                \
00240   return rows * cols;                                                              \
00241   }                                                                                \
00242   static unsigned int GetNumberOfComponents(const TargetType& itkNotUsed(pixel))   \
00243   {                                                                                \
00244   return rows * cols;                                                              \
00245   }                                                                                \
00246   static ComponentType GetNthComponent(int c, const TargetType& pixel)             \
00247   {                                                                                \
00248   const unsigned int row = c / cols;                                               \
00249   const unsigned int col = c % cols;                                               \
00250   return pixel[row][col];                                                          \
00251   }                                                                                \
00252   static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v)   \
00253   {                                                                                \
00254   const unsigned int row = i / cols;                                               \
00255   const unsigned int col = i % cols;                                               \
00256   pixel[row][col] = v;                                                             \
00257   }                                                                                \
00258   static ComponentType GetScalarValue(const TargetType& pixel)                     \
00259   {                                                                                \
00260   return pixel[0][0];                                                              \
00261   }                                                                                \
00262   };                                                                               \
00263 
00264 //
00265 //
00266 // Define traits for Classed deriving from Matrix from dimensions 1 to 6
00267 //
00268 //
00269 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, Type)      \
00270   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,1,1)                   \
00271   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,2,2)                   \
00272   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,3,3)                   \
00273   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,4,4)                   \
00274   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,5,5)                   \
00275   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE(ArrayType,Type,6,6)
00276 
00277 #define ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(ArrayType)      \
00278   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, char);           \
00279   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, signed char);    \
00280   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned char);  \
00281   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, short);          \
00282   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned short); \
00283   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, int);            \
00284   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned int);   \
00285   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, long);           \
00286   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, unsigned long);  \
00287   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, float);          \
00288   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_MACRO(ArrayType, double);
00289 
00290 //
00291 // Add here other classes that derive from Matrix or that have the same API
00292 //
00293   ITK_MESH_DEFAULTCONVERTTRAITS_MATRIX_TYPE_ALL_TYPES_MACRO(Matrix);
00294 
00295 //
00296 //  End of Traits for the classes deriving from Matrix.
00297 //
00298 //
00299 
00300 
00301 //
00302 //  Default traits for the pixel types deriving from std::complex<>
00303 //
00304 
00305 #define ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE( componenttype )                \
00306 template<>                                                                         \
00307   class MeshConvertPixelTraits< ::std::complex< componenttype > >                  \
00308   {                                                                                \
00309   public:                                                                          \
00310   typedef ::std::complex< componenttype>  TargetType;                              \
00311   typedef componenttype                     ComponentType;                         \
00312   static unsigned int GetNumberOfComponents()                                      \
00313   {                                                                                \
00314   return 2;                                                                        \
00315   }                                                                                \
00316   static unsigned int GetNumberOfComponents(const TargetType & itkNotUsed(pixel))  \
00317   {                                                                                \
00318   return 2;                                                                        \
00319   }                                                                                \
00320   static ComponentType GetNthComponent(int i, TargetType & pixel)                  \
00321   {                                                                                \
00322   if( i == 0 )                                                                     \
00323     {                                                                              \
00324     return pixel.imag();                                                           \
00325     }                                                                              \
00326   else                                                                             \
00327     {                                                                              \
00328     return pixel.real();                                                           \
00329     }                                                                              \
00330   }                                                                                \
00331   static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v)   \
00332   {                                                                                \
00333   if( i == 0 )                                                                     \
00334     {                                                                              \
00335     pixel = TargetType( v, pixel.imag() );                                         \
00336     }                                                                              \
00337   else                                                                             \
00338     {                                                                              \
00339     pixel = TargetType( pixel.real(), v );                                         \
00340     }                                                                              \
00341   }                                                                                \
00342   static ComponentType GetScalarValue(const TargetType& pixel)                     \
00343   {                                                                                \
00344   return std::norm(pixel);                                                         \
00345   }                                                                                \
00346   };                                                                               \
00347 
00348   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(float);
00349   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(double);
00350   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(signed int);
00351   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(unsigned int);
00352   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(signed char);
00353   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(unsigned char);
00354   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(signed long);
00355   ITK_MESH_DEFAULTCONVERTTRAITS_COMPLEX_TYPE(unsigned long);
00356 
00357 #define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(type,componenttype)               \
00358 template<>                                                                         \
00359   class MeshConvertPixelTraits< type< componenttype> >                             \
00360   {                                                                                \
00361   public:                                                                          \
00362   typedef type< componenttype >             TargetType;                            \
00363   typedef componenttype                     ComponentType;                         \
00364   static unsigned int GetNumberOfComponents()                                      \
00365   {                                                                                \
00366   return 0;                                                                        \
00367   }                                                                                \
00368   static unsigned int GetNumberOfComponents(const TargetType& pixel)               \
00369   {                                                                                \
00370   return pixel.Size();                                                             \
00371   }                                                                                \
00372   static ComponentType GetNthComponent(int c, const TargetType& pixel)             \
00373   {                                                                                \
00374   return pixel[c];                                                                 \
00375   }                                                                                \
00376   static void SetNthComponent(int i, TargetType & pixel, const ComponentType& v)   \
00377   {                                                                                \
00378   pixel[i] = v;                                                                    \
00379   }                                                                                \
00380   static ComponentType GetScalarValue(const TargetType& pixel)                     \
00381   {                                                                                \
00382   return pixel[0];                                                                 \
00383   }                                                                                \
00384   };                                                                               \
00385 
00386 #define ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(ArrayType)        \
00387   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, char);                       \
00388   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, signed char);                \
00389   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned char);              \
00390   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, short);                      \
00391   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned short);             \
00392   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, int);                        \
00393   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned int);               \
00394   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, long);                       \
00395   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, unsigned long);              \
00396   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, float);                      \
00397   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE(ArrayType, double);
00398 
00399   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(Array);
00400   ITK_MESH_DEFAULTCONVERTTRAITS_ARRAY_TYPE_ALL_TYPES_MACRO(VariableLengthVector);
00401 //
00402 //  End of Traits for the classes deriving from std::complex.
00403 //
00404 //
00405 
00406 } // end namespace itk
00407 #endif
00408