quaternion.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* quaternion.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_QUAT_HPP
  31. #define GODOT_QUAT_HPP
  32. #include <godot_cpp/core/math.hpp>
  33. #include <godot_cpp/variant/vector3.hpp>
  34. namespace godot {
  35. class Quaternion {
  36. public:
  37. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  38. union {
  39. struct {
  40. real_t x;
  41. real_t y;
  42. real_t z;
  43. real_t w;
  44. };
  45. real_t components[4] = { 0, 0, 0, 1.0 };
  46. };
  47. inline real_t &operator[](int idx) {
  48. return components[idx];
  49. }
  50. inline const real_t &operator[](int idx) const {
  51. return components[idx];
  52. }
  53. inline real_t length_squared() const;
  54. bool is_equal_approx(const Quaternion &p_quat) const;
  55. real_t length() const;
  56. void normalize();
  57. Quaternion normalized() const;
  58. bool is_normalized() const;
  59. Quaternion inverse() const;
  60. inline real_t dot(const Quaternion &p_q) const;
  61. Vector3 get_euler_xyz() const;
  62. Vector3 get_euler_yxz() const;
  63. Vector3 get_euler() const { return get_euler_yxz(); };
  64. Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const;
  65. Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const;
  66. Quaternion cubic_slerp(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const;
  67. inline void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  68. r_angle = 2 * Math::acos(w);
  69. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  70. r_axis.x = x * r;
  71. r_axis.y = y * r;
  72. r_axis.z = z * r;
  73. }
  74. void operator*=(const Quaternion &p_q);
  75. Quaternion operator*(const Quaternion &p_q) const;
  76. Quaternion operator*(const Vector3 &v) const {
  77. return Quaternion(w * v.x + y * v.z - z * v.y,
  78. w * v.y + z * v.x - x * v.z,
  79. w * v.z + x * v.y - y * v.x,
  80. -x * v.x - y * v.y - z * v.z);
  81. }
  82. inline Vector3 xform(const Vector3 &v) const {
  83. #ifdef MATH_CHECKS
  84. ERR_FAIL_COND_V(!is_normalized(), v);
  85. #endif
  86. Vector3 u(x, y, z);
  87. Vector3 uv = u.cross(v);
  88. return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
  89. }
  90. inline Vector3 xform_inv(const Vector3 &v) const {
  91. return inverse().xform(v);
  92. }
  93. inline void operator+=(const Quaternion &p_q);
  94. inline void operator-=(const Quaternion &p_q);
  95. inline void operator*=(const real_t &s);
  96. inline void operator/=(const real_t &s);
  97. inline Quaternion operator+(const Quaternion &q2) const;
  98. inline Quaternion operator-(const Quaternion &q2) const;
  99. inline Quaternion operator-() const;
  100. inline Quaternion operator*(const real_t &s) const;
  101. inline Quaternion operator/(const real_t &s) const;
  102. inline bool operator==(const Quaternion &p_quat) const;
  103. inline bool operator!=(const Quaternion &p_quat) const;
  104. operator String() const;
  105. inline Quaternion() {}
  106. inline Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  107. x(p_x),
  108. y(p_y),
  109. z(p_z),
  110. w(p_w) {
  111. }
  112. Quaternion(const Vector3 &p_axis, real_t p_angle);
  113. Quaternion(const Vector3 &p_euler);
  114. Quaternion(const Quaternion &p_q) :
  115. x(p_q.x),
  116. y(p_q.y),
  117. z(p_q.z),
  118. w(p_q.w) {
  119. }
  120. Quaternion &operator=(const Quaternion &p_q) {
  121. x = p_q.x;
  122. y = p_q.y;
  123. z = p_q.z;
  124. w = p_q.w;
  125. return *this;
  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.0 + CMP_EPSILON) {
  132. x = 0;
  133. y = 1;
  134. z = 0;
  135. w = 0;
  136. } else {
  137. real_t s = Math::sqrt((1.0 + d) * 2.0);
  138. real_t rs = 1.0 / s;
  139. x = c.x * rs;
  140. y = c.y * rs;
  141. z = c.z * rs;
  142. w = s * 0.5;
  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.0 / 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.0 / s);
  190. }
  191. bool Quaternion::operator==(const Quaternion &p_quat) const {
  192. return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
  193. }
  194. bool Quaternion::operator!=(const Quaternion &p_quat) const {
  195. return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
  196. }
  197. inline Quaternion operator*(const real_t &p_real, const Quaternion &p_quat) {
  198. return p_quat * p_real;
  199. }
  200. } // namespace godot
  201. #endif // GODOT_QUAT_HPP