BsQuaternion.cpp 12 KB

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