quaternion.cpp 8.0 KB

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