2
0

BsMatrix3.cpp 31 KB

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