GEL
2
GEL is a library for Geometry and Linear Algebra
|
00001 // $Id: CGLA.h 417 2009-01-12 15:40:09Z jrf $ 00002 00003 #ifndef __CGLA_CGLA_H__ 00004 #define __CGLA_CGLA_H__ 00005 00006 #if (_MSC_VER >= 1200) 00007 #pragma warning (disable: 4244 4800) 00008 #endif 00009 00010 #include <cmath> 00011 #include <cfloat> 00012 #include <climits> 00013 #include <cassert> 00014 #include <algorithm> 00015 #include <functional> 00016 00017 #ifndef M_PI 00018 #define M_PI 3.14159265358979323846 00019 #define M_PI_2 1.57079632679489661923 00020 #endif 00021 00022 namespace CGLA 00023 { 00024 inline float cgla_nan() 00025 { 00026 static const float cgla_nan_value = log(-1.0f); 00027 return cgla_nan_value; 00028 } 00029 00031 #define CGLA_NAN cgla_nan() 00032 00035 #define CGLA_INIT_VALUE cgla_nan() 00036 00039 const double BIG=10e+30; 00040 00043 const double MINUTE=10e-30; 00044 00047 const double TINY=3e-7; 00048 00051 const double SMALL=10e-2; 00052 00055 const unsigned int GEL_RAND_MAX=UINT_MAX; 00056 00057 inline double sqrt3() 00058 { 00059 static const double sqrt3_val = sqrt(3.0); 00060 return sqrt3_val; 00061 } 00062 00063 #define SQRT3 sqrt3() 00064 00066 enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2}; 00067 00068 inline bool isnan(double x) { return x != x; } 00069 00070 template<class Scalar> 00071 Scalar s_min(Scalar a, Scalar b) 00072 { 00073 return a<b ? a : b; 00074 } 00075 00076 template<class Scalar> 00077 Scalar s_max(Scalar a, Scalar b) 00078 { 00079 return a>b ? a : b; 00080 } 00081 00083 template <class Scalar> 00084 inline Scalar sqr(Scalar x) { 00085 return x*x;} 00086 00088 template <class Scalar> 00089 inline Scalar qbe(Scalar x) { 00090 return x*x*x;} 00091 00092 template <class Scalar> 00093 inline bool is_zero(Scalar x) {return (x > -MINUTE && x < MINUTE);} 00094 00095 template <class Scalar> 00096 inline bool is_tiny(Scalar x) {return (x > -TINY && x < TINY);} 00097 00100 inline int two_to_what_power(unsigned int x) 00101 { 00102 if (x<1) 00103 return -1; 00104 int i = 0; 00105 while (x != 1) {x>>=1;i++;} 00106 return i; 00107 } 00108 00109 #ifdef __sgi 00110 inline int round(float x) {return int(rint(x));} 00111 #else 00112 inline int round(float x) {return int(x+0.5);} 00113 #endif 00114 00115 template<class T> 00116 inline T sign(T x) {return x>=T(0) ? 1 : -1;} 00117 00119 template<class T> 00120 inline T int_pow(T a, unsigned int n) 00121 { 00122 T result = static_cast<T>(1); 00123 for(; n > 0; n >>= 1) 00124 { 00125 if(n & 1) result = result*a; 00126 a *= a; 00127 } 00128 return result; 00129 } 00130 00132 void gel_srand(unsigned int seed); 00133 00138 unsigned int gel_rand(unsigned int k); 00139 00143 unsigned int gel_rand(); 00144 00149 template<class T, class S> 00150 void raw_assign(T& a, const S* b) 00151 { 00152 memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T)); 00153 } 00154 00155 } 00156 00157 #endif