quat.cpp 8.3 KB

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