ImathMatrix.h 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright Contributors to the OpenEXR Project.
  4. //
  5. //
  6. // 2x2, 3x3, and 4x4 transformation matrix templates
  7. //
  8. #ifndef INCLUDED_IMATHMATRIX_H
  9. #define INCLUDED_IMATHMATRIX_H
  10. #include "ImathExport.h"
  11. #include "ImathNamespace.h"
  12. #include "ImathFun.h"
  13. #include "ImathPlatform.h"
  14. #include "ImathShear.h"
  15. #include "ImathVec.h"
  16. #include <cstring>
  17. #include <iomanip>
  18. #include <iostream>
  19. #include <limits>
  20. #include <string.h>
  21. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  22. // suppress exception specification warnings
  23. # pragma warning(disable : 4290)
  24. #endif
  25. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  26. /// Enum used to indicate uninitialized construction of Matrix22,
  27. /// Matrix33, Matrix44
  28. enum IMATH_EXPORT_ENUM Uninitialized
  29. {
  30. UNINITIALIZED
  31. };
  32. ///
  33. /// 2x2 transformation matrix
  34. ///
  35. template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix22
  36. {
  37. public:
  38. /// @{
  39. /// @name Direct access to elements
  40. /// Matrix elements
  41. T x[2][2];
  42. /// @}
  43. /// Row access
  44. IMATH_HOSTDEVICE T* operator[] (int i) IMATH_NOEXCEPT;
  45. /// Row access
  46. IMATH_HOSTDEVICE const T* operator[] (int i) const IMATH_NOEXCEPT;
  47. /// @{
  48. /// @name Constructors and Assignment
  49. /// Uninitialized
  50. IMATH_HOSTDEVICE Matrix22 (Uninitialized) IMATH_NOEXCEPT {}
  51. /// Default constructor: initialize to identity
  52. ///
  53. /// 1 0
  54. /// 0 1
  55. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22() IMATH_NOEXCEPT;
  56. /// Initialize to scalar constant:
  57. ///
  58. /// a a
  59. /// a a
  60. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22 (T a) IMATH_NOEXCEPT;
  61. /// Construct from 2x2 array:
  62. ///
  63. /// a[0][0] a[0][1]
  64. /// a[1][0] a[1][1]
  65. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22 (const T a[2][2]) IMATH_NOEXCEPT;
  66. /// Construct from given scalar values:
  67. ///
  68. /// a b
  69. /// c d
  70. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22 (T a, T b, T c, T d) IMATH_NOEXCEPT;
  71. /// Copy constructor
  72. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22 (const Matrix22& v) IMATH_NOEXCEPT;
  73. /// Construct from Matrix22 of another base type
  74. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 explicit Matrix22 (const Matrix22<S>& v) IMATH_NOEXCEPT;
  75. /// Assignment
  76. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator= (const Matrix22& v) IMATH_NOEXCEPT;
  77. /// Assignment from scalar
  78. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator= (T a) IMATH_NOEXCEPT;
  79. /// Destructor
  80. ~Matrix22() IMATH_NOEXCEPT = default;
  81. /// @}
  82. #if IMATH_FOREIGN_VECTOR_INTEROP
  83. /// @{
  84. /// @name Interoperability with other matrix types
  85. ///
  86. /// Construction and assignment are allowed from other classes that
  87. /// appear to be equivalent matrix types, provided that they support
  88. /// double-subscript (i.e., `m[j][i]`) giving the same type as the
  89. /// elements of this matrix, and their total size appears to be the
  90. /// right number of matrix elements.
  91. ///
  92. /// This functionality is disabled for gcc 4.x, which seems to have a
  93. /// compiler bug that results in spurious errors. It can also be
  94. /// disabled by defining IMATH_FOREIGN_VECTOR_INTEROP to be 0 prior to
  95. /// including any Imath header files.
  96. ///
  97. template<typename M, IMATH_ENABLE_IF(has_double_subscript<M,T,2,2>::value)>
  98. IMATH_HOSTDEVICE explicit Matrix22 (const M& m)
  99. : Matrix22(T(m[0][0]), T(m[0][1]), T(m[1][0]), T(m[1][1]))
  100. { }
  101. template<typename M, IMATH_ENABLE_IF(has_double_subscript<M,T,2,2>::value)>
  102. IMATH_HOSTDEVICE const Matrix22& operator= (const M& m)
  103. {
  104. *this = Matrix22(T(m[0][0]), T(m[0][1]), T(m[1][0]), T(m[1][1]));
  105. return *this;
  106. }
  107. /// @}
  108. #endif
  109. /// @{
  110. /// @name Compatibility with Sb
  111. /// Return a raw pointer to the array of values
  112. IMATH_HOSTDEVICE T* getValue() IMATH_NOEXCEPT;
  113. /// Return a raw pointer to the array of values
  114. IMATH_HOSTDEVICE const T* getValue() const IMATH_NOEXCEPT;
  115. /// Return the value in `v`
  116. template <class S> IMATH_HOSTDEVICE void getValue (Matrix22<S>& v) const IMATH_NOEXCEPT;
  117. /// Set the value
  118. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22& setValue (const Matrix22<S>& v) IMATH_NOEXCEPT;
  119. /// Set the value
  120. template <class S>
  121. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22& setTheMatrix (const Matrix22<S>& v) IMATH_NOEXCEPT;
  122. /// @}
  123. /// @{
  124. /// @name Arithmetic and Comparison
  125. /// Equality
  126. IMATH_HOSTDEVICE constexpr bool operator== (const Matrix22& v) const IMATH_NOEXCEPT;
  127. /// Inequality
  128. IMATH_HOSTDEVICE constexpr bool operator!= (const Matrix22& v) const IMATH_NOEXCEPT;
  129. /// Compare two matrices and test if they are "approximately equal":
  130. /// @return True if the coefficients of this and `m` are the same
  131. /// with an absolute error of no more than e, i.e., for all i, j:
  132. ///
  133. /// abs (this[i][j] - m[i][j]) <= e
  134. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError (const Matrix22<T>& v, T e) const IMATH_NOEXCEPT;
  135. /// Compare two matrices and test if they are "approximately equal":
  136. /// @return True if the coefficients of this and m are the same with
  137. /// a relative error of no more than e, i.e., for all i, j:
  138. ///
  139. /// abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  140. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError (const Matrix22<T>& v, T e) const IMATH_NOEXCEPT;
  141. /// Component-wise addition
  142. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator+= (const Matrix22& v) IMATH_NOEXCEPT;
  143. /// Component-wise addition
  144. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator+= (T a) IMATH_NOEXCEPT;
  145. /// Component-wise addition
  146. IMATH_HOSTDEVICE constexpr Matrix22 operator+ (const Matrix22& v) const IMATH_NOEXCEPT;
  147. /// Component-wise subtraction
  148. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator-= (const Matrix22& v) IMATH_NOEXCEPT;
  149. /// Component-wise subtraction
  150. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator-= (T a) IMATH_NOEXCEPT;
  151. /// Component-wise subtraction
  152. IMATH_HOSTDEVICE constexpr Matrix22 operator- (const Matrix22& v) const IMATH_NOEXCEPT;
  153. /// Component-wise multiplication by -1
  154. IMATH_HOSTDEVICE constexpr Matrix22 operator-() const IMATH_NOEXCEPT;
  155. /// Component-wise multiplication by -1
  156. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& negate() IMATH_NOEXCEPT;
  157. /// Component-wise multiplication
  158. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator*= (T a) IMATH_NOEXCEPT;
  159. /// Component-wise multiplication
  160. IMATH_HOSTDEVICE constexpr Matrix22 operator* (T a) const IMATH_NOEXCEPT;
  161. /// Component-wise division
  162. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator/= (T a) IMATH_NOEXCEPT;
  163. /// Component-wise division
  164. IMATH_HOSTDEVICE constexpr Matrix22 operator/ (T a) const IMATH_NOEXCEPT;
  165. /// Matrix-matrix multiplication
  166. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& operator*= (const Matrix22& v) IMATH_NOEXCEPT;
  167. /// Matrix-matrix multiplication
  168. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22 operator* (const Matrix22& v) const IMATH_NOEXCEPT;
  169. /// Vector * matrix multiplication
  170. /// @param[in] src Input vector
  171. /// @param[out] dst transformed vector
  172. template <class S> IMATH_HOSTDEVICE void multDirMatrix (const Vec2<S>& src, Vec2<S>& dst) const IMATH_NOEXCEPT;
  173. /// @}
  174. /// @{
  175. /// @name Maniplation
  176. /// Set to the identity
  177. IMATH_HOSTDEVICE void makeIdentity() IMATH_NOEXCEPT;
  178. /// Transpose
  179. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& transpose() IMATH_NOEXCEPT;
  180. /// Return the transpose
  181. IMATH_HOSTDEVICE constexpr Matrix22 transposed() const IMATH_NOEXCEPT;
  182. /// Invert in place
  183. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  184. /// @return const reference to this
  185. IMATH_CONSTEXPR14 const Matrix22& invert (bool singExc);
  186. /// Invert in place
  187. /// @return const reference to this
  188. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& invert() IMATH_NOEXCEPT;
  189. /// Return the inverse, leaving this unmodified.
  190. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  191. IMATH_CONSTEXPR14 Matrix22<T> inverse (bool singExc) const;
  192. /// Return the inverse, leaving this unmodified.
  193. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix22<T> inverse() const IMATH_NOEXCEPT;
  194. /// Determinant
  195. IMATH_HOSTDEVICE constexpr T determinant() const IMATH_NOEXCEPT;
  196. /// Trace
  197. IMATH_HOSTDEVICE constexpr T trace() const IMATH_NOEXCEPT;
  198. /// Set matrix to rotation by r (in radians)
  199. /// @return const referenced to this
  200. template <class S> IMATH_HOSTDEVICE const Matrix22& setRotation (S r) IMATH_NOEXCEPT;
  201. /// Rotate the given matrix by r (in radians)
  202. /// @return const referenced to this
  203. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& rotate (S r) IMATH_NOEXCEPT;
  204. /// Set matrix to scale by given uniform factor
  205. /// @return const referenced to this
  206. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& setScale (T s) IMATH_NOEXCEPT;
  207. /// Set matrix to scale by given vector
  208. /// @return const referenced to this
  209. template <class S>
  210. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& setScale (const Vec2<S>& s) IMATH_NOEXCEPT;
  211. // Scale the matrix by s
  212. /// @return const referenced to this
  213. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix22& scale (const Vec2<S>& s) IMATH_NOEXCEPT;
  214. /// @}
  215. /// @{
  216. /// @name Numeric Limits
  217. /// Largest possible negative value
  218. IMATH_HOSTDEVICE constexpr static T baseTypeLowest() IMATH_NOEXCEPT { return std::numeric_limits<T>::lowest(); }
  219. /// Largest possible positive value
  220. IMATH_HOSTDEVICE constexpr static T baseTypeMax() IMATH_NOEXCEPT { return std::numeric_limits<T>::max(); }
  221. /// Smallest possible positive value
  222. IMATH_HOSTDEVICE constexpr static T baseTypeSmallest() IMATH_NOEXCEPT { return std::numeric_limits<T>::min(); }
  223. /// Smallest possible e for which 1+e != 1
  224. IMATH_HOSTDEVICE constexpr static T baseTypeEpsilon() IMATH_NOEXCEPT { return std::numeric_limits<T>::epsilon(); }
  225. /// @}
  226. /// Return the number of the row and column dimensions, i.e. 2.
  227. IMATH_HOSTDEVICE constexpr static unsigned int dimensions() IMATH_NOEXCEPT { return 2; }
  228. /// The base type: In templates that accept a parameter `V`, you
  229. /// can refer to `T` as `V::BaseType`
  230. typedef T BaseType;
  231. /// The base vector type
  232. typedef Vec2<T> BaseVecType;
  233. };
  234. ///
  235. /// 3x3 transformation matrix
  236. ///
  237. template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix33
  238. {
  239. public:
  240. /// @{
  241. /// @name Direct access to elements
  242. /// Matrix elements
  243. T x[3][3];
  244. /// @}
  245. /// Row access
  246. IMATH_HOSTDEVICE T* operator[] (int i) IMATH_NOEXCEPT;
  247. /// Row access
  248. IMATH_HOSTDEVICE const T* operator[] (int i) const IMATH_NOEXCEPT;
  249. /// @{
  250. /// @name Constructors and Assignment
  251. /// Uninitialized
  252. IMATH_HOSTDEVICE Matrix33 (Uninitialized) IMATH_NOEXCEPT {}
  253. /// Default constructor: initialize to identity
  254. /// 1 0 0
  255. /// 0 1 0
  256. /// 0 0 1
  257. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33() IMATH_NOEXCEPT;
  258. /// Initialize to scalar constant
  259. /// a a a
  260. /// a a a
  261. /// a a a
  262. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33 (T a) IMATH_NOEXCEPT;
  263. /// Construct from 3x3 array
  264. /// a[0][0] a[0][1] a[0][2]
  265. /// a[1][0] a[1][1] a[1][2]
  266. /// a[2][0] a[2][1] a[2][2]
  267. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33 (const T a[3][3]) IMATH_NOEXCEPT;
  268. /// Construct from given scalar values
  269. /// a b c
  270. /// d e f
  271. /// g h i
  272. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i) IMATH_NOEXCEPT;
  273. /// Copy constructor
  274. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33 (const Matrix33& v) IMATH_NOEXCEPT;
  275. /// Construct from Matrix33 of another base type
  276. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 explicit Matrix33 (const Matrix33<S>& v) IMATH_NOEXCEPT;
  277. /// Assignment operator
  278. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator= (const Matrix33& v) IMATH_NOEXCEPT;
  279. /// Assignment from scalar
  280. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator= (T a) IMATH_NOEXCEPT;
  281. /// Destructor
  282. ~Matrix33() IMATH_NOEXCEPT = default;
  283. /// @}
  284. #if IMATH_FOREIGN_VECTOR_INTEROP
  285. /// @{
  286. /// @name Interoperability with other matrix types
  287. ///
  288. /// Construction and assignment are allowed from other classes that
  289. /// appear to be equivalent matrix types, provided that they support
  290. /// double-subscript (i.e., `m[j][i]`) giving the same type as the
  291. /// elements of this matrix, and their total size appears to be the
  292. /// right number of matrix elements.
  293. ///
  294. /// This functionality is disabled for gcc 4.x, which seems to have a
  295. /// compiler bug that results in spurious errors. It can also be
  296. /// disabled by defining IMATH_FOREIGN_VECTOR_INTEROP to be 0 prior to
  297. /// including any Imath header files.
  298. ///
  299. template<typename M, IMATH_ENABLE_IF(has_double_subscript<M,T,3,3>::value)>
  300. IMATH_HOSTDEVICE explicit Matrix33 (const M& m)
  301. : Matrix33(T(m[0][0]), T(m[0][1]), T(m[0][2]),
  302. T(m[1][0]), T(m[1][1]), T(m[1][2]),
  303. T(m[2][0]), T(m[2][1]), T(m[2][2]))
  304. { }
  305. /// Interoperability assignment from another type that behaves as if it
  306. /// were an equivalent matrix.
  307. template<typename M, IMATH_ENABLE_IF(has_double_subscript<M,T,3,3>::value)>
  308. IMATH_HOSTDEVICE const Matrix33& operator= (const M& m)
  309. {
  310. *this = Matrix33(T(m[0][0]), T(m[0][1]), T(m[0][2]),
  311. T(m[1][0]), T(m[1][1]), T(m[1][2]),
  312. T(m[2][0]), T(m[2][1]), T(m[2][2]));
  313. return *this;
  314. }
  315. /// @}
  316. #endif
  317. /// @{
  318. /// @name Compatibility with Sb
  319. /// Return a raw pointer to the array of values
  320. IMATH_HOSTDEVICE T* getValue() IMATH_NOEXCEPT;
  321. /// Return a raw pointer to the array of values
  322. IMATH_HOSTDEVICE const T* getValue() const IMATH_NOEXCEPT;
  323. /// Return the value in `v`
  324. template <class S> IMATH_HOSTDEVICE void getValue (Matrix33<S>& v) const IMATH_NOEXCEPT;
  325. /// Set the value
  326. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33& setValue (const Matrix33<S>& v) IMATH_NOEXCEPT;
  327. /// Set the value
  328. template <class S>
  329. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33& setTheMatrix (const Matrix33<S>& v) IMATH_NOEXCEPT;
  330. /// @}
  331. /// @{
  332. /// @name Arithmetic and Comparison
  333. /// Equality
  334. IMATH_HOSTDEVICE constexpr bool operator== (const Matrix33& v) const IMATH_NOEXCEPT;
  335. /// Inequality
  336. IMATH_HOSTDEVICE constexpr bool operator!= (const Matrix33& v) const IMATH_NOEXCEPT;
  337. /// Compare two matrices and test if they are "approximately equal":
  338. /// @return True if the coefficients of this and `m` are the same
  339. /// with an absolute error of no more than e, i.e., for all i, j:
  340. ///
  341. /// abs (this[i][j] - m[i][j]) <= e
  342. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError (const Matrix33<T>& v, T e) const IMATH_NOEXCEPT;
  343. /// Compare two matrices and test if they are "approximately equal":
  344. /// @return True if the coefficients of this and m are the same with
  345. /// a relative error of no more than e, i.e., for all i, j:
  346. ///
  347. /// abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  348. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError (const Matrix33<T>& v, T e) const IMATH_NOEXCEPT;
  349. /// Component-wise addition
  350. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator+= (const Matrix33& v) IMATH_NOEXCEPT;
  351. /// Component-wise addition
  352. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator+= (T a) IMATH_NOEXCEPT;
  353. /// Component-wise addition
  354. IMATH_HOSTDEVICE constexpr Matrix33 operator+ (const Matrix33& v) const IMATH_NOEXCEPT;
  355. /// Component-wise subtraction
  356. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator-= (const Matrix33& v) IMATH_NOEXCEPT;
  357. /// Component-wise subtraction
  358. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator-= (T a) IMATH_NOEXCEPT;
  359. /// Component-wise subtraction
  360. IMATH_HOSTDEVICE constexpr Matrix33 operator- (const Matrix33& v) const IMATH_NOEXCEPT;
  361. /// Component-wise multiplication by -1
  362. IMATH_HOSTDEVICE constexpr Matrix33 operator-() const IMATH_NOEXCEPT;
  363. /// Component-wise multiplication by -1
  364. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& negate() IMATH_NOEXCEPT;
  365. /// Component-wise multiplication
  366. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator*= (T a) IMATH_NOEXCEPT;
  367. /// Component-wise multiplication
  368. IMATH_HOSTDEVICE constexpr Matrix33 operator* (T a) const IMATH_NOEXCEPT;
  369. /// Component-wise division
  370. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator/= (T a) IMATH_NOEXCEPT;
  371. /// Component-wise division
  372. IMATH_HOSTDEVICE constexpr Matrix33 operator/ (T a) const IMATH_NOEXCEPT;
  373. /// Matrix-matrix multiplication
  374. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& operator*= (const Matrix33& v) IMATH_NOEXCEPT;
  375. /// Matrix-matrix multiplication
  376. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33 operator* (const Matrix33& v) const IMATH_NOEXCEPT;
  377. /// Vector-matrix multiplication: a homogeneous transformation
  378. /// by computing Vec3 (src.x, src.y, 1) * m and dividing by the
  379. /// result's third element.
  380. /// @param[in] src The input vector
  381. /// @param[out] dst The output vector
  382. template <class S> IMATH_HOSTDEVICE void multVecMatrix (const Vec2<S>& src, Vec2<S>& dst) const IMATH_NOEXCEPT;
  383. /// Vector-matrix multiplication: multiply `src` by the upper left 2x2
  384. /// submatrix, ignoring the rest of matrix.
  385. /// @param[in] src The input vector
  386. /// @param[out] dst The output vector
  387. template <class S> IMATH_HOSTDEVICE void multDirMatrix (const Vec2<S>& src, Vec2<S>& dst) const IMATH_NOEXCEPT;
  388. /// @}
  389. /// @{
  390. /// @name Maniplation
  391. /// Set to the identity matrix
  392. IMATH_HOSTDEVICE void makeIdentity() IMATH_NOEXCEPT;
  393. /// Transpose
  394. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& transpose() IMATH_NOEXCEPT;
  395. /// Return the transpose
  396. IMATH_HOSTDEVICE constexpr Matrix33 transposed() const IMATH_NOEXCEPT;
  397. /// Invert in place using the determinant.
  398. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  399. /// @return const reference to this
  400. IMATH_CONSTEXPR14 const Matrix33& invert (bool singExc);
  401. /// Invert in place using the determinant.
  402. /// @return const reference to this
  403. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& invert() IMATH_NOEXCEPT;
  404. /// Return the inverse using the determinant, leaving this unmodified.
  405. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  406. IMATH_CONSTEXPR14 Matrix33<T> inverse (bool singExc) const;
  407. /// Return the inverse using the determinant, leaving this unmodified.
  408. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix33<T> inverse() const IMATH_NOEXCEPT;
  409. /// Invert in place using the Gauss-Jordan method. Significantly slower
  410. /// but more accurate than invert().
  411. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  412. /// @return const reference to this
  413. const Matrix33& gjInvert (bool singExc);
  414. /// Invert in place using the Gauss-Jordan method. Significantly slower
  415. /// but more accurate than invert().
  416. /// @return const reference to this
  417. IMATH_HOSTDEVICE const Matrix33& gjInvert() IMATH_NOEXCEPT;
  418. /// Return the inverse using the Gauss-Jordan method, leaving this
  419. /// unmodified. Significantly slower but more accurate than inverse().
  420. Matrix33<T> gjInverse (bool singExc) const;
  421. /// Return the inverse using the Gauss-Jordan method. Significantly slower,
  422. /// leaving this unmodified. Slower but more accurate than inverse().
  423. IMATH_HOSTDEVICE Matrix33<T> gjInverse() const IMATH_NOEXCEPT;
  424. /// Calculate the matrix minor of the (r,c) element
  425. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T minorOf (const int r, const int c) const IMATH_NOEXCEPT;
  426. /// Build a minor using the specified rows and columns
  427. IMATH_HOSTDEVICE
  428. constexpr T fastMinor (const int r0, const int r1, const int c0, const int c1) const IMATH_NOEXCEPT;
  429. /// Determinant
  430. IMATH_HOSTDEVICE constexpr T determinant() const IMATH_NOEXCEPT;
  431. /// Trace
  432. IMATH_HOSTDEVICE constexpr T trace() const IMATH_NOEXCEPT;
  433. /// Set matrix to rotation by r (in radians)
  434. /// @return const referenced to this
  435. template <class S> IMATH_HOSTDEVICE const Matrix33& setRotation (S r) IMATH_NOEXCEPT;
  436. // Rotate the given matrix by r (in radians)
  437. /// @return const referenced to this
  438. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& rotate (S r) IMATH_NOEXCEPT;
  439. /// Set matrix to scale by given uniform factor
  440. /// @return const referenced to this
  441. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& setScale (T s) IMATH_NOEXCEPT;
  442. /// Set matrix to scale by given vector
  443. /// @return const referenced to this
  444. template <class S>
  445. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& setScale (const Vec2<S>& s) IMATH_NOEXCEPT;
  446. /// Scale the matrix by s
  447. /// @return const referenced to this
  448. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& scale (const Vec2<S>& s) IMATH_NOEXCEPT;
  449. /// Set matrix to translation by given vector
  450. /// @return const referenced to this
  451. template <class S>
  452. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& setTranslation (const Vec2<S>& t) IMATH_NOEXCEPT;
  453. /// Return the translation component
  454. IMATH_HOSTDEVICE constexpr Vec2<T> translation() const IMATH_NOEXCEPT;
  455. /// Translate the matrix by t
  456. /// @return const referenced to this
  457. template <class S>
  458. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& translate (const Vec2<S>& t) IMATH_NOEXCEPT;
  459. /// Set matrix to shear x for each y coord. by given factor xy
  460. /// @return const referenced to this
  461. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& setShear (const S& h) IMATH_NOEXCEPT;
  462. /// Set matrix to shear x for each y coord. by given factor h.x
  463. /// and to shear y for each x coord. by given factor h.y
  464. /// @return const referenced to this
  465. template <class S>
  466. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& setShear (const Vec2<S>& h) IMATH_NOEXCEPT;
  467. /// Shear the matrix in x for each y coord. by given factor xy
  468. /// @return const referenced to this
  469. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& shear (const S& xy) IMATH_NOEXCEPT;
  470. /// Shear the matrix in x for each y coord. by given factor xy
  471. /// and shear y for each x coord. by given factor yx
  472. /// @return const referenced to this
  473. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix33& shear (const Vec2<S>& h) IMATH_NOEXCEPT;
  474. /// @}
  475. /// @{
  476. /// @name Numeric Limits
  477. /// Largest possible negative value
  478. IMATH_HOSTDEVICE constexpr static T baseTypeLowest() IMATH_NOEXCEPT { return std::numeric_limits<T>::lowest(); }
  479. /// Largest possible positive value
  480. IMATH_HOSTDEVICE constexpr static T baseTypeMax() IMATH_NOEXCEPT { return std::numeric_limits<T>::max(); }
  481. /// Smallest possible positive value
  482. IMATH_HOSTDEVICE constexpr static T baseTypeSmallest() IMATH_NOEXCEPT { return std::numeric_limits<T>::min(); }
  483. /// Smallest possible e for which 1+e != 1
  484. IMATH_HOSTDEVICE constexpr static T baseTypeEpsilon() IMATH_NOEXCEPT { return std::numeric_limits<T>::epsilon(); }
  485. /// @}
  486. /// Return the number of the row and column dimensions, i.e. 3.
  487. IMATH_HOSTDEVICE constexpr static unsigned int dimensions() IMATH_NOEXCEPT { return 3; }
  488. /// The base type: In templates that accept a parameter `V` (could be a Color4), you can refer to `T` as `V::BaseType`
  489. typedef T BaseType;
  490. /// The base vector type
  491. typedef Vec3<T> BaseVecType;
  492. };
  493. ///
  494. /// 4x4 transformation matrix
  495. ///
  496. template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix44
  497. {
  498. public:
  499. /// @{
  500. /// @name Direct access to elements
  501. /// Matrix elements
  502. T x[4][4];
  503. /// @}
  504. /// Row access
  505. IMATH_HOSTDEVICE T* operator[] (int i) IMATH_NOEXCEPT;
  506. /// Row access
  507. IMATH_HOSTDEVICE const T* operator[] (int i) const IMATH_NOEXCEPT;
  508. /// @{
  509. /// @name Constructors and Assignment
  510. /// Uninitialized
  511. IMATH_HOSTDEVICE constexpr Matrix44 (Uninitialized) IMATH_NOEXCEPT {}
  512. /// Default constructor: initialize to identity
  513. /// 1 0 0 0
  514. /// 0 1 0 0
  515. /// 0 0 1 0
  516. /// 0 0 0 1
  517. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44() IMATH_NOEXCEPT;
  518. /// Initialize to scalar constant
  519. /// a a a a
  520. /// a a a a
  521. /// a a a a
  522. /// a a a a
  523. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44 (T a) IMATH_NOEXCEPT;
  524. /// Construct from 4x4 array
  525. /// a[0][0] a[0][1] a[0][2] a[0][3]
  526. /// a[1][0] a[1][1] a[1][2] a[1][3]
  527. /// a[2][0] a[2][1] a[2][2] a[2][3]
  528. /// a[3][0] a[3][1] a[3][2] a[3][3]
  529. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44 (const T a[4][4]) IMATH_NOEXCEPT;
  530. /// Construct from given scalar values
  531. /// a b c d
  532. /// e f g h
  533. /// i j k l
  534. /// m n o p
  535. IMATH_HOSTDEVICE IMATH_CONSTEXPR14
  536. Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k, T l, T m, T n, T o, T p) IMATH_NOEXCEPT;
  537. /// Construct from a 3x3 rotation matrix and a translation vector
  538. /// r r r 0
  539. /// r r r 0
  540. /// r r r 0
  541. /// t t t 1
  542. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44 (Matrix33<T> r, Vec3<T> t) IMATH_NOEXCEPT;
  543. /// Copy constructor
  544. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44 (const Matrix44& v) IMATH_NOEXCEPT;
  545. /// Construct from Matrix44 of another base type
  546. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 explicit Matrix44 (const Matrix44<S>& v) IMATH_NOEXCEPT;
  547. /// Assignment operator
  548. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator= (const Matrix44& v) IMATH_NOEXCEPT;
  549. /// Assignment from scalar
  550. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator= (T a) IMATH_NOEXCEPT;
  551. /// Destructor
  552. ~Matrix44() IMATH_NOEXCEPT = default;
  553. /// @}
  554. #if IMATH_FOREIGN_VECTOR_INTEROP
  555. /// @{
  556. /// @name Interoperability with other matrix types
  557. ///
  558. /// Construction and assignment are allowed from other classes that
  559. /// appear to be equivalent matrix types, provided that they support
  560. /// double-subscript (i.e., `m[j][i]`) giving the same type as the
  561. /// elements of this matrix, and their total size appears to be the
  562. /// right number of matrix elements.
  563. ///
  564. /// This functionality is disabled for gcc 4.x, which seems to have a
  565. /// compiler bug that results in spurious errors. It can also be
  566. /// disabled by defining IMATH_FOREIGN_VECTOR_INTEROP to be 0 prior to
  567. /// including any Imath header files.
  568. ///
  569. template<typename M, IMATH_ENABLE_IF(has_double_subscript<M,T,4,4>::value)>
  570. IMATH_HOSTDEVICE explicit Matrix44 (const M& m)
  571. : Matrix44(T(m[0][0]), T(m[0][1]), T(m[0][2]), T(m[0][3]),
  572. T(m[1][0]), T(m[1][1]), T(m[1][2]), T(m[1][3]),
  573. T(m[2][0]), T(m[2][1]), T(m[2][2]), T(m[2][3]),
  574. T(m[3][0]), T(m[3][1]), T(m[3][2]), T(m[3][3]))
  575. { }
  576. /// Interoperability assignment from another type that behaves as if it
  577. /// were an equivalent matrix.
  578. template<typename M, IMATH_ENABLE_IF(has_double_subscript<M,T,4,4>::value)>
  579. IMATH_HOSTDEVICE const Matrix44& operator= (const M& m)
  580. {
  581. *this = Matrix44(T(m[0][0]), T(m[0][1]), T(m[0][2]), T(m[0][3]),
  582. T(m[1][0]), T(m[1][1]), T(m[1][2]), T(m[1][3]),
  583. T(m[2][0]), T(m[2][1]), T(m[2][2]), T(m[2][3]),
  584. T(m[3][0]), T(m[3][1]), T(m[3][2]), T(m[3][3]));
  585. return *this;
  586. }
  587. /// @}
  588. #endif
  589. /// @{
  590. /// @name Compatibility with Sb
  591. /// Return a raw pointer to the array of values
  592. IMATH_HOSTDEVICE T* getValue() IMATH_NOEXCEPT;
  593. /// Return a raw pointer to the array of values
  594. IMATH_HOSTDEVICE const T* getValue() const IMATH_NOEXCEPT;
  595. /// Return the value in `v`
  596. template <class S> IMATH_HOSTDEVICE void getValue (Matrix44<S>& v) const IMATH_NOEXCEPT;
  597. /// Set the value
  598. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44& setValue (const Matrix44<S>& v) IMATH_NOEXCEPT;
  599. /// Set the value
  600. template <class S>
  601. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44& setTheMatrix (const Matrix44<S>& v) IMATH_NOEXCEPT;
  602. /// @}
  603. /// @{
  604. /// @name Arithmetic and Comparison
  605. /// Equality
  606. IMATH_HOSTDEVICE constexpr bool operator== (const Matrix44& v) const IMATH_NOEXCEPT;
  607. /// Inequality
  608. IMATH_HOSTDEVICE constexpr bool operator!= (const Matrix44& v) const IMATH_NOEXCEPT;
  609. /// Compare two matrices and test if they are "approximately equal":
  610. /// @return True if the coefficients of this and `m` are the same
  611. /// with an absolute error of no more than e, i.e., for all i, j:
  612. ///
  613. /// abs (this[i][j] - m[i][j]) <= e
  614. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError (const Matrix44<T>& v, T e) const IMATH_NOEXCEPT;
  615. /// Compare two matrices and test if they are "approximately equal":
  616. /// @return True if the coefficients of this and m are the same with
  617. /// a relative error of no more than e, i.e., for all i, j:
  618. ///
  619. /// abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  620. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError (const Matrix44<T>& v, T e) const IMATH_NOEXCEPT;
  621. /// Component-wise addition
  622. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator+= (const Matrix44& v) IMATH_NOEXCEPT;
  623. /// Component-wise addition
  624. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator+= (T a) IMATH_NOEXCEPT;
  625. /// Component-wise addition
  626. IMATH_HOSTDEVICE constexpr Matrix44 operator+ (const Matrix44& v) const IMATH_NOEXCEPT;
  627. /// Component-wise subtraction
  628. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator-= (const Matrix44& v) IMATH_NOEXCEPT;
  629. /// Component-wise subtraction
  630. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator-= (T a) IMATH_NOEXCEPT;
  631. /// Component-wise subtraction
  632. IMATH_HOSTDEVICE constexpr Matrix44 operator- (const Matrix44& v) const IMATH_NOEXCEPT;
  633. /// Component-wise multiplication by -1
  634. IMATH_HOSTDEVICE constexpr Matrix44 operator-() const IMATH_NOEXCEPT;
  635. /// Component-wise multiplication by -1
  636. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& negate() IMATH_NOEXCEPT;
  637. /// Component-wise multiplication
  638. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator*= (T a) IMATH_NOEXCEPT;
  639. /// Component-wise multiplication
  640. IMATH_HOSTDEVICE constexpr Matrix44 operator* (T a) const IMATH_NOEXCEPT;
  641. /// Component-wise division
  642. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator/= (T a) IMATH_NOEXCEPT;
  643. /// Component-wise division
  644. IMATH_HOSTDEVICE constexpr Matrix44 operator/ (T a) const IMATH_NOEXCEPT;
  645. /// Matrix-matrix multiplication
  646. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& operator*= (const Matrix44& v) IMATH_NOEXCEPT;
  647. /// Matrix-matrix multiplication
  648. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44 operator* (const Matrix44& v) const IMATH_NOEXCEPT;
  649. /// Matrix-matrix multiplication: compute c = a * b
  650. IMATH_HOSTDEVICE
  651. static void multiply (const Matrix44& a, // assumes that
  652. const Matrix44& b, // &a != &c and
  653. Matrix44& c) IMATH_NOEXCEPT; // &b != &c.
  654. /// Matrix-matrix multiplication returning a result.
  655. IMATH_HOSTDEVICE
  656. static IMATH_CONSTEXPR14 Matrix44 multiply (const Matrix44& a, const Matrix44& b) IMATH_NOEXCEPT;
  657. /// Vector-matrix multiplication: a homogeneous transformation
  658. /// by computing Vec3 (src.x, src.y, src.z, 1) * m and dividing by the
  659. /// result's third element.
  660. /// @param[in] src The input vector
  661. /// @param[out] dst The output vector
  662. template <class S> IMATH_HOSTDEVICE void multVecMatrix (const Vec3<S>& src, Vec3<S>& dst) const IMATH_NOEXCEPT;
  663. /// Vector-matrix multiplication: multiply `src` by the upper left 2x2
  664. /// submatrix, ignoring the rest of matrix.
  665. /// @param[in] src The input vector
  666. /// @param[out] dst The output vector
  667. template <class S> IMATH_HOSTDEVICE void multDirMatrix (const Vec3<S>& src, Vec3<S>& dst) const IMATH_NOEXCEPT;
  668. /// @}
  669. /// @{
  670. /// @name Maniplation
  671. /// Set to the identity matrix
  672. IMATH_HOSTDEVICE void makeIdentity() IMATH_NOEXCEPT;
  673. /// Transpose
  674. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& transpose() IMATH_NOEXCEPT;
  675. /// Return the transpose
  676. IMATH_HOSTDEVICE constexpr Matrix44 transposed() const IMATH_NOEXCEPT;
  677. /// Invert in place using the determinant.
  678. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  679. /// @return const reference to this
  680. IMATH_CONSTEXPR14 const Matrix44& invert (bool singExc);
  681. /// Invert in place using the determinant.
  682. /// @return const reference to this
  683. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& invert() IMATH_NOEXCEPT;
  684. /// Return the inverse using the determinant, leaving this unmodified.
  685. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  686. IMATH_CONSTEXPR14 Matrix44<T> inverse (bool singExc) const;
  687. /// Return the inverse using the determinant, leaving this unmodified.
  688. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Matrix44<T> inverse() const IMATH_NOEXCEPT;
  689. /// Invert in place using the Gauss-Jordan method. Significantly slower
  690. /// but more accurate than invert().
  691. /// @param singExc If true, throw an exception if the matrix cannot be inverted.
  692. /// @return const reference to this
  693. IMATH_CONSTEXPR14 const Matrix44& gjInvert (bool singExc);
  694. /// Invert in place using the Gauss-Jordan method. Significantly slower
  695. /// but more accurate than invert().
  696. /// @return const reference to this
  697. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& gjInvert() IMATH_NOEXCEPT;
  698. /// Return the inverse using the Gauss-Jordan method, leaving this
  699. /// unmodified. Significantly slower but more accurate than inverse().
  700. Matrix44<T> gjInverse (bool singExc) const;
  701. /// Return the inverse using the Gauss-Jordan method, leaving this
  702. /// unmodified Significantly slower but more accurate than inverse().
  703. IMATH_HOSTDEVICE Matrix44<T> gjInverse() const IMATH_NOEXCEPT;
  704. /// Calculate the matrix minor of the (r,c) element
  705. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T minorOf (const int r, const int c) const IMATH_NOEXCEPT;
  706. /// Build a minor using the specified rows and columns
  707. IMATH_HOSTDEVICE
  708. constexpr T fastMinor (const int r0,
  709. const int r1,
  710. const int r2,
  711. const int c0,
  712. const int c1,
  713. const int c2) const IMATH_NOEXCEPT;
  714. /// Determinant
  715. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T determinant() const IMATH_NOEXCEPT;
  716. /// Trace
  717. IMATH_HOSTDEVICE constexpr T trace() const IMATH_NOEXCEPT;
  718. /// Set matrix to rotation by XYZ euler angles (in radians)
  719. /// @return const referenced to this
  720. template <class S> IMATH_HOSTDEVICE const Matrix44& setEulerAngles (const Vec3<S>& r) IMATH_NOEXCEPT;
  721. /// Set matrix to rotation around given axis by given angle (in radians)
  722. /// @return const referenced to this
  723. template <class S>
  724. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& setAxisAngle (const Vec3<S>& ax, S ang) IMATH_NOEXCEPT;
  725. /// Rotate the matrix by XYZ euler angles in r (in radians)
  726. /// @return const referenced to this
  727. template <class S> IMATH_HOSTDEVICE const Matrix44& rotate (const Vec3<S>& r) IMATH_NOEXCEPT;
  728. /// Set matrix to scale by given uniform factor
  729. /// @return const referenced to this
  730. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& setScale (T s) IMATH_NOEXCEPT;
  731. /// Set matrix to scale by given vector
  732. /// @return const referenced to this
  733. template <class S>
  734. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& setScale (const Vec3<S>& s) IMATH_NOEXCEPT;
  735. /// Scale the matrix by s
  736. /// @return const referenced to this
  737. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& scale (const Vec3<S>& s) IMATH_NOEXCEPT;
  738. /// Set matrix to translation by given vector
  739. /// @return const referenced to this
  740. template <class S>
  741. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& setTranslation (const Vec3<S>& t) IMATH_NOEXCEPT;
  742. /// Return translation component
  743. IMATH_HOSTDEVICE constexpr const Vec3<T> translation() const IMATH_NOEXCEPT;
  744. /// Translate the matrix by t
  745. /// @return const referenced to this
  746. template <class S>
  747. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& translate (const Vec3<S>& t) IMATH_NOEXCEPT;
  748. /// Set matrix to shear by given vector h. The resulting matrix
  749. /// - will shear x for each y coord. by a factor of h[0] ;
  750. /// - will shear x for each z coord. by a factor of h[1] ;
  751. /// - will shear y for each z coord. by a factor of h[2] .
  752. /// @return const referenced to this
  753. template <class S>
  754. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& setShear (const Vec3<S>& h) IMATH_NOEXCEPT;
  755. /// Set matrix to shear by given factors. The resulting matrix
  756. /// - will shear x for each y coord. by a factor of h.xy ;
  757. /// - will shear x for each z coord. by a factor of h.xz ;
  758. /// - will shear y for each z coord. by a factor of h.yz ;
  759. /// - will shear y for each x coord. by a factor of h.yx ;
  760. /// - will shear z for each x coord. by a factor of h.zx ;
  761. /// - will shear z for each y coord. by a factor of h.zy .
  762. /// @return const referenced to this
  763. template <class S>
  764. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& setShear (const Shear6<S>& h) IMATH_NOEXCEPT;
  765. /// Shear the matrix by given vector. The composed matrix
  766. /// will be `shear` * `this`, where the shear matrix ...
  767. /// - will shear x for each y coord. by a factor of h[0] ;
  768. /// - will shear x for each z coord. by a factor of h[1] ;
  769. /// - will shear y for each z coord. by a factor of h[2] .
  770. /// @return const referenced to this
  771. template <class S> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& shear (const Vec3<S>& h) IMATH_NOEXCEPT;
  772. /// Shear the matrix by the given factors. The composed matrix
  773. /// will be `shear` * `this`, where the shear matrix ...
  774. /// - will shear x for each y coord. by a factor of h.xy ;
  775. /// - will shear x for each z coord. by a factor of h.xz ;
  776. /// - will shear y for each z coord. by a factor of h.yz ;
  777. /// - will shear y for each x coord. by a factor of h.yx ;
  778. /// - will shear z for each x coord. by a factor of h.zx ;
  779. /// - will shear z for each y coord. by a factor of h.zy .
  780. /// @return const referenced to this
  781. template <class S>
  782. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44& shear (const Shear6<S>& h) IMATH_NOEXCEPT;
  783. /// @}
  784. /// @{
  785. /// @name Numeric Limits
  786. /// Largest possible negative value
  787. IMATH_HOSTDEVICE constexpr static T baseTypeLowest() IMATH_NOEXCEPT { return std::numeric_limits<T>::lowest(); }
  788. /// Largest possible positive value
  789. IMATH_HOSTDEVICE constexpr static T baseTypeMax() IMATH_NOEXCEPT { return std::numeric_limits<T>::max(); }
  790. /// Smallest possible positive value
  791. IMATH_HOSTDEVICE constexpr static T baseTypeSmallest() IMATH_NOEXCEPT { return std::numeric_limits<T>::min(); }
  792. /// Smallest possible e for which 1+e != 1
  793. IMATH_HOSTDEVICE constexpr static T baseTypeEpsilon() IMATH_NOEXCEPT { return std::numeric_limits<T>::epsilon(); }
  794. /// @}
  795. /// Return the number of the row and column dimensions, i.e. 4
  796. IMATH_HOSTDEVICE constexpr static unsigned int dimensions() IMATH_NOEXCEPT { return 4; }
  797. /// The base type: In templates that accept a parameter `V` (could be a Color4), you can refer to `T` as `V::BaseType`
  798. typedef T BaseType;
  799. /// The base vector type
  800. typedef Vec4<T> BaseVecType;
  801. };
  802. /// Stream output, as:
  803. /// (m00 m01
  804. /// m10 m11)
  805. template <class T> std::ostream& operator<< (std::ostream& s, const Matrix22<T>& m);
  806. /// Stream output, as:
  807. /// (m00 m01 m02
  808. /// m10 m11 m12
  809. /// m20 m21 m22)
  810. template <class T> std::ostream& operator<< (std::ostream& s, const Matrix33<T>& m);
  811. /// Stream output, as:
  812. ///
  813. /// (m00 m01 m02 m03
  814. /// m10 m11 m12 m13
  815. /// m20 m21 m22 m23
  816. /// m30 m31 m32 m33)
  817. template <class T> std::ostream& operator<< (std::ostream& s, const Matrix44<T>& m);
  818. //---------------------------------------------
  819. // Vector-times-matrix multiplication operators
  820. //---------------------------------------------
  821. /// Vector-matrix multiplication: v *= m
  822. template <class S, class T>
  823. IMATH_HOSTDEVICE inline const Vec2<S>& operator*= (Vec2<S>& v, const Matrix22<T>& m) IMATH_NOEXCEPT;
  824. /// Vector-matrix multiplication: r = v * m
  825. template <class S, class T>
  826. IMATH_HOSTDEVICE inline Vec2<S> operator* (const Vec2<S>& v, const Matrix22<T>& m) IMATH_NOEXCEPT;
  827. /// Vector-matrix multiplication: v *= m
  828. template <class S, class T>
  829. IMATH_HOSTDEVICE inline const Vec2<S>& operator*= (Vec2<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT;
  830. /// Vector-matrix multiplication: r = v * m
  831. template <class S, class T>
  832. IMATH_HOSTDEVICE inline Vec2<S> operator* (const Vec2<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT;
  833. /// Vector-matrix multiplication: v *= m
  834. template <class S, class T>
  835. IMATH_HOSTDEVICE inline const Vec3<S>& operator*= (Vec3<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT;
  836. /// Vector-matrix multiplication: r = v * m
  837. template <class S, class T>
  838. IMATH_HOSTDEVICE inline Vec3<S> operator* (const Vec3<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT;
  839. /// Vector-matrix multiplication: v *= m
  840. template <class S, class T>
  841. IMATH_HOSTDEVICE inline const Vec3<S>& operator*= (Vec3<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT;
  842. /// Vector-matrix multiplication: r = v * m
  843. template <class S, class T>
  844. IMATH_HOSTDEVICE inline Vec3<S> operator* (const Vec3<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT;
  845. /// Vector-matrix multiplication: v *= m
  846. template <class S, class T>
  847. IMATH_HOSTDEVICE inline const Vec4<S>& operator*= (Vec4<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT;
  848. /// Vector-matrix multiplication: r = v * m
  849. template <class S, class T>
  850. IMATH_HOSTDEVICE inline Vec4<S> operator* (const Vec4<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT;
  851. //-------------------------
  852. // Typedefs for convenience
  853. //-------------------------
  854. /// 2x2 matrix of float
  855. typedef Matrix22<float> M22f;
  856. /// 2x2 matrix of double
  857. typedef Matrix22<double> M22d;
  858. /// 3x3 matrix of float
  859. typedef Matrix33<float> M33f;
  860. /// 3x3 matrix of double
  861. typedef Matrix33<double> M33d;
  862. /// 4x4 matrix of float
  863. typedef Matrix44<float> M44f;
  864. /// 4x4 matrix of double
  865. typedef Matrix44<double> M44d;
  866. //---------------------------
  867. // Implementation of Matrix22
  868. //---------------------------
  869. template <class T>
  870. IMATH_HOSTDEVICE inline T*
  871. Matrix22<T>::operator[] (int i) IMATH_NOEXCEPT
  872. {
  873. return x[i];
  874. }
  875. template <class T>
  876. IMATH_HOSTDEVICE inline const T*
  877. Matrix22<T>::operator[] (int i) const IMATH_NOEXCEPT
  878. {
  879. return x[i];
  880. }
  881. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>::Matrix22() IMATH_NOEXCEPT
  882. {
  883. x[0][0] = 1;
  884. x[0][1] = 0;
  885. x[1][0] = 0;
  886. x[1][1] = 1;
  887. }
  888. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>::Matrix22 (T a) IMATH_NOEXCEPT
  889. {
  890. x[0][0] = a;
  891. x[0][1] = a;
  892. x[1][0] = a;
  893. x[1][1] = a;
  894. }
  895. template <class T>
  896. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>::Matrix22 (
  897. const T a[2][2]) IMATH_NOEXCEPT
  898. {
  899. // Function calls and aliasing issues can inhibit vectorization versus
  900. // straight assignment of data members, so instead of this:
  901. // memcpy (x, a, sizeof (x));
  902. // we do this:
  903. x[0][0] = a[0][0];
  904. x[0][1] = a[0][1];
  905. x[1][0] = a[1][0];
  906. x[1][1] = a[1][1];
  907. }
  908. template <class T>
  909. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>::Matrix22 (
  910. T a, T b, T c, T d) IMATH_NOEXCEPT
  911. {
  912. x[0][0] = a;
  913. x[0][1] = b;
  914. x[1][0] = c;
  915. x[1][1] = d;
  916. }
  917. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>::Matrix22 (const Matrix22& v) IMATH_NOEXCEPT
  918. {
  919. // Function calls and aliasing issues can inhibit vectorization versus
  920. // straight assignment of data members, so we don't do this:
  921. // memcpy (x, v.x, sizeof (x));
  922. // we do this:
  923. x[0][0] = v.x[0][0];
  924. x[0][1] = v.x[0][1];
  925. x[1][0] = v.x[1][0];
  926. x[1][1] = v.x[1][1];
  927. }
  928. template <class T>
  929. template <class S>
  930. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>::Matrix22 (const Matrix22<S>& v) IMATH_NOEXCEPT
  931. {
  932. x[0][0] = T (v.x[0][0]);
  933. x[0][1] = T (v.x[0][1]);
  934. x[1][0] = T (v.x[1][0]);
  935. x[1][1] = T (v.x[1][1]);
  936. }
  937. template <class T>
  938. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  939. Matrix22<T>::operator= (const Matrix22& v) IMATH_NOEXCEPT
  940. {
  941. // Function calls and aliasing issues can inhibit vectorization versus
  942. // straight assignment of data members, so we don't do this:
  943. // memcpy (x, v.x, sizeof (x));
  944. // we do this:
  945. x[0][0] = v.x[0][0];
  946. x[0][1] = v.x[0][1];
  947. x[1][0] = v.x[1][0];
  948. x[1][1] = v.x[1][1];
  949. return *this;
  950. }
  951. template <class T>
  952. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  953. Matrix22<T>::operator= (T a) IMATH_NOEXCEPT
  954. {
  955. x[0][0] = a;
  956. x[0][1] = a;
  957. x[1][0] = a;
  958. x[1][1] = a;
  959. return *this;
  960. }
  961. template <class T>
  962. IMATH_HOSTDEVICE inline T*
  963. Matrix22<T>::getValue () IMATH_NOEXCEPT
  964. {
  965. return (T*) &x[0][0];
  966. }
  967. template <class T>
  968. IMATH_HOSTDEVICE inline const T*
  969. Matrix22<T>::getValue() const IMATH_NOEXCEPT
  970. {
  971. return (const T*) &x[0][0];
  972. }
  973. template <class T>
  974. template <class S>
  975. IMATH_HOSTDEVICE inline void
  976. Matrix22<T>::getValue (Matrix22<S>& v) const IMATH_NOEXCEPT
  977. {
  978. v.x[0][0] = x[0][0];
  979. v.x[0][1] = x[0][1];
  980. v.x[1][0] = x[1][0];
  981. v.x[1][1] = x[1][1];
  982. }
  983. template <class T>
  984. template <class S>
  985. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>&
  986. Matrix22<T>::setValue (const Matrix22<S>& v) IMATH_NOEXCEPT
  987. {
  988. x[0][0] = v.x[0][0];
  989. x[0][1] = v.x[0][1];
  990. x[1][0] = v.x[1][0];
  991. x[1][1] = v.x[1][1];
  992. return *this;
  993. }
  994. template <class T>
  995. template <class S>
  996. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>&
  997. Matrix22<T>::setTheMatrix (const Matrix22<S>& v) IMATH_NOEXCEPT
  998. {
  999. x[0][0] = v.x[0][0];
  1000. x[0][1] = v.x[0][1];
  1001. x[1][0] = v.x[1][0];
  1002. x[1][1] = v.x[1][1];
  1003. return *this;
  1004. }
  1005. template <class T>
  1006. IMATH_HOSTDEVICE inline void
  1007. Matrix22<T>::makeIdentity() IMATH_NOEXCEPT
  1008. {
  1009. x[0][0] = 1;
  1010. x[0][1] = 0;
  1011. x[1][0] = 0;
  1012. x[1][1] = 1;
  1013. }
  1014. template <class T>
  1015. IMATH_HOSTDEVICE constexpr inline bool
  1016. Matrix22<T>::operator== (const Matrix22& v) const IMATH_NOEXCEPT
  1017. {
  1018. return x[0][0] == v.x[0][0] && x[0][1] == v.x[0][1] && x[1][0] == v.x[1][0] &&
  1019. x[1][1] == v.x[1][1];
  1020. }
  1021. template <class T>
  1022. IMATH_HOSTDEVICE constexpr inline bool
  1023. Matrix22<T>::operator!= (const Matrix22& v) const IMATH_NOEXCEPT
  1024. {
  1025. return x[0][0] != v.x[0][0] || x[0][1] != v.x[0][1] || x[1][0] != v.x[1][0] ||
  1026. x[1][1] != v.x[1][1];
  1027. }
  1028. template <class T>
  1029. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1030. Matrix22<T>::equalWithAbsError (const Matrix22<T>& m, T e) const IMATH_NOEXCEPT
  1031. {
  1032. for (int i = 0; i < 2; i++)
  1033. for (int j = 0; j < 2; j++)
  1034. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this).x[i][j], m.x[i][j], e))
  1035. return false;
  1036. return true;
  1037. }
  1038. template <class T>
  1039. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1040. Matrix22<T>::equalWithRelError (const Matrix22<T>& m, T e) const IMATH_NOEXCEPT
  1041. {
  1042. for (int i = 0; i < 2; i++)
  1043. for (int j = 0; j < 2; j++)
  1044. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this).x[i][j], m.x[i][j], e))
  1045. return false;
  1046. return true;
  1047. }
  1048. template <class T>
  1049. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1050. Matrix22<T>::operator+= (const Matrix22<T>& v) IMATH_NOEXCEPT
  1051. {
  1052. x[0][0] += v.x[0][0];
  1053. x[0][1] += v.x[0][1];
  1054. x[1][0] += v.x[1][0];
  1055. x[1][1] += v.x[1][1];
  1056. return *this;
  1057. }
  1058. template <class T>
  1059. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1060. Matrix22<T>::operator+= (T a) IMATH_NOEXCEPT
  1061. {
  1062. x[0][0] += a;
  1063. x[0][1] += a;
  1064. x[1][0] += a;
  1065. x[1][1] += a;
  1066. return *this;
  1067. }
  1068. template <class T>
  1069. IMATH_HOSTDEVICE constexpr inline Matrix22<T>
  1070. Matrix22<T>::operator+ (const Matrix22<T>& v) const IMATH_NOEXCEPT
  1071. {
  1072. return Matrix22 (x[0][0] + v.x[0][0],
  1073. x[0][1] + v.x[0][1],
  1074. x[1][0] + v.x[1][0],
  1075. x[1][1] + v.x[1][1]);
  1076. }
  1077. template <class T>
  1078. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1079. Matrix22<T>::operator-= (const Matrix22<T>& v) IMATH_NOEXCEPT
  1080. {
  1081. x[0][0] -= v.x[0][0];
  1082. x[0][1] -= v.x[0][1];
  1083. x[1][0] -= v.x[1][0];
  1084. x[1][1] -= v.x[1][1];
  1085. return *this;
  1086. }
  1087. template <class T>
  1088. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1089. Matrix22<T>::operator-= (T a) IMATH_NOEXCEPT
  1090. {
  1091. x[0][0] -= a;
  1092. x[0][1] -= a;
  1093. x[1][0] -= a;
  1094. x[1][1] -= a;
  1095. return *this;
  1096. }
  1097. template <class T>
  1098. IMATH_HOSTDEVICE constexpr inline Matrix22<T>
  1099. Matrix22<T>::operator- (const Matrix22<T>& v) const IMATH_NOEXCEPT
  1100. {
  1101. return Matrix22 (x[0][0] - v.x[0][0],
  1102. x[0][1] - v.x[0][1],
  1103. x[1][0] - v.x[1][0],
  1104. x[1][1] - v.x[1][1]);
  1105. }
  1106. template <class T>
  1107. IMATH_HOSTDEVICE constexpr inline Matrix22<T>
  1108. Matrix22<T>::operator-() const IMATH_NOEXCEPT
  1109. {
  1110. return Matrix22 (-x[0][0], -x[0][1], -x[1][0], -x[1][1]);
  1111. }
  1112. template <class T>
  1113. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1114. Matrix22<T>::negate() IMATH_NOEXCEPT
  1115. {
  1116. x[0][0] = -x[0][0];
  1117. x[0][1] = -x[0][1];
  1118. x[1][0] = -x[1][0];
  1119. x[1][1] = -x[1][1];
  1120. return *this;
  1121. }
  1122. template <class T>
  1123. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1124. Matrix22<T>::operator*= (T a) IMATH_NOEXCEPT
  1125. {
  1126. x[0][0] *= a;
  1127. x[0][1] *= a;
  1128. x[1][0] *= a;
  1129. x[1][1] *= a;
  1130. return *this;
  1131. }
  1132. template <class T>
  1133. IMATH_HOSTDEVICE constexpr inline Matrix22<T>
  1134. Matrix22<T>::operator* (T a) const IMATH_NOEXCEPT
  1135. {
  1136. return Matrix22 (x[0][0] * a, x[0][1] * a, x[1][0] * a, x[1][1] * a);
  1137. }
  1138. /// Matrix-scalar multiplication
  1139. template <class T>
  1140. IMATH_HOSTDEVICE inline Matrix22<T>
  1141. operator* (T a, const Matrix22<T>& v) IMATH_NOEXCEPT
  1142. {
  1143. return v * a;
  1144. }
  1145. template <class T>
  1146. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1147. Matrix22<T>::operator*= (const Matrix22<T>& v) IMATH_NOEXCEPT
  1148. {
  1149. Matrix22 tmp (T (0));
  1150. for (int i = 0; i < 2; i++)
  1151. for (int j = 0; j < 2; j++)
  1152. for (int k = 0; k < 2; k++)
  1153. tmp.x[i][j] += x[i][k] * v.x[k][j];
  1154. *this = tmp;
  1155. return *this;
  1156. }
  1157. template <class T>
  1158. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>
  1159. Matrix22<T>::operator* (const Matrix22<T>& v) const IMATH_NOEXCEPT
  1160. {
  1161. Matrix22 tmp (T (0));
  1162. for (int i = 0; i < 2; i++)
  1163. for (int j = 0; j < 2; j++)
  1164. for (int k = 0; k < 2; k++)
  1165. tmp.x[i][j] += x[i][k] * v.x[k][j];
  1166. return tmp;
  1167. }
  1168. template <class T>
  1169. template <class S>
  1170. IMATH_HOSTDEVICE inline void
  1171. Matrix22<T>::multDirMatrix (const Vec2<S>& src, Vec2<S>& dst) const IMATH_NOEXCEPT
  1172. {
  1173. S a, b;
  1174. a = src.x * x[0][0] + src.y * x[1][0];
  1175. b = src.x * x[0][1] + src.y * x[1][1];
  1176. dst.x = a;
  1177. dst.y = b;
  1178. }
  1179. template <class T>
  1180. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1181. Matrix22<T>::operator/= (T a) IMATH_NOEXCEPT
  1182. {
  1183. x[0][0] /= a;
  1184. x[0][1] /= a;
  1185. x[1][0] /= a;
  1186. x[1][1] /= a;
  1187. return *this;
  1188. }
  1189. template <class T>
  1190. IMATH_HOSTDEVICE constexpr inline Matrix22<T>
  1191. Matrix22<T>::operator/ (T a) const IMATH_NOEXCEPT
  1192. {
  1193. return Matrix22 (x[0][0] / a, x[0][1] / a, x[1][0] / a, x[1][1] / a);
  1194. }
  1195. template <class T>
  1196. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1197. Matrix22<T>::transpose() IMATH_NOEXCEPT
  1198. {
  1199. Matrix22 tmp (x[0][0], x[1][0], x[0][1], x[1][1]);
  1200. *this = tmp;
  1201. return *this;
  1202. }
  1203. template <class T>
  1204. IMATH_HOSTDEVICE constexpr inline Matrix22<T>
  1205. Matrix22<T>::transposed() const IMATH_NOEXCEPT
  1206. {
  1207. return Matrix22 (x[0][0], x[1][0], x[0][1], x[1][1]);
  1208. }
  1209. template <class T>
  1210. IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1211. Matrix22<T>::invert (bool singExc)
  1212. {
  1213. *this = inverse (singExc);
  1214. return *this;
  1215. }
  1216. template <class T>
  1217. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1218. Matrix22<T>::invert() IMATH_NOEXCEPT
  1219. {
  1220. *this = inverse();
  1221. return *this;
  1222. }
  1223. template <class T>
  1224. IMATH_CONSTEXPR14 inline Matrix22<T>
  1225. Matrix22<T>::inverse (bool singExc) const
  1226. {
  1227. Matrix22 s (x[1][1], -x[0][1], -x[1][0], x[0][0]);
  1228. T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
  1229. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  1230. {
  1231. for (int i = 0; i < 2; ++i)
  1232. {
  1233. for (int j = 0; j < 2; ++j)
  1234. {
  1235. s[i][j] /= r;
  1236. }
  1237. }
  1238. }
  1239. else
  1240. {
  1241. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  1242. for (int i = 0; i < 2; ++i)
  1243. {
  1244. for (int j = 0; j < 2; ++j)
  1245. {
  1246. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s[i][j]))
  1247. {
  1248. s[i][j] /= r;
  1249. }
  1250. else
  1251. {
  1252. if (singExc)
  1253. throw std::invalid_argument ("Cannot invert "
  1254. "singular matrix.");
  1255. return Matrix22();
  1256. }
  1257. }
  1258. }
  1259. }
  1260. return s;
  1261. }
  1262. template <class T>
  1263. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix22<T>
  1264. Matrix22<T>::inverse() const IMATH_NOEXCEPT
  1265. {
  1266. Matrix22 s (x[1][1], -x[0][1], -x[1][0], x[0][0]);
  1267. T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
  1268. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  1269. {
  1270. for (int i = 0; i < 2; ++i)
  1271. {
  1272. for (int j = 0; j < 2; ++j)
  1273. {
  1274. s[i][j] /= r;
  1275. }
  1276. }
  1277. }
  1278. else
  1279. {
  1280. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  1281. for (int i = 0; i < 2; ++i)
  1282. {
  1283. for (int j = 0; j < 2; ++j)
  1284. {
  1285. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s[i][j]))
  1286. {
  1287. s[i][j] /= r;
  1288. }
  1289. else
  1290. {
  1291. return Matrix22();
  1292. }
  1293. }
  1294. }
  1295. }
  1296. return s;
  1297. }
  1298. template <class T>
  1299. IMATH_HOSTDEVICE constexpr inline T
  1300. Matrix22<T>::determinant() const IMATH_NOEXCEPT
  1301. {
  1302. return x[0][0] * x[1][1] - x[1][0] * x[0][1];
  1303. }
  1304. template <class T>
  1305. IMATH_HOSTDEVICE constexpr inline T
  1306. Matrix22<T>::trace () const IMATH_NOEXCEPT
  1307. {
  1308. return x[0][0] + x[1][1];
  1309. }
  1310. template <class T>
  1311. template <class S>
  1312. IMATH_HOSTDEVICE inline const Matrix22<T>&
  1313. Matrix22<T>::setRotation (S r) IMATH_NOEXCEPT
  1314. {
  1315. S cos_r, sin_r;
  1316. cos_r = cos ((T) r);
  1317. sin_r = sin ((T) r);
  1318. x[0][0] = cos_r;
  1319. x[0][1] = sin_r;
  1320. x[1][0] = -sin_r;
  1321. x[1][1] = cos_r;
  1322. return *this;
  1323. }
  1324. template <class T>
  1325. template <class S>
  1326. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1327. Matrix22<T>::rotate (S r) IMATH_NOEXCEPT
  1328. {
  1329. *this *= Matrix22<T>().setRotation (r);
  1330. return *this;
  1331. }
  1332. template <class T>
  1333. IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1334. Matrix22<T>::setScale (T s) IMATH_NOEXCEPT
  1335. {
  1336. //
  1337. // Set the matrix to:
  1338. // | s 0 |
  1339. // | 0 s |
  1340. //
  1341. x[0][0] = s;
  1342. x[0][1] = static_cast<T> (0);
  1343. x[1][0] = static_cast<T> (0);
  1344. x[1][1] = s;
  1345. return *this;
  1346. }
  1347. template <class T>
  1348. template <class S>
  1349. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1350. Matrix22<T>::setScale (const Vec2<S>& s) IMATH_NOEXCEPT
  1351. {
  1352. //
  1353. // Set the matrix to:
  1354. // | s.x 0 |
  1355. // | 0 s.y |
  1356. //
  1357. x[0][0] = s.x;
  1358. x[0][1] = static_cast<T> (0);
  1359. x[1][0] = static_cast<T> (0);
  1360. x[1][1] = s.y;
  1361. return *this;
  1362. }
  1363. template <class T>
  1364. template <class S>
  1365. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix22<T>&
  1366. Matrix22<T>::scale (const Vec2<S>& s) IMATH_NOEXCEPT
  1367. {
  1368. x[0][0] *= s.x;
  1369. x[0][1] *= s.x;
  1370. x[1][0] *= s.y;
  1371. x[1][1] *= s.y;
  1372. return *this;
  1373. }
  1374. //---------------------------
  1375. // Implementation of Matrix33
  1376. //---------------------------
  1377. template <class T>
  1378. IMATH_HOSTDEVICE inline T*
  1379. Matrix33<T>::operator[] (int i) IMATH_NOEXCEPT
  1380. {
  1381. return x[i];
  1382. }
  1383. template <class T>
  1384. IMATH_HOSTDEVICE inline const T*
  1385. Matrix33<T>::operator[] (int i) const IMATH_NOEXCEPT
  1386. {
  1387. return x[i];
  1388. }
  1389. template <class T>
  1390. IMATH_HOSTDEVICE inline IMATH_CONSTEXPR14
  1391. Matrix33<T>::Matrix33() IMATH_NOEXCEPT
  1392. {
  1393. x[0][0] = 1;
  1394. x[0][1] = 0;
  1395. x[0][2] = 0;
  1396. x[1][0] = 0;
  1397. x[1][1] = 1;
  1398. x[1][2] = 0;
  1399. x[2][0] = 0;
  1400. x[2][1] = 0;
  1401. x[2][2] = 1;
  1402. }
  1403. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>::Matrix33 (T a) IMATH_NOEXCEPT
  1404. {
  1405. x[0][0] = a;
  1406. x[0][1] = a;
  1407. x[0][2] = a;
  1408. x[1][0] = a;
  1409. x[1][1] = a;
  1410. x[1][2] = a;
  1411. x[2][0] = a;
  1412. x[2][1] = a;
  1413. x[2][2] = a;
  1414. }
  1415. template <class T>
  1416. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>::Matrix33 (
  1417. const T a[3][3]) IMATH_NOEXCEPT
  1418. {
  1419. // Function calls and aliasing issues can inhibit vectorization versus
  1420. // straight assignment of data members, so instead of this:
  1421. // memcpy (x, a, sizeof (x));
  1422. // we do this:
  1423. x[0][0] = a[0][0];
  1424. x[0][1] = a[0][1];
  1425. x[0][2] = a[0][2];
  1426. x[1][0] = a[1][0];
  1427. x[1][1] = a[1][1];
  1428. x[1][2] = a[1][2];
  1429. x[2][0] = a[2][0];
  1430. x[2][1] = a[2][1];
  1431. x[2][2] = a[2][2];
  1432. }
  1433. template <class T>
  1434. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>::Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i) IMATH_NOEXCEPT
  1435. {
  1436. x[0][0] = a;
  1437. x[0][1] = b;
  1438. x[0][2] = c;
  1439. x[1][0] = d;
  1440. x[1][1] = e;
  1441. x[1][2] = f;
  1442. x[2][0] = g;
  1443. x[2][1] = h;
  1444. x[2][2] = i;
  1445. }
  1446. template <class T>
  1447. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>::Matrix33 (const Matrix33& v) IMATH_NOEXCEPT
  1448. {
  1449. // Function calls and aliasing issues can inhibit vectorization versus
  1450. // straight assignment of data members, so instead of this:
  1451. // memcpy (x, v.x, sizeof (x));
  1452. // we do this:
  1453. x[0][0] = v.x[0][0];
  1454. x[0][1] = v.x[0][1];
  1455. x[0][2] = v.x[0][2];
  1456. x[1][0] = v.x[1][0];
  1457. x[1][1] = v.x[1][1];
  1458. x[1][2] = v.x[1][2];
  1459. x[2][0] = v.x[2][0];
  1460. x[2][1] = v.x[2][1];
  1461. x[2][2] = v.x[2][2];
  1462. }
  1463. template <class T>
  1464. template <class S>
  1465. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>::Matrix33 (const Matrix33<S>& v) IMATH_NOEXCEPT
  1466. {
  1467. x[0][0] = T (v.x[0][0]);
  1468. x[0][1] = T (v.x[0][1]);
  1469. x[0][2] = T (v.x[0][2]);
  1470. x[1][0] = T (v.x[1][0]);
  1471. x[1][1] = T (v.x[1][1]);
  1472. x[1][2] = T (v.x[1][2]);
  1473. x[2][0] = T (v.x[2][0]);
  1474. x[2][1] = T (v.x[2][1]);
  1475. x[2][2] = T (v.x[2][2]);
  1476. }
  1477. template <class T>
  1478. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1479. Matrix33<T>::operator= (const Matrix33& v) IMATH_NOEXCEPT
  1480. {
  1481. // Function calls and aliasing issues can inhibit vectorization versus
  1482. // straight assignment of data members, so instead of this:
  1483. // memcpy (x, v.x, sizeof (x));
  1484. // we do this:
  1485. x[0][0] = v.x[0][0];
  1486. x[0][1] = v.x[0][1];
  1487. x[0][2] = v.x[0][2];
  1488. x[1][0] = v.x[1][0];
  1489. x[1][1] = v.x[1][1];
  1490. x[1][2] = v.x[1][2];
  1491. x[2][0] = v.x[2][0];
  1492. x[2][1] = v.x[2][1];
  1493. x[2][2] = v.x[2][2];
  1494. return *this;
  1495. }
  1496. template <class T>
  1497. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1498. Matrix33<T>::operator= (T a) IMATH_NOEXCEPT
  1499. {
  1500. x[0][0] = a;
  1501. x[0][1] = a;
  1502. x[0][2] = a;
  1503. x[1][0] = a;
  1504. x[1][1] = a;
  1505. x[1][2] = a;
  1506. x[2][0] = a;
  1507. x[2][1] = a;
  1508. x[2][2] = a;
  1509. return *this;
  1510. }
  1511. template <class T>
  1512. IMATH_HOSTDEVICE inline T*
  1513. Matrix33<T>::getValue () IMATH_NOEXCEPT
  1514. {
  1515. return (T*) &x[0][0];
  1516. }
  1517. template <class T>
  1518. IMATH_HOSTDEVICE inline const T*
  1519. Matrix33<T>::getValue() const IMATH_NOEXCEPT
  1520. {
  1521. return (const T*) &x[0][0];
  1522. }
  1523. template <class T>
  1524. template <class S>
  1525. IMATH_HOSTDEVICE inline void
  1526. Matrix33<T>::getValue (Matrix33<S>& v) const IMATH_NOEXCEPT
  1527. {
  1528. v.x[0][0] = x[0][0];
  1529. v.x[0][1] = x[0][1];
  1530. v.x[0][2] = x[0][2];
  1531. v.x[1][0] = x[1][0];
  1532. v.x[1][1] = x[1][1];
  1533. v.x[1][2] = x[1][2];
  1534. v.x[2][0] = x[2][0];
  1535. v.x[2][1] = x[2][1];
  1536. v.x[2][2] = x[2][2];
  1537. }
  1538. template <class T>
  1539. template <class S>
  1540. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>&
  1541. Matrix33<T>::setValue (const Matrix33<S>& v) IMATH_NOEXCEPT
  1542. {
  1543. x[0][0] = v.x[0][0];
  1544. x[0][1] = v.x[0][1];
  1545. x[0][2] = v.x[0][2];
  1546. x[1][0] = v.x[1][0];
  1547. x[1][1] = v.x[1][1];
  1548. x[1][2] = v.x[1][2];
  1549. x[2][0] = v.x[2][0];
  1550. x[2][1] = v.x[2][1];
  1551. x[2][2] = v.x[2][2];
  1552. return *this;
  1553. }
  1554. template <class T>
  1555. template <class S>
  1556. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>&
  1557. Matrix33<T>::setTheMatrix (const Matrix33<S>& v) IMATH_NOEXCEPT
  1558. {
  1559. x[0][0] = v.x[0][0];
  1560. x[0][1] = v.x[0][1];
  1561. x[0][2] = v.x[0][2];
  1562. x[1][0] = v.x[1][0];
  1563. x[1][1] = v.x[1][1];
  1564. x[1][2] = v.x[1][2];
  1565. x[2][0] = v.x[2][0];
  1566. x[2][1] = v.x[2][1];
  1567. x[2][2] = v.x[2][2];
  1568. return *this;
  1569. }
  1570. template <class T>
  1571. IMATH_HOSTDEVICE inline void
  1572. Matrix33<T>::makeIdentity() IMATH_NOEXCEPT
  1573. {
  1574. x[0][0] = 1;
  1575. x[0][1] = 0;
  1576. x[0][2] = 0;
  1577. x[1][0] = 0;
  1578. x[1][1] = 1;
  1579. x[1][2] = 0;
  1580. x[2][0] = 0;
  1581. x[2][1] = 0;
  1582. x[2][2] = 1;
  1583. }
  1584. template <class T>
  1585. IMATH_HOSTDEVICE constexpr inline bool
  1586. Matrix33<T>::operator== (const Matrix33& v) const IMATH_NOEXCEPT
  1587. {
  1588. return x[0][0] == v.x[0][0] && x[0][1] == v.x[0][1] && x[0][2] == v.x[0][2] &&
  1589. x[1][0] == v.x[1][0] && x[1][1] == v.x[1][1] && x[1][2] == v.x[1][2] &&
  1590. x[2][0] == v.x[2][0] && x[2][1] == v.x[2][1] && x[2][2] == v.x[2][2];
  1591. }
  1592. template <class T>
  1593. IMATH_HOSTDEVICE constexpr inline bool
  1594. Matrix33<T>::operator!= (const Matrix33& v) const IMATH_NOEXCEPT
  1595. {
  1596. return x[0][0] != v.x[0][0] || x[0][1] != v.x[0][1] || x[0][2] != v.x[0][2] ||
  1597. x[1][0] != v.x[1][0] || x[1][1] != v.x[1][1] || x[1][2] != v.x[1][2] ||
  1598. x[2][0] != v.x[2][0] || x[2][1] != v.x[2][1] || x[2][2] != v.x[2][2];
  1599. }
  1600. template <class T>
  1601. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1602. Matrix33<T>::equalWithAbsError (const Matrix33<T>& m, T e) const IMATH_NOEXCEPT
  1603. {
  1604. for (int i = 0; i < 3; i++)
  1605. for (int j = 0; j < 3; j++)
  1606. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i][j], m[i][j], e))
  1607. return false;
  1608. return true;
  1609. }
  1610. template <class T>
  1611. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1612. Matrix33<T>::equalWithRelError (const Matrix33<T>& m, T e) const IMATH_NOEXCEPT
  1613. {
  1614. for (int i = 0; i < 3; i++)
  1615. for (int j = 0; j < 3; j++)
  1616. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i][j], m[i][j], e))
  1617. return false;
  1618. return true;
  1619. }
  1620. template <class T>
  1621. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1622. Matrix33<T>::operator+= (const Matrix33<T>& v) IMATH_NOEXCEPT
  1623. {
  1624. x[0][0] += v.x[0][0];
  1625. x[0][1] += v.x[0][1];
  1626. x[0][2] += v.x[0][2];
  1627. x[1][0] += v.x[1][0];
  1628. x[1][1] += v.x[1][1];
  1629. x[1][2] += v.x[1][2];
  1630. x[2][0] += v.x[2][0];
  1631. x[2][1] += v.x[2][1];
  1632. x[2][2] += v.x[2][2];
  1633. return *this;
  1634. }
  1635. template <class T>
  1636. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1637. Matrix33<T>::operator+= (T a) IMATH_NOEXCEPT
  1638. {
  1639. x[0][0] += a;
  1640. x[0][1] += a;
  1641. x[0][2] += a;
  1642. x[1][0] += a;
  1643. x[1][1] += a;
  1644. x[1][2] += a;
  1645. x[2][0] += a;
  1646. x[2][1] += a;
  1647. x[2][2] += a;
  1648. return *this;
  1649. }
  1650. template <class T>
  1651. IMATH_HOSTDEVICE constexpr inline Matrix33<T>
  1652. Matrix33<T>::operator+ (const Matrix33<T>& v) const IMATH_NOEXCEPT
  1653. {
  1654. return Matrix33 (x[0][0] + v.x[0][0],
  1655. x[0][1] + v.x[0][1],
  1656. x[0][2] + v.x[0][2],
  1657. x[1][0] + v.x[1][0],
  1658. x[1][1] + v.x[1][1],
  1659. x[1][2] + v.x[1][2],
  1660. x[2][0] + v.x[2][0],
  1661. x[2][1] + v.x[2][1],
  1662. x[2][2] + v.x[2][2]);
  1663. }
  1664. template <class T>
  1665. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1666. Matrix33<T>::operator-= (const Matrix33<T>& v) IMATH_NOEXCEPT
  1667. {
  1668. x[0][0] -= v.x[0][0];
  1669. x[0][1] -= v.x[0][1];
  1670. x[0][2] -= v.x[0][2];
  1671. x[1][0] -= v.x[1][0];
  1672. x[1][1] -= v.x[1][1];
  1673. x[1][2] -= v.x[1][2];
  1674. x[2][0] -= v.x[2][0];
  1675. x[2][1] -= v.x[2][1];
  1676. x[2][2] -= v.x[2][2];
  1677. return *this;
  1678. }
  1679. template <class T>
  1680. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1681. Matrix33<T>::operator-= (T a) IMATH_NOEXCEPT
  1682. {
  1683. x[0][0] -= a;
  1684. x[0][1] -= a;
  1685. x[0][2] -= a;
  1686. x[1][0] -= a;
  1687. x[1][1] -= a;
  1688. x[1][2] -= a;
  1689. x[2][0] -= a;
  1690. x[2][1] -= a;
  1691. x[2][2] -= a;
  1692. return *this;
  1693. }
  1694. template <class T>
  1695. IMATH_HOSTDEVICE constexpr inline Matrix33<T>
  1696. Matrix33<T>::operator- (const Matrix33<T>& v) const IMATH_NOEXCEPT
  1697. {
  1698. return Matrix33 (x[0][0] - v.x[0][0],
  1699. x[0][1] - v.x[0][1],
  1700. x[0][2] - v.x[0][2],
  1701. x[1][0] - v.x[1][0],
  1702. x[1][1] - v.x[1][1],
  1703. x[1][2] - v.x[1][2],
  1704. x[2][0] - v.x[2][0],
  1705. x[2][1] - v.x[2][1],
  1706. x[2][2] - v.x[2][2]);
  1707. }
  1708. template <class T>
  1709. IMATH_HOSTDEVICE constexpr inline Matrix33<T>
  1710. Matrix33<T>::operator-() const IMATH_NOEXCEPT
  1711. {
  1712. return Matrix33 (-x[0][0],
  1713. -x[0][1],
  1714. -x[0][2],
  1715. -x[1][0],
  1716. -x[1][1],
  1717. -x[1][2],
  1718. -x[2][0],
  1719. -x[2][1],
  1720. -x[2][2]);
  1721. }
  1722. template <class T>
  1723. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1724. Matrix33<T>::negate() IMATH_NOEXCEPT
  1725. {
  1726. x[0][0] = -x[0][0];
  1727. x[0][1] = -x[0][1];
  1728. x[0][2] = -x[0][2];
  1729. x[1][0] = -x[1][0];
  1730. x[1][1] = -x[1][1];
  1731. x[1][2] = -x[1][2];
  1732. x[2][0] = -x[2][0];
  1733. x[2][1] = -x[2][1];
  1734. x[2][2] = -x[2][2];
  1735. return *this;
  1736. }
  1737. template <class T>
  1738. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1739. Matrix33<T>::operator*= (T a) IMATH_NOEXCEPT
  1740. {
  1741. x[0][0] *= a;
  1742. x[0][1] *= a;
  1743. x[0][2] *= a;
  1744. x[1][0] *= a;
  1745. x[1][1] *= a;
  1746. x[1][2] *= a;
  1747. x[2][0] *= a;
  1748. x[2][1] *= a;
  1749. x[2][2] *= a;
  1750. return *this;
  1751. }
  1752. template <class T>
  1753. IMATH_HOSTDEVICE constexpr inline Matrix33<T>
  1754. Matrix33<T>::operator* (T a) const IMATH_NOEXCEPT
  1755. {
  1756. return Matrix33 (x[0][0] * a,
  1757. x[0][1] * a,
  1758. x[0][2] * a,
  1759. x[1][0] * a,
  1760. x[1][1] * a,
  1761. x[1][2] * a,
  1762. x[2][0] * a,
  1763. x[2][1] * a,
  1764. x[2][2] * a);
  1765. }
  1766. /// Matrix-scalar multiplication
  1767. template <class T>
  1768. IMATH_HOSTDEVICE inline Matrix33<T> constexpr
  1769. operator* (T a, const Matrix33<T>& v) IMATH_NOEXCEPT
  1770. {
  1771. return v * a;
  1772. }
  1773. template <class T>
  1774. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1775. Matrix33<T>::operator*= (const Matrix33<T>& v) IMATH_NOEXCEPT
  1776. {
  1777. // Avoid initializing with 0 values before immediately overwriting them,
  1778. // and unroll all loops for the best autovectorization.
  1779. Matrix33 tmp(IMATH_INTERNAL_NAMESPACE::UNINITIALIZED);
  1780. tmp.x[0][0] = x[0][0] * v.x[0][0] + x[0][1] * v.x[1][0] + x[0][2] * v.x[2][0];
  1781. tmp.x[0][1] = x[0][0] * v.x[0][1] + x[0][1] * v.x[1][1] + x[0][2] * v.x[2][1];
  1782. tmp.x[0][2] = x[0][0] * v.x[0][2] + x[0][1] * v.x[1][2] + x[0][2] * v.x[2][2];
  1783. tmp.x[1][0] = x[1][0] * v.x[0][0] + x[1][1] * v.x[1][0] + x[1][2] * v.x[2][0];
  1784. tmp.x[1][1] = x[1][0] * v.x[0][1] + x[1][1] * v.x[1][1] + x[1][2] * v.x[2][1];
  1785. tmp.x[1][2] = x[1][0] * v.x[0][2] + x[1][1] * v.x[1][2] + x[1][2] * v.x[2][2];
  1786. tmp.x[2][0] = x[2][0] * v.x[0][0] + x[2][1] * v.x[1][0] + x[2][2] * v.x[2][0];
  1787. tmp.x[2][1] = x[2][0] * v.x[0][1] + x[2][1] * v.x[1][1] + x[2][2] * v.x[2][1];
  1788. tmp.x[2][2] = x[2][0] * v.x[0][2] + x[2][1] * v.x[1][2] + x[2][2] * v.x[2][2];
  1789. *this = tmp;
  1790. return *this;
  1791. }
  1792. template <class T>
  1793. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>
  1794. Matrix33<T>::operator* (const Matrix33<T>& v) const IMATH_NOEXCEPT
  1795. {
  1796. // Avoid initializing with 0 values before immediately overwriting them,
  1797. // and unroll all loops for the best autovectorization.
  1798. Matrix33 tmp(IMATH_INTERNAL_NAMESPACE::UNINITIALIZED);
  1799. tmp.x[0][0] = x[0][0] * v.x[0][0] + x[0][1] * v.x[1][0] + x[0][2] * v.x[2][0];
  1800. tmp.x[0][1] = x[0][0] * v.x[0][1] + x[0][1] * v.x[1][1] + x[0][2] * v.x[2][1];
  1801. tmp.x[0][2] = x[0][0] * v.x[0][2] + x[0][1] * v.x[1][2] + x[0][2] * v.x[2][2];
  1802. tmp.x[1][0] = x[1][0] * v.x[0][0] + x[1][1] * v.x[1][0] + x[1][2] * v.x[2][0];
  1803. tmp.x[1][1] = x[1][0] * v.x[0][1] + x[1][1] * v.x[1][1] + x[1][2] * v.x[2][1];
  1804. tmp.x[1][2] = x[1][0] * v.x[0][2] + x[1][1] * v.x[1][2] + x[1][2] * v.x[2][2];
  1805. tmp.x[2][0] = x[2][0] * v.x[0][0] + x[2][1] * v.x[1][0] + x[2][2] * v.x[2][0];
  1806. tmp.x[2][1] = x[2][0] * v.x[0][1] + x[2][1] * v.x[1][1] + x[2][2] * v.x[2][1];
  1807. tmp.x[2][2] = x[2][0] * v.x[0][2] + x[2][1] * v.x[1][2] + x[2][2] * v.x[2][2];
  1808. return tmp;
  1809. }
  1810. template <class T>
  1811. template <class S>
  1812. IMATH_HOSTDEVICE inline void
  1813. Matrix33<T>::multVecMatrix (const Vec2<S>& src, Vec2<S>& dst) const IMATH_NOEXCEPT
  1814. {
  1815. S a, b, w;
  1816. a = src.x * x[0][0] + src.y * x[1][0] + x[2][0];
  1817. b = src.x * x[0][1] + src.y * x[1][1] + x[2][1];
  1818. w = src.x * x[0][2] + src.y * x[1][2] + x[2][2];
  1819. dst.x = a / w;
  1820. dst.y = b / w;
  1821. }
  1822. template <class T>
  1823. template <class S>
  1824. IMATH_HOSTDEVICE inline void
  1825. Matrix33<T>::multDirMatrix (const Vec2<S>& src, Vec2<S>& dst) const IMATH_NOEXCEPT
  1826. {
  1827. S a, b;
  1828. a = src.x * x[0][0] + src.y * x[1][0];
  1829. b = src.x * x[0][1] + src.y * x[1][1];
  1830. dst.x = a;
  1831. dst.y = b;
  1832. }
  1833. template <class T>
  1834. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1835. Matrix33<T>::operator/= (T a) IMATH_NOEXCEPT
  1836. {
  1837. x[0][0] /= a;
  1838. x[0][1] /= a;
  1839. x[0][2] /= a;
  1840. x[1][0] /= a;
  1841. x[1][1] /= a;
  1842. x[1][2] /= a;
  1843. x[2][0] /= a;
  1844. x[2][1] /= a;
  1845. x[2][2] /= a;
  1846. return *this;
  1847. }
  1848. template <class T>
  1849. IMATH_HOSTDEVICE constexpr inline Matrix33<T>
  1850. Matrix33<T>::operator/ (T a) const IMATH_NOEXCEPT
  1851. {
  1852. return Matrix33 (x[0][0] / a,
  1853. x[0][1] / a,
  1854. x[0][2] / a,
  1855. x[1][0] / a,
  1856. x[1][1] / a,
  1857. x[1][2] / a,
  1858. x[2][0] / a,
  1859. x[2][1] / a,
  1860. x[2][2] / a);
  1861. }
  1862. template <class T>
  1863. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  1864. Matrix33<T>::transpose() IMATH_NOEXCEPT
  1865. {
  1866. Matrix33 tmp (x[0][0], x[1][0], x[2][0], x[0][1], x[1][1], x[2][1], x[0][2], x[1][2], x[2][2]);
  1867. *this = tmp;
  1868. return *this;
  1869. }
  1870. template <class T>
  1871. IMATH_HOSTDEVICE constexpr inline Matrix33<T>
  1872. Matrix33<T>::transposed() const IMATH_NOEXCEPT
  1873. {
  1874. return Matrix33 (x[0][0],
  1875. x[1][0],
  1876. x[2][0],
  1877. x[0][1],
  1878. x[1][1],
  1879. x[2][1],
  1880. x[0][2],
  1881. x[1][2],
  1882. x[2][2]);
  1883. }
  1884. template <class T>
  1885. const inline Matrix33<T>&
  1886. Matrix33<T>::gjInvert (bool singExc)
  1887. {
  1888. *this = gjInverse (singExc);
  1889. return *this;
  1890. }
  1891. template <class T>
  1892. IMATH_HOSTDEVICE const inline Matrix33<T>&
  1893. Matrix33<T>::gjInvert() IMATH_NOEXCEPT
  1894. {
  1895. *this = gjInverse();
  1896. return *this;
  1897. }
  1898. template <class T>
  1899. inline Matrix33<T>
  1900. Matrix33<T>::gjInverse (bool singExc) const
  1901. {
  1902. int i, j, k;
  1903. Matrix33 s;
  1904. Matrix33 t (*this);
  1905. // Forward elimination
  1906. for (i = 0; i < 2; i++)
  1907. {
  1908. int pivot = i;
  1909. T pivotsize = t.x[i][i];
  1910. if (pivotsize < 0)
  1911. pivotsize = -pivotsize;
  1912. for (j = i + 1; j < 3; j++)
  1913. {
  1914. T tmp = t.x[j][i];
  1915. if (tmp < 0)
  1916. tmp = -tmp;
  1917. if (tmp > pivotsize)
  1918. {
  1919. pivot = j;
  1920. pivotsize = tmp;
  1921. }
  1922. }
  1923. if (pivotsize == 0)
  1924. {
  1925. if (singExc)
  1926. throw std::invalid_argument ("Cannot invert singular matrix.");
  1927. return Matrix33();
  1928. }
  1929. if (pivot != i)
  1930. {
  1931. for (j = 0; j < 3; j++)
  1932. {
  1933. T tmp;
  1934. tmp = t.x[i][j];
  1935. t.x[i][j] = t.x[pivot][j];
  1936. t.x[pivot][j] = tmp;
  1937. tmp = s.x[i][j];
  1938. s.x[i][j] = s.x[pivot][j];
  1939. s.x[pivot][j] = tmp;
  1940. }
  1941. }
  1942. for (j = i + 1; j < 3; j++)
  1943. {
  1944. T f = t.x[j][i] / t.x[i][i];
  1945. for (k = 0; k < 3; k++)
  1946. {
  1947. t.x[j][k] -= f * t.x[i][k];
  1948. s.x[j][k] -= f * s.x[i][k];
  1949. }
  1950. }
  1951. }
  1952. // Backward substitution
  1953. for (i = 2; i >= 0; --i)
  1954. {
  1955. T f;
  1956. if ((f = t[i][i]) == 0)
  1957. {
  1958. if (singExc)
  1959. throw std::invalid_argument ("Cannot invert singular matrix.");
  1960. return Matrix33();
  1961. }
  1962. for (j = 0; j < 3; j++)
  1963. {
  1964. t.x[i][j] /= f;
  1965. s.x[i][j] /= f;
  1966. }
  1967. for (j = 0; j < i; j++)
  1968. {
  1969. f = t.x[j][i];
  1970. for (k = 0; k < 3; k++)
  1971. {
  1972. t.x[j][k] -= f * t.x[i][k];
  1973. s.x[j][k] -= f * s.x[i][k];
  1974. }
  1975. }
  1976. }
  1977. return s;
  1978. }
  1979. template <class T>
  1980. IMATH_HOSTDEVICE inline Matrix33<T>
  1981. Matrix33<T>::gjInverse() const IMATH_NOEXCEPT
  1982. {
  1983. int i, j, k;
  1984. Matrix33 s;
  1985. Matrix33 t (*this);
  1986. // Forward elimination
  1987. for (i = 0; i < 2; i++)
  1988. {
  1989. int pivot = i;
  1990. T pivotsize = t.x[i][i];
  1991. if (pivotsize < 0)
  1992. pivotsize = -pivotsize;
  1993. for (j = i + 1; j < 3; j++)
  1994. {
  1995. T tmp = t.x[j][i];
  1996. if (tmp < 0)
  1997. tmp = -tmp;
  1998. if (tmp > pivotsize)
  1999. {
  2000. pivot = j;
  2001. pivotsize = tmp;
  2002. }
  2003. }
  2004. if (pivotsize == 0)
  2005. {
  2006. return Matrix33();
  2007. }
  2008. if (pivot != i)
  2009. {
  2010. for (j = 0; j < 3; j++)
  2011. {
  2012. T tmp;
  2013. tmp = t.x[i][j];
  2014. t.x[i][j] = t.x[pivot][j];
  2015. t.x[pivot][j] = tmp;
  2016. tmp = s.x[i][j];
  2017. s.x[i][j] = s.x[pivot][j];
  2018. s.x[pivot][j] = tmp;
  2019. }
  2020. }
  2021. for (j = i + 1; j < 3; j++)
  2022. {
  2023. T f = t.x[j][i] / t.x[i][i];
  2024. for (k = 0; k < 3; k++)
  2025. {
  2026. t.x[j][k] -= f * t.x[i][k];
  2027. s.x[j][k] -= f * s.x[i][k];
  2028. }
  2029. }
  2030. }
  2031. // Backward substitution
  2032. for (i = 2; i >= 0; --i)
  2033. {
  2034. T f;
  2035. if ((f = t.x[i][i]) == 0)
  2036. {
  2037. return Matrix33();
  2038. }
  2039. for (j = 0; j < 3; j++)
  2040. {
  2041. t.x[i][j] /= f;
  2042. s.x[i][j] /= f;
  2043. }
  2044. for (j = 0; j < i; j++)
  2045. {
  2046. f = t.x[j][i];
  2047. for (k = 0; k < 3; k++)
  2048. {
  2049. t.x[j][k] -= f * t.x[i][k];
  2050. s.x[j][k] -= f * s.x[i][k];
  2051. }
  2052. }
  2053. }
  2054. return s;
  2055. }
  2056. template <class T>
  2057. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2058. Matrix33<T>::invert (bool singExc)
  2059. {
  2060. *this = inverse (singExc);
  2061. return *this;
  2062. }
  2063. template <class T>
  2064. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2065. Matrix33<T>::invert() IMATH_NOEXCEPT
  2066. {
  2067. *this = inverse();
  2068. return *this;
  2069. }
  2070. template <class T>
  2071. IMATH_CONSTEXPR14 inline Matrix33<T>
  2072. Matrix33<T>::inverse (bool singExc) const
  2073. {
  2074. if (x[0][2] != 0 || x[1][2] != 0 || x[2][2] != 1)
  2075. {
  2076. Matrix33 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
  2077. x[2][1] * x[0][2] - x[0][1] * x[2][2],
  2078. x[0][1] * x[1][2] - x[1][1] * x[0][2],
  2079. x[2][0] * x[1][2] - x[1][0] * x[2][2],
  2080. x[0][0] * x[2][2] - x[2][0] * x[0][2],
  2081. x[1][0] * x[0][2] - x[0][0] * x[1][2],
  2082. x[1][0] * x[2][1] - x[2][0] * x[1][1],
  2083. x[2][0] * x[0][1] - x[0][0] * x[2][1],
  2084. x[0][0] * x[1][1] - x[1][0] * x[0][1]);
  2085. T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
  2086. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  2087. {
  2088. for (int i = 0; i < 3; ++i)
  2089. {
  2090. for (int j = 0; j < 3; ++j)
  2091. {
  2092. s.x[i][j] /= r;
  2093. }
  2094. }
  2095. }
  2096. else
  2097. {
  2098. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  2099. for (int i = 0; i < 3; ++i)
  2100. {
  2101. for (int j = 0; j < 3; ++j)
  2102. {
  2103. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s.x[i][j]))
  2104. {
  2105. s.x[i][j] /= r;
  2106. }
  2107. else
  2108. {
  2109. if (singExc)
  2110. throw std::invalid_argument ("Cannot invert "
  2111. "singular matrix.");
  2112. return Matrix33();
  2113. }
  2114. }
  2115. }
  2116. }
  2117. return s;
  2118. }
  2119. else
  2120. {
  2121. Matrix33 s (x[1][1],
  2122. -x[0][1],
  2123. 0,
  2124. -x[1][0],
  2125. x[0][0],
  2126. 0,
  2127. 0,
  2128. 0,
  2129. 1);
  2130. T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
  2131. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  2132. {
  2133. for (int i = 0; i < 2; ++i)
  2134. {
  2135. for (int j = 0; j < 2; ++j)
  2136. {
  2137. s.x[i][j] /= r;
  2138. }
  2139. }
  2140. }
  2141. else
  2142. {
  2143. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  2144. for (int i = 0; i < 2; ++i)
  2145. {
  2146. for (int j = 0; j < 2; ++j)
  2147. {
  2148. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s.x[i][j]))
  2149. {
  2150. s.x[i][j] /= r;
  2151. }
  2152. else
  2153. {
  2154. if (singExc)
  2155. throw std::invalid_argument ("Cannot invert "
  2156. "singular matrix.");
  2157. return Matrix33();
  2158. }
  2159. }
  2160. }
  2161. }
  2162. s.x[2][0] = -x[2][0] * s.x[0][0] - x[2][1] * s.x[1][0];
  2163. s.x[2][1] = -x[2][0] * s.x[0][1] - x[2][1] * s.x[1][1];
  2164. return s;
  2165. }
  2166. }
  2167. template <class T>
  2168. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix33<T>
  2169. Matrix33<T>::inverse () const IMATH_NOEXCEPT
  2170. {
  2171. if (x[0][2] != 0 || x[1][2] != 0 || x[2][2] != 1)
  2172. {
  2173. Matrix33 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
  2174. x[2][1] * x[0][2] - x[0][1] * x[2][2],
  2175. x[0][1] * x[1][2] - x[1][1] * x[0][2],
  2176. x[2][0] * x[1][2] - x[1][0] * x[2][2],
  2177. x[0][0] * x[2][2] - x[2][0] * x[0][2],
  2178. x[1][0] * x[0][2] - x[0][0] * x[1][2],
  2179. x[1][0] * x[2][1] - x[2][0] * x[1][1],
  2180. x[2][0] * x[0][1] - x[0][0] * x[2][1],
  2181. x[0][0] * x[1][1] - x[1][0] * x[0][1]);
  2182. T r = x[0][0] * s.x[0][0] + x[0][1] * s.x[1][0] + x[0][2] * s.x[2][0];
  2183. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  2184. {
  2185. for (int i = 0; i < 3; ++i)
  2186. {
  2187. for (int j = 0; j < 3; ++j)
  2188. {
  2189. s.x[i][j] /= r;
  2190. }
  2191. }
  2192. }
  2193. else
  2194. {
  2195. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  2196. for (int i = 0; i < 3; ++i)
  2197. {
  2198. for (int j = 0; j < 3; ++j)
  2199. {
  2200. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s.x[i][j]))
  2201. {
  2202. s.x[i][j] /= r;
  2203. }
  2204. else
  2205. {
  2206. return Matrix33();
  2207. }
  2208. }
  2209. }
  2210. }
  2211. return s;
  2212. }
  2213. else
  2214. {
  2215. Matrix33 s (x[1][1],
  2216. -x[0][1],
  2217. 0,
  2218. -x[1][0],
  2219. x[0][0],
  2220. 0,
  2221. 0,
  2222. 0,
  2223. 1);
  2224. T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
  2225. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  2226. {
  2227. for (int i = 0; i < 2; ++i)
  2228. {
  2229. for (int j = 0; j < 2; ++j)
  2230. {
  2231. s.x[i][j] /= r;
  2232. }
  2233. }
  2234. }
  2235. else
  2236. {
  2237. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  2238. for (int i = 0; i < 2; ++i)
  2239. {
  2240. for (int j = 0; j < 2; ++j)
  2241. {
  2242. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s.x[i][j]))
  2243. {
  2244. s.x[i][j] /= r;
  2245. }
  2246. else
  2247. {
  2248. return Matrix33();
  2249. }
  2250. }
  2251. }
  2252. }
  2253. s.x[2][0] = -x[2][0] * s.x[0][0] - x[2][1] * s.x[1][0];
  2254. s.x[2][1] = -x[2][0] * s.x[0][1] - x[2][1] * s.x[1][1];
  2255. return s;
  2256. }
  2257. }
  2258. template <class T>
  2259. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
  2260. Matrix33<T>::minorOf (const int r, const int c) const IMATH_NOEXCEPT
  2261. {
  2262. int r0 = 0 + (r < 1 ? 1 : 0);
  2263. int r1 = 1 + (r < 2 ? 1 : 0);
  2264. int c0 = 0 + (c < 1 ? 1 : 0);
  2265. int c1 = 1 + (c < 2 ? 1 : 0);
  2266. return x[r0][c0] * x[r1][c1] - x[r1][c0] * x[r0][c1];
  2267. }
  2268. template <class T>
  2269. IMATH_HOSTDEVICE constexpr inline T
  2270. Matrix33<T>::fastMinor (const int r0, const int r1, const int c0, const int c1) const IMATH_NOEXCEPT
  2271. {
  2272. return x[r0][c0] * x[r1][c1] - x[r0][c1] * x[r1][c0];
  2273. }
  2274. template <class T>
  2275. IMATH_HOSTDEVICE constexpr inline T
  2276. Matrix33<T>::determinant() const IMATH_NOEXCEPT
  2277. {
  2278. return x[0][0] * (x[1][1] * x[2][2] - x[1][2] * x[2][1]) +
  2279. x[0][1] * (x[1][2] * x[2][0] - x[1][0] * x[2][2]) +
  2280. x[0][2] * (x[1][0] * x[2][1] - x[1][1] * x[2][0]);
  2281. }
  2282. template <class T>
  2283. IMATH_HOSTDEVICE constexpr inline T
  2284. Matrix33<T>::trace () const IMATH_NOEXCEPT
  2285. {
  2286. return x[0][0] + x[1][1] + x[2][2];
  2287. }
  2288. template <class T>
  2289. template <class S>
  2290. IMATH_HOSTDEVICE inline const Matrix33<T>&
  2291. Matrix33<T>::setRotation (S r) IMATH_NOEXCEPT
  2292. {
  2293. S cos_r, sin_r;
  2294. cos_r = cos ((T) r);
  2295. sin_r = sin ((T) r);
  2296. x[0][0] = cos_r;
  2297. x[0][1] = sin_r;
  2298. x[0][2] = 0;
  2299. x[1][0] = -sin_r;
  2300. x[1][1] = cos_r;
  2301. x[1][2] = 0;
  2302. x[2][0] = 0;
  2303. x[2][1] = 0;
  2304. x[2][2] = 1;
  2305. return *this;
  2306. }
  2307. template <class T>
  2308. template <class S>
  2309. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2310. Matrix33<T>::rotate (S r) IMATH_NOEXCEPT
  2311. {
  2312. *this *= Matrix33<T>().setRotation (r);
  2313. return *this;
  2314. }
  2315. template <class T>
  2316. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2317. Matrix33<T>::setScale (T s) IMATH_NOEXCEPT
  2318. {
  2319. //
  2320. // Set the matrix to a 2D homogeneous transform scale:
  2321. // | s 0 0 |
  2322. // | 0 s 0 |
  2323. // | 0 0 1 |
  2324. //
  2325. x[0][0] = s;
  2326. x[0][1] = 0;
  2327. x[0][2] = 0;
  2328. x[1][0] = 0;
  2329. x[1][1] = s;
  2330. x[1][2] = 0;
  2331. x[2][0] = 0;
  2332. x[2][1] = 0;
  2333. x[2][2] = 1;
  2334. return *this;
  2335. }
  2336. template <class T>
  2337. template <class S>
  2338. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2339. Matrix33<T>::setScale (const Vec2<S>& s) IMATH_NOEXCEPT
  2340. {
  2341. //
  2342. // Set the matrix to a 2D homogeneous transform scale:
  2343. // | s.x 0 0 |
  2344. // | 0 s.y 0 |
  2345. // | 0 0 1 |
  2346. //
  2347. x[0][0] = s.x;
  2348. x[0][1] = 0;
  2349. x[0][2] = 0;
  2350. x[1][0] = 0;
  2351. x[1][1] = s.y;
  2352. x[1][2] = 0;
  2353. x[2][0] = 0;
  2354. x[2][1] = 0;
  2355. x[2][2] = 1;
  2356. return *this;
  2357. }
  2358. template <class T>
  2359. template <class S>
  2360. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2361. Matrix33<T>::scale (const Vec2<S>& s) IMATH_NOEXCEPT
  2362. {
  2363. x[0][0] *= s.x;
  2364. x[0][1] *= s.x;
  2365. x[0][2] *= s.x;
  2366. x[1][0] *= s.y;
  2367. x[1][1] *= s.y;
  2368. x[1][2] *= s.y;
  2369. return *this;
  2370. }
  2371. template <class T>
  2372. template <class S>
  2373. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2374. Matrix33<T>::setTranslation (const Vec2<S>& t) IMATH_NOEXCEPT
  2375. {
  2376. x[0][0] = 1;
  2377. x[0][1] = 0;
  2378. x[0][2] = 0;
  2379. x[1][0] = 0;
  2380. x[1][1] = 1;
  2381. x[1][2] = 0;
  2382. x[2][0] = t.x;
  2383. x[2][1] = t.y;
  2384. x[2][2] = 1;
  2385. return *this;
  2386. }
  2387. template <class T>
  2388. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  2389. Matrix33<T>::translation() const IMATH_NOEXCEPT
  2390. {
  2391. return Vec2<T> (x[2][0], x[2][1]);
  2392. }
  2393. template <class T>
  2394. template <class S>
  2395. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2396. Matrix33<T>::translate (const Vec2<S>& t) IMATH_NOEXCEPT
  2397. {
  2398. x[2][0] += t.x * x[0][0] + t.y * x[1][0];
  2399. x[2][1] += t.x * x[0][1] + t.y * x[1][1];
  2400. x[2][2] += t.x * x[0][2] + t.y * x[1][2];
  2401. return *this;
  2402. }
  2403. template <class T>
  2404. template <class S>
  2405. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2406. Matrix33<T>::setShear (const S& xy) IMATH_NOEXCEPT
  2407. {
  2408. x[0][0] = 1;
  2409. x[0][1] = 0;
  2410. x[0][2] = 0;
  2411. x[1][0] = xy;
  2412. x[1][1] = 1;
  2413. x[1][2] = 0;
  2414. x[2][0] = 0;
  2415. x[2][1] = 0;
  2416. x[2][2] = 1;
  2417. return *this;
  2418. }
  2419. template <class T>
  2420. template <class S>
  2421. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2422. Matrix33<T>::setShear (const Vec2<S>& h) IMATH_NOEXCEPT
  2423. {
  2424. x[0][0] = 1;
  2425. x[0][1] = h.y;
  2426. x[0][2] = 0;
  2427. x[1][0] = h.x;
  2428. x[1][1] = 1;
  2429. x[1][2] = 0;
  2430. x[2][0] = 0;
  2431. x[2][1] = 0;
  2432. x[2][2] = 1;
  2433. return *this;
  2434. }
  2435. template <class T>
  2436. template <class S>
  2437. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2438. Matrix33<T>::shear (const S& xy) IMATH_NOEXCEPT
  2439. {
  2440. //
  2441. // In this case, we don't need a temp. copy of the matrix
  2442. // because we never use a value on the RHS after we've
  2443. // changed it on the LHS.
  2444. //
  2445. x[1][0] += xy * x[0][0];
  2446. x[1][1] += xy * x[0][1];
  2447. x[1][2] += xy * x[0][2];
  2448. return *this;
  2449. }
  2450. template <class T>
  2451. template <class S>
  2452. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix33<T>&
  2453. Matrix33<T>::shear (const Vec2<S>& h) IMATH_NOEXCEPT
  2454. {
  2455. Matrix33<T> P (*this);
  2456. x[0][0] = P.x[0][0] + h.y * P.x[1][0];
  2457. x[0][1] = P.x[0][1] + h.y * P.x[1][1];
  2458. x[0][2] = P.x[0][2] + h.y * P.x[1][2];
  2459. x[1][0] = P.x[1][0] + h.x * P.x[0][0];
  2460. x[1][1] = P.x[1][1] + h.x * P.x[0][1];
  2461. x[1][2] = P.x[1][2] + h.x * P.x[0][2];
  2462. return *this;
  2463. }
  2464. //---------------------------
  2465. // Implementation of Matrix44
  2466. //---------------------------
  2467. template <class T>
  2468. IMATH_HOSTDEVICE inline T*
  2469. Matrix44<T>::operator[] (int i) IMATH_NOEXCEPT
  2470. {
  2471. return x[i];
  2472. }
  2473. template <class T>
  2474. IMATH_HOSTDEVICE inline const T*
  2475. Matrix44<T>::operator[] (int i) const IMATH_NOEXCEPT
  2476. {
  2477. return x[i];
  2478. }
  2479. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>::Matrix44() IMATH_NOEXCEPT
  2480. {
  2481. x[0][0] = 1;
  2482. x[0][1] = 0;
  2483. x[0][2] = 0;
  2484. x[0][3] = 0;
  2485. x[1][0] = 0;
  2486. x[1][1] = 1;
  2487. x[1][2] = 0;
  2488. x[1][3] = 0;
  2489. x[2][0] = 0;
  2490. x[2][1] = 0;
  2491. x[2][2] = 1;
  2492. x[2][3] = 0;
  2493. x[3][0] = 0;
  2494. x[3][1] = 0;
  2495. x[3][2] = 0;
  2496. x[3][3] = 1;
  2497. }
  2498. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>::Matrix44 (T a) IMATH_NOEXCEPT
  2499. {
  2500. x[0][0] = a;
  2501. x[0][1] = a;
  2502. x[0][2] = a;
  2503. x[0][3] = a;
  2504. x[1][0] = a;
  2505. x[1][1] = a;
  2506. x[1][2] = a;
  2507. x[1][3] = a;
  2508. x[2][0] = a;
  2509. x[2][1] = a;
  2510. x[2][2] = a;
  2511. x[2][3] = a;
  2512. x[3][0] = a;
  2513. x[3][1] = a;
  2514. x[3][2] = a;
  2515. x[3][3] = a;
  2516. }
  2517. template <class T>
  2518. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>::Matrix44 (
  2519. const T a[4][4]) IMATH_NOEXCEPT
  2520. {
  2521. x[0][0] = a[0][0];
  2522. x[0][1] = a[0][1];
  2523. x[0][2] = a[0][2];
  2524. x[0][3] = a[0][3];
  2525. x[1][0] = a[1][0];
  2526. x[1][1] = a[1][1];
  2527. x[1][2] = a[1][2];
  2528. x[1][3] = a[1][3];
  2529. x[2][0] = a[2][0];
  2530. x[2][1] = a[2][1];
  2531. x[2][2] = a[2][2];
  2532. x[2][3] = a[2][3];
  2533. x[3][0] = a[3][0];
  2534. x[3][1] = a[3][1];
  2535. x[3][2] = a[3][2];
  2536. x[3][3] = a[3][3];
  2537. }
  2538. template <class T>
  2539. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<
  2540. T>::Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h, T i, T j, T k, T l, T m, T n, T o, T p) IMATH_NOEXCEPT
  2541. {
  2542. x[0][0] = a;
  2543. x[0][1] = b;
  2544. x[0][2] = c;
  2545. x[0][3] = d;
  2546. x[1][0] = e;
  2547. x[1][1] = f;
  2548. x[1][2] = g;
  2549. x[1][3] = h;
  2550. x[2][0] = i;
  2551. x[2][1] = j;
  2552. x[2][2] = k;
  2553. x[2][3] = l;
  2554. x[3][0] = m;
  2555. x[3][1] = n;
  2556. x[3][2] = o;
  2557. x[3][3] = p;
  2558. }
  2559. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>::Matrix44 (Matrix33<T> r, Vec3<T> t) IMATH_NOEXCEPT
  2560. {
  2561. x[0][0] = r.x[0][0];
  2562. x[0][1] = r.x[0][1];
  2563. x[0][2] = r.x[0][2];
  2564. x[0][3] = 0;
  2565. x[1][0] = r.x[1][0];
  2566. x[1][1] = r.x[1][1];
  2567. x[1][2] = r.x[1][2];
  2568. x[1][3] = 0;
  2569. x[2][0] = r.x[2][0];
  2570. x[2][1] = r.x[2][1];
  2571. x[2][2] = r.x[2][2];
  2572. x[2][3] = 0;
  2573. x[3][0] = t.x;
  2574. x[3][1] = t.y;
  2575. x[3][2] = t.z;
  2576. x[3][3] = 1;
  2577. }
  2578. template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>::Matrix44 (const Matrix44& v) IMATH_NOEXCEPT
  2579. {
  2580. x[0][0] = v.x[0][0];
  2581. x[0][1] = v.x[0][1];
  2582. x[0][2] = v.x[0][2];
  2583. x[0][3] = v.x[0][3];
  2584. x[1][0] = v.x[1][0];
  2585. x[1][1] = v.x[1][1];
  2586. x[1][2] = v.x[1][2];
  2587. x[1][3] = v.x[1][3];
  2588. x[2][0] = v.x[2][0];
  2589. x[2][1] = v.x[2][1];
  2590. x[2][2] = v.x[2][2];
  2591. x[2][3] = v.x[2][3];
  2592. x[3][0] = v.x[3][0];
  2593. x[3][1] = v.x[3][1];
  2594. x[3][2] = v.x[3][2];
  2595. x[3][3] = v.x[3][3];
  2596. }
  2597. template <class T>
  2598. template <class S>
  2599. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>::Matrix44 (const Matrix44<S>& v) IMATH_NOEXCEPT
  2600. {
  2601. x[0][0] = T (v.x[0][0]);
  2602. x[0][1] = T (v.x[0][1]);
  2603. x[0][2] = T (v.x[0][2]);
  2604. x[0][3] = T (v.x[0][3]);
  2605. x[1][0] = T (v.x[1][0]);
  2606. x[1][1] = T (v.x[1][1]);
  2607. x[1][2] = T (v.x[1][2]);
  2608. x[1][3] = T (v.x[1][3]);
  2609. x[2][0] = T (v.x[2][0]);
  2610. x[2][1] = T (v.x[2][1]);
  2611. x[2][2] = T (v.x[2][2]);
  2612. x[2][3] = T (v.x[2][3]);
  2613. x[3][0] = T (v.x[3][0]);
  2614. x[3][1] = T (v.x[3][1]);
  2615. x[3][2] = T (v.x[3][2]);
  2616. x[3][3] = T (v.x[3][3]);
  2617. }
  2618. template <class T>
  2619. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2620. Matrix44<T>::operator= (const Matrix44& v) IMATH_NOEXCEPT
  2621. {
  2622. x[0][0] = v.x[0][0];
  2623. x[0][1] = v.x[0][1];
  2624. x[0][2] = v.x[0][2];
  2625. x[0][3] = v.x[0][3];
  2626. x[1][0] = v.x[1][0];
  2627. x[1][1] = v.x[1][1];
  2628. x[1][2] = v.x[1][2];
  2629. x[1][3] = v.x[1][3];
  2630. x[2][0] = v.x[2][0];
  2631. x[2][1] = v.x[2][1];
  2632. x[2][2] = v.x[2][2];
  2633. x[2][3] = v.x[2][3];
  2634. x[3][0] = v.x[3][0];
  2635. x[3][1] = v.x[3][1];
  2636. x[3][2] = v.x[3][2];
  2637. x[3][3] = v.x[3][3];
  2638. return *this;
  2639. }
  2640. template <class T>
  2641. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2642. Matrix44<T>::operator= (T a) IMATH_NOEXCEPT
  2643. {
  2644. x[0][0] = a;
  2645. x[0][1] = a;
  2646. x[0][2] = a;
  2647. x[0][3] = a;
  2648. x[1][0] = a;
  2649. x[1][1] = a;
  2650. x[1][2] = a;
  2651. x[1][3] = a;
  2652. x[2][0] = a;
  2653. x[2][1] = a;
  2654. x[2][2] = a;
  2655. x[2][3] = a;
  2656. x[3][0] = a;
  2657. x[3][1] = a;
  2658. x[3][2] = a;
  2659. x[3][3] = a;
  2660. return *this;
  2661. }
  2662. template <class T>
  2663. IMATH_HOSTDEVICE inline T*
  2664. Matrix44<T>::getValue () IMATH_NOEXCEPT
  2665. {
  2666. return (T*) &x[0][0];
  2667. }
  2668. template <class T>
  2669. IMATH_HOSTDEVICE inline const T*
  2670. Matrix44<T>::getValue() const IMATH_NOEXCEPT
  2671. {
  2672. return (const T*) &x[0][0];
  2673. }
  2674. template <class T>
  2675. template <class S>
  2676. IMATH_HOSTDEVICE inline void
  2677. Matrix44<T>::getValue (Matrix44<S>& v) const IMATH_NOEXCEPT
  2678. {
  2679. v.x[0][0] = x[0][0];
  2680. v.x[0][1] = x[0][1];
  2681. v.x[0][2] = x[0][2];
  2682. v.x[0][3] = x[0][3];
  2683. v.x[1][0] = x[1][0];
  2684. v.x[1][1] = x[1][1];
  2685. v.x[1][2] = x[1][2];
  2686. v.x[1][3] = x[1][3];
  2687. v.x[2][0] = x[2][0];
  2688. v.x[2][1] = x[2][1];
  2689. v.x[2][2] = x[2][2];
  2690. v.x[2][3] = x[2][3];
  2691. v.x[3][0] = x[3][0];
  2692. v.x[3][1] = x[3][1];
  2693. v.x[3][2] = x[3][2];
  2694. v.x[3][3] = x[3][3];
  2695. }
  2696. template <class T>
  2697. template <class S>
  2698. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>&
  2699. Matrix44<T>::setValue (const Matrix44<S>& v) IMATH_NOEXCEPT
  2700. {
  2701. x[0][0] = T(v.x[0][0]);
  2702. x[0][1] = T(v.x[0][1]);
  2703. x[0][2] = T(v.x[0][2]);
  2704. x[0][3] = T(v.x[0][3]);
  2705. x[1][0] = T(v.x[1][0]);
  2706. x[1][1] = T(v.x[1][1]);
  2707. x[1][2] = T(v.x[1][2]);
  2708. x[1][3] = T(v.x[1][3]);
  2709. x[2][0] = T(v.x[2][0]);
  2710. x[2][1] = T(v.x[2][1]);
  2711. x[2][2] = T(v.x[2][2]);
  2712. x[2][3] = T(v.x[2][3]);
  2713. x[3][0] = T(v.x[3][0]);
  2714. x[3][1] = T(v.x[3][1]);
  2715. x[3][2] = T(v.x[3][2]);
  2716. x[3][3] = T(v.x[3][3]);
  2717. return *this;
  2718. }
  2719. template <class T>
  2720. template <class S>
  2721. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>&
  2722. Matrix44<T>::setTheMatrix (const Matrix44<S>& v) IMATH_NOEXCEPT
  2723. {
  2724. x[0][0] = v.x[0][0];
  2725. x[0][1] = v.x[0][1];
  2726. x[0][2] = v.x[0][2];
  2727. x[0][3] = v.x[0][3];
  2728. x[1][0] = v.x[1][0];
  2729. x[1][1] = v.x[1][1];
  2730. x[1][2] = v.x[1][2];
  2731. x[1][3] = v.x[1][3];
  2732. x[2][0] = v.x[2][0];
  2733. x[2][1] = v.x[2][1];
  2734. x[2][2] = v.x[2][2];
  2735. x[2][3] = v.x[2][3];
  2736. x[3][0] = v.x[3][0];
  2737. x[3][1] = v.x[3][1];
  2738. x[3][2] = v.x[3][2];
  2739. x[3][3] = v.x[3][3];
  2740. return *this;
  2741. }
  2742. template <class T>
  2743. IMATH_HOSTDEVICE inline void
  2744. Matrix44<T>::makeIdentity() IMATH_NOEXCEPT
  2745. {
  2746. x[0][0] = 1;
  2747. x[0][1] = 0;
  2748. x[0][2] = 0;
  2749. x[0][3] = 0;
  2750. x[1][0] = 0;
  2751. x[1][1] = 1;
  2752. x[1][2] = 0;
  2753. x[1][3] = 0;
  2754. x[2][0] = 0;
  2755. x[2][1] = 0;
  2756. x[2][2] = 1;
  2757. x[2][3] = 0;
  2758. x[3][0] = 0;
  2759. x[3][1] = 0;
  2760. x[3][2] = 0;
  2761. x[3][3] = 1;
  2762. }
  2763. template <class T>
  2764. IMATH_HOSTDEVICE constexpr inline bool
  2765. Matrix44<T>::operator== (const Matrix44& v) const IMATH_NOEXCEPT
  2766. {
  2767. return x[0][0] == v.x[0][0] && x[0][1] == v.x[0][1] && x[0][2] == v.x[0][2] &&
  2768. x[0][3] == v.x[0][3] && x[1][0] == v.x[1][0] && x[1][1] == v.x[1][1] &&
  2769. x[1][2] == v.x[1][2] && x[1][3] == v.x[1][3] && x[2][0] == v.x[2][0] &&
  2770. x[2][1] == v.x[2][1] && x[2][2] == v.x[2][2] && x[2][3] == v.x[2][3] &&
  2771. x[3][0] == v.x[3][0] && x[3][1] == v.x[3][1] && x[3][2] == v.x[3][2] &&
  2772. x[3][3] == v.x[3][3];
  2773. }
  2774. template <class T>
  2775. IMATH_HOSTDEVICE constexpr inline bool
  2776. Matrix44<T>::operator!= (const Matrix44& v) const IMATH_NOEXCEPT
  2777. {
  2778. return x[0][0] != v.x[0][0] || x[0][1] != v.x[0][1] || x[0][2] != v.x[0][2] ||
  2779. x[0][3] != v.x[0][3] || x[1][0] != v.x[1][0] || x[1][1] != v.x[1][1] ||
  2780. x[1][2] != v.x[1][2] || x[1][3] != v.x[1][3] || x[2][0] != v.x[2][0] ||
  2781. x[2][1] != v.x[2][1] || x[2][2] != v.x[2][2] || x[2][3] != v.x[2][3] ||
  2782. x[3][0] != v.x[3][0] || x[3][1] != v.x[3][1] || x[3][2] != v.x[3][2] ||
  2783. x[3][3] != v.x[3][3];
  2784. }
  2785. template <class T>
  2786. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  2787. Matrix44<T>::equalWithAbsError (const Matrix44<T>& m, T e) const IMATH_NOEXCEPT
  2788. {
  2789. for (int i = 0; i < 4; i++)
  2790. for (int j = 0; j < 4; j++)
  2791. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this).x[i][j], m.x[i][j], e))
  2792. return false;
  2793. return true;
  2794. }
  2795. template <class T>
  2796. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  2797. Matrix44<T>::equalWithRelError (const Matrix44<T>& m, T e) const IMATH_NOEXCEPT
  2798. {
  2799. for (int i = 0; i < 4; i++)
  2800. for (int j = 0; j < 4; j++)
  2801. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this).x[i][j], m.x[i][j], e))
  2802. return false;
  2803. return true;
  2804. }
  2805. template <class T>
  2806. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2807. Matrix44<T>::operator+= (const Matrix44<T>& v) IMATH_NOEXCEPT
  2808. {
  2809. x[0][0] += v.x[0][0];
  2810. x[0][1] += v.x[0][1];
  2811. x[0][2] += v.x[0][2];
  2812. x[0][3] += v.x[0][3];
  2813. x[1][0] += v.x[1][0];
  2814. x[1][1] += v.x[1][1];
  2815. x[1][2] += v.x[1][2];
  2816. x[1][3] += v.x[1][3];
  2817. x[2][0] += v.x[2][0];
  2818. x[2][1] += v.x[2][1];
  2819. x[2][2] += v.x[2][2];
  2820. x[2][3] += v.x[2][3];
  2821. x[3][0] += v.x[3][0];
  2822. x[3][1] += v.x[3][1];
  2823. x[3][2] += v.x[3][2];
  2824. x[3][3] += v.x[3][3];
  2825. return *this;
  2826. }
  2827. template <class T>
  2828. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2829. Matrix44<T>::operator+= (T a) IMATH_NOEXCEPT
  2830. {
  2831. x[0][0] += a;
  2832. x[0][1] += a;
  2833. x[0][2] += a;
  2834. x[0][3] += a;
  2835. x[1][0] += a;
  2836. x[1][1] += a;
  2837. x[1][2] += a;
  2838. x[1][3] += a;
  2839. x[2][0] += a;
  2840. x[2][1] += a;
  2841. x[2][2] += a;
  2842. x[2][3] += a;
  2843. x[3][0] += a;
  2844. x[3][1] += a;
  2845. x[3][2] += a;
  2846. x[3][3] += a;
  2847. return *this;
  2848. }
  2849. template <class T>
  2850. IMATH_HOSTDEVICE constexpr inline Matrix44<T>
  2851. Matrix44<T>::operator+ (const Matrix44<T>& v) const IMATH_NOEXCEPT
  2852. {
  2853. return Matrix44 (x[0][0] + v.x[0][0],
  2854. x[0][1] + v.x[0][1],
  2855. x[0][2] + v.x[0][2],
  2856. x[0][3] + v.x[0][3],
  2857. x[1][0] + v.x[1][0],
  2858. x[1][1] + v.x[1][1],
  2859. x[1][2] + v.x[1][2],
  2860. x[1][3] + v.x[1][3],
  2861. x[2][0] + v.x[2][0],
  2862. x[2][1] + v.x[2][1],
  2863. x[2][2] + v.x[2][2],
  2864. x[2][3] + v.x[2][3],
  2865. x[3][0] + v.x[3][0],
  2866. x[3][1] + v.x[3][1],
  2867. x[3][2] + v.x[3][2],
  2868. x[3][3] + v.x[3][3]);
  2869. }
  2870. template <class T>
  2871. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2872. Matrix44<T>::operator-= (const Matrix44<T>& v) IMATH_NOEXCEPT
  2873. {
  2874. x[0][0] -= v.x[0][0];
  2875. x[0][1] -= v.x[0][1];
  2876. x[0][2] -= v.x[0][2];
  2877. x[0][3] -= v.x[0][3];
  2878. x[1][0] -= v.x[1][0];
  2879. x[1][1] -= v.x[1][1];
  2880. x[1][2] -= v.x[1][2];
  2881. x[1][3] -= v.x[1][3];
  2882. x[2][0] -= v.x[2][0];
  2883. x[2][1] -= v.x[2][1];
  2884. x[2][2] -= v.x[2][2];
  2885. x[2][3] -= v.x[2][3];
  2886. x[3][0] -= v.x[3][0];
  2887. x[3][1] -= v.x[3][1];
  2888. x[3][2] -= v.x[3][2];
  2889. x[3][3] -= v.x[3][3];
  2890. return *this;
  2891. }
  2892. template <class T>
  2893. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2894. Matrix44<T>::operator-= (T a) IMATH_NOEXCEPT
  2895. {
  2896. x[0][0] -= a;
  2897. x[0][1] -= a;
  2898. x[0][2] -= a;
  2899. x[0][3] -= a;
  2900. x[1][0] -= a;
  2901. x[1][1] -= a;
  2902. x[1][2] -= a;
  2903. x[1][3] -= a;
  2904. x[2][0] -= a;
  2905. x[2][1] -= a;
  2906. x[2][2] -= a;
  2907. x[2][3] -= a;
  2908. x[3][0] -= a;
  2909. x[3][1] -= a;
  2910. x[3][2] -= a;
  2911. x[3][3] -= a;
  2912. return *this;
  2913. }
  2914. template <class T>
  2915. IMATH_HOSTDEVICE constexpr inline Matrix44<T>
  2916. Matrix44<T>::operator- (const Matrix44<T>& v) const IMATH_NOEXCEPT
  2917. {
  2918. return Matrix44 (x[0][0] - v.x[0][0],
  2919. x[0][1] - v.x[0][1],
  2920. x[0][2] - v.x[0][2],
  2921. x[0][3] - v.x[0][3],
  2922. x[1][0] - v.x[1][0],
  2923. x[1][1] - v.x[1][1],
  2924. x[1][2] - v.x[1][2],
  2925. x[1][3] - v.x[1][3],
  2926. x[2][0] - v.x[2][0],
  2927. x[2][1] - v.x[2][1],
  2928. x[2][2] - v.x[2][2],
  2929. x[2][3] - v.x[2][3],
  2930. x[3][0] - v.x[3][0],
  2931. x[3][1] - v.x[3][1],
  2932. x[3][2] - v.x[3][2],
  2933. x[3][3] - v.x[3][3]);
  2934. }
  2935. template <class T>
  2936. IMATH_HOSTDEVICE constexpr inline Matrix44<T>
  2937. Matrix44<T>::operator-() const IMATH_NOEXCEPT
  2938. {
  2939. return Matrix44 (-x[0][0],
  2940. -x[0][1],
  2941. -x[0][2],
  2942. -x[0][3],
  2943. -x[1][0],
  2944. -x[1][1],
  2945. -x[1][2],
  2946. -x[1][3],
  2947. -x[2][0],
  2948. -x[2][1],
  2949. -x[2][2],
  2950. -x[2][3],
  2951. -x[3][0],
  2952. -x[3][1],
  2953. -x[3][2],
  2954. -x[3][3]);
  2955. }
  2956. template <class T>
  2957. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2958. Matrix44<T>::negate() IMATH_NOEXCEPT
  2959. {
  2960. x[0][0] = -x[0][0];
  2961. x[0][1] = -x[0][1];
  2962. x[0][2] = -x[0][2];
  2963. x[0][3] = -x[0][3];
  2964. x[1][0] = -x[1][0];
  2965. x[1][1] = -x[1][1];
  2966. x[1][2] = -x[1][2];
  2967. x[1][3] = -x[1][3];
  2968. x[2][0] = -x[2][0];
  2969. x[2][1] = -x[2][1];
  2970. x[2][2] = -x[2][2];
  2971. x[2][3] = -x[2][3];
  2972. x[3][0] = -x[3][0];
  2973. x[3][1] = -x[3][1];
  2974. x[3][2] = -x[3][2];
  2975. x[3][3] = -x[3][3];
  2976. return *this;
  2977. }
  2978. template <class T>
  2979. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  2980. Matrix44<T>::operator*= (T a) IMATH_NOEXCEPT
  2981. {
  2982. x[0][0] *= a;
  2983. x[0][1] *= a;
  2984. x[0][2] *= a;
  2985. x[0][3] *= a;
  2986. x[1][0] *= a;
  2987. x[1][1] *= a;
  2988. x[1][2] *= a;
  2989. x[1][3] *= a;
  2990. x[2][0] *= a;
  2991. x[2][1] *= a;
  2992. x[2][2] *= a;
  2993. x[2][3] *= a;
  2994. x[3][0] *= a;
  2995. x[3][1] *= a;
  2996. x[3][2] *= a;
  2997. x[3][3] *= a;
  2998. return *this;
  2999. }
  3000. template <class T>
  3001. IMATH_HOSTDEVICE constexpr inline Matrix44<T>
  3002. Matrix44<T>::operator* (T a) const IMATH_NOEXCEPT
  3003. {
  3004. return Matrix44 (x[0][0] * a,
  3005. x[0][1] * a,
  3006. x[0][2] * a,
  3007. x[0][3] * a,
  3008. x[1][0] * a,
  3009. x[1][1] * a,
  3010. x[1][2] * a,
  3011. x[1][3] * a,
  3012. x[2][0] * a,
  3013. x[2][1] * a,
  3014. x[2][2] * a,
  3015. x[2][3] * a,
  3016. x[3][0] * a,
  3017. x[3][1] * a,
  3018. x[3][2] * a,
  3019. x[3][3] * a);
  3020. }
  3021. /// Matrix-scalar multiplication
  3022. template <class T>
  3023. IMATH_HOSTDEVICE inline Matrix44<T>
  3024. operator* (T a, const Matrix44<T>& v) IMATH_NOEXCEPT
  3025. {
  3026. return v * a;
  3027. }
  3028. template <class T>
  3029. IMATH_HOSTDEVICE inline IMATH_CONSTEXPR14 Matrix44<T>
  3030. Matrix44<T>::multiply (const Matrix44 &a, const Matrix44 &b) IMATH_NOEXCEPT
  3031. {
  3032. const auto a00 = a.x[0][0];
  3033. const auto a01 = a.x[0][1];
  3034. const auto a02 = a.x[0][2];
  3035. const auto a03 = a.x[0][3];
  3036. const auto c00 = a00 * b.x[0][0] + a01 * b.x[1][0] + a02 * b.x[2][0] + a03 * b.x[3][0];
  3037. const auto c01 = a00 * b.x[0][1] + a01 * b.x[1][1] + a02 * b.x[2][1] + a03 * b.x[3][1];
  3038. const auto c02 = a00 * b.x[0][2] + a01 * b.x[1][2] + a02 * b.x[2][2] + a03 * b.x[3][2];
  3039. const auto c03 = a00 * b.x[0][3] + a01 * b.x[1][3] + a02 * b.x[2][3] + a03 * b.x[3][3];
  3040. const auto a10 = a.x[1][0];
  3041. const auto a11 = a.x[1][1];
  3042. const auto a12 = a.x[1][2];
  3043. const auto a13 = a.x[1][3];
  3044. const auto c10 = a10 * b.x[0][0] + a11 * b.x[1][0] + a12 * b.x[2][0] + a13 * b.x[3][0];
  3045. const auto c11 = a10 * b.x[0][1] + a11 * b.x[1][1] + a12 * b.x[2][1] + a13 * b.x[3][1];
  3046. const auto c12 = a10 * b.x[0][2] + a11 * b.x[1][2] + a12 * b.x[2][2] + a13 * b.x[3][2];
  3047. const auto c13 = a10 * b.x[0][3] + a11 * b.x[1][3] + a12 * b.x[2][3] + a13 * b.x[3][3];
  3048. const auto a20 = a.x[2][0];
  3049. const auto a21 = a.x[2][1];
  3050. const auto a22 = a.x[2][2];
  3051. const auto a23 = a.x[2][3];
  3052. const auto c20 = a20 * b.x[0][0] + a21 * b.x[1][0] + a22 * b.x[2][0] + a23 * b.x[3][0];
  3053. const auto c21 = a20 * b.x[0][1] + a21 * b.x[1][1] + a22 * b.x[2][1] + a23 * b.x[3][1];
  3054. const auto c22 = a20 * b.x[0][2] + a21 * b.x[1][2] + a22 * b.x[2][2] + a23 * b.x[3][2];
  3055. const auto c23 = a20 * b.x[0][3] + a21 * b.x[1][3] + a22 * b.x[2][3] + a23 * b.x[3][3];
  3056. const auto a30 = a.x[3][0];
  3057. const auto a31 = a.x[3][1];
  3058. const auto a32 = a.x[3][2];
  3059. const auto a33 = a.x[3][3];
  3060. const auto c30 = a30 * b.x[0][0] + a31 * b.x[1][0] + a32 * b.x[2][0] + a33 * b.x[3][0];
  3061. const auto c31 = a30 * b.x[0][1] + a31 * b.x[1][1] + a32 * b.x[2][1] + a33 * b.x[3][1];
  3062. const auto c32 = a30 * b.x[0][2] + a31 * b.x[1][2] + a32 * b.x[2][2] + a33 * b.x[3][2];
  3063. const auto c33 = a30 * b.x[0][3] + a31 * b.x[1][3] + a32 * b.x[2][3] + a33 * b.x[3][3];
  3064. return Matrix44(c00, c01, c02, c03,
  3065. c10, c11, c12, c13,
  3066. c20, c21, c22, c23,
  3067. c30, c31, c32, c33);
  3068. }
  3069. template <class T>
  3070. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3071. Matrix44<T>::operator*= (const Matrix44<T>& v) IMATH_NOEXCEPT
  3072. {
  3073. *this = multiply(*this, v);
  3074. return *this;
  3075. }
  3076. template <class T>
  3077. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>
  3078. Matrix44<T>::operator* (const Matrix44<T>& v) const IMATH_NOEXCEPT
  3079. {
  3080. return multiply(*this, v);
  3081. }
  3082. template <class T>
  3083. IMATH_HOSTDEVICE inline void
  3084. Matrix44<T>::multiply (const Matrix44<T>& a, const Matrix44<T>& b, Matrix44<T>& c) IMATH_NOEXCEPT
  3085. {
  3086. c = multiply(a, b);
  3087. }
  3088. template <class T>
  3089. template <class S>
  3090. IMATH_HOSTDEVICE inline void
  3091. Matrix44<T>::multVecMatrix (const Vec3<S>& src, Vec3<S>& dst) const IMATH_NOEXCEPT
  3092. {
  3093. S a, b, c, w;
  3094. a = src.x * x[0][0] + src.y * x[1][0] + src.z * x[2][0] + x[3][0];
  3095. b = src.x * x[0][1] + src.y * x[1][1] + src.z * x[2][1] + x[3][1];
  3096. c = src.x * x[0][2] + src.y * x[1][2] + src.z * x[2][2] + x[3][2];
  3097. w = src.x * x[0][3] + src.y * x[1][3] + src.z * x[2][3] + x[3][3];
  3098. dst.x = a / w;
  3099. dst.y = b / w;
  3100. dst.z = c / w;
  3101. }
  3102. template <class T>
  3103. template <class S>
  3104. IMATH_HOSTDEVICE inline void
  3105. Matrix44<T>::multDirMatrix (const Vec3<S>& src, Vec3<S>& dst) const IMATH_NOEXCEPT
  3106. {
  3107. S a, b, c;
  3108. a = src.x * x[0][0] + src.y * x[1][0] + src.z * x[2][0];
  3109. b = src.x * x[0][1] + src.y * x[1][1] + src.z * x[2][1];
  3110. c = src.x * x[0][2] + src.y * x[1][2] + src.z * x[2][2];
  3111. dst.x = a;
  3112. dst.y = b;
  3113. dst.z = c;
  3114. }
  3115. template <class T>
  3116. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3117. Matrix44<T>::operator/= (T a) IMATH_NOEXCEPT
  3118. {
  3119. x[0][0] /= a;
  3120. x[0][1] /= a;
  3121. x[0][2] /= a;
  3122. x[0][3] /= a;
  3123. x[1][0] /= a;
  3124. x[1][1] /= a;
  3125. x[1][2] /= a;
  3126. x[1][3] /= a;
  3127. x[2][0] /= a;
  3128. x[2][1] /= a;
  3129. x[2][2] /= a;
  3130. x[2][3] /= a;
  3131. x[3][0] /= a;
  3132. x[3][1] /= a;
  3133. x[3][2] /= a;
  3134. x[3][3] /= a;
  3135. return *this;
  3136. }
  3137. template <class T>
  3138. IMATH_HOSTDEVICE constexpr inline Matrix44<T>
  3139. Matrix44<T>::operator/ (T a) const IMATH_NOEXCEPT
  3140. {
  3141. return Matrix44 (x[0][0] / a,
  3142. x[0][1] / a,
  3143. x[0][2] / a,
  3144. x[0][3] / a,
  3145. x[1][0] / a,
  3146. x[1][1] / a,
  3147. x[1][2] / a,
  3148. x[1][3] / a,
  3149. x[2][0] / a,
  3150. x[2][1] / a,
  3151. x[2][2] / a,
  3152. x[2][3] / a,
  3153. x[3][0] / a,
  3154. x[3][1] / a,
  3155. x[3][2] / a,
  3156. x[3][3] / a);
  3157. }
  3158. template <class T>
  3159. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3160. Matrix44<T>::transpose() IMATH_NOEXCEPT
  3161. {
  3162. Matrix44 tmp (x[0][0],
  3163. x[1][0],
  3164. x[2][0],
  3165. x[3][0],
  3166. x[0][1],
  3167. x[1][1],
  3168. x[2][1],
  3169. x[3][1],
  3170. x[0][2],
  3171. x[1][2],
  3172. x[2][2],
  3173. x[3][2],
  3174. x[0][3],
  3175. x[1][3],
  3176. x[2][3],
  3177. x[3][3]);
  3178. *this = tmp;
  3179. return *this;
  3180. }
  3181. template <class T>
  3182. IMATH_HOSTDEVICE constexpr inline Matrix44<T>
  3183. Matrix44<T>::transposed() const IMATH_NOEXCEPT
  3184. {
  3185. return Matrix44 (x[0][0],
  3186. x[1][0],
  3187. x[2][0],
  3188. x[3][0],
  3189. x[0][1],
  3190. x[1][1],
  3191. x[2][1],
  3192. x[3][1],
  3193. x[0][2],
  3194. x[1][2],
  3195. x[2][2],
  3196. x[3][2],
  3197. x[0][3],
  3198. x[1][3],
  3199. x[2][3],
  3200. x[3][3]);
  3201. }
  3202. template <class T>
  3203. IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3204. Matrix44<T>::gjInvert (bool singExc)
  3205. {
  3206. *this = gjInverse (singExc);
  3207. return *this;
  3208. }
  3209. template <class T>
  3210. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3211. Matrix44<T>::gjInvert() IMATH_NOEXCEPT
  3212. {
  3213. *this = gjInverse();
  3214. return *this;
  3215. }
  3216. template <class T>
  3217. inline Matrix44<T>
  3218. Matrix44<T>::gjInverse (bool singExc) const
  3219. {
  3220. int i, j, k;
  3221. Matrix44 s;
  3222. Matrix44 t (*this);
  3223. // Forward elimination
  3224. for (i = 0; i < 3; i++)
  3225. {
  3226. int pivot = i;
  3227. T pivotsize = t.x[i][i];
  3228. if (pivotsize < 0)
  3229. pivotsize = -pivotsize;
  3230. for (j = i + 1; j < 4; j++)
  3231. {
  3232. T tmp = t.x[j][i];
  3233. if (tmp < 0)
  3234. tmp = -tmp;
  3235. if (tmp > pivotsize)
  3236. {
  3237. pivot = j;
  3238. pivotsize = tmp;
  3239. }
  3240. }
  3241. if (pivotsize == 0)
  3242. {
  3243. if (singExc)
  3244. throw std::invalid_argument ("Cannot invert singular matrix.");
  3245. return Matrix44();
  3246. }
  3247. if (pivot != i)
  3248. {
  3249. for (j = 0; j < 4; j++)
  3250. {
  3251. T tmp;
  3252. tmp = t.x[i][j];
  3253. t.x[i][j] = t.x[pivot][j];
  3254. t.x[pivot][j] = tmp;
  3255. tmp = s.x[i][j];
  3256. s.x[i][j] = s.x[pivot][j];
  3257. s.x[pivot][j] = tmp;
  3258. }
  3259. }
  3260. for (j = i + 1; j < 4; j++)
  3261. {
  3262. T f = t.x[j][i] / t.x[i][i];
  3263. for (k = 0; k < 4; k++)
  3264. {
  3265. t.x[j][k] -= f * t.x[i][k];
  3266. s.x[j][k] -= f * s.x[i][k];
  3267. }
  3268. }
  3269. }
  3270. // Backward substitution
  3271. for (i = 3; i >= 0; --i)
  3272. {
  3273. T f;
  3274. if ((f = t.x[i][i]) == 0)
  3275. {
  3276. if (singExc)
  3277. throw std::invalid_argument ("Cannot invert singular matrix.");
  3278. return Matrix44();
  3279. }
  3280. for (j = 0; j < 4; j++)
  3281. {
  3282. t.x[i][j] /= f;
  3283. s.x[i][j] /= f;
  3284. }
  3285. for (j = 0; j < i; j++)
  3286. {
  3287. f = t.x[j][i];
  3288. for (k = 0; k < 4; k++)
  3289. {
  3290. t.x[j][k] -= f * t.x[i][k];
  3291. s.x[j][k] -= f * s.x[i][k];
  3292. }
  3293. }
  3294. }
  3295. return s;
  3296. }
  3297. template <class T>
  3298. IMATH_HOSTDEVICE inline Matrix44<T>
  3299. Matrix44<T>::gjInverse() const IMATH_NOEXCEPT
  3300. {
  3301. int i, j, k;
  3302. Matrix44 s;
  3303. Matrix44 t (*this);
  3304. // Forward elimination
  3305. for (i = 0; i < 3; i++)
  3306. {
  3307. int pivot = i;
  3308. T pivotsize = t.x[i][i];
  3309. if (pivotsize < 0)
  3310. pivotsize = -pivotsize;
  3311. for (j = i + 1; j < 4; j++)
  3312. {
  3313. T tmp = t.x[j][i];
  3314. if (tmp < 0)
  3315. tmp = -tmp;
  3316. if (tmp > pivotsize)
  3317. {
  3318. pivot = j;
  3319. pivotsize = tmp;
  3320. }
  3321. }
  3322. if (pivotsize == 0)
  3323. {
  3324. return Matrix44();
  3325. }
  3326. if (pivot != i)
  3327. {
  3328. for (j = 0; j < 4; j++)
  3329. {
  3330. T tmp;
  3331. tmp = t.x[i][j];
  3332. t.x[i][j] = t.x[pivot][j];
  3333. t.x[pivot][j] = tmp;
  3334. tmp = s.x[i][j];
  3335. s.x[i][j] = s.x[pivot][j];
  3336. s.x[pivot][j] = tmp;
  3337. }
  3338. }
  3339. for (j = i + 1; j < 4; j++)
  3340. {
  3341. T f = t.x[j][i] / t.x[i][i];
  3342. for (k = 0; k < 4; k++)
  3343. {
  3344. t.x[j][k] -= f * t.x[i][k];
  3345. s.x[j][k] -= f * s.x[i][k];
  3346. }
  3347. }
  3348. }
  3349. // Backward substitution
  3350. for (i = 3; i >= 0; --i)
  3351. {
  3352. T f;
  3353. if ((f = t.x[i][i]) == 0)
  3354. {
  3355. return Matrix44();
  3356. }
  3357. for (j = 0; j < 4; j++)
  3358. {
  3359. t.x[i][j] /= f;
  3360. s.x[i][j] /= f;
  3361. }
  3362. for (j = 0; j < i; j++)
  3363. {
  3364. f = t.x[j][i];
  3365. for (k = 0; k < 4; k++)
  3366. {
  3367. t.x[j][k] -= f * t.x[i][k];
  3368. s.x[j][k] -= f * s.x[i][k];
  3369. }
  3370. }
  3371. }
  3372. return s;
  3373. }
  3374. template <class T>
  3375. IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3376. Matrix44<T>::invert (bool singExc)
  3377. {
  3378. *this = inverse (singExc);
  3379. return *this;
  3380. }
  3381. template <class T>
  3382. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3383. Matrix44<T>::invert() IMATH_NOEXCEPT
  3384. {
  3385. *this = inverse();
  3386. return *this;
  3387. }
  3388. template <class T>
  3389. IMATH_CONSTEXPR14 inline Matrix44<T>
  3390. Matrix44<T>::inverse (bool singExc) const
  3391. {
  3392. if (x[0][3] != 0 || x[1][3] != 0 || x[2][3] != 0 || x[3][3] != 1)
  3393. return gjInverse (singExc);
  3394. Matrix44 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
  3395. x[2][1] * x[0][2] - x[0][1] * x[2][2],
  3396. x[0][1] * x[1][2] - x[1][1] * x[0][2],
  3397. 0,
  3398. x[2][0] * x[1][2] - x[1][0] * x[2][2],
  3399. x[0][0] * x[2][2] - x[2][0] * x[0][2],
  3400. x[1][0] * x[0][2] - x[0][0] * x[1][2],
  3401. 0,
  3402. x[1][0] * x[2][1] - x[2][0] * x[1][1],
  3403. x[2][0] * x[0][1] - x[0][0] * x[2][1],
  3404. x[0][0] * x[1][1] - x[1][0] * x[0][1],
  3405. 0,
  3406. 0,
  3407. 0,
  3408. 0,
  3409. 1);
  3410. T r = x[0][0] * s.x[0][0] + x[0][1] * s.x[1][0] + x[0][2] * s.x[2][0];
  3411. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  3412. {
  3413. for (int i = 0; i < 3; ++i)
  3414. {
  3415. for (int j = 0; j < 3; ++j)
  3416. {
  3417. s.x[i][j] /= r;
  3418. }
  3419. }
  3420. }
  3421. else
  3422. {
  3423. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  3424. for (int i = 0; i < 3; ++i)
  3425. {
  3426. for (int j = 0; j < 3; ++j)
  3427. {
  3428. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s.x[i][j]))
  3429. {
  3430. s.x[i][j] /= r;
  3431. }
  3432. else
  3433. {
  3434. if (singExc)
  3435. throw std::invalid_argument ("Cannot invert singular matrix.");
  3436. return Matrix44();
  3437. }
  3438. }
  3439. }
  3440. }
  3441. s.x[3][0] = -x[3][0] * s.x[0][0] - x[3][1] * s.x[1][0] - x[3][2] * s.x[2][0];
  3442. s.x[3][1] = -x[3][0] * s.x[0][1] - x[3][1] * s.x[1][1] - x[3][2] * s.x[2][1];
  3443. s.x[3][2] = -x[3][0] * s.x[0][2] - x[3][1] * s.x[1][2] - x[3][2] * s.x[2][2];
  3444. return s;
  3445. }
  3446. template <class T>
  3447. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Matrix44<T>
  3448. Matrix44<T>::inverse() const IMATH_NOEXCEPT
  3449. {
  3450. if (x[0][3] != 0 || x[1][3] != 0 || x[2][3] != 0 || x[3][3] != 1)
  3451. return gjInverse();
  3452. Matrix44 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
  3453. x[2][1] * x[0][2] - x[0][1] * x[2][2],
  3454. x[0][1] * x[1][2] - x[1][1] * x[0][2],
  3455. 0,
  3456. x[2][0] * x[1][2] - x[1][0] * x[2][2],
  3457. x[0][0] * x[2][2] - x[2][0] * x[0][2],
  3458. x[1][0] * x[0][2] - x[0][0] * x[1][2],
  3459. 0,
  3460. x[1][0] * x[2][1] - x[2][0] * x[1][1],
  3461. x[2][0] * x[0][1] - x[0][0] * x[2][1],
  3462. x[0][0] * x[1][1] - x[1][0] * x[0][1],
  3463. 0,
  3464. 0,
  3465. 0,
  3466. 0,
  3467. 1);
  3468. T r = x[0][0] * s.x[0][0] + x[0][1] * s.x[1][0] + x[0][2] * s.x[2][0];
  3469. if (IMATH_INTERNAL_NAMESPACE::abs (r) >= 1)
  3470. {
  3471. for (int i = 0; i < 3; ++i)
  3472. {
  3473. for (int j = 0; j < 3; ++j)
  3474. {
  3475. s.x[i][j] /= r;
  3476. }
  3477. }
  3478. }
  3479. else
  3480. {
  3481. T mr = IMATH_INTERNAL_NAMESPACE::abs (r) / std::numeric_limits<T>::min();
  3482. for (int i = 0; i < 3; ++i)
  3483. {
  3484. for (int j = 0; j < 3; ++j)
  3485. {
  3486. if (mr > IMATH_INTERNAL_NAMESPACE::abs (s.x[i][j]))
  3487. {
  3488. s.x[i][j] /= r;
  3489. }
  3490. else
  3491. {
  3492. return Matrix44();
  3493. }
  3494. }
  3495. }
  3496. }
  3497. s.x[3][0] = -x[3][0] * s.x[0][0] - x[3][1] * s.x[1][0] - x[3][2] * s.x[2][0];
  3498. s.x[3][1] = -x[3][0] * s.x[0][1] - x[3][1] * s.x[1][1] - x[3][2] * s.x[2][1];
  3499. s.x[3][2] = -x[3][0] * s.x[0][2] - x[3][1] * s.x[1][2] - x[3][2] * s.x[2][2];
  3500. return s;
  3501. }
  3502. template <class T>
  3503. IMATH_HOSTDEVICE constexpr inline T
  3504. Matrix44<T>::fastMinor (const int r0,
  3505. const int r1,
  3506. const int r2,
  3507. const int c0,
  3508. const int c1,
  3509. const int c2) const IMATH_NOEXCEPT
  3510. {
  3511. return x[r0][c0] * (x[r1][c1] * x[r2][c2] - x[r1][c2] * x[r2][c1]) +
  3512. x[r0][c1] * (x[r1][c2] * x[r2][c0] - x[r1][c0] * x[r2][c2]) +
  3513. x[r0][c2] * (x[r1][c0] * x[r2][c1] - x[r1][c1] * x[r2][c0]);
  3514. }
  3515. template <class T>
  3516. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
  3517. Matrix44<T>::minorOf (const int r, const int c) const IMATH_NOEXCEPT
  3518. {
  3519. int r0 = 0 + (r < 1 ? 1 : 0);
  3520. int r1 = 1 + (r < 2 ? 1 : 0);
  3521. int r2 = 2 + (r < 3 ? 1 : 0);
  3522. int c0 = 0 + (c < 1 ? 1 : 0);
  3523. int c1 = 1 + (c < 2 ? 1 : 0);
  3524. int c2 = 2 + (c < 3 ? 1 : 0);
  3525. Matrix33<T> working (x[r0][c0],
  3526. x[r1][c0],
  3527. x[r2][c0],
  3528. x[r0][c1],
  3529. x[r1][c1],
  3530. x[r2][c1],
  3531. x[r0][c2],
  3532. x[r1][c2],
  3533. x[r2][c2]);
  3534. return working.determinant();
  3535. }
  3536. template <class T>
  3537. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
  3538. Matrix44<T>::determinant() const IMATH_NOEXCEPT
  3539. {
  3540. T sum = (T) 0;
  3541. if (x[0][3] != 0.)
  3542. sum -= x[0][3] * fastMinor (1, 2, 3, 0, 1, 2);
  3543. if (x[1][3] != 0.)
  3544. sum += x[1][3] * fastMinor (0, 2, 3, 0, 1, 2);
  3545. if (x[2][3] != 0.)
  3546. sum -= x[2][3] * fastMinor (0, 1, 3, 0, 1, 2);
  3547. if (x[3][3] != 0.)
  3548. sum += x[3][3] * fastMinor (0, 1, 2, 0, 1, 2);
  3549. return sum;
  3550. }
  3551. template <class T>
  3552. IMATH_HOSTDEVICE constexpr inline T
  3553. Matrix44<T>::trace () const IMATH_NOEXCEPT
  3554. {
  3555. return x[0][0] + x[1][1] + x[2][2] + x[3][3];
  3556. }
  3557. template <class T>
  3558. template <class S>
  3559. IMATH_HOSTDEVICE inline const Matrix44<T>&
  3560. Matrix44<T>::setEulerAngles (const Vec3<S>& r) IMATH_NOEXCEPT
  3561. {
  3562. S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
  3563. cos_rz = cos ((T) r.z);
  3564. cos_ry = cos ((T) r.y);
  3565. cos_rx = cos ((T) r.x);
  3566. sin_rz = sin ((T) r.z);
  3567. sin_ry = sin ((T) r.y);
  3568. sin_rx = sin ((T) r.x);
  3569. x[0][0] = cos_rz * cos_ry;
  3570. x[0][1] = sin_rz * cos_ry;
  3571. x[0][2] = -sin_ry;
  3572. x[0][3] = 0;
  3573. x[1][0] = -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx;
  3574. x[1][1] = cos_rz * cos_rx + sin_rz * sin_ry * sin_rx;
  3575. x[1][2] = cos_ry * sin_rx;
  3576. x[1][3] = 0;
  3577. x[2][0] = sin_rz * sin_rx + cos_rz * sin_ry * cos_rx;
  3578. x[2][1] = -cos_rz * sin_rx + sin_rz * sin_ry * cos_rx;
  3579. x[2][2] = cos_ry * cos_rx;
  3580. x[2][3] = 0;
  3581. x[3][0] = 0;
  3582. x[3][1] = 0;
  3583. x[3][2] = 0;
  3584. x[3][3] = 1;
  3585. return *this;
  3586. }
  3587. template <class T>
  3588. template <class S>
  3589. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3590. Matrix44<T>::setAxisAngle (const Vec3<S>& axis, S angle) IMATH_NOEXCEPT
  3591. {
  3592. Vec3<S> unit (axis.normalized());
  3593. S sine = std::sin (angle);
  3594. S cosine = std::cos (angle);
  3595. x[0][0] = unit.x * unit.x * (1 - cosine) + cosine;
  3596. x[0][1] = unit.x * unit.y * (1 - cosine) + unit.z * sine;
  3597. x[0][2] = unit.x * unit.z * (1 - cosine) - unit.y * sine;
  3598. x[0][3] = 0;
  3599. x[1][0] = unit.x * unit.y * (1 - cosine) - unit.z * sine;
  3600. x[1][1] = unit.y * unit.y * (1 - cosine) + cosine;
  3601. x[1][2] = unit.y * unit.z * (1 - cosine) + unit.x * sine;
  3602. x[1][3] = 0;
  3603. x[2][0] = unit.x * unit.z * (1 - cosine) + unit.y * sine;
  3604. x[2][1] = unit.y * unit.z * (1 - cosine) - unit.x * sine;
  3605. x[2][2] = unit.z * unit.z * (1 - cosine) + cosine;
  3606. x[2][3] = 0;
  3607. x[3][0] = 0;
  3608. x[3][1] = 0;
  3609. x[3][2] = 0;
  3610. x[3][3] = 1;
  3611. return *this;
  3612. }
  3613. template <class T>
  3614. template <class S>
  3615. IMATH_HOSTDEVICE inline const Matrix44<T>&
  3616. Matrix44<T>::rotate (const Vec3<S>& r) IMATH_NOEXCEPT
  3617. {
  3618. S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
  3619. S m00, m01, m02;
  3620. S m10, m11, m12;
  3621. S m20, m21, m22;
  3622. cos_rz = cos ((S) r.z);
  3623. cos_ry = cos ((S) r.y);
  3624. cos_rx = cos ((S) r.x);
  3625. sin_rz = sin ((S) r.z);
  3626. sin_ry = sin ((S) r.y);
  3627. sin_rx = sin ((S) r.x);
  3628. m00 = cos_rz * cos_ry;
  3629. m01 = sin_rz * cos_ry;
  3630. m02 = -sin_ry;
  3631. m10 = -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx;
  3632. m11 = cos_rz * cos_rx + sin_rz * sin_ry * sin_rx;
  3633. m12 = cos_ry * sin_rx;
  3634. m20 = -sin_rz * -sin_rx + cos_rz * sin_ry * cos_rx;
  3635. m21 = cos_rz * -sin_rx + sin_rz * sin_ry * cos_rx;
  3636. m22 = cos_ry * cos_rx;
  3637. Matrix44<T> P (*this);
  3638. x[0][0] = P.x[0][0] * m00 + P.x[1][0] * m01 + P.x[2][0] * m02;
  3639. x[0][1] = P.x[0][1] * m00 + P.x[1][1] * m01 + P.x[2][1] * m02;
  3640. x[0][2] = P.x[0][2] * m00 + P.x[1][2] * m01 + P.x[2][2] * m02;
  3641. x[0][3] = P.x[0][3] * m00 + P.x[1][3] * m01 + P.x[2][3] * m02;
  3642. x[1][0] = P.x[0][0] * m10 + P.x[1][0] * m11 + P.x[2][0] * m12;
  3643. x[1][1] = P.x[0][1] * m10 + P.x[1][1] * m11 + P.x[2][1] * m12;
  3644. x[1][2] = P.x[0][2] * m10 + P.x[1][2] * m11 + P.x[2][2] * m12;
  3645. x[1][3] = P.x[0][3] * m10 + P.x[1][3] * m11 + P.x[2][3] * m12;
  3646. x[2][0] = P.x[0][0] * m20 + P.x[1][0] * m21 + P.x[2][0] * m22;
  3647. x[2][1] = P.x[0][1] * m20 + P.x[1][1] * m21 + P.x[2][1] * m22;
  3648. x[2][2] = P.x[0][2] * m20 + P.x[1][2] * m21 + P.x[2][2] * m22;
  3649. x[2][3] = P.x[0][3] * m20 + P.x[1][3] * m21 + P.x[2][3] * m22;
  3650. return *this;
  3651. }
  3652. template <class T>
  3653. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3654. Matrix44<T>::setScale (T s) IMATH_NOEXCEPT
  3655. {
  3656. //
  3657. // Set the matrix to a 3D homogeneous transform scale:
  3658. // | s 0 0 0 |
  3659. // | 0 s 0 0 |
  3660. // | 0 0 s 0 |
  3661. // | 0 0 0 1 |
  3662. //
  3663. x[0][0] = s;
  3664. x[0][1] = 0;
  3665. x[0][2] = 0;
  3666. x[0][3] = 0;
  3667. x[1][0] = 0;
  3668. x[1][1] = s;
  3669. x[1][2] = 0;
  3670. x[1][3] = 0;
  3671. x[2][0] = 0;
  3672. x[2][1] = 0;
  3673. x[2][2] = s;
  3674. x[2][3] = 0;
  3675. x[3][0] = 0;
  3676. x[3][1] = 0;
  3677. x[3][2] = 0;
  3678. x[3][3] = 1;
  3679. return *this;
  3680. }
  3681. template <class T>
  3682. template <class S>
  3683. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3684. Matrix44<T>::setScale (const Vec3<S>& s) IMATH_NOEXCEPT
  3685. {
  3686. //
  3687. // Set the matrix to a 3D homogeneous transform scale:
  3688. // | s.x 0 0 0 |
  3689. // | 0 s.y 0 0 |
  3690. // | 0 0 s.z 0 |
  3691. // | 0 0 0 1 |
  3692. //
  3693. x[0][0] = s.x;
  3694. x[0][1] = 0;
  3695. x[0][2] = 0;
  3696. x[0][3] = 0;
  3697. x[1][0] = 0;
  3698. x[1][1] = s.y;
  3699. x[1][2] = 0;
  3700. x[1][3] = 0;
  3701. x[2][0] = 0;
  3702. x[2][1] = 0;
  3703. x[2][2] = s.z;
  3704. x[2][3] = 0;
  3705. x[3][0] = 0;
  3706. x[3][1] = 0;
  3707. x[3][2] = 0;
  3708. x[3][3] = 1;
  3709. return *this;
  3710. }
  3711. template <class T>
  3712. template <class S>
  3713. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3714. Matrix44<T>::scale (const Vec3<S>& s) IMATH_NOEXCEPT
  3715. {
  3716. x[0][0] *= s.x;
  3717. x[0][1] *= s.x;
  3718. x[0][2] *= s.x;
  3719. x[0][3] *= s.x;
  3720. x[1][0] *= s.y;
  3721. x[1][1] *= s.y;
  3722. x[1][2] *= s.y;
  3723. x[1][3] *= s.y;
  3724. x[2][0] *= s.z;
  3725. x[2][1] *= s.z;
  3726. x[2][2] *= s.z;
  3727. x[2][3] *= s.z;
  3728. return *this;
  3729. }
  3730. template <class T>
  3731. template <class S>
  3732. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3733. Matrix44<T>::setTranslation (const Vec3<S>& t) IMATH_NOEXCEPT
  3734. {
  3735. x[0][0] = 1;
  3736. x[0][1] = 0;
  3737. x[0][2] = 0;
  3738. x[0][3] = 0;
  3739. x[1][0] = 0;
  3740. x[1][1] = 1;
  3741. x[1][2] = 0;
  3742. x[1][3] = 0;
  3743. x[2][0] = 0;
  3744. x[2][1] = 0;
  3745. x[2][2] = 1;
  3746. x[2][3] = 0;
  3747. x[3][0] = t.x;
  3748. x[3][1] = t.y;
  3749. x[3][2] = t.z;
  3750. x[3][3] = 1;
  3751. return *this;
  3752. }
  3753. template <class T>
  3754. IMATH_HOSTDEVICE constexpr inline const Vec3<T>
  3755. Matrix44<T>::translation() const IMATH_NOEXCEPT
  3756. {
  3757. return Vec3<T> (x[3][0], x[3][1], x[3][2]);
  3758. }
  3759. template <class T>
  3760. template <class S>
  3761. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3762. Matrix44<T>::translate (const Vec3<S>& t) IMATH_NOEXCEPT
  3763. {
  3764. x[3][0] += t.x * x[0][0] + t.y * x[1][0] + t.z * x[2][0];
  3765. x[3][1] += t.x * x[0][1] + t.y * x[1][1] + t.z * x[2][1];
  3766. x[3][2] += t.x * x[0][2] + t.y * x[1][2] + t.z * x[2][2];
  3767. x[3][3] += t.x * x[0][3] + t.y * x[1][3] + t.z * x[2][3];
  3768. return *this;
  3769. }
  3770. template <class T>
  3771. template <class S>
  3772. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3773. Matrix44<T>::setShear (const Vec3<S>& h) IMATH_NOEXCEPT
  3774. {
  3775. x[0][0] = 1;
  3776. x[0][1] = 0;
  3777. x[0][2] = 0;
  3778. x[0][3] = 0;
  3779. x[1][0] = h.x;
  3780. x[1][1] = 1;
  3781. x[1][2] = 0;
  3782. x[1][3] = 0;
  3783. x[2][0] = h.y;
  3784. x[2][1] = h.z;
  3785. x[2][2] = 1;
  3786. x[2][3] = 0;
  3787. x[3][0] = 0;
  3788. x[3][1] = 0;
  3789. x[3][2] = 0;
  3790. x[3][3] = 1;
  3791. return *this;
  3792. }
  3793. template <class T>
  3794. template <class S>
  3795. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3796. Matrix44<T>::setShear (const Shear6<S>& h) IMATH_NOEXCEPT
  3797. {
  3798. x[0][0] = 1;
  3799. x[0][1] = h.yx;
  3800. x[0][2] = h.zx;
  3801. x[0][3] = 0;
  3802. x[1][0] = h.xy;
  3803. x[1][1] = 1;
  3804. x[1][2] = h.zy;
  3805. x[1][3] = 0;
  3806. x[2][0] = h.xz;
  3807. x[2][1] = h.yz;
  3808. x[2][2] = 1;
  3809. x[2][3] = 0;
  3810. x[3][0] = 0;
  3811. x[3][1] = 0;
  3812. x[3][2] = 0;
  3813. x[3][3] = 1;
  3814. return *this;
  3815. }
  3816. template <class T>
  3817. template <class S>
  3818. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3819. Matrix44<T>::shear (const Vec3<S>& h) IMATH_NOEXCEPT
  3820. {
  3821. //
  3822. // In this case, we don't need a temp. copy of the matrix
  3823. // because we never use a value on the RHS after we've
  3824. // changed it on the LHS.
  3825. //
  3826. for (int i = 0; i < 4; i++)
  3827. {
  3828. x[2][i] += h.y * x[0][i] + h.z * x[1][i];
  3829. x[1][i] += h.x * x[0][i];
  3830. }
  3831. return *this;
  3832. }
  3833. template <class T>
  3834. template <class S>
  3835. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Matrix44<T>&
  3836. Matrix44<T>::shear (const Shear6<S>& h) IMATH_NOEXCEPT
  3837. {
  3838. Matrix44<T> P (*this);
  3839. for (int i = 0; i < 4; i++)
  3840. {
  3841. x[0][i] = P.x[0][i] + h.yx * P.x[1][i] + h.zx * P.x[2][i];
  3842. x[1][i] = h.xy * P.x[0][i] + P.x[1][i] + h.zy * P.x[2][i];
  3843. x[2][i] = h.xz * P.x[0][i] + h.yz * P.x[1][i] + P.x[2][i];
  3844. }
  3845. return *this;
  3846. }
  3847. //--------------------------------
  3848. // Implementation of stream output
  3849. //--------------------------------
  3850. template <class T>
  3851. std::ostream&
  3852. operator<< (std::ostream& s, const Matrix22<T>& m)
  3853. {
  3854. std::ios_base::fmtflags oldFlags = s.flags();
  3855. int width;
  3856. if (s.flags() & std::ios_base::fixed)
  3857. {
  3858. s.setf (std::ios_base::showpoint);
  3859. width = static_cast<int> (s.precision()) + 5;
  3860. }
  3861. else
  3862. {
  3863. s.setf (std::ios_base::scientific);
  3864. s.setf (std::ios_base::showpoint);
  3865. width = static_cast<int> (s.precision()) + 8;
  3866. }
  3867. s << "(" << std::setw (width) << m[0][0] << " " << std::setw (width) << m[0][1] << "\n"
  3868. <<
  3869. " " << std::setw (width) << m[1][0] << " " << std::setw (width) << m[1][1] << ")\n";
  3870. s.flags (oldFlags);
  3871. return s;
  3872. }
  3873. template <class T>
  3874. std::ostream&
  3875. operator<< (std::ostream& s, const Matrix33<T>& m)
  3876. {
  3877. std::ios_base::fmtflags oldFlags = s.flags();
  3878. int width;
  3879. if (s.flags() & std::ios_base::fixed)
  3880. {
  3881. s.setf (std::ios_base::showpoint);
  3882. width = static_cast<int> (s.precision()) + 5;
  3883. }
  3884. else
  3885. {
  3886. s.setf (std::ios_base::scientific);
  3887. s.setf (std::ios_base::showpoint);
  3888. width = static_cast<int> (s.precision()) + 8;
  3889. }
  3890. s << "(" << std::setw (width) << m[0][0] << " " << std::setw (width) << m[0][1] << " "
  3891. << std::setw (width) << m[0][2] << "\n"
  3892. <<
  3893. " " << std::setw (width) << m[1][0] << " " << std::setw (width) << m[1][1] << " "
  3894. << std::setw (width) << m[1][2] << "\n"
  3895. <<
  3896. " " << std::setw (width) << m[2][0] << " " << std::setw (width) << m[2][1] << " "
  3897. << std::setw (width) << m[2][2] << ")\n";
  3898. s.flags (oldFlags);
  3899. return s;
  3900. }
  3901. template <class T>
  3902. std::ostream&
  3903. operator<< (std::ostream& s, const Matrix44<T>& m)
  3904. {
  3905. std::ios_base::fmtflags oldFlags = s.flags();
  3906. int width;
  3907. if (s.flags() & std::ios_base::fixed)
  3908. {
  3909. s.setf (std::ios_base::showpoint);
  3910. width = static_cast<int> (s.precision()) + 5;
  3911. }
  3912. else
  3913. {
  3914. s.setf (std::ios_base::scientific);
  3915. s.setf (std::ios_base::showpoint);
  3916. width = static_cast<int> (s.precision()) + 8;
  3917. }
  3918. s << "(" << std::setw (width) << m[0][0] << " " << std::setw (width) << m[0][1] << " "
  3919. << std::setw (width) << m[0][2] << " " << std::setw (width) << m[0][3] << "\n"
  3920. <<
  3921. " " << std::setw (width) << m[1][0] << " " << std::setw (width) << m[1][1] << " "
  3922. << std::setw (width) << m[1][2] << " " << std::setw (width) << m[1][3] << "\n"
  3923. <<
  3924. " " << std::setw (width) << m[2][0] << " " << std::setw (width) << m[2][1] << " "
  3925. << std::setw (width) << m[2][2] << " " << std::setw (width) << m[2][3] << "\n"
  3926. <<
  3927. " " << std::setw (width) << m[3][0] << " " << std::setw (width) << m[3][1] << " "
  3928. << std::setw (width) << m[3][2] << " " << std::setw (width) << m[3][3] << ")\n";
  3929. s.flags (oldFlags);
  3930. return s;
  3931. }
  3932. //---------------------------------------------------------------
  3933. // Implementation of vector-times-matrix multiplication operators
  3934. //---------------------------------------------------------------
  3935. template <class S, class T>
  3936. IMATH_HOSTDEVICE inline const Vec2<S>&
  3937. operator*= (Vec2<S>& v, const Matrix22<T>& m) IMATH_NOEXCEPT
  3938. {
  3939. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0]);
  3940. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1]);
  3941. v.x = x;
  3942. v.y = y;
  3943. return v;
  3944. }
  3945. template <class S, class T>
  3946. IMATH_HOSTDEVICE inline Vec2<S>
  3947. operator* (const Vec2<S>& v, const Matrix22<T>& m) IMATH_NOEXCEPT
  3948. {
  3949. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0]);
  3950. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1]);
  3951. return Vec2<S> (x, y);
  3952. }
  3953. template <class S, class T>
  3954. IMATH_HOSTDEVICE inline const Vec2<S>&
  3955. operator*= (Vec2<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT
  3956. {
  3957. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + m.x[2][0]);
  3958. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + m.x[2][1]);
  3959. S w = S (v.x * m.x[0][2] + v.y * m.x[1][2] + m.x[2][2]);
  3960. v.x = x / w;
  3961. v.y = y / w;
  3962. return v;
  3963. }
  3964. template <class S, class T>
  3965. IMATH_HOSTDEVICE inline Vec2<S>
  3966. operator* (const Vec2<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT
  3967. {
  3968. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + m.x[2][0]);
  3969. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + m.x[2][1]);
  3970. S w = S (v.x * m.x[0][2] + v.y * m.x[1][2] + m.x[2][2]);
  3971. return Vec2<S> (x / w, y / w);
  3972. }
  3973. template <class S, class T>
  3974. IMATH_HOSTDEVICE inline const Vec3<S>&
  3975. operator*= (Vec3<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT
  3976. {
  3977. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + v.z * m.x[2][0]);
  3978. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + v.z * m.x[2][1]);
  3979. S z = S (v.x * m.x[0][2] + v.y * m.x[1][2] + v.z * m.x[2][2]);
  3980. v.x = x;
  3981. v.y = y;
  3982. v.z = z;
  3983. return v;
  3984. }
  3985. template <class S, class T>
  3986. IMATH_HOSTDEVICE inline Vec3<S>
  3987. operator* (const Vec3<S>& v, const Matrix33<T>& m) IMATH_NOEXCEPT
  3988. {
  3989. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + v.z * m.x[2][0]);
  3990. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + v.z * m.x[2][1]);
  3991. S z = S (v.x * m.x[0][2] + v.y * m.x[1][2] + v.z * m.x[2][2]);
  3992. return Vec3<S> (x, y, z);
  3993. }
  3994. template <class S, class T>
  3995. IMATH_HOSTDEVICE inline const Vec3<S>&
  3996. operator*= (Vec3<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT
  3997. {
  3998. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + v.z * m.x[2][0] + m.x[3][0]);
  3999. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + v.z * m.x[2][1] + m.x[3][1]);
  4000. S z = S (v.x * m.x[0][2] + v.y * m.x[1][2] + v.z * m.x[2][2] + m.x[3][2]);
  4001. S w = S (v.x * m.x[0][3] + v.y * m.x[1][3] + v.z * m.x[2][3] + m.x[3][3]);
  4002. v.x = x / w;
  4003. v.y = y / w;
  4004. v.z = z / w;
  4005. return v;
  4006. }
  4007. template <class S, class T>
  4008. IMATH_HOSTDEVICE inline Vec3<S>
  4009. IMATH_HOSTDEVICE operator* (const Vec3<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT
  4010. {
  4011. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + v.z * m.x[2][0] + m.x[3][0]);
  4012. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + v.z * m.x[2][1] + m.x[3][1]);
  4013. S z = S (v.x * m.x[0][2] + v.y * m.x[1][2] + v.z * m.x[2][2] + m.x[3][2]);
  4014. S w = S (v.x * m.x[0][3] + v.y * m.x[1][3] + v.z * m.x[2][3] + m.x[3][3]);
  4015. return Vec3<S> (x / w, y / w, z / w);
  4016. }
  4017. template <class S, class T>
  4018. IMATH_HOSTDEVICE inline const Vec4<S>&
  4019. IMATH_HOSTDEVICE operator*= (Vec4<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT
  4020. {
  4021. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + v.z * m.x[2][0] + v.w * m.x[3][0]);
  4022. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + v.z * m.x[2][1] + v.w * m.x[3][1]);
  4023. S z = S (v.x * m.x[0][2] + v.y * m.x[1][2] + v.z * m.x[2][2] + v.w * m.x[3][2]);
  4024. S w = S (v.x * m.x[0][3] + v.y * m.x[1][3] + v.z * m.x[2][3] + v.w * m.x[3][3]);
  4025. v.x = x;
  4026. v.y = y;
  4027. v.z = z;
  4028. v.w = w;
  4029. return v;
  4030. }
  4031. template <class S, class T>
  4032. IMATH_HOSTDEVICE inline Vec4<S>
  4033. IMATH_HOSTDEVICE operator* (const Vec4<S>& v, const Matrix44<T>& m) IMATH_NOEXCEPT
  4034. {
  4035. S x = S (v.x * m.x[0][0] + v.y * m.x[1][0] + v.z * m.x[2][0] + v.w * m.x[3][0]);
  4036. S y = S (v.x * m.x[0][1] + v.y * m.x[1][1] + v.z * m.x[2][1] + v.w * m.x[3][1]);
  4037. S z = S (v.x * m.x[0][2] + v.y * m.x[1][2] + v.z * m.x[2][2] + v.w * m.x[3][2]);
  4038. S w = S (v.x * m.x[0][3] + v.y * m.x[1][3] + v.z * m.x[2][3] + v.w * m.x[3][3]);
  4039. return Vec4<S> (x, y, z, w);
  4040. }
  4041. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  4042. #endif // INCLUDED_IMATHMATRIX_H