BsMatrix3.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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[1][2]));
  657. if (xAngle < Radian(Math::HALF_PI))
  658. {
  659. if (xAngle > Radian(-Math::HALF_PI))
  660. {
  661. yAngle = Math::atan2(m[0][2], m[2][2]);
  662. zAngle = Math::atan2(m[1][0], m[1][1]);
  663. return true;
  664. }
  665. else
  666. {
  667. // Note: Not an unique solution.
  668. xAngle = Radian(-Math::HALF_PI);
  669. yAngle = Math::atan2(-m[0][1], m[0][0]);
  670. zAngle = Radian(0.0f);
  671. return false;
  672. }
  673. }
  674. else
  675. {
  676. // Note: Not an unique solution.
  677. xAngle = Radian(Math::HALF_PI);
  678. yAngle = Math::atan2(m[0][1], m[0][0]);
  679. zAngle = Radian(0.0f);
  680. return false;
  681. }
  682. }
  683. void Matrix3::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  684. {
  685. float cx = Math::cos(xAngle);
  686. float sx = Math::sin(xAngle);
  687. float cy = Math::cos(yAngle);
  688. float sy = Math::sin(yAngle);
  689. float cz = Math::cos(zAngle);
  690. float sz = Math::sin(zAngle);
  691. m[0][0] = cy * cz + sx * sy * sz;
  692. m[0][1] = cz * sx * sy - cy * sz;
  693. m[0][2] = cx * sy;
  694. m[1][0] = cx * sz;
  695. m[1][1] = cx * cz;
  696. m[1][2] = -sx;
  697. m[2][0] = -cz * sy + cy * sx * sz;
  698. m[2][1] = cy * cz * sx + sy * sz;
  699. m[2][2] = cx * cy;
  700. }
  701. void Matrix3::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  702. {
  703. const EulerAngleOrderData& l = EA_LOOKUP[(int)order];
  704. Matrix3 mats[3];
  705. float cx = Math::cos(xAngle);
  706. float sx = Math::sin(xAngle);
  707. mats[0] = Matrix3(
  708. 1.0f, 0.0f, 0.0f,
  709. 0.0f, cx, -sx,
  710. 0.0f, sx, cx);
  711. float cy = Math::cos(yAngle);
  712. float sy = Math::sin(yAngle);
  713. mats[1] = Matrix3(
  714. cy, 0.0f, sy,
  715. 0.0f, 1.0f, 0.0f,
  716. -sy, 0.0f, cy);
  717. float cz = Math::cos(zAngle);
  718. float sz = Math::sin(zAngle);
  719. mats[2] = Matrix3(
  720. cz, -sz, 0.0f,
  721. sz, cz, 0.0f,
  722. 0.0f, 0.0f, 1.0f);
  723. *this = mats[l.a]*(mats[l.b]*mats[l.c]);
  724. }
  725. void Matrix3::tridiagonal(float diag[3], float subDiag[3])
  726. {
  727. // Householder reduction T = Q^t M Q
  728. // Input:
  729. // mat, symmetric 3x3 matrix M
  730. // Output:
  731. // mat, orthogonal matrix Q
  732. // diag, diagonal entries of T
  733. // subd, subdiagonal entries of T (T is symmetric)
  734. float fA = m[0][0];
  735. float fB = m[0][1];
  736. float fC = m[0][2];
  737. float fD = m[1][1];
  738. float fE = m[1][2];
  739. float fF = m[2][2];
  740. diag[0] = fA;
  741. subDiag[2] = 0.0;
  742. if (Math::abs(fC) >= EPSILON)
  743. {
  744. float length = Math::sqrt(fB*fB+fC*fC);
  745. float invLength = 1.0f/length;
  746. fB *= invLength;
  747. fC *= invLength;
  748. float fQ = 2.0f*fB*fE+fC*(fF-fD);
  749. diag[1] = fD+fC*fQ;
  750. diag[2] = fF-fC*fQ;
  751. subDiag[0] = length;
  752. subDiag[1] = fE-fB*fQ;
  753. m[0][0] = 1.0;
  754. m[0][1] = 0.0;
  755. m[0][2] = 0.0;
  756. m[1][0] = 0.0;
  757. m[1][1] = fB;
  758. m[1][2] = fC;
  759. m[2][0] = 0.0;
  760. m[2][1] = fC;
  761. m[2][2] = -fB;
  762. }
  763. else
  764. {
  765. diag[1] = fD;
  766. diag[2] = fF;
  767. subDiag[0] = fB;
  768. subDiag[1] = fE;
  769. m[0][0] = 1.0;
  770. m[0][1] = 0.0;
  771. m[0][2] = 0.0;
  772. m[1][0] = 0.0;
  773. m[1][1] = 1.0;
  774. m[1][2] = 0.0;
  775. m[2][0] = 0.0;
  776. m[2][1] = 0.0;
  777. m[2][2] = 1.0;
  778. }
  779. }
  780. bool Matrix3::QLAlgorithm(float diag[3], float subDiag[3])
  781. {
  782. // QL iteration with implicit shifting to reduce matrix from tridiagonal to diagonal
  783. for (int i = 0; i < 3; i++)
  784. {
  785. const unsigned int maxIter = 32;
  786. unsigned int iter;
  787. for (iter = 0; iter < maxIter; iter++)
  788. {
  789. int j;
  790. for (j = i; j <= 1; j++)
  791. {
  792. float sum = Math::abs(diag[j]) + Math::abs(diag[j+1]);
  793. if (Math::abs(subDiag[j]) + sum == sum)
  794. break;
  795. }
  796. if (j == i)
  797. break;
  798. float tmp0 = (diag[i+1]-diag[i])/(2.0f*subDiag[i]);
  799. float tmp1 = Math::sqrt(tmp0*tmp0+1.0f);
  800. if (tmp0 < 0.0f)
  801. tmp0 = diag[j]-diag[i]+subDiag[i]/(tmp0-tmp1);
  802. else
  803. tmp0 = diag[j]-diag[i]+subDiag[i]/(tmp0+tmp1);
  804. float sin = 1.0f;
  805. float cos = 1.0f;
  806. float tmp2 = 0.0f;
  807. for (int k = j-1; k >= i; k--)
  808. {
  809. float tmp3 = sin*subDiag[k];
  810. float tmp4 = cos*subDiag[k];
  811. if (Math::abs(tmp3) >= Math::abs(tmp0))
  812. {
  813. cos = tmp0/tmp3;
  814. tmp1 = Math::sqrt(cos*cos+1.0f);
  815. subDiag[k+1] = tmp3*tmp1;
  816. sin = 1.0f/tmp1;
  817. cos *= sin;
  818. }
  819. else
  820. {
  821. sin = tmp3/tmp0;
  822. tmp1 = Math::sqrt(sin*sin+1.0f);
  823. subDiag[k+1] = tmp0*tmp1;
  824. cos = 1.0f/tmp1;
  825. sin *= cos;
  826. }
  827. tmp0 = diag[k+1]-tmp2;
  828. tmp1 = (diag[k]-tmp0)*sin+2.0f*tmp4*cos;
  829. tmp2 = sin*tmp1;
  830. diag[k+1] = tmp0+tmp2;
  831. tmp0 = cos*tmp1-tmp4;
  832. for (int row = 0; row < 3; row++)
  833. {
  834. tmp3 = m[row][k+1];
  835. m[row][k+1] = sin*m[row][k] + cos*tmp3;
  836. m[row][k] = cos*m[row][k] - sin*tmp3;
  837. }
  838. }
  839. diag[i] -= tmp2;
  840. subDiag[i] = tmp0;
  841. subDiag[j] = 0.0;
  842. }
  843. if (iter == maxIter)
  844. {
  845. // Should not get here under normal circumstances
  846. return false;
  847. }
  848. }
  849. return true;
  850. }
  851. void Matrix3::eigenSolveSymmetric(float eigenValues[3], Vector3 eigenVectors[3]) const
  852. {
  853. Matrix3 mat = *this;
  854. float subDiag[3];
  855. mat.tridiagonal(eigenValues, subDiag);
  856. mat.QLAlgorithm(eigenValues, subDiag);
  857. for (UINT32 i = 0; i < 3; i++)
  858. {
  859. eigenVectors[i][0] = mat[0][i];
  860. eigenVectors[i][1] = mat[1][i];
  861. eigenVectors[i][2] = mat[2][i];
  862. }
  863. // Make eigenvectors form a right--handed system
  864. Vector3 cross = eigenVectors[1].cross(eigenVectors[2]);
  865. float det = eigenVectors[0].dot(cross);
  866. if (det < 0.0f)
  867. {
  868. eigenVectors[2][0] = -eigenVectors[2][0];
  869. eigenVectors[2][1] = -eigenVectors[2][1];
  870. eigenVectors[2][2] = -eigenVectors[2][2];
  871. }
  872. }
  873. }