quaternion_exponential.inl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "scalar_constants.hpp"
  2. namespace glm
  3. {
  4. template<typename T, qualifier Q>
  5. GLM_FUNC_QUALIFIER qua<T, Q> exp(qua<T, Q> const& q)
  6. {
  7. vec<3, T, Q> u(q.x, q.y, q.z);
  8. T const Angle = glm::length(u);
  9. if (Angle < epsilon<T>())
  10. return qua<T, Q>();
  11. vec<3, T, Q> const v(u / Angle);
  12. return qua<T, Q>(cos(Angle), sin(Angle) * v);
  13. }
  14. template<typename T, qualifier Q>
  15. GLM_FUNC_QUALIFIER qua<T, Q> log(qua<T, Q> const& q)
  16. {
  17. vec<3, T, Q> u(q.x, q.y, q.z);
  18. T Vec3Len = length(u);
  19. if (Vec3Len < epsilon<T>())
  20. {
  21. if(q.w > static_cast<T>(0))
  22. return qua<T, Q>(log(q.w), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
  23. else if(q.w < static_cast<T>(0))
  24. return qua<T, Q>(log(-q.w), pi<T>(), static_cast<T>(0), static_cast<T>(0));
  25. else
  26. return qua<T, Q>(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity());
  27. }
  28. else
  29. {
  30. T t = atan(Vec3Len, T(q.w)) / Vec3Len;
  31. T QuatLen2 = Vec3Len * Vec3Len + q.w * q.w;
  32. return qua<T, Q>(static_cast<T>(0.5) * log(QuatLen2), t * q.x, t * q.y, t * q.z);
  33. }
  34. }
  35. template<typename T, qualifier Q>
  36. GLM_FUNC_QUALIFIER qua<T, Q> pow(qua<T, Q> const& x, T y)
  37. {
  38. //Raising to the power of 0 should yield 1
  39. //Needed to prevent a division by 0 error later on
  40. if(y > -epsilon<T>() && y < epsilon<T>())
  41. return qua<T, Q>(1,0,0,0);
  42. //To deal with non-unit quaternions
  43. T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w);
  44. T Angle;
  45. if(abs(x.w / magnitude) > cos_one_over_two<T>())
  46. {
  47. //Scalar component is close to 1; using it to recover angle would lose precision
  48. //Instead, we use the non-scalar components since sin() is accurate around 0
  49. //Prevent a division by 0 error later on
  50. T VectorMagnitude = x.x * x.x + x.y * x.y + x.z * x.z;
  51. //Despite the compiler might say, we actually want to compare
  52. //VectorMagnitude to 0. here; we could use denorm_int() compiling a
  53. //project with unsafe maths optimizations might make the comparison
  54. //always false, even when VectorMagnitude is 0.
  55. if (VectorMagnitude < std::numeric_limits<T>::min()) {
  56. //Equivalent to raising a real number to a power
  57. return qua<T, Q>(pow(x.w, y), 0, 0, 0);
  58. }
  59. Angle = asin(sqrt(VectorMagnitude) / magnitude);
  60. }
  61. else
  62. {
  63. //Scalar component is small, shouldn't cause loss of precision
  64. Angle = acos(x.w / magnitude);
  65. }
  66. T NewAngle = Angle * y;
  67. T Div = sin(NewAngle) / sin(Angle);
  68. T Mag = pow(magnitude, y - static_cast<T>(1));
  69. return qua<T, Q>(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag);
  70. }
  71. template<typename T, qualifier Q>
  72. GLM_FUNC_QUALIFIER qua<T, Q> sqrt(qua<T, Q> const& x)
  73. {
  74. return pow(x, static_cast<T>(0.5));
  75. }
  76. }//namespace glm