matrix3d.cpp 54 KB

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