quat.h 6.1 KB

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