BsQuaternion.cpp 10 KB

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