quaternion.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* quaternion.h */
  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. #pragma once
  31. #include "core/math/math_funcs.h"
  32. #include "core/math/vector3.h"
  33. #include "core/string/ustring.h"
  34. struct [[nodiscard]] Quaternion {
  35. union {
  36. // NOLINTBEGIN(modernize-use-default-member-init)
  37. struct {
  38. real_t x;
  39. real_t y;
  40. real_t z;
  41. real_t w;
  42. };
  43. real_t components[4] = { 0, 0, 0, 1.0 };
  44. // NOLINTEND(modernize-use-default-member-init)
  45. };
  46. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  47. return components[p_idx];
  48. }
  49. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  50. return components[p_idx];
  51. }
  52. _FORCE_INLINE_ real_t length_squared() const;
  53. bool is_equal_approx(const Quaternion &p_quaternion) const;
  54. bool is_same(const Quaternion &p_quaternion) const;
  55. bool is_finite() const;
  56. real_t length() const;
  57. void normalize();
  58. Quaternion normalized() const;
  59. bool is_normalized() const;
  60. Quaternion inverse() const;
  61. Quaternion log() const;
  62. Quaternion exp() const;
  63. _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
  64. real_t angle_to(const Quaternion &p_to) const;
  65. Vector3 get_euler(EulerOrder p_order = EulerOrder::YXZ) const;
  66. static Quaternion from_euler(const Vector3 &p_euler);
  67. Quaternion slerp(const Quaternion &p_to, real_t p_weight) const;
  68. Quaternion slerpni(const Quaternion &p_to, real_t p_weight) const;
  69. Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight) const;
  70. Quaternion spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
  71. Vector3 get_axis() const;
  72. real_t get_angle() const;
  73. _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  74. r_angle = 2 * Math::acos(w);
  75. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  76. r_axis.x = x * r;
  77. r_axis.y = y * r;
  78. r_axis.z = z * r;
  79. }
  80. constexpr void operator*=(const Quaternion &p_q);
  81. constexpr Quaternion operator*(const Quaternion &p_q) const;
  82. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_v) const {
  83. #ifdef MATH_CHECKS
  84. ERR_FAIL_COND_V_MSG(!is_normalized(), p_v, "The quaternion " + operator String() + " must be normalized.");
  85. #endif
  86. Vector3 u(x, y, z);
  87. Vector3 uv = u.cross(p_v);
  88. return p_v + ((uv * w) + u.cross(uv)) * ((real_t)2);
  89. }
  90. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_v) const {
  91. return inverse().xform(p_v);
  92. }
  93. constexpr void operator+=(const Quaternion &p_q);
  94. constexpr void operator-=(const Quaternion &p_q);
  95. constexpr void operator*=(real_t p_s);
  96. constexpr void operator/=(real_t p_s);
  97. constexpr Quaternion operator+(const Quaternion &p_q2) const;
  98. constexpr Quaternion operator-(const Quaternion &p_q2) const;
  99. constexpr Quaternion operator-() const;
  100. constexpr Quaternion operator*(real_t p_s) const;
  101. constexpr Quaternion operator/(real_t p_s) const;
  102. constexpr bool operator==(const Quaternion &p_quaternion) const;
  103. constexpr bool operator!=(const Quaternion &p_quaternion) const;
  104. operator String() const;
  105. constexpr Quaternion() :
  106. x(0), y(0), z(0), w(1) {}
  107. constexpr Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  108. x(p_x), y(p_y), z(p_z), w(p_w) {}
  109. Quaternion(const Vector3 &p_axis, real_t p_angle);
  110. constexpr Quaternion(const Quaternion &p_q) :
  111. x(p_q.x), y(p_q.y), z(p_q.z), w(p_q.w) {}
  112. constexpr void operator=(const Quaternion &p_q) {
  113. x = p_q.x;
  114. y = p_q.y;
  115. z = p_q.z;
  116. w = p_q.w;
  117. }
  118. Quaternion(const Vector3 &p_v0, const Vector3 &p_v1) { // Shortest arc.
  119. #ifdef MATH_CHECKS
  120. ERR_FAIL_COND_MSG(p_v0.is_zero_approx() || p_v1.is_zero_approx(), "The vectors must not be zero.");
  121. #endif
  122. constexpr real_t ALMOST_ONE = 1.0f - (real_t)CMP_EPSILON;
  123. Vector3 n0 = p_v0.normalized();
  124. Vector3 n1 = p_v1.normalized();
  125. real_t d = n0.dot(n1);
  126. if (abs(d) > ALMOST_ONE) {
  127. if (d >= 0) {
  128. return; // Vectors are same.
  129. }
  130. Vector3 axis = n0.get_any_perpendicular();
  131. x = axis.x;
  132. y = axis.y;
  133. z = axis.z;
  134. w = 0;
  135. } else {
  136. Vector3 c = n0.cross(n1);
  137. real_t s = Math::sqrt((1.0f + d) * 2.0f);
  138. real_t rs = 1.0f / s;
  139. x = c.x * rs;
  140. y = c.y * rs;
  141. z = c.z * rs;
  142. w = s * 0.5f;
  143. }
  144. }
  145. };
  146. real_t Quaternion::dot(const Quaternion &p_q) const {
  147. return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w;
  148. }
  149. real_t Quaternion::length_squared() const {
  150. return dot(*this);
  151. }
  152. constexpr void Quaternion::operator+=(const Quaternion &p_q) {
  153. x += p_q.x;
  154. y += p_q.y;
  155. z += p_q.z;
  156. w += p_q.w;
  157. }
  158. constexpr void Quaternion::operator-=(const Quaternion &p_q) {
  159. x -= p_q.x;
  160. y -= p_q.y;
  161. z -= p_q.z;
  162. w -= p_q.w;
  163. }
  164. constexpr void Quaternion::operator*=(real_t p_s) {
  165. x *= p_s;
  166. y *= p_s;
  167. z *= p_s;
  168. w *= p_s;
  169. }
  170. constexpr void Quaternion::operator/=(real_t p_s) {
  171. *this *= (1 / p_s);
  172. }
  173. constexpr Quaternion Quaternion::operator+(const Quaternion &p_q2) const {
  174. const Quaternion &q1 = *this;
  175. return Quaternion(q1.x + p_q2.x, q1.y + p_q2.y, q1.z + p_q2.z, q1.w + p_q2.w);
  176. }
  177. constexpr Quaternion Quaternion::operator-(const Quaternion &p_q2) const {
  178. const Quaternion &q1 = *this;
  179. return Quaternion(q1.x - p_q2.x, q1.y - p_q2.y, q1.z - p_q2.z, q1.w - p_q2.w);
  180. }
  181. constexpr Quaternion Quaternion::operator-() const {
  182. const Quaternion &q2 = *this;
  183. return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w);
  184. }
  185. constexpr Quaternion Quaternion::operator*(real_t p_s) const {
  186. return Quaternion(x * p_s, y * p_s, z * p_s, w * p_s);
  187. }
  188. constexpr Quaternion Quaternion::operator/(real_t p_s) const {
  189. return *this * (1 / p_s);
  190. }
  191. constexpr bool Quaternion::operator==(const Quaternion &p_quaternion) const {
  192. return x == p_quaternion.x && y == p_quaternion.y && z == p_quaternion.z && w == p_quaternion.w;
  193. }
  194. constexpr bool Quaternion::operator!=(const Quaternion &p_quaternion) const {
  195. return x != p_quaternion.x || y != p_quaternion.y || z != p_quaternion.z || w != p_quaternion.w;
  196. }
  197. constexpr void Quaternion::operator*=(const Quaternion &p_q) {
  198. real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y;
  199. real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z;
  200. real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x;
  201. w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z;
  202. x = xx;
  203. y = yy;
  204. z = zz;
  205. }
  206. constexpr Quaternion Quaternion::operator*(const Quaternion &p_q) const {
  207. Quaternion r = *this;
  208. r *= p_q;
  209. return r;
  210. }
  211. constexpr Quaternion operator*(real_t p_real, const Quaternion &p_quaternion) {
  212. return p_quaternion * p_real;
  213. }