2
0

MathFuncs.inl.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "MathCommon.inl.h"
  2. namespace M {
  3. // polynomialSinQuadrant
  4. /// Used in sinCos
  5. inline static float polynomialSinQuadrant(float a)
  6. {
  7. return a * (1.0 + a * a * (-0.16666 + a * a * (0.0083143 - a * a * 0.00018542)));
  8. }
  9. // Sine and Cosine
  10. inline void sinCos(float a, float& sina, float& cosa)
  11. {
  12. bool negative = false;
  13. if(a < 0.0)
  14. {
  15. a = -a;
  16. negative = true;
  17. }
  18. const float kTwoOverPi = 1.0 / (PI/2.0);
  19. float floatA = kTwoOverPi * a;
  20. int intA = (int)floatA;
  21. const float k_rational_half_pi = 201 / 128.0;
  22. const float kRemainderHalfPi = 4.8382679e-4;
  23. floatA = (a - k_rational_half_pi * intA) - kRemainderHalfPi * intA;
  24. float floatAMinusHalfPi = (floatA - k_rational_half_pi) - kRemainderHalfPi;
  25. switch(intA & 3)
  26. {
  27. case 0: // 0 - Pi/2
  28. sina = polynomialSinQuadrant(floatA);
  29. cosa = polynomialSinQuadrant(-floatAMinusHalfPi);
  30. break;
  31. case 1: // Pi/2 - Pi
  32. sina = polynomialSinQuadrant(-floatAMinusHalfPi);
  33. cosa = polynomialSinQuadrant(-floatA);
  34. break;
  35. case 2: // Pi - 3Pi/2
  36. sina = polynomialSinQuadrant(-floatA);
  37. cosa = polynomialSinQuadrant(floatAMinusHalfPi);
  38. break;
  39. case 3: // 3Pi/2 - 2Pi
  40. sina = polynomialSinQuadrant(floatAMinusHalfPi);
  41. cosa = polynomialSinQuadrant(floatA);
  42. break;
  43. };
  44. if(negative)
  45. sina = -sina;
  46. /*RASSERT_THROW_EXCEPTION(!isZero(M::sin(a) - sina));
  47. RASSERT_THROW_EXCEPTION(!isZero(M::cos(a) - cosa));*/
  48. }
  49. //======================================================================================================================
  50. // Small funcs =
  51. //======================================================================================================================
  52. inline float sqrt(float f)
  53. {
  54. #if defined(MATH_INTEL_SIMD)
  55. __m128 mm = _mm_set_ss(f);
  56. mm = _mm_sqrt_ss(mm);
  57. float o;
  58. _mm_store_ss(&o, mm);
  59. return o;
  60. #else
  61. return ::sqrtf(f);
  62. #endif
  63. }
  64. inline float toRad(float degrees)
  65. {
  66. return degrees*(PI/180.0);
  67. }
  68. inline float toDegrees(float rad)
  69. {
  70. return rad*(180.0/PI);
  71. }
  72. inline float sin(float rad)
  73. {
  74. return ::sin(rad);
  75. }
  76. inline float cos(float rad)
  77. {
  78. return ::cos(rad);
  79. }
  80. inline bool isZero(float f)
  81. {
  82. return fabs(f) < EPSILON;
  83. }
  84. // combineTransformations
  85. // mat4(t0,r0,s0)*mat4(t1,r1,s1) == mat4(tf,rf,sf)
  86. inline void combineTransformations(const Vec3& t0, const Mat3& r0, float s0,
  87. const Vec3& t1, const Mat3& r1, float s1,
  88. Vec3& tf, Mat3& rf, float& sf)
  89. {
  90. tf = t1.getTransformed(t0, r0, s0);
  91. rf = r0 * r1;
  92. sf = s0 * s1;
  93. }
  94. // combineTransformations as the above but without scale
  95. inline void combineTransformations(const Vec3& t0, const Mat3& r0, const Vec3& t1, const Mat3& r1, Vec3& tf, Mat3& rf)
  96. {
  97. tf = t1.getTransformed(t0, r0);
  98. rf = r0 * r1;
  99. }
  100. } // end namespace