matrix3d.h 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /*
  2. ** Command & Conquer Generals(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.h 55 5/11/01 7:11p Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Voxel Technology *
  24. * *
  25. * File Name : MATRIX3D.H *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 02/24/97 *
  30. * *
  31. * Last Update : February 24, 1997 [GH] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Matrix3D::Matrix3D -- Constructors for Matrix3D *
  36. * Matrix3D::Set -- init a Matrix3D from an arrray of 12 floats *
  37. * Matrix3D::Set -- Init a Matrix3D from 12 individual floats *
  38. * Matrix3D::Set -- Init a matrix from 3 axis vectors and a position *
  39. * Matrix3D::Set -- init a matrix to be a rotation about the given axis *
  40. * Matrix3D::Set -- init a matrix to be a rotation about the given axis *
  41. * Matrix3D::Set -- Init a matrix to be a pure translation *
  42. * Matrix3D::Make_Identity -- Initializes the matrix to be the identity matrix *
  43. * Matrix3D::Translate -- Post-Multiplies by a Translation Matrix *
  44. * Matrix3D::Translate -- Post-Multiplies the matrix by a translation matrix *
  45. * M3DC::Translate_X -- Post-Multiplies the matrix by a translation matrix with X only *
  46. * M3DC::Translate_Y -- Post-Multiplies the matrix by a translation matrix with Y only *
  47. * M3DC::Translate_Z -- Post-Multiplies the matrix by a translation matrix with Z only *
  48. * Matrix3D::Rotate_X -- Post-Multiplies the matrix by a rotation about the X axis *
  49. * Matrix3D::Rotate_X -- Post-Multiplies the matrix by a rotation about the X axis *
  50. * Matrix3D::Rotate_Y -- Post-multiplies the matrix by a rotation about the Y axis *
  51. * Matrix3D::Rotate_Y -- Post-Multiplies the matrix by a rotation about Y *
  52. * Matrix3D::Rotate_Z -- Post-multiplies the matrix by a rotation about Z *
  53. * Matrix3D::Rotate_Z -- Post-multiplies the matrix by a rotation about Z *
  54. * Matrix3D::Scale -- Scales Matrix !!! WARNING DO NOT USE, INV FUNCTION WILL FAIL *
  55. * Matrix3D::Scale -- Scales Matrix !!! WARNING DO NOT USE, INV FUNCTION WILL FAIL *
  56. * Matrix3D::Scale -- Scales Matrix !!! WARNING DO NOT USE, INV FUNCTION WILL FAIL *
  57. * M3DC::Pre_Rotate_X -- Pre-multiplies the matrix by a rotation about X *
  58. * M3DC::Pre_Rotate_Y -- Pre-multiplies the matrix by a rotation about Y *
  59. * M3DC::Pre_Rotate_Z -- Pre-multiplies the matrix by a rotation about Z *
  60. * M3DC::Pre_Rotate_X -- Pre-multiplies the matrix by a rotation about X *
  61. * M3DC::Pre_Rotate_Y -- Pre-multiplies the matrix by a rotation about Y *
  62. * M3DC::Pre_Rotate_Z -- Pre-multiplies the matrix by a rotation about Z *
  63. * M3DC::In_Place_Pre_Rotate_X -- Pre-multiplies rotation part of matrix by rotation about X *
  64. * M3DC::In_Place_Pre_Rotate_Y -- Pre-multiplies rotation part of matrix by rotation about Y *
  65. * M3DC::In_Place_Pre_Rotate_Z -- Pre-multiplies rotation part of matrix by rotation about Z *
  66. * M3DC::In_Place_Pre_Rotate_X -- Pre-multiplies rotation part of matrix by rotation about X *
  67. * M3DC::In_Place_Pre_Rotate_Y -- Pre-multiplies rotation part of matrix by rotation about Y *
  68. * M3DC::In_Place_Pre_Rotate_Z -- Pre-multiplies rotation part of matrix by rotation about Z *
  69. * operator * -- Matrix multiplication *
  70. * operator * -- Matrix - vector multiplication *
  71. * operator == -- Matrix equality operator *
  72. * operator != -- Matrix inequality operator *
  73. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  74. #define NO_ALLOW_TEMPORARIES
  75. #if defined(_MSC_VER)
  76. #pragma once
  77. #endif
  78. #ifndef MATRIX3D_H
  79. #define MATRIX3D_H
  80. #ifdef _UNIX
  81. #include "osdep.h"
  82. #endif
  83. #include "always.h"
  84. #include <assert.h>
  85. #include "vector2.h"
  86. #include "vector3.h"
  87. #include "vector4.h"
  88. #ifdef _UNIX
  89. #include "osdep.h"
  90. #endif
  91. class Matrix3;
  92. class Matrix4;
  93. class Quaternion;
  94. /*******************************************************************************
  95. Matrix3D
  96. Three important notes:
  97. - I use *column-vectors*
  98. - I use a *right-handed* coordinate system
  99. - These matrices are *orthogonal*
  100. 3D Transformation matrices. This class is really a 4x4 homogeneous
  101. matrix where the last row is assumed to always be 0 0 0 1. However,
  102. since I don't store the last row, you cant do some things that you can
  103. do with a real 4x4 homogeneous matrix.
  104. I use column-vectors so normally transformations are post-multiplied
  105. and camera transformations should be pre-multiplied. The methods of
  106. this class called Translate, Rotate_X, etc. all perform post-multiplication
  107. with the current matix. These methods (Translate, Rotate_X, etc) also
  108. have been hand-coded to only perform the necessary arithmetic. The
  109. * operator can be used for general purpose matrix multiplication or to
  110. transform a vector by a matrix.
  111. Some operations in this class assume that the matrix is orthogonal.
  112. *********************************************************************************/
  113. class Matrix3D
  114. {
  115. public:
  116. // Constructors
  117. WWINLINE Matrix3D(void) {}
  118. WWINLINE explicit Matrix3D(bool init) { if (init) Make_Identity(); }
  119. WWINLINE explicit Matrix3D(float m[12]);
  120. WWINLINE explicit Matrix3D(
  121. float m11,float m12,float m13,float m14,
  122. float m21,float m22,float m23,float m24,
  123. float m31,float m32,float m33,float m34
  124. );
  125. WWINLINE explicit Matrix3D(
  126. const Vector3 &x, // x-axis unit vector
  127. const Vector3 &y, // y-axis unit vector
  128. const Vector3 &z, // z-axis unit vector
  129. const Vector3 &pos // position
  130. );
  131. WWINLINE explicit Matrix3D(
  132. const Vector3 &axis,
  133. float angle
  134. );
  135. WWINLINE explicit Matrix3D(
  136. const Vector3 &axis,
  137. float sine,
  138. float cosine
  139. );
  140. WWINLINE explicit Matrix3D(
  141. const Matrix3 & rotation,
  142. const Vector3 & position
  143. );
  144. WWINLINE explicit Matrix3D(
  145. const Quaternion & rotation,
  146. const Vector3 & position
  147. );
  148. // Creates identity rotation w. given position.
  149. WWINLINE explicit Matrix3D(const Vector3 & position);
  150. // Copy constructor
  151. WWINLINE Matrix3D(const Matrix3D & m);
  152. // Assignment operator
  153. WWINLINE Matrix3D & operator = (const Matrix3D & m);
  154. // Init functions
  155. WWINLINE void Set(float m[12]);
  156. WWINLINE void Set(
  157. float m11,float m12,float m13,float m14,
  158. float m21,float m22,float m23,float m24,
  159. float m31,float m32,float m33,float m34
  160. );
  161. WWINLINE void Set(
  162. const Vector3 &x, // x-axis unit vector
  163. const Vector3 &y, // y-axis unit vector
  164. const Vector3 &z, // z-axis unit vector
  165. const Vector3 &pos // position
  166. );
  167. WWINLINE void Set(const Vector3 & axis,float angle);
  168. WWINLINE void Set(const Vector3 & axis,float sine,float cosine);
  169. void Set(const Matrix3 & rotation,const Vector3 & position);
  170. void Set(const Quaternion & rotation,const Vector3 & position);
  171. // Creates identity rotation w. given position.
  172. WWINLINE void Set(const Vector3 & position);
  173. // access functions:
  174. WWINLINE Vector4 & operator [] (int i) { return Row[i]; }
  175. WWINLINE const Vector4 & operator [] (int i) const { return Row[i]; }
  176. WWINLINE Vector3 Get_Translation(void) const { return Vector3(Row[0][3],Row[1][3],Row[2][3]); }
  177. WWINLINE void Get_Translation(Vector3 * set) const { set->X = Row[0][3]; set->Y = Row[1][3]; set->Z = Row[2][3]; }
  178. WWINLINE void Set_Translation(const Vector3 & t) { Row[0][3] = t[0]; Row[1][3] = t[1];Row[2][3] = t[2]; }
  179. void Set_Rotation(const Matrix3 & m);
  180. void Set_Rotation(const Quaternion & q);
  181. WWINLINE float Get_X_Translation(void) const { return Row[0][3]; };
  182. WWINLINE float Get_Y_Translation(void) const { return Row[1][3]; };
  183. WWINLINE float Get_Z_Translation(void) const { return Row[2][3]; };
  184. WWINLINE void Set_X_Translation(float x) { Row[0][3] = x; };
  185. WWINLINE void Set_Y_Translation(float y) { Row[1][3] = y; };
  186. WWINLINE void Set_Z_Translation(float z) { Row[2][3] = z; };
  187. WWINLINE void Adjust_Translation(const Vector3 & t) { Row[0][3] += t[0]; Row[1][3] += t[1]; Row[2][3] += t[2]; };
  188. WWINLINE void Adjust_X_Translation(float x) { Row[0][3] += x; };
  189. WWINLINE void Adjust_Y_Translation(float y) { Row[1][3] += y; };
  190. WWINLINE void Adjust_Z_Translation(float z) { Row[2][3] += z; };
  191. // These functions will give you the approximate amount that the
  192. // matrix has been rotated about a given axis. These functions
  193. // cannot be used to re-build a matrx. Use the EulerAnglesClass
  194. // to convert a matrix into a set of three Euler angles.
  195. float Get_X_Rotation(void) const;
  196. float Get_Y_Rotation(void) const;
  197. float Get_Z_Rotation(void) const;
  198. // Each of the transformation methods performs an
  199. // "optimized" post-multiplication with the current matrix.
  200. // All angles are assumed to be radians.
  201. WWINLINE void Make_Identity(void);
  202. void Translate(float x,float y,float z);
  203. void Translate(const Vector3 &t);
  204. void Translate_X(float x);
  205. void Translate_Y(float y);
  206. void Translate_Z(float z);
  207. void Rotate_X(float theta);
  208. void Rotate_Y(float theta);
  209. void Rotate_Z(float theta);
  210. void Rotate_X(float s,float c);
  211. void Rotate_Y(float s,float c);
  212. void Rotate_Z(float s,float c);
  213. // !!
  214. // !! Use Scale methods with Extreme Caution
  215. // !! The Matrix Inverse function, only works
  216. // !! with Orthogonal Matrices, for optimization purposes
  217. // !!
  218. WWINLINE void Scale(float scale); // uniform scale all 3 axis
  219. WWINLINE void Scale(float x, float y, float z); // separate input for each axis
  220. WWINLINE void Scale(Vector3 &scale); // scale each axis
  221. // Each of these performs an "optimized" pre-multiplication with the
  222. // current matrix. All angles are assumed to be radians. The "In_Place"
  223. // versions do not affect the translation part of the matrix,
  224. WWINLINE void Pre_Rotate_X(float theta);
  225. WWINLINE void Pre_Rotate_Y(float theta);
  226. WWINLINE void Pre_Rotate_Z(float theta);
  227. WWINLINE void Pre_Rotate_X(float s,float c);
  228. WWINLINE void Pre_Rotate_Y(float s,float c);
  229. WWINLINE void Pre_Rotate_Z(float s,float c);
  230. WWINLINE void In_Place_Pre_Rotate_X(float theta);
  231. WWINLINE void In_Place_Pre_Rotate_Y(float theta);
  232. WWINLINE void In_Place_Pre_Rotate_Z(float theta);
  233. WWINLINE void In_Place_Pre_Rotate_X(float s,float c);
  234. WWINLINE void In_Place_Pre_Rotate_Y(float s,float c);
  235. WWINLINE void In_Place_Pre_Rotate_Z(float s,float c);
  236. // Points the negative Z axis at the target t. Assumes that
  237. // the "world" uses x-y as the ground and z as altitude.
  238. // Used for pointing cameras at targets.
  239. void Look_At(const Vector3 &p,const Vector3 &t,float roll);
  240. // Previous look_at function follows the camera coordinate convention.
  241. // This one follows the object convention used in Commando and G. I
  242. // special cased this convention since it is used so much by us rather
  243. // than supporting every one of the 24(?) possible conventions...
  244. void Obj_Look_At(const Vector3 &p,const Vector3 &t,float roll);
  245. // Create a matrix given a position and a direction (x axis will point in direction)
  246. void buildTransformMatrix( const Vector3 &pos, const Vector3 &dir );
  247. // use the 3x3 sub-matrix only (ignore translation)
  248. Vector3 Rotate_Vector(const Vector3 &vect) const;
  249. Vector3 Inverse_Rotate_Vector(const Vector3 &vect) const;
  250. // these get the a vector representing the direction an axis is pointing
  251. WWINLINE Vector3 Get_X_Vector() const { return Vector3(Row[0][0], Row[1][0], Row[2][0]); }
  252. WWINLINE Vector3 Get_Y_Vector() const { return Vector3(Row[0][1], Row[1][1], Row[2][1]); }
  253. WWINLINE Vector3 Get_Z_Vector() const { return Vector3(Row[0][2], Row[1][2], Row[2][2]); }
  254. WWINLINE void Get_X_Vector(Vector3 * set_x) const { set_x->Set(Row[0][0], Row[1][0], Row[2][0]); }
  255. WWINLINE void Get_Y_Vector(Vector3 * set_y) const { set_y->Set(Row[0][1], Row[1][1], Row[2][1]); }
  256. WWINLINE void Get_Z_Vector(Vector3 * set_z) const { set_z->Set(Row[0][2], Row[1][2], Row[2][2]); }
  257. // Get the inverse of the matrix.
  258. // TODO: currently the "intended-to-be" general inverse function just calls
  259. // the special case Orthogonal inverse functions. Also, when we implement
  260. // general case, check where we were using Get_Inverse since usually it should
  261. // be changed to Get_Orthogonal_Inverse...
  262. void Get_Inverse(Matrix3D & set_inverse) const;
  263. void Get_Orthogonal_Inverse(Matrix3D & set_inverse) const;
  264. // used for importing SurRender matrices
  265. void Copy_3x3_Matrix(float matrix[3][3]);
  266. // Optimized Axis-Aligned Box transforms. One for each of the common forms of
  267. // axis aligned box: min,max vectors and center,extent vectors.
  268. void Transform_Min_Max_AABox(const Vector3 & min,const Vector3 & max,Vector3 * set_min,Vector3 * set_max) const;
  269. void Transform_Center_Extent_AABox(const Vector3 & center,const Vector3 & extent,Vector3 * set_center,Vector3 * set_extent) const;
  270. // matrix multiplication without temporaries
  271. static void Multiply(const Matrix3D &A,const Matrix3D &B,Matrix3D * set_result);
  272. static WWINLINE void Transform_Vector(const Matrix3D & tm,const Vector3 & in,Vector3 * out);
  273. static WWINLINE void Rotate_Vector(const Matrix3D & tm,const Vector3 & in,Vector3 * out);
  274. // transform a vector by the inverse of this matrix (note: assumes the matrix
  275. // is orthogonal; if you've manually scaled or sheared the matrix this function
  276. // will not give correct results)
  277. static WWINLINE void Inverse_Transform_Vector(const Matrix3D & tm,const Vector3 & in,Vector3 * out);
  278. static WWINLINE void Inverse_Rotate_Vector(const Matrix3D & tm,const Vector3 & in,Vector3 * out);
  279. // Solve a linear system of 3 equations and 3 unknowns. The result of this function is
  280. // that the 3x3 portion of the matrix is inverted and the final column is your answer...
  281. static bool Solve_Linear_System(Matrix3D & system);
  282. // Check whether a matrix is orthogonal or FORCE it to be :-)
  283. int Is_Orthogonal(void) const;
  284. void Re_Orthogonalize(void);
  285. static void Lerp(const Matrix3D &A, const Matrix3D &B, float factor, Matrix3D& result);
  286. #ifdef ALLOW_TEMPORARIES
  287. // nothing
  288. #else
  289. // does "this = that * this"
  290. void preMul(const Matrix3D& that);
  291. // does "this = this * that"
  292. void postMul(const Matrix3D& that);
  293. // does "this = a * b"
  294. void mul(const Matrix3D& a, const Matrix3D& b);
  295. void mulVector3(const Vector3& in, Vector3& out) const;
  296. void mulVector3(Vector3& inout) const { mulVector3(inout, inout); }
  297. #endif
  298. void mulVector3Array(const Vector3* in, Vector3* out, int count) const;
  299. void mulVector3Array(Vector3* inout, int count) const;
  300. // some static matrices which are sometimes useful
  301. static const Matrix3D Identity;
  302. static const Matrix3D RotateX90;
  303. static const Matrix3D RotateX180;
  304. static const Matrix3D RotateX270;
  305. static const Matrix3D RotateY90;
  306. static const Matrix3D RotateY180;
  307. static const Matrix3D RotateY270;
  308. static const Matrix3D RotateZ90;
  309. static const Matrix3D RotateZ180;
  310. static const Matrix3D RotateZ270;
  311. protected:
  312. Vector4 Row[3];
  313. #ifdef ALLOW_TEMPORARIES
  314. WWINLINE friend Vector3 operator * (const Matrix3D &A,const Vector3 &a);
  315. #endif
  316. };
  317. /* ---------------------------------------------------------------
  318. Vector Transformation, Matrix concatenation
  319. --------------------------------------------------------------- */
  320. #ifdef ALLOW_TEMPORARIES
  321. Vector3 operator * (const Matrix3D &A,const Vector3 &v);
  322. Matrix3D operator * (const Matrix3D &A,const Matrix3D &B);
  323. #endif
  324. /* ---------------------------------------------------------------
  325. Equality and inequality operators
  326. --------------------------------------------------------------- */
  327. bool operator == (const Matrix3D &A, const Matrix3D &B);
  328. bool operator != (const Matrix3D &A, const Matrix3D &B);
  329. /***********************************************************************************************
  330. * M3DC::Matrix3D -- Constructors for Matrix3D *
  331. * *
  332. * INPUT: *
  333. * *
  334. * OUTPUT: *
  335. * *
  336. * WARNINGS: *
  337. * *
  338. * HISTORY: *
  339. * 02/24/1997 GH : Created. *
  340. *=============================================================================================*/
  341. WWINLINE Matrix3D::Matrix3D(float m[12])
  342. {
  343. Row[0].Set(m[0],m[1],m[2],m[3]);
  344. Row[1].Set(m[4],m[5],m[6],m[7]);
  345. Row[2].Set(m[8],m[9],m[10],m[11]);
  346. }
  347. WWINLINE Matrix3D::Matrix3D
  348. (
  349. float m11,float m12,float m13,float m14,
  350. float m21,float m22,float m23,float m24,
  351. float m31,float m32,float m33,float m34
  352. )
  353. {
  354. Row[0].Set(m11,m12,m13,m14);
  355. Row[1].Set(m21,m22,m23,m24);
  356. Row[2].Set(m31,m32,m33,m34);
  357. }
  358. WWINLINE Matrix3D::Matrix3D
  359. (
  360. const Vector3 &x, // x-axis unit vector
  361. const Vector3 &y, // y-axis unit vector
  362. const Vector3 &z, // z-axis unit vector
  363. const Vector3 &pos // position
  364. )
  365. {
  366. Set(x,y,z,pos);
  367. }
  368. WWINLINE Matrix3D::Matrix3D(const Vector3 & axis,float angle)
  369. {
  370. Set(axis,angle);
  371. }
  372. WWINLINE Matrix3D::Matrix3D(const Vector3 & axis,float sine,float cosine)
  373. {
  374. Set(axis,sine,cosine);
  375. }
  376. WWINLINE Matrix3D::Matrix3D(const Matrix3 & rot,const Vector3 & pos)
  377. {
  378. Set(rot,pos);
  379. }
  380. WWINLINE Matrix3D::Matrix3D(const Quaternion & rot,const Vector3 & pos)
  381. {
  382. Set(rot,pos);
  383. }
  384. WWINLINE Matrix3D::Matrix3D(const Vector3 & position)
  385. {
  386. Set(position);
  387. }
  388. // Copy Constructor
  389. WWINLINE Matrix3D::Matrix3D(const Matrix3D & m)
  390. {
  391. Row[0] = m.Row[0];
  392. Row[1] = m.Row[1];
  393. Row[2] = m.Row[2];
  394. }
  395. // Assignment operator
  396. WWINLINE Matrix3D & Matrix3D::operator = (const Matrix3D & m)
  397. {
  398. Row[0] = m.Row[0];
  399. Row[1] = m.Row[1];
  400. Row[2] = m.Row[2];
  401. return *this;
  402. }
  403. /***********************************************************************************************
  404. * Matrix3D::Set -- init a Matrix3D from an arrray of 12 floats *
  405. * *
  406. * INPUT: *
  407. * *
  408. * OUTPUT: *
  409. * *
  410. * WARNINGS: *
  411. * *
  412. * HISTORY: *
  413. * 2/24/98 GTH : Created. *
  414. *=============================================================================================*/
  415. WWINLINE void Matrix3D::Set(float m[12])
  416. {
  417. Row[0].Set(m[0],m[1],m[2],m[3]);
  418. Row[1].Set(m[4],m[5],m[6],m[7]);
  419. Row[2].Set(m[8],m[9],m[10],m[11]);
  420. }
  421. /***********************************************************************************************
  422. * Matrix3D::Set -- Init a Matrix3D from 12 individual floats *
  423. * *
  424. * INPUT: *
  425. * *
  426. * OUTPUT: *
  427. * *
  428. * WARNINGS: *
  429. * *
  430. * HISTORY: *
  431. * 2/24/98 GTH : Created. *
  432. *=============================================================================================*/
  433. WWINLINE void Matrix3D::Set( float m11,float m12,float m13,float m14,
  434. float m21,float m22,float m23,float m24,
  435. float m31,float m32,float m33,float m34)
  436. {
  437. Row[0].Set(m11,m12,m13,m14);
  438. Row[1].Set(m21,m22,m23,m24);
  439. Row[2].Set(m31,m32,m33,m34);
  440. }
  441. /***********************************************************************************************
  442. * Matrix3D::Set -- Init a matrix from 3 axis vectors and a position *
  443. * *
  444. * INPUT: *
  445. * *
  446. * OUTPUT: *
  447. * *
  448. * WARNINGS: *
  449. * *
  450. * HISTORY: *
  451. * 2/24/98 GTH : Created. *
  452. *=============================================================================================*/
  453. WWINLINE void Matrix3D::Set( const Vector3 &x, // x-axis unit vector
  454. const Vector3 &y, // y-axis unit vector
  455. const Vector3 &z, // z-axis unit vector
  456. const Vector3 &pos) // position
  457. {
  458. Row[0].Set(x[0],y[0],z[0],pos[0]);
  459. Row[1].Set(x[1],y[1],z[1],pos[1]);
  460. Row[2].Set(x[2],y[2],z[2],pos[2]);
  461. }
  462. /***********************************************************************************************
  463. * Matrix3D::Set -- init a matrix to be a rotation about the given axis *
  464. * *
  465. * INPUT: *
  466. * *
  467. * OUTPUT: *
  468. * *
  469. * WARNINGS: *
  470. * *
  471. * HISTORY: *
  472. * 2/24/98 GTH : Created. *
  473. *=============================================================================================*/
  474. WWINLINE void Matrix3D::Set(const Vector3 & axis,float angle)
  475. {
  476. float c = cosf(angle);
  477. float s = sinf(angle);
  478. Set(axis,s,c);
  479. }
  480. /***********************************************************************************************
  481. * Matrix3D::Set -- init a matrix to be a rotation about the given axis *
  482. * *
  483. * INPUT: *
  484. * *
  485. * OUTPUT: *
  486. * *
  487. * WARNINGS: *
  488. * *
  489. * HISTORY: *
  490. * 2/24/98 GTH : Created. *
  491. *=============================================================================================*/
  492. WWINLINE void Matrix3D::Set(const Vector3 & axis,float s,float c)
  493. {
  494. assert(WWMath::Fabs(axis.Length2() - 1.0f) < 0.001f);
  495. Row[0].Set(
  496. (float)(axis[0]*axis[0] + c*(1.0f - axis[0]*axis[0])),
  497. (float)(axis[0]*axis[1]*(1.0f - c) - axis[2]*s),
  498. (float)(axis[2]*axis[0]*(1.0f - c) + axis[1]*s),
  499. 0.0f
  500. );
  501. Row[1].Set(
  502. (float)(axis[0]*axis[1]*(1.0f - c) + axis[2]*s),
  503. (float)(axis[1]*axis[1] + c*(1.0f - axis[1]*axis[1])),
  504. (float)(axis[1]*axis[2]*(1.0f - c) - axis[0]*s),
  505. 0.0f
  506. );
  507. Row[2].Set(
  508. (float)(axis[2]*axis[0]*(1.0f - c) - axis[1]*s),
  509. (float)(axis[1]*axis[2]*(1.0f - c) + axis[0]*s),
  510. (float)(axis[2]*axis[2] + c*(1 - axis[2]*axis[2])),
  511. 0.0f
  512. );
  513. }
  514. /***********************************************************************************************
  515. * Matrix3D::Set -- Init a matrix to be a pure translation *
  516. * *
  517. * INPUT: *
  518. * *
  519. * OUTPUT: *
  520. * *
  521. * WARNINGS: *
  522. * *
  523. * HISTORY: *
  524. * 2/24/98 GTH : Created. *
  525. *=============================================================================================*/
  526. WWINLINE void Matrix3D::Set(const Vector3 & position)
  527. {
  528. Row[0].Set(1.0f,0.0f,0.0f,position[0]);
  529. Row[1].Set(0.0f,1.0f,0.0f,position[1]);
  530. Row[2].Set(0.0f,0.0f,1.0f,position[2]);
  531. }
  532. /***********************************************************************************************
  533. * M3DC::Make_Identity -- Initializes the matrix to be the identity matrix *
  534. * *
  535. * INPUT: *
  536. * *
  537. * OUTPUT: *
  538. * *
  539. * WARNINGS: *
  540. * *
  541. * HISTORY: *
  542. * 02/24/1997 GH : Created. *
  543. *=============================================================================================*/
  544. WWINLINE void Matrix3D::Make_Identity(void)
  545. {
  546. Row[0].Set(1.0f,0.0f,0.0f,0.0f);
  547. Row[1].Set(0.0f,1.0f,0.0f,0.0f);
  548. Row[2].Set(0.0f,0.0f,1.0f,0.0f);
  549. }
  550. /***********************************************************************************************
  551. * M3DC::Translate -- Post-Multiplies by a Translation Matrix *
  552. * *
  553. * INPUT: *
  554. * *
  555. * OUTPUT: *
  556. * *
  557. * WARNINGS: *
  558. * *
  559. * HISTORY: *
  560. * 02/24/1997 GH : Created. *
  561. *=============================================================================================*/
  562. WWINLINE void Matrix3D::Translate(float x,float y,float z)
  563. {
  564. Row[0][3] += (float)(Row[0][0]*x + Row[0][1]*y + Row[0][2]*z);
  565. Row[1][3] += (float)(Row[1][0]*x + Row[1][1]*y + Row[1][2]*z);
  566. Row[2][3] += (float)(Row[2][0]*x + Row[2][1]*y + Row[2][2]*z);
  567. }
  568. /***********************************************************************************************
  569. * M3DC::Translate -- Post-Multiplies the matrix by a translation matrix *
  570. * *
  571. * INPUT: *
  572. * *
  573. * OUTPUT: *
  574. * *
  575. * WARNINGS: *
  576. * *
  577. * HISTORY: *
  578. * 02/24/1997 GH : Created. *
  579. *=============================================================================================*/
  580. WWINLINE void Matrix3D::Translate(const Vector3 &t)
  581. {
  582. Row[0][3] += Row[0][0]*t[0] + Row[0][1]*t[1] + Row[0][2]*t[2];
  583. Row[1][3] += Row[1][0]*t[0] + Row[1][1]*t[1] + Row[1][2]*t[2];
  584. Row[2][3] += Row[2][0]*t[0] + Row[2][1]*t[1] + Row[2][2]*t[2];
  585. }
  586. /***********************************************************************************************
  587. * M3DC::Translate_X -- Post-Multiplies the matrix by a translation matrix with X only *
  588. * *
  589. * INPUT: *
  590. * *
  591. * OUTPUT: *
  592. * *
  593. * WARNINGS: *
  594. * *
  595. * HISTORY: *
  596. * 07/06/1998 NH : Created. *
  597. *=============================================================================================*/
  598. WWINLINE void Matrix3D::Translate_X(float x)
  599. {
  600. Row[0][3] += (float)(Row[0][0]*x);
  601. Row[1][3] += (float)(Row[1][0]*x);
  602. Row[2][3] += (float)(Row[2][0]*x);
  603. }
  604. /***********************************************************************************************
  605. * M3DC::Translate_Y -- Post-Multiplies the matrix by a translation matrix with Y only *
  606. * *
  607. * INPUT: *
  608. * *
  609. * OUTPUT: *
  610. * *
  611. * WARNINGS: *
  612. * *
  613. * HISTORY: *
  614. * 07/06/1998 NH : Created. *
  615. *=============================================================================================*/
  616. WWINLINE void Matrix3D::Translate_Y(float y)
  617. {
  618. Row[0][3] += (float)(Row[0][1]*y);
  619. Row[1][3] += (float)(Row[1][1]*y);
  620. Row[2][3] += (float)(Row[2][1]*y);
  621. }
  622. /***********************************************************************************************
  623. * M3DC::Translate_Z -- Post-Multiplies the matrix by a translation matrix with Z only *
  624. * *
  625. * INPUT: *
  626. * *
  627. * OUTPUT: *
  628. * *
  629. * WARNINGS: *
  630. * *
  631. * HISTORY: *
  632. * 07/06/1998 NH : Created. *
  633. *=============================================================================================*/
  634. WWINLINE void Matrix3D::Translate_Z(float z)
  635. {
  636. Row[0][3] += (float)(Row[0][2]*z);
  637. Row[1][3] += (float)(Row[1][2]*z);
  638. Row[2][3] += (float)(Row[2][2]*z);
  639. }
  640. /***********************************************************************************************
  641. * M3DC::Rotate_X -- Post-Multiplies the matrix by a rotation about the X axis *
  642. * *
  643. * INPUT: *
  644. * *
  645. * OUTPUT: *
  646. * *
  647. * WARNINGS: *
  648. * *
  649. * HISTORY: *
  650. * 02/24/1997 GH : Created. *
  651. *=============================================================================================*/
  652. WWINLINE void Matrix3D::Rotate_X(float theta)
  653. {
  654. float tmp1,tmp2;
  655. float s,c;
  656. s = sinf(theta);
  657. c = cosf(theta);
  658. tmp1 = Row[0][1]; tmp2 = Row[0][2];
  659. Row[0][1] = (float)( c*tmp1 + s*tmp2);
  660. Row[0][2] = (float)(-s*tmp1 + c*tmp2);
  661. tmp1 = Row[1][1]; tmp2 = Row[1][2];
  662. Row[1][1] = (float)( c*tmp1 + s*tmp2);
  663. Row[1][2] = (float)(-s*tmp1 + c*tmp2);
  664. tmp1 = Row[2][1]; tmp2 = Row[2][2];
  665. Row[2][1] = (float)( c*tmp1 + s*tmp2);
  666. Row[2][2] = (float)(-s*tmp1 + c*tmp2);
  667. }
  668. /***********************************************************************************************
  669. * M3DC::Rotate_X -- Post-Multiplies the matrix by a rotation about the X axis *
  670. * *
  671. * INPUT: *
  672. * s - sine of the angle *
  673. * c - cosine of the angle *
  674. * *
  675. * OUTPUT: *
  676. * *
  677. * WARNINGS: *
  678. * *
  679. * HISTORY: *
  680. * 02/24/1997 GH : Created. *
  681. *=============================================================================================*/
  682. WWINLINE void Matrix3D::Rotate_X(float s,float c)
  683. {
  684. float tmp1,tmp2;
  685. tmp1 = Row[0][1]; tmp2 = Row[0][2];
  686. Row[0][1] = (float)( c*tmp1 + s*tmp2);
  687. Row[0][2] = (float)(-s*tmp1 + c*tmp2);
  688. tmp1 = Row[1][1]; tmp2 = Row[1][2];
  689. Row[1][1] = (float)( c*tmp1 + s*tmp2);
  690. Row[1][2] = (float)(-s*tmp1 + c*tmp2);
  691. tmp1 = Row[2][1]; tmp2 = Row[2][2];
  692. Row[2][1] = (float)( c*tmp1 + s*tmp2);
  693. Row[2][2] = (float)(-s*tmp1 + c*tmp2);
  694. }
  695. /***********************************************************************************************
  696. * M3DC::Rotate_Y -- Post-multiplies the matrix by a rotation about the Y axis *
  697. * *
  698. * INPUT: *
  699. * theta - angle (in radians) to rotate *
  700. * *
  701. * OUTPUT: *
  702. * *
  703. * WARNINGS: *
  704. * *
  705. * HISTORY: *
  706. * 02/24/1997 GH : Created. *
  707. *=============================================================================================*/
  708. WWINLINE void Matrix3D::Rotate_Y(float theta)
  709. {
  710. float tmp1,tmp2;
  711. float s,c;
  712. s = sinf(theta);
  713. c = cosf(theta);
  714. tmp1 = Row[0][0]; tmp2 = Row[0][2];
  715. Row[0][0] = (float)(c*tmp1 - s*tmp2);
  716. Row[0][2] = (float)(s*tmp1 + c*tmp2);
  717. tmp1 = Row[1][0]; tmp2 = Row[1][2];
  718. Row[1][0] = (float)(c*tmp1 - s*tmp2);
  719. Row[1][2] = (float)(s*tmp1 + c*tmp2);
  720. tmp1 = Row[2][0]; tmp2 = Row[2][2];
  721. Row[2][0] = (float)(c*tmp1 - s*tmp2);
  722. Row[2][2] = (float)(s*tmp1 + c*tmp2);
  723. }
  724. /***********************************************************************************************
  725. * M3DC::Rotate_Y -- Post-Multiplies the matrix by a rotation about Y *
  726. * *
  727. * INPUT: *
  728. * s - sine of the angle *
  729. * c - cosine of the angle *
  730. * *
  731. * OUTPUT: *
  732. * *
  733. * WARNINGS: *
  734. * *
  735. * HISTORY: *
  736. * 02/24/1997 GH : Created. *
  737. *=============================================================================================*/
  738. WWINLINE void Matrix3D::Rotate_Y(float s,float c)
  739. {
  740. float tmp1,tmp2;
  741. tmp1 = Row[0][0]; tmp2 = Row[0][2];
  742. Row[0][0] = (float)(c*tmp1 - s*tmp2);
  743. Row[0][2] = (float)(s*tmp1 + c*tmp2);
  744. tmp1 = Row[1][0]; tmp2 = Row[1][2];
  745. Row[1][0] = (float)(c*tmp1 - s*tmp2);
  746. Row[1][2] = (float)(s*tmp1 + c*tmp2);
  747. tmp1 = Row[2][0]; tmp2 = Row[2][2];
  748. Row[2][0] = (float)(c*tmp1 - s*tmp2);
  749. Row[2][2] = (float)(s*tmp1 + c*tmp2);
  750. }
  751. /***********************************************************************************************
  752. * M3DC::Rotate_Z -- Post-multiplies the matrix by a rotation about Z *
  753. * *
  754. * INPUT: *
  755. * theta - angle (in radians) to rotate *
  756. * *
  757. * OUTPUT: *
  758. * *
  759. * WARNINGS: *
  760. * *
  761. * HISTORY: *
  762. * 02/24/1997 GH : Created. *
  763. *=============================================================================================*/
  764. WWINLINE void Matrix3D::Rotate_Z(float theta)
  765. {
  766. float tmp1,tmp2;
  767. float c,s;
  768. c = cosf(theta);
  769. s = sinf(theta);
  770. tmp1 = Row[0][0]; tmp2 = Row[0][1];
  771. Row[0][0] = (float)( c*tmp1 + s*tmp2);
  772. Row[0][1] = (float)(-s*tmp1 + c*tmp2);
  773. tmp1 = Row[1][0]; tmp2 = Row[1][1];
  774. Row[1][0] = (float)( c*tmp1 + s*tmp2);
  775. Row[1][1] = (float)(-s*tmp1 + c*tmp2);
  776. tmp1 = Row[2][0]; tmp2 = Row[2][1];
  777. Row[2][0] = (float)( c*tmp1 + s*tmp2);
  778. Row[2][1] = (float)(-s*tmp1 + c*tmp2);
  779. }
  780. /***********************************************************************************************
  781. * M3DC::Rotate_Z -- Post-multiplies the matrix by a rotation about Z *
  782. * *
  783. * INPUT: *
  784. * s - sine of the angle to rotate *
  785. * c - cosine of the angle to rotate *
  786. * *
  787. * OUTPUT: *
  788. * *
  789. * WARNINGS: *
  790. * *
  791. * HISTORY: *
  792. * 02/24/1997 GH : Created. *
  793. *=============================================================================================*/
  794. WWINLINE void Matrix3D::Rotate_Z(float s,float c)
  795. {
  796. float tmp1,tmp2;
  797. tmp1 = Row[0][0]; tmp2 = Row[0][1];
  798. Row[0][0] = (float)( c*tmp1 + s*tmp2);
  799. Row[0][1] = (float)(-s*tmp1 + c*tmp2);
  800. tmp1 = Row[1][0]; tmp2 = Row[1][1];
  801. Row[1][0] = (float)( c*tmp1 + s*tmp2);
  802. Row[1][1] = (float)(-s*tmp1 + c*tmp2);
  803. tmp1 = Row[2][0]; tmp2 = Row[2][1];
  804. Row[2][0] = (float)( c*tmp1 + s*tmp2);
  805. Row[2][1] = (float)(-s*tmp1 + c*tmp2);
  806. }
  807. /***********************************************************************************************
  808. * Matrix3D::Scale -- Scales each Axis *
  809. * *
  810. * INPUT: *
  811. * *
  812. * OUTPUT: *
  813. * *
  814. * WARNINGS: *
  815. * *
  816. * HISTORY: *
  817. * 03/07/2000 jga : Created. *
  818. *=============================================================================================*/
  819. // !!
  820. // !! Use Scale methods with Extreme Caution
  821. // !! The Matrix Inverse function, only works
  822. // !! with Orthogonal Matrices, for optimization purposes
  823. // !!
  824. WWINLINE void Matrix3D::Scale(float scale)
  825. { // uniform scale all 3 axis
  826. // X
  827. Row[0][0] *= scale;
  828. Row[1][0] *= scale;
  829. Row[2][0] *= scale;
  830. // Y
  831. Row[0][1] *= scale;
  832. Row[1][1] *= scale;
  833. Row[2][1] *= scale;
  834. // Z
  835. Row[0][2] *= scale;
  836. Row[1][2] *= scale;
  837. Row[2][2] *= scale;
  838. }
  839. /***********************************************************************************************
  840. * Matrix3D::Scale -- Scales each Axis *
  841. * *
  842. * INPUT: *
  843. * *
  844. * OUTPUT: *
  845. * *
  846. * WARNINGS: *
  847. * *
  848. * HISTORY: *
  849. * 03/07/2000 jga : Created. *
  850. *=============================================================================================*/
  851. // !!
  852. // !! Use Scale methods with Extreme Caution
  853. // !! The Matrix Inverse function, only works
  854. // !! with Orthogonal Matrices, for optimization purposes
  855. // !!
  856. WWINLINE void Matrix3D::Scale(float x, float y, float z)
  857. { // separate input for each axis
  858. // X
  859. Row[0][0] *= x;
  860. Row[1][0] *= x;
  861. Row[2][0] *= x;
  862. // Y
  863. Row[0][1] *= y;
  864. Row[1][1] *= y;
  865. Row[2][1] *= y;
  866. // Z
  867. Row[0][2] *= z;
  868. Row[1][2] *= z;
  869. Row[2][2] *= z;
  870. }
  871. /***********************************************************************************************
  872. * Matrix3D::Scale -- Scales each Axis *
  873. * *
  874. * INPUT: *
  875. * *
  876. * OUTPUT: *
  877. * *
  878. * WARNINGS: *
  879. * *
  880. * HISTORY: *
  881. * 03/07/2000 jga : Created. *
  882. *=============================================================================================*/
  883. // !!
  884. // !! Use Scale methods with Extreme Caution
  885. // !! The Matrix Inverse function, only works
  886. // !! with Orthogonal Matrices, for optimization purposes
  887. // !!
  888. WWINLINE void Matrix3D::Scale(Vector3 &scale)
  889. { // scale each axis
  890. Scale(scale.X, scale.Y, scale.Z);
  891. }
  892. /***********************************************************************************************
  893. * M3DC::Pre_Rotate_X -- Pre-multiplies the matrix by a rotation about X *
  894. * *
  895. * INPUT: *
  896. * theta - angle (in radians) to rotate *
  897. * *
  898. * OUTPUT: *
  899. * *
  900. * WARNINGS: *
  901. * *
  902. * HISTORY: *
  903. * 07/1/1999 NH : Created. *
  904. *=============================================================================================*/
  905. WWINLINE void Matrix3D::Pre_Rotate_X(float theta)
  906. {
  907. float tmp1,tmp2;
  908. float c,s;
  909. c = cosf(theta);
  910. s = sinf(theta);
  911. tmp1 = Row[1][0]; tmp2 = Row[2][0];
  912. Row[1][0] = (float)(c*tmp1 - s*tmp2);
  913. Row[2][0] = (float)(s*tmp1 + c*tmp2);
  914. tmp1 = Row[1][1]; tmp2 = Row[2][1];
  915. Row[1][1] = (float)(c*tmp1 - s*tmp2);
  916. Row[2][1] = (float)(s*tmp1 + c*tmp2);
  917. tmp1 = Row[1][2]; tmp2 = Row[2][2];
  918. Row[1][2] = (float)(c*tmp1 - s*tmp2);
  919. Row[2][2] = (float)(s*tmp1 + c*tmp2);
  920. tmp1 = Row[1][3]; tmp2 = Row[2][3];
  921. Row[1][3] = (float)(c*tmp1 - s*tmp2);
  922. Row[2][3] = (float)(s*tmp1 + c*tmp2);
  923. }
  924. /***********************************************************************************************
  925. * M3DC::Pre_Rotate_Y -- Pre-multiplies the matrix by a rotation about Y *
  926. * *
  927. * INPUT: *
  928. * theta - angle (in radians) to rotate *
  929. * *
  930. * OUTPUT: *
  931. * *
  932. * WARNINGS: *
  933. * *
  934. * HISTORY: *
  935. * 07/1/1999 NH : Created. *
  936. *=============================================================================================*/
  937. WWINLINE void Matrix3D::Pre_Rotate_Y(float theta)
  938. {
  939. float tmp1,tmp2;
  940. float c,s;
  941. c = cosf(theta);
  942. s = sinf(theta);
  943. tmp1 = Row[0][0]; tmp2 = Row[2][0];
  944. Row[0][0] = (float)( c*tmp1 + s*tmp2);
  945. Row[2][0] = (float)(-s*tmp1 + c*tmp2);
  946. tmp1 = Row[0][1]; tmp2 = Row[2][1];
  947. Row[0][1] = (float)( c*tmp1 + s*tmp2);
  948. Row[2][1] = (float)(-s*tmp1 + c*tmp2);
  949. tmp1 = Row[0][2]; tmp2 = Row[2][2];
  950. Row[0][2] = (float)( c*tmp1 + s*tmp2);
  951. Row[2][2] = (float)(-s*tmp1 + c*tmp2);
  952. tmp1 = Row[0][3]; tmp2 = Row[2][3];
  953. Row[0][3] = (float)( c*tmp1 + s*tmp2);
  954. Row[2][3] = (float)(-s*tmp1 + c*tmp2);
  955. }
  956. /***********************************************************************************************
  957. * M3DC::Pre_Rotate_Z -- Pre-multiplies the matrix by a rotation about Z *
  958. * *
  959. * INPUT: *
  960. * theta - angle (in radians) to rotate *
  961. * *
  962. * OUTPUT: *
  963. * *
  964. * WARNINGS: *
  965. * *
  966. * HISTORY: *
  967. * 07/1/1999 NH : Created. *
  968. *=============================================================================================*/
  969. WWINLINE void Matrix3D::Pre_Rotate_Z(float theta)
  970. {
  971. float tmp1,tmp2;
  972. float c,s;
  973. c = cosf(theta);
  974. s = sinf(theta);
  975. tmp1 = Row[0][0]; tmp2 = Row[1][0];
  976. Row[0][0] = (float)(c*tmp1 - s*tmp2);
  977. Row[1][0] = (float)(s*tmp1 + c*tmp2);
  978. tmp1 = Row[0][1]; tmp2 = Row[1][1];
  979. Row[0][1] = (float)(c*tmp1 - s*tmp2);
  980. Row[1][1] = (float)(s*tmp1 + c*tmp2);
  981. tmp1 = Row[0][2]; tmp2 = Row[1][2];
  982. Row[0][2] = (float)(c*tmp1 - s*tmp2);
  983. Row[1][2] = (float)(s*tmp1 + c*tmp2);
  984. tmp1 = Row[0][3]; tmp2 = Row[1][3];
  985. Row[0][3] = (float)(c*tmp1 - s*tmp2);
  986. Row[1][3] = (float)(s*tmp1 + c*tmp2);
  987. }
  988. /***********************************************************************************************
  989. * M3DC::Pre_Rotate_X -- Pre-multiplies the matrix by a rotation about X *
  990. * *
  991. * INPUT: *
  992. * s - sine of the angle to rotate *
  993. * c - cosine of the angle to rotate *
  994. * *
  995. * OUTPUT: *
  996. * *
  997. * WARNINGS: *
  998. * *
  999. * HISTORY: *
  1000. * 07/1/1999 NH : Created. *
  1001. *=============================================================================================*/
  1002. WWINLINE void Matrix3D::Pre_Rotate_X(float s,float c)
  1003. {
  1004. float tmp1,tmp2;
  1005. tmp1 = Row[1][0]; tmp2 = Row[2][0];
  1006. Row[1][0] = (float)(c*tmp1 - s*tmp2);
  1007. Row[2][0] = (float)(s*tmp1 + c*tmp2);
  1008. tmp1 = Row[1][1]; tmp2 = Row[2][1];
  1009. Row[1][1] = (float)(c*tmp1 - s*tmp2);
  1010. Row[2][1] = (float)(s*tmp1 + c*tmp2);
  1011. tmp1 = Row[1][2]; tmp2 = Row[2][2];
  1012. Row[1][2] = (float)(c*tmp1 - s*tmp2);
  1013. Row[2][2] = (float)(s*tmp1 + c*tmp2);
  1014. tmp1 = Row[1][3]; tmp2 = Row[2][3];
  1015. Row[1][3] = (float)(c*tmp1 - s*tmp2);
  1016. Row[2][3] = (float)(s*tmp1 + c*tmp2);
  1017. }
  1018. /***********************************************************************************************
  1019. * M3DC::Pre_Rotate_Y -- Pre-multiplies the matrix by a rotation about Y *
  1020. * *
  1021. * INPUT: *
  1022. * s - sine of the angle to rotate *
  1023. * c - cosine of the angle to rotate *
  1024. * *
  1025. * OUTPUT: *
  1026. * *
  1027. * WARNINGS: *
  1028. * *
  1029. * HISTORY: *
  1030. * 07/1/1999 NH : Created. *
  1031. *=============================================================================================*/
  1032. WWINLINE void Matrix3D::Pre_Rotate_Y(float s,float c)
  1033. {
  1034. float tmp1,tmp2;
  1035. tmp1 = Row[0][0]; tmp2 = Row[2][0];
  1036. Row[0][0] = (float)( c*tmp1 + s*tmp2);
  1037. Row[2][0] = (float)(-s*tmp1 + c*tmp2);
  1038. tmp1 = Row[0][1]; tmp2 = Row[2][1];
  1039. Row[0][1] = (float)( c*tmp1 + s*tmp2);
  1040. Row[2][1] = (float)(-s*tmp1 + c*tmp2);
  1041. tmp1 = Row[0][2]; tmp2 = Row[2][2];
  1042. Row[0][2] = (float)( c*tmp1 + s*tmp2);
  1043. Row[2][2] = (float)(-s*tmp1 + c*tmp2);
  1044. tmp1 = Row[0][3]; tmp2 = Row[2][3];
  1045. Row[0][3] = (float)( c*tmp1 + s*tmp2);
  1046. Row[2][3] = (float)(-s*tmp1 + c*tmp2);
  1047. }
  1048. /***********************************************************************************************
  1049. * M3DC::Pre_Rotate_Z -- Pre-multiplies the matrix by a rotation about Z *
  1050. * *
  1051. * INPUT: *
  1052. * s - sine of the angle to rotate *
  1053. * c - cosine of the angle to rotate *
  1054. * *
  1055. * OUTPUT: *
  1056. * *
  1057. * WARNINGS: *
  1058. * *
  1059. * HISTORY: *
  1060. * 07/1/1999 NH : Created. *
  1061. *=============================================================================================*/
  1062. WWINLINE void Matrix3D::Pre_Rotate_Z(float s,float c)
  1063. {
  1064. float tmp1,tmp2;
  1065. tmp1 = Row[0][0]; tmp2 = Row[1][0];
  1066. Row[0][0] = (float)(c*tmp1 - s*tmp2);
  1067. Row[1][0] = (float)(s*tmp1 + c*tmp2);
  1068. tmp1 = Row[0][1]; tmp2 = Row[1][1];
  1069. Row[0][1] = (float)(c*tmp1 - s*tmp2);
  1070. Row[1][1] = (float)(s*tmp1 + c*tmp2);
  1071. tmp1 = Row[0][2]; tmp2 = Row[1][2];
  1072. Row[0][2] = (float)(c*tmp1 - s*tmp2);
  1073. Row[1][2] = (float)(s*tmp1 + c*tmp2);
  1074. tmp1 = Row[0][3]; tmp2 = Row[1][3];
  1075. Row[0][3] = (float)(c*tmp1 - s*tmp2);
  1076. Row[1][3] = (float)(s*tmp1 + c*tmp2);
  1077. }
  1078. /***********************************************************************************************
  1079. * M3DC::In_Place_Pre_Rotate_X -- Pre-multiplies rotation part of matrix by a rotation about X *
  1080. * *
  1081. * INPUT: *
  1082. * theta - angle (in radians) to rotate *
  1083. * *
  1084. * OUTPUT: *
  1085. * *
  1086. * WARNINGS: *
  1087. * *
  1088. * HISTORY: *
  1089. * 07/1/1999 NH : Created. *
  1090. *=============================================================================================*/
  1091. WWINLINE void Matrix3D::In_Place_Pre_Rotate_X(float theta)
  1092. {
  1093. float tmp1,tmp2;
  1094. float c,s;
  1095. c = cosf(theta);
  1096. s = sinf(theta);
  1097. tmp1 = Row[1][0]; tmp2 = Row[2][0];
  1098. Row[1][0] = (float)(c*tmp1 - s*tmp2);
  1099. Row[2][0] = (float)(s*tmp1 + c*tmp2);
  1100. tmp1 = Row[1][1]; tmp2 = Row[2][1];
  1101. Row[1][1] = (float)(c*tmp1 - s*tmp2);
  1102. Row[2][1] = (float)(s*tmp1 + c*tmp2);
  1103. tmp1 = Row[1][2]; tmp2 = Row[2][2];
  1104. Row[1][2] = (float)(c*tmp1 - s*tmp2);
  1105. Row[2][2] = (float)(s*tmp1 + c*tmp2);
  1106. }
  1107. /***********************************************************************************************
  1108. * M3DC::In_Place_Pre_Rotate_Y -- Pre-multiplies rotation part of matrix by a rotation about Y *
  1109. * *
  1110. * INPUT: *
  1111. * theta - angle (in radians) to rotate *
  1112. * *
  1113. * OUTPUT: *
  1114. * *
  1115. * WARNINGS: *
  1116. * *
  1117. * HISTORY: *
  1118. * 07/1/1999 NH : Created. *
  1119. *=============================================================================================*/
  1120. WWINLINE void Matrix3D::In_Place_Pre_Rotate_Y(float theta)
  1121. {
  1122. float tmp1,tmp2;
  1123. float c,s;
  1124. c = cosf(theta);
  1125. s = sinf(theta);
  1126. tmp1 = Row[0][0]; tmp2 = Row[2][0];
  1127. Row[0][0] = (float)( c*tmp1 + s*tmp2);
  1128. Row[2][0] = (float)(-s*tmp1 + c*tmp2);
  1129. tmp1 = Row[0][1]; tmp2 = Row[2][1];
  1130. Row[0][1] = (float)( c*tmp1 + s*tmp2);
  1131. Row[2][1] = (float)(-s*tmp1 + c*tmp2);
  1132. tmp1 = Row[0][2]; tmp2 = Row[2][2];
  1133. Row[0][2] = (float)( c*tmp1 + s*tmp2);
  1134. Row[2][2] = (float)(-s*tmp1 + c*tmp2);
  1135. }
  1136. /***********************************************************************************************
  1137. * M3DC::In_Place_Pre_Rotate_Z -- Pre-multiplies rotation part of matrix by a rotation about Z *
  1138. * *
  1139. * INPUT: *
  1140. * theta - angle (in radians) to rotate *
  1141. * *
  1142. * OUTPUT: *
  1143. * *
  1144. * WARNINGS: *
  1145. * *
  1146. * HISTORY: *
  1147. * 07/1/1999 NH : Created. *
  1148. *=============================================================================================*/
  1149. WWINLINE void Matrix3D::In_Place_Pre_Rotate_Z(float theta)
  1150. {
  1151. float tmp1,tmp2;
  1152. float c,s;
  1153. c = cosf(theta);
  1154. s = sinf(theta);
  1155. tmp1 = Row[0][0]; tmp2 = Row[1][0];
  1156. Row[0][0] = (float)(c*tmp1 - s*tmp2);
  1157. Row[1][0] = (float)(s*tmp1 + c*tmp2);
  1158. tmp1 = Row[0][1]; tmp2 = Row[1][1];
  1159. Row[0][1] = (float)(c*tmp1 - s*tmp2);
  1160. Row[1][1] = (float)(s*tmp1 + c*tmp2);
  1161. tmp1 = Row[0][2]; tmp2 = Row[1][2];
  1162. Row[0][2] = (float)(c*tmp1 - s*tmp2);
  1163. Row[1][2] = (float)(s*tmp1 + c*tmp2);
  1164. }
  1165. /***********************************************************************************************
  1166. * M3DC::In_Place_Pre_Rotate_X -- Pre-multiplies rotation part of matrix by a rotation about X *
  1167. * *
  1168. * INPUT: *
  1169. * s - sine of the angle to rotate *
  1170. * c - cosine of the angle to rotate *
  1171. * *
  1172. * OUTPUT: *
  1173. * *
  1174. * WARNINGS: *
  1175. * *
  1176. * HISTORY: *
  1177. * 07/1/1999 NH : Created. *
  1178. *=============================================================================================*/
  1179. WWINLINE void Matrix3D::In_Place_Pre_Rotate_X(float s,float c)
  1180. {
  1181. float tmp1,tmp2;
  1182. tmp1 = Row[1][0]; tmp2 = Row[2][0];
  1183. Row[1][0] = (float)(c*tmp1 - s*tmp2);
  1184. Row[2][0] = (float)(s*tmp1 + c*tmp2);
  1185. tmp1 = Row[1][1]; tmp2 = Row[2][1];
  1186. Row[1][1] = (float)(c*tmp1 - s*tmp2);
  1187. Row[2][1] = (float)(s*tmp1 + c*tmp2);
  1188. tmp1 = Row[1][2]; tmp2 = Row[2][2];
  1189. Row[1][2] = (float)(c*tmp1 - s*tmp2);
  1190. Row[2][2] = (float)(s*tmp1 + c*tmp2);
  1191. }
  1192. /***********************************************************************************************
  1193. * M3DC::In_Place_Pre_Rotate_Y -- Pre-multiplies rotation part of matrix by a rotation about Y *
  1194. * *
  1195. * INPUT: *
  1196. * s - sine of the angle to rotate *
  1197. * c - cosine of the angle to rotate *
  1198. * *
  1199. * OUTPUT: *
  1200. * *
  1201. * WARNINGS: *
  1202. * *
  1203. * HISTORY: *
  1204. * 07/1/1999 NH : Created. *
  1205. *=============================================================================================*/
  1206. WWINLINE void Matrix3D::In_Place_Pre_Rotate_Y(float s,float c)
  1207. {
  1208. float tmp1,tmp2;
  1209. tmp1 = Row[0][0]; tmp2 = Row[2][0];
  1210. Row[0][0] = (float)( c*tmp1 + s*tmp2);
  1211. Row[2][0] = (float)(-s*tmp1 + c*tmp2);
  1212. tmp1 = Row[0][1]; tmp2 = Row[2][1];
  1213. Row[0][1] = (float)( c*tmp1 + s*tmp2);
  1214. Row[2][1] = (float)(-s*tmp1 + c*tmp2);
  1215. tmp1 = Row[0][2]; tmp2 = Row[2][2];
  1216. Row[0][2] = (float)( c*tmp1 + s*tmp2);
  1217. Row[2][2] = (float)(-s*tmp1 + c*tmp2);
  1218. }
  1219. /***********************************************************************************************
  1220. * M3DC::In_Place_Pre_Rotate_Z -- Pre-multiplies rotation part of matrix by a rotation about Z *
  1221. * *
  1222. * INPUT: *
  1223. * s - sine of the angle to rotate *
  1224. * c - cosine of the angle to rotate *
  1225. * *
  1226. * OUTPUT: *
  1227. * *
  1228. * WARNINGS: *
  1229. * *
  1230. * HISTORY: *
  1231. * 07/1/1999 NH : Created. *
  1232. *=============================================================================================*/
  1233. WWINLINE void Matrix3D::In_Place_Pre_Rotate_Z(float s,float c)
  1234. {
  1235. float tmp1,tmp2;
  1236. tmp1 = Row[0][0]; tmp2 = Row[1][0];
  1237. Row[0][0] = (float)(c*tmp1 - s*tmp2);
  1238. Row[1][0] = (float)(s*tmp1 + c*tmp2);
  1239. tmp1 = Row[0][1]; tmp2 = Row[1][1];
  1240. Row[0][1] = (float)(c*tmp1 - s*tmp2);
  1241. Row[1][1] = (float)(s*tmp1 + c*tmp2);
  1242. tmp1 = Row[0][2]; tmp2 = Row[1][2];
  1243. Row[0][2] = (float)(c*tmp1 - s*tmp2);
  1244. Row[1][2] = (float)(s*tmp1 + c*tmp2);
  1245. }
  1246. #ifdef ALLOW_TEMPORARIES
  1247. /***********************************************************************************************
  1248. * operator * -- Matrix multiplication *
  1249. * *
  1250. * INPUT: *
  1251. * *
  1252. * OUTPUT: *
  1253. * *
  1254. * WARNINGS: *
  1255. * *
  1256. * HISTORY: *
  1257. * 02/24/1997 GH : Created. *
  1258. *=============================================================================================*/
  1259. WWINLINE Matrix3D operator * (const Matrix3D &A,const Matrix3D &B)
  1260. {
  1261. Matrix3D C;
  1262. float tmp1,tmp2,tmp3;
  1263. tmp1 = B[0][0];
  1264. tmp2 = B[1][0];
  1265. tmp3 = B[2][0];
  1266. C[0][0] = (float)(A[0][0]*tmp1 + A[0][1]*tmp2 + A[0][2]*tmp3);
  1267. C[1][0] = (float)(A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3);
  1268. C[2][0] = (float)(A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3);
  1269. tmp1 = B[0][1];
  1270. tmp2 = B[1][1];
  1271. tmp3 = B[2][1];
  1272. C[0][1] = (float)(A[0][0]*tmp1 + A[0][1]*tmp2 + A[0][2]*tmp3);
  1273. C[1][1] = (float)(A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3);
  1274. C[2][1] = (float)(A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3);
  1275. tmp1 = B[0][2];
  1276. tmp2 = B[1][2];
  1277. tmp3 = B[2][2];
  1278. C[0][2] = (float)(A[0][0]*tmp1 + A[0][1]*tmp2 + A[0][2]*tmp3);
  1279. C[1][2] = (float)(A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3);
  1280. C[2][2] = (float)(A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3);
  1281. tmp1 = B[0][3];
  1282. tmp2 = B[1][3];
  1283. tmp3 = B[2][3];
  1284. C[0][3] = (float)(A[0][0]*tmp1 + A[0][1]*tmp2 + A[0][2]*tmp3 + A[0][3]);
  1285. C[1][3] = (float)(A[1][0]*tmp1 + A[1][1]*tmp2 + A[1][2]*tmp3 + A[1][3]);
  1286. C[2][3] = (float)(A[2][0]*tmp1 + A[2][1]*tmp2 + A[2][2]*tmp3 + A[2][3]);
  1287. return C;
  1288. }
  1289. #else
  1290. WWINLINE float submul(const Vector4& row, float tmp1, float tmp2, float tmp3)
  1291. {
  1292. return row.X * tmp1 + row.Y * tmp2 + row.Z * tmp3;
  1293. }
  1294. // does "this = that * this"
  1295. WWINLINE void Matrix3D::preMul(const Matrix3D& that)
  1296. {
  1297. assert(this != &that);
  1298. // yes, this is ok. see the comments for 'mul'
  1299. this->mul(that, *this);
  1300. }
  1301. // does "this = this * that"
  1302. WWINLINE void Matrix3D::postMul(const Matrix3D& that)
  1303. {
  1304. assert(this != &that);
  1305. #define AVOID_TEMP_IN_POSTMUL
  1306. #ifdef AVOID_TEMP_IN_POSTMUL
  1307. float tmpX, tmpY, tmpZ, tmpW;
  1308. tmpX = submul(this->Row[0], that.Row[0].X, that.Row[1].X, that.Row[2].X);
  1309. tmpY = submul(this->Row[0], that.Row[0].Y, that.Row[1].Y, that.Row[2].Y);
  1310. tmpZ = submul(this->Row[0], that.Row[0].Z, that.Row[1].Z, that.Row[2].Z);
  1311. tmpW = submul(this->Row[0], that.Row[0].W, that.Row[1].W, that.Row[2].W);
  1312. this->Row[0].X = tmpX;
  1313. this->Row[0].Y = tmpY;
  1314. this->Row[0].Z = tmpZ;
  1315. this->Row[0].W += tmpW;
  1316. tmpX = submul(this->Row[1], that.Row[0].X, that.Row[1].X, that.Row[2].X);
  1317. tmpY = submul(this->Row[1], that.Row[0].Y, that.Row[1].Y, that.Row[2].Y);
  1318. tmpZ = submul(this->Row[1], that.Row[0].Z, that.Row[1].Z, that.Row[2].Z);
  1319. tmpW = submul(this->Row[1], that.Row[0].W, that.Row[1].W, that.Row[2].W);
  1320. this->Row[1].X = tmpX;
  1321. this->Row[1].Y = tmpY;
  1322. this->Row[1].Z = tmpZ;
  1323. this->Row[1].W += tmpW;
  1324. tmpX = submul(this->Row[2], that.Row[0].X, that.Row[1].X, that.Row[2].X);
  1325. tmpY = submul(this->Row[2], that.Row[0].Y, that.Row[1].Y, that.Row[2].Y);
  1326. tmpZ = submul(this->Row[2], that.Row[0].Z, that.Row[1].Z, that.Row[2].Z);
  1327. tmpW = submul(this->Row[2], that.Row[0].W, that.Row[1].W, that.Row[2].W);
  1328. this->Row[2].X = tmpX;
  1329. this->Row[2].Y = tmpY;
  1330. this->Row[2].Z = tmpZ;
  1331. this->Row[2].W += tmpW;
  1332. #else
  1333. Matrix3D tmp = *this;
  1334. this->mul(tmp, that);
  1335. #endif
  1336. }
  1337. // does "this = A * B"
  1338. WWINLINE void Matrix3D::mul(const Matrix3D& A, const Matrix3D& B)
  1339. {
  1340. assert(this != &A);
  1341. //assert(this != &B);
  1342. // nope, this is actually ok. (srj)
  1343. //assert(this != &B);
  1344. float tmp1,tmp2,tmp3;
  1345. tmp1 = B.Row[0].X;
  1346. tmp2 = B.Row[1].X;
  1347. tmp3 = B.Row[2].X;
  1348. this->Row[0].X = submul(A.Row[0], tmp1, tmp2, tmp3);
  1349. this->Row[1].X = submul(A.Row[1], tmp1, tmp2, tmp3);
  1350. this->Row[2].X = submul(A.Row[2], tmp1, tmp2, tmp3);
  1351. tmp1 = B.Row[0].Y;
  1352. tmp2 = B.Row[1].Y;
  1353. tmp3 = B.Row[2].Y;
  1354. this->Row[0].Y = submul(A.Row[0], tmp1, tmp2, tmp3);
  1355. this->Row[1].Y = submul(A.Row[1], tmp1, tmp2, tmp3);
  1356. this->Row[2].Y = submul(A.Row[2], tmp1, tmp2, tmp3);
  1357. tmp1 = B.Row[0].Z;
  1358. tmp2 = B.Row[1].Z;
  1359. tmp3 = B.Row[2].Z;
  1360. this->Row[0].Z = submul(A.Row[0], tmp1, tmp2, tmp3);
  1361. this->Row[1].Z = submul(A.Row[1], tmp1, tmp2, tmp3);
  1362. this->Row[2].Z = submul(A.Row[2], tmp1, tmp2, tmp3);
  1363. tmp1 = B.Row[0].W;
  1364. tmp2 = B.Row[1].W;
  1365. tmp3 = B.Row[2].W;
  1366. this->Row[0].W = submul(A.Row[0], tmp1, tmp2, tmp3) + A.Row[0].W;
  1367. this->Row[1].W = submul(A.Row[1], tmp1, tmp2, tmp3) + A.Row[1].W;
  1368. this->Row[2].W = submul(A.Row[2], tmp1, tmp2, tmp3) + A.Row[2].W;
  1369. }
  1370. #endif
  1371. #ifdef ALLOW_TEMPORARIES
  1372. /***********************************************************************************************
  1373. * operator * -- Matrix - vector multiplication *
  1374. * *
  1375. * INPUT: *
  1376. * *
  1377. * OUTPUT: *
  1378. * *
  1379. * WARNINGS: *
  1380. * *
  1381. * HISTORY: *
  1382. * 02/24/1997 GH : Created. *
  1383. *=============================================================================================*/
  1384. WWINLINE Vector3 operator * (const Matrix3D &A,const Vector3 &a)
  1385. {
  1386. #if 0
  1387. return Vector3
  1388. (
  1389. (A[0][0]*a[0] + A[0][1]*a[1] + A[0][2]*a[2] + A[0][3]),
  1390. (A[1][0]*a[0] + A[1][1]*a[1] + A[1][2]*a[2] + A[1][3]),
  1391. (A[2][0]*a[0] + A[2][1]*a[1] + A[2][2]*a[2] + A[2][3])
  1392. );
  1393. #else
  1394. return Vector3
  1395. (
  1396. (A.Row[0].X*a.X + A.Row[0].Y*a.Y + A.Row[0].Z*a.Z + A.Row[0].W),
  1397. (A.Row[1].X*a.X + A.Row[1].Y*a.Y + A.Row[1].Z*a.Z + A.Row[1].W),
  1398. (A.Row[2].X*a.X + A.Row[2].Y*a.Y + A.Row[2].Z*a.Z + A.Row[2].W)
  1399. );
  1400. #endif
  1401. }
  1402. #else
  1403. WWINLINE void Matrix3D::mulVector3(const Vector3& in, Vector3& out) const
  1404. {
  1405. // note, it's ok for in==out here.
  1406. float x = (Row[0].X * in.X + Row[0].Y * in.Y + Row[0].Z * in.Z + Row[0].W);
  1407. float y = (Row[1].X * in.X + Row[1].Y * in.Y + Row[1].Z * in.Z + Row[1].W);
  1408. float z = (Row[2].X * in.X + Row[2].Y * in.Y + Row[2].Z * in.Z + Row[2].W);
  1409. out.Set(x, y, z);
  1410. }
  1411. #endif
  1412. WWINLINE void Matrix3D::mulVector3Array(const Vector3* in, Vector3* out, int count) const
  1413. {
  1414. assert(in != out);
  1415. #ifdef ALLOW_TEMPORARIES
  1416. for (i=0; i<count; i++)
  1417. {
  1418. out[i] = (*this) * in[i];
  1419. }
  1420. #else
  1421. while (count--)
  1422. {
  1423. // we know that in != out, so no temps needed
  1424. out->X = (Row[0].X * in->X + Row[0].Y * in->Y + Row[0].Z * in->Z + Row[0].W);
  1425. out->Y = (Row[1].X * in->X + Row[1].Y * in->Y + Row[1].Z * in->Z + Row[1].W);
  1426. out->Z = (Row[2].X * in->X + Row[2].Y * in->Y + Row[2].Z * in->Z + Row[2].W);
  1427. ++in;
  1428. ++out;
  1429. }
  1430. #endif
  1431. }
  1432. WWINLINE void Matrix3D::mulVector3Array(Vector3* inout, int count) const
  1433. {
  1434. #ifdef ALLOW_TEMPORARIES
  1435. for (i=0; i<count; i++)
  1436. {
  1437. inout[i] = (*this) * inout[i];
  1438. }
  1439. #else
  1440. while (count--)
  1441. {
  1442. float x = (Row[0].X * inout->X + Row[0].Y * inout->Y + Row[0].Z * inout->Z + Row[0].W);
  1443. float y = (Row[1].X * inout->X + Row[1].Y * inout->Y + Row[1].Z * inout->Z + Row[1].W);
  1444. float z = (Row[2].X * inout->X + Row[2].Y * inout->Y + Row[2].Z * inout->Z + Row[2].W);
  1445. inout->Set(x, y, z);
  1446. ++inout;
  1447. }
  1448. #endif
  1449. }
  1450. /***********************************************************************************************
  1451. * operator == -- Matrix equality operator *
  1452. * *
  1453. * INPUT: *
  1454. * *
  1455. * OUTPUT: *
  1456. * *
  1457. * WARNINGS: *
  1458. * *
  1459. * HISTORY: *
  1460. * 04/29/1998 NH : Created. *
  1461. *=============================================================================================*/
  1462. WWINLINE bool operator == (const Matrix3D &A, const Matrix3D &B)
  1463. {
  1464. for (int i = 0; i < 3; i++) {
  1465. for (int j = 0; j < 4; j++) {
  1466. if (A[i][j] != B[i][j]) return false;
  1467. }
  1468. }
  1469. return true;
  1470. }
  1471. /***********************************************************************************************
  1472. * operator != -- Matrix inequality operator *
  1473. * *
  1474. * INPUT: *
  1475. * *
  1476. * OUTPUT: *
  1477. * *
  1478. * WARNINGS: *
  1479. * *
  1480. * HISTORY: *
  1481. * 04/29/1998 NH : Created. *
  1482. *=============================================================================================*/
  1483. WWINLINE bool operator != (const Matrix3D &A, const Matrix3D &B)
  1484. {
  1485. return !(A == B);
  1486. }
  1487. WWINLINE void Matrix3D::Transform_Vector(const Matrix3D & A,const Vector3 & in,Vector3 * out)
  1488. {
  1489. Vector3 tmp;
  1490. Vector3 * v;
  1491. // check for aliased parameters
  1492. if (out == &in) {
  1493. tmp = in;
  1494. v = &tmp;
  1495. } else {
  1496. v = (Vector3 *)&in; // whats the right way to do this...
  1497. }
  1498. out->X = (A[0][0] * v->X + A[0][1] * v->Y + A[0][2] * v->Z + A[0][3]);
  1499. out->Y = (A[1][0] * v->X + A[1][1] * v->Y + A[1][2] * v->Z + A[1][3]);
  1500. out->Z = (A[2][0] * v->X + A[2][1] * v->Y + A[2][2] * v->Z + A[2][3]);
  1501. }
  1502. WWINLINE void Matrix3D::Rotate_Vector(const Matrix3D & A,const Vector3 & in,Vector3 * out)
  1503. {
  1504. Vector3 tmp;
  1505. Vector3 * v;
  1506. // check for aliased parameters
  1507. if (out == &in) {
  1508. tmp = in;
  1509. v = &tmp;
  1510. } else {
  1511. v = (Vector3 *)&in;
  1512. }
  1513. out->X = (A[0][0] * v->X + A[0][1] * v->Y + A[0][2] * v->Z);
  1514. out->Y = (A[1][0] * v->X + A[1][1] * v->Y + A[1][2] * v->Z);
  1515. out->Z = (A[2][0] * v->X + A[2][1] * v->Y + A[2][2] * v->Z);
  1516. }
  1517. WWINLINE void Matrix3D::Inverse_Transform_Vector(const Matrix3D & A,const Vector3 & in,Vector3 * out)
  1518. {
  1519. Vector3 tmp;
  1520. Vector3 * v;
  1521. // check for aliased parameters
  1522. if (out == &in) {
  1523. tmp = in;
  1524. v = &tmp;
  1525. } else {
  1526. v = (Vector3 *)&in; // whats the right way to do this...
  1527. }
  1528. Vector3 diff(v->X - A[0][3], v->Y - A[1][3], v->Z - A[2][3]);
  1529. Matrix3D::Inverse_Rotate_Vector(A, diff, out);
  1530. }
  1531. WWINLINE void Matrix3D::Inverse_Rotate_Vector(const Matrix3D & A,const Vector3 & in,Vector3 * out)
  1532. {
  1533. Vector3 tmp;
  1534. Vector3 * v;
  1535. // check for aliased parameters
  1536. if (out == &in) {
  1537. tmp = in;
  1538. v = &tmp;
  1539. } else {
  1540. v = (Vector3 *)&in;
  1541. }
  1542. out->X = (A[0][0] * v->X + A[1][0] * v->Y + A[2][0] * v->Z);
  1543. out->Y = (A[0][1] * v->X + A[1][1] * v->Y + A[2][1] * v->Z);
  1544. out->Z = (A[0][2] * v->X + A[1][2] * v->Y + A[2][2] * v->Z);
  1545. }
  1546. class DynamicMatrix3D : public W3DMPO
  1547. {
  1548. W3DMPO_GLUE(DynamicMatrix3D)
  1549. public:
  1550. Matrix3D Mat;
  1551. };
  1552. #endif /* MATRIX3D_H */