Quat.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef QUAT_H
  2. #define QUAT_H
  3. #include <cmath>
  4. #include "Vector3.h"
  5. namespace godot {
  6. #define CMP_EPSILON 0.00001
  7. typedef float real_t;
  8. class Quat{
  9. public:
  10. real_t x,y,z,w;
  11. real_t length_squared() const;
  12. real_t length() const;
  13. void normalize();
  14. Quat normalized() const;
  15. Quat inverse() const;
  16. real_t dot(const Quat& q) const;
  17. void set_euler(const Vector3& p_euler);
  18. Vector3 get_euler() const;
  19. Quat slerp(const Quat& q, const real_t& t) const;
  20. Quat slerpni(const Quat& q, const real_t& t) const;
  21. Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const;
  22. void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
  23. r_angle = 2 * ::acos(w);
  24. r_axis.x = x / ::sqrt(1-w*w);
  25. r_axis.y = y / ::sqrt(1-w*w);
  26. r_axis.z = z / ::sqrt(1-w*w);
  27. }
  28. void operator*=(const Quat& q);
  29. Quat operator*(const Quat& q) const;
  30. Quat operator*(const Vector3& v) const
  31. {
  32. return Quat( w * v.x + y * v.z - z * v.y,
  33. w * v.y + z * v.x - x * v.z,
  34. w * v.z + x * v.y - y * v.x,
  35. -x * v.x - y * v.y - z * v.z);
  36. }
  37. Vector3 xform(const Vector3& v) const {
  38. Quat q = *this * v;
  39. q *= this->inverse();
  40. return Vector3(q.x,q.y,q.z);
  41. }
  42. void operator+=(const Quat& q);
  43. void operator-=(const Quat& q);
  44. void operator*=(const real_t& s);
  45. void operator/=(const real_t& s);
  46. Quat operator+(const Quat& q2) const;
  47. Quat operator-(const Quat& q2) const;
  48. Quat operator-() const;
  49. Quat operator*(const real_t& s) const;
  50. Quat operator/(const real_t& s) const;
  51. bool operator==(const Quat& p_quat) const;
  52. bool operator!=(const Quat& p_quat) const;
  53. operator String() const;
  54. inline void set( real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  55. x=p_x; y=p_y; z=p_z; w=p_w;
  56. }
  57. inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  58. x=p_x; y=p_y; z=p_z; w=p_w;
  59. }
  60. Quat(const Vector3& axis, const real_t& angle);
  61. Quat(const Vector3& v0, const Vector3& v1) // shortest arc
  62. {
  63. Vector3 c = v0.cross(v1);
  64. real_t d = v0.dot(v1);
  65. if (d < -1.0 + CMP_EPSILON) {
  66. x=0;
  67. y=1;
  68. z=0;
  69. w=0;
  70. } else {
  71. real_t s = ::sqrt((1.0 + d) * 2.0);
  72. real_t rs = 1.0 / s;
  73. x=c.x*rs;
  74. y=c.y*rs;
  75. z=c.z*rs;
  76. w=s * 0.5;
  77. }
  78. }
  79. inline Quat() {x=y=z=0; w=1; }
  80. };
  81. real_t Quat::dot(const Quat& q) const {
  82. return x * q.x+y * q.y+z * q.z+w * q.w;
  83. }
  84. real_t Quat::length_squared() const {
  85. return dot(*this);
  86. }
  87. void Quat::operator+=(const Quat& q) {
  88. x += q.x; y += q.y; z += q.z; w += q.w;
  89. }
  90. void Quat::operator-=(const Quat& q) {
  91. x -= q.x; y -= q.y; z -= q.z; w -= q.w;
  92. }
  93. void Quat::operator*=(const real_t& s) {
  94. x *= s; y *= s; z *= s; w *= s;
  95. }
  96. void Quat::operator/=(const real_t& s) {
  97. *this *= 1.0 / s;
  98. }
  99. Quat Quat::operator+(const Quat& q2) const {
  100. const Quat& q1 = *this;
  101. return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
  102. }
  103. Quat Quat::operator-(const Quat& q2) const {
  104. const Quat& q1 = *this;
  105. return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
  106. }
  107. Quat Quat::operator-() const {
  108. const Quat& q2 = *this;
  109. return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
  110. }
  111. Quat Quat::operator*(const real_t& s) const {
  112. return Quat(x * s, y * s, z * s, w * s);
  113. }
  114. Quat Quat::operator/(const real_t& s) const {
  115. return *this * (1.0 / s);
  116. }
  117. bool Quat::operator==(const Quat& p_quat) const {
  118. return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
  119. }
  120. bool Quat::operator!=(const Quat& p_quat) const {
  121. return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
  122. }
  123. }
  124. #endif // QUAT_H