dual_quaternion.inl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /// @ref gtx_dual_quaternion
  2. #include "../geometric.hpp"
  3. #include <limits>
  4. namespace glm
  5. {
  6. // -- Component accesses --
  7. template<typename T, qualifier Q>
  8. GLM_FUNC_QUALIFIER typename tdualquat<T, Q>::part_type & tdualquat<T, Q>::operator[](typename tdualquat<T, Q>::length_type i)
  9. {
  10. assert(i >= 0 && i < this->length());
  11. return (&real)[i];
  12. }
  13. template<typename T, qualifier Q>
  14. GLM_FUNC_QUALIFIER typename tdualquat<T, Q>::part_type const& tdualquat<T, Q>::operator[](typename tdualquat<T, Q>::length_type i) const
  15. {
  16. assert(i >= 0 && i < this->length());
  17. return (&real)[i];
  18. }
  19. // -- Implicit basic constructors --
  20. # if GLM_CONFIG_DEFAULTED_FUNCTIONS == GLM_DISABLE
  21. template<typename T, qualifier Q>
  22. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat()
  23. # if GLM_CONFIG_DEFAULTED_FUNCTIONS != GLM_DISABLE
  24. : real(qua<T, Q>())
  25. , dual(qua<T, Q>(0, 0, 0, 0))
  26. # endif
  27. {}
  28. template<typename T, qualifier Q>
  29. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(tdualquat<T, Q> const& d)
  30. : real(d.real)
  31. , dual(d.dual)
  32. {}
  33. # endif
  34. template<typename T, qualifier Q>
  35. template<qualifier P>
  36. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(tdualquat<T, P> const& d)
  37. : real(d.real)
  38. , dual(d.dual)
  39. {}
  40. // -- Explicit basic constructors --
  41. template<typename T, qualifier Q>
  42. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& r)
  43. : real(r), dual(qua<T, Q>(0, 0, 0, 0))
  44. {}
  45. template<typename T, qualifier Q>
  46. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& q, vec<3, T, Q> const& p)
  47. : real(q), dual(
  48. T(-0.5) * ( p.x*q.x + p.y*q.y + p.z*q.z),
  49. T(+0.5) * ( p.x*q.w + p.y*q.z - p.z*q.y),
  50. T(+0.5) * (-p.x*q.z + p.y*q.w + p.z*q.x),
  51. T(+0.5) * ( p.x*q.y - p.y*q.x + p.z*q.w))
  52. {}
  53. template<typename T, qualifier Q>
  54. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(qua<T, Q> const& r, qua<T, Q> const& d)
  55. : real(r), dual(d)
  56. {}
  57. // -- Conversion constructors --
  58. template<typename T, qualifier Q>
  59. template<typename U, qualifier P>
  60. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(tdualquat<U, P> const& q)
  61. : real(q.real)
  62. , dual(q.dual)
  63. {}
  64. template<typename T, qualifier Q>
  65. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(mat<2, 4, T, Q> const& m)
  66. {
  67. *this = dualquat_cast(m);
  68. }
  69. template<typename T, qualifier Q>
  70. GLM_FUNC_QUALIFIER GLM_CONSTEXPR tdualquat<T, Q>::tdualquat(mat<3, 4, T, Q> const& m)
  71. {
  72. *this = dualquat_cast(m);
  73. }
  74. // -- Unary arithmetic operators --
  75. # if GLM_CONFIG_DEFAULTED_FUNCTIONS == GLM_DISABLE
  76. template<typename T, qualifier Q>
  77. GLM_FUNC_QUALIFIER tdualquat<T, Q> & tdualquat<T, Q>::operator=(tdualquat<T, Q> const& q)
  78. {
  79. this->real = q.real;
  80. this->dual = q.dual;
  81. return *this;
  82. }
  83. # endif
  84. template<typename T, qualifier Q>
  85. template<typename U>
  86. GLM_FUNC_QUALIFIER tdualquat<T, Q> & tdualquat<T, Q>::operator=(tdualquat<U, Q> const& q)
  87. {
  88. this->real = q.real;
  89. this->dual = q.dual;
  90. return *this;
  91. }
  92. template<typename T, qualifier Q>
  93. template<typename U>
  94. GLM_FUNC_QUALIFIER tdualquat<T, Q> & tdualquat<T, Q>::operator*=(U s)
  95. {
  96. this->real *= static_cast<T>(s);
  97. this->dual *= static_cast<T>(s);
  98. return *this;
  99. }
  100. template<typename T, qualifier Q>
  101. template<typename U>
  102. GLM_FUNC_QUALIFIER tdualquat<T, Q> & tdualquat<T, Q>::operator/=(U s)
  103. {
  104. this->real /= static_cast<T>(s);
  105. this->dual /= static_cast<T>(s);
  106. return *this;
  107. }
  108. // -- Unary bit operators --
  109. template<typename T, qualifier Q>
  110. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator+(tdualquat<T, Q> const& q)
  111. {
  112. return q;
  113. }
  114. template<typename T, qualifier Q>
  115. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator-(tdualquat<T, Q> const& q)
  116. {
  117. return tdualquat<T, Q>(-q.real, -q.dual);
  118. }
  119. // -- Binary operators --
  120. template<typename T, qualifier Q>
  121. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator+(tdualquat<T, Q> const& q, tdualquat<T, Q> const& p)
  122. {
  123. return tdualquat<T, Q>(q.real + p.real,q.dual + p.dual);
  124. }
  125. template<typename T, qualifier Q>
  126. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator*(tdualquat<T, Q> const& p, tdualquat<T, Q> const& o)
  127. {
  128. return tdualquat<T, Q>(p.real * o.real,p.real * o.dual + p.dual * o.real);
  129. }
  130. template<typename T, qualifier Q>
  131. GLM_FUNC_QUALIFIER vec<3, T, Q> operator*(tdualquat<T, Q> const& q, vec<3, T, Q> const& v)
  132. {
  133. vec<3, T, Q> const real_v3(q.real.x,q.real.y,q.real.z);
  134. vec<3, T, Q> const dual_v3(q.dual.x,q.dual.y,q.dual.z);
  135. return (cross(real_v3, cross(real_v3,v) + v * q.real.w + dual_v3) + dual_v3 * q.real.w - real_v3 * q.dual.w) * T(2) + v;
  136. }
  137. template<typename T, qualifier Q>
  138. GLM_FUNC_QUALIFIER vec<3, T, Q> operator*(vec<3, T, Q> const& v, tdualquat<T, Q> const& q)
  139. {
  140. return glm::inverse(q) * v;
  141. }
  142. template<typename T, qualifier Q>
  143. GLM_FUNC_QUALIFIER vec<4, T, Q> operator*(tdualquat<T, Q> const& q, vec<4, T, Q> const& v)
  144. {
  145. return vec<4, T, Q>(q * vec<3, T, Q>(v), v.w);
  146. }
  147. template<typename T, qualifier Q>
  148. GLM_FUNC_QUALIFIER vec<4, T, Q> operator*(vec<4, T, Q> const& v, tdualquat<T, Q> const& q)
  149. {
  150. return glm::inverse(q) * v;
  151. }
  152. template<typename T, qualifier Q>
  153. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator*(tdualquat<T, Q> const& q, T const& s)
  154. {
  155. return tdualquat<T, Q>(q.real * s, q.dual * s);
  156. }
  157. template<typename T, qualifier Q>
  158. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator*(T const& s, tdualquat<T, Q> const& q)
  159. {
  160. return q * s;
  161. }
  162. template<typename T, qualifier Q>
  163. GLM_FUNC_QUALIFIER tdualquat<T, Q> operator/(tdualquat<T, Q> const& q, T const& s)
  164. {
  165. return tdualquat<T, Q>(q.real / s, q.dual / s);
  166. }
  167. // -- Boolean operators --
  168. template<typename T, qualifier Q>
  169. GLM_FUNC_QUALIFIER bool operator==(tdualquat<T, Q> const& q1, tdualquat<T, Q> const& q2)
  170. {
  171. return (q1.real == q2.real) && (q1.dual == q2.dual);
  172. }
  173. template<typename T, qualifier Q>
  174. GLM_FUNC_QUALIFIER bool operator!=(tdualquat<T, Q> const& q1, tdualquat<T, Q> const& q2)
  175. {
  176. return (q1.real != q2.real) || (q1.dual != q2.dual);
  177. }
  178. // -- Operations --
  179. template<typename T, qualifier Q>
  180. GLM_FUNC_QUALIFIER tdualquat<T, Q> dual_quat_identity()
  181. {
  182. return tdualquat<T, Q>(
  183. qua<T, Q>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)),
  184. qua<T, Q>(static_cast<T>(0), static_cast<T>(0), static_cast<T>(0), static_cast<T>(0)));
  185. }
  186. template<typename T, qualifier Q>
  187. GLM_FUNC_QUALIFIER tdualquat<T, Q> normalize(tdualquat<T, Q> const& q)
  188. {
  189. return q / length(q.real);
  190. }
  191. template<typename T, qualifier Q>
  192. GLM_FUNC_QUALIFIER tdualquat<T, Q> lerp(tdualquat<T, Q> const& x, tdualquat<T, Q> const& y, T const& a)
  193. {
  194. // Dual Quaternion Linear blend aka DLB:
  195. // Lerp is only defined in [0, 1]
  196. assert(a >= static_cast<T>(0));
  197. assert(a <= static_cast<T>(1));
  198. T const k = dot(x.real,y.real) < static_cast<T>(0) ? -a : a;
  199. T const one(1);
  200. return tdualquat<T, Q>(x * (one - a) + y * k);
  201. }
  202. template<typename T, qualifier Q>
  203. GLM_FUNC_QUALIFIER tdualquat<T, Q> inverse(tdualquat<T, Q> const& q)
  204. {
  205. const glm::qua<T, Q> real = conjugate(q.real);
  206. const glm::qua<T, Q> dual = conjugate(q.dual);
  207. return tdualquat<T, Q>(real, dual + (real * (-2.0f * dot(real,dual))));
  208. }
  209. template<typename T, qualifier Q>
  210. GLM_FUNC_QUALIFIER mat<2, 4, T, Q> mat2x4_cast(tdualquat<T, Q> const& x)
  211. {
  212. return mat<2, 4, T, Q>( x[0].x, x[0].y, x[0].z, x[0].w, x[1].x, x[1].y, x[1].z, x[1].w );
  213. }
  214. template<typename T, qualifier Q>
  215. GLM_FUNC_QUALIFIER mat<3, 4, T, Q> mat3x4_cast(tdualquat<T, Q> const& x)
  216. {
  217. qua<T, Q> r = x.real / length2(x.real);
  218. qua<T, Q> const rr(r.w * x.real.w, r.x * x.real.x, r.y * x.real.y, r.z * x.real.z);
  219. r *= static_cast<T>(2);
  220. T const xy = r.x * x.real.y;
  221. T const xz = r.x * x.real.z;
  222. T const yz = r.y * x.real.z;
  223. T const wx = r.w * x.real.x;
  224. T const wy = r.w * x.real.y;
  225. T const wz = r.w * x.real.z;
  226. vec<4, T, Q> const a(
  227. rr.w + rr.x - rr.y - rr.z,
  228. xy - wz,
  229. xz + wy,
  230. -(x.dual.w * r.x - x.dual.x * r.w + x.dual.y * r.z - x.dual.z * r.y));
  231. vec<4, T, Q> const b(
  232. xy + wz,
  233. rr.w + rr.y - rr.x - rr.z,
  234. yz - wx,
  235. -(x.dual.w * r.y - x.dual.x * r.z - x.dual.y * r.w + x.dual.z * r.x));
  236. vec<4, T, Q> const c(
  237. xz - wy,
  238. yz + wx,
  239. rr.w + rr.z - rr.x - rr.y,
  240. -(x.dual.w * r.z + x.dual.x * r.y - x.dual.y * r.x - x.dual.z * r.w));
  241. return mat<3, 4, T, Q>(a, b, c);
  242. }
  243. template<typename T, qualifier Q>
  244. GLM_FUNC_QUALIFIER tdualquat<T, Q> dualquat_cast(mat<2, 4, T, Q> const& x)
  245. {
  246. return tdualquat<T, Q>(
  247. qua<T, Q>( x[0].w, x[0].x, x[0].y, x[0].z ),
  248. qua<T, Q>( x[1].w, x[1].x, x[1].y, x[1].z ));
  249. }
  250. template<typename T, qualifier Q>
  251. GLM_FUNC_QUALIFIER tdualquat<T, Q> dualquat_cast(mat<3, 4, T, Q> const& x)
  252. {
  253. qua<T, Q> real;
  254. T const trace = x[0].x + x[1].y + x[2].z;
  255. if(trace > static_cast<T>(0))
  256. {
  257. T const r = sqrt(T(1) + trace);
  258. T const invr = static_cast<T>(0.5) / r;
  259. real.w = static_cast<T>(0.5) * r;
  260. real.x = (x[2].y - x[1].z) * invr;
  261. real.y = (x[0].z - x[2].x) * invr;
  262. real.z = (x[1].x - x[0].y) * invr;
  263. }
  264. else if(x[0].x > x[1].y && x[0].x > x[2].z)
  265. {
  266. T const r = sqrt(T(1) + x[0].x - x[1].y - x[2].z);
  267. T const invr = static_cast<T>(0.5) / r;
  268. real.x = static_cast<T>(0.5)*r;
  269. real.y = (x[1].x + x[0].y) * invr;
  270. real.z = (x[0].z + x[2].x) * invr;
  271. real.w = (x[2].y - x[1].z) * invr;
  272. }
  273. else if(x[1].y > x[2].z)
  274. {
  275. T const r = sqrt(T(1) + x[1].y - x[0].x - x[2].z);
  276. T const invr = static_cast<T>(0.5) / r;
  277. real.x = (x[1].x + x[0].y) * invr;
  278. real.y = static_cast<T>(0.5) * r;
  279. real.z = (x[2].y + x[1].z) * invr;
  280. real.w = (x[0].z - x[2].x) * invr;
  281. }
  282. else
  283. {
  284. T const r = sqrt(T(1) + x[2].z - x[0].x - x[1].y);
  285. T const invr = static_cast<T>(0.5) / r;
  286. real.x = (x[0].z + x[2].x) * invr;
  287. real.y = (x[2].y + x[1].z) * invr;
  288. real.z = static_cast<T>(0.5) * r;
  289. real.w = (x[1].x - x[0].y) * invr;
  290. }
  291. qua<T, Q> dual;
  292. dual.x = static_cast<T>(0.5) * ( x[0].w * real.w + x[1].w * real.z - x[2].w * real.y);
  293. dual.y = static_cast<T>(0.5) * (-x[0].w * real.z + x[1].w * real.w + x[2].w * real.x);
  294. dual.z = static_cast<T>(0.5) * ( x[0].w * real.y - x[1].w * real.x + x[2].w * real.w);
  295. dual.w = -static_cast<T>(0.5) * ( x[0].w * real.x + x[1].w * real.y + x[2].w * real.z);
  296. return tdualquat<T, Q>(real, dual);
  297. }
  298. }//namespace glm