Quat.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. bool Quat::is_normalized() const {
  77. return std::abs(length_squared() - 1.0) < 0.00001;
  78. }
  79. Quat Quat::inverse() const {
  80. return Quat(-x, -y, -z, w);
  81. }
  82. Quat Quat::slerp(const Quat &q, const real_t &t) const {
  83. Quat to1;
  84. real_t omega, cosom, sinom, scale0, scale1;
  85. // calc cosine
  86. cosom = dot(q);
  87. // adjust signs (if necessary)
  88. if (cosom < 0.0) {
  89. cosom = -cosom;
  90. to1.x = -q.x;
  91. to1.y = -q.y;
  92. to1.z = -q.z;
  93. to1.w = -q.w;
  94. } else {
  95. to1.x = q.x;
  96. to1.y = q.y;
  97. to1.z = q.z;
  98. to1.w = q.w;
  99. }
  100. // calculate coefficients
  101. if ((1.0 - cosom) > CMP_EPSILON) {
  102. // standard case (slerp)
  103. omega = ::acos(cosom);
  104. sinom = ::sin(omega);
  105. scale0 = ::sin((1.0 - t) * omega) / sinom;
  106. scale1 = ::sin(t * omega) / sinom;
  107. } else {
  108. // "from" and "to" quaternions are very close
  109. // ... so we can do a linear interpolation
  110. scale0 = 1.0 - t;
  111. scale1 = t;
  112. }
  113. // calculate final values
  114. return Quat(
  115. scale0 * x + scale1 * to1.x,
  116. scale0 * y + scale1 * to1.y,
  117. scale0 * z + scale1 * to1.z,
  118. scale0 * w + scale1 * to1.w);
  119. }
  120. Quat Quat::slerpni(const Quat &q, const real_t &t) const {
  121. const Quat &from = *this;
  122. real_t dot = from.dot(q);
  123. if (::fabs(dot) > 0.9999) return from;
  124. real_t theta = ::acos(dot),
  125. sinT = 1.0 / ::sin(theta),
  126. newFactor = ::sin(t * theta) * sinT,
  127. invFactor = ::sin((1.0 - t) * theta) * sinT;
  128. return Quat(invFactor * from.x + newFactor * q.x,
  129. invFactor * from.y + newFactor * q.y,
  130. invFactor * from.z + newFactor * q.z,
  131. invFactor * from.w + newFactor * q.w);
  132. }
  133. Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const {
  134. //the only way to do slerp :|
  135. real_t t2 = (1.0 - t) * t * 2;
  136. Quat sp = this->slerp(q, t);
  137. Quat sq = prep.slerpni(postq, t);
  138. return sp.slerpni(sq, t2);
  139. }
  140. void Quat::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const {
  141. r_angle = 2 * ::acos(w);
  142. r_axis.x = x / ::sqrt(1 - w * w);
  143. r_axis.y = y / ::sqrt(1 - w * w);
  144. r_axis.z = z / ::sqrt(1 - w * w);
  145. }
  146. void Quat::set_axis_angle(const Vector3 &axis, const float angle) {
  147. ERR_FAIL_COND(!axis.is_normalized());
  148. real_t d = axis.length();
  149. if (d == 0)
  150. set(0, 0, 0, 0);
  151. else {
  152. real_t sin_angle = ::sin(angle * 0.5);
  153. real_t cos_angle = ::cos(angle * 0.5);
  154. real_t s = sin_angle / d;
  155. set(axis.x * s, axis.y * s, axis.z * s,
  156. cos_angle);
  157. }
  158. }
  159. Quat Quat::operator*(const Vector3 &v) const {
  160. return Quat(w * v.x + y * v.z - z * v.y,
  161. w * v.y + z * v.x - x * v.z,
  162. w * v.z + x * v.y - y * v.x,
  163. -x * v.x - y * v.y - z * v.z);
  164. }
  165. Vector3 Quat::xform(const Vector3 &v) const {
  166. Quat q = *this * v;
  167. q *= this->inverse();
  168. return Vector3(q.x, q.y, q.z);
  169. }
  170. Quat::operator String() const {
  171. return String(); // @Todo
  172. }
  173. Quat::Quat(const Vector3 &axis, const real_t &angle) {
  174. real_t d = axis.length();
  175. if (d == 0)
  176. set(0, 0, 0, 0);
  177. else {
  178. real_t sin_angle = ::sin(angle * 0.5);
  179. real_t cos_angle = ::cos(angle * 0.5);
  180. real_t s = sin_angle / d;
  181. set(axis.x * s, axis.y * s, axis.z * s,
  182. cos_angle);
  183. }
  184. }
  185. Quat::Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc
  186. {
  187. Vector3 c = v0.cross(v1);
  188. real_t d = v0.dot(v1);
  189. if (d < -1.0 + CMP_EPSILON) {
  190. x = 0;
  191. y = 1;
  192. z = 0;
  193. w = 0;
  194. } else {
  195. real_t s = ::sqrt((1.0 + d) * 2.0);
  196. real_t rs = 1.0 / s;
  197. x = c.x * rs;
  198. y = c.y * rs;
  199. z = c.z * rs;
  200. w = s * 0.5;
  201. }
  202. }
  203. real_t Quat::dot(const Quat &q) const {
  204. return x * q.x + y * q.y + z * q.z + w * q.w;
  205. }
  206. real_t Quat::length_squared() const {
  207. return dot(*this);
  208. }
  209. void Quat::operator+=(const Quat &q) {
  210. x += q.x;
  211. y += q.y;
  212. z += q.z;
  213. w += q.w;
  214. }
  215. void Quat::operator-=(const Quat &q) {
  216. x -= q.x;
  217. y -= q.y;
  218. z -= q.z;
  219. w -= q.w;
  220. }
  221. void Quat::operator*=(const Quat &q) {
  222. set(w * q.x + x * q.w + y * q.z - z * q.y,
  223. w * q.y + y * q.w + z * q.x - x * q.z,
  224. w * q.z + z * q.w + x * q.y - y * q.x,
  225. w * q.w - x * q.x - y * q.y - z * q.z);
  226. }
  227. void Quat::operator*=(const real_t &s) {
  228. x *= s;
  229. y *= s;
  230. z *= s;
  231. w *= s;
  232. }
  233. void Quat::operator/=(const real_t &s) {
  234. *this *= 1.0 / s;
  235. }
  236. Quat Quat::operator+(const Quat &q2) const {
  237. const Quat &q1 = *this;
  238. return Quat(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
  239. }
  240. Quat Quat::operator-(const Quat &q2) const {
  241. const Quat &q1 = *this;
  242. return Quat(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
  243. }
  244. Quat Quat::operator*(const Quat &q2) const {
  245. Quat q1 = *this;
  246. q1 *= q2;
  247. return q1;
  248. }
  249. Quat Quat::operator-() const {
  250. const Quat &q2 = *this;
  251. return Quat(-q2.x, -q2.y, -q2.z, -q2.w);
  252. }
  253. Quat Quat::operator*(const real_t &s) const {
  254. return Quat(x * s, y * s, z * s, w * s);
  255. }
  256. Quat Quat::operator/(const real_t &s) const {
  257. return *this * (1.0 / s);
  258. }
  259. bool Quat::operator==(const Quat &p_quat) const {
  260. return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w;
  261. }
  262. bool Quat::operator!=(const Quat &p_quat) const {
  263. return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w;
  264. }
  265. } // namespace godot