quaternion.cpp 13 KB

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