BsQuaternion.cpp 10 KB

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