quaternion.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. class Quaternion {
  36. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  37. friend class Variant;
  38. public:
  39. union {
  40. struct {
  41. real_t x;
  42. real_t y;
  43. real_t z;
  44. real_t w;
  45. };
  46. real_t components[4] = { 0, 0, 0, 1.0 };
  47. };
  48. _FORCE_INLINE_ real_t &operator[](int idx) {
  49. return components[idx];
  50. }
  51. _FORCE_INLINE_ const real_t &operator[](int idx) const {
  52. return components[idx];
  53. }
  54. _FORCE_INLINE_ real_t length_squared() const;
  55. bool is_equal_approx(const Quaternion &p_quaternion) 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_xyz() const;
  66. Vector3 get_euler_yxz() const;
  67. Vector3 get_euler() const { return get_euler_yxz(); };
  68. Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const;
  69. Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const;
  70. Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const;
  71. 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;
  72. Vector3 get_axis() const;
  73. real_t get_angle() const;
  74. _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  75. r_angle = 2 * Math::acos(w);
  76. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  77. r_axis.x = x * r;
  78. r_axis.y = y * r;
  79. r_axis.z = z * r;
  80. }
  81. void operator*=(const Quaternion &p_q);
  82. Quaternion operator*(const Quaternion &p_q) const;
  83. _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const {
  84. #ifdef MATH_CHECKS
  85. ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized.");
  86. #endif
  87. Vector3 u(x, y, z);
  88. Vector3 uv = u.cross(v);
  89. return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
  90. }
  91. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &v) const {
  92. return inverse().xform(v);
  93. }
  94. _FORCE_INLINE_ void operator+=(const Quaternion &p_q);
  95. _FORCE_INLINE_ void operator-=(const Quaternion &p_q);
  96. _FORCE_INLINE_ void operator*=(const real_t &s);
  97. _FORCE_INLINE_ void operator/=(const real_t &s);
  98. _FORCE_INLINE_ Quaternion operator+(const Quaternion &q2) const;
  99. _FORCE_INLINE_ Quaternion operator-(const Quaternion &q2) const;
  100. _FORCE_INLINE_ Quaternion operator-() const;
  101. _FORCE_INLINE_ Quaternion operator*(const real_t &s) const;
  102. _FORCE_INLINE_ Quaternion operator/(const real_t &s) const;
  103. _FORCE_INLINE_ bool operator==(const Quaternion &p_quaternion) const;
  104. _FORCE_INLINE_ bool operator!=(const Quaternion &p_quaternion) const;
  105. operator String() const;
  106. _FORCE_INLINE_ Quaternion() {}
  107. _FORCE_INLINE_ Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  108. x(p_x),
  109. y(p_y),
  110. z(p_z),
  111. w(p_w) {
  112. }
  113. Quaternion(const Vector3 &p_axis, real_t p_angle);
  114. Quaternion(const Vector3 &p_euler);
  115. Quaternion(const Quaternion &p_q) :
  116. x(p_q.x),
  117. y(p_q.y),
  118. z(p_q.z),
  119. w(p_q.w) {
  120. }
  121. void operator=(const Quaternion &p_q) {
  122. x = p_q.x;
  123. y = p_q.y;
  124. z = p_q.z;
  125. w = p_q.w;
  126. }
  127. Quaternion(const Vector3 &v0, const Vector3 &v1) // shortest arc
  128. {
  129. Vector3 c = v0.cross(v1);
  130. real_t d = v0.dot(v1);
  131. if (d < -1.0f + (real_t)CMP_EPSILON) {
  132. x = 0;
  133. y = 1;
  134. z = 0;
  135. w = 0;
  136. } else {
  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. 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. 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. void Quaternion::operator*=(const real_t &s) {
  165. x *= s;
  166. y *= s;
  167. z *= s;
  168. w *= s;
  169. }
  170. void Quaternion::operator/=(const real_t &s) {
  171. *this *= 1.0f / s;
  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 Quaternion &q2) const {
  178. const Quaternion &q1 = *this;
  179. return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
  180. }
  181. Quaternion Quaternion::operator-() const {
  182. const Quaternion &q2 = *this;
  183. return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w);
  184. }
  185. Quaternion Quaternion::operator*(const real_t &s) const {
  186. return Quaternion(x * s, y * s, z * s, w * s);
  187. }
  188. Quaternion Quaternion::operator/(const real_t &s) const {
  189. return *this * (1.0f / s);
  190. }
  191. 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. 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. _FORCE_INLINE_ Quaternion operator*(const real_t &p_real, const Quaternion &p_quaternion) {
  198. return p_quaternion * p_real;
  199. }
  200. } // namespace godot
  201. #endif // GODOT_QUATERNION_HPP