quaternion_exponential.inl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. namespace glm
  2. {
  3. template<typename T, qualifier Q>
  4. GLM_FUNC_QUALIFIER qua<T, Q> exp(qua<T, Q> const& q)
  5. {
  6. vec<3, T, Q> u(q.x, q.y, q.z);
  7. T const Angle = glm::length(u);
  8. if (Angle < epsilon<T>())
  9. return qua<T, Q>();
  10. vec<3, T, Q> const v(u / Angle);
  11. return qua<T, Q>(cos(Angle), sin(Angle) * v);
  12. }
  13. template<typename T, qualifier Q>
  14. GLM_FUNC_QUALIFIER qua<T, Q> log(qua<T, Q> const& q)
  15. {
  16. vec<3, T, Q> u(q.x, q.y, q.z);
  17. T Vec3Len = length(u);
  18. if (Vec3Len < epsilon<T>())
  19. {
  20. if(q.w > static_cast<T>(0))
  21. return qua<T, Q>(log(q.w), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
  22. else if(q.w < static_cast<T>(0))
  23. return qua<T, Q>(log(-q.w), pi<T>(), static_cast<T>(0), static_cast<T>(0));
  24. else
  25. return qua<T, Q>(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity());
  26. }
  27. else
  28. {
  29. T t = atan(Vec3Len, T(q.w)) / Vec3Len;
  30. T QuatLen2 = Vec3Len * Vec3Len + q.w * q.w;
  31. return qua<T, Q>(static_cast<T>(0.5) * log(QuatLen2), t * q.x, t * q.y, t * q.z);
  32. }
  33. }
  34. template<typename T, qualifier Q>
  35. GLM_FUNC_QUALIFIER qua<T, Q> pow(qua<T, Q> const& x, T y)
  36. {
  37. //Raising to the power of 0 should yield 1
  38. //Needed to prevent a division by 0 error later on
  39. if(y > -epsilon<T>() && y < epsilon<T>())
  40. return qua<T, Q>(1,0,0,0);
  41. //To deal with non-unit quaternions
  42. T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w);
  43. //Equivalent to raising a real number to a power
  44. //Needed to prevent a division by 0 error later on
  45. if(abs(x.w / magnitude) > static_cast<T>(1) - epsilon<T>() && abs(x.w / magnitude) < static_cast<T>(1) + epsilon<T>())
  46. return qua<T, Q>(pow(x.w, y), 0, 0, 0);
  47. T Angle = acos(x.w / magnitude);
  48. T NewAngle = Angle * y;
  49. T Div = sin(NewAngle) / sin(Angle);
  50. T Mag = pow(magnitude, y - static_cast<T>(1));
  51. return qua<T, Q>(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag);
  52. }
  53. template<typename T, qualifier Q>
  54. GLM_FUNC_QUALIFIER qua<T, Q> sqrt(qua<T, Q> const& x)
  55. {
  56. return pow(x, static_cast<T>(0.5));
  57. }
  58. }//namespace glm