matrix3d.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/wwmath/matrix3d.cpp 42 6/29/01 6:41p Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Voxel Technology *
  24. * *
  25. * File Name : MATRIX3D.CPP *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 02/24/97 *
  30. * *
  31. * Last Update : February 28, 1997 [GH] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Matrix3D::Set_Rotation -- Sets the rotation part of the matrix *
  36. * Matrix3D::Set_Rotation -- Sets the rotation part of the matrix *
  37. * Matrix3D::Set -- Init a matrix3D from a matrix3 and a position *
  38. * Matrix3D::Set -- Init a matrix3D from a quaternion and a position *
  39. * Matrix3D::Get_X_Rotation -- approximates the rotation about the X axis *
  40. * Matrix3D::Get_Y_Rotation -- approximates the rotation about the Y axis *
  41. * Matrix3D::Get_Z_Rotation -- approximates the rotation about the Z axis *
  42. * Matrix3D::Multiply -- matrix multiplication without temporaries. *
  43. * Matrix3D::Inverse_Rotate_Vector -- rotates a vector by the inverse of the 3x3 sub-matrix *
  44. * Matrix3D::Transform_Min_Max_AABox -- compute transformed axis-aligned box *
  45. * Matrix3D::Transform_Center_Extent_AABox -- compute transformed axis-aligned box *
  46. * Matrix3D::Get_Inverse -- calculate the inverse of this matrix *
  47. * Matrix3D::Get_Orthogonal_Inverse -- Returns the inverse of the matrix *
  48. * Matrix3D::Re_Orthogonalize -- makes this matrix orthogonal. *
  49. * Matrix3D::Is_Orthogonal -- checks whether this matrix is orthogonal *
  50. * Lerp - linearly interpolate matrices (orientation is slerped) *
  51. * Matrix3D::Solve_Linear_System -- 3x3 Gauss-Jordan elimination *
  52. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  53. #include "matrix3d.h"
  54. #include <math.h>
  55. #include <assert.h>
  56. #include <stdlib.h>
  57. //#include <stdio.h>
  58. #include "vector3.h"
  59. #include "matrix3.h"
  60. #include "matrix4.h"
  61. #include "quat.h"
  62. // some static matrices which are sometimes useful
  63. const Matrix3D Matrix3D::Identity
  64. (
  65. 1.0, 0.0, 0.0, 0.0,
  66. 0.0, 1.0, 0.0, 0.0,
  67. 0.0, 0.0, 1.0, 0.0
  68. );
  69. const Matrix3D Matrix3D::RotateX90
  70. (
  71. 1.0, 0.0, 0.0, 0.0,
  72. 0.0, 0.0, -1.0, 0.0,
  73. 0.0, 1.0, 0.0, 0.0
  74. );
  75. const Matrix3D Matrix3D::RotateX180
  76. (
  77. 1.0, 0.0, 0.0, 0.0,
  78. 0.0, -1.0, 0.0, 0.0,
  79. 0.0, 0.0, -1.0, 0.0
  80. );
  81. const Matrix3D Matrix3D::RotateX270
  82. (
  83. 1.0, 0.0, 0.0, 0.0,
  84. 0.0, 0.0, 1.0, 0.0,
  85. 0.0, -1.0, 0.0, 0.0
  86. );
  87. const Matrix3D Matrix3D::RotateY90
  88. (
  89. 0.0, 0.0, 1.0, 0.0,
  90. 0.0, 1.0, 0.0, 0.0,
  91. -1.0, 0.0, 0.0, 0.0
  92. );
  93. const Matrix3D Matrix3D::RotateY180
  94. (
  95. -1.0, 0.0, 0.0, 0.0,
  96. 0.0, 1.0, 0.0, 0.0,
  97. 0.0, 0.0, -1.0, 0.0
  98. );
  99. const Matrix3D Matrix3D::RotateY270
  100. (
  101. 0.0, 0.0, -1.0, 0.0,
  102. 0.0, 1.0, 0.0, 0.0,
  103. 1.0, 0.0, 0.0, 0.0
  104. );
  105. const Matrix3D Matrix3D::RotateZ90
  106. (
  107. 0.0, -1.0, 0.0, 0.0,
  108. 1.0, 0.0, 0.0, 0.0,
  109. 0.0, 0.0, 1.0, 0.0
  110. );
  111. const Matrix3D Matrix3D::RotateZ180
  112. (
  113. -1.0, 0.0, 0.0, 0.0,
  114. 0.0, -1.0, 0.0, 0.0,
  115. 0.0, 0.0, 1.0, 0.0
  116. );
  117. const Matrix3D Matrix3D::RotateZ270
  118. (
  119. 0.0, 1.0, 0.0, 0.0,
  120. -1.0, 0.0, 0.0, 0.0,
  121. 0.0, 0.0, 1.0, 0.0
  122. );
  123. /***********************************************************************************************
  124. * Matrix3D::Set -- Init a matrix3D from a matrix3 and a position *
  125. * *
  126. * INPUT: *
  127. * *
  128. * OUTPUT: *
  129. * *
  130. * WARNINGS: *
  131. * *
  132. * HISTORY: *
  133. *=============================================================================================*/
  134. void Matrix3D::Set(const Matrix3 & rot,const Vector3 & pos)
  135. {
  136. Row[0].Set( rot[0][0], rot[0][1], rot[0][2], pos[0]);
  137. Row[1].Set( rot[1][0], rot[1][1], rot[1][2], pos[1]);
  138. Row[2].Set( rot[2][0], rot[2][1], rot[2][2], pos[2]);
  139. }
  140. /***********************************************************************************************
  141. * Matrix3D::Set -- Init a matrix3D from a quaternion and a position *
  142. * *
  143. * INPUT: *
  144. * *
  145. * OUTPUT: *
  146. * *
  147. * WARNINGS: *
  148. * *
  149. * HISTORY: *
  150. *=============================================================================================*/
  151. void Matrix3D::Set(const Quaternion & rot,const Vector3 & pos)
  152. {
  153. Set_Rotation(rot);
  154. Set_Translation(pos);
  155. }
  156. /***********************************************************************************************
  157. * Matrix3D::Set_Rotation -- Sets the rotation part of the matrix *
  158. * *
  159. * INPUT: *
  160. * *
  161. * OUTPUT: *
  162. * *
  163. * WARNINGS: *
  164. * *
  165. * HISTORY: *
  166. * 5/11/98 GTH : Created. *
  167. *=============================================================================================*/
  168. void Matrix3D::Set_Rotation(const Matrix3 & m)
  169. {
  170. Row[0][0] = m[0][0];
  171. Row[0][1] = m[0][1];
  172. Row[0][2] = m[0][2];
  173. Row[1][0] = m[1][0];
  174. Row[1][1] = m[1][1];
  175. Row[1][2] = m[1][2];
  176. Row[2][0] = m[2][0];
  177. Row[2][1] = m[2][1];
  178. Row[2][2] = m[2][2];
  179. }
  180. /***********************************************************************************************
  181. * Matrix3D::Set_Rotation -- Sets the rotation part of the matrix *
  182. * *
  183. * INPUT: *
  184. * *
  185. * OUTPUT: *
  186. * *
  187. * WARNINGS: *
  188. * *
  189. * HISTORY: *
  190. * 5/11/98 GTH : Created. *
  191. *=============================================================================================*/
  192. void Matrix3D::Set_Rotation(const Quaternion & q)
  193. {
  194. Row[0][0] = (float)(1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]));
  195. Row[0][1] = (float)(2.0 * (q[0] * q[1] - q[2] * q[3]));
  196. Row[0][2] = (float)(2.0 * (q[2] * q[0] + q[1] * q[3]));
  197. Row[1][0] = (float)(2.0 * (q[0] * q[1] + q[2] * q[3]));
  198. Row[1][1] = (float)(1.0 - 2.0f * (q[2] * q[2] + q[0] * q[0]));
  199. Row[1][2] = (float)(2.0 * (q[1] * q[2] - q[0] * q[3]));
  200. Row[2][0] = (float)(2.0 * (q[2] * q[0] - q[1] * q[3]));
  201. Row[2][1] = (float)(2.0 * (q[1] * q[2] + q[0] * q[3]));
  202. Row[2][2] =(float)(1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]));
  203. }
  204. /***********************************************************************************************
  205. * Matrix3D::Get_X_Rotation -- approximates the rotation about the X axis *
  206. * *
  207. * INPUT: *
  208. * *
  209. * OUTPUT: *
  210. * *
  211. * WARNINGS: *
  212. * *
  213. * HISTORY: *
  214. * 08/11/1997 GH : Created. *
  215. *=============================================================================================*/
  216. float Matrix3D::Get_X_Rotation(void) const
  217. {
  218. return WWMath::Atan2(Row[2][1], Row[1][1]);
  219. }
  220. /***********************************************************************************************
  221. * Matrix3D::Get_Y_Rotation -- approximates the rotation about the Y axis *
  222. * *
  223. * INPUT: *
  224. * *
  225. * OUTPUT: *
  226. * *
  227. * WARNINGS: *
  228. * *
  229. * HISTORY: *
  230. * 08/11/1997 GH : Created. *
  231. *=============================================================================================*/
  232. float Matrix3D::Get_Y_Rotation(void) const
  233. {
  234. return WWMath::Atan2(Row[0][2], Row[2][2]);
  235. }
  236. /***********************************************************************************************
  237. * Matrix3D::Get_Z_Rotation -- approximates the rotation about the Z axis *
  238. * *
  239. * INPUT: *
  240. * *
  241. * OUTPUT: *
  242. * *
  243. * WARNINGS: *
  244. * *
  245. * HISTORY: *
  246. * 08/11/1997 GH : Created. *
  247. *=============================================================================================*/
  248. float Matrix3D::Get_Z_Rotation(void) const
  249. {
  250. return WWMath::Atan2(Row[1][0], Row[0][0]);
  251. }
  252. /***********************************************************************************************
  253. * M3DC::Rotate_Vector -- Uses the 3x3 sub-matrix to rotate a vector *
  254. * *
  255. * INPUT: *
  256. * *
  257. * OUTPUT: *
  258. * *
  259. * WARNINGS: *
  260. * *
  261. * HISTORY: *
  262. *=============================================================================================*/
  263. Vector3 Matrix3D::Rotate_Vector(const Vector3 &vect) const
  264. {
  265. return Vector3(
  266. (Row[0][0]*vect[0] + Row[0][1]*vect[1] + Row[0][2]*vect[2]),
  267. (Row[1][0]*vect[0] + Row[1][1]*vect[1] + Row[1][2]*vect[2]),
  268. (Row[2][0]*vect[0] + Row[2][1]*vect[1] + Row[2][2]*vect[2])
  269. );
  270. }
  271. /***********************************************************************************************
  272. * Matrix3D::Inverse_Rotate_Vector -- rotates a vector by the inverse of the 3x3 sub-matrix *
  273. * *
  274. * INPUT: *
  275. * *
  276. * OUTPUT: *
  277. * *
  278. * WARNINGS: *
  279. * *
  280. * HISTORY: *
  281. * 4/27/98 GTH : Created. *
  282. *=============================================================================================*/
  283. Vector3 Matrix3D::Inverse_Rotate_Vector(const Vector3 &vect) const
  284. {
  285. return Vector3(
  286. (Row[0][0]*vect[0] + Row[1][0]*vect[1] + Row[2][0]*vect[2]),
  287. (Row[0][1]*vect[0] + Row[1][1]*vect[1] + Row[2][1]*vect[2]),
  288. (Row[0][2]*vect[0] + Row[1][2]*vect[1] + Row[2][2]*vect[2])
  289. );
  290. }
  291. /***********************************************************************************************
  292. * M3DC::Look_At -- Creates a "look at" transformation. *
  293. * *
  294. * Builds a transformation matrix which positions the origin at p, *
  295. * points the negative z-axis towards a target t, and rolls about the z-axis *
  296. * by the angle specified by roll. *
  297. * *
  298. * This can be useful for creating a camera matrix, just invert *
  299. * the matrix after initializing it with this function... *
  300. * *
  301. * INPUT: *
  302. * p - position of the coordinate system *
  303. * t - target of the coordinate system *
  304. * roll - roll angle (in radians) *
  305. * *
  306. * OUTPUT: *
  307. * *
  308. * WARNINGS: *
  309. * This function is written assuming the convention that the "ground" is the X-Y plane and *
  310. * Z is altitude. *
  311. * *
  312. * HISTORY: *
  313. *=============================================================================================*/
  314. void Matrix3D::Look_At(const Vector3 &p,const Vector3 &t,float roll)
  315. {
  316. float dx,dy,dz; //vector from p to t
  317. float sinp,cosp; //sine and cosine of the pitch ("up-down" tilt about x)
  318. float siny,cosy; //sine and cosine of the yaw ("left-right"tilt about z)
  319. dx = (t[0] - p[0]);
  320. dy = (t[1] - p[1]);
  321. dz = (t[2] - p[2]);
  322. float rad2=dx*dx + dy*dy;
  323. float len=(float)WWMath::Sqrt(rad2);
  324. if (rad2!=0.0f) {
  325. // float inv_len = WWMath::Inv_Sqrt(rad2);
  326. float inv_len=1.0f/len;
  327. siny = dy*inv_len;
  328. cosy = dx*inv_len;
  329. } else {
  330. siny = 0.0f;
  331. cosy = 1.0f;
  332. }
  333. rad2+=dz*dz;
  334. if (rad2!=0.0f) {
  335. float inv_len2 = (float)WWMath::Inv_Sqrt(rad2);
  336. sinp = dz*inv_len2;
  337. cosp = len*inv_len2;
  338. } else {
  339. sinp = 0.0f;
  340. cosp = 1.0f;
  341. }
  342. // init the matrix with position p and -z pointing down +x and +y up
  343. Row[0].X = 0.0f; Row[0].Y = 0.0f; Row[0].Z = -1.0f;
  344. Row[1].X = -1.0f; Row[1].Y = 0.0f; Row[1].Z = 0.0f;
  345. Row[2].X = 0.0f; Row[2].Y = 1.0f; Row[2].Z = 0.0f;
  346. Row[0].W = p.X;
  347. Row[1].W = p.Y;
  348. Row[2].W = p.Z;
  349. // Yaw rotation to make the matrix look at the projection of the target
  350. // into the x-y plane
  351. Rotate_Y(siny,cosy);
  352. // rotate about local x axis to pitch up to the targets position
  353. Rotate_X(sinp,cosp);
  354. // roll about the local z axis (negate since we look down -z)
  355. Rotate_Z(-roll);
  356. }
  357. /***********************************************************************************************
  358. * M3DC::Obj_Look_At -- Commando Object "look at" transformation. *
  359. * *
  360. * Builds a transformation matrix which positions the origin at p, *
  361. * points the positive X axis towards a target t, and rolls about the X axis *
  362. * by the angle specified by roll. *
  363. * *
  364. * The object convention used by Commando and G is Forward = +X, Left = +Y, Up = +Z. The *
  365. * world is basically the x-y plane with z as altitude and +x is the default "forward". *
  366. * *
  367. * INPUT: *
  368. * p - position of the coordinate system *
  369. * t - target of the coordinate system *
  370. * roll - roll angle (in radians) *
  371. * *
  372. * OUTPUT: *
  373. * *
  374. * WARNINGS: *
  375. * *
  376. * HISTORY: *
  377. *=============================================================================================*/
  378. void Matrix3D::Obj_Look_At(const Vector3 &p,const Vector3 &t,float roll)
  379. {
  380. float dx,dy,dz; //vector from p to t
  381. float len1,len2;
  382. float sinp,cosp; //sine and cosine of the pitch ("up-down" tilt about y)
  383. float siny,cosy; //sine and cosine of the yaw ("left-right"tilt about z)
  384. dx = (t[0] - p[0]);
  385. dy = (t[1] - p[1]);
  386. dz = (t[2] - p[2]);
  387. len1 = (float)sqrt(dx*dx + dy*dy + dz*dz);
  388. len2 = (float)sqrt(dx*dx + dy*dy);
  389. if (len1 != 0.0f) {
  390. sinp = dz/len1;
  391. cosp = len2/len1;
  392. } else {
  393. sinp = 0.0f;
  394. cosp = 1.0f;
  395. }
  396. if (len2 != 0.0f) {
  397. siny = dy/len2;
  398. cosy = dx/len2;
  399. } else {
  400. siny = 0.0f;
  401. cosy = 1.0f;
  402. }
  403. Make_Identity();
  404. Translate(p);
  405. // Yaw rotation to projection of target in x-y plane
  406. Rotate_Z(siny,cosy);
  407. // Pitch rotation
  408. Rotate_Y(-sinp,cosp);
  409. // Roll rotation
  410. Rotate_X(roll);
  411. }
  412. /***********************************************************************************************
  413. * Matrix3D::Get_Inverse -- calculate the inverse of this matrix *
  414. * *
  415. * INPUT: *
  416. * *
  417. * OUTPUT: *
  418. * *
  419. * WARNINGS: *
  420. * *
  421. * HISTORY: *
  422. * 8/7/98 GTH : Created. *
  423. *=============================================================================================*/
  424. void Matrix3D::Get_Inverse(Matrix3D & inv) const
  425. {
  426. // TODO: Implement the general purpose inverse function here (once we need it :-)
  427. Get_Orthogonal_Inverse(inv);
  428. }
  429. /***********************************************************************************************
  430. * Matrix3D::Get_Orthogonal_Inverse -- Returns the inverse of the matrix *
  431. * *
  432. * NOTE!!! This only works if the matrix is really ORTHOGONAL!!! *
  433. * *
  434. ***********************************************************************************************
  435. * Inverting an orthogonal Matrix3D *
  436. * *
  437. * M is the original transform, *
  438. * R is rotation submatrix, *
  439. * T is translation vector in M. *
  440. * *
  441. * To build MINV *
  442. * *
  443. * R' = transpose of R (inverse of orthogonal 3x3 matrix is transpose) *
  444. * T' = -R'T *
  445. * *
  446. * Build MINV with R'and T' *
  447. * MINV is the inverse of M *
  448. * *
  449. ***********************************************************************************************
  450. * INPUT: *
  451. * *
  452. * OUTPUT: *
  453. * *
  454. * WARNINGS: *
  455. * *
  456. * HISTORY: *
  457. *=============================================================================================*/
  458. void Matrix3D::Get_Orthogonal_Inverse(Matrix3D & inv) const
  459. {
  460. // Transposing the rotation submatrix
  461. inv.Row[0][0] = Row[0][0];
  462. inv.Row[0][1] = Row[1][0];
  463. inv.Row[0][2] = Row[2][0];
  464. inv.Row[1][0] = Row[0][1];
  465. inv.Row[1][1] = Row[1][1];
  466. inv.Row[1][2] = Row[2][1];
  467. inv.Row[2][0] = Row[0][2];
  468. inv.Row[2][1] = Row[1][2];
  469. inv.Row[2][2] = Row[2][2];
  470. // Now, calculate translation portion of matrix:
  471. // T' = -R'T
  472. Vector3 trans = Get_Translation();
  473. trans = inv.Rotate_Vector(trans);
  474. trans = -trans;
  475. inv.Row[0][3] = trans[0];
  476. inv.Row[1][3] = trans[1];
  477. inv.Row[2][3] = trans[2];
  478. }
  479. /***********************************************************************************************
  480. * Copy_3x3_Matrix(float *matrix) -- Copies a 3x3 (float[9]) matrix into this matrix *
  481. * *
  482. * INPUT: *
  483. * *
  484. * OUTPUT: *
  485. * *
  486. * WARNINGS: *
  487. * *
  488. * HISTORY: *
  489. * 1/16/98 EHC : Created. *
  490. *=============================================================================================*/
  491. void Matrix3D::Copy_3x3_Matrix(float matrix[3][3])
  492. {
  493. Row[0][0] = matrix[0][0];
  494. Row[0][1] = matrix[0][1];
  495. Row[0][2] = matrix[0][2];
  496. Row[0][3] = 0;
  497. Row[1][0] = matrix[1][0];
  498. Row[1][1] = matrix[1][1];
  499. Row[1][2] = matrix[1][2];
  500. Row[1][3] = 0;
  501. Row[2][0] = matrix[2][0];
  502. Row[2][1] = matrix[2][1];
  503. Row[2][2] = matrix[2][2];
  504. Row[2][3] = 0;
  505. }
  506. /***********************************************************************************************
  507. * Matrix3D::Multiply -- matrix multiplication without temporaries. *
  508. * *
  509. * INPUT: *
  510. * *
  511. * OUTPUT: *
  512. * *
  513. * WARNINGS: *
  514. * *
  515. * HISTORY: *
  516. * 4/22/98 GTH : Created. *
  517. *=============================================================================================*/
  518. //void print_matrix(const Matrix3D & m);
  519. void Matrix3D::Multiply(const Matrix3D & A,const Matrix3D & B,Matrix3D * set_res)
  520. {
  521. assert(set_res != NULL);
  522. Matrix3D tmp;
  523. Matrix3D * Aptr;
  524. float tmp1,tmp2,tmp3;
  525. // Check for aliased parameters, copy the 'A' matrix into a temporary if the
  526. // result is going into 'A'. (in this case, this function is no better than
  527. // the overloaded C++ operator...)
  528. if (set_res == &A) {
  529. tmp = A;
  530. Aptr = &tmp;
  531. } else {
  532. Aptr = (Matrix3D *)&A;
  533. }
  534. tmp1 = B[0][0];
  535. tmp2 = B[1][0];
  536. tmp3 = B[2][0];
  537. (*set_res)[0][0] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  538. (*set_res)[1][0] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3;
  539. (*set_res)[2][0] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3;
  540. tmp1 = B[0][1];
  541. tmp2 = B[1][1];
  542. tmp3 = B[2][1];
  543. (*set_res)[0][1] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  544. (*set_res)[1][1] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3;
  545. (*set_res)[2][1] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3;
  546. tmp1 = B[0][2];
  547. tmp2 = B[1][2];
  548. tmp3 = B[2][2];
  549. (*set_res)[0][2] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  550. (*set_res)[1][2] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3;
  551. (*set_res)[2][2] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3;
  552. tmp1 = B[0][3];
  553. tmp2 = B[1][3];
  554. tmp3 = B[2][3];
  555. (*set_res)[0][3] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3 + (*Aptr)[0][3];
  556. (*set_res)[1][3] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3 + (*Aptr)[1][3];
  557. (*set_res)[2][3] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3 + (*Aptr)[2][3];
  558. }
  559. #if 0
  560. void Matrix3D::Multiply(const Matrix3D & A,const Matrix3D & B,Matrix3D * set_res)
  561. {
  562. assert(set_res != NULL);
  563. float tmp[12];
  564. // Check for aliased parameters, copy the 'A' matrix into a temporary if the
  565. // result is going into 'A'. (in this case, this function is no better than
  566. // the overloaded C++ operator...)
  567. /* if (set_res == &A)
  568. {
  569. Matrix3D tmp;
  570. Matrix3D * Aptr;
  571. float tmp1,tmp2,tmp3;
  572. tmp = A;
  573. Aptr = &tmp;
  574. tmp1 = B[0][0];
  575. tmp2 = B[1][0];
  576. tmp3 = B[2][0];
  577. (*set_res)[0][0] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  578. (*set_res)[1][0] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3;
  579. (*set_res)[2][0] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3;
  580. tmp1 = B[0][1];
  581. tmp2 = B[1][1];
  582. tmp3 = B[2][1];
  583. (*set_res)[0][1] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  584. (*set_res)[1][1] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3;
  585. (*set_res)[2][1] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3;
  586. tmp1 = B[0][2];
  587. tmp2 = B[1][2];
  588. tmp3 = B[2][2];
  589. (*set_res)[0][2] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  590. (*set_res)[1][2] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3;
  591. (*set_res)[2][2] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3;
  592. tmp1 = B[0][3];
  593. tmp2 = B[1][3];
  594. tmp3 = B[2][3];
  595. (*set_res)[0][3] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3 + (*Aptr)[0][3];
  596. (*set_res)[1][3] = (*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3 + (*Aptr)[1][3];
  597. (*set_res)[2][3] = (*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3 + (*Aptr)[2][3];
  598. return;
  599. }
  600. */
  601. __asm {
  602. mov ecx,B
  603. fld dword ptr [ecx+32] // B[2][0]
  604. mov edx,A
  605. lea ebx,tmp
  606. mov eax,set_res
  607. cmp eax,edx
  608. jne not_equal
  609. mov eax,ebx
  610. not_equal:
  611. fld dword ptr [ecx+16] // B[1][0]
  612. fld dword ptr [ecx] // B[0][0]
  613. // tmp1 = B[0][0];
  614. // tmp2 = B[1][0];
  615. // tmp3 = B[2][0];
  616. // (*set_res)[0][0] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  617. fld dword ptr [edx+8] // A[0][2]
  618. fmul st(0),st(3) // A[0][2] * B[2][0]
  619. fld dword ptr [edx+4] // A[0][1]
  620. fmul st(0),st(3) // A[0][1] * B[1][0]
  621. fld dword ptr [edx] // A[0][0]
  622. fmul st(0),st(3) // A[0][2] * B[0][0]
  623. faddp st(1),st(0)
  624. faddp st(1),st(0)
  625. fstp dword ptr [eax] // res[0][0]
  626. // (*set_res)[1][0] = A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3;
  627. fld dword ptr [edx+24] // A[1][2]
  628. fmul st(0),st(3) // A[1][2] * B[2][0]
  629. fld dword ptr [edx+20] // A[1][1]
  630. fmul st(0),st(3) // A[1][1] * B[1][0]
  631. fld dword ptr [edx+16] // A[1][0]
  632. fmul st(0),st(3) // A[1][0] * B[0][0]
  633. faddp st(1),st(0)
  634. faddp st(1),st(0)
  635. fstp dword ptr [eax+16] // res[1][0]
  636. // (*set_res)[2][0] = A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3;
  637. fld dword ptr [edx+40] // A[2][2]
  638. fmul st(0),st(3) // A[2][2] * B[2][0]
  639. fld dword ptr [edx+36] // A[2][1]
  640. fmul st(0),st(3) // A[2][1] * B[1][0]
  641. fld dword ptr [edx+32] // A[2][0]
  642. fmul st(0),st(3) // A[2][0] * B[0][0]
  643. faddp st(1),st(0)
  644. faddp st(1),st(0)
  645. fstp dword ptr [eax+32] // res[2][0]
  646. // tmp1 = B[0][1];
  647. // tmp2 = B[1][1];
  648. // tmp3 = B[2][1];
  649. fstp st(0)
  650. fstp st(0)
  651. fstp st(0)
  652. fld dword ptr [ecx+36] // B[2][1]
  653. fld dword ptr [ecx+20] // B[1][1]
  654. fld dword ptr [ecx+4] // B[0][1]
  655. // (*set_res)[0][1] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  656. fld dword ptr [edx+8] // A[0][2]
  657. fmul st(0),st(3) // A[0][2] * B[2][1]
  658. fld dword ptr [edx+4] // A[0][1]
  659. fmul st(0),st(3) // A[0][1] * B[1][1]
  660. fld dword ptr [edx] // A[0][0]
  661. fmul st(0),st(3) // A[0][2] * B[0][1]
  662. faddp st(1),st(0)
  663. faddp st(1),st(0)
  664. fstp dword ptr [eax+4] // res[0][1]
  665. // (*set_res)[1][1] = A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3;
  666. fld dword ptr [edx+24] // A[1][2]
  667. fmul st(0),st(3) // A[1][2] * B[2][1]
  668. fld dword ptr [edx+20] // A[1][1]
  669. fmul st(0),st(3) // A[1][1] * B[1][1]
  670. fld dword ptr [edx+16] // A[1][0]
  671. fmul st(0),st(3) // A[1][0] * B[0][1]
  672. faddp st(1),st(0)
  673. faddp st(1),st(0)
  674. fstp dword ptr [eax+20] // res[1][1]
  675. // (*set_res)[2][1] = A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3;
  676. fld dword ptr [edx+40] // A[2][2]
  677. fmul st(0),st(3) // A[2][2] * B[2][1]
  678. fld dword ptr [edx+36] // A[2][1]
  679. fmul st(0),st(3) // A[2][1] * B[1][1]
  680. fld dword ptr [edx+32] // A[2][0]
  681. fmul st(0),st(3) // A[2][0] * B[0][1]
  682. faddp st(1),st(0)
  683. faddp st(1),st(0)
  684. fstp dword ptr [eax+36] // res[2][1]
  685. // tmp1 = B[0][2];
  686. // tmp2 = B[1][2];
  687. // tmp3 = B[2][2];
  688. fstp st(0)
  689. fstp st(0)
  690. fstp st(0)
  691. fld dword ptr [ecx+40] // B[2][2]
  692. fld dword ptr [ecx+24] // B[1][2]
  693. fld dword ptr [ecx+8] // B[0][2]
  694. // (*set_res)[0][2] = (*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3;
  695. fld dword ptr [edx+8] // A[0][2]
  696. fmul st(0),st(3) // A[0][2] * B[2][2]
  697. fld dword ptr [edx+4] // A[0][1]
  698. fmul st(0),st(3) // A[0][1] * B[1][2]
  699. fld dword ptr [edx] // A[0][0]
  700. fmul st(0),st(3) // A[0][2] * B[0][2]
  701. faddp st(1),st(0)
  702. faddp st(1),st(0)
  703. fstp dword ptr [eax+8] // res[0][2]
  704. // (*set_res)[1][2] = A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3;
  705. fld dword ptr [edx+24] // A[1][2]
  706. fmul st(0),st(3) // A[1][2] * B[2][2]
  707. fld dword ptr [edx+20] // A[1][1]
  708. fmul st(0),st(3) // A[1][1] * B[1][2]
  709. fld dword ptr [edx+16] // A[1][0]
  710. fmul st(0),st(3) // A[1][0] * B[0][2]
  711. faddp st(1),st(0)
  712. faddp st(1),st(0)
  713. fstp dword ptr [eax+24] // res[1][2]
  714. // (*set_res)[2][2] = A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3;
  715. fld dword ptr [edx+40] // A[2][2]
  716. fmul st(0),st(3) // A[2][2] * B[2][2]
  717. fld dword ptr [edx+36] // A[2][1]
  718. fmul st(0),st(3) // A[2][1] * B[1][2]
  719. fld dword ptr [edx+32] // A[2][0]
  720. fmul st(0),st(3) // A[2][0] * B[0][2]
  721. faddp st(1),st(0)
  722. faddp st(1),st(0)
  723. fstp dword ptr [eax+40] // res[2][2]
  724. // -------------------------------
  725. // tmp1 = B[0][3];
  726. // tmp2 = B[1][3];
  727. // tmp3 = B[2][3];
  728. fstp st(0)
  729. fstp st(0)
  730. fstp st(0)
  731. fld dword ptr [ecx+44] // B[2][3]
  732. fld dword ptr [ecx+28] // B[1][3]
  733. fld dword ptr [ecx+12] // B[0][3]
  734. // (*set_res)[0][3] = A[0][0]*tmp1 + A[0][1]*tmp2 + A[0][2]*tmp3 + A[0][3];
  735. fld dword ptr [edx+8] // A[0][2]
  736. fmul st(0),st(3) // A[0][2] * B[2][3]
  737. fld dword ptr [edx+4] // A[0][1]
  738. fmul st(0),st(3) // A[0][1] * B[1][3]
  739. fld dword ptr [edx] // A[0][0]
  740. fmul st(0),st(3) // A[0][2] * B[0][3]
  741. faddp st(1),st(0)
  742. faddp st(1),st(0)
  743. fadd dword ptr [edx+12] // + A[0][3]
  744. fstp dword ptr [eax+12] // res[0][3]
  745. // (*set_res)[1][3] = A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3 + A[1][3];
  746. fld dword ptr [edx+24] // A[1][2]
  747. fmul st(0),st(3) // A[1][2] * B[2][3]
  748. fld dword ptr [edx+20] // A[1][1]
  749. fmul st(0),st(3) // A[1][1] * B[1][3]
  750. fld dword ptr [edx+16] // A[1][0]
  751. fmul st(0),st(3) // A[1][0] * B[0][3]
  752. faddp st(1),st(0)
  753. faddp st(1),st(0)
  754. fadd dword ptr [edx+28] // + A[1][3]
  755. fstp dword ptr [eax+28] // res[1][3]
  756. // (*set_res)[2][3] = A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3 + A[2][3];
  757. fld dword ptr [edx+40] // A[2][2]
  758. fmul st(0),st(3) // A[2][2] * B[2][3]
  759. fld dword ptr [edx+36] // A[2][1]
  760. fmul st(0),st(3) // A[2][1] * B[1][3]
  761. fld dword ptr [edx+32] // A[2][0]
  762. fmul st(0),st(3) // A[2][0] * B[0][3]
  763. faddp st(1),st(0)
  764. faddp st(1),st(0)
  765. fadd dword ptr [edx+44] // + A[2][3]
  766. fstp dword ptr [eax+44] // res[2][3]
  767. fstp st(0)
  768. fstp st(0)
  769. fstp st(0)
  770. cmp eax,ebx
  771. jne not_equal2
  772. mov ecx,12 // copy 12 dwords
  773. mov esi,eax // set source to tmp
  774. mov edi,set_res // set destination to set_res
  775. rep movsd // copy
  776. not_equal2:
  777. }
  778. /*
  779. WWDEBUG_SAY(("{%2.2f, %2.2f, %2.2f, %2.2f}, {%2.2f, %2.2f, %2.2f, %2.2f}, {%2.2f, %2.2f, %2.2f, %2.2f}\n"
  780. ,res[0][0],res[0][1],res[0][2],res[0][3]
  781. ,res[1][0],res[1][1],res[1][2],res[1][3]
  782. ,res[2][0],res[2][1],res[2][2],res[2][3]));
  783. WWDEBUG_SAY(("{%2.2f, %2.2f, %2.2f, %2.2f}, {%2.2f, %2.2f, %2.2f, %2.2f}, {%2.2f, %2.2f, %2.2f, %2.2f}\n"
  784. ,res2[0][0],res2[0][1],res2[0][2],res2[0][3]
  785. ,res2[1][0],res2[1][1],res2[1][2],res2[1][3]
  786. ,res2[2][0],res2[2][1],res2[2][2],res2[2][3]));
  787. // res[2][3]=res2[2][3];
  788. // res=res2;
  789. */
  790. /* for (int y=0;y<3;++y) {
  791. for (int x=0;x<4;++x) {
  792. if (fabs(res2[y][x]-res[y][x])>0.001f) {
  793. WWDEBUG_SAY(("x: %d, y: %d, %f != %f\n",x,y,res2[y][x],res[y][x]));
  794. __asm nop
  795. }
  796. }
  797. }
  798. */
  799. /* WWASSERT(res2[0][0]==res[0][0]);
  800. WWASSERT(res2[0][1]==res[0][1]);
  801. WWASSERT(res2[0][2]==res[0][2]);
  802. WWASSERT(res2[0][3]==res[0][3]);
  803. WWASSERT(res2[1][0]==res[1][0]);
  804. WWASSERT(res2[1][1]==res[1][1]);
  805. WWASSERT(res2[1][2]==res[1][2]);
  806. WWASSERT(res2[1][3]==res[1][3]);
  807. WWASSERT(res2[2][0]==res[2][0]);
  808. WWASSERT(res2[2][1]==res[2][1]);
  809. WWASSERT(res2[2][2]==res[2][2]);
  810. WWASSERT(res2[2][3]==res[2][3]);
  811. */
  812. }
  813. #endif
  814. /***********************************************************************************************
  815. * Matrix3D::Transform_Min_Max_AABox -- compute transformed axis-aligned box *
  816. * *
  817. * INPUT: *
  818. * *
  819. * OUTPUT: *
  820. * *
  821. * WARNINGS: *
  822. * *
  823. * HISTORY: *
  824. * 7/17/98 GTH : Created. *
  825. *=============================================================================================*/
  826. void Matrix3D::Transform_Min_Max_AABox
  827. (
  828. const Vector3 & min,
  829. const Vector3 & max,
  830. Vector3 * set_min,
  831. Vector3 * set_max
  832. ) const
  833. {
  834. WWASSERT(set_min != &min);
  835. WWASSERT(set_max != &max);
  836. float tmp0,tmp1;
  837. // init the min and max to the translation of the transform
  838. set_min->X = set_max->X = Row[0][3];
  839. set_min->Y = set_max->Y = Row[1][3];
  840. set_min->Z = set_max->Z = Row[2][3];
  841. // now push them both out by the projections of the original intervals
  842. for (int i=0; i<3; i++) {
  843. for (int j=0; j<3; j++) {
  844. tmp0 = Row[i][j] * min[j];
  845. tmp1 = Row[i][j] * max[j];
  846. if (tmp0 < tmp1) {
  847. (*set_min)[i] += tmp0;
  848. (*set_max)[i] += tmp1;
  849. } else {
  850. (*set_min)[i] += tmp1;
  851. (*set_max)[i] += tmp0;
  852. }
  853. }
  854. }
  855. }
  856. /***********************************************************************************************
  857. * Matrix3D::Transform_Center_Extent_AABox -- compute transformed axis-aligned box *
  858. * *
  859. * INPUT: *
  860. * *
  861. * OUTPUT: *
  862. * *
  863. * WARNINGS: *
  864. * *
  865. * HISTORY: *
  866. * 7/17/98 GTH : Created. *
  867. *=============================================================================================*/
  868. void Matrix3D::Transform_Center_Extent_AABox
  869. (
  870. const Vector3 & center,
  871. const Vector3 & extent,
  872. Vector3 * set_center,
  873. Vector3 * set_extent
  874. ) const
  875. {
  876. WWASSERT(set_center != &center);
  877. WWASSERT(set_extent != &extent);
  878. // push each extent out to the projections of the original extents
  879. for (int i=0; i<3; i++) {
  880. // start the center out at the translation portion of the matrix
  881. // and the extent at zero
  882. (*set_center)[i] = Row[i][3];
  883. (*set_extent)[i] = 0.0f;
  884. for (int j=0; j<3; j++) {
  885. (*set_center)[i] += Row[i][j] * center[j];
  886. (*set_extent)[i] += WWMath::Fabs(Row[i][j] * extent[j]);
  887. }
  888. }
  889. }
  890. /***********************************************************************************************
  891. * Matrix3D::Is_Orthogonal -- checks whether this matrix is orthogonal *
  892. * *
  893. * INPUT: *
  894. * *
  895. * OUTPUT: *
  896. * *
  897. * WARNINGS: *
  898. * *
  899. * HISTORY: *
  900. * 9/16/98 GTH : Created. *
  901. *=============================================================================================*/
  902. int Matrix3D::Is_Orthogonal(void) const
  903. {
  904. Vector3 x(Row[0].X,Row[0].Y,Row[0].Z);
  905. Vector3 y(Row[1].X,Row[1].Y,Row[1].Z);
  906. Vector3 z(Row[2].X,Row[2].Y,Row[2].Z);
  907. if (Vector3::Dot_Product(x,y) > WWMATH_EPSILON) return 0;
  908. if (Vector3::Dot_Product(y,z) > WWMATH_EPSILON) return 0;
  909. if (Vector3::Dot_Product(z,x) > WWMATH_EPSILON) return 0;
  910. if (WWMath::Fabs(x.Length2() - 1.0f) > WWMATH_EPSILON) return 0;
  911. if (WWMath::Fabs(y.Length2() - 1.0f) > WWMATH_EPSILON) return 0;
  912. if (WWMath::Fabs(z.Length2() - 1.0f) > WWMATH_EPSILON) return 0;
  913. return 1;
  914. }
  915. /***********************************************************************************************
  916. * Matrix3D::Re_Orthogonalize -- makes this matrix orthogonal. *
  917. * *
  918. * INPUT: *
  919. * *
  920. * OUTPUT: *
  921. * *
  922. * WARNINGS: *
  923. * This function is rather expensive, should only be used if you *know* numerical error is *
  924. * killing you. *
  925. * *
  926. * HISTORY: *
  927. * 9/16/98 GTH : Created. *
  928. *=============================================================================================*/
  929. void Matrix3D::Re_Orthogonalize(void)
  930. {
  931. Vector3 x(Row[0][0],Row[0][1],Row[0][2]);
  932. Vector3 y(Row[1][0],Row[1][1],Row[1][2]);
  933. Vector3 z;
  934. Vector3::Cross_Product(x,y,&z);
  935. Vector3::Cross_Product(z,x,&y);
  936. float len = x.Length();
  937. if (len < WWMATH_EPSILON) {
  938. Make_Identity();
  939. return;
  940. } else {
  941. x *= 1.0f/len;
  942. }
  943. len = y.Length();
  944. if (len < WWMATH_EPSILON) {
  945. Make_Identity();
  946. return;
  947. } else {
  948. y *= 1.0f/len;
  949. }
  950. len = z.Length();
  951. if (len < WWMATH_EPSILON) {
  952. Make_Identity();
  953. return;
  954. } else {
  955. z *= 1.0f/len;
  956. }
  957. Row[0][0] = x.X;
  958. Row[0][1] = x.Y;
  959. Row[0][2] = x.Z;
  960. Row[1][0] = y.X;
  961. Row[1][1] = y.Y;
  962. Row[1][2] = y.Z;
  963. Row[2][0] = z.X;
  964. Row[2][1] = z.Y;
  965. Row[2][2] = z.Z;
  966. }
  967. /***********************************************************************************************
  968. * Lerp - linearly interpolate matrices (orientation is slerped) *
  969. * *
  970. * INPUT: *
  971. * *
  972. * OUTPUT: *
  973. * *
  974. * WARNINGS: *
  975. * *
  976. * HISTORY: *
  977. * 10/05/1998 NH : Created. *
  978. *=============================================================================================*/
  979. Matrix3D Lerp(const Matrix3D &A, const Matrix3D &B, float factor)
  980. {
  981. assert(factor >= 0.0f);
  982. assert(factor <= 1.0f);
  983. // Lerp position
  984. Vector3 pos = Lerp(A.Get_Translation(), B.Get_Translation(), factor);
  985. Quaternion rot;
  986. Slerp(rot,Build_Quaternion(A), Build_Quaternion(B), factor);
  987. return Matrix3D(rot, pos);
  988. }
  989. /***********************************************************************************************
  990. * Matrix3D::Solve_Linear_System -- 3x3 Gauss-Jordan elimination *
  991. * *
  992. * The result of this function will be that the 3x3 sub-matrix is inverted and the *
  993. * final column will contain the result. False is returned if the system cannot be solved. *
  994. * *
  995. * INPUT: *
  996. * *
  997. * OUTPUT: *
  998. * *
  999. * WARNINGS: *
  1000. * *
  1001. * HISTORY: *
  1002. * 2/18/2001 gth : Created. *
  1003. *=============================================================================================*/
  1004. bool Matrix3D::Solve_Linear_System(Matrix3D & system)
  1005. {
  1006. /*
  1007. ** Gauss-Jordan Elimination
  1008. ** We repeatedly replace rows in the matrix with a linear combination of itself and
  1009. ** another row in the system in order to reduce the matrix to the identity matrix.
  1010. ** TODO: optimize away all unnecessary math operations!
  1011. */
  1012. if (system[0][0] == 0.0f) return false;
  1013. system[0] *= 1.0f / system[0][0]; // (0,0) now equals 1.0 (row,col)
  1014. system[1] -= system[1][0] * system[0]; // (1,0) now equals 0.0
  1015. system[2] -= system[2][0] * system[0]; // (2,0) now equals 0.0
  1016. if (system[1][1] == 0.0f) return false;
  1017. system[1] *= 1.0f / system[1][1]; // (1,1) now equals 1.0
  1018. system[2] -= system[2][1] * system[1]; // (2,1) now equals 0.0
  1019. if (system[2][2] == 0.0f) return false;
  1020. system[2] *= 1.0f / system[2][2]; // (2,2) now equals 1.0, and we already have one answer
  1021. system[1] -= system[1][2] * system[2]; // (1,2) now equals 0.0, and we have another answer
  1022. system[0] -= system[0][2] * system[2]; // (0,2) now equals 0.0
  1023. system[0] -= system[0][1] * system[1]; // (0,1) now equals 0.0, and we are done!
  1024. return true;
  1025. }