quaternion.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* quaternion.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "quaternion.h"
  31. #include "core/math/basis.h"
  32. #include "core/string/ustring.h"
  33. real_t Quaternion::angle_to(const Quaternion &p_to) const {
  34. real_t d = dot(p_to);
  35. return Math::acos(CLAMP(d * d * 2 - 1, -1, 1));
  36. }
  37. Vector3 Quaternion::get_euler(EulerOrder p_order) const {
  38. #ifdef MATH_CHECKS
  39. ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized.");
  40. #endif
  41. return Basis(*this).get_euler(p_order);
  42. }
  43. void Quaternion::operator*=(const Quaternion &p_q) {
  44. real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y;
  45. real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z;
  46. real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x;
  47. w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z;
  48. x = xx;
  49. y = yy;
  50. z = zz;
  51. }
  52. Quaternion Quaternion::operator*(const Quaternion &p_q) const {
  53. Quaternion r = *this;
  54. r *= p_q;
  55. return r;
  56. }
  57. bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const {
  58. return Math::is_equal_approx(x, p_quaternion.x) && Math::is_equal_approx(y, p_quaternion.y) && Math::is_equal_approx(z, p_quaternion.z) && Math::is_equal_approx(w, p_quaternion.w);
  59. }
  60. bool Quaternion::is_finite() const {
  61. return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w);
  62. }
  63. real_t Quaternion::length() const {
  64. return Math::sqrt(length_squared());
  65. }
  66. void Quaternion::normalize() {
  67. *this /= length();
  68. }
  69. Quaternion Quaternion::normalized() const {
  70. return *this / length();
  71. }
  72. bool Quaternion::is_normalized() const {
  73. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon
  74. }
  75. Quaternion Quaternion::inverse() const {
  76. #ifdef MATH_CHECKS
  77. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion must be normalized.");
  78. #endif
  79. return Quaternion(-x, -y, -z, w);
  80. }
  81. Quaternion Quaternion::log() const {
  82. Quaternion src = *this;
  83. Vector3 src_v = src.get_axis() * src.get_angle();
  84. return Quaternion(src_v.x, src_v.y, src_v.z, 0);
  85. }
  86. Quaternion Quaternion::exp() const {
  87. Quaternion src = *this;
  88. Vector3 src_v = Vector3(src.x, src.y, src.z);
  89. real_t theta = src_v.length();
  90. src_v = src_v.normalized();
  91. if (theta < CMP_EPSILON || !src_v.is_normalized()) {
  92. return Quaternion(0, 0, 0, 1);
  93. }
  94. return Quaternion(src_v, theta);
  95. }
  96. Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const {
  97. #ifdef MATH_CHECKS
  98. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  99. ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  100. #endif
  101. Quaternion to1;
  102. real_t omega, cosom, sinom, scale0, scale1;
  103. // calc cosine
  104. cosom = dot(p_to);
  105. // adjust signs (if necessary)
  106. if (cosom < 0.0f) {
  107. cosom = -cosom;
  108. to1 = -p_to;
  109. } else {
  110. to1 = p_to;
  111. }
  112. // calculate coefficients
  113. if ((1.0f - cosom) > (real_t)CMP_EPSILON) {
  114. // standard case (slerp)
  115. omega = Math::acos(cosom);
  116. sinom = Math::sin(omega);
  117. scale0 = Math::sin((1.0 - p_weight) * omega) / sinom;
  118. scale1 = Math::sin(p_weight * omega) / sinom;
  119. } else {
  120. // "from" and "to" quaternions are very close
  121. // ... so we can do a linear interpolation
  122. scale0 = 1.0f - p_weight;
  123. scale1 = p_weight;
  124. }
  125. // calculate final values
  126. return Quaternion(
  127. scale0 * x + scale1 * to1.x,
  128. scale0 * y + scale1 * to1.y,
  129. scale0 * z + scale1 * to1.z,
  130. scale0 * w + scale1 * to1.w);
  131. }
  132. Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) const {
  133. #ifdef MATH_CHECKS
  134. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  135. ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  136. #endif
  137. const Quaternion &from = *this;
  138. real_t dot = from.dot(p_to);
  139. if (Math::absf(dot) > 0.9999f) {
  140. return from;
  141. }
  142. real_t theta = Math::acos(dot),
  143. sinT = 1.0f / Math::sin(theta),
  144. newFactor = Math::sin(p_weight * theta) * sinT,
  145. invFactor = Math::sin((1.0f - p_weight) * theta) * sinT;
  146. return Quaternion(invFactor * from.x + newFactor * p_to.x,
  147. invFactor * from.y + newFactor * p_to.y,
  148. invFactor * from.z + newFactor * p_to.z,
  149. invFactor * from.w + newFactor * p_to.w);
  150. }
  151. Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const {
  152. #ifdef MATH_CHECKS
  153. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  154. ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  155. #endif
  156. Quaternion from_q = *this;
  157. Quaternion pre_q = p_pre_a;
  158. Quaternion to_q = p_b;
  159. Quaternion post_q = p_post_b;
  160. // Align flip phases.
  161. from_q = Basis(from_q).get_rotation_quaternion();
  162. pre_q = Basis(pre_q).get_rotation_quaternion();
  163. to_q = Basis(to_q).get_rotation_quaternion();
  164. post_q = Basis(post_q).get_rotation_quaternion();
  165. // Flip quaternions to shortest path if necessary.
  166. bool flip1 = signbit(from_q.dot(pre_q));
  167. pre_q = flip1 ? -pre_q : pre_q;
  168. bool flip2 = signbit(from_q.dot(to_q));
  169. to_q = flip2 ? -to_q : to_q;
  170. bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q));
  171. post_q = flip3 ? -post_q : post_q;
  172. // Calc by Expmap in from_q space.
  173. Quaternion ln_from = Quaternion(0, 0, 0, 0);
  174. Quaternion ln_to = (from_q.inverse() * to_q).log();
  175. Quaternion ln_pre = (from_q.inverse() * pre_q).log();
  176. Quaternion ln_post = (from_q.inverse() * post_q).log();
  177. Quaternion ln = Quaternion(0, 0, 0, 0);
  178. ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight);
  179. ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight);
  180. ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
  181. Quaternion q1 = from_q * ln.exp();
  182. // Calc by Expmap in to_q space.
  183. ln_from = (to_q.inverse() * from_q).log();
  184. ln_to = Quaternion(0, 0, 0, 0);
  185. ln_pre = (to_q.inverse() * pre_q).log();
  186. ln_post = (to_q.inverse() * post_q).log();
  187. ln = Quaternion(0, 0, 0, 0);
  188. ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight);
  189. ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight);
  190. ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
  191. Quaternion q2 = to_q * ln.exp();
  192. // To cancel error made by Expmap ambiguity, do blends.
  193. return q1.slerp(q2, p_weight);
  194. }
  195. Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight,
  196. const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
  197. #ifdef MATH_CHECKS
  198. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  199. ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  200. #endif
  201. Quaternion from_q = *this;
  202. Quaternion pre_q = p_pre_a;
  203. Quaternion to_q = p_b;
  204. Quaternion post_q = p_post_b;
  205. // Align flip phases.
  206. from_q = Basis(from_q).get_rotation_quaternion();
  207. pre_q = Basis(pre_q).get_rotation_quaternion();
  208. to_q = Basis(to_q).get_rotation_quaternion();
  209. post_q = Basis(post_q).get_rotation_quaternion();
  210. // Flip quaternions to shortest path if necessary.
  211. bool flip1 = signbit(from_q.dot(pre_q));
  212. pre_q = flip1 ? -pre_q : pre_q;
  213. bool flip2 = signbit(from_q.dot(to_q));
  214. to_q = flip2 ? -to_q : to_q;
  215. bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q));
  216. post_q = flip3 ? -post_q : post_q;
  217. // Calc by Expmap in from_q space.
  218. Quaternion ln_from = Quaternion(0, 0, 0, 0);
  219. Quaternion ln_to = (from_q.inverse() * to_q).log();
  220. Quaternion ln_pre = (from_q.inverse() * pre_q).log();
  221. Quaternion ln_post = (from_q.inverse() * post_q).log();
  222. Quaternion ln = Quaternion(0, 0, 0, 0);
  223. ln.x = Math::cubic_interpolate_in_time(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  224. ln.y = Math::cubic_interpolate_in_time(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  225. ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  226. Quaternion q1 = from_q * ln.exp();
  227. // Calc by Expmap in to_q space.
  228. ln_from = (to_q.inverse() * from_q).log();
  229. ln_to = Quaternion(0, 0, 0, 0);
  230. ln_pre = (to_q.inverse() * pre_q).log();
  231. ln_post = (to_q.inverse() * post_q).log();
  232. ln = Quaternion(0, 0, 0, 0);
  233. ln.x = Math::cubic_interpolate_in_time(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  234. ln.y = Math::cubic_interpolate_in_time(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  235. ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  236. Quaternion q2 = to_q * ln.exp();
  237. // To cancel error made by Expmap ambiguity, do blends.
  238. return q1.slerp(q2, p_weight);
  239. }
  240. Quaternion::operator String() const {
  241. return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
  242. }
  243. Vector3 Quaternion::get_axis() const {
  244. if (Math::abs(w) > 1 - CMP_EPSILON) {
  245. return Vector3(x, y, z);
  246. }
  247. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  248. return Vector3(x * r, y * r, z * r);
  249. }
  250. real_t Quaternion::get_angle() const {
  251. return 2 * Math::acos(w);
  252. }
  253. Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) {
  254. #ifdef MATH_CHECKS
  255. ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
  256. #endif
  257. real_t d = p_axis.length();
  258. if (d == 0) {
  259. x = 0;
  260. y = 0;
  261. z = 0;
  262. w = 0;
  263. } else {
  264. real_t sin_angle = Math::sin(p_angle * 0.5f);
  265. real_t cos_angle = Math::cos(p_angle * 0.5f);
  266. real_t s = sin_angle / d;
  267. x = p_axis.x * s;
  268. y = p_axis.y * s;
  269. z = p_axis.z * s;
  270. w = cos_angle;
  271. }
  272. }
  273. // Euler constructor expects a vector containing the Euler angles in the format
  274. // (ax, ay, az), where ax is the angle of rotation around x axis,
  275. // and similar for other axes.
  276. // This implementation uses YXZ convention (Z is the first rotation).
  277. Quaternion Quaternion::from_euler(const Vector3 &p_euler) {
  278. real_t half_a1 = p_euler.y * 0.5f;
  279. real_t half_a2 = p_euler.x * 0.5f;
  280. real_t half_a3 = p_euler.z * 0.5f;
  281. // R = Y(a1).X(a2).Z(a3) convention for Euler angles.
  282. // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6)
  283. // a3 is the angle of the first rotation, following the notation in this reference.
  284. real_t cos_a1 = Math::cos(half_a1);
  285. real_t sin_a1 = Math::sin(half_a1);
  286. real_t cos_a2 = Math::cos(half_a2);
  287. real_t sin_a2 = Math::sin(half_a2);
  288. real_t cos_a3 = Math::cos(half_a3);
  289. real_t sin_a3 = Math::sin(half_a3);
  290. return Quaternion(
  291. sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3,
  292. sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3,
  293. -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3,
  294. sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
  295. }