BsMatrix3.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsMatrix3.h"
  5. #include "BsQuaternion.h"
  6. #include "BsMath.h"
  7. namespace BansheeEngine
  8. {
  9. const float Matrix3::EPSILON = 1e-06f;
  10. const Matrix3 Matrix3::ZERO(0,0,0,0,0,0,0,0,0);
  11. const Matrix3 Matrix3::IDENTITY(1,0,0,0,1,0,0,0,1);
  12. const float Matrix3::SVD_EPSILON = 1e-04f;
  13. const unsigned int Matrix3::SVD_MAX_ITERS = 32;
  14. const Matrix3::EulerAngleOrderData Matrix3::EA_LOOKUP[6] =
  15. { { 0, 1, 2, 1.0f}, { 0, 2, 1, -1.0f}, { 1, 0, 2, -1.0f},
  16. { 1, 2, 0, 1.0f}, { 2, 0, 1, 1.0f}, { 2, 1, 0, -1.0f} };;
  17. Vector3 Matrix3::getColumn(UINT32 col) const
  18. {
  19. assert(col < 3);
  20. return Vector3(m[0][col],m[1][col], m[2][col]);
  21. }
  22. void Matrix3::setColumn(UINT32 col, const Vector3& vec)
  23. {
  24. assert(col < 3);
  25. m[0][col] = vec.x;
  26. m[1][col] = vec.y;
  27. m[2][col] = vec.z;
  28. }
  29. void Matrix3::fromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
  30. {
  31. setColumn(0, xAxis);
  32. setColumn(1, yAxis);
  33. setColumn(2, zAxis);
  34. }
  35. bool Matrix3::operator== (const Matrix3& rhs) const
  36. {
  37. for (UINT32 row = 0; row < 3; row++)
  38. {
  39. for (UINT32 col = 0; col < 3; col++)
  40. {
  41. if (m[row][col] != rhs.m[row][col])
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. bool Matrix3::operator!= (const Matrix3& rhs) const
  48. {
  49. return !operator==(rhs);
  50. }
  51. Matrix3 Matrix3::operator+ (const Matrix3& rhs) const
  52. {
  53. Matrix3 sum;
  54. for (UINT32 row = 0; row < 3; row++)
  55. {
  56. for (UINT32 col = 0; col < 3; col++)
  57. {
  58. sum.m[row][col] = m[row][col] + rhs.m[row][col];
  59. }
  60. }
  61. return sum;
  62. }
  63. Matrix3 Matrix3::operator- (const Matrix3& rhs) const
  64. {
  65. Matrix3 diff;
  66. for (UINT32 row = 0; row < 3; row++)
  67. {
  68. for (UINT32 col = 0; col < 3; col++)
  69. {
  70. diff.m[row][col] = m[row][col] -
  71. rhs.m[row][col];
  72. }
  73. }
  74. return diff;
  75. }
  76. Matrix3 Matrix3::operator* (const Matrix3& rhs) const
  77. {
  78. Matrix3 prod;
  79. for (UINT32 row = 0; row < 3; row++)
  80. {
  81. for (UINT32 col = 0; col < 3; col++)
  82. {
  83. prod.m[row][col] = m[row][0]*rhs.m[0][col] +
  84. m[row][1]*rhs.m[1][col] + m[row][2]*rhs.m[2][col];
  85. }
  86. }
  87. return prod;
  88. }
  89. Matrix3 Matrix3::operator- () const
  90. {
  91. Matrix3 neg;
  92. for (UINT32 row = 0; row < 3; row++)
  93. {
  94. for (UINT32 col = 0; col < 3; col++)
  95. neg[row][col] = -m[row][col];
  96. }
  97. return neg;
  98. }
  99. Matrix3 Matrix3::operator* (float rhs) const
  100. {
  101. Matrix3 prod;
  102. for (UINT32 row = 0; row < 3; row++)
  103. {
  104. for (UINT32 col = 0; col < 3; col++)
  105. prod[row][col] = rhs*m[row][col];
  106. }
  107. return prod;
  108. }
  109. Matrix3 operator* (float lhs, const Matrix3& rhs)
  110. {
  111. Matrix3 prod;
  112. for (UINT32 row = 0; row < 3; row++)
  113. {
  114. for (UINT32 col = 0; col < 3; col++)
  115. prod[row][col] = lhs*rhs.m[row][col];
  116. }
  117. return prod;
  118. }
  119. Vector3 Matrix3::transform(const Vector3& vec) const
  120. {
  121. Vector3 prod;
  122. for (UINT32 row = 0; row < 3; row++)
  123. {
  124. prod[row] =
  125. m[row][0]*vec[0] +
  126. m[row][1]*vec[1] +
  127. m[row][2]*vec[2];
  128. }
  129. return prod;
  130. }
  131. Matrix3 Matrix3::transpose() const
  132. {
  133. Matrix3 matTranspose;
  134. for (UINT32 row = 0; row < 3; row++)
  135. {
  136. for (UINT32 col = 0; col < 3; col++)
  137. matTranspose[row][col] = m[col][row];
  138. }
  139. return matTranspose;
  140. }
  141. bool Matrix3::inverse(Matrix3& matInv, float tolerance) const
  142. {
  143. matInv[0][0] = m[1][1]*m[2][2] - m[1][2]*m[2][1];
  144. matInv[0][1] = m[0][2]*m[2][1] - m[0][1]*m[2][2];
  145. matInv[0][2] = m[0][1]*m[1][2] - m[0][2]*m[1][1];
  146. matInv[1][0] = m[1][2]*m[2][0] - m[1][0]*m[2][2];
  147. matInv[1][1] = m[0][0]*m[2][2] - m[0][2]*m[2][0];
  148. matInv[1][2] = m[0][2]*m[1][0] - m[0][0]*m[1][2];
  149. matInv[2][0] = m[1][0]*m[2][1] - m[1][1]*m[2][0];
  150. matInv[2][1] = m[0][1]*m[2][0] - m[0][0]*m[2][1];
  151. matInv[2][2] = m[0][0]*m[1][1] - m[0][1]*m[1][0];
  152. float det = m[0][0]*matInv[0][0] + m[0][1]*matInv[1][0] + m[0][2]*matInv[2][0];
  153. if (Math::abs(det) <= tolerance)
  154. return false;
  155. float invDet = 1.0f/det;
  156. for (UINT32 row = 0; row < 3; row++)
  157. {
  158. for (UINT32 col = 0; col < 3; col++)
  159. matInv[row][col] *= invDet;
  160. }
  161. return true;
  162. }
  163. Matrix3 Matrix3::inverse(float tolerance) const
  164. {
  165. Matrix3 matInv = Matrix3::ZERO;
  166. inverse(matInv, tolerance);
  167. return matInv;
  168. }
  169. float Matrix3::determinant() const
  170. {
  171. float cofactor00 = m[1][1]*m[2][2] - m[1][2]*m[2][1];
  172. float cofactor10 = m[1][2]*m[2][0] - m[1][0]*m[2][2];
  173. float cofactor20 = m[1][0]*m[2][1] - m[1][1]*m[2][0];
  174. float det = m[0][0]*cofactor00 + m[0][1]*cofactor10 + m[0][2]*cofactor20;
  175. return det;
  176. }
  177. void Matrix3::bidiagonalize (Matrix3& matA, Matrix3& matL, Matrix3& matR)
  178. {
  179. float v[3], w[3];
  180. float length, sign, t1, invT1, t2;
  181. bool bIdentity;
  182. // Map first column to (*,0,0)
  183. length = Math::sqrt(matA[0][0]*matA[0][0] + matA[1][0]*matA[1][0] + matA[2][0]*matA[2][0]);
  184. if (length > 0.0f)
  185. {
  186. sign = (matA[0][0] > 0.0f ? 1.0f : -1.0f);
  187. t1 = matA[0][0] + sign*length;
  188. invT1 = 1.0f/t1;
  189. v[1] = matA[1][0]*invT1;
  190. v[2] = matA[2][0]*invT1;
  191. t2 = -2.0f/(1.0f+v[1]*v[1]+v[2]*v[2]);
  192. w[0] = t2*(matA[0][0]+matA[1][0]*v[1]+matA[2][0]*v[2]);
  193. w[1] = t2*(matA[0][1]+matA[1][1]*v[1]+matA[2][1]*v[2]);
  194. w[2] = t2*(matA[0][2]+matA[1][2]*v[1]+matA[2][2]*v[2]);
  195. matA[0][0] += w[0];
  196. matA[0][1] += w[1];
  197. matA[0][2] += w[2];
  198. matA[1][1] += v[1]*w[1];
  199. matA[1][2] += v[1]*w[2];
  200. matA[2][1] += v[2]*w[1];
  201. matA[2][2] += v[2]*w[2];
  202. matL[0][0] = 1.0f+t2;
  203. matL[0][1] = matL[1][0] = t2*v[1];
  204. matL[0][2] = matL[2][0] = t2*v[2];
  205. matL[1][1] = 1.0f+t2*v[1]*v[1];
  206. matL[1][2] = matL[2][1] = t2*v[1]*v[2];
  207. matL[2][2] = 1.0f+t2*v[2]*v[2];
  208. bIdentity = false;
  209. }
  210. else
  211. {
  212. matL = Matrix3::IDENTITY;
  213. bIdentity = true;
  214. }
  215. // Map first row to (*,*,0)
  216. length = Math::sqrt(matA[0][1]*matA[0][1]+matA[0][2]*matA[0][2]);
  217. if ( length > 0.0 )
  218. {
  219. sign = (matA[0][1] > 0.0f ? 1.0f : -1.0f);
  220. t1 = matA[0][1] + sign*length;
  221. v[2] = matA[0][2]/t1;
  222. t2 = -2.0f/(1.0f+v[2]*v[2]);
  223. w[0] = t2*(matA[0][1]+matA[0][2]*v[2]);
  224. w[1] = t2*(matA[1][1]+matA[1][2]*v[2]);
  225. w[2] = t2*(matA[2][1]+matA[2][2]*v[2]);
  226. matA[0][1] += w[0];
  227. matA[1][1] += w[1];
  228. matA[1][2] += w[1]*v[2];
  229. matA[2][1] += w[2];
  230. matA[2][2] += w[2]*v[2];
  231. matR[0][0] = 1.0;
  232. matR[0][1] = matR[1][0] = 0.0;
  233. matR[0][2] = matR[2][0] = 0.0;
  234. matR[1][1] = 1.0f+t2;
  235. matR[1][2] = matR[2][1] = t2*v[2];
  236. matR[2][2] = 1.0f+t2*v[2]*v[2];
  237. }
  238. else
  239. {
  240. matR = Matrix3::IDENTITY;
  241. }
  242. // Map second column to (*,*,0)
  243. length = Math::sqrt(matA[1][1]*matA[1][1]+matA[2][1]*matA[2][1]);
  244. if ( length > 0.0 )
  245. {
  246. sign = (matA[1][1] > 0.0f ? 1.0f : -1.0f);
  247. t1 = matA[1][1] + sign*length;
  248. v[2] = matA[2][1]/t1;
  249. t2 = -2.0f/(1.0f+v[2]*v[2]);
  250. w[1] = t2*(matA[1][1]+matA[2][1]*v[2]);
  251. w[2] = t2*(matA[1][2]+matA[2][2]*v[2]);
  252. matA[1][1] += w[1];
  253. matA[1][2] += w[2];
  254. matA[2][2] += v[2]*w[2];
  255. float a = 1.0f+t2;
  256. float b = t2*v[2];
  257. float c = 1.0f+b*v[2];
  258. if (bIdentity)
  259. {
  260. matL[0][0] = 1.0;
  261. matL[0][1] = matL[1][0] = 0.0;
  262. matL[0][2] = matL[2][0] = 0.0;
  263. matL[1][1] = a;
  264. matL[1][2] = matL[2][1] = b;
  265. matL[2][2] = c;
  266. }
  267. else
  268. {
  269. for (int row = 0; row < 3; row++)
  270. {
  271. float tmp0 = matL[row][1];
  272. float tmp1 = matL[row][2];
  273. matL[row][1] = a*tmp0+b*tmp1;
  274. matL[row][2] = b*tmp0+c*tmp1;
  275. }
  276. }
  277. }
  278. }
  279. void Matrix3::golubKahanStep (Matrix3& matA, Matrix3& matL, Matrix3& matR)
  280. {
  281. float f11 = matA[0][1]*matA[0][1]+matA[1][1]*matA[1][1];
  282. float t22 = matA[1][2]*matA[1][2]+matA[2][2]*matA[2][2];
  283. float t12 = matA[1][1]*matA[1][2];
  284. float trace = f11+t22;
  285. float diff = f11-t22;
  286. float discr = Math::sqrt(diff*diff+4.0f*t12*t12);
  287. float root1 = 0.5f*(trace+discr);
  288. float root2 = 0.5f*(trace-discr);
  289. // Adjust right
  290. float y = matA[0][0] - (Math::abs(root1-t22) <= Math::abs(root2-t22) ? root1 : root2);
  291. float z = matA[0][1];
  292. float invLength = Math::invSqrt(y*y+z*z);
  293. float sin = z*invLength;
  294. float cos = -y*invLength;
  295. float tmp0 = matA[0][0];
  296. float tmp1 = matA[0][1];
  297. matA[0][0] = cos*tmp0-sin*tmp1;
  298. matA[0][1] = sin*tmp0+cos*tmp1;
  299. matA[1][0] = -sin*matA[1][1];
  300. matA[1][1] *= cos;
  301. UINT32 row;
  302. for (row = 0; row < 3; row++)
  303. {
  304. tmp0 = matR[0][row];
  305. tmp1 = matR[1][row];
  306. matR[0][row] = cos*tmp0-sin*tmp1;
  307. matR[1][row] = sin*tmp0+cos*tmp1;
  308. }
  309. // Adjust left
  310. y = matA[0][0];
  311. z = matA[1][0];
  312. invLength = Math::invSqrt(y*y+z*z);
  313. sin = z*invLength;
  314. cos = -y*invLength;
  315. matA[0][0] = cos*matA[0][0]-sin*matA[1][0];
  316. tmp0 = matA[0][1];
  317. tmp1 = matA[1][1];
  318. matA[0][1] = cos*tmp0-sin*tmp1;
  319. matA[1][1] = sin*tmp0+cos*tmp1;
  320. matA[0][2] = -sin*matA[1][2];
  321. matA[1][2] *= cos;
  322. UINT32 col;
  323. for (col = 0; col < 3; col++)
  324. {
  325. tmp0 = matL[col][0];
  326. tmp1 = matL[col][1];
  327. matL[col][0] = cos*tmp0-sin*tmp1;
  328. matL[col][1] = sin*tmp0+cos*tmp1;
  329. }
  330. // Adjust right
  331. y = matA[0][1];
  332. z = matA[0][2];
  333. invLength = Math::invSqrt(y*y+z*z);
  334. sin = z*invLength;
  335. cos = -y*invLength;
  336. matA[0][1] = cos*matA[0][1]-sin*matA[0][2];
  337. tmp0 = matA[1][1];
  338. tmp1 = matA[1][2];
  339. matA[1][1] = cos*tmp0-sin*tmp1;
  340. matA[1][2] = sin*tmp0+cos*tmp1;
  341. matA[2][1] = -sin*matA[2][2];
  342. matA[2][2] *= cos;
  343. for (row = 0; row < 3; row++)
  344. {
  345. tmp0 = matR[1][row];
  346. tmp1 = matR[2][row];
  347. matR[1][row] = cos*tmp0-sin*tmp1;
  348. matR[2][row] = sin*tmp0+cos*tmp1;
  349. }
  350. // Adjust left
  351. y = matA[1][1];
  352. z = matA[2][1];
  353. invLength = Math::invSqrt(y*y+z*z);
  354. sin = z*invLength;
  355. cos = -y*invLength;
  356. matA[1][1] = cos*matA[1][1]-sin*matA[2][1];
  357. tmp0 = matA[1][2];
  358. tmp1 = matA[2][2];
  359. matA[1][2] = cos*tmp0-sin*tmp1;
  360. matA[2][2] = sin*tmp0+cos*tmp1;
  361. for (col = 0; col < 3; col++)
  362. {
  363. tmp0 = matL[col][1];
  364. tmp1 = matL[col][2];
  365. matL[col][1] = cos*tmp0-sin*tmp1;
  366. matL[col][2] = sin*tmp0+cos*tmp1;
  367. }
  368. }
  369. void Matrix3::singularValueDecomposition(Matrix3& matL, Vector3& matS, Matrix3& matR) const
  370. {
  371. UINT32 row, col;
  372. Matrix3 mat = *this;
  373. bidiagonalize(mat, matL, matR);
  374. for (unsigned int i = 0; i < SVD_MAX_ITERS; i++)
  375. {
  376. float tmp, tmp0, tmp1;
  377. float sin0, cos0, tan0;
  378. float sin1, cos1, tan1;
  379. bool test1 = (Math::abs(mat[0][1]) <= SVD_EPSILON*(Math::abs(mat[0][0])+Math::abs(mat[1][1])));
  380. bool test2 = (Math::abs(mat[1][2]) <= SVD_EPSILON*(Math::abs(mat[1][1])+Math::abs(mat[2][2])));
  381. if (test1)
  382. {
  383. if (test2)
  384. {
  385. matS[0] = mat[0][0];
  386. matS[1] = mat[1][1];
  387. matS[2] = mat[2][2];
  388. break;
  389. }
  390. else
  391. {
  392. // 2x2 closed form factorization
  393. tmp = (mat[1][1]*mat[1][1] - mat[2][2]*mat[2][2] + mat[1][2]*mat[1][2])/(mat[1][2]*mat[2][2]);
  394. tan0 = 0.5f*(tmp+Math::sqrt(tmp*tmp + 4.0f));
  395. cos0 = Math::invSqrt(1.0f+tan0*tan0);
  396. sin0 = tan0*cos0;
  397. for (col = 0; col < 3; col++)
  398. {
  399. tmp0 = matL[col][1];
  400. tmp1 = matL[col][2];
  401. matL[col][1] = cos0*tmp0-sin0*tmp1;
  402. matL[col][2] = sin0*tmp0+cos0*tmp1;
  403. }
  404. tan1 = (mat[1][2]-mat[2][2]*tan0)/mat[1][1];
  405. cos1 = Math::invSqrt(1.0f+tan1*tan1);
  406. sin1 = -tan1*cos1;
  407. for (row = 0; row < 3; row++)
  408. {
  409. tmp0 = matR[1][row];
  410. tmp1 = matR[2][row];
  411. matR[1][row] = cos1*tmp0-sin1*tmp1;
  412. matR[2][row] = sin1*tmp0+cos1*tmp1;
  413. }
  414. matS[0] = mat[0][0];
  415. matS[1] = cos0*cos1*mat[1][1] - sin1*(cos0*mat[1][2]-sin0*mat[2][2]);
  416. matS[2] = sin0*sin1*mat[1][1] + cos1*(sin0*mat[1][2]+cos0*mat[2][2]);
  417. break;
  418. }
  419. }
  420. else
  421. {
  422. if (test2)
  423. {
  424. // 2x2 closed form factorization
  425. tmp = (mat[0][0]*mat[0][0] + mat[1][1]*mat[1][1] - mat[0][1]*mat[0][1])/(mat[0][1]*mat[1][1]);
  426. tan0 = 0.5f*(-tmp+Math::sqrt(tmp*tmp + 4.0f));
  427. cos0 = Math::invSqrt(1.0f+tan0*tan0);
  428. sin0 = tan0*cos0;
  429. for (col = 0; col < 3; col++)
  430. {
  431. tmp0 = matL[col][0];
  432. tmp1 = matL[col][1];
  433. matL[col][0] = cos0*tmp0-sin0*tmp1;
  434. matL[col][1] = sin0*tmp0+cos0*tmp1;
  435. }
  436. tan1 = (mat[0][1]-mat[1][1]*tan0)/mat[0][0];
  437. cos1 = Math::invSqrt(1.0f+tan1*tan1);
  438. sin1 = -tan1*cos1;
  439. for (row = 0; row < 3; row++)
  440. {
  441. tmp0 = matR[0][row];
  442. tmp1 = matR[1][row];
  443. matR[0][row] = cos1*tmp0-sin1*tmp1;
  444. matR[1][row] = sin1*tmp0+cos1*tmp1;
  445. }
  446. matS[0] = cos0*cos1*mat[0][0] - sin1*(cos0*mat[0][1]-sin0*mat[1][1]);
  447. matS[1] = sin0*sin1*mat[0][0] + cos1*(sin0*mat[0][1]+cos0*mat[1][1]);
  448. matS[2] = mat[2][2];
  449. break;
  450. }
  451. else
  452. {
  453. golubKahanStep(mat, matL, matR);
  454. }
  455. }
  456. }
  457. // Positize diagonal
  458. for (row = 0; row < 3; row++)
  459. {
  460. if ( matS[row] < 0.0 )
  461. {
  462. matS[row] = -matS[row];
  463. for (col = 0; col < 3; col++)
  464. matR[row][col] = -matR[row][col];
  465. }
  466. }
  467. }
  468. void Matrix3::orthonormalize()
  469. {
  470. // Compute q0
  471. float invLength = Math::invSqrt(m[0][0]*m[0][0]+ m[1][0]*m[1][0] + m[2][0]*m[2][0]);
  472. m[0][0] *= invLength;
  473. m[1][0] *= invLength;
  474. m[2][0] *= invLength;
  475. // Compute q1
  476. float dot0 = m[0][0]*m[0][1] + m[1][0]*m[1][1] + m[2][0]*m[2][1];
  477. m[0][1] -= dot0*m[0][0];
  478. m[1][1] -= dot0*m[1][0];
  479. m[2][1] -= dot0*m[2][0];
  480. invLength = Math::invSqrt(m[0][1]*m[0][1] + m[1][1]*m[1][1] + m[2][1]*m[2][1]);
  481. m[0][1] *= invLength;
  482. m[1][1] *= invLength;
  483. m[2][1] *= invLength;
  484. // Compute q2
  485. float dot1 = m[0][1]*m[0][2] + m[1][1]*m[1][2] + m[2][1]*m[2][2];
  486. dot0 = m[0][0]*m[0][2] + m[1][0]*m[1][2] + m[2][0]*m[2][2];
  487. m[0][2] -= dot0*m[0][0] + dot1*m[0][1];
  488. m[1][2] -= dot0*m[1][0] + dot1*m[1][1];
  489. m[2][2] -= dot0*m[2][0] + dot1*m[2][1];
  490. invLength = Math::invSqrt(m[0][2]*m[0][2] + m[1][2]*m[1][2] + m[2][2]*m[2][2]);
  491. m[0][2] *= invLength;
  492. m[1][2] *= invLength;
  493. m[2][2] *= invLength;
  494. }
  495. void Matrix3::QDUDecomposition(Matrix3& matQ, Vector3& vecD, Vector3& vecU) const
  496. {
  497. // Build orthogonal matrix Q
  498. float invLength = Math::invSqrt(m[0][0]*m[0][0] + m[1][0]*m[1][0] + m[2][0]*m[2][0]);
  499. matQ[0][0] = m[0][0]*invLength;
  500. matQ[1][0] = m[1][0]*invLength;
  501. matQ[2][0] = m[2][0]*invLength;
  502. float dot = matQ[0][0]*m[0][1] + matQ[1][0]*m[1][1] + matQ[2][0]*m[2][1];
  503. matQ[0][1] = m[0][1]-dot*matQ[0][0];
  504. matQ[1][1] = m[1][1]-dot*matQ[1][0];
  505. matQ[2][1] = m[2][1]-dot*matQ[2][0];
  506. invLength = Math::invSqrt(matQ[0][1]*matQ[0][1] + matQ[1][1]*matQ[1][1] + matQ[2][1]*matQ[2][1]);
  507. matQ[0][1] *= invLength;
  508. matQ[1][1] *= invLength;
  509. matQ[2][1] *= invLength;
  510. dot = matQ[0][0]*m[0][2] + matQ[1][0]*m[1][2] + matQ[2][0]*m[2][2];
  511. matQ[0][2] = m[0][2]-dot*matQ[0][0];
  512. matQ[1][2] = m[1][2]-dot*matQ[1][0];
  513. matQ[2][2] = m[2][2]-dot*matQ[2][0];
  514. dot = matQ[0][1]*m[0][2] + matQ[1][1]*m[1][2] + matQ[2][1]*m[2][2];
  515. matQ[0][2] -= dot*matQ[0][1];
  516. matQ[1][2] -= dot*matQ[1][1];
  517. matQ[2][2] -= dot*matQ[2][1];
  518. invLength = Math::invSqrt(matQ[0][2]*matQ[0][2] + matQ[1][2]*matQ[1][2] + matQ[2][2]*matQ[2][2]);
  519. matQ[0][2] *= invLength;
  520. matQ[1][2] *= invLength;
  521. matQ[2][2] *= invLength;
  522. // Guarantee that orthogonal matrix has determinant 1 (no reflections)
  523. float fDet = matQ[0][0]*matQ[1][1]*matQ[2][2] + matQ[0][1]*matQ[1][2]*matQ[2][0] +
  524. matQ[0][2]*matQ[1][0]*matQ[2][1] - matQ[0][2]*matQ[1][1]*matQ[2][0] -
  525. matQ[0][1]*matQ[1][0]*matQ[2][2] - matQ[0][0]*matQ[1][2]*matQ[2][1];
  526. if (fDet < 0.0f)
  527. {
  528. for (UINT32 row = 0; row < 3; row++)
  529. for (UINT32 col = 0; col < 3; col++)
  530. matQ[row][col] = -matQ[row][col];
  531. }
  532. // Build "right" matrix R
  533. Matrix3 matRight;
  534. matRight[0][0] = matQ[0][0]*m[0][0] + matQ[1][0]*m[1][0] +
  535. matQ[2][0]*m[2][0];
  536. matRight[0][1] = matQ[0][0]*m[0][1] + matQ[1][0]*m[1][1] +
  537. matQ[2][0]*m[2][1];
  538. matRight[1][1] = matQ[0][1]*m[0][1] + matQ[1][1]*m[1][1] +
  539. matQ[2][1]*m[2][1];
  540. matRight[0][2] = matQ[0][0]*m[0][2] + matQ[1][0]*m[1][2] +
  541. matQ[2][0]*m[2][2];
  542. matRight[1][2] = matQ[0][1]*m[0][2] + matQ[1][1]*m[1][2] +
  543. matQ[2][1]*m[2][2];
  544. matRight[2][2] = matQ[0][2]*m[0][2] + matQ[1][2]*m[1][2] +
  545. matQ[2][2]*m[2][2];
  546. // The scaling component
  547. vecD[0] = matRight[0][0];
  548. vecD[1] = matRight[1][1];
  549. vecD[2] = matRight[2][2];
  550. // The shear component
  551. float invD0 = 1.0f/vecD[0];
  552. vecU[0] = matRight[0][1]*invD0;
  553. vecU[1] = matRight[0][2]*invD0;
  554. vecU[2] = matRight[1][2]/vecD[1];
  555. }
  556. void Matrix3::toAxisAngle(Vector3& axis, Radian& radians) const
  557. {
  558. float trace = m[0][0] + m[1][1] + m[2][2];
  559. float cos = 0.5f*(trace-1.0f);
  560. radians = Math::acos(cos); // In [0, PI]
  561. if (radians > Radian(0.0f))
  562. {
  563. if (radians < Radian(Math::PI))
  564. {
  565. axis.x = m[2][1]-m[1][2];
  566. axis.y = m[0][2]-m[2][0];
  567. axis.z = m[1][0]-m[0][1];
  568. axis.normalize();
  569. }
  570. else
  571. {
  572. // Angle is PI
  573. float fHalfInverse;
  574. if (m[0][0] >= m[1][1])
  575. {
  576. // r00 >= r11
  577. if (m[0][0] >= m[2][2])
  578. {
  579. // r00 is maximum diagonal term
  580. axis.x = 0.5f*Math::sqrt(m[0][0] - m[1][1] - m[2][2] + 1.0f);
  581. fHalfInverse = 0.5f/axis.x;
  582. axis.y = fHalfInverse*m[0][1];
  583. axis.z = fHalfInverse*m[0][2];
  584. }
  585. else
  586. {
  587. // r22 is maximum diagonal term
  588. axis.z = 0.5f*Math::sqrt(m[2][2] - m[0][0] - m[1][1] + 1.0f);
  589. fHalfInverse = 0.5f/axis.z;
  590. axis.x = fHalfInverse*m[0][2];
  591. axis.y = fHalfInverse*m[1][2];
  592. }
  593. }
  594. else
  595. {
  596. // r11 > r00
  597. if ( m[1][1] >= m[2][2] )
  598. {
  599. // r11 is maximum diagonal term
  600. axis.y = 0.5f*Math::sqrt(m[1][1] - m[0][0] - m[2][2] + 1.0f);
  601. fHalfInverse = 0.5f/axis.y;
  602. axis.x = fHalfInverse*m[0][1];
  603. axis.z = fHalfInverse*m[1][2];
  604. }
  605. else
  606. {
  607. // r22 is maximum diagonal term
  608. axis.z = 0.5f*Math::sqrt(m[2][2] - m[0][0] - m[1][1] + 1.0f);
  609. fHalfInverse = 0.5f/axis.z;
  610. axis.x = fHalfInverse*m[0][2];
  611. axis.y = fHalfInverse*m[1][2];
  612. }
  613. }
  614. }
  615. }
  616. else
  617. {
  618. // The angle is 0 and the matrix is the identity. Any axis will
  619. // work, so just use the x-axis.
  620. axis.x = 1.0f;
  621. axis.y = 0.0f;
  622. axis.z = 0.0f;
  623. }
  624. }
  625. void Matrix3::fromAxisAngle(const Vector3& axis, const Radian& angle)
  626. {
  627. float cos = Math::cos(angle);
  628. float sin = Math::sin(angle);
  629. float oneMinusCos = 1.0f-cos;
  630. float x2 = axis.x*axis.x;
  631. float y2 = axis.y*axis.y;
  632. float z2 = axis.z*axis.z;
  633. float xym = axis.x*axis.y*oneMinusCos;
  634. float xzm = axis.x*axis.z*oneMinusCos;
  635. float yzm = axis.y*axis.z*oneMinusCos;
  636. float xSin = axis.x*sin;
  637. float ySin = axis.y*sin;
  638. float zSin = axis.z*sin;
  639. m[0][0] = x2*oneMinusCos+cos;
  640. m[0][1] = xym-zSin;
  641. m[0][2] = xzm+ySin;
  642. m[1][0] = xym+zSin;
  643. m[1][1] = y2*oneMinusCos+cos;
  644. m[1][2] = yzm-xSin;
  645. m[2][0] = xzm-ySin;
  646. m[2][1] = yzm+xSin;
  647. m[2][2] = z2*oneMinusCos+cos;
  648. }
  649. void Matrix3::toQuaternion(Quaternion& quat) const
  650. {
  651. quat.fromRotationMatrix(*this);
  652. }
  653. void Matrix3::fromQuaternion(const Quaternion& quat)
  654. {
  655. quat.toRotationMatrix(*this);
  656. }
  657. bool Matrix3::toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const
  658. {
  659. xAngle = Radian(Math::asin(m[0][2]));
  660. if (xAngle < Radian(Math::HALF_PI))
  661. {
  662. if (xAngle > Radian(-Math::HALF_PI))
  663. {
  664. yAngle = Math::atan2(-m[1][2], m[2][2]);
  665. zAngle = Math::atan2(-m[0][1], m[0][0]);
  666. return true;
  667. }
  668. else
  669. {
  670. // WARNING. Not a unique solution.
  671. Radian angle = Math::atan2(m[1][0],m[1][1]);
  672. zAngle = Radian(0.0f); // Any angle works
  673. yAngle = zAngle - angle;
  674. return false;
  675. }
  676. }
  677. else
  678. {
  679. // WARNING. Not a unique solution.
  680. Radian angle = Math::atan2(m[1][0],m[1][1]);
  681. zAngle = Radian(0.0f); // Any angle works
  682. yAngle = angle - zAngle;
  683. return false;
  684. }
  685. }
  686. bool Matrix3::toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle, EulerAngleOrder order) const
  687. {
  688. const EulerAngleOrderData& l = EA_LOOKUP[(int)order];
  689. xAngle = Radian(Math::asin(l.sign * m[l.a][l.c]));
  690. if (xAngle < Radian(Math::HALF_PI))
  691. {
  692. if (xAngle > Radian(-Math::HALF_PI))
  693. {
  694. yAngle = Math::atan2(-l.sign * m[l.b][l.c], m[l.c][l.c]);
  695. zAngle = Math::atan2(-l.sign * m[l.a][l.b], m[l.a][l.a]);
  696. return true;
  697. }
  698. else
  699. {
  700. // WARNING. Not a unique solution.
  701. Radian angle = Math::atan2(l.sign * m[l.b][l.a], m[l.b][l.b]);
  702. zAngle = Radian(0.0f); // Any angle works
  703. yAngle = zAngle - angle;
  704. return false;
  705. }
  706. }
  707. else
  708. {
  709. // WARNING. Not a unique solution.
  710. Radian angle = Math::atan2(l.sign * m[l.b][l.a], m[l.b][l.b]);
  711. zAngle = Radian(0.0f); // Any angle works
  712. yAngle = angle - zAngle;
  713. return false;
  714. }
  715. }
  716. void Matrix3::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  717. {
  718. float cos, sin;
  719. cos = Math::cos(yAngle);
  720. sin = Math::sin(yAngle);
  721. Matrix3 xMat(1.0f, 0.0f, 0.0f, 0.0f, cos, -sin, 0.0f, sin, cos);
  722. cos = Math::cos(xAngle);
  723. sin = Math::sin(xAngle);
  724. Matrix3 yMat(cos, 0.0f, sin, 0.0f, 1.0f, 0.0f, -sin, 0.0f, cos);
  725. cos = Math::cos(zAngle);
  726. sin = Math::sin(zAngle);
  727. Matrix3 zMat(cos,-sin, 0.0f, sin, cos, 0.0f, 0.0f, 0.0f, 1.0f);
  728. *this = xMat*(yMat*zMat);
  729. }
  730. void Matrix3::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  731. {
  732. const EulerAngleOrderData& l = EA_LOOKUP[(int)order];
  733. Matrix3 mats[3];
  734. float cos, sin;
  735. cos = Math::cos(yAngle);
  736. sin = Math::sin(yAngle);
  737. mats[0] = Matrix3(1.0f, 0.0f, 0.0f, 0.0f, cos, -sin, 0.0f, sin, cos);
  738. cos = Math::cos(xAngle);
  739. sin = Math::sin(xAngle);
  740. mats[1] = Matrix3(cos, 0.0f, sin, 0.0f, 1.0f, 0.0f, -sin, 0.0f, cos);
  741. cos = Math::cos(zAngle);
  742. sin = Math::sin(zAngle);
  743. mats[2] = Matrix3(cos,-sin, 0.0f, sin, cos, 0.0f, 0.0f, 0.0f, 1.0f);
  744. *this = mats[l.a]*(mats[l.b]*mats[l.c]);
  745. }
  746. void Matrix3::tridiagonal(float diag[3], float subDiag[3])
  747. {
  748. // Householder reduction T = Q^t M Q
  749. // Input:
  750. // mat, symmetric 3x3 matrix M
  751. // Output:
  752. // mat, orthogonal matrix Q
  753. // diag, diagonal entries of T
  754. // subd, subdiagonal entries of T (T is symmetric)
  755. float fA = m[0][0];
  756. float fB = m[0][1];
  757. float fC = m[0][2];
  758. float fD = m[1][1];
  759. float fE = m[1][2];
  760. float fF = m[2][2];
  761. diag[0] = fA;
  762. subDiag[2] = 0.0;
  763. if (Math::abs(fC) >= EPSILON)
  764. {
  765. float length = Math::sqrt(fB*fB+fC*fC);
  766. float invLength = 1.0f/length;
  767. fB *= invLength;
  768. fC *= invLength;
  769. float fQ = 2.0f*fB*fE+fC*(fF-fD);
  770. diag[1] = fD+fC*fQ;
  771. diag[2] = fF-fC*fQ;
  772. subDiag[0] = length;
  773. subDiag[1] = fE-fB*fQ;
  774. m[0][0] = 1.0;
  775. m[0][1] = 0.0;
  776. m[0][2] = 0.0;
  777. m[1][0] = 0.0;
  778. m[1][1] = fB;
  779. m[1][2] = fC;
  780. m[2][0] = 0.0;
  781. m[2][1] = fC;
  782. m[2][2] = -fB;
  783. }
  784. else
  785. {
  786. diag[1] = fD;
  787. diag[2] = fF;
  788. subDiag[0] = fB;
  789. subDiag[1] = fE;
  790. m[0][0] = 1.0;
  791. m[0][1] = 0.0;
  792. m[0][2] = 0.0;
  793. m[1][0] = 0.0;
  794. m[1][1] = 1.0;
  795. m[1][2] = 0.0;
  796. m[2][0] = 0.0;
  797. m[2][1] = 0.0;
  798. m[2][2] = 1.0;
  799. }
  800. }
  801. bool Matrix3::QLAlgorithm(float diag[3], float subDiag[3])
  802. {
  803. // QL iteration with implicit shifting to reduce matrix from tridiagonal to diagonal
  804. for (int i = 0; i < 3; i++)
  805. {
  806. const unsigned int maxIter = 32;
  807. unsigned int iter;
  808. for (iter = 0; iter < maxIter; iter++)
  809. {
  810. int j;
  811. for (j = i; j <= 1; j++)
  812. {
  813. float sum = Math::abs(diag[j]) + Math::abs(diag[j+1]);
  814. if (Math::abs(subDiag[j]) + sum == sum)
  815. break;
  816. }
  817. if (j == i)
  818. break;
  819. float tmp0 = (diag[i+1]-diag[i])/(2.0f*subDiag[i]);
  820. float tmp1 = Math::sqrt(tmp0*tmp0+1.0f);
  821. if (tmp0 < 0.0f)
  822. tmp0 = diag[j]-diag[i]+subDiag[i]/(tmp0-tmp1);
  823. else
  824. tmp0 = diag[j]-diag[i]+subDiag[i]/(tmp0+tmp1);
  825. float sin = 1.0f;
  826. float cos = 1.0f;
  827. float tmp2 = 0.0f;
  828. for (int k = j-1; k >= i; k--)
  829. {
  830. float tmp3 = sin*subDiag[k];
  831. float tmp4 = cos*subDiag[k];
  832. if (Math::abs(tmp3) >= Math::abs(tmp0))
  833. {
  834. cos = tmp0/tmp3;
  835. tmp1 = Math::sqrt(cos*cos+1.0f);
  836. subDiag[k+1] = tmp3*tmp1;
  837. sin = 1.0f/tmp1;
  838. cos *= sin;
  839. }
  840. else
  841. {
  842. sin = tmp3/tmp0;
  843. tmp1 = Math::sqrt(sin*sin+1.0f);
  844. subDiag[k+1] = tmp0*tmp1;
  845. cos = 1.0f/tmp1;
  846. sin *= cos;
  847. }
  848. tmp0 = diag[k+1]-tmp2;
  849. tmp1 = (diag[k]-tmp0)*sin+2.0f*tmp4*cos;
  850. tmp2 = sin*tmp1;
  851. diag[k+1] = tmp0+tmp2;
  852. tmp0 = cos*tmp1-tmp4;
  853. for (int row = 0; row < 3; row++)
  854. {
  855. tmp3 = m[row][k+1];
  856. m[row][k+1] = sin*m[row][k] + cos*tmp3;
  857. m[row][k] = cos*m[row][k] - sin*tmp3;
  858. }
  859. }
  860. diag[i] -= tmp2;
  861. subDiag[i] = tmp0;
  862. subDiag[j] = 0.0;
  863. }
  864. if (iter == maxIter)
  865. {
  866. // Should not get here under normal circumstances
  867. return false;
  868. }
  869. }
  870. return true;
  871. }
  872. void Matrix3::eigenSolveSymmetric(float eigenValues[3], Vector3 eigenVectors[3]) const
  873. {
  874. Matrix3 mat = *this;
  875. float subDiag[3];
  876. mat.tridiagonal(eigenValues, subDiag);
  877. mat.QLAlgorithm(eigenValues, subDiag);
  878. for (UINT32 i = 0; i < 3; i++)
  879. {
  880. eigenVectors[i][0] = mat[0][i];
  881. eigenVectors[i][1] = mat[1][i];
  882. eigenVectors[i][2] = mat[2][i];
  883. }
  884. // Make eigenvectors form a right--handed system
  885. Vector3 cross = eigenVectors[1].cross(eigenVectors[2]);
  886. float det = eigenVectors[0].dot(cross);
  887. if (det < 0.0f)
  888. {
  889. eigenVectors[2][0] = -eigenVectors[2][0];
  890. eigenVectors[2][1] = -eigenVectors[2][1];
  891. eigenVectors[2][2] = -eigenVectors[2][2];
  892. }
  893. }
  894. }