quat.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*************************************************************************/
  2. /* quat.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef QUAT_H
  30. #define QUAT_H
  31. #include "math_defs.h"
  32. #include "math_funcs.h"
  33. #include "ustring.h"
  34. #include "vector3.h"
  35. /**
  36. @author Juan Linietsky <[email protected]>
  37. */
  38. class Quat{
  39. public:
  40. real_t x,y,z,w;
  41. _FORCE_INLINE_ real_t length_squared() const;
  42. real_t length() const;
  43. void normalize();
  44. Quat normalized() const;
  45. Quat inverse() const;
  46. _FORCE_INLINE_ real_t dot(const Quat& q) const;
  47. void set_euler(const Vector3& p_euler);
  48. Quat slerp(const Quat& q, const real_t& t) const;
  49. Quat slerpni(const Quat& q, const real_t& t) const;
  50. Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const;
  51. _FORCE_INLINE_ void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
  52. r_angle = 2 * Math::acos(w);
  53. r_axis.x = -x / Math::sqrt(1-w*w);
  54. r_axis.y = -y / Math::sqrt(1-w*w);
  55. r_axis.z = -z / Math::sqrt(1-w*w);
  56. }
  57. void operator*=(const Quat& q);
  58. Quat operator*(const Quat& q) const;
  59. Quat operator*(const Vector3& v) const
  60. {
  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) {
  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; y=p_y; z=p_z; w=p_w;
  85. }
  86. inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  87. x=p_x; y=p_y; z=p_z; w=p_w;
  88. }
  89. Quat(const Vector3& axis, const real_t& angle);
  90. Quat(const Vector3& v0, const Vector3& v1) // shortest arc
  91. {
  92. Vector3 c = v0.cross(v1);
  93. real_t d = v0.dot(v1);
  94. if (d < -1.0 + CMP_EPSILON) {
  95. x=0;
  96. y=1;
  97. z=0;
  98. w=0;
  99. } else {
  100. real_t s = Math::sqrt((1.0f + d) * 2.0f);
  101. real_t rs = 1.0f / s;
  102. x=c.x*rs;
  103. y=c.y*rs;
  104. z=c.z*rs;
  105. w=s * 0.5;
  106. }
  107. }
  108. inline Quat() {x=y=z=0; w=1; }
  109. };
  110. real_t Quat::dot(const Quat& q) const {
  111. return x * q.x+y * q.y+z * q.z+w * q.w;
  112. }
  113. real_t Quat::length_squared() const {
  114. return dot(*this);
  115. }
  116. void Quat::operator+=(const Quat& q) {
  117. x += q.x; y += q.y; z += q.z; w += q.w;
  118. }
  119. void Quat::operator-=(const Quat& q) {
  120. x -= q.x; y -= q.y; z -= q.z; w -= q.w;
  121. }
  122. void Quat::operator*=(const real_t& s) {
  123. x *= s; y *= s; z *= s; w *= s;
  124. }
  125. void Quat::operator/=(const real_t& s) {
  126. *this *= 1.0 / s;
  127. }
  128. Quat Quat::operator+(const Quat& q2) const {
  129. const Quat& q1 = *this;
  130. return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
  131. }
  132. Quat Quat::operator-(const Quat& q2) const {
  133. const Quat& q1 = *this;
  134. return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
  135. }
  136. Quat Quat::operator-() const {
  137. const Quat& q2 = *this;
  138. return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
  139. }
  140. Quat Quat::operator*(const real_t& s) const {
  141. return Quat(x * s, y * s, z * s, w * s);
  142. }
  143. Quat Quat::operator/(const real_t& s) const {
  144. return *this * (1.0 / s);
  145. }
  146. bool Quat::operator==(const Quat& p_quat) const {
  147. return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
  148. }
  149. bool Quat::operator!=(const Quat& p_quat) const {
  150. return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
  151. }
  152. #endif