Quat.cpp 7.5 KB

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