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
51
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
MLint8 MLTypeRangeMax(MLint8)
Returns the 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 the 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, it returns false.
bool MLValuesDiffer(MLint8 a, MLint8 b, MLint8)
Returns true if values differ (numerically safely compared); otherwise, it returns 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, it returns false.
DT MLAbs(const DT val)
Defines a 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, it returns 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, it returns false.
MLint8 MLTypeMinDifference(MLint8)
Returns the minimum range between two values that should be considered significant.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
#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:204
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition mlTypeDefs.h:425
long double MLldouble
Definition mlTypeDefs.h:232
#define ML_INT16_MAX
Definition mlTypeDefs.h:129
#define ML_INT64_MAX
Definition mlTypeDefs.h:467
#define ML_UINT16_MAX
Definition mlTypeDefs.h:146
#define ML_LDOUBLE_EPSILON
Definition mlTypeDefs.h:236
unsigned int MLuint32
Definition mlTypeDefs.h:185
#define ML_UINT8_MAX
Definition mlTypeDefs.h:112
unsigned char MLuint8
Definition mlTypeDefs.h:109
#define ML_INT8_MIN
Definition mlTypeDefs.h:99
#define ML_INT64_MIN
Definition mlTypeDefs.h:466
#define ML_INT16_MIN
Definition mlTypeDefs.h:128
#define ML_FLOAT_EPSILON
Definition mlTypeDefs.h:205
#define ML_UINT32_MIN
Definition mlTypeDefs.h:190
double MLdouble
Definition mlTypeDefs.h:217
#define ML_UINT64_MIN
Definition mlTypeDefs.h:476
#define ML_UINT64_MAX
Definition mlTypeDefs.h:477
#define ML_INT32_MIN
Definition mlTypeDefs.h:170
#define ML_LDOUBLE_MIN
Definition mlTypeDefs.h:234
unsigned short MLuint16
Definition mlTypeDefs.h:142
signed short MLint16
Definition mlTypeDefs.h:125
char MLint8
Definition mlTypeDefs.h:97
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
Definition mlTypeDefs.h:412
#define ML_UINT16_MIN
Definition mlTypeDefs.h:145
#define ML_UINT32_MAX
Definition mlTypeDefs.h:191
#define ML_DOUBLE_EPSILON
Definition mlTypeDefs.h:221
#define ML_INT32_MAX
Definition mlTypeDefs.h:171
#define ML_FLOAT_MIN
Definition mlTypeDefs.h:203
#define ML_LDOUBLE_MAX
Definition mlTypeDefs.h:235
signed int MLint32
Definition mlTypeDefs.h:161
#define ML_DOUBLE_MIN
Definition mlTypeDefs.h:219
#define ML_UINT8_MIN
Definition mlTypeDefs.h:111
#define ML_DOUBLE_MAX
Definition mlTypeDefs.h:220
float MLfloat
Definition mlTypeDefs.h:201
#define ML_INT8_MAX
Definition mlTypeDefs.h:100
DT abs(DT val)