quaternion.cpp 11 KB

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