💿🐜 Antkeeper source code https://antkeeper.com
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.

510 lines
12 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_QUATERNION_FUNCTIONS_HPP
  20. #define ANTKEEPER_MATH_QUATERNION_FUNCTIONS_HPP
  21. #include "math/constants.hpp"
  22. #include "math/matrix-type.hpp"
  23. #include "math/quaternion-type.hpp"
  24. #include "math/vector.hpp"
  25. #include <cmath>
  26. namespace math {
  27. /**
  28. * Adds two quaternions.
  29. *
  30. * @param x First quaternion.
  31. * @param y Second quaternion.
  32. * @return Sum of the two quaternions.
  33. */
  34. template <class T>
  35. quaternion<T> add(const quaternion<T>& x, const quaternion<T>& y);
  36. /**
  37. * Calculates the conjugate of a quaternion.
  38. *
  39. * @param x Quaternion from which the conjugate will be calculated.
  40. * @return Conjugate of the quaternion.
  41. */
  42. template <class T>
  43. quaternion<T> conjugate(const quaternion<T>& x);
  44. /**
  45. * Calculates the dot product of two quaternions.
  46. *
  47. * @param x First quaternion.
  48. * @param y Second quaternion.
  49. * @return Dot product of the two quaternions.
  50. */
  51. template <class T>
  52. T dot(const quaternion<T>& x, const quaternion<T>& y);
  53. /**
  54. * Divides a quaternion by a scalar.
  55. *
  56. * @param q Quaternion.
  57. * @param s Scalar.
  58. * @return Result of the division.
  59. */
  60. template <class T>
  61. quaternion<T> div(const quaternion<T>& q, T s);
  62. /**
  63. * Calculates the length of a quaternion.
  64. *
  65. * @param x Quaternion to calculate the length of.
  66. * @return Length of the quaternion.
  67. */
  68. template <class T>
  69. T length(const quaternion<T>& x);
  70. /**
  71. * Calculates the squared length of a quaternion. The squared length can be calculated faster than the length because a call to `std::sqrt` is saved.
  72. *
  73. * @param x Quaternion to calculate the squared length of.
  74. * @return Squared length of the quaternion.
  75. */
  76. template <class T>
  77. T length_squared(const quaternion<T>& x);
  78. /**
  79. * Performs linear interpolation between two quaternions.
  80. *
  81. * @param x First quaternion.
  82. * @param y Second quaternion.
  83. * @param a Interpolation factor.
  84. * @return Interpolated quaternion.
  85. */
  86. template <class T>
  87. quaternion<T> lerp(const quaternion<T>& x, const quaternion<T>& y, T a);
  88. /**
  89. * Creates a unit quaternion rotation using forward and up vectors.
  90. *
  91. * @param forward Unit forward vector.
  92. * @param up Unit up vector.
  93. * @return Unit rotation quaternion.
  94. */
  95. template <class T>
  96. quaternion<T> look_rotation(const vector<T, 3>& forward, vector<T, 3> up);
  97. /**
  98. * Converts a quaternion to a rotation matrix.
  99. *
  100. * @param q Unit quaternion.
  101. * @return Matrix representing the rotation described by `q`.
  102. */
  103. template <class T>
  104. matrix<T, 3, 3> matrix_cast(const quaternion<T>& q);
  105. /**
  106. * Multiplies two quaternions.
  107. *
  108. * @param x First quaternion.
  109. * @param y Second quaternion.
  110. * @return Product of the two quaternions.
  111. */
  112. template <class T>
  113. quaternion<T> mul(const quaternion<T>& x, const quaternion<T>& y);
  114. /**
  115. * Multiplies a quaternion by a scalar.
  116. *
  117. * @param q Quaternion.
  118. * @param s Scalar.
  119. * @return Product of the quaternion and scalar.
  120. */
  121. template <class T>
  122. quaternion<T> mul(const quaternion<T>& q, T s);
  123. /**
  124. * Rotates a 3-dimensional vector by a quaternion.
  125. *
  126. * @param q Unit quaternion.
  127. * @param v Vector.
  128. * @return Rotated vector.
  129. */
  130. template <class T>
  131. vector<T, 3> mul(const quaternion<T>& q, const vector<T, 3>& v);
  132. /**
  133. * Negates a quaternion.
  134. */
  135. template <class T>
  136. quaternion<T> negate(const quaternion<T>& x);
  137. /**
  138. * Performs normalized linear interpolation between two quaternions.
  139. *
  140. * @param x First quaternion.
  141. * @param y Second quaternion.
  142. * @param a Interpolation factor.
  143. * @return Interpolated quaternion.
  144. */
  145. template <class T>
  146. quaternion<T> nlerp(const quaternion<T>& x, const quaternion<T>& y, T a);
  147. /**
  148. * Normalizes a quaternion.
  149. */
  150. template <class T>
  151. quaternion<T> normalize(const quaternion<T>& x);
  152. /**
  153. * Creates a rotation from an angle and axis.
  154. *
  155. * @param angle Angle of rotation (in radians).
  156. * @param axis Axis of rotation
  157. * @return Quaternion representing the rotation.
  158. */
  159. template <class T>
  160. quaternion<T> angle_axis(T angle, const vector<T, 3>& axis);
  161. /**
  162. * Calculates the minimum rotation between two normalized direction vectors.
  163. *
  164. * @param source Normalized source direction.
  165. * @param destination Normalized destination direction.
  166. * @return Quaternion representing the minimum rotation between the source and destination vectors.
  167. */
  168. template <class T>
  169. quaternion<T> rotation(const vector<T, 3>& source, const vector<T, 3>& destination);
  170. /**
  171. * Performs spherical linear interpolation between two quaternions.
  172. *
  173. * @param x First quaternion.
  174. * @param y Second quaternion.
  175. * @param a Interpolation factor.
  176. * @return Interpolated quaternion.
  177. */
  178. template <class T>
  179. quaternion<T> slerp(const quaternion<T>& x, const quaternion<T>& y, T a);
  180. /**
  181. * Subtracts a quaternion from another quaternion.
  182. *
  183. * @param x First quaternion.
  184. * @param y Second quaternion.
  185. * @return Difference between the quaternions.
  186. */
  187. template <class T>
  188. quaternion<T> sub(const quaternion<T>& x, const quaternion<T>& y);
  189. /**
  190. * Decomposes a quaternion into swing and twist rotation components.
  191. *
  192. * @param[in] q Quaternion to decompose.
  193. * @param[in] a Axis of twist rotation.
  194. * @param[out] swing Swing rotation component.
  195. * @param[out] twist Twist rotation component.
  196. * @param[in] epsilon Threshold at which a number is considered zero.
  197. *
  198. * @see https://www.euclideanspace.com/maths/geometry/rotations/for/decomposition/
  199. */
  200. template <class T>
  201. void swing_twist(const quaternion<T>& q, const vector<T, 3>& a, quaternion<T>& qs, quaternion<T>& qt, T epsilon = T(1e-6));
  202. /**
  203. * Converts a 3x3 rotation matrix to a quaternion.
  204. *
  205. * @param m Rotation matrix.
  206. * @return Unit quaternion representing the rotation described by `m`.
  207. */
  208. template <class T>
  209. quaternion<T> quaternion_cast(const matrix<T, 3, 3>& m);
  210. /**
  211. * Types casts each quaternion component and returns a quaternion of the casted type.
  212. *
  213. * @tparam T2 Target quaternion component type.
  214. * @tparam T1 Source quaternion component type.
  215. * @param q Quaternion to type cast.
  216. * @return Type-casted quaternion.
  217. */
  218. template <class T2, class T1>
  219. quaternion<T2> type_cast(const quaternion<T1>& q);
  220. template <class T>
  221. inline quaternion<T> add(const quaternion<T>& x, const quaternion<T>& y)
  222. {
  223. return {x.w + y.w, x.x + y.x, x.y + y.y, x.z + y.z};
  224. }
  225. template <class T>
  226. inline quaternion<T> conjugate(const quaternion<T>& x)
  227. {
  228. return {x.w, -x.x, -x.y, -x.z};
  229. }
  230. template <class T>
  231. inline T dot(const quaternion<T>& x, const quaternion<T>& y)
  232. {
  233. return {x.w * y.w + x.x * y.x + x.y * y.y + x.z * y.z};
  234. }
  235. template <class T>
  236. inline quaternion<T> div(const quaternion<T>& q, T s)
  237. {
  238. return {q.w / s, q.x / s, q.y / s, q.z / s}
  239. }
  240. template <class T>
  241. inline T length(const quaternion<T>& x)
  242. {
  243. return std::sqrt(x.w * x.w + x.x * x.x + x.y * x.y + x.z * x.z);
  244. }
  245. template <class T>
  246. inline T length_squared(const quaternion<T>& x)
  247. {
  248. return x.w * x.w + x.x * x.x + x.y * x.y + x.z * x.z;
  249. }
  250. template <class T>
  251. inline quaternion<T> lerp(const quaternion<T>& x, const quaternion<T>& y, T a)
  252. {
  253. return
  254. {
  255. (y.w - x.w) * a + x.w,
  256. (y.x - x.x) * a + x.x,
  257. (y.y - x.y) * a + x.y,
  258. (y.z - x.z) * a + x.z
  259. };
  260. }
  261. template <class T>
  262. quaternion<T> look_rotation(const vector<T, 3>& forward, vector<T, 3> up)
  263. {
  264. vector<T, 3> right = normalize(cross(forward, up));
  265. up = cross(right, forward);
  266. matrix<T, 3, 3> m =
  267. {
  268. right,
  269. up,
  270. -forward
  271. };
  272. // Convert to quaternion
  273. return normalize(quaternion_cast(m));
  274. }
  275. template <class T>
  276. matrix<T, 3, 3> matrix_cast(const quaternion<T>& q)
  277. {
  278. const T xx = q.x * q.x;
  279. const T xy = q.x * q.y;
  280. const T xz = q.x * q.z;
  281. const T xw = q.x * q.w;
  282. const T yy = q.y * q.y;
  283. const T yz = q.y * q.z;
  284. const T yw = q.y * q.w;
  285. const T zz = q.z * q.z;
  286. const T zw = q.z * q.w;
  287. return
  288. {
  289. T(1) - (yy + zz) * T(2), (xy + zw) * T(2), (xz - yw) * T(2),
  290. (xy - zw) * T(2), T(1) - (xx + zz) * T(2), (yz + xw) * T(2),
  291. (xz + yw) * T(2), (yz - xw) * T(2), T(1) - (xx + yy) * T(2)
  292. };
  293. }
  294. template <class T>
  295. quaternion<T> mul(const quaternion<T>& x, const quaternion<T>& y)
  296. {
  297. return
  298. {
  299. -x.x * y.x - x.y * y.y - x.z * y.z + x.w * y.w,
  300. x.x * y.w + x.y * y.z - x.z * y.y + x.w * y.x,
  301. -x.x * y.z + x.y * y.w + x.z * y.x + x.w * y.y,
  302. x.x * y.y - x.y * y.x + x.z * y.w + x.w * y.z
  303. };
  304. }
  305. template <class T>
  306. inline quaternion<T> mul(const quaternion<T>& q, T s)
  307. {
  308. return {q.w * s, q.x * s, q.y * s, q.z * s};
  309. }
  310. template <class T>
  311. vector<T, 3> mul(const quaternion<T>& q, const vector<T, 3>& v)
  312. {
  313. const vector<T, 3>& i = reinterpret_cast<const vector<T, 3>&>(q.x);
  314. return add(add(mul(i, dot(i, v) * T(2)), mul(v, (q.w * q.w - dot(i, i)))), mul(cross(i, v), q.w * T(2)));
  315. }
  316. template <class T>
  317. inline quaternion<T> negate(const quaternion<T>& x)
  318. {
  319. return {-x.w, -x.x, -x.y, -x.z};
  320. }
  321. template <class T>
  322. quaternion<T> nlerp(const quaternion<T>& x, const quaternion<T>& y, T a)
  323. {
  324. return normalize(add(mul(x, T(1) - a), mul(y, a * std::copysign(T(1), dot(x, y)))));
  325. }
  326. template <class T>
  327. inline quaternion<T> normalize(const quaternion<T>& x)
  328. {
  329. return mul(x, T(1) / length(x));
  330. }
  331. template <class T>
  332. quaternion<T> angle_axis(T angle, const vector<T, 3>& axis)
  333. {
  334. T s = std::sin(angle * T(0.5));
  335. return {static_cast<T>(std::cos(angle * T(0.5))), axis.x() * s, axis.y() * s, axis.z() * s};
  336. }
  337. template <class T>
  338. quaternion<T> rotation(const vector<T, 3>& source, const vector<T, 3>& destination)
  339. {
  340. quaternion<T> q;
  341. q.w = dot(source, destination);
  342. reinterpret_cast<vector<T, 3>&>(q.x) = cross(source, destination);
  343. q.w += length(q);
  344. return normalize(q);
  345. }
  346. template <class T>
  347. quaternion<T> slerp(const quaternion<T>& x, const quaternion<T>& y, T a)
  348. {
  349. T cos_theta = dot(x, y);
  350. constexpr T epsilon = T(0.0005);
  351. if (cos_theta > T(1) - epsilon)
  352. {
  353. return normalize(lerp(x, y, a));
  354. }
  355. cos_theta = std::max<T>(T(-1), std::min<T>(T(1), cos_theta));
  356. T theta = static_cast<T>(std::acos(cos_theta)) * a;
  357. quaternion<T> z = normalize(sub(y, mul(x, cos_theta)));
  358. return add(mul(x, static_cast<T>(std::cos(theta))), mul(z, static_cast<T>(std::sin(theta))));
  359. }
  360. template <class T>
  361. inline quaternion<T> sub(const quaternion<T>& x, const quaternion<T>& y)
  362. {
  363. return {x.w - y.w, x.x - y.x, x.y - y.y, x.z - y.z};
  364. }
  365. template <class T>
  366. void swing_twist(const quaternion<T>& q, const vector<T, 3>& a, quaternion<T>& qs, quaternion<T>& qt, T epsilon)
  367. {
  368. if (q.x * q.x + q.y * q.y + q.z * q.z > epsilon)
  369. {
  370. const vector<T, 3> pa = mul(a, (a.x() * q.x + a.y() * q.y + a.z() * q.z));
  371. qt = normalize(quaternion<T>{q.w, pa.x(), pa.y(), pa.z()});
  372. qs = mul(q, conjugate(qt));
  373. }
  374. else
  375. {
  376. qt = angle_axis(pi<T>, a);
  377. const vector<T, 3> qa = mul(q, a);
  378. const vector<T, 3> sa = cross(a, qa);
  379. if (length_squared(sa) > epsilon)
  380. qs = angle_axis(std::acos(dot(a, qa)), sa);
  381. else
  382. qs = quaternion<T>::identity;
  383. }
  384. }
  385. template <class T>
  386. quaternion<T> quaternion_cast(const matrix<T, 3, 3>& m)
  387. {
  388. T trace = m[0][0] + m[1][1] + m[2][2];
  389. if (trace > T(0))
  390. {
  391. T s = T(0.5) / std::sqrt(trace + T(1));
  392. return
  393. {
  394. T(0.25) / s,
  395. (m[1][2] - m[2][1]) * s,
  396. (m[2][0] - m[0][2]) * s,
  397. (m[0][1] - m[1][0]) * s
  398. };
  399. }
  400. else
  401. {
  402. if (m[0][0] > m[1][1] && m[0][0] > m[2][2])
  403. {
  404. T s = T(2) * std::sqrt(T(1) + m[0][0] - m[1][1] - m[2][2]);
  405. return
  406. {
  407. (m[1][2] - m[2][1]) / s,
  408. T(0.25) * s,
  409. (m[1][0] + m[0][1]) / s,
  410. (m[2][0] + m[0][2]) / s
  411. };
  412. }
  413. else if (m[1][1] > m[2][2])
  414. {
  415. T s = T(2) * std::sqrt(T(1) + m[1][1] - m[0][0] - m[2][2]);
  416. return
  417. {
  418. (m[2][0] - m[0][2]) / s,
  419. (m[1][0] + m[0][1]) / s,
  420. T(0.25) * s,
  421. (m[2][1] + m[1][2]) / s
  422. };
  423. }
  424. else
  425. {
  426. T s = T(2) * std::sqrt(T(1) + m[2][2] - m[0][0] - m[1][1]);
  427. return
  428. {
  429. (m[0][1] - m[1][0]) / s,
  430. (m[2][0] + m[0][2]) / s,
  431. (m[2][1] + m[1][2]) / s,
  432. T(0.25) * s
  433. };
  434. }
  435. }
  436. }
  437. template <class T2, class T1>
  438. inline quaternion<T2> type_cast(const quaternion<T1>& q)
  439. {
  440. return quaternion<T2>
  441. {
  442. static_cast<T2>(q.w),
  443. static_cast<T2>(q.x),
  444. static_cast<T2>(q.y),
  445. static_cast<T2>(q.z)
  446. };
  447. }
  448. } // namespace math
  449. #endif // ANTKEEPER_MATH_QUATERNION_FUNCTIONS_HPP