9
3

quaternion_exponential.inl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if (glm::abs(VectorMagnitude - static_cast<T>(0)) < glm::epsilon<T>()) {
  52. //Equivalent to raising a real number to a power
  53. return qua<T, Q>(pow(x.w, y), 0, 0, 0);
  54. }
  55. Angle = asin(sqrt(VectorMagnitude) / magnitude);
  56. }
  57. else
  58. {
  59. //Scalar component is small, shouldn't cause loss of precision
  60. Angle = acos(x.w / magnitude);
  61. }
  62. T NewAngle = Angle * y;
  63. T Div = sin(NewAngle) / sin(Angle);
  64. T Mag = pow(magnitude, y - static_cast<T>(1));
  65. return qua<T, Q>(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag);
  66. }
  67. template<typename T, qualifier Q>
  68. GLM_FUNC_QUALIFIER qua<T, Q> sqrt(qua<T, Q> const& x)
  69. {
  70. return pow(x, static_cast<T>(0.5));
  71. }
  72. }//namespace glm