1/// @ref gtx_fast_trigonometry 2/// @file glm/gtx/fast_trigonometry.inl 3 4namespace glm{ 5namespace detail 6{ 7 template <typename T, precision P, template <typename, precision> class vecType> 8 GLM_FUNC_QUALIFIER vecType<T, P> taylorCos(vecType<T, P> const & x) 9 { 10 return static_cast<T>(1) 11 - (x * x) / 2.f 12 + (x * x * x * x) / 24.f 13 - (x * x * x * x * x * x) / 720.f 14 + (x * x * x * x * x * x * x * x) / 40320.f; 15 } 16 17 template <typename T> 18 GLM_FUNC_QUALIFIER T cos_52s(T x) 19 { 20 T const xx(x * x); 21 return (T(0.9999932946) + xx * (T(-0.4999124376) + xx * (T(0.0414877472) + xx * T(-0.0012712095)))); 22 } 23 24 template <typename T, precision P, template <typename, precision> class vecType> 25 GLM_FUNC_QUALIFIER vecType<T, P> cos_52s(vecType<T, P> const & x) 26 { 27 return detail::functor1<T, T, P, vecType>::call(cos_52s, x); 28 } 29}//namespace detail 30 31 // wrapAngle 32 template <typename T> 33 GLM_FUNC_QUALIFIER T wrapAngle(T angle) 34 { 35 return abs<T>(mod<T>(angle, two_pi<T>())); 36 } 37 38 template <typename T, precision P, template <typename, precision> class vecType> 39 GLM_FUNC_QUALIFIER vecType<T, P> wrapAngle(vecType<T, P> const & x) 40 { 41 return detail::functor1<T, T, P, vecType>::call(wrapAngle, x); 42 } 43 44 // cos 45 template <typename T> 46 GLM_FUNC_QUALIFIER T fastCos(T x) 47 { 48 T const angle(wrapAngle<T>(x)); 49 50 if(angle < half_pi<T>()) 51 return detail::cos_52s(angle); 52 if(angle < pi<T>()) 53 return -detail::cos_52s(pi<T>() - angle); 54 if(angle < (T(3) * half_pi<T>())) 55 return -detail::cos_52s(angle - pi<T>()); 56 57 return detail::cos_52s(two_pi<T>() - angle); 58 } 59 60 template <typename T, precision P, template <typename, precision> class vecType> 61 GLM_FUNC_QUALIFIER vecType<T, P> fastCos(vecType<T, P> const & x) 62 { 63 return detail::functor1<T, T, P, vecType>::call(fastCos, x); 64 } 65 66 // sin 67 template <typename T> 68 GLM_FUNC_QUALIFIER T fastSin(T x) 69 { 70 return fastCos<T>(half_pi<T>() - x); 71 } 72 73 template <typename T, precision P, template <typename, precision> class vecType> 74 GLM_FUNC_QUALIFIER vecType<T, P> fastSin(vecType<T, P> const & x) 75 { 76 return detail::functor1<T, T, P, vecType>::call(fastSin, x); 77 } 78 79 // tan 80 template <typename T> 81 GLM_FUNC_QUALIFIER T fastTan(T x) 82 { 83 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)); 84 } 85 86 template <typename T, precision P, template <typename, precision> class vecType> 87 GLM_FUNC_QUALIFIER vecType<T, P> fastTan(vecType<T, P> const & x) 88 { 89 return detail::functor1<T, T, P, vecType>::call(fastTan, x); 90 } 91 92 // asin 93 template <typename T> 94 GLM_FUNC_QUALIFIER T fastAsin(T x) 95 { 96 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)); 97 } 98 99 template <typename T, precision P, template <typename, precision> class vecType> 100 GLM_FUNC_QUALIFIER vecType<T, P> fastAsin(vecType<T, P> const & x) 101 { 102 return detail::functor1<T, T, P, vecType>::call(fastAsin, x); 103 } 104 105 // acos 106 template <typename T> 107 GLM_FUNC_QUALIFIER T fastAcos(T x) 108 { 109 return T(1.5707963267948966192313216916398) - fastAsin(x); //(PI / 2) 110 } 111 112 template <typename T, precision P, template <typename, precision> class vecType> 113 GLM_FUNC_QUALIFIER vecType<T, P> fastAcos(vecType<T, P> const & x) 114 { 115 return detail::functor1<T, T, P, vecType>::call(fastAcos, x); 116 } 117 118 // atan 119 template <typename T> 120 GLM_FUNC_QUALIFIER T fastAtan(T y, T x) 121 { 122 T sgn = sign(y) * sign(x); 123 return abs(fastAtan(y / x)) * sgn; 124 } 125 126 template <typename T, precision P, template <typename, precision> class vecType> 127 GLM_FUNC_QUALIFIER vecType<T, P> fastAtan(vecType<T, P> const & y, vecType<T, P> const & x) 128 { 129 return detail::functor2<T, P, vecType>::call(fastAtan, y, x); 130 } 131 132 template <typename T> 133 GLM_FUNC_QUALIFIER T fastAtan(T x) 134 { 135 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)); 136 } 137 138 template <typename T, precision P, template <typename, precision> class vecType> 139 GLM_FUNC_QUALIFIER vecType<T, P> fastAtan(vecType<T, P> const & x) 140 { 141 return detail::functor1<T, T, P, vecType>::call(fastAtan, x); 142 } 143}//namespace glm 144