quat.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*************************************************************************/
  2. /* quat.h */
  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 QUAT_H
  31. #define QUAT_H
  32. #include "core/math/math_defs.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/math/vector3.h"
  35. #include "core/ustring.h"
  36. class Quat {
  37. public:
  38. real_t x, y, z, w;
  39. _FORCE_INLINE_ real_t length_squared() const;
  40. bool is_equal_approx(const Quat &p_quat) const;
  41. real_t length() const;
  42. void normalize();
  43. Quat normalized() const;
  44. bool is_normalized() const;
  45. Quat inverse() const;
  46. _FORCE_INLINE_ real_t dot(const Quat &p_q) const;
  47. void set_euler_xyz(const Vector3 &p_euler);
  48. Vector3 get_euler_xyz() const;
  49. void set_euler_yxz(const Vector3 &p_euler);
  50. Vector3 get_euler_yxz() const;
  51. void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); };
  52. Vector3 get_euler() const { return get_euler_yxz(); };
  53. Quat slerp(const Quat &p_to, const real_t &p_weight) const;
  54. Quat slerpni(const Quat &p_to, const real_t &p_weight) const;
  55. Quat cubic_slerp(const Quat &p_b, const Quat &p_pre_a, const Quat &p_post_b, const real_t &p_weight) const;
  56. void set_axis_angle(const Vector3 &axis, const real_t &angle);
  57. _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  58. r_angle = 2 * Math::acos(w);
  59. real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
  60. r_axis.x = x * r;
  61. r_axis.y = y * r;
  62. r_axis.z = z * r;
  63. }
  64. void operator*=(const Quat &p_q);
  65. Quat operator*(const Quat &p_q) const;
  66. Quat operator*(const Vector3 &v) const {
  67. return Quat(w * v.x + y * v.z - z * v.y,
  68. w * v.y + z * v.x - x * v.z,
  69. w * v.z + x * v.y - y * v.x,
  70. -x * v.x - y * v.y - z * v.z);
  71. }
  72. _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const {
  73. #ifdef MATH_CHECKS
  74. ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized.");
  75. #endif
  76. Vector3 u(x, y, z);
  77. Vector3 uv = u.cross(v);
  78. return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
  79. }
  80. _FORCE_INLINE_ void operator+=(const Quat &p_q);
  81. _FORCE_INLINE_ void operator-=(const Quat &p_q);
  82. _FORCE_INLINE_ void operator*=(const real_t &s);
  83. _FORCE_INLINE_ void operator/=(const real_t &s);
  84. _FORCE_INLINE_ Quat operator+(const Quat &q2) const;
  85. _FORCE_INLINE_ Quat operator-(const Quat &q2) const;
  86. _FORCE_INLINE_ Quat operator-() const;
  87. _FORCE_INLINE_ Quat operator*(const real_t &s) const;
  88. _FORCE_INLINE_ Quat operator/(const real_t &s) const;
  89. _FORCE_INLINE_ bool operator==(const Quat &p_quat) const;
  90. _FORCE_INLINE_ bool operator!=(const Quat &p_quat) const;
  91. operator String() const;
  92. inline void set(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  93. x = p_x;
  94. y = p_y;
  95. z = p_z;
  96. w = p_w;
  97. }
  98. inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  99. x(p_x),
  100. y(p_y),
  101. z(p_z),
  102. w(p_w) {
  103. }
  104. Quat(const Vector3 &axis, const real_t &angle) { set_axis_angle(axis, angle); }
  105. Quat(const Vector3 &euler) { set_euler(euler); }
  106. Quat(const Quat &p_q) :
  107. x(p_q.x),
  108. y(p_q.y),
  109. z(p_q.z),
  110. w(p_q.w) {
  111. }
  112. Quat operator=(const Quat &p_q) {
  113. x = p_q.x;
  114. y = p_q.y;
  115. z = p_q.z;
  116. w = p_q.w;
  117. return *this;
  118. }
  119. Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc
  120. {
  121. Vector3 c = v0.cross(v1);
  122. real_t d = v0.dot(v1);
  123. if (d < -1.0 + CMP_EPSILON) {
  124. x = 0;
  125. y = 1;
  126. z = 0;
  127. w = 0;
  128. } else {
  129. real_t s = Math::sqrt((1.0 + d) * 2.0);
  130. real_t rs = 1.0 / s;
  131. x = c.x * rs;
  132. y = c.y * rs;
  133. z = c.z * rs;
  134. w = s * 0.5;
  135. }
  136. }
  137. inline Quat() :
  138. x(0),
  139. y(0),
  140. z(0),
  141. w(1) {
  142. }
  143. };
  144. real_t Quat::dot(const Quat &p_q) const {
  145. return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w;
  146. }
  147. real_t Quat::length_squared() const {
  148. return dot(*this);
  149. }
  150. void Quat::operator+=(const Quat &p_q) {
  151. x += p_q.x;
  152. y += p_q.y;
  153. z += p_q.z;
  154. w += p_q.w;
  155. }
  156. void Quat::operator-=(const Quat &p_q) {
  157. x -= p_q.x;
  158. y -= p_q.y;
  159. z -= p_q.z;
  160. w -= p_q.w;
  161. }
  162. void Quat::operator*=(const real_t &s) {
  163. x *= s;
  164. y *= s;
  165. z *= s;
  166. w *= s;
  167. }
  168. void Quat::operator/=(const real_t &s) {
  169. *this *= 1.0 / s;
  170. }
  171. Quat Quat::operator+(const Quat &q2) const {
  172. const Quat &q1 = *this;
  173. return Quat(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
  174. }
  175. Quat Quat::operator-(const Quat &q2) const {
  176. const Quat &q1 = *this;
  177. return Quat(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
  178. }
  179. Quat Quat::operator-() const {
  180. const Quat &q2 = *this;
  181. return Quat(-q2.x, -q2.y, -q2.z, -q2.w);
  182. }
  183. Quat Quat::operator*(const real_t &s) const {
  184. return Quat(x * s, y * s, z * s, w * s);
  185. }
  186. Quat Quat::operator/(const real_t &s) const {
  187. return *this * (1.0 / s);
  188. }
  189. bool Quat::operator==(const Quat &p_quat) const {
  190. return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
  191. }
  192. bool Quat::operator!=(const Quat &p_quat) const {
  193. return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
  194. }
  195. #endif