Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.0KB

  1. /// @ref gtx_quaternion
  2. /// @file glm/gtx/quaternion.inl
  3. #include <limits>
  4. #include "../gtc/constants.hpp"
  5. namespace glm
  6. {
  7. template<typename T, qualifier Q>
  8. GLM_FUNC_QUALIFIER tquat<T, Q> quat_identity()
  9. {
  10. return tquat<T, Q>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
  11. }
  12. template<typename T, qualifier Q>
  13. GLM_FUNC_QUALIFIER vec<3, T, Q> cross(vec<3, T, Q> const& v, tquat<T, Q> const& q)
  14. {
  15. return inverse(q) * v;
  16. }
  17. template<typename T, qualifier Q>
  18. GLM_FUNC_QUALIFIER vec<3, T, Q> cross(tquat<T, Q> const& q, vec<3, T, Q> const& v)
  19. {
  20. return q * v;
  21. }
  22. template<typename T, qualifier Q>
  23. GLM_FUNC_QUALIFIER tquat<T, Q> squad
  24. (
  25. tquat<T, Q> const& q1,
  26. tquat<T, Q> const& q2,
  27. tquat<T, Q> const& s1,
  28. tquat<T, Q> const& s2,
  29. T const& h)
  30. {
  31. return mix(mix(q1, q2, h), mix(s1, s2, h), static_cast<T>(2) * (static_cast<T>(1) - h) * h);
  32. }
  33. template<typename T, qualifier Q>
  34. GLM_FUNC_QUALIFIER tquat<T, Q> intermediate
  35. (
  36. tquat<T, Q> const& prev,
  37. tquat<T, Q> const& curr,
  38. tquat<T, Q> const& next
  39. )
  40. {
  41. tquat<T, Q> invQuat = inverse(curr);
  42. return exp((log(next + invQuat) + log(prev + invQuat)) / static_cast<T>(-4)) * curr;
  43. }
  44. template<typename T, qualifier Q>
  45. GLM_FUNC_QUALIFIER tquat<T, Q> exp(tquat<T, Q> const& q)
  46. {
  47. vec<3, T, Q> u(q.x, q.y, q.z);
  48. T const Angle = glm::length(u);
  49. if (Angle < epsilon<T>())
  50. return tquat<T, Q>();
  51. vec<3, T, Q> const v(u / Angle);
  52. return tquat<T, Q>(cos(Angle), sin(Angle) * v);
  53. }
  54. template<typename T, qualifier Q>
  55. GLM_FUNC_QUALIFIER tquat<T, Q> log(tquat<T, Q> const& q)
  56. {
  57. vec<3, T, Q> u(q.x, q.y, q.z);
  58. T Vec3Len = length(u);
  59. if (Vec3Len < epsilon<T>())
  60. {
  61. if(q.w > static_cast<T>(0))
  62. return tquat<T, Q>(log(q.w), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0));
  63. else if(q.w < static_cast<T>(0))
  64. return tquat<T, Q>(log(-q.w), pi<T>(), static_cast<T>(0), static_cast<T>(0));
  65. else
  66. return tquat<T, Q>(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity());
  67. }
  68. else
  69. {
  70. T t = atan(Vec3Len, T(q.w)) / Vec3Len;
  71. T QuatLen2 = Vec3Len * Vec3Len + q.w * q.w;
  72. return tquat<T, Q>(static_cast<T>(0.5) * log(QuatLen2), t * q.x, t * q.y, t * q.z);
  73. }
  74. }
  75. template<typename T, qualifier Q>
  76. GLM_FUNC_QUALIFIER tquat<T, Q> pow(tquat<T, Q> const& x, T const& y)
  77. {
  78. //Raising to the power of 0 should yield 1
  79. //Needed to prevent a division by 0 error later on
  80. if(y > -epsilon<T>() && y < epsilon<T>())
  81. return tquat<T, Q>(1,0,0,0);
  82. //To deal with non-unit quaternions
  83. T magnitude = sqrt(x.x * x.x + x.y * x.y + x.z * x.z + x.w *x.w);
  84. //Equivalent to raising a real number to a power
  85. //Needed to prevent a division by 0 error later on
  86. if(abs(x.w / magnitude) > static_cast<T>(1) - epsilon<T>() && abs(x.w / magnitude) < static_cast<T>(1) + epsilon<T>())
  87. return tquat<T, Q>(pow(x.w, y),0,0,0);
  88. T Angle = acos(x.w / magnitude);
  89. T NewAngle = Angle * y;
  90. T Div = sin(NewAngle) / sin(Angle);
  91. T Mag = pow(magnitude, y - static_cast<T>(1));
  92. return tquat<T, Q>(cos(NewAngle) * magnitude * Mag, x.x * Div * Mag, x.y * Div * Mag, x.z * Div * Mag);
  93. }
  94. template<typename T, qualifier Q>
  95. GLM_FUNC_QUALIFIER vec<3, T, Q> rotate(tquat<T, Q> const& q, vec<3, T, Q> const& v)
  96. {
  97. return q * v;
  98. }
  99. template<typename T, qualifier Q>
  100. GLM_FUNC_QUALIFIER vec<4, T, Q> rotate(tquat<T, Q> const& q, vec<4, T, Q> const& v)
  101. {
  102. return q * v;
  103. }
  104. template<typename T, qualifier Q>
  105. GLM_FUNC_QUALIFIER T extractRealComponent(tquat<T, Q> const& q)
  106. {
  107. T w = static_cast<T>(1) - q.x * q.x - q.y * q.y - q.z * q.z;
  108. if(w < T(0))
  109. return T(0);
  110. else
  111. return -sqrt(w);
  112. }
  113. template<typename T, qualifier Q>
  114. GLM_FUNC_QUALIFIER T length2(tquat<T, Q> const& q)
  115. {
  116. return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
  117. }
  118. template<typename T, qualifier Q>
  119. GLM_FUNC_QUALIFIER tquat<T, Q> shortMix(tquat<T, Q> const& x, tquat<T, Q> const& y, T const& a)
  120. {
  121. if(a <= static_cast<T>(0)) return x;
  122. if(a >= static_cast<T>(1)) return y;
  123. T fCos = dot(x, y);
  124. tquat<T, Q> y2(y); //BUG!!! tquat<T> y2;
  125. if(fCos < static_cast<T>(0))
  126. {
  127. y2 = -y;
  128. fCos = -fCos;
  129. }
  130. //if(fCos > 1.0f) // problem
  131. T k0, k1;
  132. if(fCos > (static_cast<T>(1) - epsilon<T>()))
  133. {
  134. k0 = static_cast<T>(1) - a;
  135. k1 = static_cast<T>(0) + a; //BUG!!! 1.0f + a;
  136. }
  137. else
  138. {
  139. T fSin = sqrt(T(1) - fCos * fCos);
  140. T fAngle = atan(fSin, fCos);
  141. T fOneOverSin = static_cast<T>(1) / fSin;
  142. k0 = sin((static_cast<T>(1) - a) * fAngle) * fOneOverSin;
  143. k1 = sin((static_cast<T>(0) + a) * fAngle) * fOneOverSin;
  144. }
  145. return tquat<T, Q>(
  146. k0 * x.w + k1 * y2.w,
  147. k0 * x.x + k1 * y2.x,
  148. k0 * x.y + k1 * y2.y,
  149. k0 * x.z + k1 * y2.z);
  150. }
  151. template<typename T, qualifier Q>
  152. GLM_FUNC_QUALIFIER tquat<T, Q> fastMix(tquat<T, Q> const& x, tquat<T, Q> const& y, T const& a)
  153. {
  154. return glm::normalize(x * (static_cast<T>(1) - a) + (y * a));
  155. }
  156. template<typename T, qualifier Q>
  157. GLM_FUNC_QUALIFIER tquat<T, Q> rotation(vec<3, T, Q> const& orig, vec<3, T, Q> const& dest)
  158. {
  159. T cosTheta = dot(orig, dest);
  160. vec<3, T, Q> rotationAxis;
  161. if(cosTheta >= static_cast<T>(1) - epsilon<T>()) {
  162. // orig and dest point in the same direction
  163. return quat_identity<T,Q>();
  164. }
  165. if(cosTheta < static_cast<T>(-1) + epsilon<T>())
  166. {
  167. // special case when vectors in opposite directions :
  168. // there is no "ideal" rotation axis
  169. // So guess one; any will do as long as it's perpendicular to start
  170. // This implementation favors a rotation around the Up axis (Y),
  171. // since it's often what you want to do.
  172. rotationAxis = cross(vec<3, T, Q>(0, 0, 1), orig);
  173. if(length2(rotationAxis) < epsilon<T>()) // bad luck, they were parallel, try again!
  174. rotationAxis = cross(vec<3, T, Q>(1, 0, 0), orig);
  175. rotationAxis = normalize(rotationAxis);
  176. return angleAxis(pi<T>(), rotationAxis);
  177. }
  178. // Implementation from Stan Melax's Game Programming Gems 1 article
  179. rotationAxis = cross(orig, dest);
  180. T s = sqrt((T(1) + cosTheta) * static_cast<T>(2));
  181. T invs = static_cast<T>(1) / s;
  182. return tquat<T, Q>(
  183. s * static_cast<T>(0.5f),
  184. rotationAxis.x * invs,
  185. rotationAxis.y * invs,
  186. rotationAxis.z * invs);
  187. }
  188. template<typename T, qualifier Q>
  189. GLM_FUNC_QUALIFIER tquat<T, Q> quatLookAt(vec<3, T, Q> const& direction, vec<3, T, Q> const& up)
  190. {
  191. # if GLM_COORDINATE_SYSTEM == GLM_LEFT_HANDED
  192. return quatLookAtLH(direction, up);
  193. # else
  194. return quatLookAtRH(direction, up);
  195. # endif
  196. }
  197. template<typename T, qualifier Q>
  198. GLM_FUNC_QUALIFIER tquat<T, Q> quatLookAtRH(vec<3, T, Q> const& direction, vec<3, T, Q> const& up)
  199. {
  200. mat<3, 3, T, Q> Result;
  201. Result[2] = -direction;
  202. Result[0] = normalize(cross(up, Result[2]));
  203. Result[1] = cross(Result[2], Result[0]);
  204. return quat_cast(Result);
  205. }
  206. template<typename T, qualifier Q>
  207. GLM_FUNC_QUALIFIER tquat<T, Q> quatLookAtLH(vec<3, T, Q> const& direction, vec<3, T, Q> const& up)
  208. {
  209. mat<3, 3, T, Q> Result;
  210. Result[2] = direction;
  211. Result[0] = normalize(cross(up, Result[2]));
  212. Result[1] = cross(Result[2], Result[0]);
  213. return quat_cast(Result);
  214. }
  215. }//namespace glm