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