quat.cpp 9.3 KB

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