9
3

fast_trigonometry.inl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /// @ref gtx_fast_trigonometry
  2. namespace glm{
  3. namespace detail
  4. {
  5. template<length_t L, typename T, qualifier Q>
  6. GLM_FUNC_QUALIFIER vec<L, T, Q> taylorCos(vec<L, T, Q> const& x)
  7. {
  8. return static_cast<T>(1)
  9. - (x * x) * (1.f / 2.f)
  10. + ((x * x) * (x * x)) * (1.f / 24.f)
  11. - (((x * x) * (x * x)) * (x * x)) * (1.f / 720.f)
  12. + (((x * x) * (x * x)) * ((x * x) * (x * x))) * (1.f / 40320.f);
  13. }
  14. template<typename T>
  15. GLM_FUNC_QUALIFIER T cos_52s(T x)
  16. {
  17. T const xx(x * x);
  18. return (T(0.9999932946) + xx * (T(-0.4999124376) + xx * (T(0.0414877472) + xx * T(-0.0012712095))));
  19. }
  20. template<length_t L, typename T, qualifier Q>
  21. GLM_FUNC_QUALIFIER vec<L, T, Q> cos_52s(vec<L, T, Q> const& x)
  22. {
  23. return detail::functor1<vec, L, T, T, Q>::call(cos_52s, x);
  24. }
  25. }//namespace detail
  26. // wrapAngle
  27. template<typename T>
  28. GLM_FUNC_QUALIFIER T wrapAngle(T angle)
  29. {
  30. return abs<T>(mod<T>(angle, two_pi<T>()));
  31. }
  32. template<length_t L, typename T, qualifier Q>
  33. GLM_FUNC_QUALIFIER vec<L, T, Q> wrapAngle(vec<L, T, Q> const& x)
  34. {
  35. return detail::functor1<vec, L, T, T, Q>::call(wrapAngle, x);
  36. }
  37. // cos
  38. template<typename T>
  39. GLM_FUNC_QUALIFIER T fastCos(T x)
  40. {
  41. T const angle(wrapAngle<T>(x));
  42. if(angle < half_pi<T>())
  43. return detail::cos_52s(angle);
  44. if(angle < pi<T>())
  45. return -detail::cos_52s(pi<T>() - angle);
  46. if(angle < (T(3) * half_pi<T>()))
  47. return -detail::cos_52s(angle - pi<T>());
  48. return detail::cos_52s(two_pi<T>() - angle);
  49. }
  50. template<length_t L, typename T, qualifier Q>
  51. GLM_FUNC_QUALIFIER vec<L, T, Q> fastCos(vec<L, T, Q> const& x)
  52. {
  53. return detail::functor1<vec, L, T, T, Q>::call(fastCos, x);
  54. }
  55. // sin
  56. template<typename T>
  57. GLM_FUNC_QUALIFIER T fastSin(T x)
  58. {
  59. return fastCos<T>(half_pi<T>() - x);
  60. }
  61. template<length_t L, typename T, qualifier Q>
  62. GLM_FUNC_QUALIFIER vec<L, T, Q> fastSin(vec<L, T, Q> const& x)
  63. {
  64. return detail::functor1<vec, L, T, T, Q>::call(fastSin, x);
  65. }
  66. // tan
  67. template<typename T>
  68. GLM_FUNC_QUALIFIER T fastTan(T x)
  69. {
  70. return x + (x * x * x * T(0.3333333333)) + (x * x * x * x * x * T(0.1333333333333)) + (x * x * x * x * x * x * x * T(0.0539682539));
  71. }
  72. template<length_t L, typename T, qualifier Q>
  73. GLM_FUNC_QUALIFIER vec<L, T, Q> fastTan(vec<L, T, Q> const& x)
  74. {
  75. return detail::functor1<vec, L, T, T, Q>::call(fastTan, x);
  76. }
  77. // asin
  78. template<typename T>
  79. GLM_FUNC_QUALIFIER T fastAsin(T x)
  80. {
  81. return x + (x * x * x * T(0.166666667)) + (x * x * x * x * x * T(0.075)) + (x * x * x * x * x * x * x * T(0.0446428571)) + (x * x * x * x * x * x * x * x * x * T(0.0303819444));// + (x * x * x * x * x * x * x * x * x * x * x * T(0.022372159));
  82. }
  83. template<length_t L, typename T, qualifier Q>
  84. GLM_FUNC_QUALIFIER vec<L, T, Q> fastAsin(vec<L, T, Q> const& x)
  85. {
  86. return detail::functor1<vec, L, T, T, Q>::call(fastAsin, x);
  87. }
  88. // acos
  89. template<typename T>
  90. GLM_FUNC_QUALIFIER T fastAcos(T x)
  91. {
  92. return T(1.5707963267948966192313216916398) - fastAsin(x); //(PI / 2)
  93. }
  94. template<length_t L, typename T, qualifier Q>
  95. GLM_FUNC_QUALIFIER vec<L, T, Q> fastAcos(vec<L, T, Q> const& x)
  96. {
  97. return detail::functor1<vec, L, T, T, Q>::call(fastAcos, x);
  98. }
  99. // atan
  100. template<typename T>
  101. GLM_FUNC_QUALIFIER T fastAtan(T y, T x)
  102. {
  103. T sgn = sign(y) * sign(x);
  104. return abs(fastAtan(y / x)) * sgn;
  105. }
  106. template<length_t L, typename T, qualifier Q>
  107. GLM_FUNC_QUALIFIER vec<L, T, Q> fastAtan(vec<L, T, Q> const& y, vec<L, T, Q> const& x)
  108. {
  109. return detail::functor2<vec, L, T, Q>::call(fastAtan, y, x);
  110. }
  111. template<typename T>
  112. GLM_FUNC_QUALIFIER T fastAtan(T x)
  113. {
  114. return x - (x * x * x * T(0.333333333333)) + (x * x * x * x * x * T(0.2)) - (x * x * x * x * x * x * x * T(0.1428571429)) + (x * x * x * x * x * x * x * x * x * T(0.111111111111)) - (x * x * x * x * x * x * x * x * x * x * x * T(0.0909090909));
  115. }
  116. template<length_t L, typename T, qualifier Q>
  117. GLM_FUNC_QUALIFIER vec<L, T, Q> fastAtan(vec<L, T, Q> const& x)
  118. {
  119. return detail::functor1<vec, L, T, T, Q>::call(fastAtan, x);
  120. }
  121. }//namespace glm