ImathMatrix.h 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHMATRIX_H
  35. #define INCLUDED_IMATHMATRIX_H
  36. //----------------------------------------------------------------
  37. //
  38. // 2D (3x3) and 3D (4x4) transformation matrix templates.
  39. //
  40. //----------------------------------------------------------------
  41. #include "ImathPlatform.h"
  42. #include "ImathFun.h"
  43. #include "ImathExc.h"
  44. #include "ImathVec.h"
  45. #include "ImathShear.h"
  46. #include "ImathNamespace.h"
  47. #include <cstring>
  48. #include <iostream>
  49. #include <iomanip>
  50. #include <string.h>
  51. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  52. // suppress exception specification warnings
  53. #pragma warning(disable:4290)
  54. #endif
  55. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  56. enum Uninitialized {UNINITIALIZED};
  57. template <class T> class Matrix33
  58. {
  59. public:
  60. //-------------------
  61. // Access to elements
  62. //-------------------
  63. T x[3][3];
  64. T * operator [] (int i);
  65. const T * operator [] (int i) const;
  66. //-------------
  67. // Constructors
  68. //-------------
  69. Matrix33 (Uninitialized) {}
  70. Matrix33 ();
  71. // 1 0 0
  72. // 0 1 0
  73. // 0 0 1
  74. Matrix33 (T a);
  75. // a a a
  76. // a a a
  77. // a a a
  78. Matrix33 (const T a[3][3]);
  79. // a[0][0] a[0][1] a[0][2]
  80. // a[1][0] a[1][1] a[1][2]
  81. // a[2][0] a[2][1] a[2][2]
  82. Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i);
  83. // a b c
  84. // d e f
  85. // g h i
  86. //--------------------------------
  87. // Copy constructor and assignment
  88. //--------------------------------
  89. Matrix33 (const Matrix33 &v);
  90. template <class S> explicit Matrix33 (const Matrix33<S> &v);
  91. const Matrix33 & operator = (const Matrix33 &v);
  92. const Matrix33 & operator = (T a);
  93. //----------------------
  94. // Compatibility with Sb
  95. //----------------------
  96. T * getValue ();
  97. const T * getValue () const;
  98. template <class S>
  99. void getValue (Matrix33<S> &v) const;
  100. template <class S>
  101. Matrix33 & setValue (const Matrix33<S> &v);
  102. template <class S>
  103. Matrix33 & setTheMatrix (const Matrix33<S> &v);
  104. //---------
  105. // Identity
  106. //---------
  107. void makeIdentity();
  108. //---------
  109. // Equality
  110. //---------
  111. bool operator == (const Matrix33 &v) const;
  112. bool operator != (const Matrix33 &v) const;
  113. //-----------------------------------------------------------------------
  114. // Compare two matrices and test if they are "approximately equal":
  115. //
  116. // equalWithAbsError (m, e)
  117. //
  118. // Returns true if the coefficients of this and m are the same with
  119. // an absolute error of no more than e, i.e., for all i, j
  120. //
  121. // abs (this[i][j] - m[i][j]) <= e
  122. //
  123. // equalWithRelError (m, e)
  124. //
  125. // Returns true if the coefficients of this and m are the same with
  126. // a relative error of no more than e, i.e., for all i, j
  127. //
  128. // abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  129. //-----------------------------------------------------------------------
  130. bool equalWithAbsError (const Matrix33<T> &v, T e) const;
  131. bool equalWithRelError (const Matrix33<T> &v, T e) const;
  132. //------------------------
  133. // Component-wise addition
  134. //------------------------
  135. const Matrix33 & operator += (const Matrix33 &v);
  136. const Matrix33 & operator += (T a);
  137. Matrix33 operator + (const Matrix33 &v) const;
  138. //---------------------------
  139. // Component-wise subtraction
  140. //---------------------------
  141. const Matrix33 & operator -= (const Matrix33 &v);
  142. const Matrix33 & operator -= (T a);
  143. Matrix33 operator - (const Matrix33 &v) const;
  144. //------------------------------------
  145. // Component-wise multiplication by -1
  146. //------------------------------------
  147. Matrix33 operator - () const;
  148. const Matrix33 & negate ();
  149. //------------------------------
  150. // Component-wise multiplication
  151. //------------------------------
  152. const Matrix33 & operator *= (T a);
  153. Matrix33 operator * (T a) const;
  154. //-----------------------------------
  155. // Matrix-times-matrix multiplication
  156. //-----------------------------------
  157. const Matrix33 & operator *= (const Matrix33 &v);
  158. Matrix33 operator * (const Matrix33 &v) const;
  159. //-----------------------------------------------------------------
  160. // Vector-times-matrix multiplication; see also the "operator *"
  161. // functions defined below.
  162. //
  163. // m.multVecMatrix(src,dst) implements a homogeneous transformation
  164. // by computing Vec3 (src.x, src.y, 1) * m and dividing by the
  165. // result's third element.
  166. //
  167. // m.multDirMatrix(src,dst) multiplies src by the upper left 2x2
  168. // submatrix, ignoring the rest of matrix m.
  169. //-----------------------------------------------------------------
  170. template <class S>
  171. void multVecMatrix(const Vec2<S> &src, Vec2<S> &dst) const;
  172. template <class S>
  173. void multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const;
  174. //------------------------
  175. // Component-wise division
  176. //------------------------
  177. const Matrix33 & operator /= (T a);
  178. Matrix33 operator / (T a) const;
  179. //------------------
  180. // Transposed matrix
  181. //------------------
  182. const Matrix33 & transpose ();
  183. Matrix33 transposed () const;
  184. //------------------------------------------------------------
  185. // Inverse matrix: If singExc is false, inverting a singular
  186. // matrix produces an identity matrix. If singExc is true,
  187. // inverting a singular matrix throws a SingMatrixExc.
  188. //
  189. // inverse() and invert() invert matrices using determinants;
  190. // gjInverse() and gjInvert() use the Gauss-Jordan method.
  191. //
  192. // inverse() and invert() are significantly faster than
  193. // gjInverse() and gjInvert(), but the results may be slightly
  194. // less accurate.
  195. //
  196. //------------------------------------------------------------
  197. const Matrix33 & invert (bool singExc = false);
  198. Matrix33<T> inverse (bool singExc = false) const;
  199. const Matrix33 & gjInvert (bool singExc = false);
  200. Matrix33<T> gjInverse (bool singExc = false) const;
  201. //------------------------------------------------
  202. // Calculate the matrix minor of the (r,c) element
  203. //------------------------------------------------
  204. T minorOf (const int r, const int c) const;
  205. //---------------------------------------------------
  206. // Build a minor using the specified rows and columns
  207. //---------------------------------------------------
  208. T fastMinor (const int r0, const int r1,
  209. const int c0, const int c1) const;
  210. //------------
  211. // Determinant
  212. //------------
  213. T determinant() const;
  214. //-----------------------------------------
  215. // Set matrix to rotation by r (in radians)
  216. //-----------------------------------------
  217. template <class S>
  218. const Matrix33 & setRotation (S r);
  219. //-----------------------------
  220. // Rotate the given matrix by r
  221. //-----------------------------
  222. template <class S>
  223. const Matrix33 & rotate (S r);
  224. //--------------------------------------------
  225. // Set matrix to scale by given uniform factor
  226. //--------------------------------------------
  227. const Matrix33 & setScale (T s);
  228. //------------------------------------
  229. // Set matrix to scale by given vector
  230. //------------------------------------
  231. template <class S>
  232. const Matrix33 & setScale (const Vec2<S> &s);
  233. //----------------------
  234. // Scale the matrix by s
  235. //----------------------
  236. template <class S>
  237. const Matrix33 & scale (const Vec2<S> &s);
  238. //------------------------------------------
  239. // Set matrix to translation by given vector
  240. //------------------------------------------
  241. template <class S>
  242. const Matrix33 & setTranslation (const Vec2<S> &t);
  243. //-----------------------------
  244. // Return translation component
  245. //-----------------------------
  246. Vec2<T> translation () const;
  247. //--------------------------
  248. // Translate the matrix by t
  249. //--------------------------
  250. template <class S>
  251. const Matrix33 & translate (const Vec2<S> &t);
  252. //-----------------------------------------------------------
  253. // Set matrix to shear x for each y coord. by given factor xy
  254. //-----------------------------------------------------------
  255. template <class S>
  256. const Matrix33 & setShear (const S &h);
  257. //-------------------------------------------------------------
  258. // Set matrix to shear x for each y coord. by given factor h[0]
  259. // and to shear y for each x coord. by given factor h[1]
  260. //-------------------------------------------------------------
  261. template <class S>
  262. const Matrix33 & setShear (const Vec2<S> &h);
  263. //-----------------------------------------------------------
  264. // Shear the matrix in x for each y coord. by given factor xy
  265. //-----------------------------------------------------------
  266. template <class S>
  267. const Matrix33 & shear (const S &xy);
  268. //-----------------------------------------------------------
  269. // Shear the matrix in x for each y coord. by given factor xy
  270. // and shear y for each x coord. by given factor yx
  271. //-----------------------------------------------------------
  272. template <class S>
  273. const Matrix33 & shear (const Vec2<S> &h);
  274. //--------------------------------------------------------
  275. // Number of the row and column dimensions, since
  276. // Matrix33 is a square matrix.
  277. //--------------------------------------------------------
  278. static unsigned int dimensions() {return 3;}
  279. //-------------------------------------------------
  280. // Limitations of type T (see also class limits<T>)
  281. //-------------------------------------------------
  282. static T baseTypeMin() {return limits<T>::min();}
  283. static T baseTypeMax() {return limits<T>::max();}
  284. static T baseTypeSmallest() {return limits<T>::smallest();}
  285. static T baseTypeEpsilon() {return limits<T>::epsilon();}
  286. typedef T BaseType;
  287. typedef Vec3<T> BaseVecType;
  288. private:
  289. template <typename R, typename S>
  290. struct isSameType
  291. {
  292. enum {value = 0};
  293. };
  294. template <typename R>
  295. struct isSameType<R, R>
  296. {
  297. enum {value = 1};
  298. };
  299. };
  300. template <class T> class Matrix44
  301. {
  302. public:
  303. //-------------------
  304. // Access to elements
  305. //-------------------
  306. T x[4][4];
  307. T * operator [] (int i);
  308. const T * operator [] (int i) const;
  309. //-------------
  310. // Constructors
  311. //-------------
  312. Matrix44 (Uninitialized) {}
  313. Matrix44 ();
  314. // 1 0 0 0
  315. // 0 1 0 0
  316. // 0 0 1 0
  317. // 0 0 0 1
  318. Matrix44 (T a);
  319. // a a a a
  320. // a a a a
  321. // a a a a
  322. // a a a a
  323. Matrix44 (const T a[4][4]) ;
  324. // a[0][0] a[0][1] a[0][2] a[0][3]
  325. // a[1][0] a[1][1] a[1][2] a[1][3]
  326. // a[2][0] a[2][1] a[2][2] a[2][3]
  327. // a[3][0] a[3][1] a[3][2] a[3][3]
  328. Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h,
  329. T i, T j, T k, T l, T m, T n, T o, T p);
  330. // a b c d
  331. // e f g h
  332. // i j k l
  333. // m n o p
  334. Matrix44 (Matrix33<T> r, Vec3<T> t);
  335. // r r r 0
  336. // r r r 0
  337. // r r r 0
  338. // t t t 1
  339. //--------------------------------
  340. // Copy constructor and assignment
  341. //--------------------------------
  342. Matrix44 (const Matrix44 &v);
  343. template <class S> explicit Matrix44 (const Matrix44<S> &v);
  344. const Matrix44 & operator = (const Matrix44 &v);
  345. const Matrix44 & operator = (T a);
  346. //----------------------
  347. // Compatibility with Sb
  348. //----------------------
  349. T * getValue ();
  350. const T * getValue () const;
  351. template <class S>
  352. void getValue (Matrix44<S> &v) const;
  353. template <class S>
  354. Matrix44 & setValue (const Matrix44<S> &v);
  355. template <class S>
  356. Matrix44 & setTheMatrix (const Matrix44<S> &v);
  357. //---------
  358. // Identity
  359. //---------
  360. void makeIdentity();
  361. //---------
  362. // Equality
  363. //---------
  364. bool operator == (const Matrix44 &v) const;
  365. bool operator != (const Matrix44 &v) const;
  366. //-----------------------------------------------------------------------
  367. // Compare two matrices and test if they are "approximately equal":
  368. //
  369. // equalWithAbsError (m, e)
  370. //
  371. // Returns true if the coefficients of this and m are the same with
  372. // an absolute error of no more than e, i.e., for all i, j
  373. //
  374. // abs (this[i][j] - m[i][j]) <= e
  375. //
  376. // equalWithRelError (m, e)
  377. //
  378. // Returns true if the coefficients of this and m are the same with
  379. // a relative error of no more than e, i.e., for all i, j
  380. //
  381. // abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  382. //-----------------------------------------------------------------------
  383. bool equalWithAbsError (const Matrix44<T> &v, T e) const;
  384. bool equalWithRelError (const Matrix44<T> &v, T e) const;
  385. //------------------------
  386. // Component-wise addition
  387. //------------------------
  388. const Matrix44 & operator += (const Matrix44 &v);
  389. const Matrix44 & operator += (T a);
  390. Matrix44 operator + (const Matrix44 &v) const;
  391. //---------------------------
  392. // Component-wise subtraction
  393. //---------------------------
  394. const Matrix44 & operator -= (const Matrix44 &v);
  395. const Matrix44 & operator -= (T a);
  396. Matrix44 operator - (const Matrix44 &v) const;
  397. //------------------------------------
  398. // Component-wise multiplication by -1
  399. //------------------------------------
  400. Matrix44 operator - () const;
  401. const Matrix44 & negate ();
  402. //------------------------------
  403. // Component-wise multiplication
  404. //------------------------------
  405. const Matrix44 & operator *= (T a);
  406. Matrix44 operator * (T a) const;
  407. //-----------------------------------
  408. // Matrix-times-matrix multiplication
  409. //-----------------------------------
  410. const Matrix44 & operator *= (const Matrix44 &v);
  411. Matrix44 operator * (const Matrix44 &v) const;
  412. static void multiply (const Matrix44 &a, // assumes that
  413. const Matrix44 &b, // &a != &c and
  414. Matrix44 &c); // &b != &c.
  415. //-----------------------------------------------------------------
  416. // Vector-times-matrix multiplication; see also the "operator *"
  417. // functions defined below.
  418. //
  419. // m.multVecMatrix(src,dst) implements a homogeneous transformation
  420. // by computing Vec4 (src.x, src.y, src.z, 1) * m and dividing by
  421. // the result's third element.
  422. //
  423. // m.multDirMatrix(src,dst) multiplies src by the upper left 3x3
  424. // submatrix, ignoring the rest of matrix m.
  425. //-----------------------------------------------------------------
  426. template <class S>
  427. void multVecMatrix(const Vec3<S> &src, Vec3<S> &dst) const;
  428. template <class S>
  429. void multDirMatrix(const Vec3<S> &src, Vec3<S> &dst) const;
  430. //------------------------
  431. // Component-wise division
  432. //------------------------
  433. const Matrix44 & operator /= (T a);
  434. Matrix44 operator / (T a) const;
  435. //------------------
  436. // Transposed matrix
  437. //------------------
  438. const Matrix44 & transpose ();
  439. Matrix44 transposed () const;
  440. //------------------------------------------------------------
  441. // Inverse matrix: If singExc is false, inverting a singular
  442. // matrix produces an identity matrix. If singExc is true,
  443. // inverting a singular matrix throws a SingMatrixExc.
  444. //
  445. // inverse() and invert() invert matrices using determinants;
  446. // gjInverse() and gjInvert() use the Gauss-Jordan method.
  447. //
  448. // inverse() and invert() are significantly faster than
  449. // gjInverse() and gjInvert(), but the results may be slightly
  450. // less accurate.
  451. //
  452. //------------------------------------------------------------
  453. const Matrix44 & invert (bool singExc = false);
  454. Matrix44<T> inverse (bool singExc = false) const;
  455. const Matrix44 & gjInvert (bool singExc = false);
  456. Matrix44<T> gjInverse (bool singExc = false) const;
  457. //------------------------------------------------
  458. // Calculate the matrix minor of the (r,c) element
  459. //------------------------------------------------
  460. T minorOf (const int r, const int c) const;
  461. //---------------------------------------------------
  462. // Build a minor using the specified rows and columns
  463. //---------------------------------------------------
  464. T fastMinor (const int r0, const int r1, const int r2,
  465. const int c0, const int c1, const int c2) const;
  466. //------------
  467. // Determinant
  468. //------------
  469. T determinant() const;
  470. //--------------------------------------------------------
  471. // Set matrix to rotation by XYZ euler angles (in radians)
  472. //--------------------------------------------------------
  473. template <class S>
  474. const Matrix44 & setEulerAngles (const Vec3<S>& r);
  475. //--------------------------------------------------------
  476. // Set matrix to rotation around given axis by given angle
  477. //--------------------------------------------------------
  478. template <class S>
  479. const Matrix44 & setAxisAngle (const Vec3<S>& ax, S ang);
  480. //-------------------------------------------
  481. // Rotate the matrix by XYZ euler angles in r
  482. //-------------------------------------------
  483. template <class S>
  484. const Matrix44 & rotate (const Vec3<S> &r);
  485. //--------------------------------------------
  486. // Set matrix to scale by given uniform factor
  487. //--------------------------------------------
  488. const Matrix44 & setScale (T s);
  489. //------------------------------------
  490. // Set matrix to scale by given vector
  491. //------------------------------------
  492. template <class S>
  493. const Matrix44 & setScale (const Vec3<S> &s);
  494. //----------------------
  495. // Scale the matrix by s
  496. //----------------------
  497. template <class S>
  498. const Matrix44 & scale (const Vec3<S> &s);
  499. //------------------------------------------
  500. // Set matrix to translation by given vector
  501. //------------------------------------------
  502. template <class S>
  503. const Matrix44 & setTranslation (const Vec3<S> &t);
  504. //-----------------------------
  505. // Return translation component
  506. //-----------------------------
  507. const Vec3<T> translation () const;
  508. //--------------------------
  509. // Translate the matrix by t
  510. //--------------------------
  511. template <class S>
  512. const Matrix44 & translate (const Vec3<S> &t);
  513. //-------------------------------------------------------------
  514. // Set matrix to shear by given vector h. The resulting matrix
  515. // will shear x for each y coord. by a factor of h[0] ;
  516. // will shear x for each z coord. by a factor of h[1] ;
  517. // will shear y for each z coord. by a factor of h[2] .
  518. //-------------------------------------------------------------
  519. template <class S>
  520. const Matrix44 & setShear (const Vec3<S> &h);
  521. //------------------------------------------------------------
  522. // Set matrix to shear by given factors. The resulting matrix
  523. // will shear x for each y coord. by a factor of h.xy ;
  524. // will shear x for each z coord. by a factor of h.xz ;
  525. // will shear y for each z coord. by a factor of h.yz ;
  526. // will shear y for each x coord. by a factor of h.yx ;
  527. // will shear z for each x coord. by a factor of h.zx ;
  528. // will shear z for each y coord. by a factor of h.zy .
  529. //------------------------------------------------------------
  530. template <class S>
  531. const Matrix44 & setShear (const Shear6<S> &h);
  532. //--------------------------------------------------------
  533. // Shear the matrix by given vector. The composed matrix
  534. // will be <shear> * <this>, where the shear matrix ...
  535. // will shear x for each y coord. by a factor of h[0] ;
  536. // will shear x for each z coord. by a factor of h[1] ;
  537. // will shear y for each z coord. by a factor of h[2] .
  538. //--------------------------------------------------------
  539. template <class S>
  540. const Matrix44 & shear (const Vec3<S> &h);
  541. //--------------------------------------------------------
  542. // Number of the row and column dimensions, since
  543. // Matrix44 is a square matrix.
  544. //--------------------------------------------------------
  545. static unsigned int dimensions() {return 4;}
  546. //------------------------------------------------------------
  547. // Shear the matrix by the given factors. The composed matrix
  548. // will be <shear> * <this>, where the shear matrix ...
  549. // will shear x for each y coord. by a factor of h.xy ;
  550. // will shear x for each z coord. by a factor of h.xz ;
  551. // will shear y for each z coord. by a factor of h.yz ;
  552. // will shear y for each x coord. by a factor of h.yx ;
  553. // will shear z for each x coord. by a factor of h.zx ;
  554. // will shear z for each y coord. by a factor of h.zy .
  555. //------------------------------------------------------------
  556. template <class S>
  557. const Matrix44 & shear (const Shear6<S> &h);
  558. //-------------------------------------------------
  559. // Limitations of type T (see also class limits<T>)
  560. //-------------------------------------------------
  561. static T baseTypeMin() {return limits<T>::min();}
  562. static T baseTypeMax() {return limits<T>::max();}
  563. static T baseTypeSmallest() {return limits<T>::smallest();}
  564. static T baseTypeEpsilon() {return limits<T>::epsilon();}
  565. typedef T BaseType;
  566. typedef Vec4<T> BaseVecType;
  567. private:
  568. template <typename R, typename S>
  569. struct isSameType
  570. {
  571. enum {value = 0};
  572. };
  573. template <typename R>
  574. struct isSameType<R, R>
  575. {
  576. enum {value = 1};
  577. };
  578. };
  579. //--------------
  580. // Stream output
  581. //--------------
  582. template <class T>
  583. std::ostream & operator << (std::ostream & s, const Matrix33<T> &m);
  584. template <class T>
  585. std::ostream & operator << (std::ostream & s, const Matrix44<T> &m);
  586. //---------------------------------------------
  587. // Vector-times-matrix multiplication operators
  588. //---------------------------------------------
  589. template <class S, class T>
  590. const Vec2<S> & operator *= (Vec2<S> &v, const Matrix33<T> &m);
  591. template <class S, class T>
  592. Vec2<S> operator * (const Vec2<S> &v, const Matrix33<T> &m);
  593. template <class S, class T>
  594. const Vec3<S> & operator *= (Vec3<S> &v, const Matrix33<T> &m);
  595. template <class S, class T>
  596. Vec3<S> operator * (const Vec3<S> &v, const Matrix33<T> &m);
  597. template <class S, class T>
  598. const Vec3<S> & operator *= (Vec3<S> &v, const Matrix44<T> &m);
  599. template <class S, class T>
  600. Vec3<S> operator * (const Vec3<S> &v, const Matrix44<T> &m);
  601. template <class S, class T>
  602. const Vec4<S> & operator *= (Vec4<S> &v, const Matrix44<T> &m);
  603. template <class S, class T>
  604. Vec4<S> operator * (const Vec4<S> &v, const Matrix44<T> &m);
  605. //-------------------------
  606. // Typedefs for convenience
  607. //-------------------------
  608. typedef Matrix33 <float> M33f;
  609. typedef Matrix33 <double> M33d;
  610. typedef Matrix44 <float> M44f;
  611. typedef Matrix44 <double> M44d;
  612. //---------------------------
  613. // Implementation of Matrix33
  614. //---------------------------
  615. template <class T>
  616. inline T *
  617. Matrix33<T>::operator [] (int i)
  618. {
  619. return x[i];
  620. }
  621. template <class T>
  622. inline const T *
  623. Matrix33<T>::operator [] (int i) const
  624. {
  625. return x[i];
  626. }
  627. template <class T>
  628. inline
  629. Matrix33<T>::Matrix33 ()
  630. {
  631. memset (x, 0, sizeof (x));
  632. x[0][0] = 1;
  633. x[1][1] = 1;
  634. x[2][2] = 1;
  635. }
  636. template <class T>
  637. inline
  638. Matrix33<T>::Matrix33 (T a)
  639. {
  640. x[0][0] = a;
  641. x[0][1] = a;
  642. x[0][2] = a;
  643. x[1][0] = a;
  644. x[1][1] = a;
  645. x[1][2] = a;
  646. x[2][0] = a;
  647. x[2][1] = a;
  648. x[2][2] = a;
  649. }
  650. template <class T>
  651. inline
  652. Matrix33<T>::Matrix33 (const T a[3][3])
  653. {
  654. memcpy (x, a, sizeof (x));
  655. }
  656. template <class T>
  657. inline
  658. Matrix33<T>::Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i)
  659. {
  660. x[0][0] = a;
  661. x[0][1] = b;
  662. x[0][2] = c;
  663. x[1][0] = d;
  664. x[1][1] = e;
  665. x[1][2] = f;
  666. x[2][0] = g;
  667. x[2][1] = h;
  668. x[2][2] = i;
  669. }
  670. template <class T>
  671. inline
  672. Matrix33<T>::Matrix33 (const Matrix33 &v)
  673. {
  674. memcpy (x, v.x, sizeof (x));
  675. }
  676. template <class T>
  677. template <class S>
  678. inline
  679. Matrix33<T>::Matrix33 (const Matrix33<S> &v)
  680. {
  681. x[0][0] = T (v.x[0][0]);
  682. x[0][1] = T (v.x[0][1]);
  683. x[0][2] = T (v.x[0][2]);
  684. x[1][0] = T (v.x[1][0]);
  685. x[1][1] = T (v.x[1][1]);
  686. x[1][2] = T (v.x[1][2]);
  687. x[2][0] = T (v.x[2][0]);
  688. x[2][1] = T (v.x[2][1]);
  689. x[2][2] = T (v.x[2][2]);
  690. }
  691. template <class T>
  692. inline const Matrix33<T> &
  693. Matrix33<T>::operator = (const Matrix33 &v)
  694. {
  695. memcpy (x, v.x, sizeof (x));
  696. return *this;
  697. }
  698. template <class T>
  699. inline const Matrix33<T> &
  700. Matrix33<T>::operator = (T a)
  701. {
  702. x[0][0] = a;
  703. x[0][1] = a;
  704. x[0][2] = a;
  705. x[1][0] = a;
  706. x[1][1] = a;
  707. x[1][2] = a;
  708. x[2][0] = a;
  709. x[2][1] = a;
  710. x[2][2] = a;
  711. return *this;
  712. }
  713. template <class T>
  714. inline T *
  715. Matrix33<T>::getValue ()
  716. {
  717. return (T *) &x[0][0];
  718. }
  719. template <class T>
  720. inline const T *
  721. Matrix33<T>::getValue () const
  722. {
  723. return (const T *) &x[0][0];
  724. }
  725. template <class T>
  726. template <class S>
  727. inline void
  728. Matrix33<T>::getValue (Matrix33<S> &v) const
  729. {
  730. if (isSameType<S,T>::value)
  731. {
  732. memcpy (v.x, x, sizeof (x));
  733. }
  734. else
  735. {
  736. v.x[0][0] = x[0][0];
  737. v.x[0][1] = x[0][1];
  738. v.x[0][2] = x[0][2];
  739. v.x[1][0] = x[1][0];
  740. v.x[1][1] = x[1][1];
  741. v.x[1][2] = x[1][2];
  742. v.x[2][0] = x[2][0];
  743. v.x[2][1] = x[2][1];
  744. v.x[2][2] = x[2][2];
  745. }
  746. }
  747. template <class T>
  748. template <class S>
  749. inline Matrix33<T> &
  750. Matrix33<T>::setValue (const Matrix33<S> &v)
  751. {
  752. if (isSameType<S,T>::value)
  753. {
  754. memcpy (x, v.x, sizeof (x));
  755. }
  756. else
  757. {
  758. x[0][0] = v.x[0][0];
  759. x[0][1] = v.x[0][1];
  760. x[0][2] = v.x[0][2];
  761. x[1][0] = v.x[1][0];
  762. x[1][1] = v.x[1][1];
  763. x[1][2] = v.x[1][2];
  764. x[2][0] = v.x[2][0];
  765. x[2][1] = v.x[2][1];
  766. x[2][2] = v.x[2][2];
  767. }
  768. return *this;
  769. }
  770. template <class T>
  771. template <class S>
  772. inline Matrix33<T> &
  773. Matrix33<T>::setTheMatrix (const Matrix33<S> &v)
  774. {
  775. if (isSameType<S,T>::value)
  776. {
  777. memcpy (x, v.x, sizeof (x));
  778. }
  779. else
  780. {
  781. x[0][0] = v.x[0][0];
  782. x[0][1] = v.x[0][1];
  783. x[0][2] = v.x[0][2];
  784. x[1][0] = v.x[1][0];
  785. x[1][1] = v.x[1][1];
  786. x[1][2] = v.x[1][2];
  787. x[2][0] = v.x[2][0];
  788. x[2][1] = v.x[2][1];
  789. x[2][2] = v.x[2][2];
  790. }
  791. return *this;
  792. }
  793. template <class T>
  794. inline void
  795. Matrix33<T>::makeIdentity()
  796. {
  797. memset (x, 0, sizeof (x));
  798. x[0][0] = 1;
  799. x[1][1] = 1;
  800. x[2][2] = 1;
  801. }
  802. template <class T>
  803. bool
  804. Matrix33<T>::operator == (const Matrix33 &v) const
  805. {
  806. return x[0][0] == v.x[0][0] &&
  807. x[0][1] == v.x[0][1] &&
  808. x[0][2] == v.x[0][2] &&
  809. x[1][0] == v.x[1][0] &&
  810. x[1][1] == v.x[1][1] &&
  811. x[1][2] == v.x[1][2] &&
  812. x[2][0] == v.x[2][0] &&
  813. x[2][1] == v.x[2][1] &&
  814. x[2][2] == v.x[2][2];
  815. }
  816. template <class T>
  817. bool
  818. Matrix33<T>::operator != (const Matrix33 &v) const
  819. {
  820. return x[0][0] != v.x[0][0] ||
  821. x[0][1] != v.x[0][1] ||
  822. x[0][2] != v.x[0][2] ||
  823. x[1][0] != v.x[1][0] ||
  824. x[1][1] != v.x[1][1] ||
  825. x[1][2] != v.x[1][2] ||
  826. x[2][0] != v.x[2][0] ||
  827. x[2][1] != v.x[2][1] ||
  828. x[2][2] != v.x[2][2];
  829. }
  830. template <class T>
  831. bool
  832. Matrix33<T>::equalWithAbsError (const Matrix33<T> &m, T e) const
  833. {
  834. for (int i = 0; i < 3; i++)
  835. for (int j = 0; j < 3; j++)
  836. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i][j], m[i][j], e))
  837. return false;
  838. return true;
  839. }
  840. template <class T>
  841. bool
  842. Matrix33<T>::equalWithRelError (const Matrix33<T> &m, T e) const
  843. {
  844. for (int i = 0; i < 3; i++)
  845. for (int j = 0; j < 3; j++)
  846. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i][j], m[i][j], e))
  847. return false;
  848. return true;
  849. }
  850. template <class T>
  851. const Matrix33<T> &
  852. Matrix33<T>::operator += (const Matrix33<T> &v)
  853. {
  854. x[0][0] += v.x[0][0];
  855. x[0][1] += v.x[0][1];
  856. x[0][2] += v.x[0][2];
  857. x[1][0] += v.x[1][0];
  858. x[1][1] += v.x[1][1];
  859. x[1][2] += v.x[1][2];
  860. x[2][0] += v.x[2][0];
  861. x[2][1] += v.x[2][1];
  862. x[2][2] += v.x[2][2];
  863. return *this;
  864. }
  865. template <class T>
  866. const Matrix33<T> &
  867. Matrix33<T>::operator += (T a)
  868. {
  869. x[0][0] += a;
  870. x[0][1] += a;
  871. x[0][2] += a;
  872. x[1][0] += a;
  873. x[1][1] += a;
  874. x[1][2] += a;
  875. x[2][0] += a;
  876. x[2][1] += a;
  877. x[2][2] += a;
  878. return *this;
  879. }
  880. template <class T>
  881. Matrix33<T>
  882. Matrix33<T>::operator + (const Matrix33<T> &v) const
  883. {
  884. return Matrix33 (x[0][0] + v.x[0][0],
  885. x[0][1] + v.x[0][1],
  886. x[0][2] + v.x[0][2],
  887. x[1][0] + v.x[1][0],
  888. x[1][1] + v.x[1][1],
  889. x[1][2] + v.x[1][2],
  890. x[2][0] + v.x[2][0],
  891. x[2][1] + v.x[2][1],
  892. x[2][2] + v.x[2][2]);
  893. }
  894. template <class T>
  895. const Matrix33<T> &
  896. Matrix33<T>::operator -= (const Matrix33<T> &v)
  897. {
  898. x[0][0] -= v.x[0][0];
  899. x[0][1] -= v.x[0][1];
  900. x[0][2] -= v.x[0][2];
  901. x[1][0] -= v.x[1][0];
  902. x[1][1] -= v.x[1][1];
  903. x[1][2] -= v.x[1][2];
  904. x[2][0] -= v.x[2][0];
  905. x[2][1] -= v.x[2][1];
  906. x[2][2] -= v.x[2][2];
  907. return *this;
  908. }
  909. template <class T>
  910. const Matrix33<T> &
  911. Matrix33<T>::operator -= (T a)
  912. {
  913. x[0][0] -= a;
  914. x[0][1] -= a;
  915. x[0][2] -= a;
  916. x[1][0] -= a;
  917. x[1][1] -= a;
  918. x[1][2] -= a;
  919. x[2][0] -= a;
  920. x[2][1] -= a;
  921. x[2][2] -= a;
  922. return *this;
  923. }
  924. template <class T>
  925. Matrix33<T>
  926. Matrix33<T>::operator - (const Matrix33<T> &v) const
  927. {
  928. return Matrix33 (x[0][0] - v.x[0][0],
  929. x[0][1] - v.x[0][1],
  930. x[0][2] - v.x[0][2],
  931. x[1][0] - v.x[1][0],
  932. x[1][1] - v.x[1][1],
  933. x[1][2] - v.x[1][2],
  934. x[2][0] - v.x[2][0],
  935. x[2][1] - v.x[2][1],
  936. x[2][2] - v.x[2][2]);
  937. }
  938. template <class T>
  939. Matrix33<T>
  940. Matrix33<T>::operator - () const
  941. {
  942. return Matrix33 (-x[0][0],
  943. -x[0][1],
  944. -x[0][2],
  945. -x[1][0],
  946. -x[1][1],
  947. -x[1][2],
  948. -x[2][0],
  949. -x[2][1],
  950. -x[2][2]);
  951. }
  952. template <class T>
  953. const Matrix33<T> &
  954. Matrix33<T>::negate ()
  955. {
  956. x[0][0] = -x[0][0];
  957. x[0][1] = -x[0][1];
  958. x[0][2] = -x[0][2];
  959. x[1][0] = -x[1][0];
  960. x[1][1] = -x[1][1];
  961. x[1][2] = -x[1][2];
  962. x[2][0] = -x[2][0];
  963. x[2][1] = -x[2][1];
  964. x[2][2] = -x[2][2];
  965. return *this;
  966. }
  967. template <class T>
  968. const Matrix33<T> &
  969. Matrix33<T>::operator *= (T a)
  970. {
  971. x[0][0] *= a;
  972. x[0][1] *= a;
  973. x[0][2] *= a;
  974. x[1][0] *= a;
  975. x[1][1] *= a;
  976. x[1][2] *= a;
  977. x[2][0] *= a;
  978. x[2][1] *= a;
  979. x[2][2] *= a;
  980. return *this;
  981. }
  982. template <class T>
  983. Matrix33<T>
  984. Matrix33<T>::operator * (T a) const
  985. {
  986. return Matrix33 (x[0][0] * a,
  987. x[0][1] * a,
  988. x[0][2] * a,
  989. x[1][0] * a,
  990. x[1][1] * a,
  991. x[1][2] * a,
  992. x[2][0] * a,
  993. x[2][1] * a,
  994. x[2][2] * a);
  995. }
  996. template <class T>
  997. inline Matrix33<T>
  998. operator * (T a, const Matrix33<T> &v)
  999. {
  1000. return v * a;
  1001. }
  1002. template <class T>
  1003. const Matrix33<T> &
  1004. Matrix33<T>::operator *= (const Matrix33<T> &v)
  1005. {
  1006. Matrix33 tmp (T (0));
  1007. for (int i = 0; i < 3; i++)
  1008. for (int j = 0; j < 3; j++)
  1009. for (int k = 0; k < 3; k++)
  1010. tmp.x[i][j] += x[i][k] * v.x[k][j];
  1011. *this = tmp;
  1012. return *this;
  1013. }
  1014. template <class T>
  1015. Matrix33<T>
  1016. Matrix33<T>::operator * (const Matrix33<T> &v) const
  1017. {
  1018. Matrix33 tmp (T (0));
  1019. for (int i = 0; i < 3; i++)
  1020. for (int j = 0; j < 3; j++)
  1021. for (int k = 0; k < 3; k++)
  1022. tmp.x[i][j] += x[i][k] * v.x[k][j];
  1023. return tmp;
  1024. }
  1025. template <class T>
  1026. template <class S>
  1027. void
  1028. Matrix33<T>::multVecMatrix(const Vec2<S> &src, Vec2<S> &dst) const
  1029. {
  1030. S a, b, w;
  1031. a = src[0] * x[0][0] + src[1] * x[1][0] + x[2][0];
  1032. b = src[0] * x[0][1] + src[1] * x[1][1] + x[2][1];
  1033. w = src[0] * x[0][2] + src[1] * x[1][2] + x[2][2];
  1034. dst.x = a / w;
  1035. dst.y = b / w;
  1036. }
  1037. template <class T>
  1038. template <class S>
  1039. void
  1040. Matrix33<T>::multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const
  1041. {
  1042. S a, b;
  1043. a = src[0] * x[0][0] + src[1] * x[1][0];
  1044. b = src[0] * x[0][1] + src[1] * x[1][1];
  1045. dst.x = a;
  1046. dst.y = b;
  1047. }
  1048. template <class T>
  1049. const Matrix33<T> &
  1050. Matrix33<T>::operator /= (T a)
  1051. {
  1052. x[0][0] /= a;
  1053. x[0][1] /= a;
  1054. x[0][2] /= a;
  1055. x[1][0] /= a;
  1056. x[1][1] /= a;
  1057. x[1][2] /= a;
  1058. x[2][0] /= a;
  1059. x[2][1] /= a;
  1060. x[2][2] /= a;
  1061. return *this;
  1062. }
  1063. template <class T>
  1064. Matrix33<T>
  1065. Matrix33<T>::operator / (T a) const
  1066. {
  1067. return Matrix33 (x[0][0] / a,
  1068. x[0][1] / a,
  1069. x[0][2] / a,
  1070. x[1][0] / a,
  1071. x[1][1] / a,
  1072. x[1][2] / a,
  1073. x[2][0] / a,
  1074. x[2][1] / a,
  1075. x[2][2] / a);
  1076. }
  1077. template <class T>
  1078. const Matrix33<T> &
  1079. Matrix33<T>::transpose ()
  1080. {
  1081. Matrix33 tmp (x[0][0],
  1082. x[1][0],
  1083. x[2][0],
  1084. x[0][1],
  1085. x[1][1],
  1086. x[2][1],
  1087. x[0][2],
  1088. x[1][2],
  1089. x[2][2]);
  1090. *this = tmp;
  1091. return *this;
  1092. }
  1093. template <class T>
  1094. Matrix33<T>
  1095. Matrix33<T>::transposed () const
  1096. {
  1097. return Matrix33 (x[0][0],
  1098. x[1][0],
  1099. x[2][0],
  1100. x[0][1],
  1101. x[1][1],
  1102. x[2][1],
  1103. x[0][2],
  1104. x[1][2],
  1105. x[2][2]);
  1106. }
  1107. template <class T>
  1108. const Matrix33<T> &
  1109. Matrix33<T>::gjInvert (bool singExc)
  1110. {
  1111. *this = gjInverse (singExc);
  1112. return *this;
  1113. }
  1114. template <class T>
  1115. Matrix33<T>
  1116. Matrix33<T>::gjInverse (bool singExc) const
  1117. {
  1118. int i, j, k;
  1119. Matrix33 s;
  1120. Matrix33 t (*this);
  1121. // Forward elimination
  1122. for (i = 0; i < 2 ; i++)
  1123. {
  1124. int pivot = i;
  1125. T pivotsize = t[i][i];
  1126. if (pivotsize < 0)
  1127. pivotsize = -pivotsize;
  1128. for (j = i + 1; j < 3; j++)
  1129. {
  1130. T tmp = t[j][i];
  1131. if (tmp < 0)
  1132. tmp = -tmp;
  1133. if (tmp > pivotsize)
  1134. {
  1135. pivot = j;
  1136. pivotsize = tmp;
  1137. }
  1138. }
  1139. if (pivotsize == 0)
  1140. {
  1141. if (singExc)
  1142. throw ::IMATH_INTERNAL_NAMESPACE::SingMatrixExc ("Cannot invert singular matrix.");
  1143. return Matrix33();
  1144. }
  1145. if (pivot != i)
  1146. {
  1147. for (j = 0; j < 3; j++)
  1148. {
  1149. T tmp;
  1150. tmp = t[i][j];
  1151. t[i][j] = t[pivot][j];
  1152. t[pivot][j] = tmp;
  1153. tmp = s[i][j];
  1154. s[i][j] = s[pivot][j];
  1155. s[pivot][j] = tmp;
  1156. }
  1157. }
  1158. for (j = i + 1; j < 3; j++)
  1159. {
  1160. T f = t[j][i] / t[i][i];
  1161. for (k = 0; k < 3; k++)
  1162. {
  1163. t[j][k] -= f * t[i][k];
  1164. s[j][k] -= f * s[i][k];
  1165. }
  1166. }
  1167. }
  1168. // Backward substitution
  1169. for (i = 2; i >= 0; --i)
  1170. {
  1171. T f;
  1172. if ((f = t[i][i]) == 0)
  1173. {
  1174. if (singExc)
  1175. throw ::IMATH_INTERNAL_NAMESPACE::SingMatrixExc ("Cannot invert singular matrix.");
  1176. return Matrix33();
  1177. }
  1178. for (j = 0; j < 3; j++)
  1179. {
  1180. t[i][j] /= f;
  1181. s[i][j] /= f;
  1182. }
  1183. for (j = 0; j < i; j++)
  1184. {
  1185. f = t[j][i];
  1186. for (k = 0; k < 3; k++)
  1187. {
  1188. t[j][k] -= f * t[i][k];
  1189. s[j][k] -= f * s[i][k];
  1190. }
  1191. }
  1192. }
  1193. return s;
  1194. }
  1195. template <class T>
  1196. const Matrix33<T> &
  1197. Matrix33<T>::invert (bool singExc)
  1198. {
  1199. *this = inverse (singExc);
  1200. return *this;
  1201. }
  1202. template <class T>
  1203. Matrix33<T>
  1204. Matrix33<T>::inverse (bool singExc) const
  1205. {
  1206. if (x[0][2] != 0 || x[1][2] != 0 || x[2][2] != 1)
  1207. {
  1208. Matrix33 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
  1209. x[2][1] * x[0][2] - x[0][1] * x[2][2],
  1210. x[0][1] * x[1][2] - x[1][1] * x[0][2],
  1211. x[2][0] * x[1][2] - x[1][0] * x[2][2],
  1212. x[0][0] * x[2][2] - x[2][0] * x[0][2],
  1213. x[1][0] * x[0][2] - x[0][0] * x[1][2],
  1214. x[1][0] * x[2][1] - x[2][0] * x[1][1],
  1215. x[2][0] * x[0][1] - x[0][0] * x[2][1],
  1216. x[0][0] * x[1][1] - x[1][0] * x[0][1]);
  1217. T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
  1218. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  1219. {
  1220. for (int i = 0; i < 3; ++i)
  1221. {
  1222. for (int j = 0; j < 3; ++j)
  1223. {
  1224. s[i][j] /= r;
  1225. }
  1226. }
  1227. }
  1228. else
  1229. {
  1230. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / limits<T>::smallest();
  1231. for (int i = 0; i < 3; ++i)
  1232. {
  1233. for (int j = 0; j < 3; ++j)
  1234. {
  1235. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s[i][j]))
  1236. {
  1237. s[i][j] /= r;
  1238. }
  1239. else
  1240. {
  1241. if (singExc)
  1242. throw SingMatrixExc ("Cannot invert "
  1243. "singular matrix.");
  1244. return Matrix33();
  1245. }
  1246. }
  1247. }
  1248. }
  1249. return s;
  1250. }
  1251. else
  1252. {
  1253. Matrix33 s ( x[1][1],
  1254. -x[0][1],
  1255. 0,
  1256. -x[1][0],
  1257. x[0][0],
  1258. 0,
  1259. 0,
  1260. 0,
  1261. 1);
  1262. T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
  1263. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  1264. {
  1265. for (int i = 0; i < 2; ++i)
  1266. {
  1267. for (int j = 0; j < 2; ++j)
  1268. {
  1269. s[i][j] /= r;
  1270. }
  1271. }
  1272. }
  1273. else
  1274. {
  1275. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / limits<T>::smallest();
  1276. for (int i = 0; i < 2; ++i)
  1277. {
  1278. for (int j = 0; j < 2; ++j)
  1279. {
  1280. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s[i][j]))
  1281. {
  1282. s[i][j] /= r;
  1283. }
  1284. else
  1285. {
  1286. if (singExc)
  1287. throw SingMatrixExc ("Cannot invert "
  1288. "singular matrix.");
  1289. return Matrix33();
  1290. }
  1291. }
  1292. }
  1293. }
  1294. s[2][0] = -x[2][0] * s[0][0] - x[2][1] * s[1][0];
  1295. s[2][1] = -x[2][0] * s[0][1] - x[2][1] * s[1][1];
  1296. return s;
  1297. }
  1298. }
  1299. template <class T>
  1300. inline T
  1301. Matrix33<T>::minorOf (const int r, const int c) const
  1302. {
  1303. int r0 = 0 + (r < 1 ? 1 : 0);
  1304. int r1 = 1 + (r < 2 ? 1 : 0);
  1305. int c0 = 0 + (c < 1 ? 1 : 0);
  1306. int c1 = 1 + (c < 2 ? 1 : 0);
  1307. return x[r0][c0]*x[r1][c1] - x[r1][c0]*x[r0][c1];
  1308. }
  1309. template <class T>
  1310. inline T
  1311. Matrix33<T>::fastMinor( const int r0, const int r1,
  1312. const int c0, const int c1) const
  1313. {
  1314. return x[r0][c0]*x[r1][c1] - x[r0][c1]*x[r1][c0];
  1315. }
  1316. template <class T>
  1317. inline T
  1318. Matrix33<T>::determinant () const
  1319. {
  1320. return x[0][0]*(x[1][1]*x[2][2] - x[1][2]*x[2][1]) +
  1321. x[0][1]*(x[1][2]*x[2][0] - x[1][0]*x[2][2]) +
  1322. x[0][2]*(x[1][0]*x[2][1] - x[1][1]*x[2][0]);
  1323. }
  1324. template <class T>
  1325. template <class S>
  1326. const Matrix33<T> &
  1327. Matrix33<T>::setRotation (S r)
  1328. {
  1329. S cos_r, sin_r;
  1330. cos_r = Math<T>::cos (r);
  1331. sin_r = Math<T>::sin (r);
  1332. x[0][0] = cos_r;
  1333. x[0][1] = sin_r;
  1334. x[0][2] = 0;
  1335. x[1][0] = -sin_r;
  1336. x[1][1] = cos_r;
  1337. x[1][2] = 0;
  1338. x[2][0] = 0;
  1339. x[2][1] = 0;
  1340. x[2][2] = 1;
  1341. return *this;
  1342. }
  1343. template <class T>
  1344. template <class S>
  1345. const Matrix33<T> &
  1346. Matrix33<T>::rotate (S r)
  1347. {
  1348. *this *= Matrix33<T>().setRotation (r);
  1349. return *this;
  1350. }
  1351. template <class T>
  1352. const Matrix33<T> &
  1353. Matrix33<T>::setScale (T s)
  1354. {
  1355. memset (x, 0, sizeof (x));
  1356. x[0][0] = s;
  1357. x[1][1] = s;
  1358. x[2][2] = 1;
  1359. return *this;
  1360. }
  1361. template <class T>
  1362. template <class S>
  1363. const Matrix33<T> &
  1364. Matrix33<T>::setScale (const Vec2<S> &s)
  1365. {
  1366. memset (x, 0, sizeof (x));
  1367. x[0][0] = s[0];
  1368. x[1][1] = s[1];
  1369. x[2][2] = 1;
  1370. return *this;
  1371. }
  1372. template <class T>
  1373. template <class S>
  1374. const Matrix33<T> &
  1375. Matrix33<T>::scale (const Vec2<S> &s)
  1376. {
  1377. x[0][0] *= s[0];
  1378. x[0][1] *= s[0];
  1379. x[0][2] *= s[0];
  1380. x[1][0] *= s[1];
  1381. x[1][1] *= s[1];
  1382. x[1][2] *= s[1];
  1383. return *this;
  1384. }
  1385. template <class T>
  1386. template <class S>
  1387. const Matrix33<T> &
  1388. Matrix33<T>::setTranslation (const Vec2<S> &t)
  1389. {
  1390. x[0][0] = 1;
  1391. x[0][1] = 0;
  1392. x[0][2] = 0;
  1393. x[1][0] = 0;
  1394. x[1][1] = 1;
  1395. x[1][2] = 0;
  1396. x[2][0] = t[0];
  1397. x[2][1] = t[1];
  1398. x[2][2] = 1;
  1399. return *this;
  1400. }
  1401. template <class T>
  1402. inline Vec2<T>
  1403. Matrix33<T>::translation () const
  1404. {
  1405. return Vec2<T> (x[2][0], x[2][1]);
  1406. }
  1407. template <class T>
  1408. template <class S>
  1409. const Matrix33<T> &
  1410. Matrix33<T>::translate (const Vec2<S> &t)
  1411. {
  1412. x[2][0] += t[0] * x[0][0] + t[1] * x[1][0];
  1413. x[2][1] += t[0] * x[0][1] + t[1] * x[1][1];
  1414. x[2][2] += t[0] * x[0][2] + t[1] * x[1][2];
  1415. return *this;
  1416. }
  1417. template <class T>
  1418. template <class S>
  1419. const Matrix33<T> &
  1420. Matrix33<T>::setShear (const S &xy)
  1421. {
  1422. x[0][0] = 1;
  1423. x[0][1] = 0;
  1424. x[0][2] = 0;
  1425. x[1][0] = xy;
  1426. x[1][1] = 1;
  1427. x[1][2] = 0;
  1428. x[2][0] = 0;
  1429. x[2][1] = 0;
  1430. x[2][2] = 1;
  1431. return *this;
  1432. }
  1433. template <class T>
  1434. template <class S>
  1435. const Matrix33<T> &
  1436. Matrix33<T>::setShear (const Vec2<S> &h)
  1437. {
  1438. x[0][0] = 1;
  1439. x[0][1] = h[1];
  1440. x[0][2] = 0;
  1441. x[1][0] = h[0];
  1442. x[1][1] = 1;
  1443. x[1][2] = 0;
  1444. x[2][0] = 0;
  1445. x[2][1] = 0;
  1446. x[2][2] = 1;
  1447. return *this;
  1448. }
  1449. template <class T>
  1450. template <class S>
  1451. const Matrix33<T> &
  1452. Matrix33<T>::shear (const S &xy)
  1453. {
  1454. //
  1455. // In this case, we don't need a temp. copy of the matrix
  1456. // because we never use a value on the RHS after we've
  1457. // changed it on the LHS.
  1458. //
  1459. x[1][0] += xy * x[0][0];
  1460. x[1][1] += xy * x[0][1];
  1461. x[1][2] += xy * x[0][2];
  1462. return *this;
  1463. }
  1464. template <class T>
  1465. template <class S>
  1466. const Matrix33<T> &
  1467. Matrix33<T>::shear (const Vec2<S> &h)
  1468. {
  1469. Matrix33<T> P (*this);
  1470. x[0][0] = P[0][0] + h[1] * P[1][0];
  1471. x[0][1] = P[0][1] + h[1] * P[1][1];
  1472. x[0][2] = P[0][2] + h[1] * P[1][2];
  1473. x[1][0] = P[1][0] + h[0] * P[0][0];
  1474. x[1][1] = P[1][1] + h[0] * P[0][1];
  1475. x[1][2] = P[1][2] + h[0] * P[0][2];
  1476. return *this;
  1477. }
  1478. //---------------------------
  1479. // Implementation of Matrix44
  1480. //---------------------------
  1481. template <class T>
  1482. inline T *
  1483. Matrix44<T>::operator [] (int i)
  1484. {
  1485. return x[i];
  1486. }
  1487. template <class T>
  1488. inline const T *
  1489. Matrix44<T>::operator [] (int i) const
  1490. {
  1491. return x[i];
  1492. }
  1493. template <class T>
  1494. inline
  1495. Matrix44<T>::Matrix44 ()
  1496. {
  1497. memset (x, 0, sizeof (x));
  1498. x[0][0] = 1;
  1499. x[1][1] = 1;
  1500. x[2][2] = 1;
  1501. x[3][3] = 1;
  1502. }
  1503. template <class T>
  1504. inline
  1505. Matrix44<T>::Matrix44 (T a)
  1506. {
  1507. x[0][0] = a;
  1508. x[0][1] = a;
  1509. x[0][2] = a;
  1510. x[0][3] = a;
  1511. x[1][0] = a;
  1512. x[1][1] = a;
  1513. x[1][2] = a;
  1514. x[1][3] = a;
  1515. x[2][0] = a;
  1516. x[2][1] = a;
  1517. x[2][2] = a;
  1518. x[2][3] = a;
  1519. x[3][0] = a;
  1520. x[3][1] = a;
  1521. x[3][2] = a;
  1522. x[3][3] = a;
  1523. }
  1524. template <class T>
  1525. inline
  1526. Matrix44<T>::Matrix44 (const T a[4][4])
  1527. {
  1528. memcpy (x, a, sizeof (x));
  1529. }
  1530. template <class T>
  1531. inline
  1532. Matrix44<T>::Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h,
  1533. T i, T j, T k, T l, T m, T n, T o, T p)
  1534. {
  1535. x[0][0] = a;
  1536. x[0][1] = b;
  1537. x[0][2] = c;
  1538. x[0][3] = d;
  1539. x[1][0] = e;
  1540. x[1][1] = f;
  1541. x[1][2] = g;
  1542. x[1][3] = h;
  1543. x[2][0] = i;
  1544. x[2][1] = j;
  1545. x[2][2] = k;
  1546. x[2][3] = l;
  1547. x[3][0] = m;
  1548. x[3][1] = n;
  1549. x[3][2] = o;
  1550. x[3][3] = p;
  1551. }
  1552. template <class T>
  1553. inline
  1554. Matrix44<T>::Matrix44 (Matrix33<T> r, Vec3<T> t)
  1555. {
  1556. x[0][0] = r[0][0];
  1557. x[0][1] = r[0][1];
  1558. x[0][2] = r[0][2];
  1559. x[0][3] = 0;
  1560. x[1][0] = r[1][0];
  1561. x[1][1] = r[1][1];
  1562. x[1][2] = r[1][2];
  1563. x[1][3] = 0;
  1564. x[2][0] = r[2][0];
  1565. x[2][1] = r[2][1];
  1566. x[2][2] = r[2][2];
  1567. x[2][3] = 0;
  1568. x[3][0] = t[0];
  1569. x[3][1] = t[1];
  1570. x[3][2] = t[2];
  1571. x[3][3] = 1;
  1572. }
  1573. template <class T>
  1574. inline
  1575. Matrix44<T>::Matrix44 (const Matrix44 &v)
  1576. {
  1577. x[0][0] = v.x[0][0];
  1578. x[0][1] = v.x[0][1];
  1579. x[0][2] = v.x[0][2];
  1580. x[0][3] = v.x[0][3];
  1581. x[1][0] = v.x[1][0];
  1582. x[1][1] = v.x[1][1];
  1583. x[1][2] = v.x[1][2];
  1584. x[1][3] = v.x[1][3];
  1585. x[2][0] = v.x[2][0];
  1586. x[2][1] = v.x[2][1];
  1587. x[2][2] = v.x[2][2];
  1588. x[2][3] = v.x[2][3];
  1589. x[3][0] = v.x[3][0];
  1590. x[3][1] = v.x[3][1];
  1591. x[3][2] = v.x[3][2];
  1592. x[3][3] = v.x[3][3];
  1593. }
  1594. template <class T>
  1595. template <class S>
  1596. inline
  1597. Matrix44<T>::Matrix44 (const Matrix44<S> &v)
  1598. {
  1599. x[0][0] = T (v.x[0][0]);
  1600. x[0][1] = T (v.x[0][1]);
  1601. x[0][2] = T (v.x[0][2]);
  1602. x[0][3] = T (v.x[0][3]);
  1603. x[1][0] = T (v.x[1][0]);
  1604. x[1][1] = T (v.x[1][1]);
  1605. x[1][2] = T (v.x[1][2]);
  1606. x[1][3] = T (v.x[1][3]);
  1607. x[2][0] = T (v.x[2][0]);
  1608. x[2][1] = T (v.x[2][1]);
  1609. x[2][2] = T (v.x[2][2]);
  1610. x[2][3] = T (v.x[2][3]);
  1611. x[3][0] = T (v.x[3][0]);
  1612. x[3][1] = T (v.x[3][1]);
  1613. x[3][2] = T (v.x[3][2]);
  1614. x[3][3] = T (v.x[3][3]);
  1615. }
  1616. template <class T>
  1617. inline const Matrix44<T> &
  1618. Matrix44<T>::operator = (const Matrix44 &v)
  1619. {
  1620. x[0][0] = v.x[0][0];
  1621. x[0][1] = v.x[0][1];
  1622. x[0][2] = v.x[0][2];
  1623. x[0][3] = v.x[0][3];
  1624. x[1][0] = v.x[1][0];
  1625. x[1][1] = v.x[1][1];
  1626. x[1][2] = v.x[1][2];
  1627. x[1][3] = v.x[1][3];
  1628. x[2][0] = v.x[2][0];
  1629. x[2][1] = v.x[2][1];
  1630. x[2][2] = v.x[2][2];
  1631. x[2][3] = v.x[2][3];
  1632. x[3][0] = v.x[3][0];
  1633. x[3][1] = v.x[3][1];
  1634. x[3][2] = v.x[3][2];
  1635. x[3][3] = v.x[3][3];
  1636. return *this;
  1637. }
  1638. template <class T>
  1639. inline const Matrix44<T> &
  1640. Matrix44<T>::operator = (T a)
  1641. {
  1642. x[0][0] = a;
  1643. x[0][1] = a;
  1644. x[0][2] = a;
  1645. x[0][3] = a;
  1646. x[1][0] = a;
  1647. x[1][1] = a;
  1648. x[1][2] = a;
  1649. x[1][3] = a;
  1650. x[2][0] = a;
  1651. x[2][1] = a;
  1652. x[2][2] = a;
  1653. x[2][3] = a;
  1654. x[3][0] = a;
  1655. x[3][1] = a;
  1656. x[3][2] = a;
  1657. x[3][3] = a;
  1658. return *this;
  1659. }
  1660. template <class T>
  1661. inline T *
  1662. Matrix44<T>::getValue ()
  1663. {
  1664. return (T *) &x[0][0];
  1665. }
  1666. template <class T>
  1667. inline const T *
  1668. Matrix44<T>::getValue () const
  1669. {
  1670. return (const T *) &x[0][0];
  1671. }
  1672. template <class T>
  1673. template <class S>
  1674. inline void
  1675. Matrix44<T>::getValue (Matrix44<S> &v) const
  1676. {
  1677. if (isSameType<S,T>::value)
  1678. {
  1679. memcpy (v.x, x, sizeof (x));
  1680. }
  1681. else
  1682. {
  1683. v.x[0][0] = x[0][0];
  1684. v.x[0][1] = x[0][1];
  1685. v.x[0][2] = x[0][2];
  1686. v.x[0][3] = x[0][3];
  1687. v.x[1][0] = x[1][0];
  1688. v.x[1][1] = x[1][1];
  1689. v.x[1][2] = x[1][2];
  1690. v.x[1][3] = x[1][3];
  1691. v.x[2][0] = x[2][0];
  1692. v.x[2][1] = x[2][1];
  1693. v.x[2][2] = x[2][2];
  1694. v.x[2][3] = x[2][3];
  1695. v.x[3][0] = x[3][0];
  1696. v.x[3][1] = x[3][1];
  1697. v.x[3][2] = x[3][2];
  1698. v.x[3][3] = x[3][3];
  1699. }
  1700. }
  1701. template <class T>
  1702. template <class S>
  1703. inline Matrix44<T> &
  1704. Matrix44<T>::setValue (const Matrix44<S> &v)
  1705. {
  1706. if (isSameType<S,T>::value)
  1707. {
  1708. memcpy (x, v.x, sizeof (x));
  1709. }
  1710. else
  1711. {
  1712. x[0][0] = v.x[0][0];
  1713. x[0][1] = v.x[0][1];
  1714. x[0][2] = v.x[0][2];
  1715. x[0][3] = v.x[0][3];
  1716. x[1][0] = v.x[1][0];
  1717. x[1][1] = v.x[1][1];
  1718. x[1][2] = v.x[1][2];
  1719. x[1][3] = v.x[1][3];
  1720. x[2][0] = v.x[2][0];
  1721. x[2][1] = v.x[2][1];
  1722. x[2][2] = v.x[2][2];
  1723. x[2][3] = v.x[2][3];
  1724. x[3][0] = v.x[3][0];
  1725. x[3][1] = v.x[3][1];
  1726. x[3][2] = v.x[3][2];
  1727. x[3][3] = v.x[3][3];
  1728. }
  1729. return *this;
  1730. }
  1731. template <class T>
  1732. template <class S>
  1733. inline Matrix44<T> &
  1734. Matrix44<T>::setTheMatrix (const Matrix44<S> &v)
  1735. {
  1736. if (isSameType<S,T>::value)
  1737. {
  1738. memcpy (x, v.x, sizeof (x));
  1739. }
  1740. else
  1741. {
  1742. x[0][0] = v.x[0][0];
  1743. x[0][1] = v.x[0][1];
  1744. x[0][2] = v.x[0][2];
  1745. x[0][3] = v.x[0][3];
  1746. x[1][0] = v.x[1][0];
  1747. x[1][1] = v.x[1][1];
  1748. x[1][2] = v.x[1][2];
  1749. x[1][3] = v.x[1][3];
  1750. x[2][0] = v.x[2][0];
  1751. x[2][1] = v.x[2][1];
  1752. x[2][2] = v.x[2][2];
  1753. x[2][3] = v.x[2][3];
  1754. x[3][0] = v.x[3][0];
  1755. x[3][1] = v.x[3][1];
  1756. x[3][2] = v.x[3][2];
  1757. x[3][3] = v.x[3][3];
  1758. }
  1759. return *this;
  1760. }
  1761. template <class T>
  1762. inline void
  1763. Matrix44<T>::makeIdentity()
  1764. {
  1765. memset (x, 0, sizeof (x));
  1766. x[0][0] = 1;
  1767. x[1][1] = 1;
  1768. x[2][2] = 1;
  1769. x[3][3] = 1;
  1770. }
  1771. template <class T>
  1772. bool
  1773. Matrix44<T>::operator == (const Matrix44 &v) const
  1774. {
  1775. return x[0][0] == v.x[0][0] &&
  1776. x[0][1] == v.x[0][1] &&
  1777. x[0][2] == v.x[0][2] &&
  1778. x[0][3] == v.x[0][3] &&
  1779. x[1][0] == v.x[1][0] &&
  1780. x[1][1] == v.x[1][1] &&
  1781. x[1][2] == v.x[1][2] &&
  1782. x[1][3] == v.x[1][3] &&
  1783. x[2][0] == v.x[2][0] &&
  1784. x[2][1] == v.x[2][1] &&
  1785. x[2][2] == v.x[2][2] &&
  1786. x[2][3] == v.x[2][3] &&
  1787. x[3][0] == v.x[3][0] &&
  1788. x[3][1] == v.x[3][1] &&
  1789. x[3][2] == v.x[3][2] &&
  1790. x[3][3] == v.x[3][3];
  1791. }
  1792. template <class T>
  1793. bool
  1794. Matrix44<T>::operator != (const Matrix44 &v) const
  1795. {
  1796. return x[0][0] != v.x[0][0] ||
  1797. x[0][1] != v.x[0][1] ||
  1798. x[0][2] != v.x[0][2] ||
  1799. x[0][3] != v.x[0][3] ||
  1800. x[1][0] != v.x[1][0] ||
  1801. x[1][1] != v.x[1][1] ||
  1802. x[1][2] != v.x[1][2] ||
  1803. x[1][3] != v.x[1][3] ||
  1804. x[2][0] != v.x[2][0] ||
  1805. x[2][1] != v.x[2][1] ||
  1806. x[2][2] != v.x[2][2] ||
  1807. x[2][3] != v.x[2][3] ||
  1808. x[3][0] != v.x[3][0] ||
  1809. x[3][1] != v.x[3][1] ||
  1810. x[3][2] != v.x[3][2] ||
  1811. x[3][3] != v.x[3][3];
  1812. }
  1813. template <class T>
  1814. bool
  1815. Matrix44<T>::equalWithAbsError (const Matrix44<T> &m, T e) const
  1816. {
  1817. for (int i = 0; i < 4; i++)
  1818. for (int j = 0; j < 4; j++)
  1819. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i][j], m[i][j], e))
  1820. return false;
  1821. return true;
  1822. }
  1823. template <class T>
  1824. bool
  1825. Matrix44<T>::equalWithRelError (const Matrix44<T> &m, T e) const
  1826. {
  1827. for (int i = 0; i < 4; i++)
  1828. for (int j = 0; j < 4; j++)
  1829. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i][j], m[i][j], e))
  1830. return false;
  1831. return true;
  1832. }
  1833. template <class T>
  1834. const Matrix44<T> &
  1835. Matrix44<T>::operator += (const Matrix44<T> &v)
  1836. {
  1837. x[0][0] += v.x[0][0];
  1838. x[0][1] += v.x[0][1];
  1839. x[0][2] += v.x[0][2];
  1840. x[0][3] += v.x[0][3];
  1841. x[1][0] += v.x[1][0];
  1842. x[1][1] += v.x[1][1];
  1843. x[1][2] += v.x[1][2];
  1844. x[1][3] += v.x[1][3];
  1845. x[2][0] += v.x[2][0];
  1846. x[2][1] += v.x[2][1];
  1847. x[2][2] += v.x[2][2];
  1848. x[2][3] += v.x[2][3];
  1849. x[3][0] += v.x[3][0];
  1850. x[3][1] += v.x[3][1];
  1851. x[3][2] += v.x[3][2];
  1852. x[3][3] += v.x[3][3];
  1853. return *this;
  1854. }
  1855. template <class T>
  1856. const Matrix44<T> &
  1857. Matrix44<T>::operator += (T a)
  1858. {
  1859. x[0][0] += a;
  1860. x[0][1] += a;
  1861. x[0][2] += a;
  1862. x[0][3] += a;
  1863. x[1][0] += a;
  1864. x[1][1] += a;
  1865. x[1][2] += a;
  1866. x[1][3] += a;
  1867. x[2][0] += a;
  1868. x[2][1] += a;
  1869. x[2][2] += a;
  1870. x[2][3] += a;
  1871. x[3][0] += a;
  1872. x[3][1] += a;
  1873. x[3][2] += a;
  1874. x[3][3] += a;
  1875. return *this;
  1876. }
  1877. template <class T>
  1878. Matrix44<T>
  1879. Matrix44<T>::operator + (const Matrix44<T> &v) const
  1880. {
  1881. return Matrix44 (x[0][0] + v.x[0][0],
  1882. x[0][1] + v.x[0][1],
  1883. x[0][2] + v.x[0][2],
  1884. x[0][3] + v.x[0][3],
  1885. x[1][0] + v.x[1][0],
  1886. x[1][1] + v.x[1][1],
  1887. x[1][2] + v.x[1][2],
  1888. x[1][3] + v.x[1][3],
  1889. x[2][0] + v.x[2][0],
  1890. x[2][1] + v.x[2][1],
  1891. x[2][2] + v.x[2][2],
  1892. x[2][3] + v.x[2][3],
  1893. x[3][0] + v.x[3][0],
  1894. x[3][1] + v.x[3][1],
  1895. x[3][2] + v.x[3][2],
  1896. x[3][3] + v.x[3][3]);
  1897. }
  1898. template <class T>
  1899. const Matrix44<T> &
  1900. Matrix44<T>::operator -= (const Matrix44<T> &v)
  1901. {
  1902. x[0][0] -= v.x[0][0];
  1903. x[0][1] -= v.x[0][1];
  1904. x[0][2] -= v.x[0][2];
  1905. x[0][3] -= v.x[0][3];
  1906. x[1][0] -= v.x[1][0];
  1907. x[1][1] -= v.x[1][1];
  1908. x[1][2] -= v.x[1][2];
  1909. x[1][3] -= v.x[1][3];
  1910. x[2][0] -= v.x[2][0];
  1911. x[2][1] -= v.x[2][1];
  1912. x[2][2] -= v.x[2][2];
  1913. x[2][3] -= v.x[2][3];
  1914. x[3][0] -= v.x[3][0];
  1915. x[3][1] -= v.x[3][1];
  1916. x[3][2] -= v.x[3][2];
  1917. x[3][3] -= v.x[3][3];
  1918. return *this;
  1919. }
  1920. template <class T>
  1921. const Matrix44<T> &
  1922. Matrix44<T>::operator -= (T a)
  1923. {
  1924. x[0][0] -= a;
  1925. x[0][1] -= a;
  1926. x[0][2] -= a;
  1927. x[0][3] -= a;
  1928. x[1][0] -= a;
  1929. x[1][1] -= a;
  1930. x[1][2] -= a;
  1931. x[1][3] -= a;
  1932. x[2][0] -= a;
  1933. x[2][1] -= a;
  1934. x[2][2] -= a;
  1935. x[2][3] -= a;
  1936. x[3][0] -= a;
  1937. x[3][1] -= a;
  1938. x[3][2] -= a;
  1939. x[3][3] -= a;
  1940. return *this;
  1941. }
  1942. template <class T>
  1943. Matrix44<T>
  1944. Matrix44<T>::operator - (const Matrix44<T> &v) const
  1945. {
  1946. return Matrix44 (x[0][0] - v.x[0][0],
  1947. x[0][1] - v.x[0][1],
  1948. x[0][2] - v.x[0][2],
  1949. x[0][3] - v.x[0][3],
  1950. x[1][0] - v.x[1][0],
  1951. x[1][1] - v.x[1][1],
  1952. x[1][2] - v.x[1][2],
  1953. x[1][3] - v.x[1][3],
  1954. x[2][0] - v.x[2][0],
  1955. x[2][1] - v.x[2][1],
  1956. x[2][2] - v.x[2][2],
  1957. x[2][3] - v.x[2][3],
  1958. x[3][0] - v.x[3][0],
  1959. x[3][1] - v.x[3][1],
  1960. x[3][2] - v.x[3][2],
  1961. x[3][3] - v.x[3][3]);
  1962. }
  1963. template <class T>
  1964. Matrix44<T>
  1965. Matrix44<T>::operator - () const
  1966. {
  1967. return Matrix44 (-x[0][0],
  1968. -x[0][1],
  1969. -x[0][2],
  1970. -x[0][3],
  1971. -x[1][0],
  1972. -x[1][1],
  1973. -x[1][2],
  1974. -x[1][3],
  1975. -x[2][0],
  1976. -x[2][1],
  1977. -x[2][2],
  1978. -x[2][3],
  1979. -x[3][0],
  1980. -x[3][1],
  1981. -x[3][2],
  1982. -x[3][3]);
  1983. }
  1984. template <class T>
  1985. const Matrix44<T> &
  1986. Matrix44<T>::negate ()
  1987. {
  1988. x[0][0] = -x[0][0];
  1989. x[0][1] = -x[0][1];
  1990. x[0][2] = -x[0][2];
  1991. x[0][3] = -x[0][3];
  1992. x[1][0] = -x[1][0];
  1993. x[1][1] = -x[1][1];
  1994. x[1][2] = -x[1][2];
  1995. x[1][3] = -x[1][3];
  1996. x[2][0] = -x[2][0];
  1997. x[2][1] = -x[2][1];
  1998. x[2][2] = -x[2][2];
  1999. x[2][3] = -x[2][3];
  2000. x[3][0] = -x[3][0];
  2001. x[3][1] = -x[3][1];
  2002. x[3][2] = -x[3][2];
  2003. x[3][3] = -x[3][3];
  2004. return *this;
  2005. }
  2006. template <class T>
  2007. const Matrix44<T> &
  2008. Matrix44<T>::operator *= (T a)
  2009. {
  2010. x[0][0] *= a;
  2011. x[0][1] *= a;
  2012. x[0][2] *= a;
  2013. x[0][3] *= a;
  2014. x[1][0] *= a;
  2015. x[1][1] *= a;
  2016. x[1][2] *= a;
  2017. x[1][3] *= a;
  2018. x[2][0] *= a;
  2019. x[2][1] *= a;
  2020. x[2][2] *= a;
  2021. x[2][3] *= a;
  2022. x[3][0] *= a;
  2023. x[3][1] *= a;
  2024. x[3][2] *= a;
  2025. x[3][3] *= a;
  2026. return *this;
  2027. }
  2028. template <class T>
  2029. Matrix44<T>
  2030. Matrix44<T>::operator * (T a) const
  2031. {
  2032. return Matrix44 (x[0][0] * a,
  2033. x[0][1] * a,
  2034. x[0][2] * a,
  2035. x[0][3] * a,
  2036. x[1][0] * a,
  2037. x[1][1] * a,
  2038. x[1][2] * a,
  2039. x[1][3] * a,
  2040. x[2][0] * a,
  2041. x[2][1] * a,
  2042. x[2][2] * a,
  2043. x[2][3] * a,
  2044. x[3][0] * a,
  2045. x[3][1] * a,
  2046. x[3][2] * a,
  2047. x[3][3] * a);
  2048. }
  2049. template <class T>
  2050. inline Matrix44<T>
  2051. operator * (T a, const Matrix44<T> &v)
  2052. {
  2053. return v * a;
  2054. }
  2055. template <class T>
  2056. inline const Matrix44<T> &
  2057. Matrix44<T>::operator *= (const Matrix44<T> &v)
  2058. {
  2059. Matrix44 tmp (T (0));
  2060. multiply (*this, v, tmp);
  2061. *this = tmp;
  2062. return *this;
  2063. }
  2064. template <class T>
  2065. inline Matrix44<T>
  2066. Matrix44<T>::operator * (const Matrix44<T> &v) const
  2067. {
  2068. Matrix44 tmp (T (0));
  2069. multiply (*this, v, tmp);
  2070. return tmp;
  2071. }
  2072. template <class T>
  2073. void
  2074. Matrix44<T>::multiply (const Matrix44<T> &a,
  2075. const Matrix44<T> &b,
  2076. Matrix44<T> &c)
  2077. {
  2078. const T * IMATH_RESTRICT ap = &a.x[0][0];
  2079. const T * IMATH_RESTRICT bp = &b.x[0][0];
  2080. T * IMATH_RESTRICT cp = &c.x[0][0];
  2081. T a0, a1, a2, a3;
  2082. a0 = ap[0];
  2083. a1 = ap[1];
  2084. a2 = ap[2];
  2085. a3 = ap[3];
  2086. cp[0] = a0 * bp[0] + a1 * bp[4] + a2 * bp[8] + a3 * bp[12];
  2087. cp[1] = a0 * bp[1] + a1 * bp[5] + a2 * bp[9] + a3 * bp[13];
  2088. cp[2] = a0 * bp[2] + a1 * bp[6] + a2 * bp[10] + a3 * bp[14];
  2089. cp[3] = a0 * bp[3] + a1 * bp[7] + a2 * bp[11] + a3 * bp[15];
  2090. a0 = ap[4];
  2091. a1 = ap[5];
  2092. a2 = ap[6];
  2093. a3 = ap[7];
  2094. cp[4] = a0 * bp[0] + a1 * bp[4] + a2 * bp[8] + a3 * bp[12];
  2095. cp[5] = a0 * bp[1] + a1 * bp[5] + a2 * bp[9] + a3 * bp[13];
  2096. cp[6] = a0 * bp[2] + a1 * bp[6] + a2 * bp[10] + a3 * bp[14];
  2097. cp[7] = a0 * bp[3] + a1 * bp[7] + a2 * bp[11] + a3 * bp[15];
  2098. a0 = ap[8];
  2099. a1 = ap[9];
  2100. a2 = ap[10];
  2101. a3 = ap[11];
  2102. cp[8] = a0 * bp[0] + a1 * bp[4] + a2 * bp[8] + a3 * bp[12];
  2103. cp[9] = a0 * bp[1] + a1 * bp[5] + a2 * bp[9] + a3 * bp[13];
  2104. cp[10] = a0 * bp[2] + a1 * bp[6] + a2 * bp[10] + a3 * bp[14];
  2105. cp[11] = a0 * bp[3] + a1 * bp[7] + a2 * bp[11] + a3 * bp[15];
  2106. a0 = ap[12];
  2107. a1 = ap[13];
  2108. a2 = ap[14];
  2109. a3 = ap[15];
  2110. cp[12] = a0 * bp[0] + a1 * bp[4] + a2 * bp[8] + a3 * bp[12];
  2111. cp[13] = a0 * bp[1] + a1 * bp[5] + a2 * bp[9] + a3 * bp[13];
  2112. cp[14] = a0 * bp[2] + a1 * bp[6] + a2 * bp[10] + a3 * bp[14];
  2113. cp[15] = a0 * bp[3] + a1 * bp[7] + a2 * bp[11] + a3 * bp[15];
  2114. }
  2115. template <class T> template <class S>
  2116. void
  2117. Matrix44<T>::multVecMatrix(const Vec3<S> &src, Vec3<S> &dst) const
  2118. {
  2119. S a, b, c, w;
  2120. a = src[0] * x[0][0] + src[1] * x[1][0] + src[2] * x[2][0] + x[3][0];
  2121. b = src[0] * x[0][1] + src[1] * x[1][1] + src[2] * x[2][1] + x[3][1];
  2122. c = src[0] * x[0][2] + src[1] * x[1][2] + src[2] * x[2][2] + x[3][2];
  2123. w = src[0] * x[0][3] + src[1] * x[1][3] + src[2] * x[2][3] + x[3][3];
  2124. dst.x = a / w;
  2125. dst.y = b / w;
  2126. dst.z = c / w;
  2127. }
  2128. template <class T> template <class S>
  2129. void
  2130. Matrix44<T>::multDirMatrix(const Vec3<S> &src, Vec3<S> &dst) const
  2131. {
  2132. S a, b, c;
  2133. a = src[0] * x[0][0] + src[1] * x[1][0] + src[2] * x[2][0];
  2134. b = src[0] * x[0][1] + src[1] * x[1][1] + src[2] * x[2][1];
  2135. c = src[0] * x[0][2] + src[1] * x[1][2] + src[2] * x[2][2];
  2136. dst.x = a;
  2137. dst.y = b;
  2138. dst.z = c;
  2139. }
  2140. template <class T>
  2141. const Matrix44<T> &
  2142. Matrix44<T>::operator /= (T a)
  2143. {
  2144. x[0][0] /= a;
  2145. x[0][1] /= a;
  2146. x[0][2] /= a;
  2147. x[0][3] /= a;
  2148. x[1][0] /= a;
  2149. x[1][1] /= a;
  2150. x[1][2] /= a;
  2151. x[1][3] /= a;
  2152. x[2][0] /= a;
  2153. x[2][1] /= a;
  2154. x[2][2] /= a;
  2155. x[2][3] /= a;
  2156. x[3][0] /= a;
  2157. x[3][1] /= a;
  2158. x[3][2] /= a;
  2159. x[3][3] /= a;
  2160. return *this;
  2161. }
  2162. template <class T>
  2163. Matrix44<T>
  2164. Matrix44<T>::operator / (T a) const
  2165. {
  2166. return Matrix44 (x[0][0] / a,
  2167. x[0][1] / a,
  2168. x[0][2] / a,
  2169. x[0][3] / a,
  2170. x[1][0] / a,
  2171. x[1][1] / a,
  2172. x[1][2] / a,
  2173. x[1][3] / a,
  2174. x[2][0] / a,
  2175. x[2][1] / a,
  2176. x[2][2] / a,
  2177. x[2][3] / a,
  2178. x[3][0] / a,
  2179. x[3][1] / a,
  2180. x[3][2] / a,
  2181. x[3][3] / a);
  2182. }
  2183. template <class T>
  2184. const Matrix44<T> &
  2185. Matrix44<T>::transpose ()
  2186. {
  2187. Matrix44 tmp (x[0][0],
  2188. x[1][0],
  2189. x[2][0],
  2190. x[3][0],
  2191. x[0][1],
  2192. x[1][1],
  2193. x[2][1],
  2194. x[3][1],
  2195. x[0][2],
  2196. x[1][2],
  2197. x[2][2],
  2198. x[3][2],
  2199. x[0][3],
  2200. x[1][3],
  2201. x[2][3],
  2202. x[3][3]);
  2203. *this = tmp;
  2204. return *this;
  2205. }
  2206. template <class T>
  2207. Matrix44<T>
  2208. Matrix44<T>::transposed () const
  2209. {
  2210. return Matrix44 (x[0][0],
  2211. x[1][0],
  2212. x[2][0],
  2213. x[3][0],
  2214. x[0][1],
  2215. x[1][1],
  2216. x[2][1],
  2217. x[3][1],
  2218. x[0][2],
  2219. x[1][2],
  2220. x[2][2],
  2221. x[3][2],
  2222. x[0][3],
  2223. x[1][3],
  2224. x[2][3],
  2225. x[3][3]);
  2226. }
  2227. template <class T>
  2228. const Matrix44<T> &
  2229. Matrix44<T>::gjInvert (bool singExc)
  2230. {
  2231. *this = gjInverse (singExc);
  2232. return *this;
  2233. }
  2234. template <class T>
  2235. Matrix44<T>
  2236. Matrix44<T>::gjInverse (bool singExc) const
  2237. {
  2238. int i, j, k;
  2239. Matrix44 s;
  2240. Matrix44 t (*this);
  2241. // Forward elimination
  2242. for (i = 0; i < 3 ; i++)
  2243. {
  2244. int pivot = i;
  2245. T pivotsize = t[i][i];
  2246. if (pivotsize < 0)
  2247. pivotsize = -pivotsize;
  2248. for (j = i + 1; j < 4; j++)
  2249. {
  2250. T tmp = t[j][i];
  2251. if (tmp < 0)
  2252. tmp = -tmp;
  2253. if (tmp > pivotsize)
  2254. {
  2255. pivot = j;
  2256. pivotsize = tmp;
  2257. }
  2258. }
  2259. if (pivotsize == 0)
  2260. {
  2261. if (singExc)
  2262. throw ::IMATH_INTERNAL_NAMESPACE::SingMatrixExc ("Cannot invert singular matrix.");
  2263. return Matrix44();
  2264. }
  2265. if (pivot != i)
  2266. {
  2267. for (j = 0; j < 4; j++)
  2268. {
  2269. T tmp;
  2270. tmp = t[i][j];
  2271. t[i][j] = t[pivot][j];
  2272. t[pivot][j] = tmp;
  2273. tmp = s[i][j];
  2274. s[i][j] = s[pivot][j];
  2275. s[pivot][j] = tmp;
  2276. }
  2277. }
  2278. for (j = i + 1; j < 4; j++)
  2279. {
  2280. T f = t[j][i] / t[i][i];
  2281. for (k = 0; k < 4; k++)
  2282. {
  2283. t[j][k] -= f * t[i][k];
  2284. s[j][k] -= f * s[i][k];
  2285. }
  2286. }
  2287. }
  2288. // Backward substitution
  2289. for (i = 3; i >= 0; --i)
  2290. {
  2291. T f;
  2292. if ((f = t[i][i]) == 0)
  2293. {
  2294. if (singExc)
  2295. throw ::IMATH_INTERNAL_NAMESPACE::SingMatrixExc ("Cannot invert singular matrix.");
  2296. return Matrix44();
  2297. }
  2298. for (j = 0; j < 4; j++)
  2299. {
  2300. t[i][j] /= f;
  2301. s[i][j] /= f;
  2302. }
  2303. for (j = 0; j < i; j++)
  2304. {
  2305. f = t[j][i];
  2306. for (k = 0; k < 4; k++)
  2307. {
  2308. t[j][k] -= f * t[i][k];
  2309. s[j][k] -= f * s[i][k];
  2310. }
  2311. }
  2312. }
  2313. return s;
  2314. }
  2315. template <class T>
  2316. const Matrix44<T> &
  2317. Matrix44<T>::invert (bool singExc)
  2318. {
  2319. *this = inverse (singExc);
  2320. return *this;
  2321. }
  2322. template <class T>
  2323. Matrix44<T>
  2324. Matrix44<T>::inverse (bool singExc) const
  2325. {
  2326. if (x[0][3] != 0 || x[1][3] != 0 || x[2][3] != 0 || x[3][3] != 1)
  2327. return gjInverse(singExc);
  2328. Matrix44 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
  2329. x[2][1] * x[0][2] - x[0][1] * x[2][2],
  2330. x[0][1] * x[1][2] - x[1][1] * x[0][2],
  2331. 0,
  2332. x[2][0] * x[1][2] - x[1][0] * x[2][2],
  2333. x[0][0] * x[2][2] - x[2][0] * x[0][2],
  2334. x[1][0] * x[0][2] - x[0][0] * x[1][2],
  2335. 0,
  2336. x[1][0] * x[2][1] - x[2][0] * x[1][1],
  2337. x[2][0] * x[0][1] - x[0][0] * x[2][1],
  2338. x[0][0] * x[1][1] - x[1][0] * x[0][1],
  2339. 0,
  2340. 0,
  2341. 0,
  2342. 0,
  2343. 1);
  2344. T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
  2345. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  2346. {
  2347. for (int i = 0; i < 3; ++i)
  2348. {
  2349. for (int j = 0; j < 3; ++j)
  2350. {
  2351. s[i][j] /= r;
  2352. }
  2353. }
  2354. }
  2355. else
  2356. {
  2357. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / limits<T>::smallest();
  2358. for (int i = 0; i < 3; ++i)
  2359. {
  2360. for (int j = 0; j < 3; ++j)
  2361. {
  2362. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s[i][j]))
  2363. {
  2364. s[i][j] /= r;
  2365. }
  2366. else
  2367. {
  2368. if (singExc)
  2369. throw SingMatrixExc ("Cannot invert singular matrix.");
  2370. return Matrix44();
  2371. }
  2372. }
  2373. }
  2374. }
  2375. s[3][0] = -x[3][0] * s[0][0] - x[3][1] * s[1][0] - x[3][2] * s[2][0];
  2376. s[3][1] = -x[3][0] * s[0][1] - x[3][1] * s[1][1] - x[3][2] * s[2][1];
  2377. s[3][2] = -x[3][0] * s[0][2] - x[3][1] * s[1][2] - x[3][2] * s[2][2];
  2378. return s;
  2379. }
  2380. template <class T>
  2381. inline T
  2382. Matrix44<T>::fastMinor( const int r0, const int r1, const int r2,
  2383. const int c0, const int c1, const int c2) const
  2384. {
  2385. return x[r0][c0] * (x[r1][c1]*x[r2][c2] - x[r1][c2]*x[r2][c1])
  2386. + x[r0][c1] * (x[r1][c2]*x[r2][c0] - x[r1][c0]*x[r2][c2])
  2387. + x[r0][c2] * (x[r1][c0]*x[r2][c1] - x[r1][c1]*x[r2][c0]);
  2388. }
  2389. template <class T>
  2390. inline T
  2391. Matrix44<T>::minorOf (const int r, const int c) const
  2392. {
  2393. int r0 = 0 + (r < 1 ? 1 : 0);
  2394. int r1 = 1 + (r < 2 ? 1 : 0);
  2395. int r2 = 2 + (r < 3 ? 1 : 0);
  2396. int c0 = 0 + (c < 1 ? 1 : 0);
  2397. int c1 = 1 + (c < 2 ? 1 : 0);
  2398. int c2 = 2 + (c < 3 ? 1 : 0);
  2399. Matrix33<T> working (x[r0][c0],x[r1][c0],x[r2][c0],
  2400. x[r0][c1],x[r1][c1],x[r2][c1],
  2401. x[r0][c2],x[r1][c2],x[r2][c2]);
  2402. return working.determinant();
  2403. }
  2404. template <class T>
  2405. inline T
  2406. Matrix44<T>::determinant () const
  2407. {
  2408. T sum = (T)0;
  2409. if (x[0][3] != 0.) sum -= x[0][3] * fastMinor(1,2,3,0,1,2);
  2410. if (x[1][3] != 0.) sum += x[1][3] * fastMinor(0,2,3,0,1,2);
  2411. if (x[2][3] != 0.) sum -= x[2][3] * fastMinor(0,1,3,0,1,2);
  2412. if (x[3][3] != 0.) sum += x[3][3] * fastMinor(0,1,2,0,1,2);
  2413. return sum;
  2414. }
  2415. template <class T>
  2416. template <class S>
  2417. const Matrix44<T> &
  2418. Matrix44<T>::setEulerAngles (const Vec3<S>& r)
  2419. {
  2420. S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
  2421. cos_rz = Math<T>::cos (r[2]);
  2422. cos_ry = Math<T>::cos (r[1]);
  2423. cos_rx = Math<T>::cos (r[0]);
  2424. sin_rz = Math<T>::sin (r[2]);
  2425. sin_ry = Math<T>::sin (r[1]);
  2426. sin_rx = Math<T>::sin (r[0]);
  2427. x[0][0] = cos_rz * cos_ry;
  2428. x[0][1] = sin_rz * cos_ry;
  2429. x[0][2] = -sin_ry;
  2430. x[0][3] = 0;
  2431. x[1][0] = -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx;
  2432. x[1][1] = cos_rz * cos_rx + sin_rz * sin_ry * sin_rx;
  2433. x[1][2] = cos_ry * sin_rx;
  2434. x[1][3] = 0;
  2435. x[2][0] = sin_rz * sin_rx + cos_rz * sin_ry * cos_rx;
  2436. x[2][1] = -cos_rz * sin_rx + sin_rz * sin_ry * cos_rx;
  2437. x[2][2] = cos_ry * cos_rx;
  2438. x[2][3] = 0;
  2439. x[3][0] = 0;
  2440. x[3][1] = 0;
  2441. x[3][2] = 0;
  2442. x[3][3] = 1;
  2443. return *this;
  2444. }
  2445. template <class T>
  2446. template <class S>
  2447. const Matrix44<T> &
  2448. Matrix44<T>::setAxisAngle (const Vec3<S>& axis, S angle)
  2449. {
  2450. Vec3<S> unit (axis.normalized());
  2451. S sine = Math<T>::sin (angle);
  2452. S cosine = Math<T>::cos (angle);
  2453. x[0][0] = unit[0] * unit[0] * (1 - cosine) + cosine;
  2454. x[0][1] = unit[0] * unit[1] * (1 - cosine) + unit[2] * sine;
  2455. x[0][2] = unit[0] * unit[2] * (1 - cosine) - unit[1] * sine;
  2456. x[0][3] = 0;
  2457. x[1][0] = unit[0] * unit[1] * (1 - cosine) - unit[2] * sine;
  2458. x[1][1] = unit[1] * unit[1] * (1 - cosine) + cosine;
  2459. x[1][2] = unit[1] * unit[2] * (1 - cosine) + unit[0] * sine;
  2460. x[1][3] = 0;
  2461. x[2][0] = unit[0] * unit[2] * (1 - cosine) + unit[1] * sine;
  2462. x[2][1] = unit[1] * unit[2] * (1 - cosine) - unit[0] * sine;
  2463. x[2][2] = unit[2] * unit[2] * (1 - cosine) + cosine;
  2464. x[2][3] = 0;
  2465. x[3][0] = 0;
  2466. x[3][1] = 0;
  2467. x[3][2] = 0;
  2468. x[3][3] = 1;
  2469. return *this;
  2470. }
  2471. template <class T>
  2472. template <class S>
  2473. const Matrix44<T> &
  2474. Matrix44<T>::rotate (const Vec3<S> &r)
  2475. {
  2476. S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
  2477. S m00, m01, m02;
  2478. S m10, m11, m12;
  2479. S m20, m21, m22;
  2480. cos_rz = Math<S>::cos (r[2]);
  2481. cos_ry = Math<S>::cos (r[1]);
  2482. cos_rx = Math<S>::cos (r[0]);
  2483. sin_rz = Math<S>::sin (r[2]);
  2484. sin_ry = Math<S>::sin (r[1]);
  2485. sin_rx = Math<S>::sin (r[0]);
  2486. m00 = cos_rz * cos_ry;
  2487. m01 = sin_rz * cos_ry;
  2488. m02 = -sin_ry;
  2489. m10 = -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx;
  2490. m11 = cos_rz * cos_rx + sin_rz * sin_ry * sin_rx;
  2491. m12 = cos_ry * sin_rx;
  2492. m20 = -sin_rz * -sin_rx + cos_rz * sin_ry * cos_rx;
  2493. m21 = cos_rz * -sin_rx + sin_rz * sin_ry * cos_rx;
  2494. m22 = cos_ry * cos_rx;
  2495. Matrix44<T> P (*this);
  2496. x[0][0] = P[0][0] * m00 + P[1][0] * m01 + P[2][0] * m02;
  2497. x[0][1] = P[0][1] * m00 + P[1][1] * m01 + P[2][1] * m02;
  2498. x[0][2] = P[0][2] * m00 + P[1][2] * m01 + P[2][2] * m02;
  2499. x[0][3] = P[0][3] * m00 + P[1][3] * m01 + P[2][3] * m02;
  2500. x[1][0] = P[0][0] * m10 + P[1][0] * m11 + P[2][0] * m12;
  2501. x[1][1] = P[0][1] * m10 + P[1][1] * m11 + P[2][1] * m12;
  2502. x[1][2] = P[0][2] * m10 + P[1][2] * m11 + P[2][2] * m12;
  2503. x[1][3] = P[0][3] * m10 + P[1][3] * m11 + P[2][3] * m12;
  2504. x[2][0] = P[0][0] * m20 + P[1][0] * m21 + P[2][0] * m22;
  2505. x[2][1] = P[0][1] * m20 + P[1][1] * m21 + P[2][1] * m22;
  2506. x[2][2] = P[0][2] * m20 + P[1][2] * m21 + P[2][2] * m22;
  2507. x[2][3] = P[0][3] * m20 + P[1][3] * m21 + P[2][3] * m22;
  2508. return *this;
  2509. }
  2510. template <class T>
  2511. const Matrix44<T> &
  2512. Matrix44<T>::setScale (T s)
  2513. {
  2514. memset (x, 0, sizeof (x));
  2515. x[0][0] = s;
  2516. x[1][1] = s;
  2517. x[2][2] = s;
  2518. x[3][3] = 1;
  2519. return *this;
  2520. }
  2521. template <class T>
  2522. template <class S>
  2523. const Matrix44<T> &
  2524. Matrix44<T>::setScale (const Vec3<S> &s)
  2525. {
  2526. memset (x, 0, sizeof (x));
  2527. x[0][0] = s[0];
  2528. x[1][1] = s[1];
  2529. x[2][2] = s[2];
  2530. x[3][3] = 1;
  2531. return *this;
  2532. }
  2533. template <class T>
  2534. template <class S>
  2535. const Matrix44<T> &
  2536. Matrix44<T>::scale (const Vec3<S> &s)
  2537. {
  2538. x[0][0] *= s[0];
  2539. x[0][1] *= s[0];
  2540. x[0][2] *= s[0];
  2541. x[0][3] *= s[0];
  2542. x[1][0] *= s[1];
  2543. x[1][1] *= s[1];
  2544. x[1][2] *= s[1];
  2545. x[1][3] *= s[1];
  2546. x[2][0] *= s[2];
  2547. x[2][1] *= s[2];
  2548. x[2][2] *= s[2];
  2549. x[2][3] *= s[2];
  2550. return *this;
  2551. }
  2552. template <class T>
  2553. template <class S>
  2554. const Matrix44<T> &
  2555. Matrix44<T>::setTranslation (const Vec3<S> &t)
  2556. {
  2557. x[0][0] = 1;
  2558. x[0][1] = 0;
  2559. x[0][2] = 0;
  2560. x[0][3] = 0;
  2561. x[1][0] = 0;
  2562. x[1][1] = 1;
  2563. x[1][2] = 0;
  2564. x[1][3] = 0;
  2565. x[2][0] = 0;
  2566. x[2][1] = 0;
  2567. x[2][2] = 1;
  2568. x[2][3] = 0;
  2569. x[3][0] = t[0];
  2570. x[3][1] = t[1];
  2571. x[3][2] = t[2];
  2572. x[3][3] = 1;
  2573. return *this;
  2574. }
  2575. template <class T>
  2576. inline const Vec3<T>
  2577. Matrix44<T>::translation () const
  2578. {
  2579. return Vec3<T> (x[3][0], x[3][1], x[3][2]);
  2580. }
  2581. template <class T>
  2582. template <class S>
  2583. const Matrix44<T> &
  2584. Matrix44<T>::translate (const Vec3<S> &t)
  2585. {
  2586. x[3][0] += t[0] * x[0][0] + t[1] * x[1][0] + t[2] * x[2][0];
  2587. x[3][1] += t[0] * x[0][1] + t[1] * x[1][1] + t[2] * x[2][1];
  2588. x[3][2] += t[0] * x[0][2] + t[1] * x[1][2] + t[2] * x[2][2];
  2589. x[3][3] += t[0] * x[0][3] + t[1] * x[1][3] + t[2] * x[2][3];
  2590. return *this;
  2591. }
  2592. template <class T>
  2593. template <class S>
  2594. const Matrix44<T> &
  2595. Matrix44<T>::setShear (const Vec3<S> &h)
  2596. {
  2597. x[0][0] = 1;
  2598. x[0][1] = 0;
  2599. x[0][2] = 0;
  2600. x[0][3] = 0;
  2601. x[1][0] = h[0];
  2602. x[1][1] = 1;
  2603. x[1][2] = 0;
  2604. x[1][3] = 0;
  2605. x[2][0] = h[1];
  2606. x[2][1] = h[2];
  2607. x[2][2] = 1;
  2608. x[2][3] = 0;
  2609. x[3][0] = 0;
  2610. x[3][1] = 0;
  2611. x[3][2] = 0;
  2612. x[3][3] = 1;
  2613. return *this;
  2614. }
  2615. template <class T>
  2616. template <class S>
  2617. const Matrix44<T> &
  2618. Matrix44<T>::setShear (const Shear6<S> &h)
  2619. {
  2620. x[0][0] = 1;
  2621. x[0][1] = h.yx;
  2622. x[0][2] = h.zx;
  2623. x[0][3] = 0;
  2624. x[1][0] = h.xy;
  2625. x[1][1] = 1;
  2626. x[1][2] = h.zy;
  2627. x[1][3] = 0;
  2628. x[2][0] = h.xz;
  2629. x[2][1] = h.yz;
  2630. x[2][2] = 1;
  2631. x[2][3] = 0;
  2632. x[3][0] = 0;
  2633. x[3][1] = 0;
  2634. x[3][2] = 0;
  2635. x[3][3] = 1;
  2636. return *this;
  2637. }
  2638. template <class T>
  2639. template <class S>
  2640. const Matrix44<T> &
  2641. Matrix44<T>::shear (const Vec3<S> &h)
  2642. {
  2643. //
  2644. // In this case, we don't need a temp. copy of the matrix
  2645. // because we never use a value on the RHS after we've
  2646. // changed it on the LHS.
  2647. //
  2648. for (int i=0; i < 4; i++)
  2649. {
  2650. x[2][i] += h[1] * x[0][i] + h[2] * x[1][i];
  2651. x[1][i] += h[0] * x[0][i];
  2652. }
  2653. return *this;
  2654. }
  2655. template <class T>
  2656. template <class S>
  2657. const Matrix44<T> &
  2658. Matrix44<T>::shear (const Shear6<S> &h)
  2659. {
  2660. Matrix44<T> P (*this);
  2661. for (int i=0; i < 4; i++)
  2662. {
  2663. x[0][i] = P[0][i] + h.yx * P[1][i] + h.zx * P[2][i];
  2664. x[1][i] = h.xy * P[0][i] + P[1][i] + h.zy * P[2][i];
  2665. x[2][i] = h.xz * P[0][i] + h.yz * P[1][i] + P[2][i];
  2666. }
  2667. return *this;
  2668. }
  2669. //--------------------------------
  2670. // Implementation of stream output
  2671. //--------------------------------
  2672. template <class T>
  2673. std::ostream &
  2674. operator << (std::ostream &s, const Matrix33<T> &m)
  2675. {
  2676. std::ios_base::fmtflags oldFlags = s.flags();
  2677. int width;
  2678. if (s.flags() & std::ios_base::fixed)
  2679. {
  2680. s.setf (std::ios_base::showpoint);
  2681. width = static_cast<int>(s.precision()) + 5;
  2682. }
  2683. else
  2684. {
  2685. s.setf (std::ios_base::scientific);
  2686. s.setf (std::ios_base::showpoint);
  2687. width = static_cast<int>(s.precision()) + 8;
  2688. }
  2689. s << "(" << std::setw (width) << m[0][0] <<
  2690. " " << std::setw (width) << m[0][1] <<
  2691. " " << std::setw (width) << m[0][2] << "\n" <<
  2692. " " << std::setw (width) << m[1][0] <<
  2693. " " << std::setw (width) << m[1][1] <<
  2694. " " << std::setw (width) << m[1][2] << "\n" <<
  2695. " " << std::setw (width) << m[2][0] <<
  2696. " " << std::setw (width) << m[2][1] <<
  2697. " " << std::setw (width) << m[2][2] << ")\n";
  2698. s.flags (oldFlags);
  2699. return s;
  2700. }
  2701. template <class T>
  2702. std::ostream &
  2703. operator << (std::ostream &s, const Matrix44<T> &m)
  2704. {
  2705. std::ios_base::fmtflags oldFlags = s.flags();
  2706. int width;
  2707. if (s.flags() & std::ios_base::fixed)
  2708. {
  2709. s.setf (std::ios_base::showpoint);
  2710. width = static_cast<int>(s.precision()) + 5;
  2711. }
  2712. else
  2713. {
  2714. s.setf (std::ios_base::scientific);
  2715. s.setf (std::ios_base::showpoint);
  2716. width = static_cast<int>(s.precision()) + 8;
  2717. }
  2718. s << "(" << std::setw (width) << m[0][0] <<
  2719. " " << std::setw (width) << m[0][1] <<
  2720. " " << std::setw (width) << m[0][2] <<
  2721. " " << std::setw (width) << m[0][3] << "\n" <<
  2722. " " << std::setw (width) << m[1][0] <<
  2723. " " << std::setw (width) << m[1][1] <<
  2724. " " << std::setw (width) << m[1][2] <<
  2725. " " << std::setw (width) << m[1][3] << "\n" <<
  2726. " " << std::setw (width) << m[2][0] <<
  2727. " " << std::setw (width) << m[2][1] <<
  2728. " " << std::setw (width) << m[2][2] <<
  2729. " " << std::setw (width) << m[2][3] << "\n" <<
  2730. " " << std::setw (width) << m[3][0] <<
  2731. " " << std::setw (width) << m[3][1] <<
  2732. " " << std::setw (width) << m[3][2] <<
  2733. " " << std::setw (width) << m[3][3] << ")\n";
  2734. s.flags (oldFlags);
  2735. return s;
  2736. }
  2737. //---------------------------------------------------------------
  2738. // Implementation of vector-times-matrix multiplication operators
  2739. //---------------------------------------------------------------
  2740. template <class S, class T>
  2741. inline const Vec2<S> &
  2742. operator *= (Vec2<S> &v, const Matrix33<T> &m)
  2743. {
  2744. S x = S(v.x * m[0][0] + v.y * m[1][0] + m[2][0]);
  2745. S y = S(v.x * m[0][1] + v.y * m[1][1] + m[2][1]);
  2746. S w = S(v.x * m[0][2] + v.y * m[1][2] + m[2][2]);
  2747. v.x = x / w;
  2748. v.y = y / w;
  2749. return v;
  2750. }
  2751. template <class S, class T>
  2752. inline Vec2<S>
  2753. operator * (const Vec2<S> &v, const Matrix33<T> &m)
  2754. {
  2755. S x = S(v.x * m[0][0] + v.y * m[1][0] + m[2][0]);
  2756. S y = S(v.x * m[0][1] + v.y * m[1][1] + m[2][1]);
  2757. S w = S(v.x * m[0][2] + v.y * m[1][2] + m[2][2]);
  2758. return Vec2<S> (x / w, y / w);
  2759. }
  2760. template <class S, class T>
  2761. inline const Vec3<S> &
  2762. operator *= (Vec3<S> &v, const Matrix33<T> &m)
  2763. {
  2764. S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0]);
  2765. S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1]);
  2766. S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2]);
  2767. v.x = x;
  2768. v.y = y;
  2769. v.z = z;
  2770. return v;
  2771. }
  2772. template <class S, class T>
  2773. inline Vec3<S>
  2774. operator * (const Vec3<S> &v, const Matrix33<T> &m)
  2775. {
  2776. S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0]);
  2777. S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1]);
  2778. S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2]);
  2779. return Vec3<S> (x, y, z);
  2780. }
  2781. template <class S, class T>
  2782. inline const Vec3<S> &
  2783. operator *= (Vec3<S> &v, const Matrix44<T> &m)
  2784. {
  2785. S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]);
  2786. S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]);
  2787. S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]);
  2788. S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3]);
  2789. v.x = x / w;
  2790. v.y = y / w;
  2791. v.z = z / w;
  2792. return v;
  2793. }
  2794. template <class S, class T>
  2795. inline Vec3<S>
  2796. operator * (const Vec3<S> &v, const Matrix44<T> &m)
  2797. {
  2798. S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]);
  2799. S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]);
  2800. S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]);
  2801. S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3]);
  2802. return Vec3<S> (x / w, y / w, z / w);
  2803. }
  2804. template <class S, class T>
  2805. inline const Vec4<S> &
  2806. operator *= (Vec4<S> &v, const Matrix44<T> &m)
  2807. {
  2808. S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0]);
  2809. S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1]);
  2810. S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2]);
  2811. S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3]);
  2812. v.x = x;
  2813. v.y = y;
  2814. v.z = z;
  2815. v.w = w;
  2816. return v;
  2817. }
  2818. template <class S, class T>
  2819. inline Vec4<S>
  2820. operator * (const Vec4<S> &v, const Matrix44<T> &m)
  2821. {
  2822. S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0]);
  2823. S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1]);
  2824. S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2]);
  2825. S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3]);
  2826. return Vec4<S> (x, y, z, w);
  2827. }
  2828. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  2829. #endif // INCLUDED_IMATHMATRIX_H