common.h

Go to the documentation of this file.
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       #else
00173         return (isnan(t) != 0);
00174       #endif
00175     }
00176 
00181     template <typename T>
00182     inline 
00183     bool isInfinite(const T& t)
00184     {
00185       return (!Maths::isFinite(t) && !Maths::isNan(t));
00186     }
00187 
00192     template <typename T>
00193     inline 
00194     bool isZero(const T& t)
00195     {
00196       return (abs(t) < Constants::EPSILON);
00197     }
00198 
00203     template <typename T>
00204     inline 
00205     bool isNotZero(const T& t)
00206     {
00207       return (abs(t) >= Constants::EPSILON);
00208     }
00209 
00215     template <typename T1, typename T2>
00216     inline 
00217     bool isEqual(const T1& a, const T2& b)
00218     {
00219       return (abs(a - b) < Constants::EPSILON);
00220     }
00221 
00227     template <typename T1, typename T2>
00228     inline 
00229     bool isNotEqual(const T1& a, const T2& b)
00230     {
00231       return (abs(a - b) >= Constants::EPSILON);
00232     }
00233 
00239     template <typename T1, typename T2>
00240     inline 
00241     bool isLess(const T1& a, const T2& b)
00242 
00243     {
00244       return ((a - b) <= -Constants::EPSILON);
00245     }
00246 
00252     template <typename T1, typename T2>
00253     inline 
00254     bool isLessOrEqual(const T1& a, const T2& b)
00255     {
00256       return ((a - b) < Constants::EPSILON);
00257     }
00258 
00264     template <typename T1, typename T2>
00265     inline 
00266     bool isGreaterOrEqual(const T1& a, const T2& b)
00267     {
00268       return ((a - b) > -Constants::EPSILON);
00269     }
00270 
00276     template <typename T1, typename T2>
00277     inline 
00278     bool isGreater(const T1& a, const T2& b)
00279     {
00280       return (a - b >= Constants::EPSILON);
00281     }
00282 
00287     template <typename T>
00288     inline 
00289     long floor(const T& t)
00290     {
00291       return (long)(Maths::isGreater(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t - 1));
00292     }
00293 
00298     template <typename T>
00299     inline 
00300     long ceiling(const T& t)
00301     {
00302       return (long)(Maths::isLess(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t + 1));
00303     }
00304 
00310     template <typename T1, typename T2>
00311     inline 
00312     Index compare(const T1& a, const T2& b)
00313     {
00314       return (Maths::isLess(a, b) ? -1 : Maths::isEqual(a, b) ? 0 : 1);
00315     }
00316 
00323     template <typename T>
00324     inline 
00325     bool isNear(const T& a, const T& b, const T& max_diff)
00326     {
00327       return (abs((double)a - (double)b) < abs((double)max_diff));
00328     }
00329 
00330 
00332     inline double rint(double x)
00333     {
00334       if (x < 0.0) return (double)(int)(x - 0.5);
00335       else return (double)(int)(x + 0.5);
00336     }
00337 
00339     
00340   } // namespace Maths
00341 } // namespace BALL
00342 
00343 #endif // BALL_MATHS_COMMON_H