MeVisLab Toolbox Reference
mlTypeDefTraits.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_TYPE_DEF_TRAITS_H
14 #define ML_TYPE_DEF_TRAITS_H
15 
17 
19 #include "mlTypeDefs.h"
20 
21 #include <ThirdPartyWarningsDisable.h>
22 
23 #if defined(WIN32)
24 # include <float.h>
25 # define __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION(__ml_win_func,__ml_unix_func) __ml_win_func
26 #else
27 # include <cmath>
28 # define __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION(__ml_win_func,__ml_unix_func) __ml_unix_func
29 #endif
30 
31 #include <limits>
32 #include <boost/static_assert.hpp>
33 
34 #include <ThirdPartyWarningsRestore.h>
35 
39  inline MLint8 MLTypeRangeMax(MLint8 ) { return ML_INT8_MAX ; }
51 
55  inline MLint8 MLTypeRangeMin(MLint8 ) { return ML_INT8_MIN ; }
61  inline MLfloat MLTypeRangeMin(MLfloat ) { return -ML_FLOAT_MAX ; }
67 
71  inline MLint8 MLTypeMinDifference(MLint8 ) { return 1 ; }
72  inline MLuint8 MLTypeMinDifference(MLuint8 ) { return 1 ; }
73  inline MLint16 MLTypeMinDifference(MLint16 ) { return 1 ; }
74  inline MLuint16 MLTypeMinDifference(MLuint16 ) { return 1 ; }
75  inline MLint32 MLTypeMinDifference(MLint32 ) { return 1 ; }
76  inline MLuint32 MLTypeMinDifference(MLuint32 ) { return 1 ; }
80  inline MLuint64 MLTypeMinDifference(MLuint64 ) { return 1 ; }
81  inline MLint64 MLTypeMinDifference(MLint64 ) { return 1 ; }
83 
87  inline MLfloat MLfabs(MLfloat v){ return fabsf(v); }
88  inline MLdouble MLfabs(MLdouble v){ return fabs (v); }
89  inline MLldouble MLfabs(MLldouble v){ return fabsl(v); }
91 
94  inline int MLisfinite(MLdouble v){ return __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION( _finite(v) , std::isfinite(v) ); }
95 
98  inline int MLisnan(MLdouble v){ return __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION( _isnan(v) , std::isnan(v) ); }
99 
103  template <typename DT>
104  inline DT MLAbs(const DT val){ return (val < 0) ? -val : val; }
105 
107  template<>
108  inline MLuint8 MLAbs(const MLuint8 val){ return val; }
109  template<>
110  inline MLuint16 MLAbs(const MLuint16 val){ return val; }
111  template<>
112  inline MLuint32 MLAbs(const MLuint32 val){ return val; }
113  template<>
114  inline MLuint64 MLAbs(const MLuint64 val){ return val; }
115 
116  // Prevent usage of abs() for floating point types, which will result in miscalculations
117  // on UNIX systems due to stdlib.h 'int abs(int)'
118  namespace ML_NAMESPACE {
119  template <typename DT>
120  inline DT abs (DT val) {
121  // If you come here because of a compiler error, please replace abs() by MLAbs(),
122  // MLfabs, or std::abs(). The usage of abs() will result in calculation errors!
123  BOOST_STATIC_ASSERT(std::numeric_limits<DT>::is_integer);
124  return (val < 0) ? -val : val;
125  }
126  }
127 
143  template <typename T>
144  inline bool MLFloatValuesEqual(const T a, const T b, const T m)
145  {
146  return MLfabs(a - b) < MLTypeMinDifference(a)*m;
147  }
148 
152  inline bool MLValuesAreEqual(MLint8 a, MLint8 b, MLint8 /*m*/) { return a == b; }
153  inline bool MLValuesAreEqual(MLuint8 a, MLuint8 b, MLuint8 /*m*/) { return a == b; }
154  inline bool MLValuesAreEqual(MLint16 a, MLint16 b, MLint16 /*m*/) { return a == b; }
155  inline bool MLValuesAreEqual(MLuint16 a, MLuint16 b, MLuint16 /*m*/) { return a == b; }
156  inline bool MLValuesAreEqual(MLint32 a, MLint32 b, MLint32 /*m*/) { return a == b; }
157  inline bool MLValuesAreEqual(MLuint32 a, MLuint32 b, MLuint32 /*m*/) { return a == b; }
158  inline bool MLValuesAreEqual(MLfloat a, MLfloat b, MLfloat m) { return MLFloatValuesEqual(a, b, m); }
159  inline bool MLValuesAreEqual(MLdouble a, MLdouble b, MLdouble m) { return MLFloatValuesEqual(a, b, m); }
160  inline bool MLValuesAreEqual(MLldouble a, MLldouble b, MLldouble m) { return MLFloatValuesEqual(a, b, m); }
161  inline bool MLValuesAreEqual(MLuint64 a, MLuint64 b, MLuint64 /*m*/) { return a == b; }
162  inline bool MLValuesAreEqual(MLint64 a, MLint64 b, MLint64 /*m*/) { return a == b; }
163 
164  // For all other cases.
165  template <typename T1, typename T2>
166  inline bool MLValuesAreEqual(T1 a, T2 b, MLint64 /*m*/) { return a == b; }
168 
172  inline bool MLValuesDiffer(MLint8 a, MLint8 b, MLint8 /*m*/) { return a != b; }
173  inline bool MLValuesDiffer(MLuint8 a, MLuint8 b, MLuint8 /*m*/) { return a != b; }
174  inline bool MLValuesDiffer(MLint16 a, MLint16 b, MLint16 /*m*/) { return a != b; }
175  inline bool MLValuesDiffer(MLuint16 a, MLuint16 b, MLuint16 /*m*/) { return a != b; }
176  inline bool MLValuesDiffer(MLint32 a, MLint32 b, MLint32 /*m*/) { return a != b; }
177  inline bool MLValuesDiffer(MLuint32 a, MLuint32 b, MLuint32 /*m*/) { return a != b; }
178  inline bool MLValuesDiffer(MLfloat a, MLfloat b, MLfloat m) { return !MLFloatValuesEqual(a, b, m); }
179  inline bool MLValuesDiffer(MLdouble a, MLdouble b, MLdouble m) { return !MLFloatValuesEqual(a, b, m); }
180  inline bool MLValuesDiffer(MLldouble a, MLldouble b, MLldouble m) { return !MLFloatValuesEqual(a, b, m); }
181  inline bool MLValuesDiffer(MLuint64 a, MLuint64 b, MLuint64 /*m*/) { return a != b; }
182  inline bool MLValuesDiffer(MLint64 a, MLint64 b, MLint64 /*m*/) { return a != b; }
183 
184  // For all other cases.
185  template <typename T1, typename T2>
186  inline bool MLValuesDiffer(T1 a, T2 b, MLint64 /*m*/) { return a != b; }
188 
192  inline bool MLValuesAreEqualWOM(MLint8 a, MLint8 b) { return a == b; }
193  inline bool MLValuesAreEqualWOM(MLuint8 a, MLuint8 b) { return a == b; }
194  inline bool MLValuesAreEqualWOM(MLint16 a, MLint16 b) { return a == b; }
195  inline bool MLValuesAreEqualWOM(MLuint16 a, MLuint16 b) { return a == b; }
196  inline bool MLValuesAreEqualWOM(MLint32 a, MLint32 b) { return a == b; }
197  inline bool MLValuesAreEqualWOM(MLuint32 a, MLuint32 b) { return a == b; }
198  inline bool MLValuesAreEqualWOM(MLfloat a, MLfloat b) { return MLfabs(a -b) < ML_FLOAT_MIN ; }
199  inline bool MLValuesAreEqualWOM(MLdouble a, MLdouble b) { return MLfabs(a -b) < ML_DOUBLE_MIN ; }
200  inline bool MLValuesAreEqualWOM(MLldouble a, MLldouble b) { return MLfabs(a -b) < ML_LDOUBLE_MIN; }
201  inline bool MLValuesAreEqualWOM(MLuint64 a, MLuint64 b) { return a == b; }
202  inline bool MLValuesAreEqualWOM(MLint64 a, MLint64 b) { return a == b; }
203 
204  // For all other cases.
205  template <typename T1, typename T2>
206  inline bool MLValuesAreEqualWOM(T1 a, T2 b) { return a == b; }
208 
212  inline bool MLValuesDifferWOM(MLint8 a, MLint8 b) { return a != b; }
213  inline bool MLValuesDifferWOM(MLuint8 a, MLuint8 b) { return a != b; }
214  inline bool MLValuesDifferWOM(MLint16 a, MLint16 b) { return a != b; }
215  inline bool MLValuesDifferWOM(MLuint16 a, MLuint16 b) { return a != b; }
216  inline bool MLValuesDifferWOM(MLint32 a, MLint32 b) { return a != b; }
217  inline bool MLValuesDifferWOM(MLuint32 a, MLuint32 b) { return a != b; }
218  inline bool MLValuesDifferWOM(MLfloat a, MLfloat b) { return MLfabs(a -b) >= ML_FLOAT_MIN ; }
219  inline bool MLValuesDifferWOM(MLdouble a, MLdouble b) { return MLfabs(a -b) >= ML_DOUBLE_MIN ; }
220  inline bool MLValuesDifferWOM(MLldouble a, MLldouble b) { return MLfabs(a -b) >= ML_LDOUBLE_MIN; }
221  inline bool MLValuesDifferWOM(MLuint64 a, MLuint64 b) { return a != b; }
222  inline bool MLValuesDifferWOM(MLint64 a, MLint64 b) { return a != b; }
223 
224  // For all other cases.
225  template <typename T1, typename T2>
226  inline bool MLValuesDifferWOM(T1 a, T2 b) { return a != b; }
228 
232  inline bool MLValueIs0WOM(MLint8 a) { return a == 0; }
233  inline bool MLValueIs0WOM(MLuint8 a) { return a == 0; }
234  inline bool MLValueIs0WOM(MLint16 a) { return a == 0; }
235  inline bool MLValueIs0WOM(MLuint16 a) { return a == 0; }
236  inline bool MLValueIs0WOM(MLint32 a) { return a == 0; }
237  inline bool MLValueIs0WOM(MLuint32 a) { return a == 0; }
238  inline bool MLValueIs0WOM(MLfloat a) { return MLfabs(a) < ML_FLOAT_MIN ; }
239  inline bool MLValueIs0WOM(MLdouble a) { return MLfabs(a) < ML_DOUBLE_MIN ; }
240  inline bool MLValueIs0WOM(MLldouble a) { return MLfabs(a) < ML_LDOUBLE_MIN; }
241  inline bool MLValueIs0WOM(MLuint64 a) { return a == 0; }
242  inline bool MLValueIs0WOM(MLint64 a) { return a == 0; }
243 
244  // For all other cases.
245  template <typename T1>
246  inline bool MLValueIs0WOM(T1 a) { return a == 0; }
248 
249 #endif // __mlTypeDefTraits_H
250 
251 
@ T
Definition: SoKeyGrabber.h:71
MLint8 MLTypeRangeMax(MLint8)
Returns maximum range of the data type of any passed standard data type value.
bool MLFloatValuesEqual(const T a, const T b, const T m)
Returns true if floating point numbers a and b differ less or equal than their technically availably ...
MLint8 MLTypeRangeMin(MLint8)
Returns minimum range of the data type of any passed standard data type value.
bool MLValuesAreEqual(MLint8 a, MLint8 b, MLint8)
Returns true if values are equal (numerically safely compared), otherwise false.
bool MLValuesDiffer(MLint8 a, MLint8 b, MLint8)
Returns true if values differ (numerically safely compared), otherwise false.
MLfloat MLfabs(MLfloat v)
fabs functions to implement type and platform independent MLabs function.
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0, otherwise false.
DT MLAbs(const DT val)
Defines templated MLAbs version to circumvent fabs ambiguities on different platforms.
int MLisnan(MLdouble v)
Returns a non-zero value if and only if its argument is NaN.
bool MLValuesDifferWOM(MLint8 a, MLint8 b)
Returns true if values differ, otherwise false.
int MLisfinite(MLdouble v)
Returns a non-zero value if and only if its argument has a finite value.
bool MLValuesAreEqualWOM(MLint8 a, MLint8 b)
Returns true if values a and b are equal, otherwise false.
MLint8 MLTypeMinDifference(MLint8)
Returns minimum range between two values which should be considered significant.
#define __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION(__ml_win_func, __ml_unix_func)
Some traits and template support for types and constants from mlTypeDefs project.
#define ML_FLOAT_MAX
Definition: mlTypeDefs.h:210
UINT64 MLuint64
Introduce platform independent 64 bit unsigned integer type.
Definition: mlTypeDefs.h:513
long double MLldouble
Definition: mlTypeDefs.h:238
#define ML_INT16_MAX
Definition: mlTypeDefs.h:135
#define ML_INT64_MAX
Definition: mlTypeDefs.h:555
#define ML_UINT16_MAX
Definition: mlTypeDefs.h:152
#define ML_LDOUBLE_EPSILON
Definition: mlTypeDefs.h:242
unsigned int MLuint32
Definition: mlTypeDefs.h:191
#define ML_UINT8_MAX
Definition: mlTypeDefs.h:118
unsigned char MLuint8
Definition: mlTypeDefs.h:115
#define ML_INT8_MIN
Definition: mlTypeDefs.h:105
#define ML_INT64_MIN
Definition: mlTypeDefs.h:554
#define ML_INT16_MIN
Definition: mlTypeDefs.h:134
#define ML_FLOAT_EPSILON
Definition: mlTypeDefs.h:211
#define ML_UINT32_MIN
Definition: mlTypeDefs.h:196
double MLdouble
Definition: mlTypeDefs.h:223
#define ML_UINT64_MIN
Definition: mlTypeDefs.h:564
#define ML_UINT64_MAX
Definition: mlTypeDefs.h:565
#define ML_INT32_MIN
Definition: mlTypeDefs.h:176
#define ML_LDOUBLE_MIN
Definition: mlTypeDefs.h:240
unsigned short MLuint16
Definition: mlTypeDefs.h:148
signed short MLint16
Definition: mlTypeDefs.h:131
char MLint8
Definition: mlTypeDefs.h:103
INT64 MLint64
Include 64 bit integer support for Windows or Unix.
Definition: mlTypeDefs.h:500
#define ML_UINT16_MIN
Definition: mlTypeDefs.h:151
#define ML_UINT32_MAX
Definition: mlTypeDefs.h:197
#define ML_DOUBLE_EPSILON
Definition: mlTypeDefs.h:227
#define ML_INT32_MAX
Definition: mlTypeDefs.h:177
#define ML_FLOAT_MIN
Definition: mlTypeDefs.h:209
#define ML_LDOUBLE_MAX
Definition: mlTypeDefs.h:241
signed int MLint32
Definition: mlTypeDefs.h:167
#define ML_DOUBLE_MIN
Definition: mlTypeDefs.h:225
#define ML_UINT8_MIN
Definition: mlTypeDefs.h:117
#define ML_DOUBLE_MAX
Definition: mlTypeDefs.h:226
float MLfloat
Definition: mlTypeDefs.h:207
#define ML_INT8_MAX
Definition: mlTypeDefs.h:106
DT abs(DT val)