quaternion.cpp 9.4 KB

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