quat.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* quat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "quat.h"
  31. #include "core/math/basis.h"
  32. #include "core/string/print_string.h"
  33. // get_euler_xyz returns a vector containing the Euler angles in the format
  34. // (ax,ay,az), where ax is the angle of rotation around x axis,
  35. // and similar for other axes.
  36. // This implementation uses XYZ convention (Z is the first rotation).
  37. Vector3 Quat::get_euler_xyz() const {
  38. Basis m(*this);
  39. return m.get_euler_xyz();
  40. }
  41. // get_euler_yxz returns a vector containing the Euler angles in the format
  42. // (ax,ay,az), where ax is the angle of rotation around x axis,
  43. // and similar for other axes.
  44. // This implementation uses YXZ convention (Z is the first rotation).
  45. Vector3 Quat::get_euler_yxz() const {
  46. #ifdef MATH_CHECKS
  47. ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized.");
  48. #endif
  49. Basis m(*this);
  50. return m.get_euler_yxz();
  51. }
  52. void Quat::operator*=(const Quat &p_q) {
  53. x = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y;
  54. y = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z;
  55. z = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x;
  56. w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z;
  57. }
  58. Quat Quat::operator*(const Quat &p_q) const {
  59. Quat r = *this;
  60. r *= p_q;
  61. return r;
  62. }
  63. bool Quat::is_equal_approx(const Quat &p_quat) const {
  64. return Math::is_equal_approx(x, p_quat.x) && Math::is_equal_approx(y, p_quat.y) && Math::is_equal_approx(z, p_quat.z) && Math::is_equal_approx(w, p_quat.w);
  65. }
  66. real_t Quat::length() const {
  67. return Math::sqrt(length_squared());
  68. }
  69. void Quat::normalize() {
  70. *this /= length();
  71. }
  72. Quat Quat::normalized() const {
  73. return *this / length();
  74. }
  75. bool Quat::is_normalized() const {
  76. return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); //use less epsilon
  77. }
  78. Quat Quat::inverse() const {
  79. #ifdef MATH_CHECKS
  80. ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The quaternion must be normalized.");
  81. #endif
  82. return Quat(-x, -y, -z, w);
  83. }
  84. Quat Quat::slerp(const Quat &p_to, const real_t &p_weight) const {
  85. #ifdef MATH_CHECKS
  86. ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized.");
  87. ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quat(), "The end quaternion must be normalized.");
  88. #endif
  89. Quat to1;
  90. real_t omega, cosom, sinom, scale0, scale1;
  91. // calc cosine
  92. cosom = dot(p_to);
  93. // adjust signs (if necessary)
  94. if (cosom < 0.0) {
  95. cosom = -cosom;
  96. to1.x = -p_to.x;
  97. to1.y = -p_to.y;
  98. to1.z = -p_to.z;
  99. to1.w = -p_to.w;
  100. } else {
  101. to1.x = p_to.x;
  102. to1.y = p_to.y;
  103. to1.z = p_to.z;
  104. to1.w = p_to.w;
  105. }
  106. // calculate coefficients
  107. if ((1.0 - cosom) > CMP_EPSILON) {
  108. // standard case (slerp)
  109. omega = Math::acos(cosom);
  110. sinom = Math::sin(omega);
  111. scale0 = Math::sin((1.0 - p_weight) * omega) / sinom;
  112. scale1 = Math::sin(p_weight * omega) / sinom;
  113. } else {
  114. // "from" and "to" quaternions are very close
  115. // ... so we can do a linear interpolation
  116. scale0 = 1.0 - p_weight;
  117. scale1 = p_weight;
  118. }
  119. // calculate final values
  120. return Quat(
  121. scale0 * x + scale1 * to1.x,
  122. scale0 * y + scale1 * to1.y,
  123. scale0 * z + scale1 * to1.z,
  124. scale0 * w + scale1 * to1.w);
  125. }
  126. Quat Quat::slerpni(const Quat &p_to, const real_t &p_weight) const {
  127. #ifdef MATH_CHECKS
  128. ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized.");
  129. ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quat(), "The end quaternion must be normalized.");
  130. #endif
  131. const Quat &from = *this;
  132. real_t dot = from.dot(p_to);
  133. if (Math::absf(dot) > 0.9999) {
  134. return from;
  135. }
  136. real_t theta = Math::acos(dot),
  137. sinT = 1.0 / Math::sin(theta),
  138. newFactor = Math::sin(p_weight * theta) * sinT,
  139. invFactor = Math::sin((1.0 - p_weight) * theta) * sinT;
  140. return Quat(invFactor * from.x + newFactor * p_to.x,
  141. invFactor * from.y + newFactor * p_to.y,
  142. invFactor * from.z + newFactor * p_to.z,
  143. invFactor * from.w + newFactor * p_to.w);
  144. }
  145. Quat Quat::cubic_slerp(const Quat &p_b, const Quat &p_pre_a, const Quat &p_post_b, const real_t &p_weight) const {
  146. #ifdef MATH_CHECKS
  147. ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized.");
  148. ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quat(), "The end quaternion must be normalized.");
  149. #endif
  150. //the only way to do slerp :|
  151. real_t t2 = (1.0 - p_weight) * p_weight * 2;
  152. Quat sp = this->slerp(p_b, p_weight);
  153. Quat sq = p_pre_a.slerpni(p_post_b, p_weight);
  154. return sp.slerpni(sq, t2);
  155. }
  156. Quat::operator String() const {
  157. return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w);
  158. }
  159. Quat::Quat(const Vector3 &p_axis, real_t p_angle) {
  160. #ifdef MATH_CHECKS
  161. ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
  162. #endif
  163. real_t d = p_axis.length();
  164. if (d == 0) {
  165. x = 0;
  166. y = 0;
  167. z = 0;
  168. w = 0;
  169. } else {
  170. real_t sin_angle = Math::sin(p_angle * 0.5);
  171. real_t cos_angle = Math::cos(p_angle * 0.5);
  172. real_t s = sin_angle / d;
  173. x = p_axis.x * s;
  174. y = p_axis.y * s;
  175. z = p_axis.z * s;
  176. w = cos_angle;
  177. }
  178. }
  179. // Euler constructor expects a vector containing the Euler angles in the format
  180. // (ax, ay, az), where ax is the angle of rotation around x axis,
  181. // and similar for other axes.
  182. // This implementation uses YXZ convention (Z is the first rotation).
  183. Quat::Quat(const Vector3 &p_euler) {
  184. real_t half_a1 = p_euler.y * 0.5;
  185. real_t half_a2 = p_euler.x * 0.5;
  186. real_t half_a3 = p_euler.z * 0.5;
  187. // R = Y(a1).X(a2).Z(a3) convention for Euler angles.
  188. // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6)
  189. // a3 is the angle of the first rotation, following the notation in this reference.
  190. real_t cos_a1 = Math::cos(half_a1);
  191. real_t sin_a1 = Math::sin(half_a1);
  192. real_t cos_a2 = Math::cos(half_a2);
  193. real_t sin_a2 = Math::sin(half_a2);
  194. real_t cos_a3 = Math::cos(half_a3);
  195. real_t sin_a3 = Math::sin(half_a3);
  196. x = sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3;
  197. y = sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3;
  198. z = -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3;
  199. w = sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3;
  200. }