Math 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef __OSG_MATH
  14. #define __OSG_MATH
  15. #include <cmath>
  16. #include <cfloat>
  17. #include <osg/Export>
  18. namespace osg {
  19. // define the standard trig values
  20. #ifdef PI
  21. #undef PI
  22. #undef PI_2
  23. #undef PI_4
  24. #endif
  25. const double PI = 3.14159265358979323846;
  26. const double PI_2 = 1.57079632679489661923;
  27. const double PI_4 = 0.78539816339744830962;
  28. const double LN_2 = 0.69314718055994530942;
  29. const double INVLN_2 = 1.0 / LN_2;
  30. const float PIf = 3.14159265358979323846f;
  31. const float PI_2f = 1.57079632679489661923f;
  32. const float PI_4f = 0.78539816339744830962f;
  33. const float LN_2f = 0.69314718055994530942f;
  34. const float INVLN_2f = 1.0f / LN_2f;
  35. template<typename T>
  36. inline T default_value() { return T(); }
  37. template<> inline float default_value<float>() { return 0.0f; }
  38. template<> inline double default_value<double>() { return 0.0; }
  39. template<> inline char default_value<char>() { return 0; }
  40. template<> inline unsigned char default_value<unsigned char>() { return 0; }
  41. template<> inline short default_value<short>() { return 0; }
  42. template<> inline unsigned short default_value<unsigned short>() { return 0; }
  43. template<> inline int default_value<int>() { return 0; }
  44. template<> inline unsigned int default_value<unsigned int>() { return 0; }
  45. template<typename T>
  46. inline T absolute(T v) { return v<(T)0?-v:v; }
  47. /** return true if float lhs and rhs are equivalent,
  48. * meaning that the difference between them is less than an epsilon value
  49. * which defaults to 1e-6.
  50. */
  51. inline bool equivalent(float lhs,float rhs,float epsilon=1e-6)
  52. { float delta = rhs-lhs; return delta<0.0f?delta>=-epsilon:delta<=epsilon; }
  53. /** return true if double lhs and rhs are equivalent,
  54. * meaning that the difference between them is less than an epsilon value
  55. * which defaults to 1e-6.
  56. */
  57. inline bool equivalent(double lhs,double rhs,double epsilon=1e-6)
  58. { double delta = rhs-lhs; return delta<0.0?delta>=-epsilon:delta<=epsilon; }
  59. /** return the minimum of two values, equivalent to std::min.
  60. * std::min not used because of STL implementation under IRIX not containing
  61. * std::min.
  62. */
  63. template<typename T>
  64. inline T minimum(T lhs,T rhs) { return lhs<rhs?lhs:rhs; }
  65. /** return the maximum of two values, equivalent to std::max.
  66. * std::max not used because of STL implementation under IRIX not containing
  67. * std::max.
  68. */
  69. template<typename T>
  70. inline T maximum(T lhs,T rhs) { return lhs>rhs?lhs:rhs; }
  71. template<typename T>
  72. inline T clampTo(T v,T minimum,T maximum) { return v<minimum?minimum:v>maximum?maximum:v; }
  73. template<typename T>
  74. inline T clampAbove(T v,T minimum) { return v<minimum?minimum:v; }
  75. template<typename T>
  76. inline T clampBelow(T v,T maximum) { return v>maximum?maximum:v; }
  77. template<typename T>
  78. inline T clampBetween(T v,T minimum, T maximum)
  79. { return clampBelow(clampAbove(v,minimum),maximum); }
  80. template<typename T>
  81. inline T sign(T v) { return v<(T)0?(T)-1:(T)1; }
  82. template<typename T>
  83. inline T signOrZero(T v) { return v<(T)0 ? (T)-1 : ( v>(T)0 ? (T)1 : 0 ); }
  84. template<typename T>
  85. inline T square(T v) { return v*v; }
  86. template<typename T>
  87. inline T signedSquare(T v) { return v<(T)0?-v*v:v*v;; }
  88. inline float inDegrees(float angle) { return angle*(float)PI/180.0f; }
  89. inline double inDegrees(double angle) { return angle*PI/180.0; }
  90. template<typename T>
  91. inline T inRadians(T angle) { return angle; }
  92. inline float DegreesToRadians(float angle) { return angle*(float)PI/180.0f; }
  93. inline double DegreesToRadians(double angle) { return angle*PI/180.0; }
  94. inline float RadiansToDegrees(float angle) { return angle*180.0f/(float)PI; }
  95. inline double RadiansToDegrees(double angle) { return angle*180.0/PI; }
  96. inline float round(float v) { return v>=0.0f?floorf(v+0.5f):ceilf(v-0.5f); }
  97. inline double round(double v) { return v>=0.0?floor(v+0.5):ceil(v-0.5); }
  98. #if defined(_MSC_VER)
  99. inline bool isNaN(double v) { return _isnan(v)!=0; }
  100. #elif defined(__ANDROID__)
  101. inline bool isNaN(float v) { return isnan(v); }
  102. inline bool isNaN(double v) { return isnan(v); }
  103. #else
  104. inline bool isNaN(float v) { return std::isnan(v); }
  105. inline bool isNaN(double v) { return std::isnan(v); }
  106. #endif
  107. /** compute the volume of a tetrahedron. */
  108. template<typename T>
  109. inline float computeVolume(const T& a,const T& b,const T& c,const T& d)
  110. {
  111. return fabsf(((b-c)^(a-b))*(d-b));
  112. }
  113. /** compute the volume of a prism. */
  114. template<typename T>
  115. inline float computeVolume(const T& f1,const T& f2,const T& f3,
  116. const T& b1,const T& b2,const T& b3)
  117. {
  118. return computeVolume(f1,f2,f3,b1)+
  119. computeVolume(b1,b2,b3,f2)+
  120. computeVolume(b1,b3,f2,f3);
  121. }
  122. /** Convert a ascii number to a double, ignoring locale settings.*/
  123. extern OSG_EXPORT double asciiToDouble(const char* str);
  124. /** Convert a ascii number to a float, ignoring locale settings.*/
  125. inline float asciiToFloat(const char* str) { return static_cast<float>(asciiToDouble(str)); }
  126. /** Detect first ascii POSITIVE number in string and convert to double.*/
  127. extern OSG_EXPORT double findAsciiToDouble(const char* str);
  128. /** Detect first ascii POSITIVE number in string and convert to double.*/
  129. inline float findAsciiToFloat(const char* str) { return static_cast<float>(findAsciiToDouble(str)); }
  130. }
  131. #endif // __OSG_MATH