| 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 <math.h> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Export> |
|---|
| 20 | |
|---|
| 21 | //certain math functions were not defined until 10.2 |
|---|
| 22 | //so this code checks the version so it can add in workarounds for older versions. |
|---|
| 23 | #ifdef __APPLE__ |
|---|
| 24 | // Using std::isnan will work for OS X, but use of <cmath> |
|---|
| 25 | // and std:: are not necessarily portible with other systems so |
|---|
| 26 | // the include of <cmath> is isolated here. |
|---|
| 27 | #include <cmath> |
|---|
| 28 | #include <AvailabilityMacros.h> |
|---|
| 29 | #if !defined(MAC_OS_X_VERSION_10_2) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_2) |
|---|
| 30 | // One extra check to verify the gcc version. |
|---|
| 31 | // The assumption is that there is no possible way to use gcc 4+ |
|---|
| 32 | // on anything less than 10.3.9. So if gcc 4 is in use, this means |
|---|
| 33 | // pre-10.2 support is not intended and we need not define APPLE_PRE_10_2. |
|---|
| 34 | // The reason for this extra check is that if the user relies on default |
|---|
| 35 | // settings, MAC_OS_X_VERSION_MIN_REQUIRED will be set to 1010 and hit |
|---|
| 36 | // this code path, but this is probably not what they want if using gcc 4+. |
|---|
| 37 | #if (__GNUC__ < 4) |
|---|
| 38 | #define APPLE_PRE_10_2 |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6) |
|---|
| 42 | #define APPLE_PRE_10_6 |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | #endif |
|---|
| 46 | #endif |
|---|
| 47 | |
|---|
| 48 | #if defined(_MSC_VER) |
|---|
| 49 | #include <float.h> |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | #if defined (sun) || \ |
|---|
| 53 | defined (APPLE_PRE_10_6) || \ |
|---|
| 54 | (defined (_AIX) && defined (__GNUC__)) |
|---|
| 55 | |
|---|
| 56 | #include <float.h> |
|---|
| 57 | |
|---|
| 58 | #ifndef acosf |
|---|
| 59 | inline float acosf(float value) { return static_cast<float>(acos(value)); } |
|---|
| 60 | #endif |
|---|
| 61 | |
|---|
| 62 | #ifndef asinf |
|---|
| 63 | inline float asinf(float value) { return static_cast<float>(asin(value)); } |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | #ifndef cosf |
|---|
| 67 | inline float cosf(float value) { return static_cast<float>(cos(value)); } |
|---|
| 68 | #endif |
|---|
| 69 | |
|---|
| 70 | #ifndef sinf |
|---|
| 71 | inline float sinf(float value) { return static_cast<float>(sin(value)); } |
|---|
| 72 | #endif |
|---|
| 73 | |
|---|
| 74 | #ifndef logf |
|---|
| 75 | inline float logf(float value) { return static_cast<float>(log(value)); } |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | #ifndef powf |
|---|
| 79 | inline float powf(float value1,float value2) { return static_cast<float>(pow(value1,value2)); } |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 82 | #ifndef sqrtf |
|---|
| 83 | inline float sqrtf(float value) { return static_cast<float>(sqrt(value)); } |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | #ifndef fabsf |
|---|
| 87 | inline float fabsf(float value) { return static_cast<float>(fabs(value)); } |
|---|
| 88 | #endif |
|---|
| 89 | |
|---|
| 90 | #ifndef atan2f |
|---|
| 91 | inline float atan2f(float value1, float value2) { return static_cast<float>(atan2(value1,value2)); } |
|---|
| 92 | #endif |
|---|
| 93 | |
|---|
| 94 | #ifndef fmodf |
|---|
| 95 | inline float fmodf(float value1, float value2) { return static_cast<float>(fmod(value1,value2)); } |
|---|
| 96 | #endif |
|---|
| 97 | |
|---|
| 98 | #ifndef tanf |
|---|
| 99 | inline float tanf(float value) { return static_cast<float>(tan(value)); } |
|---|
| 100 | #endif |
|---|
| 101 | |
|---|
| 102 | #endif |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | #if defined (sun) || \ |
|---|
| 106 | defined (__hpux) || \ |
|---|
| 107 | defined (APPLE_PRE_10_2) || \ |
|---|
| 108 | (defined (_AIX) && defined (__GNUC__)) |
|---|
| 109 | |
|---|
| 110 | #ifndef floorf |
|---|
| 111 | inline float floorf(float value) { return static_cast<float>(floor(value)); } |
|---|
| 112 | #endif |
|---|
| 113 | |
|---|
| 114 | #ifndef ceilf |
|---|
| 115 | inline float ceilf(float value) { return static_cast<float>(ceil(value)); } |
|---|
| 116 | #endif |
|---|
| 117 | |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | namespace osg { |
|---|
| 121 | |
|---|
| 122 | // define the standard trig values |
|---|
| 123 | #ifdef PI |
|---|
| 124 | #undef PI |
|---|
| 125 | #undef PI_2 |
|---|
| 126 | #undef PI_4 |
|---|
| 127 | #endif |
|---|
| 128 | const double PI = 3.14159265358979323846; |
|---|
| 129 | const double PI_2 = 1.57079632679489661923; |
|---|
| 130 | const double PI_4 = 0.78539816339744830962; |
|---|
| 131 | const double LN_2 = 0.69314718055994530942; |
|---|
| 132 | const double INVLN_2 = 1.0 / LN_2; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** return the minimum of two values, equivalent to std::min. |
|---|
| 136 | * std::min not used because of STL implementation under IRIX not |
|---|
| 137 | * containing std::min. |
|---|
| 138 | */ |
|---|
| 139 | template<typename T> |
|---|
| 140 | inline T absolute(T v) { return v<(T)0?-v:v; } |
|---|
| 141 | |
|---|
| 142 | /** return true if float lhs and rhs are equivalent, |
|---|
| 143 | * meaning that the difference between them is less than an epsilon value |
|---|
| 144 | * which defaults to 1e-6. |
|---|
| 145 | */ |
|---|
| 146 | inline bool equivalent(float lhs,float rhs,float epsilon=1e-6) |
|---|
| 147 | { float delta = rhs-lhs; return delta<0.0f?delta>=-epsilon:delta<=epsilon; } |
|---|
| 148 | |
|---|
| 149 | /** return true if double lhs and rhs are equivalent, |
|---|
| 150 | * meaning that the difference between them is less than an epsilon value |
|---|
| 151 | * which defaults to 1e-6. |
|---|
| 152 | */ |
|---|
| 153 | inline bool equivalent(double lhs,double rhs,double epsilon=1e-6) |
|---|
| 154 | { double delta = rhs-lhs; return delta<0.0?delta>=-epsilon:delta<=epsilon; } |
|---|
| 155 | |
|---|
| 156 | /** return the minimum of two values, equivalent to std::min. |
|---|
| 157 | * std::min not used because of STL implementation under IRIX not containing |
|---|
| 158 | * std::min. |
|---|
| 159 | */ |
|---|
| 160 | template<typename T> |
|---|
| 161 | inline T minimum(T lhs,T rhs) { return lhs<rhs?lhs:rhs; } |
|---|
| 162 | |
|---|
| 163 | /** return the maximum of two values, equivalent to std::max. |
|---|
| 164 | * std::max not used because of STL implementation under IRIX not containing |
|---|
| 165 | * std::max. |
|---|
| 166 | */ |
|---|
| 167 | template<typename T> |
|---|
| 168 | inline T maximum(T lhs,T rhs) { return lhs>rhs?lhs:rhs; } |
|---|
| 169 | |
|---|
| 170 | template<typename T> |
|---|
| 171 | inline T clampTo(T v,T minimum,T maximum) |
|---|
| 172 | { return v<minimum?minimum:v>maximum?maximum:v; } |
|---|
| 173 | |
|---|
| 174 | template<typename T> |
|---|
| 175 | inline T clampAbove(T v,T minimum) { return v<minimum?minimum:v; } |
|---|
| 176 | |
|---|
| 177 | template<typename T> |
|---|
| 178 | inline T clampBelow(T v,T maximum) { return v>maximum?maximum:v; } |
|---|
| 179 | |
|---|
| 180 | template<typename T> |
|---|
| 181 | inline T clampBetween(T v,T minimum, T maximum) |
|---|
| 182 | { return clampBelow(clampAbove(v,minimum),maximum); } |
|---|
| 183 | |
|---|
| 184 | template<typename T> |
|---|
| 185 | inline T sign(T v) { return v<(T)0?(T)-1:(T)1; } |
|---|
| 186 | |
|---|
| 187 | template<typename T> |
|---|
| 188 | inline T signOrZero(T v) { return v<(T)0 ? (T)-1 : ( v>(T)0 ? (T)1 : 0 ); } |
|---|
| 189 | |
|---|
| 190 | template<typename T> |
|---|
| 191 | inline T square(T v) { return v*v; } |
|---|
| 192 | |
|---|
| 193 | template<typename T> |
|---|
| 194 | inline T signedSquare(T v) { return v<(T)0?-v*v:v*v;; } |
|---|
| 195 | |
|---|
| 196 | inline float inDegrees(float angle) { return angle*(float)PI/180.0f; } |
|---|
| 197 | inline double inDegrees(double angle) { return angle*PI/180.0; } |
|---|
| 198 | |
|---|
| 199 | template<typename T> |
|---|
| 200 | inline T inRadians(T angle) { return angle; } |
|---|
| 201 | |
|---|
| 202 | inline float DegreesToRadians(float angle) { return angle*(float)PI/180.0f; } |
|---|
| 203 | inline double DegreesToRadians(double angle) { return angle*PI/180.0; } |
|---|
| 204 | |
|---|
| 205 | inline float RadiansToDegrees(float angle) { return angle*180.0f/(float)PI; } |
|---|
| 206 | inline double RadiansToDegrees(double angle) { return angle*180.0/PI; } |
|---|
| 207 | |
|---|
| 208 | inline float round(float v) { return v>=0.0f?floorf(v+0.5f):ceilf(v-0.5f); } |
|---|
| 209 | inline double round(double v) { return v>=0.0?floor(v+0.5):ceil(v-0.5); } |
|---|
| 210 | |
|---|
| 211 | #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MWERKS__) |
|---|
| 212 | inline bool isNaN(float v) { return _isnan(v)!=0; } |
|---|
| 213 | inline bool isNaN(double v) { return _isnan(v)!=0; } |
|---|
| 214 | #else |
|---|
| 215 | #if defined(__APPLE__) |
|---|
| 216 | inline bool isNaN(float v) { return std::isnan(v); } |
|---|
| 217 | inline bool isNaN(double v) { return std::isnan(v); } |
|---|
| 218 | #else |
|---|
| 219 | // Need to use to std::isnan to avoid undef problem from <cmath> |
|---|
| 220 | inline bool isNaN(float v) { return isnan(v); } |
|---|
| 221 | inline bool isNaN(double v) { return isnan(v); } |
|---|
| 222 | #endif |
|---|
| 223 | #endif |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | /** compute the volume of a tetrahedron. */ |
|---|
| 227 | template<typename T> |
|---|
| 228 | inline float computeVolume(const T& a,const T& b,const T& c,const T& d) |
|---|
| 229 | { |
|---|
| 230 | return fabsf(((b-c)^(a-b))*(d-b)); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** compute the volume of a prism. */ |
|---|
| 234 | template<typename T> |
|---|
| 235 | inline float computeVolume(const T& f1,const T& f2,const T& f3, |
|---|
| 236 | const T& b1,const T& b2,const T& b3) |
|---|
| 237 | { |
|---|
| 238 | return computeVolume(f1,f2,f3,b1)+ |
|---|
| 239 | computeVolume(b1,b2,b3,f2)+ |
|---|
| 240 | computeVolume(b1,b3,f2,f3); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | /** Convert a ascii number to a double, ignoring locale settings.*/ |
|---|
| 244 | extern OSG_EXPORT double asciiToDouble(const char* str); |
|---|
| 245 | |
|---|
| 246 | /** Convert a ascii number to a float, ignoring locale settings.*/ |
|---|
| 247 | inline float asciiToFloat(const char* str) { return static_cast<float>(asciiToDouble(str)); } |
|---|
| 248 | |
|---|
| 249 | /** Detect first ascii POSITIVE number in string and convert to double.*/ |
|---|
| 250 | extern OSG_EXPORT double findAsciiToDouble(const char* str); |
|---|
| 251 | |
|---|
| 252 | /** Detect first ascii POSITIVE number in string and convert to double.*/ |
|---|
| 253 | inline float findAsciiToFloat(const char* str) { return static_cast<float>(findAsciiToDouble(str)); } |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | #endif // __OSG_MATH |
|---|
| 259 | |
|---|