Quat.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #ifndef QUAT_H
  2. #define QUAT_H
  3. #include <cmath>
  4. #include "Vector3.h"
  5. // #include "Basis.h"
  6. namespace godot {
  7. #define CMP_EPSILON 0.00001
  8. typedef float real_t;
  9. class Quat{
  10. public:
  11. real_t x,y,z,w;
  12. real_t length_squared() const; // down below
  13. real_t length() const
  14. {
  15. return ::sqrt(length_squared());
  16. }
  17. void normalize()
  18. {
  19. *this /= length();
  20. }
  21. Quat normalized() const
  22. {
  23. return *this / length();
  24. }
  25. Quat inverse() const
  26. {
  27. return Quat( -x, -y, -z, w );
  28. }
  29. real_t dot(const Quat& q) const; // down below
  30. void set_euler(const Vector3& p_euler)
  31. {
  32. real_t half_a1 = p_euler.x * 0.5;
  33. real_t half_a2 = p_euler.y * 0.5;
  34. real_t half_a3 = p_euler.z * 0.5;
  35. // R = X(a1).Y(a2).Z(a3) convention for Euler angles.
  36. // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-2)
  37. // a3 is the angle of the first rotation, following the notation in this reference.
  38. real_t cos_a1 = ::cos(half_a1);
  39. real_t sin_a1 = ::sin(half_a1);
  40. real_t cos_a2 = ::cos(half_a2);
  41. real_t sin_a2 = ::sin(half_a2);
  42. real_t cos_a3 = ::cos(half_a3);
  43. real_t sin_a3 = ::sin(half_a3);
  44. set(sin_a1*cos_a2*cos_a3 + sin_a2*sin_a3*cos_a1,
  45. -sin_a1*sin_a3*cos_a2 + sin_a2*cos_a1*cos_a3,
  46. sin_a1*sin_a2*cos_a3 + sin_a3*cos_a1*cos_a2,
  47. -sin_a1*sin_a2*sin_a3 + cos_a1*cos_a2*cos_a3);
  48. }
  49. Vector3 get_euler() const; // down below
  50. Quat slerp(const Quat& q, const real_t& t) const {
  51. Quat to1;
  52. real_t omega, cosom, sinom, scale0, scale1;
  53. // calc cosine
  54. cosom = dot(q);
  55. // adjust signs (if necessary)
  56. if ( cosom <0.0 ) {
  57. cosom = -cosom;
  58. to1.x = - q.x;
  59. to1.y = - q.y;
  60. to1.z = - q.z;
  61. to1.w = - q.w;
  62. } else {
  63. to1.x = q.x;
  64. to1.y = q.y;
  65. to1.z = q.z;
  66. to1.w = q.w;
  67. }
  68. // calculate coefficients
  69. if ( (1.0 - cosom) > CMP_EPSILON ) {
  70. // standard case (slerp)
  71. omega = ::acos(cosom);
  72. sinom = ::sin(omega);
  73. scale0 = ::sin((1.0 - t) * omega) / sinom;
  74. scale1 = ::sin(t * omega) / sinom;
  75. } else {
  76. // "from" and "to" quaternions are very close
  77. // ... so we can do a linear interpolation
  78. scale0 = 1.0 - t;
  79. scale1 = t;
  80. }
  81. // calculate final values
  82. return Quat(
  83. scale0 * x + scale1 * to1.x,
  84. scale0 * y + scale1 * to1.y,
  85. scale0 * z + scale1 * to1.z,
  86. scale0 * w + scale1 * to1.w
  87. );
  88. }
  89. Quat slerpni(const Quat& q, const real_t& t) const {
  90. const Quat &from = *this;
  91. real_t dot = from.dot(q);
  92. if (::fabs(dot) > 0.9999) return from;
  93. real_t theta = ::acos(dot),
  94. sinT = 1.0 / ::sin(theta),
  95. newFactor = ::sin(t * theta) * sinT,
  96. invFactor = ::sin((1.0 - t) * theta) * sinT;
  97. return Quat(invFactor * from.x + newFactor * q.x,
  98. invFactor * from.y + newFactor * q.y,
  99. invFactor * from.z + newFactor * q.z,
  100. invFactor * from.w + newFactor * q.w);
  101. }
  102. Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const
  103. {
  104. //the only way to do slerp :|
  105. real_t t2 = (1.0-t)*t*2;
  106. Quat sp = this->slerp(q,t);
  107. Quat sq = prep.slerpni(postq,t);
  108. return sp.slerpni(sq,t2);
  109. }
  110. void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
  111. r_angle = 2 * ::acos(w);
  112. r_axis.x = x / ::sqrt(1-w*w);
  113. r_axis.y = y / ::sqrt(1-w*w);
  114. r_axis.z = z / ::sqrt(1-w*w);
  115. }
  116. void operator*=(const Quat& q); // down below
  117. Quat operator*(const Quat& q) const; // down below
  118. Quat operator*(const Vector3& v) const
  119. {
  120. return Quat( w * v.x + y * v.z - z * v.y,
  121. w * v.y + z * v.x - x * v.z,
  122. w * v.z + x * v.y - y * v.x,
  123. -x * v.x - y * v.y - z * v.z);
  124. }
  125. Vector3 xform(const Vector3& v) const {
  126. Quat q = *this * v;
  127. q *= this->inverse();
  128. return Vector3(q.x,q.y,q.z);
  129. }
  130. // everything's down
  131. void operator+=(const Quat& q);
  132. void operator-=(const Quat& q);
  133. void operator*=(const real_t& s);
  134. void operator/=(const real_t& s);
  135. Quat operator+(const Quat& q2) const;
  136. Quat operator-(const Quat& q2) const;
  137. Quat operator-() const;
  138. Quat operator*(const real_t& s) const;
  139. Quat operator/(const real_t& s) const;
  140. bool operator==(const Quat& p_quat) const;
  141. bool operator!=(const Quat& p_quat) const;
  142. operator String() const
  143. {
  144. return String(); // @Todo
  145. }
  146. inline void set( real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  147. x=p_x; y=p_y; z=p_z; w=p_w;
  148. }
  149. inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  150. x=p_x; y=p_y; z=p_z; w=p_w;
  151. }
  152. Quat(const Vector3& axis, const real_t& angle)
  153. {
  154. real_t d = axis.length();
  155. if (d==0)
  156. set(0,0,0,0);
  157. else {
  158. real_t sin_angle = ::sin(angle * 0.5);
  159. real_t cos_angle = ::cos(angle * 0.5);
  160. real_t s = sin_angle / d;
  161. set(axis.x * s, axis.y * s, axis.z * s,
  162. cos_angle);
  163. }
  164. }
  165. Quat(const Vector3& v0, const Vector3& v1) // shortest arc
  166. {
  167. Vector3 c = v0.cross(v1);
  168. real_t d = v0.dot(v1);
  169. if (d < -1.0 + CMP_EPSILON) {
  170. x=0;
  171. y=1;
  172. z=0;
  173. w=0;
  174. } else {
  175. real_t s = ::sqrt((1.0 + d) * 2.0);
  176. real_t rs = 1.0 / s;
  177. x=c.x*rs;
  178. y=c.y*rs;
  179. z=c.z*rs;
  180. w=s * 0.5;
  181. }
  182. }
  183. Quat() {x=y=z=0; w=1; }
  184. };
  185. real_t Quat::dot(const Quat& q) const {
  186. return x * q.x+y * q.y+z * q.z+w * q.w;
  187. }
  188. real_t Quat::length_squared() const {
  189. return dot(*this);
  190. }
  191. void Quat::operator+=(const Quat& q) {
  192. x += q.x; y += q.y; z += q.z; w += q.w;
  193. }
  194. void Quat::operator-=(const Quat& q) {
  195. x -= q.x; y -= q.y; z -= q.z; w -= q.w;
  196. }
  197. void Quat::operator*=(const Quat& q) {
  198. x *= q.x; y *= q.y; z *= q.z; w *= q.w;
  199. }
  200. void Quat::operator*=(const real_t& s) {
  201. x *= s; y *= s; z *= s; w *= s;
  202. }
  203. void Quat::operator/=(const real_t& s) {
  204. *this *= 1.0 / s;
  205. }
  206. Quat Quat::operator+(const Quat& q2) const {
  207. const Quat& q1 = *this;
  208. return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
  209. }
  210. Quat Quat::operator-(const Quat& q2) const {
  211. const Quat& q1 = *this;
  212. return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
  213. }
  214. Quat Quat::operator*(const Quat& q2) const {
  215. Quat q1 = *this;
  216. q1 *= q2;
  217. return q1;
  218. }
  219. Quat Quat::operator-() const {
  220. const Quat& q2 = *this;
  221. return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
  222. }
  223. Quat Quat::operator*(const real_t& s) const {
  224. return Quat(x * s, y * s, z * s, w * s);
  225. }
  226. Quat Quat::operator/(const real_t& s) const {
  227. return *this * (1.0 / s);
  228. }
  229. bool Quat::operator==(const Quat& p_quat) const {
  230. return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
  231. }
  232. bool Quat::operator!=(const Quat& p_quat) const {
  233. return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
  234. }
  235. }
  236. #include "Basis.h"
  237. namespace godot {
  238. Vector3 Quat::get_euler() const
  239. {
  240. Basis m(*this);
  241. return m.get_euler();
  242. }
  243. }
  244. #endif // QUAT_H