MathMisc.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. *
  3. * Mathematics Subpackage (VrMath)
  4. *
  5. *
  6. * Author: Samuel R. Buss, [email protected].
  7. * Web page: http://math.ucsd.edu/~sbuss/MathCG
  8. *
  9. *
  10. This software is provided 'as-is', without any express or implied warranty.
  11. In no event will the authors be held liable for any damages arising from the use of this software.
  12. Permission is granted to anyone to use this software for any purpose,
  13. including commercial applications, and to alter it and redistribute it freely,
  14. subject to the following restrictions:
  15. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. *
  19. *
  20. */
  21. #ifndef MATH_MISC_H
  22. #define MATH_MISC_H
  23. #include <math.h>
  24. //
  25. // Commonly used constants
  26. //
  27. const double PI = 3.1415926535897932384626433832795028841972;
  28. const double PI2 = 2.0*PI;
  29. const double PI4 = 4.0*PI;
  30. const double PISq = PI*PI;
  31. const double PIhalves = 0.5*PI;
  32. const double PIthirds = PI/3.0;
  33. const double PItwothirds = PI2/3.0;
  34. const double PIfourths = 0.25*PI;
  35. const double PIsixths = PI/6.0;
  36. const double PIsixthsSq = PIsixths*PIsixths;
  37. const double PItwelfths = PI/12.0;
  38. const double PItwelfthsSq = PItwelfths*PItwelfths;
  39. const double PIinv = 1.0/PI;
  40. const double PI2inv = 0.5/PI;
  41. const double PIhalfinv = 2.0/PI;
  42. const double RadiansToDegrees = 180.0/PI;
  43. const double DegreesToRadians = PI/180;
  44. const double OneThird = 1.0/3.0;
  45. const double TwoThirds = 2.0/3.0;
  46. const double OneSixth = 1.0/6.0;
  47. const double OneEighth = 1.0/8.0;
  48. const double OneTwelfth = 1.0/12.0;
  49. const double Root2 = sqrt(2.0);
  50. const double Root3 = sqrt(3.0);
  51. const double Root2Inv = 1.0/Root2; // sqrt(2)/2
  52. const double HalfRoot3 = sqrtf(3)/2.0;
  53. const double LnTwo = log(2.0);
  54. const double LnTwoInv = 1.0/log(2.0);
  55. // Special purpose constants
  56. const double OnePlusEpsilon15 = 1.0+1.0e-15;
  57. const double OneMinusEpsilon15 = 1.0-1.0e-15;
  58. inline double ZeroValue(const double& x)
  59. {
  60. return 0.0;
  61. }
  62. //
  63. // Comparisons
  64. //
  65. template<class T> inline T Min ( T x, T y )
  66. {
  67. return (x<y ? x : y);
  68. }
  69. template<class T> inline T Max ( T x, T y )
  70. {
  71. return (y<x ? x : y);
  72. }
  73. template<class T> inline T ClampRange ( T x, T min, T max)
  74. {
  75. if ( x<min ) {
  76. return min;
  77. }
  78. if ( x>max ) {
  79. return max;
  80. }
  81. return x;
  82. }
  83. template<class T> inline bool ClampRange ( T *x, T min, T max)
  84. {
  85. if ( (*x)<min ) {
  86. (*x) = min;
  87. return false;
  88. }
  89. else if ( (*x)>max ) {
  90. (*x) = max;
  91. return false;
  92. }
  93. else {
  94. return true;
  95. }
  96. }
  97. template<class T> inline bool ClampMin ( T *x, T min)
  98. {
  99. if ( (*x)<min ) {
  100. (*x) = min;
  101. return false;
  102. }
  103. return true;
  104. }
  105. template<class T> inline bool ClampMax ( T *x, T max)
  106. {
  107. if ( (*x)>max ) {
  108. (*x) = max;
  109. return false;
  110. }
  111. return true;
  112. }
  113. template<class T> inline T& UpdateMin ( const T& x, T& y )
  114. {
  115. if ( x<y ) {
  116. y = x;
  117. }
  118. return y;
  119. }
  120. template<class T> inline T& UpdateMax ( const T& x, T& y )
  121. {
  122. if ( x>y ) {
  123. y = x;
  124. }
  125. return y;
  126. }
  127. template<class T> inline bool SameSignNonzero( T x, T y )
  128. {
  129. if ( x<0 ) {
  130. return (y<0);
  131. }
  132. else if ( 0<x ) {
  133. return (0<y);
  134. }
  135. else {
  136. return false;
  137. }
  138. }
  139. inline double Mag ( double x ) {
  140. return fabs(x);
  141. }
  142. inline double Dist ( double x, double y ) {
  143. return fabs(x-y);
  144. }
  145. template <class T>
  146. inline bool NearEqual( T a, T b, double tolerance ) {
  147. a -= b;
  148. return ( Mag(a)<=tolerance );
  149. }
  150. inline bool EqualZeroFuzzy( double x ) {
  151. return ( fabs(x)<=1.0e-14 );
  152. }
  153. inline bool NearZero( double x, double tolerance ) {
  154. return ( fabs(x)<=tolerance );
  155. }
  156. inline bool LessOrEqualFuzzy( double x, double y )
  157. {
  158. if ( x <= y ) {
  159. return true;
  160. }
  161. if ( y > 0.0 ) {
  162. if ( x>0.0 ) {
  163. return ( x*OneMinusEpsilon15 < y*OnePlusEpsilon15 );
  164. }
  165. else {
  166. return ( y<1.0e-15 ); // x==0 in this case
  167. }
  168. }
  169. else if ( y < 0.0 ) {
  170. if ( x<0.0 ) {
  171. return ( x*OnePlusEpsilon15 < y*OneMinusEpsilon15 );
  172. }
  173. else {
  174. return ( y>-1.0e-15 ); // x==0 in this case
  175. }
  176. }
  177. else {
  178. return ( -1.0e-15<x && x<1.0e-15 );
  179. }
  180. }
  181. inline bool GreaterOrEqualFuzzy ( double x, double y )
  182. {
  183. return LessOrEqualFuzzy( y, x );
  184. }
  185. inline bool UpdateMaxAbs( double *maxabs, double updateval )
  186. {
  187. if ( updateval > *maxabs ) {
  188. *maxabs = updateval;
  189. return true;
  190. }
  191. else if ( -updateval > *maxabs ) {
  192. *maxabs = -updateval;
  193. return true;
  194. }
  195. else {
  196. return false;
  197. }
  198. }
  199. // **********************************************************
  200. // Combinations and averages. *
  201. // **********************************************************
  202. template <class T>
  203. void averageOf ( const T& a, const T &b, T&c ) {
  204. c = a;
  205. c += b;
  206. c *= 0.5;
  207. }
  208. template <class T>
  209. void Lerp( const T& a, const T&b, double alpha, T&c ) {
  210. double beta = 1.0-alpha;
  211. if ( beta>alpha ) {
  212. c = b;
  213. c *= alpha/beta;
  214. c += a;
  215. c *= beta;
  216. }
  217. else {
  218. c = a;
  219. c *= beta/alpha;
  220. c += b;
  221. c *= alpha;
  222. }
  223. }
  224. template <class T>
  225. T Lerp( const T& a, const T&b, double alpha ) {
  226. T ret;
  227. Lerp( a, b, alpha, ret );
  228. return ret;
  229. }
  230. // **********************************************************
  231. // Trigonometry *
  232. // **********************************************************
  233. // TimesCot(x) returns x*cot(x)
  234. inline double TimesCot ( double x ) {
  235. if ( -1.0e-5 < x && x < 1.0e-5 ) {
  236. return 1.0+x*OneThird;
  237. }
  238. else {
  239. return ( x*cos(x)/sin(x) );
  240. }
  241. }
  242. // SineOver(x) returns sin(x)/x.
  243. inline double SineOver( double x ) {
  244. if ( -1.0e-5 < x && x < 1.0e-5 ) {
  245. return 1.0-x*x*OneSixth;
  246. }
  247. else {
  248. return sin(x)/x;
  249. }
  250. }
  251. // OverSine(x) returns x/sin(x).
  252. inline double OverSine( double x ) {
  253. if ( -1.0e-5 < x && x < 1.0e-5 ) {
  254. return 1.0+x*x*OneSixth;
  255. }
  256. else {
  257. return x/sin(x);
  258. }
  259. }
  260. inline double SafeAsin( double x ) {
  261. if ( x <= -1.0 ) {
  262. return -PIhalves;
  263. }
  264. else if ( x >= 1.0 ) {
  265. return PIhalves;
  266. }
  267. else {
  268. return asin(x);
  269. }
  270. }
  271. inline double SafeAcos( double x ) {
  272. if ( x <= -1.0 ) {
  273. return PI;
  274. }
  275. else if ( x >= 1.0 ) {
  276. return 0.0;
  277. }
  278. else {
  279. return acos(x);
  280. }
  281. }
  282. // **********************************************************************
  283. // Roots and powers *
  284. // **********************************************************************
  285. // Square(x) returns x*x, of course!
  286. template<class T> inline T Square ( T x )
  287. {
  288. return (x*x);
  289. }
  290. // Cube(x) returns x*x*x, of course!
  291. template<class T> inline T Cube ( T x )
  292. {
  293. return (x*x*x);
  294. }
  295. // SafeSqrt(x) = returns sqrt(max(x, 0.0));
  296. inline double SafeSqrt( double x ) {
  297. if ( x<=0.0 ) {
  298. return 0.0;
  299. }
  300. else {
  301. return sqrt(x);
  302. }
  303. }
  304. // SignedSqrt(a, s) returns (sign(s)*sqrt(a)).
  305. inline double SignedSqrt( double a, double sgn )
  306. {
  307. if ( sgn==0.0 ) {
  308. return 0.0;
  309. }
  310. else {
  311. return ( sgn>0.0 ? sqrt(a) : -sqrt(a) );
  312. }
  313. }
  314. // Template version of Sign function
  315. template<class T> inline int Sign( T x)
  316. {
  317. if ( x<0 ) {
  318. return -1;
  319. }
  320. else if ( x==0 ) {
  321. return 0;
  322. }
  323. else {
  324. return 1;
  325. }
  326. }
  327. #endif // #ifndef MATH_MISC_H