OpenSceneGraph 3.6.5
Math
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
22namespace osg {
23
24// define the standard trig values
25#ifdef PI
26#undef PI
27#undef PI_2
28#undef PI_4
29#endif
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;
35
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;
41
42
43template<typename T>
44inline T default_value() { return T(); }
45
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; }
54
55
56template<typename T>
57inline T absolute(T v) { return v<(T)0?-v:v; }
58
63inline bool equivalent(float lhs,float rhs,float epsilon=1e-6)
64 { float delta = rhs-lhs; return delta<0.0f?delta>=-epsilon:delta<=epsilon; }
65
70inline bool equivalent(double lhs,double rhs,double epsilon=1e-6)
71 { double delta = rhs-lhs; return delta<0.0?delta>=-epsilon:delta<=epsilon; }
72
77template<typename T>
78inline T minimum(T lhs,T rhs) { return lhs<rhs?lhs:rhs; }
79
84template<typename T>
85inline T maximum(T lhs,T rhs) { return lhs>rhs?lhs:rhs; }
86
87template<typename T>
88inline T clampTo(T v,T minimum,T maximum) { return v<minimum?minimum:v>maximum?maximum:v; }
89
90template<typename T>
91inline T clampAbove(T v,T minimum) { return v<minimum?minimum:v; }
92
93template<typename T>
94inline T clampBelow(T v,T maximum) { return v>maximum?maximum:v; }
95
96template<typename T>
97inline T clampBetween(T v,T minimum, T maximum)
98 { return clampBelow(clampAbove(v,minimum),maximum); }
99
100template<typename T>
101inline T sign(T v) { return v<(T)0?(T)-1:(T)1; }
102
103template<typename T>
104inline T signOrZero(T v) { return v<(T)0 ? (T)-1 : ( v>(T)0 ? (T)1 : 0 ); }
105
106template<typename T>
107inline T square(T v) { return v*v; }
108
109template<typename T>
110inline T signedSquare(T v) { return v<(T)0?-v*v:v*v;; }
111
112inline float inDegrees(float angle) { return angle*(float)PI/180.0f; }
113inline double inDegrees(double angle) { return angle*PI/180.0; }
114
115template<typename T>
116inline T inRadians(T angle) { return angle; }
117
118inline float DegreesToRadians(float angle) { return angle*(float)PI/180.0f; }
119inline double DegreesToRadians(double angle) { return angle*PI/180.0; }
120
121inline float RadiansToDegrees(float angle) { return angle*180.0f/(float)PI; }
122inline double RadiansToDegrees(double angle) { return angle*180.0/PI; }
123
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); }
126
127#if defined(_MSC_VER)
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); }
132#else
133 inline bool isNaN(float v) { return std::isnan(v); }
134 inline bool isNaN(double v) { return std::isnan(v); }
135#endif
136
137
139template<typename T>
140inline float computeVolume(const T& a,const T& b,const T& c,const T& d)
141{
142 return fabsf(((b-c)^(a-b))*(d-b));
143}
144
146template<typename T>
147inline float computeVolume(const T& f1,const T& f2,const T& f3,
148 const T& b1,const T& b2,const T& b3)
149{
150 return computeVolume(f1,f2,f3,b1)+
151 computeVolume(b1,b2,b3,f2)+
152 computeVolume(b1,b3,f2,f3);
153}
154
156extern OSG_EXPORT double asciiToDouble(const char* str);
157
159inline float asciiToFloat(const char* str) { return static_cast<float>(asciiToDouble(str)); }
160
162extern OSG_EXPORT double findAsciiToDouble(const char* str);
163
165inline float findAsciiToFloat(const char* str) { return static_cast<float>(findAsciiToDouble(str)); }
166
167}
168
169
170#endif // __OSG_MATH
171
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
T clampBelow(T v, T maximum)
Definition Math:94
const double LN_2
Definition Math:33
T signedSquare(T v)
Definition Math:110
float default_value< float >()
Definition Math:46
const double PI_2
Definition Math:31
const float INVLN_2f
Definition Math:40
T clampBetween(T v, T minimum, T maximum)
Definition Math:97
const float PI_4f
Definition Math:38
T inRadians(T angle)
Definition Math:116
OSG_EXPORT double findAsciiToDouble(const char *str)
Detect first ascii POSITIVE number in string and convert to double.
unsigned short default_value< unsigned short >()
Definition Math:51
bool equivalent(float lhs, float rhs, float epsilon=1e-6)
return true if float lhs and rhs are equivalent, meaning that the difference between them is less tha...
Definition Math:63
char default_value< char >()
Definition Math:48
int default_value< int >()
Definition Math:52
float asciiToFloat(const char *str)
Convert a ascii number to a float, ignoring locale settings.
Definition Math:159
float RadiansToDegrees(float angle)
Definition Math:121
float inDegrees(float angle)
Definition Math:112
bool isNaN(float v)
Definition Math:133
T signOrZero(T v)
Definition Math:104
float DegreesToRadians(float angle)
Definition Math:118
T clampTo(T v, T minimum, T maximum)
Definition Math:88
T absolute(T v)
Definition Math:57
const double PI_4
Definition Math:32
T square(T v)
Definition Math:107
unsigned char default_value< unsigned char >()
Definition Math:49
T sign(T v)
Definition Math:101
unsigned int default_value< unsigned int >()
Definition Math:53
const float PI_2f
Definition Math:37
float computeVolume(const T &a, const T &b, const T &c, const T &d)
compute the volume of a tetrahedron.
Definition Math:140
OSG_EXPORT double asciiToDouble(const char *str)
Convert a ascii number to a double, ignoring locale settings.
T clampAbove(T v, T minimum)
Definition Math:91
const double PI
Definition Math:30
T maximum(T lhs, T rhs)
return the maximum of two values, equivalent to std::max.
Definition Math:85
double default_value< double >()
Definition Math:47
T default_value()
Definition Math:44
float round(float v)
Definition Math:124
T minimum(T lhs, T rhs)
return the minimum of two values, equivalent to std::min.
Definition Math:78
float findAsciiToFloat(const char *str)
Detect first ascii POSITIVE number in string and convert to double.
Definition Math:165
const float PIf
Definition Math:36
const float LN_2f
Definition Math:39
const double INVLN_2
Definition Math:34
short default_value< short >()
Definition Math:50
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Wed Jul 23 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.