cmtkDataTypeTraits.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkDataTypeTraits_h_included_
00034 #define __cmtkDataTypeTraits_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkTypes.h>
00039 #include <Base/cmtkMathUtil.h>
00040 
00041 #include <math.h>
00042 #include <stdlib.h>
00043 #include <limits>
00044 
00045 namespace
00046 cmtk
00047 {
00048 
00051 
00053 template<class TType>
00054 class DataTypeTraits
00055 {
00056 public:
00058   static ScalarDataType GetScalarDataType() { return TYPE_NONE; }
00059 
00060   static const ScalarDataType DataTypeID = TYPE_NONE;
00061 };
00062 
00064 template<>
00065 class DataTypeTraits<float>
00066 {
00067 public:
00069   typedef DataTypeTraits<float> Self;
00070 
00072   static ScalarDataType GetScalarDataType() { return TYPE_FLOAT; }
00073 
00074   static const ScalarDataType DataTypeID = TYPE_FLOAT;
00075 
00077   static inline float Abs( const float value ) 
00078   {
00079     return fabsf( value );
00080   }
00081 
00083   template<class T>
00084   static inline float Convert ( const T value, const bool paddingFlag = false, const float paddingData = 0 ) 
00085   { 
00086     using namespace std;
00087     if ( MathUtil::IsNaN( value ) )
00088       if ( paddingFlag )
00089         return paddingData;
00090       else
00091         return ChoosePaddingValue();
00092     else
00093       return (float) value;
00094   }
00095 
00097   static inline float ChoosePaddingValue () 
00098   { 
00099     return std::numeric_limits<float>::infinity();
00100   }
00101 };
00102 
00104 template<>
00105 class DataTypeTraits<double>
00106 {
00107 public:
00109   typedef DataTypeTraits<double> Self;
00110 
00112   static ScalarDataType GetScalarDataType() { return TYPE_DOUBLE; }
00113 
00114   static const ScalarDataType DataTypeID = TYPE_DOUBLE;
00115 
00117   static inline double Abs( const double value ) 
00118   {
00119     return fabs( value );
00120   }
00121 
00123   template<class T>
00124   static inline double Convert ( const T value, const bool paddingFlag = false, const double paddingData = 0 ) 
00125   { 
00126     using namespace std;
00127     if ( MathUtil::IsNaN( value ) )
00128       if ( paddingFlag )
00129         return paddingData;
00130       else
00131         return ChoosePaddingValue();
00132     else
00133       return (double) value;
00134   }
00135   
00137   static inline double ChoosePaddingValue () 
00138   { 
00139     return std::numeric_limits<double>::infinity();
00140   }
00141 };
00142 
00144 template<>
00145 class DataTypeTraits<byte>
00146 {
00147 public:
00149   typedef DataTypeTraits<byte> Self;
00150 
00152   static ScalarDataType GetScalarDataType() { return TYPE_BYTE; }
00153 
00154   static const ScalarDataType DataTypeID = TYPE_BYTE;
00155 
00157   static const Types::DataItem Min;
00158 
00160   static const Types::DataItem Max;
00161 
00163   static inline byte Abs( const byte value ) 
00164   {
00165     return abs( value );
00166   }
00167 
00169   template<class T>
00170   static inline byte Convert ( const T value, const bool paddingFlag = false, const byte paddingData = 0 ) 
00171   { 
00172     using namespace std;
00173     if ( MathUtil::IsNaN( value ) )
00174       if ( paddingFlag )
00175         return paddingData;
00176       else
00177         return ChoosePaddingValue();
00178     else
00179       return (byte) ((value<Self::Min) ? Self::Min : (value+0.5>Self::Max) ? Self::Max : floor(value+0.5));
00180   }
00181   
00183   static inline byte ChoosePaddingValue () 
00184   { 
00185     return 255;
00186   }
00187 };
00188 
00190 template<>
00191 class DataTypeTraits<char>
00192 {
00193 public:
00195   typedef DataTypeTraits<char> Self;
00196 
00198   static ScalarDataType GetScalarDataType() { return TYPE_CHAR; }
00199 
00200   static const ScalarDataType DataTypeID = TYPE_CHAR;
00201 
00203   static const Types::DataItem Min;
00204 
00206   static const Types::DataItem Max;
00207 
00209   static inline char Abs( const char value ) 
00210   {
00211     return abs( value );
00212   }
00213 
00215   template<class T>
00216   static inline char Convert ( const T value, const bool paddingFlag = false, const char paddingData = 0 ) 
00217   { 
00218     using namespace std;
00219     if ( MathUtil::IsNaN( value ) )
00220       if ( paddingFlag )
00221         return paddingData;
00222       else
00223         return ChoosePaddingValue();
00224     else
00225       return (char) ((value<Self::Min) ? Self::Min : (value+0.5>Self::Max) ? Self::Max : floor(value+0.5));
00226   }
00227   
00229   static inline char ChoosePaddingValue () 
00230   { 
00231     return -1;
00232   }
00233 };
00234 
00236 template<>
00237 class DataTypeTraits<signed short>
00238 {
00239 public:
00241   typedef DataTypeTraits<signed short> Self;
00242 
00244   static ScalarDataType GetScalarDataType() { return TYPE_SHORT; }
00245 
00246   static const ScalarDataType DataTypeID = TYPE_SHORT;
00247 
00249   static const Types::DataItem Min;
00250 
00252   static const Types::DataItem Max;
00253 
00255   static inline signed short Abs( const signed short value ) 
00256   {
00257     return abs( value );
00258   }
00259 
00261   template<class T>
00262   static inline signed short Convert ( const T value, const bool paddingFlag = false, const signed short paddingData = 0 ) 
00263   { 
00264     using namespace std;
00265     if ( MathUtil::IsNaN( value ) )
00266       if ( paddingFlag )
00267         return paddingData;
00268       else
00269         return ChoosePaddingValue();
00270     else
00271       return (signed short) (((value<Self::Min) ? Self::Min : (value+0.5>Self::Max) ? Self::Max : floor(value+0.5)));
00272   }
00273   
00275   static inline signed short ChoosePaddingValue () 
00276   { 
00277     return -1;
00278   }
00279 };
00280 
00282 template<>
00283 class DataTypeTraits<unsigned short>
00284 {
00285 public:
00287   typedef DataTypeTraits<unsigned short> Self;
00288 
00290   static ScalarDataType GetScalarDataType() { return TYPE_USHORT; }
00291 
00292   static const ScalarDataType DataTypeID = TYPE_USHORT;
00293 
00295   static const Types::DataItem Min;
00296 
00298   static const Types::DataItem Max;
00299 
00301   static inline unsigned short Abs( const unsigned short value ) 
00302   {
00303     return abs( value );
00304   }
00305 
00307   template<class T>
00308   static inline unsigned short Convert ( const T value, const bool paddingFlag = false, const unsigned short paddingData = 0 ) 
00309   { 
00310     using namespace std;
00311     if ( MathUtil::IsNaN( value ) )
00312       if ( paddingFlag )
00313         return paddingData;
00314       else
00315         return ChoosePaddingValue();
00316     else
00317       return (unsigned short) ((value<Self::Min) ? Self::Min : (value+0.5>Self::Max) ? Self::Max : floor(value+0.5));
00318   }
00319   
00321   static inline unsigned short ChoosePaddingValue () 
00322   { 
00323     return USHRT_MAX;
00324   }
00325 };
00326 
00328 template<>
00329 class DataTypeTraits<int>
00330 {
00331 public:
00333   typedef DataTypeTraits<int> Self;
00334 
00336   static ScalarDataType GetScalarDataType() { return TYPE_INT; }
00337 
00338   static const ScalarDataType DataTypeID = TYPE_INT;
00339 
00341   static const Types::DataItem Min;
00342 
00344   static const Types::DataItem Max;
00345 
00347   static inline int Abs( const int value ) 
00348   {
00349     return abs( value );
00350   }
00351 
00353   template<class T>
00354   static inline int Convert ( const T value, const bool paddingFlag = false, const int paddingData = 0 ) 
00355   { 
00356     using namespace std;
00357     if ( MathUtil::IsNaN( value ) )
00358       if ( paddingFlag )
00359         return paddingData;
00360       else
00361         return ChoosePaddingValue();
00362     else
00363       return (int) ((value<Self::Min) ? Self::Min : (value+0.5>Self::Max) ? Self::Max : floor(value+0.5));
00364   }
00365   
00367   static inline int ChoosePaddingValue () 
00368   { 
00369     return -1;
00370   }
00371 };
00372 
00374 
00375 } // namespace cmtk
00376 
00377 #endif // #ifndef __cmtkDataTypeTraits_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines