💿🐜 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.

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