OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Math.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
2  *
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.
7  *
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.
12 */
13 
14 #ifndef __OSG_MATH
15 #define __OSG_MATH
16 
17 #include <cmath>
18 #include <cfloat>
19 
20 #include <osg/Export>
21 
22 namespace osg {
23 
24 // define the standard trig values
25 #ifdef PI
26 #undef PI
27 #undef PI_2
28 #undef PI_4
29 #endif
30 const double PI = 3.14159265358979323846;
31 const double PI_2 = 1.57079632679489661923;
32 const double PI_4 = 0.78539816339744830962;
33 const double LN_2 = 0.69314718055994530942;
34 const double INVLN_2 = 1.0 / LN_2;
35 
36 
37 template<typename T>
38 inline T absolute(T v) { return v<(T)0?-v:v; }
39 
44 inline bool equivalent(float lhs,float rhs,float epsilon=1e-6)
45  { float delta = rhs-lhs; return delta<0.0f?delta>=-epsilon:delta<=epsilon; }
46 
51 inline bool equivalent(double lhs,double rhs,double epsilon=1e-6)
52  { double delta = rhs-lhs; return delta<0.0?delta>=-epsilon:delta<=epsilon; }
53 
58 template<typename T>
59 inline T minimum(T lhs,T rhs) { return lhs<rhs?lhs:rhs; }
60 
65 template<typename T>
66 inline T maximum(T lhs,T rhs) { return lhs>rhs?lhs:rhs; }
67 
68 template<typename T>
69 inline T clampTo(T v,T minimum,T maximum) { return v<minimum?minimum:v>maximum?maximum:v; }
70 
71 template<typename T>
72 inline T clampAbove(T v,T minimum) { return v<minimum?minimum:v; }
73 
74 template<typename T>
75 inline T clampBelow(T v,T maximum) { return v>maximum?maximum:v; }
76 
77 template<typename T>
78 inline T clampBetween(T v,T minimum, T maximum)
79  { return clampBelow(clampAbove(v,minimum),maximum); }
80 
81 template<typename T>
82 inline T sign(T v) { return v<(T)0?(T)-1:(T)1; }
83 
84 template<typename T>
85 inline T signOrZero(T v) { return v<(T)0 ? (T)-1 : ( v>(T)0 ? (T)1 : 0 ); }
86 
87 template<typename T>
88 inline T square(T v) { return v*v; }
89 
90 template<typename T>
91 inline T signedSquare(T v) { return v<(T)0?-v*v:v*v;; }
92 
93 inline float inDegrees(float angle) { return angle*(float)PI/180.0f; }
94 inline double inDegrees(double angle) { return angle*PI/180.0; }
95 
96 template<typename T>
97 inline T inRadians(T angle) { return angle; }
98 
99 inline float DegreesToRadians(float angle) { return angle*(float)PI/180.0f; }
100 inline double DegreesToRadians(double angle) { return angle*PI/180.0; }
101 
102 inline float RadiansToDegrees(float angle) { return angle*180.0f/(float)PI; }
103 inline double RadiansToDegrees(double angle) { return angle*180.0/PI; }
104 
105 inline float round(float v) { return v>=0.0f?floorf(v+0.5f):ceilf(v-0.5f); }
106 inline double round(double v) { return v>=0.0?floor(v+0.5):ceil(v-0.5); }
107 
108 #if defined(_MSC_VER)
109  inline bool isNaN(double v) { return _isnan(v)!=0; }
110 #elif defined(__ANDROID__)
111  inline bool isNaN(float v) { return isnan(v); }
112  inline bool isNaN(double v) { return isnan(v); }
113 #else
114  inline bool isNaN(float v) { return std::isnan(v); }
115  inline bool isNaN(double v) { return std::isnan(v); }
116 #endif
117 
118 
120 template<typename T>
121 inline float computeVolume(const T& a,const T& b,const T& c,const T& d)
122 {
123  return fabsf(((b-c)^(a-b))*(d-b));
124 }
125 
127 template<typename T>
128 inline float computeVolume(const T& f1,const T& f2,const T& f3,
129  const T& b1,const T& b2,const T& b3)
130 {
131  return computeVolume(f1,f2,f3,b1)+
132  computeVolume(b1,b2,b3,f2)+
133  computeVolume(b1,b3,f2,f3);
134 }
135 
137 extern OSG_EXPORT double asciiToDouble(const char* str);
138 
140 inline float asciiToFloat(const char* str) { return static_cast<float>(asciiToDouble(str)); }
141 
143 extern OSG_EXPORT double findAsciiToDouble(const char* str);
144 
146 inline float findAsciiToFloat(const char* str) { return static_cast<float>(findAsciiToDouble(str)); }
147 
148 }
149 
150 
151 #endif // __OSG_MATH
152 
#define OSG_EXPORT
Definition: Export.h:43
bool equivalent(float lhs, float rhs, float epsilon=1e-6)
Definition: Math.h:44
const double PI_2
Definition: Math.h:31
T absolute(T v)
Definition: Math.h:38
T clampBelow(T v, T maximum)
Definition: Math.h:75
T sign(T v)
Definition: Math.h:82
float findAsciiToFloat(const char *str)
Definition: Math.h:146
T clampBetween(T v, T minimum, T maximum)
Definition: Math.h:78
T minimum(T lhs, T rhs)
Definition: Math.h:59
const double LN_2
Definition: Math.h:33
bool isNaN(float v)
Definition: Math.h:114
T maximum(T lhs, T rhs)
Definition: Math.h:66
T signOrZero(T v)
Definition: Math.h:85
const double PI_4
Definition: Math.h:32
float RadiansToDegrees(float angle)
Definition: Math.h:102
OSG_EXPORT double asciiToDouble(const char *str)
T clampAbove(T v, T minimum)
Definition: Math.h:72
T inRadians(T angle)
Definition: Math.h:97
T clampTo(T v, T minimum, T maximum)
Definition: Math.h:69
const double INVLN_2
Definition: Math.h:34
float DegreesToRadians(float angle)
Definition: Math.h:99
const double PI
Definition: Math.h:30
T square(T v)
Definition: Math.h:88
Definition: AlphaFunc.h:19
float round(float v)
Definition: Math.h:105
OSG_EXPORT double findAsciiToDouble(const char *str)
T signedSquare(T v)
Definition: Math.h:91
float asciiToFloat(const char *str)
Definition: Math.h:140
float computeVolume(const T &a, const T &b, const T &c, const T &d)
Definition: Math.h:121
float inDegrees(float angle)
Definition: Math.h:93