quaternion.cpp 13 KB

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