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