Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 }
00376
00377 #endif // #ifndef __cmtkDataTypeTraits_h_included_