quaternion.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // get_euler_xyz returns a vector containing the Euler angles in the format
  38. // (ax,ay,az), where ax is the angle of rotation around x axis,
  39. // and similar for other axes.
  40. // This implementation uses XYZ convention (Z is the first rotation).
  41. Vector3 Quaternion::get_euler_xyz() const {
  42. Basis m(*this);
  43. return m.get_euler(Basis::EULER_ORDER_XYZ);
  44. }
  45. // get_euler_yxz returns a vector containing the Euler angles in the format
  46. // (ax,ay,az), where ax is the angle of rotation around x axis,
  47. // and similar for other axes.
  48. // This implementation uses YXZ convention (Z is the first rotation).
  49. Vector3 Quaternion::get_euler_yxz() const {
  50. #ifdef MATH_CHECKS
  51. ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized.");
  52. #endif
  53. Basis m(*this);
  54. return m.get_euler(Basis::EULER_ORDER_YXZ);
  55. }
  56. void Quaternion::operator*=(const Quaternion &p_q) {
  57. real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y;
  58. real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z;
  59. real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x;
  60. w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z;
  61. x = xx;
  62. y = yy;
  63. z = zz;
  64. }
  65. Quaternion Quaternion::operator*(const Quaternion &p_q) const {
  66. Quaternion r = *this;
  67. r *= p_q;
  68. return r;
  69. }
  70. bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const {
  71. 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);
  72. }
  73. bool Quaternion::is_finite() const {
  74. return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w);
  75. }
  76. real_t Quaternion::length() const {
  77. return Math::sqrt(length_squared());
  78. }
  79. void Quaternion::normalize() {
  80. *this /= length();
  81. }
  82. Quaternion Quaternion::normalized() const {
  83. return *this / length();
  84. }
  85. bool Quaternion::is_normalized() const {
  86. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon
  87. }
  88. Quaternion Quaternion::inverse() const {
  89. #ifdef MATH_CHECKS
  90. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion must be normalized.");
  91. #endif
  92. return Quaternion(-x, -y, -z, w);
  93. }
  94. Quaternion Quaternion::log() const {
  95. Quaternion src = *this;
  96. Vector3 src_v = src.get_axis() * src.get_angle();
  97. return Quaternion(src_v.x, src_v.y, src_v.z, 0);
  98. }
  99. Quaternion Quaternion::exp() const {
  100. Quaternion src = *this;
  101. Vector3 src_v = Vector3(src.x, src.y, src.z);
  102. real_t theta = src_v.length();
  103. src_v = src_v.normalized();
  104. if (theta < CMP_EPSILON || !src_v.is_normalized()) {
  105. return Quaternion(0, 0, 0, 1);
  106. }
  107. return Quaternion(src_v, theta);
  108. }
  109. Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const {
  110. #ifdef MATH_CHECKS
  111. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  112. ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  113. #endif
  114. Quaternion to1;
  115. real_t omega, cosom, sinom, scale0, scale1;
  116. // calc cosine
  117. cosom = dot(p_to);
  118. // adjust signs (if necessary)
  119. if (cosom < 0.0f) {
  120. cosom = -cosom;
  121. to1 = -p_to;
  122. } else {
  123. to1 = p_to;
  124. }
  125. // calculate coefficients
  126. if ((1.0f - cosom) > (real_t)CMP_EPSILON) {
  127. // standard case (slerp)
  128. omega = Math::acos(cosom);
  129. sinom = Math::sin(omega);
  130. scale0 = Math::sin((1.0 - p_weight) * omega) / sinom;
  131. scale1 = Math::sin(p_weight * omega) / sinom;
  132. } else {
  133. // "from" and "to" quaternions are very close
  134. // ... so we can do a linear interpolation
  135. scale0 = 1.0f - p_weight;
  136. scale1 = p_weight;
  137. }
  138. // calculate final values
  139. return Quaternion(
  140. scale0 * x + scale1 * to1.x,
  141. scale0 * y + scale1 * to1.y,
  142. scale0 * z + scale1 * to1.z,
  143. scale0 * w + scale1 * to1.w);
  144. }
  145. Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) const {
  146. #ifdef MATH_CHECKS
  147. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  148. ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  149. #endif
  150. const Quaternion &from = *this;
  151. real_t dot = from.dot(p_to);
  152. if (Math::absf(dot) > 0.9999f) {
  153. return from;
  154. }
  155. real_t theta = Math::acos(dot),
  156. sinT = 1.0f / Math::sin(theta),
  157. newFactor = Math::sin(p_weight * theta) * sinT,
  158. invFactor = Math::sin((1.0f - p_weight) * theta) * sinT;
  159. return Quaternion(invFactor * from.x + newFactor * p_to.x,
  160. invFactor * from.y + newFactor * p_to.y,
  161. invFactor * from.z + newFactor * p_to.z,
  162. invFactor * from.w + newFactor * p_to.w);
  163. }
  164. 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 {
  165. #ifdef MATH_CHECKS
  166. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  167. ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  168. #endif
  169. Quaternion from_q = *this;
  170. Quaternion pre_q = p_pre_a;
  171. Quaternion to_q = p_b;
  172. Quaternion post_q = p_post_b;
  173. // Align flip phases.
  174. from_q = Basis(from_q).get_rotation_quaternion();
  175. pre_q = Basis(pre_q).get_rotation_quaternion();
  176. to_q = Basis(to_q).get_rotation_quaternion();
  177. post_q = Basis(post_q).get_rotation_quaternion();
  178. // Flip quaternions to shortest path if necessary.
  179. bool flip1 = signbit(from_q.dot(pre_q));
  180. pre_q = flip1 ? -pre_q : pre_q;
  181. bool flip2 = signbit(from_q.dot(to_q));
  182. to_q = flip2 ? -to_q : to_q;
  183. bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q));
  184. post_q = flip3 ? -post_q : post_q;
  185. // Calc by Expmap in from_q space.
  186. Quaternion ln_from = Quaternion(0, 0, 0, 0);
  187. Quaternion ln_to = (from_q.inverse() * to_q).log();
  188. Quaternion ln_pre = (from_q.inverse() * pre_q).log();
  189. Quaternion ln_post = (from_q.inverse() * post_q).log();
  190. Quaternion ln = Quaternion(0, 0, 0, 0);
  191. ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight);
  192. ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight);
  193. ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
  194. Quaternion q1 = from_q * ln.exp();
  195. // Calc by Expmap in to_q space.
  196. ln_from = (to_q.inverse() * from_q).log();
  197. ln_to = Quaternion(0, 0, 0, 0);
  198. ln_pre = (to_q.inverse() * pre_q).log();
  199. ln_post = (to_q.inverse() * post_q).log();
  200. ln = Quaternion(0, 0, 0, 0);
  201. ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight);
  202. ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight);
  203. ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
  204. Quaternion q2 = to_q * ln.exp();
  205. // To cancel error made by Expmap ambiguity, do blends.
  206. return q1.slerp(q2, p_weight);
  207. }
  208. 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,
  209. const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
  210. #ifdef MATH_CHECKS
  211. ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
  212. ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized.");
  213. #endif
  214. Quaternion from_q = *this;
  215. Quaternion pre_q = p_pre_a;
  216. Quaternion to_q = p_b;
  217. Quaternion post_q = p_post_b;
  218. // Align flip phases.
  219. from_q = Basis(from_q).get_rotation_quaternion();
  220. pre_q = Basis(pre_q).get_rotation_quaternion();
  221. to_q = Basis(to_q).get_rotation_quaternion();
  222. post_q = Basis(post_q).get_rotation_quaternion();
  223. // Flip quaternions to shortest path if necessary.
  224. bool flip1 = signbit(from_q.dot(pre_q));
  225. pre_q = flip1 ? -pre_q : pre_q;
  226. bool flip2 = signbit(from_q.dot(to_q));
  227. to_q = flip2 ? -to_q : to_q;
  228. bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q));
  229. post_q = flip3 ? -post_q : post_q;
  230. // Calc by Expmap in from_q space.
  231. Quaternion ln_from = Quaternion(0, 0, 0, 0);
  232. Quaternion ln_to = (from_q.inverse() * to_q).log();
  233. Quaternion ln_pre = (from_q.inverse() * pre_q).log();
  234. Quaternion ln_post = (from_q.inverse() * post_q).log();
  235. Quaternion ln = Quaternion(0, 0, 0, 0);
  236. 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);
  237. 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);
  238. 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);
  239. Quaternion q1 = from_q * ln.exp();
  240. // Calc by Expmap in to_q space.
  241. ln_from = (to_q.inverse() * from_q).log();
  242. ln_to = Quaternion(0, 0, 0, 0);
  243. ln_pre = (to_q.inverse() * pre_q).log();
  244. ln_post = (to_q.inverse() * post_q).log();
  245. ln = Quaternion(0, 0, 0, 0);
  246. 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);
  247. 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);
  248. 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);
  249. Quaternion q2 = to_q * ln.exp();
  250. // To cancel error made by Expmap ambiguity, do blends.
  251. return q1.slerp(q2, p_weight);
  252. }
  253. Quaternion::operator String() const {
  254. return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
  255. }
  256. Vector3 Quaternion::get_axis() const {
  257. if (Math::abs(w) > 1 - CMP_EPSILON) {
  258. return Vector3(x, y, z);
  259. }
  260. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  261. return Vector3(x * r, y * r, z * r);
  262. }
  263. real_t Quaternion::get_angle() const {
  264. return 2 * Math::acos(w);
  265. }
  266. Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) {
  267. #ifdef MATH_CHECKS
  268. ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
  269. #endif
  270. real_t d = p_axis.length();
  271. if (d == 0) {
  272. x = 0;
  273. y = 0;
  274. z = 0;
  275. w = 0;
  276. } else {
  277. real_t sin_angle = Math::sin(p_angle * 0.5f);
  278. real_t cos_angle = Math::cos(p_angle * 0.5f);
  279. real_t s = sin_angle / d;
  280. x = p_axis.x * s;
  281. y = p_axis.y * s;
  282. z = p_axis.z * s;
  283. w = cos_angle;
  284. }
  285. }
  286. // Euler constructor expects a vector containing the Euler angles in the format
  287. // (ax, ay, az), where ax is the angle of rotation around x axis,
  288. // and similar for other axes.
  289. // This implementation uses YXZ convention (Z is the first rotation).
  290. Quaternion::Quaternion(const Vector3 &p_euler) {
  291. real_t half_a1 = p_euler.y * 0.5f;
  292. real_t half_a2 = p_euler.x * 0.5f;
  293. real_t half_a3 = p_euler.z * 0.5f;
  294. // R = Y(a1).X(a2).Z(a3) convention for Euler angles.
  295. // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6)
  296. // a3 is the angle of the first rotation, following the notation in this reference.
  297. real_t cos_a1 = Math::cos(half_a1);
  298. real_t sin_a1 = Math::sin(half_a1);
  299. real_t cos_a2 = Math::cos(half_a2);
  300. real_t sin_a2 = Math::sin(half_a2);
  301. real_t cos_a3 = Math::cos(half_a3);
  302. real_t sin_a3 = Math::sin(half_a3);
  303. x = sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3;
  304. y = sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3;
  305. z = -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3;
  306. w = sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3;
  307. }