BsQuaternion.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "BsQuaternion.h"
  2. #include "BsMath.h"
  3. #include "BsMatrix3.h"
  4. #include "BsVector3.h"
  5. namespace BansheeEngine
  6. {
  7. const float Quaternion::EPSILON = 1e-03f;
  8. const Quaternion Quaternion::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
  9. const Quaternion Quaternion::IDENTITY(1.0f, 0.0f, 0.0f, 0.0f);
  10. const Quaternion::EulerAngleOrderData Quaternion::EA_LOOKUP[6] =
  11. { { 0, 1, 2}, { 0, 2, 1}, { 1, 0, 2},
  12. { 1, 2, 0}, { 2, 0, 1}, { 2, 1, 0} };;
  13. void Quaternion::fromRotationMatrix(const Matrix3& mat)
  14. {
  15. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  16. // article "Quaternion Calculus and Fast Animation".
  17. float trace = mat[0][0]+mat[1][1]+mat[2][2];
  18. float root;
  19. if (trace > 0.0f)
  20. {
  21. // |w| > 1/2, may as well choose w > 1/2
  22. root = Math::sqrt(trace + 1.0f); // 2w
  23. w = 0.5f*root;
  24. root = 0.5f/root; // 1/(4w)
  25. x = (mat[2][1]-mat[1][2])*root;
  26. y = (mat[0][2]-mat[2][0])*root;
  27. z = (mat[1][0]-mat[0][1])*root;
  28. }
  29. else
  30. {
  31. // |w| <= 1/2
  32. static UINT32 nextLookup[3] = { 1, 2, 0 };
  33. UINT32 i = 0;
  34. if (mat[1][1] > mat[0][0])
  35. i = 1;
  36. if (mat[2][2] > mat[i][i])
  37. i = 2;
  38. UINT32 j = nextLookup[i];
  39. UINT32 k = nextLookup[j];
  40. root = Math::sqrt(mat[i][i]-mat[j][j]-mat[k][k] + 1.0f);
  41. float* cmpntLookup[3] = { &x, &y, &z };
  42. *cmpntLookup[i] = 0.5f*root;
  43. root = 0.5f/root;
  44. w = (mat[k][j]-mat[j][k])*root;
  45. *cmpntLookup[j] = (mat[j][i]+mat[i][j])*root;
  46. *cmpntLookup[k] = (mat[k][i]+mat[i][k])*root;
  47. }
  48. normalize();
  49. }
  50. void Quaternion::fromAxisAngle(const Vector3& axis, const Radian& angle)
  51. {
  52. // Assert: axis[] is unit length
  53. Radian halfAngle (0.5f*angle);
  54. float sin = Math::sin(halfAngle);
  55. w = Math::cos(halfAngle);
  56. x = sin*axis.x;
  57. y = sin*axis.y;
  58. z = sin*axis.z;
  59. }
  60. void Quaternion::fromAxes(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  61. {
  62. Matrix3 kRot;
  63. kRot[0][0] = xaxis.x;
  64. kRot[1][0] = xaxis.y;
  65. kRot[2][0] = xaxis.z;
  66. kRot[0][1] = yaxis.x;
  67. kRot[1][1] = yaxis.y;
  68. kRot[2][1] = yaxis.z;
  69. kRot[0][2] = zaxis.x;
  70. kRot[1][2] = zaxis.y;
  71. kRot[2][2] = zaxis.z;
  72. fromRotationMatrix(kRot);
  73. }
  74. void Quaternion::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  75. {
  76. Matrix3 mat;
  77. mat.fromEulerAngles(xAngle, yAngle, zAngle);
  78. mat.toQuaternion(*this);
  79. }
  80. void Quaternion::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  81. {
  82. Matrix3 mat;
  83. mat.fromEulerAngles(xAngle, yAngle, zAngle, order);
  84. mat.toQuaternion(*this);
  85. }
  86. void Quaternion::toRotationMatrix(Matrix3& mat) const
  87. {
  88. float tx = x+x;
  89. float ty = y+y;
  90. float fTz = z+z;
  91. float twx = tx*w;
  92. float twy = ty*w;
  93. float twz = fTz*w;
  94. float txx = tx*x;
  95. float txy = ty*x;
  96. float txz = fTz*x;
  97. float tyy = ty*y;
  98. float tyz = fTz*y;
  99. float tzz = fTz*z;
  100. mat[0][0] = 1.0f-(tyy+tzz);
  101. mat[0][1] = txy-twz;
  102. mat[0][2] = txz+twy;
  103. mat[1][0] = txy+twz;
  104. mat[1][1] = 1.0f-(txx+tzz);
  105. mat[1][2] = tyz-twx;
  106. mat[2][0] = txz-twy;
  107. mat[2][1] = tyz+twx;
  108. mat[2][2] = 1.0f-(txx+tyy);
  109. }
  110. void Quaternion::toAxisAngle(Vector3& axis, Radian& angle) const
  111. {
  112. float sqrLength = x*x+y*y+z*z;
  113. if ( sqrLength > 0.0 )
  114. {
  115. angle = 2.0*Math::acos(w);
  116. float invLength = Math::invSqrt(sqrLength);
  117. axis.x = x*invLength;
  118. axis.y = y*invLength;
  119. axis.z = z*invLength;
  120. }
  121. else
  122. {
  123. // Angle is 0 (mod 2*pi), so any axis will do
  124. angle = Radian(0.0);
  125. axis.x = 1.0;
  126. axis.y = 0.0;
  127. axis.z = 0.0;
  128. }
  129. }
  130. void Quaternion::toAxes(Vector3& xaxis, Vector3& yaxis, Vector3& zaxis) const
  131. {
  132. Matrix3 matRot;
  133. toRotationMatrix(matRot);
  134. xaxis.x = matRot[0][0];
  135. xaxis.y = matRot[1][0];
  136. xaxis.z = matRot[2][0];
  137. yaxis.x = matRot[0][1];
  138. yaxis.y = matRot[1][1];
  139. yaxis.z = matRot[2][1];
  140. zaxis.x = matRot[0][2];
  141. zaxis.y = matRot[1][2];
  142. zaxis.z = matRot[2][2];
  143. }
  144. bool Quaternion::toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const
  145. {
  146. Matrix3 matRot;
  147. toRotationMatrix(matRot);
  148. return matRot.toEulerAngles(xAngle, yAngle, zAngle);
  149. }
  150. bool Quaternion::toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle, EulerAngleOrder order) const
  151. {
  152. Matrix3 matRot;
  153. toRotationMatrix(matRot);
  154. return matRot.toEulerAngles(xAngle, yAngle, zAngle, order);
  155. }
  156. Vector3 Quaternion::xAxis() const
  157. {
  158. float fTy = 2.0f*y;
  159. float fTz = 2.0f*z;
  160. float fTwy = fTy*w;
  161. float fTwz = fTz*w;
  162. float fTxy = fTy*x;
  163. float fTxz = fTz*x;
  164. float fTyy = fTy*y;
  165. float fTzz = fTz*z;
  166. return Vector3(1.0f-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy);
  167. }
  168. Vector3 Quaternion::yAxis() const
  169. {
  170. float fTx = 2.0f*x;
  171. float fTy = 2.0f*y;
  172. float fTz = 2.0f*z;
  173. float fTwx = fTx*w;
  174. float fTwz = fTz*w;
  175. float fTxx = fTx*x;
  176. float fTxy = fTy*x;
  177. float fTyz = fTz*y;
  178. float fTzz = fTz*z;
  179. return Vector3(fTxy-fTwz, 1.0f-(fTxx+fTzz), fTyz+fTwx);
  180. }
  181. Vector3 Quaternion::zAxis() const
  182. {
  183. float fTx = 2.0f*x;
  184. float fTy = 2.0f*y;
  185. float fTz = 2.0f*z;
  186. float fTwx = fTx*w;
  187. float fTwy = fTy*w;
  188. float fTxx = fTx*x;
  189. float fTxz = fTz*x;
  190. float fTyy = fTy*y;
  191. float fTyz = fTz*y;
  192. return Vector3(fTxz+fTwy, fTyz-fTwx, 1.0f-(fTxx+fTyy));
  193. }
  194. Quaternion Quaternion::operator+ (const Quaternion& rhs) const
  195. {
  196. return Quaternion(w+rhs.w,x+rhs.x,y+rhs.y,z+rhs.z);
  197. }
  198. Quaternion Quaternion::operator- (const Quaternion& rhs) const
  199. {
  200. return Quaternion(w-rhs.w,x-rhs.x,y-rhs.y,z-rhs.z);
  201. }
  202. Quaternion Quaternion::operator* (const Quaternion& rhs) const
  203. {
  204. return Quaternion
  205. (
  206. w * rhs.w - x * rhs.x - y * rhs.y - z * rhs.z,
  207. w * rhs.x + x * rhs.w + y * rhs.z - z * rhs.y,
  208. w * rhs.y + y * rhs.w + z * rhs.x - x * rhs.z,
  209. w * rhs.z + z * rhs.w + x * rhs.y - y * rhs.x
  210. );
  211. }
  212. Quaternion Quaternion::operator* (float rhs) const
  213. {
  214. return Quaternion(rhs*w,rhs*x,rhs*y,rhs*z);
  215. }
  216. Quaternion Quaternion::operator- () const
  217. {
  218. return Quaternion(-w,-x,-y,-z);
  219. }
  220. float Quaternion::dot(const Quaternion& other) const
  221. {
  222. return w*other.w+x*other.x+y*other.y+z*other.z;
  223. }
  224. Quaternion Quaternion::inverse() const
  225. {
  226. float fNorm = w*w+x*x+y*y+z*z;
  227. if (fNorm > 0.0f)
  228. {
  229. float fInvNorm = 1.0f/fNorm;
  230. return Quaternion(w*fInvNorm,-x*fInvNorm,-y*fInvNorm,-z*fInvNorm);
  231. }
  232. else
  233. {
  234. // Return an invalid result to flag the error
  235. return ZERO;
  236. }
  237. }
  238. Vector3 Quaternion::rotate(const Vector3& v) const
  239. {
  240. Matrix3 rot;
  241. toRotationMatrix(rot);
  242. return rot.transform(v);
  243. }
  244. Quaternion Quaternion::slerp(float t, const Quaternion& p, const Quaternion& q, bool shortestPath)
  245. {
  246. float cos = p.dot(q);
  247. Quaternion quat;
  248. if (cos < 0.0f && shortestPath)
  249. {
  250. cos = -cos;
  251. quat = -q;
  252. }
  253. else
  254. {
  255. quat = q;
  256. }
  257. if (Math::abs(cos) < 1 - EPSILON)
  258. {
  259. // Standard case (slerp)
  260. float sin = Math::sqrt(1 - Math::sqr(cos));
  261. Radian angle = Math::atan2(sin, cos);
  262. float invSin = 1.0f / sin;
  263. float coeff0 = Math::sin((1.0f - t) * angle) * invSin;
  264. float coeff1 = Math::sin(t * angle) * invSin;
  265. return coeff0 * p + coeff1 * quat;
  266. }
  267. else
  268. {
  269. // There are two situations:
  270. // 1. "p" and "q" are very close (fCos ~= +1), so we can do a linear
  271. // interpolation safely.
  272. // 2. "p" and "q" are almost inverse of each other (fCos ~= -1), there
  273. // are an infinite number of possibilities interpolation. but we haven't
  274. // have method to fix this case, so just use linear interpolation here.
  275. Quaternion ret = (1.0f - t) * p + t * quat;
  276. // Taking the complement requires renormalization
  277. ret.normalize();
  278. return ret;
  279. }
  280. }
  281. float Quaternion::normalize()
  282. {
  283. float len = w*w+x*x+y*y+z*z;
  284. float factor = 1.0f / Math::sqrt(len);
  285. *this = *this * factor;
  286. return len;
  287. }
  288. Quaternion Quaternion::getRotationFromTo(const Vector3& from, const Vector3& dest, const Vector3& fallbackAxis)
  289. {
  290. // Based on Stan Melax's article in Game Programming Gems
  291. Quaternion q;
  292. Vector3 v0 = from;
  293. Vector3 v1 = dest;
  294. v0.normalize();
  295. v1.normalize();
  296. float d = v0.dot(v1);
  297. // If dot == 1, vectors are the same
  298. if (d >= 1.0f)
  299. return Quaternion::IDENTITY;
  300. if (d < (1e-6f - 1.0f))
  301. {
  302. if (fallbackAxis != Vector3::ZERO)
  303. {
  304. // Rotate 180 degrees about the fallback axis
  305. q.fromAxisAngle(fallbackAxis, Radian(Math::PI));
  306. }
  307. else
  308. {
  309. // Generate an axis
  310. Vector3 axis = Vector3::UNIT_X.cross(from);
  311. if (axis.isZeroLength()) // Pick another if colinear
  312. axis = Vector3::UNIT_Y.cross(from);
  313. axis.normalize();
  314. q.fromAxisAngle(axis, Radian(Math::PI));
  315. }
  316. }
  317. else
  318. {
  319. float s = Math::sqrt( (1+d)*2 );
  320. float invs = 1 / s;
  321. Vector3 c = v0.cross(v1);
  322. q.x = c.x * invs;
  323. q.y = c.y * invs;
  324. q.z = c.z * invs;
  325. q.w = s * 0.5f;
  326. q.normalize();
  327. }
  328. return q;
  329. }
  330. Quaternion operator* (float lhs, const Quaternion& rhs)
  331. {
  332. return Quaternion(lhs*rhs.w,lhs*rhs.x,lhs*rhs.y,
  333. lhs*rhs.z);
  334. }
  335. }