quat.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* quat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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. #include "quat.h"
  30. #include "matrix3.h"
  31. #include "print_string.h"
  32. // set_euler expects a vector containing the Euler angles in the format
  33. // (c,b,a), where a is the angle of the first rotation, and c is the last.
  34. // The current implementation uses XYZ convention (Z is the first rotation).
  35. void Quat::set_euler(const Vector3& p_euler) {
  36. real_t half_a1 = p_euler.x * 0.5;
  37. real_t half_a2 = p_euler.y * 0.5;
  38. real_t half_a3 = p_euler.z * 0.5;
  39. // R = X(a1).Y(a2).Z(a3) convention for Euler angles.
  40. // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-2)
  41. // a3 is the angle of the first rotation, following the notation in this reference.
  42. real_t cos_a1 = Math::cos(half_a1);
  43. real_t sin_a1 = Math::sin(half_a1);
  44. real_t cos_a2 = Math::cos(half_a2);
  45. real_t sin_a2 = Math::sin(half_a2);
  46. real_t cos_a3 = Math::cos(half_a3);
  47. real_t sin_a3 = Math::sin(half_a3);
  48. set(sin_a1*cos_a2*cos_a3 + sin_a2*sin_a3*cos_a1,
  49. -sin_a1*sin_a3*cos_a2 + sin_a2*cos_a1*cos_a3,
  50. sin_a1*sin_a2*cos_a3 + sin_a3*cos_a1*cos_a2,
  51. -sin_a1*sin_a2*sin_a3 + cos_a1*cos_a2*cos_a3);
  52. }
  53. // get_euler returns a vector containing the Euler angles in the format
  54. // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last.
  55. // The current implementation uses XYZ convention (Z is the first rotation).
  56. Vector3 Quat::get_euler() const {
  57. Matrix3 m(*this);
  58. return m.get_euler();
  59. }
  60. void Quat::operator*=(const Quat& q) {
  61. set(w * q.x+x * q.w+y * q.z - z * q.y,
  62. w * q.y+y * q.w+z * q.x - x * q.z,
  63. w * q.z+z * q.w+x * q.y - y * q.x,
  64. w * q.w - x * q.x - y * q.y - z * q.z);
  65. }
  66. Quat Quat::operator*(const Quat& q) const {
  67. Quat r=*this;
  68. r*=q;
  69. return r;
  70. }
  71. real_t Quat::length() const {
  72. return Math::sqrt(length_squared());
  73. }
  74. void Quat::normalize() {
  75. *this /= length();
  76. }
  77. Quat Quat::normalized() const {
  78. return *this / length();
  79. }
  80. Quat Quat::inverse() const {
  81. return Quat( -x, -y, -z, w );
  82. }
  83. Quat Quat::slerp(const Quat& q, const real_t& t) const {
  84. #if 0
  85. Quat dst=q;
  86. Quat src=*this;
  87. src.normalize();
  88. dst.normalize();
  89. real_t cosine = dst.dot(src);
  90. if (cosine < 0 && true) {
  91. cosine = -cosine;
  92. dst = -dst;
  93. } else {
  94. dst = dst;
  95. }
  96. if (Math::abs(cosine) < 1 - CMP_EPSILON) {
  97. // Standard case (slerp)
  98. real_t sine = Math::sqrt(1 - cosine*cosine);
  99. real_t angle = Math::atan2(sine, cosine);
  100. real_t inv_sine = 1.0f / sine;
  101. real_t coeff_0 = Math::sin((1.0f - t) * angle) * inv_sine;
  102. real_t coeff_1 = Math::sin(t * angle) * inv_sine;
  103. Quat ret= src * coeff_0 + dst * coeff_1;
  104. return ret;
  105. } else {
  106. // There are two situations:
  107. // 1. "rkP" and "q" are very close (cosine ~= +1), so we can do a linear
  108. // interpolation safely.
  109. // 2. "rkP" and "q" are almost invedste of each other (cosine ~= -1), there
  110. // are an infinite number of possibilities interpolation. but we haven't
  111. // have method to fix this case, so just use linear interpolation here.
  112. Quat ret = src * (1.0f - t) + dst *t;
  113. // taking the complement requires renormalisation
  114. ret.normalize();
  115. return ret;
  116. }
  117. #else
  118. Quat to1;
  119. real_t omega, cosom, sinom, scale0, scale1;
  120. // calc cosine
  121. cosom = dot(q);
  122. // adjust signs (if necessary)
  123. if ( cosom <0.0 ) {
  124. cosom = -cosom;
  125. to1.x = - q.x;
  126. to1.y = - q.y;
  127. to1.z = - q.z;
  128. to1.w = - q.w;
  129. } else {
  130. to1.x = q.x;
  131. to1.y = q.y;
  132. to1.z = q.z;
  133. to1.w = q.w;
  134. }
  135. // calculate coefficients
  136. if ( (1.0 - cosom) > CMP_EPSILON ) {
  137. // standard case (slerp)
  138. omega = Math::acos(cosom);
  139. sinom = Math::sin(omega);
  140. scale0 = Math::sin((1.0 - t) * omega) / sinom;
  141. scale1 = Math::sin(t * omega) / sinom;
  142. } else {
  143. // "from" and "to" quaternions are very close
  144. // ... so we can do a linear interpolation
  145. scale0 = 1.0 - t;
  146. scale1 = t;
  147. }
  148. // calculate final values
  149. return Quat(
  150. scale0 * x + scale1 * to1.x,
  151. scale0 * y + scale1 * to1.y,
  152. scale0 * z + scale1 * to1.z,
  153. scale0 * w + scale1 * to1.w
  154. );
  155. #endif
  156. }
  157. Quat Quat::slerpni(const Quat& q, const real_t& t) const {
  158. const Quat &from = *this;
  159. float dot = from.dot(q);
  160. if (Math::absf(dot) > 0.9999f) return from;
  161. float theta = Math::acos(dot),
  162. sinT = 1.0f / Math::sin(theta),
  163. newFactor = Math::sin(t * theta) * sinT,
  164. invFactor = Math::sin((1.0f - t) * theta) * sinT;
  165. return Quat(invFactor * from.x + newFactor * q.x,
  166. invFactor * from.y + newFactor * q.y,
  167. invFactor * from.z + newFactor * q.z,
  168. invFactor * from.w + newFactor * q.w);
  169. #if 0
  170. real_t to1[4];
  171. real_t omega, cosom, sinom, scale0, scale1;
  172. // calc cosine
  173. cosom = x * q.x + y * q.y + z * q.z
  174. + w * q.w;
  175. // adjust signs (if necessary)
  176. if ( cosom <0.0 && false) {
  177. cosom = -cosom;to1[0] = - q.x;
  178. to1[1] = - q.y;
  179. to1[2] = - q.z;
  180. to1[3] = - q.w;
  181. } else {
  182. to1[0] = q.x;
  183. to1[1] = q.y;
  184. to1[2] = q.z;
  185. to1[3] = q.w;
  186. }
  187. // calculate coefficients
  188. if ( (1.0 - cosom) > CMP_EPSILON ) {
  189. // standard case (slerp)
  190. omega = Math::acos(cosom);
  191. sinom = Math::sin(omega);
  192. scale0 = Math::sin((1.0 - t) * omega) / sinom;
  193. scale1 = Math::sin(t * omega) / sinom;
  194. } else {
  195. // "from" and "to" quaternions are very close
  196. // ... so we can do a linear interpolation
  197. scale0 = 1.0 - t;
  198. scale1 = t;
  199. }
  200. // calculate final values
  201. return Quat(
  202. scale0 * x + scale1 * to1[0],
  203. scale0 * y + scale1 * to1[1],
  204. scale0 * z + scale1 * to1[2],
  205. scale0 * w + scale1 * to1[3]
  206. );
  207. #endif
  208. }
  209. Quat Quat::cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const {
  210. //the only way to do slerp :|
  211. float t2 = (1.0-t)*t*2;
  212. Quat sp = this->slerp(q,t);
  213. Quat sq = prep.slerpni(postq,t);
  214. return sp.slerpni(sq,t2);
  215. }
  216. Quat::operator String() const {
  217. return String::num(x)+", "+String::num(y)+", "+ String::num(z)+", "+ String::num(w);
  218. }
  219. Quat::Quat(const Vector3& axis, const real_t& angle) {
  220. real_t d = axis.length();
  221. if (d==0)
  222. set(0,0,0,0);
  223. else {
  224. real_t sin_angle = Math::sin(angle * 0.5);
  225. real_t cos_angle = Math::cos(angle * 0.5);
  226. real_t s = sin_angle / d;
  227. set(axis.x * s, axis.y * s, axis.z * s,
  228. cos_angle);
  229. }
  230. }