quaternion.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*************************************************************************/
  2. /* quaternion.hpp */
  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. #ifndef GODOT_QUATERNION_HPP
  31. #define GODOT_QUATERNION_HPP
  32. #include <godot_cpp/core/math.hpp>
  33. #include <godot_cpp/variant/vector3.hpp>
  34. namespace godot {
  35. struct _NO_DISCARD_ 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 idx) {
  46. return components[idx];
  47. }
  48. _FORCE_INLINE_ const real_t &operator[](int idx) const {
  49. return components[idx];
  50. }
  51. _FORCE_INLINE_ real_t length_squared() const;
  52. bool is_equal_approx(const Quaternion &p_quaternion) const;
  53. real_t length() const;
  54. void normalize();
  55. Quaternion normalized() const;
  56. bool is_normalized() const;
  57. Quaternion inverse() const;
  58. Quaternion log() const;
  59. Quaternion exp() const;
  60. _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
  61. real_t angle_to(const Quaternion &p_to) const;
  62. Vector3 get_euler_xyz() const;
  63. Vector3 get_euler_yxz() const;
  64. Vector3 get_euler() const { return get_euler_yxz(); };
  65. Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const;
  66. Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const;
  67. Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const 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, const real_t &p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const 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 &v) const {
  81. #ifdef MATH_CHECKS
  82. ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized.");
  83. #endif
  84. Vector3 u(x, y, z);
  85. Vector3 uv = u.cross(v);
  86. return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
  87. }
  88. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &v) const {
  89. return inverse().xform(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*=(const real_t &s);
  94. _FORCE_INLINE_ void operator/=(const real_t &s);
  95. _FORCE_INLINE_ Quaternion operator+(const Quaternion &q2) const;
  96. _FORCE_INLINE_ Quaternion operator-(const Quaternion &q2) const;
  97. _FORCE_INLINE_ Quaternion operator-() const;
  98. _FORCE_INLINE_ Quaternion operator*(const real_t &s) const;
  99. _FORCE_INLINE_ Quaternion operator/(const real_t &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 Vector3 &p_euler);
  112. Quaternion(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. void operator=(const Quaternion &p_q) {
  119. x = p_q.x;
  120. y = p_q.y;
  121. z = p_q.z;
  122. w = p_q.w;
  123. }
  124. Quaternion(const Vector3 &v0, const Vector3 &v1) { // Shortest arc.
  125. Vector3 c = v0.cross(v1);
  126. real_t d = v0.dot(v1);
  127. if (d < -1.0f + (real_t)CMP_EPSILON) {
  128. x = 0;
  129. y = 1;
  130. z = 0;
  131. w = 0;
  132. } else {
  133. real_t s = Math::sqrt((1.0f + d) * 2.0f);
  134. real_t rs = 1.0f / s;
  135. x = c.x * rs;
  136. y = c.y * rs;
  137. z = c.z * rs;
  138. w = s * 0.5f;
  139. }
  140. }
  141. };
  142. real_t Quaternion::dot(const Quaternion &p_q) const {
  143. return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w;
  144. }
  145. real_t Quaternion::length_squared() const {
  146. return dot(*this);
  147. }
  148. void Quaternion::operator+=(const Quaternion &p_q) {
  149. x += p_q.x;
  150. y += p_q.y;
  151. z += p_q.z;
  152. w += p_q.w;
  153. }
  154. void Quaternion::operator-=(const Quaternion &p_q) {
  155. x -= p_q.x;
  156. y -= p_q.y;
  157. z -= p_q.z;
  158. w -= p_q.w;
  159. }
  160. void Quaternion::operator*=(const real_t &s) {
  161. x *= s;
  162. y *= s;
  163. z *= s;
  164. w *= s;
  165. }
  166. void Quaternion::operator/=(const real_t &s) {
  167. *this *= 1.0f / s;
  168. }
  169. Quaternion Quaternion::operator+(const Quaternion &q2) const {
  170. const Quaternion &q1 = *this;
  171. return Quaternion(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
  172. }
  173. Quaternion Quaternion::operator-(const Quaternion &q2) const {
  174. const Quaternion &q1 = *this;
  175. return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
  176. }
  177. Quaternion Quaternion::operator-() const {
  178. const Quaternion &q2 = *this;
  179. return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w);
  180. }
  181. Quaternion Quaternion::operator*(const real_t &s) const {
  182. return Quaternion(x * s, y * s, z * s, w * s);
  183. }
  184. Quaternion Quaternion::operator/(const real_t &s) const {
  185. return *this * (1.0f / s);
  186. }
  187. bool Quaternion::operator==(const Quaternion &p_quaternion) const {
  188. return x == p_quaternion.x && y == p_quaternion.y && z == p_quaternion.z && w == p_quaternion.w;
  189. }
  190. bool Quaternion::operator!=(const Quaternion &p_quaternion) const {
  191. return x != p_quaternion.x || y != p_quaternion.y || z != p_quaternion.z || w != p_quaternion.w;
  192. }
  193. _FORCE_INLINE_ Quaternion operator*(const real_t &p_real, const Quaternion &p_quaternion) {
  194. return p_quaternion * p_real;
  195. }
  196. } // namespace godot
  197. #endif // GODOT_QUATERNION_HPP