1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
24// define the standard trig values
30const double PI = 3.14159265358979323846;
31const double PI_2 = 1.57079632679489661923;
32const double PI_4 = 0.78539816339744830962;
33const double LN_2 = 0.69314718055994530942;
34const double INVLN_2 = 1.0 / LN_2;
36const float PIf = 3.14159265358979323846f;
37const float PI_2f = 1.57079632679489661923f;
38const float PI_4f = 0.78539816339744830962f;
39const float LN_2f = 0.69314718055994530942f;
40const float INVLN_2f = 1.0f / LN_2f;
44inline T default_value() { return T(); }
46template<> inline float default_value<float>() { return 0.0f; }
47template<> inline double default_value<double>() { return 0.0; }
48template<> inline char default_value<char>() { return 0; }
49template<> inline unsigned char default_value<unsigned char>() { return 0; }
50template<> inline short default_value<short>() { return 0; }
51template<> inline unsigned short default_value<unsigned short>() { return 0; }
52template<> inline int default_value<int>() { return 0; }
53template<> inline unsigned int default_value<unsigned int>() { return 0; }
57inline T absolute(T v) { return v<(T)0?-v:v; }
59/** return true if float lhs and rhs are equivalent,
60 * meaning that the difference between them is less than an epsilon value
61 * which defaults to 1e-6.
63inline bool equivalent(float lhs,float rhs,float epsilon=1e-6)
64 { float delta = rhs-lhs; return delta<0.0f?delta>=-epsilon:delta<=epsilon; }
66/** return true if double lhs and rhs are equivalent,
67 * meaning that the difference between them is less than an epsilon value
68 * which defaults to 1e-6.
70inline bool equivalent(double lhs,double rhs,double epsilon=1e-6)
71 { double delta = rhs-lhs; return delta<0.0?delta>=-epsilon:delta<=epsilon; }
73/** return the minimum of two values, equivalent to std::min.
74 * std::min not used because of STL implementation under IRIX not containing
78inline T minimum(T lhs,T rhs) { return lhs<rhs?lhs:rhs; }
80/** return the maximum of two values, equivalent to std::max.
81 * std::max not used because of STL implementation under IRIX not containing
85inline T maximum(T lhs,T rhs) { return lhs>rhs?lhs:rhs; }
88inline T clampTo(T v,T minimum,T maximum) { return v<minimum?minimum:v>maximum?maximum:v; }
91inline T clampAbove(T v,T minimum) { return v<minimum?minimum:v; }
94inline T clampBelow(T v,T maximum) { return v>maximum?maximum:v; }
97inline T clampBetween(T v,T minimum, T maximum)
98 { return clampBelow(clampAbove(v,minimum),maximum); }
101inline T sign(T v) { return v<(T)0?(T)-1:(T)1; }
104inline T signOrZero(T v) { return v<(T)0 ? (T)-1 : ( v>(T)0 ? (T)1 : 0 ); }
107inline T square(T v) { return v*v; }
110inline T signedSquare(T v) { return v<(T)0?-v*v:v*v;; }
112inline float inDegrees(float angle) { return angle*(float)PI/180.0f; }
113inline double inDegrees(double angle) { return angle*PI/180.0; }
116inline T inRadians(T angle) { return angle; }
118inline float DegreesToRadians(float angle) { return angle*(float)PI/180.0f; }
119inline double DegreesToRadians(double angle) { return angle*PI/180.0; }
121inline float RadiansToDegrees(float angle) { return angle*180.0f/(float)PI; }
122inline double RadiansToDegrees(double angle) { return angle*180.0/PI; }
124inline float round(float v) { return v>=0.0f?floorf(v+0.5f):ceilf(v-0.5f); }
125inline double round(double v) { return v>=0.0?floor(v+0.5):ceil(v-0.5); }
128 inline bool isNaN(double v) { return _isnan(v)!=0; }
129#elif defined(__ANDROID__)
130 inline bool isNaN(float v) { return isnan(v); }
131 inline bool isNaN(double v) { return isnan(v); }
133 inline bool isNaN(float v) { return std::isnan(v); }
134 inline bool isNaN(double v) { return std::isnan(v); }
138/** compute the volume of a tetrahedron. */
140inline float computeVolume(const T& a,const T& b,const T& c,const T& d)
142 return fabsf(((b-c)^(a-b))*(d-b));
145/** compute the volume of a prism. */
147inline float computeVolume(const T& f1,const T& f2,const T& f3,
148 const T& b1,const T& b2,const T& b3)
150 return computeVolume(f1,f2,f3,b1)+
151 computeVolume(b1,b2,b3,f2)+
152 computeVolume(b1,b3,f2,f3);
155/** Convert a ascii number to a double, ignoring locale settings.*/
156extern OSG_EXPORT double asciiToDouble(const char* str);
158/** Convert a ascii number to a float, ignoring locale settings.*/
159inline float asciiToFloat(const char* str) { return static_cast<float>(asciiToDouble(str)); }
161/** Detect first ascii POSITIVE number in string and convert to double.*/
162extern OSG_EXPORT double findAsciiToDouble(const char* str);
164/** Detect first ascii POSITIVE number in string and convert to double.*/
165inline float findAsciiToFloat(const char* str) { return static_cast<float>(findAsciiToDouble(str)); }