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

485 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/matrix-type.hpp"
  22. #include "math/quaternion-type.hpp"
  23. #include "math/vector-type.hpp"
  24. #include "math/vector-functions.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. * Converts a 3x3 rotation matrix to a quaternion.
  191. *
  192. * @param m Rotation matrix.
  193. * @return Unit quaternion representing the rotation described by `m`.
  194. */
  195. template <class T>
  196. quaternion<T> quaternion_cast(const matrix<T, 3, 3>& m);
  197. /**
  198. * Types casts each quaternion component and returns a quaternion of the casted type.
  199. *
  200. * @tparam T2 Target quaternion component type.
  201. * @tparam T1 Source quaternion component type.
  202. * @param q Quaternion to type cast.
  203. * @return Type-casted quaternion.
  204. */
  205. template <class T2, class T1>
  206. quaternion<T2> type_cast(const quaternion<T1>& q);
  207. template <class T>
  208. inline quaternion<T> add(const quaternion<T>& x, const quaternion<T>& y)
  209. {
  210. return {x.w + y.w, x.x + y.x, x.y + y.y, x.z + y.z};
  211. }
  212. template <class T>
  213. inline quaternion<T> conjugate(const quaternion<T>& x)
  214. {
  215. return {x.w, -x.x, -x.y, -x.z};
  216. }
  217. template <class T>
  218. inline T dot(const quaternion<T>& x, const quaternion<T>& y)
  219. {
  220. return {x.w * y.w + x.x * y.x + x.y * y.y + x.z * y.z};
  221. }
  222. template <class T>
  223. inline quaternion<T> div(const quaternion<T>& q, T s)
  224. {
  225. return {q.w / s, q.x / s, q.y / s, q.z / s}
  226. }
  227. template <class T>
  228. inline T length(const quaternion<T>& x)
  229. {
  230. return std::sqrt(x.w * x.w + x.x * x.x + x.y * x.y + x.z * x.z);
  231. }
  232. template <class T>
  233. inline T length_squared(const quaternion<T>& x)
  234. {
  235. return x.w * x.w + x.x * x.x + x.y * x.y + x.z * x.z;
  236. }
  237. template <class T>
  238. inline quaternion<T> lerp(const quaternion<T>& x, const quaternion<T>& y, T a)
  239. {
  240. return
  241. {
  242. (y.w - x.w) * a + x.w,
  243. (y.x - x.x) * a + x.x,
  244. (y.y - x.y) * a + x.y,
  245. (y.z - x.z) * a + x.z
  246. };
  247. }
  248. template <class T>
  249. quaternion<T> look_rotation(const vector<T, 3>& forward, vector<T, 3> up)
  250. {
  251. vector<T, 3> right = normalize(cross(forward, up));
  252. up = cross(right, forward);
  253. matrix<T, 3, 3> m =
  254. {{
  255. {right[0], up[0], -forward[0]},
  256. {right[1], up[1], -forward[1]},
  257. {right[2], up[2], -forward[2]}
  258. }};
  259. // Convert to quaternion
  260. return normalize(quaternion_cast(m));
  261. }
  262. template <class T>
  263. matrix<T, 3, 3> matrix_cast(const quaternion<T>& q)
  264. {
  265. T wx = q.w * q.x;
  266. T wy = q.w * q.y;
  267. T wz = q.w * q.z;
  268. T xx = q.x * q.x;
  269. T xy = q.x * q.y;
  270. T xz = q.x * q.z;
  271. T yy = q.y * q.y;
  272. T yz = q.y * q.z;
  273. T zz = q.z * q.z;
  274. return
  275. {{
  276. {T(1) - (yy + zz) * T(2), (xy + wz) * T(2), (xz - wy) * T(2)},
  277. {(xy - wz) * T(2), T(1) - (xx + zz) * T(2), (yz + wx) * T(2)},
  278. {(xz + wy) * T(2), (yz - wx) * T(2), T(1) - (xx + yy) * T(2)}
  279. }};
  280. }
  281. template <class T>
  282. quaternion<T> mul(const quaternion<T>& x, const quaternion<T>& y)
  283. {
  284. return
  285. {
  286. -x.x * y.x - x.y * y.y - x.z * y.z + x.w * y.w,
  287. x.x * y.w + x.y * y.z - x.z * y.y + x.w * y.x,
  288. -x.x * y.z + x.y * y.w + x.z * y.x + x.w * y.y,
  289. x.x * y.y - x.y * y.x + x.z * y.w + x.w * y.z
  290. };
  291. }
  292. template <class T>
  293. inline quaternion<T> mul(const quaternion<T>& q, T s)
  294. {
  295. return {q.w * s, q.x * s, q.y * s, q.z * s};
  296. }
  297. template <class T>
  298. vector<T, 3> mul(const quaternion<T>& q, const vector<T, 3>& v)
  299. {
  300. const T r = q.w; // Real part
  301. const vector<T, 3>& i = reinterpret_cast<const vector<T, 3>&>(q.x); // Imaginary part
  302. return i * dot(i, v) * T(2) + v * (r * r - dot(i, i)) + cross(i, v) * r * T(2);
  303. }
  304. template <class T>
  305. inline quaternion<T> negate(const quaternion<T>& x)
  306. {
  307. return {-x.w, -x.x, -x.y, -x.z};
  308. }
  309. template <class T>
  310. quaternion<T> nlerp(const quaternion<T>& x, const quaternion<T>& y, T a)
  311. {
  312. if (dot(x, y) < T(0))
  313. {
  314. return normalize(add(mul(x, T(1) - a), mul(y, -a)));
  315. }
  316. else
  317. {
  318. return normalize(add(mul(x, T(1) - a), mul(y, a)));
  319. }
  320. }
  321. template <class T>
  322. inline quaternion<T> normalize(const quaternion<T>& x)
  323. {
  324. return mul(x, T(1) / length(x));
  325. }
  326. template <class T>
  327. quaternion<T> angle_axis(T angle, const vector<T, 3>& axis)
  328. {
  329. T s = std::sin(angle * T(0.5));
  330. return {static_cast<T>(std::cos(angle * T(0.5))), axis.x * s, axis.y * s, axis.z * s};
  331. }
  332. template <class T>
  333. quaternion<T> rotation(const vector<T, 3>& source, const vector<T, 3>& destination)
  334. {
  335. quaternion<T> q;
  336. q.w = dot(source, destination);
  337. reinterpret_cast<vector<T, 3>&>(q.x) = cross(source, destination);
  338. q.w += length(q);
  339. return normalize(q);
  340. }
  341. template <class T>
  342. quaternion<T> slerp(const quaternion<T>& x, const quaternion<T>& y, T a)
  343. {
  344. T cos_theta = dot(x, y);
  345. constexpr T epsilon = T(0.0005);
  346. if (cos_theta > T(1) - epsilon)
  347. {
  348. return normalize(lerp(x, y, a));
  349. }
  350. cos_theta = std::max<T>(T(-1), std::min<T>(T(1), cos_theta));
  351. T theta = static_cast<T>(std::acos(cos_theta)) * a;
  352. quaternion<T> z = normalize(sub(y, mul(x, cos_theta)));
  353. return add(mul(x, static_cast<T>(std::cos(theta))), mul(z, static_cast<T>(std::sin(theta))));
  354. }
  355. template <class T>
  356. inline quaternion<T> sub(const quaternion<T>& x, const quaternion<T>& y)
  357. {
  358. return {x.w - y.w, x.x - y.x, x.y - y.y, x.z - y.z};
  359. }
  360. template <class T>
  361. quaternion<T> quaternion_cast(const matrix<T, 3, 3>& m)
  362. {
  363. T r;
  364. vector<T, 3> i;
  365. T trace = m[0][0] + m[1][1] + m[2][2];
  366. if (trace > T(0))
  367. {
  368. T s = T(0.5) / std::sqrt(trace + T(1));
  369. r = T(0.25) / s;
  370. i =
  371. {
  372. (m[2][1] - m[1][2]) * s,
  373. (m[0][2] - m[2][0]) * s,
  374. (m[1][0] - m[0][1]) * s
  375. };
  376. }
  377. else
  378. {
  379. if (m[0][0] > m[1][1] && m[0][0] > m[2][2])
  380. {
  381. T s = T(2) * std::sqrt(T(1) + m[0][0] - m[1][1] - m[2][2]);
  382. r = (m[2][1] - m[1][2]) / s;
  383. i =
  384. {
  385. T(0.25) * s,
  386. (m[0][1] + m[1][0]) / s,
  387. (m[0][2] + m[2][0]) / s
  388. };
  389. }
  390. else if (m[1][1] > m[2][2])
  391. {
  392. T s = T(2) * std::sqrt(T(1) + m[1][1] - m[0][0] - m[2][2]);
  393. r = (m[0][2] - m[2][0]) / s;
  394. i =
  395. {
  396. (m[0][1] + m[1][0]) / s,
  397. T(0.25) * s,
  398. (m[1][2] + m[2][1]) / s
  399. };
  400. }
  401. else
  402. {
  403. T s = T(2) * std::sqrt(T(1) + m[2][2] - m[0][0] - m[1][1]);
  404. r = (m[1][0] - m[0][1]) / s;
  405. i =
  406. {
  407. (m[0][2] + m[2][0]) / s,
  408. (m[1][2] + m[2][1]) / s,
  409. T(0.25) * s
  410. };
  411. }
  412. }
  413. return {r, i.x, i.y, i.z};
  414. }
  415. template <class T2, class T1>
  416. inline quaternion<T2> type_cast(const quaternion<T1>& q)
  417. {
  418. return quaternion<T2>
  419. {
  420. static_cast<T2>(q.w),
  421. static_cast<T2>(q.x),
  422. static_cast<T2>(q.y),
  423. static_cast<T2>(q.z)
  424. };
  425. }
  426. } // namespace math
  427. #endif // ANTKEEPER_MATH_QUATERNION_FUNCTIONS_HPP