quaternion.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**************************************************************************/
  2. /* quaternion.hpp */
  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 GODOT_QUATERNION_HPP
  31. #define GODOT_QUATERNION_HPP
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. #include <godot_cpp/variant/vector3.hpp>
  35. namespace godot {
  36. struct [[nodiscard]] Quaternion {
  37. union {
  38. struct {
  39. real_t x;
  40. real_t y;
  41. real_t z;
  42. real_t w;
  43. };
  44. real_t components[4] = { 0, 0, 0, 1.0 };
  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_finite() const;
  55. real_t length() const;
  56. void normalize();
  57. Quaternion normalized() const;
  58. bool is_normalized() const;
  59. Quaternion inverse() const;
  60. Quaternion log() const;
  61. Quaternion exp() const;
  62. _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
  63. real_t angle_to(const Quaternion &p_to) const;
  64. Vector3 get_euler(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
  65. static Quaternion from_euler(const Vector3 &p_euler);
  66. Quaternion slerp(const Quaternion &p_to, real_t p_weight) const;
  67. Quaternion slerpni(const Quaternion &p_to, real_t p_weight) const;
  68. Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight) const;
  69. 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;
  70. Vector3 get_axis() const;
  71. real_t get_angle() const;
  72. _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  73. r_angle = 2 * Math::acos(w);
  74. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  75. r_axis.x = x * r;
  76. r_axis.y = y * r;
  77. r_axis.z = z * r;
  78. }
  79. void operator*=(const Quaternion &p_q);
  80. Quaternion operator*(const Quaternion &p_q) const;
  81. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_v) const {
  82. #ifdef MATH_CHECKS
  83. ERR_FAIL_COND_V_MSG(!is_normalized(), p_v, "The quaternion " + operator String() + " must be normalized.");
  84. #endif
  85. Vector3 u(x, y, z);
  86. Vector3 uv = u.cross(p_v);
  87. return p_v + ((uv * w) + u.cross(uv)) * ((real_t)2);
  88. }
  89. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_v) const {
  90. return inverse().xform(p_v);
  91. }
  92. _FORCE_INLINE_ void operator+=(const Quaternion &p_q);
  93. _FORCE_INLINE_ void operator-=(const Quaternion &p_q);
  94. _FORCE_INLINE_ void operator*=(real_t p_s);
  95. _FORCE_INLINE_ void operator/=(real_t p_s);
  96. _FORCE_INLINE_ Quaternion operator+(const Quaternion &p_q2) const;
  97. _FORCE_INLINE_ Quaternion operator-(const Quaternion &p_q2) const;
  98. _FORCE_INLINE_ Quaternion operator-() const;
  99. _FORCE_INLINE_ Quaternion operator*(real_t p_s) const;
  100. _FORCE_INLINE_ Quaternion operator/(real_t p_s) const;
  101. _FORCE_INLINE_ bool operator==(const Quaternion &p_quaternion) const;
  102. _FORCE_INLINE_ bool operator!=(const Quaternion &p_quaternion) const;
  103. operator String() const;
  104. _FORCE_INLINE_ Quaternion() {}
  105. _FORCE_INLINE_ Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  106. x(p_x),
  107. y(p_y),
  108. z(p_z),
  109. w(p_w) {
  110. }
  111. Quaternion(const Vector3 &p_axis, real_t p_angle);
  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 &p_v0, const Vector3 &p_v1) { // Shortest arc.
  125. Vector3 c = p_v0.cross(p_v1);
  126. real_t d = p_v0.dot(p_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*=(real_t p_s) {
  161. x *= p_s;
  162. y *= p_s;
  163. z *= p_s;
  164. w *= p_s;
  165. }
  166. void Quaternion::operator/=(real_t p_s) {
  167. *this *= 1.0f / p_s;
  168. }
  169. Quaternion Quaternion::operator+(const Quaternion &p_q2) const {
  170. const Quaternion &q1 = *this;
  171. return Quaternion(q1.x + p_q2.x, q1.y + p_q2.y, q1.z + p_q2.z, q1.w + p_q2.w);
  172. }
  173. 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. 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*(real_t p_s) const {
  182. return Quaternion(x * p_s, y * p_s, z * p_s, w * p_s);
  183. }
  184. Quaternion Quaternion::operator/(real_t p_s) const {
  185. return *this * (1.0f / p_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*(real_t p_real, const Quaternion &p_quaternion) {
  194. return p_quaternion * p_real;
  195. }
  196. } // namespace godot
  197. #endif // GODOT_QUATERNION_HPP