quat.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifndef QUAT_H
  31. #define QUAT_H
  32. #include "math_defs.h"
  33. #include "math_funcs.h"
  34. #include "ustring.h"
  35. #include "vector3.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. Quat inverse() const;
  47. _FORCE_INLINE_ real_t dot(const Quat &q) const;
  48. void set_euler(const Vector3 &p_euler);
  49. Quat slerp(const Quat &q, const real_t &t) const;
  50. Quat slerpni(const Quat &q, const real_t &t) const;
  51. Quat cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const;
  52. _FORCE_INLINE_ void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
  53. r_angle = 2 * Math::acos(w);
  54. r_axis.x = -x / Math::sqrt(1 - w * w);
  55. r_axis.y = -y / Math::sqrt(1 - w * w);
  56. r_axis.z = -z / Math::sqrt(1 - w * w);
  57. }
  58. void operator*=(const Quat &q);
  59. Quat operator*(const Quat &q) const;
  60. Quat operator*(const Vector3 &v) const {
  61. return Quat(w * v.x + y * v.z - z * v.y,
  62. w * v.y + z * v.x - x * v.z,
  63. w * v.z + x * v.y - y * v.x,
  64. -x * v.x - y * v.y - z * v.z);
  65. }
  66. _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const {
  67. Quat q = *this * v;
  68. q *= this->inverse();
  69. return Vector3(q.x, q.y, q.z);
  70. }
  71. _FORCE_INLINE_ void operator+=(const Quat &q);
  72. _FORCE_INLINE_ void operator-=(const Quat &q);
  73. _FORCE_INLINE_ void operator*=(const real_t &s);
  74. _FORCE_INLINE_ void operator/=(const real_t &s);
  75. _FORCE_INLINE_ Quat operator+(const Quat &q2) const;
  76. _FORCE_INLINE_ Quat operator-(const Quat &q2) const;
  77. _FORCE_INLINE_ Quat operator-() const;
  78. _FORCE_INLINE_ Quat operator*(const real_t &s) const;
  79. _FORCE_INLINE_ Quat operator/(const real_t &s) const;
  80. _FORCE_INLINE_ bool operator==(const Quat &p_quat) const;
  81. _FORCE_INLINE_ bool operator!=(const Quat &p_quat) const;
  82. operator String() const;
  83. inline void set(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  84. x = p_x;
  85. y = p_y;
  86. z = p_z;
  87. w = p_w;
  88. }
  89. inline Quat(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. Quat(const Vector3 &axis, const real_t &angle);
  96. Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc
  97. {
  98. Vector3 c = v0.cross(v1);
  99. real_t d = v0.dot(v1);
  100. if (d < -1.0 + CMP_EPSILON) {
  101. x = 0;
  102. y = 1;
  103. z = 0;
  104. w = 0;
  105. } else {
  106. real_t s = Math::sqrt((1.0f + d) * 2.0f);
  107. real_t rs = 1.0f / s;
  108. x = c.x * rs;
  109. y = c.y * rs;
  110. z = c.z * rs;
  111. w = s * 0.5;
  112. }
  113. }
  114. inline Quat() {
  115. x = y = z = 0;
  116. w = 1;
  117. }
  118. };
  119. real_t Quat::dot(const Quat &q) const {
  120. return x * q.x + y * q.y + z * q.z + w * q.w;
  121. }
  122. real_t Quat::length_squared() const {
  123. return dot(*this);
  124. }
  125. void Quat::operator+=(const Quat &q) {
  126. x += q.x;
  127. y += q.y;
  128. z += q.z;
  129. w += q.w;
  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 real_t &s) {
  138. x *= s;
  139. y *= s;
  140. z *= s;
  141. w *= s;
  142. }
  143. void Quat::operator/=(const real_t &s) {
  144. *this *= 1.0 / s;
  145. }
  146. Quat Quat::operator+(const Quat &q2) const {
  147. const Quat &q1 = *this;
  148. return Quat(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
  149. }
  150. Quat Quat::operator-(const Quat &q2) const {
  151. const Quat &q1 = *this;
  152. return Quat(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
  153. }
  154. Quat Quat::operator-() const {
  155. const Quat &q2 = *this;
  156. return Quat(-q2.x, -q2.y, -q2.z, -q2.w);
  157. }
  158. Quat Quat::operator*(const real_t &s) const {
  159. return Quat(x * s, y * s, z * s, w * s);
  160. }
  161. Quat Quat::operator/(const real_t &s) const {
  162. return *this * (1.0 / s);
  163. }
  164. bool Quat::operator==(const Quat &p_quat) const {
  165. return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
  166. }
  167. bool Quat::operator!=(const Quat &p_quat) const {
  168. return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
  169. }
  170. #endif