BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 // $Id: common.h,v 1.29.16.1 2007/03/25 21:23:45 oliver Exp $ 00005 // 00006 00007 #ifndef BALL_MATHS_COMMON_H 00008 #define BALL_MATHS_COMMON_H 00009 00010 #ifndef BALL_CONFIG_CONFIG_H 00011 # include <BALL/CONFIG/config.h> 00012 #endif 00013 00014 #include <math.h> 00015 00016 #ifdef BALL_HAS_IEEEFP_H 00017 # include <ieeefp.h> 00018 #endif 00019 00020 #ifdef BALL_HAS_FLOAT_H 00021 # include <float.h> 00022 #endif 00023 00024 #ifndef BALL_COMMON_CONSTANTS_H 00025 # include <BALL/COMMON/constants.h> 00026 #endif 00027 00028 #ifndef BALL_COMMON_GLOBAL_H 00029 # include <BALL/COMMON/global.h> 00030 #endif 00031 00032 #ifndef BALL_COMMON_MACROS_H 00033 # include <BALL/COMMON/macros.h> 00034 #endif 00035 00036 namespace BALL 00037 { 00038 00039 namespace Maths 00040 { 00041 00047 00052 template <typename T> 00053 inline 00054 T abs(const T& t) 00055 { 00056 return BALL_ABS(t); 00057 } 00058 00063 template <typename T> 00064 inline 00065 T frac(const T& t) 00066 { 00067 long tmp = (long)t; 00068 return (t - (T)tmp); 00069 } 00070 00071 #ifndef max 00072 00077 template <typename T> 00078 inline 00079 T max(const T& a, const T& b) 00080 { 00081 return BALL_MAX(a, b); 00082 } 00083 00090 template <typename T> 00091 inline 00092 T max(const T& a, const T& b, const T &ct) 00093 { 00094 return BALL_MAX3(a, b, ct); 00095 } 00096 #endif 00097 00098 #ifndef min 00099 00104 template <typename T> 00105 inline 00106 T min(const T& a, const T& b) 00107 { 00108 return BALL_MIN(a, b); 00109 } 00110 00117 template <typename T> 00118 inline 00119 T min(const T& a, const T& b, const T &ct) 00120 { 00121 return BALL_MIN3(a, b, ct); 00122 } 00123 #endif 00124 00129 template <typename T> 00130 inline 00131 T round(const T& t) 00132 { 00133 return (T)(t > 0 ? long(t + 0.5) : long(t - 0.5)); 00134 } 00135 00140 template <typename T> 00141 inline 00142 T sgn(const T& t) 00143 { 00144 return BALL_SGN(t); 00145 } 00146 00151 template <typename T> 00152 inline 00153 bool isFinite(const T& t) 00154 { 00155 #ifdef BALL_COMPILER_MSVC 00156 return ::_finite(t); 00157 #else 00158 return finite(t); 00159 #endif 00160 } 00161 00166 template <typename T> 00167 inline 00168 bool isNan(const T& t) 00169 { 00170 #ifdef BALL_COMPILER_MSVC 00171 return (_isnan(t) != 0); 00172 #elif defined(BALL_OS_DARWIN) 00173 return ( __inline_isnand(t) != 0); 00174 #else 00175 return (isnan(t) != 0); 00176 #endif 00177 } 00178 00183 template <typename T> 00184 inline 00185 bool isInfinite(const T& t) 00186 { 00187 return (!Maths::isFinite(t) && !Maths::isNan(t)); 00188 } 00189 00194 template <typename T> 00195 inline 00196 bool isZero(const T& t) 00197 { 00198 return (abs(t) < Constants::EPSILON); 00199 } 00200 00205 template <typename T> 00206 inline 00207 bool isNotZero(const T& t) 00208 { 00209 return (abs(t) >= Constants::EPSILON); 00210 } 00211 00217 template <typename T1, typename T2> 00218 inline 00219 bool isEqual(const T1& a, const T2& b) 00220 { 00221 return (abs(a - b) < Constants::EPSILON); 00222 } 00223 00229 template <typename T1, typename T2> 00230 inline 00231 bool isNotEqual(const T1& a, const T2& b) 00232 { 00233 return (abs(a - b) >= Constants::EPSILON); 00234 } 00235 00241 template <typename T1, typename T2> 00242 inline 00243 bool isLess(const T1& a, const T2& b) 00244 00245 { 00246 return ((a - b) <= -Constants::EPSILON); 00247 } 00248 00254 template <typename T1, typename T2> 00255 inline 00256 bool isLessOrEqual(const T1& a, const T2& b) 00257 { 00258 return ((a - b) < Constants::EPSILON); 00259 } 00260 00266 template <typename T1, typename T2> 00267 inline 00268 bool isGreaterOrEqual(const T1& a, const T2& b) 00269 { 00270 return ((a - b) > -Constants::EPSILON); 00271 } 00272 00278 template <typename T1, typename T2> 00279 inline 00280 bool isGreater(const T1& a, const T2& b) 00281 { 00282 return (a - b >= Constants::EPSILON); 00283 } 00284 00289 template <typename T> 00290 inline 00291 long floor(const T& t) 00292 { 00293 return (long)(Maths::isGreater(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t - 1)); 00294 } 00295 00300 template <typename T> 00301 inline 00302 long ceiling(const T& t) 00303 { 00304 return (long)(Maths::isLess(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t + 1)); 00305 } 00306 00312 template <typename T1, typename T2> 00313 inline 00314 Index compare(const T1& a, const T2& b) 00315 { 00316 return (Maths::isLess(a, b) ? -1 : Maths::isEqual(a, b) ? 0 : 1); 00317 } 00318 00325 template <typename T> 00326 inline 00327 bool isNear(const T& a, const T& b, const T& max_diff) 00328 { 00329 return (abs((double)a - (double)b) < abs((double)max_diff)); 00330 } 00331 00332 00334 inline double rint(double x) 00335 { 00336 if (x < 0.0) return (double)(int)(x - 0.5); 00337 else return (double)(int)(x + 0.5); 00338 } 00339 00341 00342 } // namespace Maths 00343 } // namespace BALL 00344 00345 #endif // BALL_MATHS_COMMON_H