quaternion.h 7.5 KB

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