quat.h 6.7 KB

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