2
0

BsMatrix3.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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::decomposition(Quaternion& rotation, Vector3& scale) const
  493. {
  494. Matrix3 matQ;
  495. Vector3 vecU;
  496. QDUDecomposition(matQ, scale, vecU);
  497. rotation = Quaternion(matQ);
  498. }
  499. void Matrix3::QDUDecomposition(Matrix3& matQ, Vector3& vecD, Vector3& vecU) const
  500. {
  501. // Build orthogonal matrix Q
  502. float invLength = Math::invSqrt(m[0][0]*m[0][0] + m[1][0]*m[1][0] + m[2][0]*m[2][0]);
  503. matQ[0][0] = m[0][0]*invLength;
  504. matQ[1][0] = m[1][0]*invLength;
  505. matQ[2][0] = m[2][0]*invLength;
  506. float dot = matQ[0][0]*m[0][1] + matQ[1][0]*m[1][1] + matQ[2][0]*m[2][1];
  507. matQ[0][1] = m[0][1]-dot*matQ[0][0];
  508. matQ[1][1] = m[1][1]-dot*matQ[1][0];
  509. matQ[2][1] = m[2][1]-dot*matQ[2][0];
  510. invLength = Math::invSqrt(matQ[0][1]*matQ[0][1] + matQ[1][1]*matQ[1][1] + matQ[2][1]*matQ[2][1]);
  511. matQ[0][1] *= invLength;
  512. matQ[1][1] *= invLength;
  513. matQ[2][1] *= invLength;
  514. dot = matQ[0][0]*m[0][2] + matQ[1][0]*m[1][2] + matQ[2][0]*m[2][2];
  515. matQ[0][2] = m[0][2]-dot*matQ[0][0];
  516. matQ[1][2] = m[1][2]-dot*matQ[1][0];
  517. matQ[2][2] = m[2][2]-dot*matQ[2][0];
  518. dot = matQ[0][1]*m[0][2] + matQ[1][1]*m[1][2] + matQ[2][1]*m[2][2];
  519. matQ[0][2] -= dot*matQ[0][1];
  520. matQ[1][2] -= dot*matQ[1][1];
  521. matQ[2][2] -= dot*matQ[2][1];
  522. invLength = Math::invSqrt(matQ[0][2]*matQ[0][2] + matQ[1][2]*matQ[1][2] + matQ[2][2]*matQ[2][2]);
  523. matQ[0][2] *= invLength;
  524. matQ[1][2] *= invLength;
  525. matQ[2][2] *= invLength;
  526. // Guarantee that orthogonal matrix has determinant 1 (no reflections)
  527. float fDet = matQ[0][0]*matQ[1][1]*matQ[2][2] + matQ[0][1]*matQ[1][2]*matQ[2][0] +
  528. matQ[0][2]*matQ[1][0]*matQ[2][1] - matQ[0][2]*matQ[1][1]*matQ[2][0] -
  529. matQ[0][1]*matQ[1][0]*matQ[2][2] - matQ[0][0]*matQ[1][2]*matQ[2][1];
  530. if (fDet < 0.0f)
  531. {
  532. for (UINT32 row = 0; row < 3; row++)
  533. for (UINT32 col = 0; col < 3; col++)
  534. matQ[row][col] = -matQ[row][col];
  535. }
  536. // Build "right" matrix R
  537. Matrix3 matRight;
  538. matRight[0][0] = matQ[0][0]*m[0][0] + matQ[1][0]*m[1][0] +
  539. matQ[2][0]*m[2][0];
  540. matRight[0][1] = matQ[0][0]*m[0][1] + matQ[1][0]*m[1][1] +
  541. matQ[2][0]*m[2][1];
  542. matRight[1][1] = matQ[0][1]*m[0][1] + matQ[1][1]*m[1][1] +
  543. matQ[2][1]*m[2][1];
  544. matRight[0][2] = matQ[0][0]*m[0][2] + matQ[1][0]*m[1][2] +
  545. matQ[2][0]*m[2][2];
  546. matRight[1][2] = matQ[0][1]*m[0][2] + matQ[1][1]*m[1][2] +
  547. matQ[2][1]*m[2][2];
  548. matRight[2][2] = matQ[0][2]*m[0][2] + matQ[1][2]*m[1][2] +
  549. matQ[2][2]*m[2][2];
  550. // The scaling component
  551. vecD[0] = matRight[0][0];
  552. vecD[1] = matRight[1][1];
  553. vecD[2] = matRight[2][2];
  554. // The shear component
  555. float invD0 = 1.0f/vecD[0];
  556. vecU[0] = matRight[0][1]*invD0;
  557. vecU[1] = matRight[0][2]*invD0;
  558. vecU[2] = matRight[1][2]/vecD[1];
  559. }
  560. void Matrix3::toAxisAngle(Vector3& axis, Radian& radians) const
  561. {
  562. float trace = m[0][0] + m[1][1] + m[2][2];
  563. float cos = 0.5f*(trace-1.0f);
  564. radians = Math::acos(cos); // In [0, PI]
  565. if (radians > Radian(0.0f))
  566. {
  567. if (radians < Radian(Math::PI))
  568. {
  569. axis.x = m[2][1]-m[1][2];
  570. axis.y = m[0][2]-m[2][0];
  571. axis.z = m[1][0]-m[0][1];
  572. axis.normalize();
  573. }
  574. else
  575. {
  576. // Angle is PI
  577. float fHalfInverse;
  578. if (m[0][0] >= m[1][1])
  579. {
  580. // r00 >= r11
  581. if (m[0][0] >= m[2][2])
  582. {
  583. // r00 is maximum diagonal term
  584. axis.x = 0.5f*Math::sqrt(m[0][0] - m[1][1] - m[2][2] + 1.0f);
  585. fHalfInverse = 0.5f/axis.x;
  586. axis.y = fHalfInverse*m[0][1];
  587. axis.z = fHalfInverse*m[0][2];
  588. }
  589. else
  590. {
  591. // r22 is maximum diagonal term
  592. axis.z = 0.5f*Math::sqrt(m[2][2] - m[0][0] - m[1][1] + 1.0f);
  593. fHalfInverse = 0.5f/axis.z;
  594. axis.x = fHalfInverse*m[0][2];
  595. axis.y = fHalfInverse*m[1][2];
  596. }
  597. }
  598. else
  599. {
  600. // r11 > r00
  601. if ( m[1][1] >= m[2][2] )
  602. {
  603. // r11 is maximum diagonal term
  604. axis.y = 0.5f*Math::sqrt(m[1][1] - m[0][0] - m[2][2] + 1.0f);
  605. fHalfInverse = 0.5f/axis.y;
  606. axis.x = fHalfInverse*m[0][1];
  607. axis.z = fHalfInverse*m[1][2];
  608. }
  609. else
  610. {
  611. // r22 is maximum diagonal term
  612. axis.z = 0.5f*Math::sqrt(m[2][2] - m[0][0] - m[1][1] + 1.0f);
  613. fHalfInverse = 0.5f/axis.z;
  614. axis.x = fHalfInverse*m[0][2];
  615. axis.y = fHalfInverse*m[1][2];
  616. }
  617. }
  618. }
  619. }
  620. else
  621. {
  622. // The angle is 0 and the matrix is the identity. Any axis will
  623. // work, so just use the x-axis.
  624. axis.x = 1.0f;
  625. axis.y = 0.0f;
  626. axis.z = 0.0f;
  627. }
  628. }
  629. void Matrix3::fromAxisAngle(const Vector3& axis, const Radian& angle)
  630. {
  631. float cos = Math::cos(angle);
  632. float sin = Math::sin(angle);
  633. float oneMinusCos = 1.0f-cos;
  634. float x2 = axis.x*axis.x;
  635. float y2 = axis.y*axis.y;
  636. float z2 = axis.z*axis.z;
  637. float xym = axis.x*axis.y*oneMinusCos;
  638. float xzm = axis.x*axis.z*oneMinusCos;
  639. float yzm = axis.y*axis.z*oneMinusCos;
  640. float xSin = axis.x*sin;
  641. float ySin = axis.y*sin;
  642. float zSin = axis.z*sin;
  643. m[0][0] = x2*oneMinusCos+cos;
  644. m[0][1] = xym-zSin;
  645. m[0][2] = xzm+ySin;
  646. m[1][0] = xym+zSin;
  647. m[1][1] = y2*oneMinusCos+cos;
  648. m[1][2] = yzm-xSin;
  649. m[2][0] = xzm-ySin;
  650. m[2][1] = yzm+xSin;
  651. m[2][2] = z2*oneMinusCos+cos;
  652. }
  653. void Matrix3::toQuaternion(Quaternion& quat) const
  654. {
  655. quat.fromRotationMatrix(*this);
  656. }
  657. void Matrix3::fromQuaternion(const Quaternion& quat)
  658. {
  659. quat.toRotationMatrix(*this);
  660. }
  661. bool Matrix3::toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const
  662. {
  663. xAngle = -Radian(Math::asin(m[1][2]));
  664. if (xAngle < Radian(Math::HALF_PI))
  665. {
  666. if (xAngle > Radian(-Math::HALF_PI))
  667. {
  668. yAngle = Math::atan2(m[0][2], m[2][2]);
  669. zAngle = Math::atan2(m[1][0], m[1][1]);
  670. return true;
  671. }
  672. else
  673. {
  674. // Note: Not an unique solution.
  675. xAngle = Radian(-Math::HALF_PI);
  676. yAngle = Math::atan2(-m[0][1], m[0][0]);
  677. zAngle = Radian(0.0f);
  678. return false;
  679. }
  680. }
  681. else
  682. {
  683. // Note: Not an unique solution.
  684. xAngle = Radian(Math::HALF_PI);
  685. yAngle = Math::atan2(m[0][1], m[0][0]);
  686. zAngle = Radian(0.0f);
  687. return false;
  688. }
  689. }
  690. void Matrix3::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  691. {
  692. float cx = Math::cos(xAngle);
  693. float sx = Math::sin(xAngle);
  694. float cy = Math::cos(yAngle);
  695. float sy = Math::sin(yAngle);
  696. float cz = Math::cos(zAngle);
  697. float sz = Math::sin(zAngle);
  698. m[0][0] = cy * cz + sx * sy * sz;
  699. m[0][1] = cz * sx * sy - cy * sz;
  700. m[0][2] = cx * sy;
  701. m[1][0] = cx * sz;
  702. m[1][1] = cx * cz;
  703. m[1][2] = -sx;
  704. m[2][0] = -cz * sy + cy * sx * sz;
  705. m[2][1] = cy * cz * sx + sy * sz;
  706. m[2][2] = cx * cy;
  707. }
  708. void Matrix3::fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  709. {
  710. const EulerAngleOrderData& l = EA_LOOKUP[(int)order];
  711. Matrix3 mats[3];
  712. float cx = Math::cos(xAngle);
  713. float sx = Math::sin(xAngle);
  714. mats[0] = Matrix3(
  715. 1.0f, 0.0f, 0.0f,
  716. 0.0f, cx, -sx,
  717. 0.0f, sx, cx);
  718. float cy = Math::cos(yAngle);
  719. float sy = Math::sin(yAngle);
  720. mats[1] = Matrix3(
  721. cy, 0.0f, sy,
  722. 0.0f, 1.0f, 0.0f,
  723. -sy, 0.0f, cy);
  724. float cz = Math::cos(zAngle);
  725. float sz = Math::sin(zAngle);
  726. mats[2] = Matrix3(
  727. cz, -sz, 0.0f,
  728. sz, cz, 0.0f,
  729. 0.0f, 0.0f, 1.0f);
  730. *this = mats[l.a]*(mats[l.b]*mats[l.c]);
  731. }
  732. void Matrix3::tridiagonal(float diag[3], float subDiag[3])
  733. {
  734. // Householder reduction T = Q^t M Q
  735. // Input:
  736. // mat, symmetric 3x3 matrix M
  737. // Output:
  738. // mat, orthogonal matrix Q
  739. // diag, diagonal entries of T
  740. // subd, subdiagonal entries of T (T is symmetric)
  741. float fA = m[0][0];
  742. float fB = m[0][1];
  743. float fC = m[0][2];
  744. float fD = m[1][1];
  745. float fE = m[1][2];
  746. float fF = m[2][2];
  747. diag[0] = fA;
  748. subDiag[2] = 0.0;
  749. if (Math::abs(fC) >= EPSILON)
  750. {
  751. float length = Math::sqrt(fB*fB+fC*fC);
  752. float invLength = 1.0f/length;
  753. fB *= invLength;
  754. fC *= invLength;
  755. float fQ = 2.0f*fB*fE+fC*(fF-fD);
  756. diag[1] = fD+fC*fQ;
  757. diag[2] = fF-fC*fQ;
  758. subDiag[0] = length;
  759. subDiag[1] = fE-fB*fQ;
  760. m[0][0] = 1.0;
  761. m[0][1] = 0.0;
  762. m[0][2] = 0.0;
  763. m[1][0] = 0.0;
  764. m[1][1] = fB;
  765. m[1][2] = fC;
  766. m[2][0] = 0.0;
  767. m[2][1] = fC;
  768. m[2][2] = -fB;
  769. }
  770. else
  771. {
  772. diag[1] = fD;
  773. diag[2] = fF;
  774. subDiag[0] = fB;
  775. subDiag[1] = fE;
  776. m[0][0] = 1.0;
  777. m[0][1] = 0.0;
  778. m[0][2] = 0.0;
  779. m[1][0] = 0.0;
  780. m[1][1] = 1.0;
  781. m[1][2] = 0.0;
  782. m[2][0] = 0.0;
  783. m[2][1] = 0.0;
  784. m[2][2] = 1.0;
  785. }
  786. }
  787. bool Matrix3::QLAlgorithm(float diag[3], float subDiag[3])
  788. {
  789. // QL iteration with implicit shifting to reduce matrix from tridiagonal to diagonal
  790. for (int i = 0; i < 3; i++)
  791. {
  792. const unsigned int maxIter = 32;
  793. unsigned int iter;
  794. for (iter = 0; iter < maxIter; iter++)
  795. {
  796. int j;
  797. for (j = i; j <= 1; j++)
  798. {
  799. float sum = Math::abs(diag[j]) + Math::abs(diag[j+1]);
  800. if (Math::abs(subDiag[j]) + sum == sum)
  801. break;
  802. }
  803. if (j == i)
  804. break;
  805. float tmp0 = (diag[i+1]-diag[i])/(2.0f*subDiag[i]);
  806. float tmp1 = Math::sqrt(tmp0*tmp0+1.0f);
  807. if (tmp0 < 0.0f)
  808. tmp0 = diag[j]-diag[i]+subDiag[i]/(tmp0-tmp1);
  809. else
  810. tmp0 = diag[j]-diag[i]+subDiag[i]/(tmp0+tmp1);
  811. float sin = 1.0f;
  812. float cos = 1.0f;
  813. float tmp2 = 0.0f;
  814. for (int k = j-1; k >= i; k--)
  815. {
  816. float tmp3 = sin*subDiag[k];
  817. float tmp4 = cos*subDiag[k];
  818. if (Math::abs(tmp3) >= Math::abs(tmp0))
  819. {
  820. cos = tmp0/tmp3;
  821. tmp1 = Math::sqrt(cos*cos+1.0f);
  822. subDiag[k+1] = tmp3*tmp1;
  823. sin = 1.0f/tmp1;
  824. cos *= sin;
  825. }
  826. else
  827. {
  828. sin = tmp3/tmp0;
  829. tmp1 = Math::sqrt(sin*sin+1.0f);
  830. subDiag[k+1] = tmp0*tmp1;
  831. cos = 1.0f/tmp1;
  832. sin *= cos;
  833. }
  834. tmp0 = diag[k+1]-tmp2;
  835. tmp1 = (diag[k]-tmp0)*sin+2.0f*tmp4*cos;
  836. tmp2 = sin*tmp1;
  837. diag[k+1] = tmp0+tmp2;
  838. tmp0 = cos*tmp1-tmp4;
  839. for (int row = 0; row < 3; row++)
  840. {
  841. tmp3 = m[row][k+1];
  842. m[row][k+1] = sin*m[row][k] + cos*tmp3;
  843. m[row][k] = cos*m[row][k] - sin*tmp3;
  844. }
  845. }
  846. diag[i] -= tmp2;
  847. subDiag[i] = tmp0;
  848. subDiag[j] = 0.0;
  849. }
  850. if (iter == maxIter)
  851. {
  852. // Should not get here under normal circumstances
  853. return false;
  854. }
  855. }
  856. return true;
  857. }
  858. void Matrix3::eigenSolveSymmetric(float eigenValues[3], Vector3 eigenVectors[3]) const
  859. {
  860. Matrix3 mat = *this;
  861. float subDiag[3];
  862. mat.tridiagonal(eigenValues, subDiag);
  863. mat.QLAlgorithm(eigenValues, subDiag);
  864. for (UINT32 i = 0; i < 3; i++)
  865. {
  866. eigenVectors[i][0] = mat[0][i];
  867. eigenVectors[i][1] = mat[1][i];
  868. eigenVectors[i][2] = mat[2][i];
  869. }
  870. // Make eigenvectors form a right--handed system
  871. Vector3 cross = eigenVectors[1].cross(eigenVectors[2]);
  872. float det = eigenVectors[0].dot(cross);
  873. if (det < 0.0f)
  874. {
  875. eigenVectors[2][0] = -eigenVectors[2][0];
  876. eigenVectors[2][1] = -eigenVectors[2][1];
  877. eigenVectors[2][2] = -eigenVectors[2][2];
  878. }
  879. }
  880. }