vmath.hpp 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101
  1. /*
  2. Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms,
  5. with or without modification, are permitted provided that the
  6. following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the Sony Computer Entertainment Inc nor the names
  13. of its contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <math.h>
  29. #ifdef _VECTORMATH_DEBUG
  30. #include <stdio.h>
  31. #endif
  32. namespace Vectormath {
  33. namespace Aos {
  34. //-----------------------------------------------------------------------------
  35. // Forward Declarations
  36. //
  37. class Vector3;
  38. class Vector4;
  39. class Point3;
  40. class Quat;
  41. class Matrix3;
  42. class Matrix4;
  43. class Transform3;
  44. // A 3-D vector in array-of-structures format
  45. //
  46. class Vector3
  47. {
  48. float mX;
  49. float mY;
  50. float mZ;
  51. #ifdef __GNUC__
  52. float d;
  53. #endif
  54. public:
  55. // Default constructor; does no initialization
  56. //
  57. inline Vector3( ) { };
  58. // Copy a 3-D vector
  59. //
  60. inline Vector3( const Vector3 & vec );
  61. // Construct a 3-D vector from x, y, and z elements
  62. //
  63. inline Vector3( float x, float y, float z );
  64. // Copy elements from a 3-D point into a 3-D vector
  65. //
  66. explicit inline Vector3( const Point3 & pnt );
  67. // Set all elements of a 3-D vector to the same scalar value
  68. //
  69. explicit inline Vector3( float scalar );
  70. // Assign one 3-D vector to another
  71. //
  72. inline Vector3 & operator =( const Vector3 & vec );
  73. // Set the x element of a 3-D vector
  74. //
  75. inline Vector3 & setX( float x );
  76. // Set the y element of a 3-D vector
  77. //
  78. inline Vector3 & setY( float y );
  79. // Set the z element of a 3-D vector
  80. //
  81. inline Vector3 & setZ( float z );
  82. // Get the x element of a 3-D vector
  83. //
  84. inline float getX( ) const;
  85. // Get the y element of a 3-D vector
  86. //
  87. inline float getY( ) const;
  88. // Get the z element of a 3-D vector
  89. //
  90. inline float getZ( ) const;
  91. // Set an x, y, or z element of a 3-D vector by index
  92. //
  93. inline Vector3 & setElem( int idx, float value );
  94. // Get an x, y, or z element of a 3-D vector by index
  95. //
  96. inline float getElem( int idx ) const;
  97. // Subscripting operator to set or get an element
  98. //
  99. inline float & operator []( int idx );
  100. // Subscripting operator to get an element
  101. //
  102. inline float operator []( int idx ) const;
  103. // Add two 3-D vectors
  104. //
  105. inline const Vector3 operator +( const Vector3 & vec ) const;
  106. // Subtract a 3-D vector from another 3-D vector
  107. //
  108. inline const Vector3 operator -( const Vector3 & vec ) const;
  109. // Add a 3-D vector to a 3-D point
  110. //
  111. inline const Point3 operator +( const Point3 & pnt ) const;
  112. // Multiply a 3-D vector by a scalar
  113. //
  114. inline const Vector3 operator *( float scalar ) const;
  115. // Divide a 3-D vector by a scalar
  116. //
  117. inline const Vector3 operator /( float scalar ) const;
  118. // Perform compound assignment and addition with a 3-D vector
  119. //
  120. inline Vector3 & operator +=( const Vector3 & vec );
  121. // Perform compound assignment and subtraction by a 3-D vector
  122. //
  123. inline Vector3 & operator -=( const Vector3 & vec );
  124. // Perform compound assignment and multiplication by a scalar
  125. //
  126. inline Vector3 & operator *=( float scalar );
  127. // Perform compound assignment and division by a scalar
  128. //
  129. inline Vector3 & operator /=( float scalar );
  130. // Negate all elements of a 3-D vector
  131. //
  132. inline const Vector3 operator -( ) const;
  133. // Construct x axis
  134. //
  135. static inline const Vector3 xAxis( );
  136. // Construct y axis
  137. //
  138. static inline const Vector3 yAxis( );
  139. // Construct z axis
  140. //
  141. static inline const Vector3 zAxis( );
  142. }
  143. #ifdef __GNUC__
  144. __attribute__ ((aligned(16)))
  145. #endif
  146. ;
  147. // Multiply a 3-D vector by a scalar
  148. //
  149. inline const Vector3 operator *( float scalar, const Vector3 & vec );
  150. // Multiply two 3-D vectors per element
  151. //
  152. inline const Vector3 mulPerElem( const Vector3 & vec0, const Vector3 & vec1 );
  153. // Divide two 3-D vectors per element
  154. // NOTE:
  155. // Floating-point behavior matches standard library function divf4.
  156. //
  157. inline const Vector3 divPerElem( const Vector3 & vec0, const Vector3 & vec1 );
  158. // Compute the reciprocal of a 3-D vector per element
  159. // NOTE:
  160. // Floating-point behavior matches standard library function recipf4.
  161. //
  162. inline const Vector3 recipPerElem( const Vector3 & vec );
  163. // Compute the square root of a 3-D vector per element
  164. // NOTE:
  165. // Floating-point behavior matches standard library function sqrtf4.
  166. //
  167. inline const Vector3 sqrtPerElem( const Vector3 & vec );
  168. // Compute the reciprocal square root of a 3-D vector per element
  169. // NOTE:
  170. // Floating-point behavior matches standard library function rsqrtf4.
  171. //
  172. inline const Vector3 rsqrtPerElem( const Vector3 & vec );
  173. // Compute the absolute value of a 3-D vector per element
  174. //
  175. inline const Vector3 absPerElem( const Vector3 & vec );
  176. // Copy sign from one 3-D vector to another, per element
  177. //
  178. inline const Vector3 copySignPerElem( const Vector3 & vec0, const Vector3 & vec1 );
  179. // Maximum of two 3-D vectors per element
  180. //
  181. inline const Vector3 maxPerElem( const Vector3 & vec0, const Vector3 & vec1 );
  182. // Minimum of two 3-D vectors per element
  183. //
  184. inline const Vector3 minPerElem( const Vector3 & vec0, const Vector3 & vec1 );
  185. // Maximum element of a 3-D vector
  186. //
  187. inline float maxElem( const Vector3 & vec );
  188. // Minimum element of a 3-D vector
  189. //
  190. inline float minElem( const Vector3 & vec );
  191. // Compute the sum of all elements of a 3-D vector
  192. //
  193. inline float sum( const Vector3 & vec );
  194. // Compute the dot product of two 3-D vectors
  195. //
  196. inline float dot( const Vector3 & vec0, const Vector3 & vec1 );
  197. // Compute the square of the length of a 3-D vector
  198. //
  199. inline float lengthSqr( const Vector3 & vec );
  200. // Compute the length of a 3-D vector
  201. //
  202. inline float length( const Vector3 & vec );
  203. // Normalize a 3-D vector
  204. // NOTE:
  205. // The result is unpredictable when all elements of vec are at or near zero.
  206. //
  207. inline const Vector3 normalize( const Vector3 & vec );
  208. // Compute cross product of two 3-D vectors
  209. //
  210. inline const Vector3 cross( const Vector3 & vec0, const Vector3 & vec1 );
  211. // Outer product of two 3-D vectors
  212. //
  213. inline const Matrix3 outer( const Vector3 & vec0, const Vector3 & vec1 );
  214. // Pre-multiply a row vector by a 3x3 matrix
  215. //
  216. inline const Vector3 rowMul( const Vector3 & vec, const Matrix3 & mat );
  217. // Cross-product matrix of a 3-D vector
  218. //
  219. inline const Matrix3 crossMatrix( const Vector3 & vec );
  220. // Create cross-product matrix and multiply
  221. // NOTE:
  222. // Faster than separately creating a cross-product matrix and multiplying.
  223. //
  224. inline const Matrix3 crossMatrixMul( const Vector3 & vec, const Matrix3 & mat );
  225. // Linear interpolation between two 3-D vectors
  226. // NOTE:
  227. // Does not clamp t between 0 and 1.
  228. //
  229. inline const Vector3 lerp( float t, const Vector3 & vec0, const Vector3 & vec1 );
  230. // Spherical linear interpolation between two 3-D vectors
  231. // NOTE:
  232. // The result is unpredictable if the vectors point in opposite directions.
  233. // Does not clamp t between 0 and 1.
  234. //
  235. inline const Vector3 slerp( float t, const Vector3 & unitVec0, const Vector3 & unitVec1 );
  236. // Conditionally select between two 3-D vectors
  237. //
  238. inline const Vector3 select( const Vector3 & vec0, const Vector3 & vec1, bool select1 );
  239. #ifdef _VECTORMATH_DEBUG
  240. // Print a 3-D vector
  241. // NOTE:
  242. // Function is only defined when _VECTORMATH_DEBUG is defined.
  243. //
  244. inline void print( const Vector3 & vec );
  245. // Print a 3-D vector and an associated string identifier
  246. // NOTE:
  247. // Function is only defined when _VECTORMATH_DEBUG is defined.
  248. //
  249. inline void print( const Vector3 & vec, const char * name );
  250. #endif
  251. // A 4-D vector in array-of-structures format
  252. //
  253. class Vector4
  254. {
  255. float mX;
  256. float mY;
  257. float mZ;
  258. float mW;
  259. public:
  260. // Default constructor; does no initialization
  261. //
  262. inline Vector4( ) { };
  263. // Copy a 4-D vector
  264. //
  265. inline Vector4( const Vector4 & vec );
  266. // Construct a 4-D vector from x, y, z, and w elements
  267. //
  268. inline Vector4( float x, float y, float z, float w );
  269. // Construct a 4-D vector from a 3-D vector and a scalar
  270. //
  271. inline Vector4( const Vector3 & xyz, float w );
  272. // Copy x, y, and z from a 3-D vector into a 4-D vector, and set w to 0
  273. //
  274. explicit inline Vector4( const Vector3 & vec );
  275. // Copy x, y, and z from a 3-D point into a 4-D vector, and set w to 1
  276. //
  277. explicit inline Vector4( const Point3 & pnt );
  278. // Copy elements from a quaternion into a 4-D vector
  279. //
  280. explicit inline Vector4( const Quat & quat );
  281. // Set all elements of a 4-D vector to the same scalar value
  282. //
  283. explicit inline Vector4( float scalar );
  284. // Assign one 4-D vector to another
  285. //
  286. inline Vector4 & operator =( const Vector4 & vec );
  287. // Set the x, y, and z elements of a 4-D vector
  288. // NOTE:
  289. // This function does not change the w element.
  290. //
  291. inline Vector4 & setXYZ( const Vector3 & vec );
  292. // Get the x, y, and z elements of a 4-D vector
  293. //
  294. inline const Vector3 getXYZ( ) const;
  295. // Set the x element of a 4-D vector
  296. //
  297. inline Vector4 & setX( float x );
  298. // Set the y element of a 4-D vector
  299. //
  300. inline Vector4 & setY( float y );
  301. // Set the z element of a 4-D vector
  302. //
  303. inline Vector4 & setZ( float z );
  304. // Set the w element of a 4-D vector
  305. //
  306. inline Vector4 & setW( float w );
  307. // Get the x element of a 4-D vector
  308. //
  309. inline float getX( ) const;
  310. // Get the y element of a 4-D vector
  311. //
  312. inline float getY( ) const;
  313. // Get the z element of a 4-D vector
  314. //
  315. inline float getZ( ) const;
  316. // Get the w element of a 4-D vector
  317. //
  318. inline float getW( ) const;
  319. // Set an x, y, z, or w element of a 4-D vector by index
  320. //
  321. inline Vector4 & setElem( int idx, float value );
  322. // Get an x, y, z, or w element of a 4-D vector by index
  323. //
  324. inline float getElem( int idx ) const;
  325. // Subscripting operator to set or get an element
  326. //
  327. inline float & operator []( int idx );
  328. // Subscripting operator to get an element
  329. //
  330. inline float operator []( int idx ) const;
  331. // Add two 4-D vectors
  332. //
  333. inline const Vector4 operator +( const Vector4 & vec ) const;
  334. // Subtract a 4-D vector from another 4-D vector
  335. //
  336. inline const Vector4 operator -( const Vector4 & vec ) const;
  337. // Multiply a 4-D vector by a scalar
  338. //
  339. inline const Vector4 operator *( float scalar ) const;
  340. // Divide a 4-D vector by a scalar
  341. //
  342. inline const Vector4 operator /( float scalar ) const;
  343. // Perform compound assignment and addition with a 4-D vector
  344. //
  345. inline Vector4 & operator +=( const Vector4 & vec );
  346. // Perform compound assignment and subtraction by a 4-D vector
  347. //
  348. inline Vector4 & operator -=( const Vector4 & vec );
  349. // Perform compound assignment and multiplication by a scalar
  350. //
  351. inline Vector4 & operator *=( float scalar );
  352. // Perform compound assignment and division by a scalar
  353. //
  354. inline Vector4 & operator /=( float scalar );
  355. // Negate all elements of a 4-D vector
  356. //
  357. inline const Vector4 operator -( ) const;
  358. // Construct x axis
  359. //
  360. static inline const Vector4 xAxis( );
  361. // Construct y axis
  362. //
  363. static inline const Vector4 yAxis( );
  364. // Construct z axis
  365. //
  366. static inline const Vector4 zAxis( );
  367. // Construct w axis
  368. //
  369. static inline const Vector4 wAxis( );
  370. }
  371. #ifdef __GNUC__
  372. __attribute__ ((aligned(16)))
  373. #endif
  374. ;
  375. // Multiply a 4-D vector by a scalar
  376. //
  377. inline const Vector4 operator *( float scalar, const Vector4 & vec );
  378. // Multiply two 4-D vectors per element
  379. //
  380. inline const Vector4 mulPerElem( const Vector4 & vec0, const Vector4 & vec1 );
  381. // Divide two 4-D vectors per element
  382. // NOTE:
  383. // Floating-point behavior matches standard library function divf4.
  384. //
  385. inline const Vector4 divPerElem( const Vector4 & vec0, const Vector4 & vec1 );
  386. // Compute the reciprocal of a 4-D vector per element
  387. // NOTE:
  388. // Floating-point behavior matches standard library function recipf4.
  389. //
  390. inline const Vector4 recipPerElem( const Vector4 & vec );
  391. // Compute the square root of a 4-D vector per element
  392. // NOTE:
  393. // Floating-point behavior matches standard library function sqrtf4.
  394. //
  395. inline const Vector4 sqrtPerElem( const Vector4 & vec );
  396. // Compute the reciprocal square root of a 4-D vector per element
  397. // NOTE:
  398. // Floating-point behavior matches standard library function rsqrtf4.
  399. //
  400. inline const Vector4 rsqrtPerElem( const Vector4 & vec );
  401. // Compute the absolute value of a 4-D vector per element
  402. //
  403. inline const Vector4 absPerElem( const Vector4 & vec );
  404. // Copy sign from one 4-D vector to another, per element
  405. //
  406. inline const Vector4 copySignPerElem( const Vector4 & vec0, const Vector4 & vec1 );
  407. // Maximum of two 4-D vectors per element
  408. //
  409. inline const Vector4 maxPerElem( const Vector4 & vec0, const Vector4 & vec1 );
  410. // Minimum of two 4-D vectors per element
  411. //
  412. inline const Vector4 minPerElem( const Vector4 & vec0, const Vector4 & vec1 );
  413. // Maximum element of a 4-D vector
  414. //
  415. inline float maxElem( const Vector4 & vec );
  416. // Minimum element of a 4-D vector
  417. //
  418. inline float minElem( const Vector4 & vec );
  419. // Compute the sum of all elements of a 4-D vector
  420. //
  421. inline float sum( const Vector4 & vec );
  422. // Compute the dot product of two 4-D vectors
  423. //
  424. inline float dot( const Vector4 & vec0, const Vector4 & vec1 );
  425. // Compute the square of the length of a 4-D vector
  426. //
  427. inline float lengthSqr( const Vector4 & vec );
  428. // Compute the length of a 4-D vector
  429. //
  430. inline float length( const Vector4 & vec );
  431. // Normalize a 4-D vector
  432. // NOTE:
  433. // The result is unpredictable when all elements of vec are at or near zero.
  434. //
  435. inline const Vector4 normalize( const Vector4 & vec );
  436. // Outer product of two 4-D vectors
  437. //
  438. inline const Matrix4 outer( const Vector4 & vec0, const Vector4 & vec1 );
  439. // Linear interpolation between two 4-D vectors
  440. // NOTE:
  441. // Does not clamp t between 0 and 1.
  442. //
  443. inline const Vector4 lerp( float t, const Vector4 & vec0, const Vector4 & vec1 );
  444. // Spherical linear interpolation between two 4-D vectors
  445. // NOTE:
  446. // The result is unpredictable if the vectors point in opposite directions.
  447. // Does not clamp t between 0 and 1.
  448. //
  449. inline const Vector4 slerp( float t, const Vector4 & unitVec0, const Vector4 & unitVec1 );
  450. // Conditionally select between two 4-D vectors
  451. //
  452. inline const Vector4 select( const Vector4 & vec0, const Vector4 & vec1, bool select1 );
  453. #ifdef _VECTORMATH_DEBUG
  454. // Print a 4-D vector
  455. // NOTE:
  456. // Function is only defined when _VECTORMATH_DEBUG is defined.
  457. //
  458. inline void print( const Vector4 & vec );
  459. // Print a 4-D vector and an associated string identifier
  460. // NOTE:
  461. // Function is only defined when _VECTORMATH_DEBUG is defined.
  462. //
  463. inline void print( const Vector4 & vec, const char * name );
  464. #endif
  465. // A 3-D point in array-of-structures format
  466. //
  467. class Point3
  468. {
  469. float mX;
  470. float mY;
  471. float mZ;
  472. #ifndef __GNUC__
  473. float d;
  474. #endif
  475. public:
  476. // Default constructor; does no initialization
  477. //
  478. inline Point3( ) { };
  479. // Copy a 3-D point
  480. //
  481. inline Point3( const Point3 & pnt );
  482. // Construct a 3-D point from x, y, and z elements
  483. //
  484. inline Point3( float x, float y, float z );
  485. // Copy elements from a 3-D vector into a 3-D point
  486. //
  487. explicit inline Point3( const Vector3 & vec );
  488. // Set all elements of a 3-D point to the same scalar value
  489. //
  490. explicit inline Point3( float scalar );
  491. // Assign one 3-D point to another
  492. //
  493. inline Point3 & operator =( const Point3 & pnt );
  494. // Set the x element of a 3-D point
  495. //
  496. inline Point3 & setX( float x );
  497. // Set the y element of a 3-D point
  498. //
  499. inline Point3 & setY( float y );
  500. // Set the z element of a 3-D point
  501. //
  502. inline Point3 & setZ( float z );
  503. // Get the x element of a 3-D point
  504. //
  505. inline float getX( ) const;
  506. // Get the y element of a 3-D point
  507. //
  508. inline float getY( ) const;
  509. // Get the z element of a 3-D point
  510. //
  511. inline float getZ( ) const;
  512. // Set an x, y, or z element of a 3-D point by index
  513. //
  514. inline Point3 & setElem( int idx, float value );
  515. // Get an x, y, or z element of a 3-D point by index
  516. //
  517. inline float getElem( int idx ) const;
  518. // Subscripting operator to set or get an element
  519. //
  520. inline float & operator []( int idx );
  521. // Subscripting operator to get an element
  522. //
  523. inline float operator []( int idx ) const;
  524. // Subtract a 3-D point from another 3-D point
  525. //
  526. inline const Vector3 operator -( const Point3 & pnt ) const;
  527. // Add a 3-D point to a 3-D vector
  528. //
  529. inline const Point3 operator +( const Vector3 & vec ) const;
  530. // Subtract a 3-D vector from a 3-D point
  531. //
  532. inline const Point3 operator -( const Vector3 & vec ) const;
  533. // Perform compound assignment and addition with a 3-D vector
  534. //
  535. inline Point3 & operator +=( const Vector3 & vec );
  536. // Perform compound assignment and subtraction by a 3-D vector
  537. //
  538. inline Point3 & operator -=( const Vector3 & vec );
  539. }
  540. #ifdef __GNUC__
  541. __attribute__ ((aligned(16)))
  542. #endif
  543. ;
  544. // Multiply two 3-D points per element
  545. //
  546. inline const Point3 mulPerElem( const Point3 & pnt0, const Point3 & pnt1 );
  547. // Divide two 3-D points per element
  548. // NOTE:
  549. // Floating-point behavior matches standard library function divf4.
  550. //
  551. inline const Point3 divPerElem( const Point3 & pnt0, const Point3 & pnt1 );
  552. // Compute the reciprocal of a 3-D point per element
  553. // NOTE:
  554. // Floating-point behavior matches standard library function recipf4.
  555. //
  556. inline const Point3 recipPerElem( const Point3 & pnt );
  557. // Compute the square root of a 3-D point per element
  558. // NOTE:
  559. // Floating-point behavior matches standard library function sqrtf4.
  560. //
  561. inline const Point3 sqrtPerElem( const Point3 & pnt );
  562. // Compute the reciprocal square root of a 3-D point per element
  563. // NOTE:
  564. // Floating-point behavior matches standard library function rsqrtf4.
  565. //
  566. inline const Point3 rsqrtPerElem( const Point3 & pnt );
  567. // Compute the absolute value of a 3-D point per element
  568. //
  569. inline const Point3 absPerElem( const Point3 & pnt );
  570. // Copy sign from one 3-D point to another, per element
  571. //
  572. inline const Point3 copySignPerElem( const Point3 & pnt0, const Point3 & pnt1 );
  573. // Maximum of two 3-D points per element
  574. //
  575. inline const Point3 maxPerElem( const Point3 & pnt0, const Point3 & pnt1 );
  576. // Minimum of two 3-D points per element
  577. //
  578. inline const Point3 minPerElem( const Point3 & pnt0, const Point3 & pnt1 );
  579. // Maximum element of a 3-D point
  580. //
  581. inline float maxElem( const Point3 & pnt );
  582. // Minimum element of a 3-D point
  583. //
  584. inline float minElem( const Point3 & pnt );
  585. // Compute the sum of all elements of a 3-D point
  586. //
  587. inline float sum( const Point3 & pnt );
  588. // Apply uniform scale to a 3-D point
  589. //
  590. inline const Point3 scale( const Point3 & pnt, float scaleVal );
  591. // Apply non-uniform scale to a 3-D point
  592. //
  593. inline const Point3 scale( const Point3 & pnt, const Vector3 & scaleVec );
  594. // Scalar projection of a 3-D point on a unit-length 3-D vector
  595. //
  596. inline float projection( const Point3 & pnt, const Vector3 & unitVec );
  597. // Compute the square of the distance of a 3-D point from the coordinate-system origin
  598. //
  599. inline float distSqrFromOrigin( const Point3 & pnt );
  600. // Compute the distance of a 3-D point from the coordinate-system origin
  601. //
  602. inline float distFromOrigin( const Point3 & pnt );
  603. // Compute the square of the distance between two 3-D points
  604. //
  605. inline float distSqr( const Point3 & pnt0, const Point3 & pnt1 );
  606. // Compute the distance between two 3-D points
  607. //
  608. inline float dist( const Point3 & pnt0, const Point3 & pnt1 );
  609. // Linear interpolation between two 3-D points
  610. // NOTE:
  611. // Does not clamp t between 0 and 1.
  612. //
  613. inline const Point3 lerp( float t, const Point3 & pnt0, const Point3 & pnt1 );
  614. // Conditionally select between two 3-D points
  615. //
  616. inline const Point3 select( const Point3 & pnt0, const Point3 & pnt1, bool select1 );
  617. #ifdef _VECTORMATH_DEBUG
  618. // Print a 3-D point
  619. // NOTE:
  620. // Function is only defined when _VECTORMATH_DEBUG is defined.
  621. //
  622. inline void print( const Point3 & pnt );
  623. // Print a 3-D point and an associated string identifier
  624. // NOTE:
  625. // Function is only defined when _VECTORMATH_DEBUG is defined.
  626. //
  627. inline void print( const Point3 & pnt, const char * name );
  628. #endif
  629. // A quaternion in array-of-structures format
  630. //
  631. class Quat
  632. {
  633. float mX;
  634. float mY;
  635. float mZ;
  636. float mW;
  637. public:
  638. // Default constructor; does no initialization
  639. //
  640. inline Quat( ) { };
  641. // Copy a quaternion
  642. //
  643. inline Quat( const Quat & quat );
  644. // Construct a quaternion from x, y, z, and w elements
  645. //
  646. inline Quat( float x, float y, float z, float w );
  647. // Construct a quaternion from a 3-D vector and a scalar
  648. //
  649. inline Quat( const Vector3 & xyz, float w );
  650. // Copy elements from a 4-D vector into a quaternion
  651. //
  652. explicit inline Quat( const Vector4 & vec );
  653. // Convert a rotation matrix to a unit-length quaternion
  654. //
  655. explicit inline Quat( const Matrix3 & rotMat );
  656. // Set all elements of a quaternion to the same scalar value
  657. //
  658. explicit inline Quat( float scalar );
  659. // Assign one quaternion to another
  660. //
  661. inline Quat & operator =( const Quat & quat );
  662. // Set the x, y, and z elements of a quaternion
  663. // NOTE:
  664. // This function does not change the w element.
  665. //
  666. inline Quat & setXYZ( const Vector3 & vec );
  667. // Get the x, y, and z elements of a quaternion
  668. //
  669. inline const Vector3 getXYZ( ) const;
  670. // Set the x element of a quaternion
  671. //
  672. inline Quat & setX( float x );
  673. // Set the y element of a quaternion
  674. //
  675. inline Quat & setY( float y );
  676. // Set the z element of a quaternion
  677. //
  678. inline Quat & setZ( float z );
  679. // Set the w element of a quaternion
  680. //
  681. inline Quat & setW( float w );
  682. // Get the x element of a quaternion
  683. //
  684. inline float getX( ) const;
  685. // Get the y element of a quaternion
  686. //
  687. inline float getY( ) const;
  688. // Get the z element of a quaternion
  689. //
  690. inline float getZ( ) const;
  691. // Get the w element of a quaternion
  692. //
  693. inline float getW( ) const;
  694. // Set an x, y, z, or w element of a quaternion by index
  695. //
  696. inline Quat & setElem( int idx, float value );
  697. // Get an x, y, z, or w element of a quaternion by index
  698. //
  699. inline float getElem( int idx ) const;
  700. // Subscripting operator to set or get an element
  701. //
  702. inline float & operator []( int idx );
  703. // Subscripting operator to get an element
  704. //
  705. inline float operator []( int idx ) const;
  706. // Add two quaternions
  707. //
  708. inline const Quat operator +( const Quat & quat ) const;
  709. // Subtract a quaternion from another quaternion
  710. //
  711. inline const Quat operator -( const Quat & quat ) const;
  712. // Multiply two quaternions
  713. //
  714. inline const Quat operator *( const Quat & quat ) const;
  715. // Multiply a quaternion by a scalar
  716. //
  717. inline const Quat operator *( float scalar ) const;
  718. // Divide a quaternion by a scalar
  719. //
  720. inline const Quat operator /( float scalar ) const;
  721. // Perform compound assignment and addition with a quaternion
  722. //
  723. inline Quat & operator +=( const Quat & quat );
  724. // Perform compound assignment and subtraction by a quaternion
  725. //
  726. inline Quat & operator -=( const Quat & quat );
  727. // Perform compound assignment and multiplication by a quaternion
  728. //
  729. inline Quat & operator *=( const Quat & quat );
  730. // Perform compound assignment and multiplication by a scalar
  731. //
  732. inline Quat & operator *=( float scalar );
  733. // Perform compound assignment and division by a scalar
  734. //
  735. inline Quat & operator /=( float scalar );
  736. // Negate all elements of a quaternion
  737. //
  738. inline const Quat operator -( ) const;
  739. // Construct an identity quaternion
  740. //
  741. static inline const Quat identity( );
  742. // Construct a quaternion to rotate between two unit-length 3-D vectors
  743. // NOTE:
  744. // The result is unpredictable if unitVec0 and unitVec1 point in opposite directions.
  745. //
  746. static inline const Quat rotation( const Vector3 & unitVec0, const Vector3 & unitVec1 );
  747. // Construct a quaternion to rotate around a unit-length 3-D vector
  748. //
  749. static inline const Quat rotation( float radians, const Vector3 & unitVec );
  750. // Construct a quaternion to rotate around the x axis
  751. //
  752. static inline const Quat rotationX( float radians );
  753. // Construct a quaternion to rotate around the y axis
  754. //
  755. static inline const Quat rotationY( float radians );
  756. // Construct a quaternion to rotate around the z axis
  757. //
  758. static inline const Quat rotationZ( float radians );
  759. }
  760. #ifdef __GNUC__
  761. __attribute__ ((aligned(16)))
  762. #endif
  763. ;
  764. // Multiply a quaternion by a scalar
  765. //
  766. inline const Quat operator *( float scalar, const Quat & quat );
  767. // Compute the conjugate of a quaternion
  768. //
  769. inline const Quat conj( const Quat & quat );
  770. // Use a unit-length quaternion to rotate a 3-D vector
  771. //
  772. inline const Vector3 rotate( const Quat & unitQuat, const Vector3 & vec );
  773. // Compute the dot product of two quaternions
  774. //
  775. inline float dot( const Quat & quat0, const Quat & quat1 );
  776. // Compute the norm of a quaternion
  777. //
  778. inline float norm( const Quat & quat );
  779. // Compute the length of a quaternion
  780. //
  781. inline float length( const Quat & quat );
  782. // Normalize a quaternion
  783. // NOTE:
  784. // The result is unpredictable when all elements of quat are at or near zero.
  785. //
  786. inline const Quat normalize( const Quat & quat );
  787. // Linear interpolation between two quaternions
  788. // NOTE:
  789. // Does not clamp t between 0 and 1.
  790. //
  791. inline const Quat lerp( float t, const Quat & quat0, const Quat & quat1 );
  792. // Spherical linear interpolation between two quaternions
  793. // NOTE:
  794. // Interpolates along the shortest path between orientations.
  795. // Does not clamp t between 0 and 1.
  796. //
  797. inline const Quat slerp( float t, const Quat & unitQuat0, const Quat & unitQuat1 );
  798. // Spherical quadrangle interpolation
  799. //
  800. inline const Quat squad( float t, const Quat & unitQuat0, const Quat & unitQuat1, const Quat & unitQuat2, const Quat & unitQuat3 );
  801. // Conditionally select between two quaternions
  802. //
  803. inline const Quat select( const Quat & quat0, const Quat & quat1, bool select1 );
  804. #ifdef _VECTORMATH_DEBUG
  805. // Print a quaternion
  806. // NOTE:
  807. // Function is only defined when _VECTORMATH_DEBUG is defined.
  808. //
  809. inline void print( const Quat & quat );
  810. // Print a quaternion and an associated string identifier
  811. // NOTE:
  812. // Function is only defined when _VECTORMATH_DEBUG is defined.
  813. //
  814. inline void print( const Quat & quat, const char * name );
  815. #endif
  816. // A 3x3 matrix in array-of-structures format
  817. //
  818. class Matrix3
  819. {
  820. Vector3 mCol0;
  821. Vector3 mCol1;
  822. Vector3 mCol2;
  823. public:
  824. // Default constructor; does no initialization
  825. //
  826. inline Matrix3( ) { };
  827. // Copy a 3x3 matrix
  828. //
  829. inline Matrix3( const Matrix3 & mat );
  830. // Construct a 3x3 matrix containing the specified columns
  831. //
  832. inline Matrix3( const Vector3 & col0, const Vector3 & col1, const Vector3 & col2 );
  833. // Construct a 3x3 rotation matrix from a unit-length quaternion
  834. //
  835. explicit inline Matrix3( const Quat & unitQuat );
  836. // Set all elements of a 3x3 matrix to the same scalar value
  837. //
  838. explicit inline Matrix3( float scalar );
  839. // Assign one 3x3 matrix to another
  840. //
  841. inline Matrix3 & operator =( const Matrix3 & mat );
  842. // Set column 0 of a 3x3 matrix
  843. //
  844. inline Matrix3 & setCol0( const Vector3 & col0 );
  845. // Set column 1 of a 3x3 matrix
  846. //
  847. inline Matrix3 & setCol1( const Vector3 & col1 );
  848. // Set column 2 of a 3x3 matrix
  849. //
  850. inline Matrix3 & setCol2( const Vector3 & col2 );
  851. // Get column 0 of a 3x3 matrix
  852. //
  853. inline const Vector3 getCol0( ) const;
  854. // Get column 1 of a 3x3 matrix
  855. //
  856. inline const Vector3 getCol1( ) const;
  857. // Get column 2 of a 3x3 matrix
  858. //
  859. inline const Vector3 getCol2( ) const;
  860. // Set the column of a 3x3 matrix referred to by the specified index
  861. //
  862. inline Matrix3 & setCol( int col, const Vector3 & vec );
  863. // Set the row of a 3x3 matrix referred to by the specified index
  864. //
  865. inline Matrix3 & setRow( int row, const Vector3 & vec );
  866. // Get the column of a 3x3 matrix referred to by the specified index
  867. //
  868. inline const Vector3 getCol( int col ) const;
  869. // Get the row of a 3x3 matrix referred to by the specified index
  870. //
  871. inline const Vector3 getRow( int row ) const;
  872. // Subscripting operator to set or get a column
  873. //
  874. inline Vector3 & operator []( int col );
  875. // Subscripting operator to get a column
  876. //
  877. inline const Vector3 operator []( int col ) const;
  878. // Set the element of a 3x3 matrix referred to by column and row indices
  879. //
  880. inline Matrix3 & setElem( int col, int row, float val );
  881. // Get the element of a 3x3 matrix referred to by column and row indices
  882. //
  883. inline float getElem( int col, int row ) const;
  884. // Add two 3x3 matrices
  885. //
  886. inline const Matrix3 operator +( const Matrix3 & mat ) const;
  887. // Subtract a 3x3 matrix from another 3x3 matrix
  888. //
  889. inline const Matrix3 operator -( const Matrix3 & mat ) const;
  890. // Negate all elements of a 3x3 matrix
  891. //
  892. inline const Matrix3 operator -( ) const;
  893. // Multiply a 3x3 matrix by a scalar
  894. //
  895. inline const Matrix3 operator *( float scalar ) const;
  896. // Multiply a 3x3 matrix by a 3-D vector
  897. //
  898. inline const Vector3 operator *( const Vector3 & vec ) const;
  899. // Multiply two 3x3 matrices
  900. //
  901. inline const Matrix3 operator *( const Matrix3 & mat ) const;
  902. // Perform compound assignment and addition with a 3x3 matrix
  903. //
  904. inline Matrix3 & operator +=( const Matrix3 & mat );
  905. // Perform compound assignment and subtraction by a 3x3 matrix
  906. //
  907. inline Matrix3 & operator -=( const Matrix3 & mat );
  908. // Perform compound assignment and multiplication by a scalar
  909. //
  910. inline Matrix3 & operator *=( float scalar );
  911. // Perform compound assignment and multiplication by a 3x3 matrix
  912. //
  913. inline Matrix3 & operator *=( const Matrix3 & mat );
  914. // Construct an identity 3x3 matrix
  915. //
  916. static inline const Matrix3 identity( );
  917. // Construct a 3x3 matrix to rotate around the x axis
  918. //
  919. static inline const Matrix3 rotationX( float radians );
  920. // Construct a 3x3 matrix to rotate around the y axis
  921. //
  922. static inline const Matrix3 rotationY( float radians );
  923. // Construct a 3x3 matrix to rotate around the z axis
  924. //
  925. static inline const Matrix3 rotationZ( float radians );
  926. // Construct a 3x3 matrix to rotate around the x, y, and z axes
  927. //
  928. static inline const Matrix3 rotationZYX( const Vector3 & radiansXYZ );
  929. // Construct a 3x3 matrix to rotate around a unit-length 3-D vector
  930. //
  931. static inline const Matrix3 rotation( float radians, const Vector3 & unitVec );
  932. // Construct a rotation matrix from a unit-length quaternion
  933. //
  934. static inline const Matrix3 rotation( const Quat & unitQuat );
  935. // Construct a 3x3 matrix to perform scaling
  936. //
  937. static inline const Matrix3 scale( const Vector3 & scaleVec );
  938. };
  939. // Multiply a 3x3 matrix by a scalar
  940. //
  941. inline const Matrix3 operator *( float scalar, const Matrix3 & mat );
  942. // Append (post-multiply) a scale transformation to a 3x3 matrix
  943. // NOTE:
  944. // Faster than creating and multiplying a scale transformation matrix.
  945. //
  946. inline const Matrix3 appendScale( const Matrix3 & mat, const Vector3 & scaleVec );
  947. // Prepend (pre-multiply) a scale transformation to a 3x3 matrix
  948. // NOTE:
  949. // Faster than creating and multiplying a scale transformation matrix.
  950. //
  951. inline const Matrix3 prependScale( const Vector3 & scaleVec, const Matrix3 & mat );
  952. // Multiply two 3x3 matrices per element
  953. //
  954. inline const Matrix3 mulPerElem( const Matrix3 & mat0, const Matrix3 & mat1 );
  955. // Compute the absolute value of a 3x3 matrix per element
  956. //
  957. inline const Matrix3 absPerElem( const Matrix3 & mat );
  958. // Transpose of a 3x3 matrix
  959. //
  960. inline const Matrix3 transpose( const Matrix3 & mat );
  961. // Compute the inverse of a 3x3 matrix
  962. // NOTE:
  963. // Result is unpredictable when the determinant of mat is equal to or near 0.
  964. //
  965. inline const Matrix3 inverse( const Matrix3 & mat );
  966. // Determinant of a 3x3 matrix
  967. //
  968. inline float determinant( const Matrix3 & mat );
  969. // Conditionally select between two 3x3 matrices
  970. //
  971. inline const Matrix3 select( const Matrix3 & mat0, const Matrix3 & mat1, bool select1 );
  972. #ifdef _VECTORMATH_DEBUG
  973. // Print a 3x3 matrix
  974. // NOTE:
  975. // Function is only defined when _VECTORMATH_DEBUG is defined.
  976. //
  977. inline void print( const Matrix3 & mat );
  978. // Print a 3x3 matrix and an associated string identifier
  979. // NOTE:
  980. // Function is only defined when _VECTORMATH_DEBUG is defined.
  981. //
  982. inline void print( const Matrix3 & mat, const char * name );
  983. #endif
  984. // A 4x4 matrix in array-of-structures format
  985. //
  986. class Matrix4
  987. {
  988. Vector4 mCol0;
  989. Vector4 mCol1;
  990. Vector4 mCol2;
  991. Vector4 mCol3;
  992. public:
  993. // Default constructor; does no initialization
  994. //
  995. inline Matrix4( ) { };
  996. // Copy a 4x4 matrix
  997. //
  998. inline Matrix4( const Matrix4 & mat );
  999. // Construct a 4x4 matrix containing the specified columns
  1000. //
  1001. inline Matrix4( const Vector4 & col0, const Vector4 & col1, const Vector4 & col2, const Vector4 & col3 );
  1002. // Construct a 4x4 matrix from a 3x4 transformation matrix
  1003. //
  1004. explicit inline Matrix4( const Transform3 & mat );
  1005. // Construct a 4x4 matrix from a 3x3 matrix and a 3-D vector
  1006. //
  1007. inline Matrix4( const Matrix3 & mat, const Vector3 & translateVec );
  1008. // Construct a 4x4 matrix from a unit-length quaternion and a 3-D vector
  1009. //
  1010. inline Matrix4( const Quat & unitQuat, const Vector3 & translateVec );
  1011. // Set all elements of a 4x4 matrix to the same scalar value
  1012. //
  1013. explicit inline Matrix4( float scalar );
  1014. // Assign one 4x4 matrix to another
  1015. //
  1016. inline Matrix4 & operator =( const Matrix4 & mat );
  1017. // Set the upper-left 3x3 submatrix
  1018. // NOTE:
  1019. // This function does not change the bottom row elements.
  1020. //
  1021. inline Matrix4 & setUpper3x3( const Matrix3 & mat3 );
  1022. // Get the upper-left 3x3 submatrix of a 4x4 matrix
  1023. //
  1024. inline const Matrix3 getUpper3x3( ) const;
  1025. // Set translation component
  1026. // NOTE:
  1027. // This function does not change the bottom row elements.
  1028. //
  1029. inline Matrix4 & setTranslation( const Vector3 & translateVec );
  1030. // Get the translation component of a 4x4 matrix
  1031. //
  1032. inline const Vector3 getTranslation( ) const;
  1033. // Set column 0 of a 4x4 matrix
  1034. //
  1035. inline Matrix4 & setCol0( const Vector4 & col0 );
  1036. // Set column 1 of a 4x4 matrix
  1037. //
  1038. inline Matrix4 & setCol1( const Vector4 & col1 );
  1039. // Set column 2 of a 4x4 matrix
  1040. //
  1041. inline Matrix4 & setCol2( const Vector4 & col2 );
  1042. // Set column 3 of a 4x4 matrix
  1043. //
  1044. inline Matrix4 & setCol3( const Vector4 & col3 );
  1045. // Get column 0 of a 4x4 matrix
  1046. //
  1047. inline const Vector4 getCol0( ) const;
  1048. // Get column 1 of a 4x4 matrix
  1049. //
  1050. inline const Vector4 getCol1( ) const;
  1051. // Get column 2 of a 4x4 matrix
  1052. //
  1053. inline const Vector4 getCol2( ) const;
  1054. // Get column 3 of a 4x4 matrix
  1055. //
  1056. inline const Vector4 getCol3( ) const;
  1057. // Set the column of a 4x4 matrix referred to by the specified index
  1058. //
  1059. inline Matrix4 & setCol( int col, const Vector4 & vec );
  1060. // Set the row of a 4x4 matrix referred to by the specified index
  1061. //
  1062. inline Matrix4 & setRow( int row, const Vector4 & vec );
  1063. // Get the column of a 4x4 matrix referred to by the specified index
  1064. //
  1065. inline const Vector4 getCol( int col ) const;
  1066. // Get the row of a 4x4 matrix referred to by the specified index
  1067. //
  1068. inline const Vector4 getRow( int row ) const;
  1069. // Subscripting operator to set or get a column
  1070. //
  1071. inline Vector4 & operator []( int col );
  1072. // Subscripting operator to get a column
  1073. //
  1074. inline const Vector4 operator []( int col ) const;
  1075. // Set the element of a 4x4 matrix referred to by column and row indices
  1076. //
  1077. inline Matrix4 & setElem( int col, int row, float val );
  1078. // Get the element of a 4x4 matrix referred to by column and row indices
  1079. //
  1080. inline float getElem( int col, int row ) const;
  1081. // Add two 4x4 matrices
  1082. //
  1083. inline const Matrix4 operator +( const Matrix4 & mat ) const;
  1084. // Subtract a 4x4 matrix from another 4x4 matrix
  1085. //
  1086. inline const Matrix4 operator -( const Matrix4 & mat ) const;
  1087. // Negate all elements of a 4x4 matrix
  1088. //
  1089. inline const Matrix4 operator -( ) const;
  1090. // Multiply a 4x4 matrix by a scalar
  1091. //
  1092. inline const Matrix4 operator *( float scalar ) const;
  1093. // Multiply a 4x4 matrix by a 4-D vector
  1094. //
  1095. inline const Vector4 operator *( const Vector4 & vec ) const;
  1096. // Multiply a 4x4 matrix by a 3-D vector
  1097. //
  1098. inline const Vector4 operator *( const Vector3 & vec ) const;
  1099. // Multiply a 4x4 matrix by a 3-D point
  1100. //
  1101. inline const Vector4 operator *( const Point3 & pnt ) const;
  1102. // Multiply two 4x4 matrices
  1103. //
  1104. inline const Matrix4 operator *( const Matrix4 & mat ) const;
  1105. // Multiply a 4x4 matrix by a 3x4 transformation matrix
  1106. //
  1107. inline const Matrix4 operator *( const Transform3 & tfrm ) const;
  1108. // Perform compound assignment and addition with a 4x4 matrix
  1109. //
  1110. inline Matrix4 & operator +=( const Matrix4 & mat );
  1111. // Perform compound assignment and subtraction by a 4x4 matrix
  1112. //
  1113. inline Matrix4 & operator -=( const Matrix4 & mat );
  1114. // Perform compound assignment and multiplication by a scalar
  1115. //
  1116. inline Matrix4 & operator *=( float scalar );
  1117. // Perform compound assignment and multiplication by a 4x4 matrix
  1118. //
  1119. inline Matrix4 & operator *=( const Matrix4 & mat );
  1120. // Perform compound assignment and multiplication by a 3x4 transformation matrix
  1121. //
  1122. inline Matrix4 & operator *=( const Transform3 & tfrm );
  1123. // Construct an identity 4x4 matrix
  1124. //
  1125. static inline const Matrix4 identity( );
  1126. // Construct a 4x4 matrix to rotate around the x axis
  1127. //
  1128. static inline const Matrix4 rotationX( float radians );
  1129. // Construct a 4x4 matrix to rotate around the y axis
  1130. //
  1131. static inline const Matrix4 rotationY( float radians );
  1132. // Construct a 4x4 matrix to rotate around the z axis
  1133. //
  1134. static inline const Matrix4 rotationZ( float radians );
  1135. // Construct a 4x4 matrix to rotate around the x, y, and z axes
  1136. //
  1137. static inline const Matrix4 rotationZYX( const Vector3 & radiansXYZ );
  1138. // Construct a 4x4 matrix to rotate around a unit-length 3-D vector
  1139. //
  1140. static inline const Matrix4 rotation( float radians, const Vector3 & unitVec );
  1141. // Construct a rotation matrix from a unit-length quaternion
  1142. //
  1143. static inline const Matrix4 rotation( const Quat & unitQuat );
  1144. // Construct a 4x4 matrix to perform scaling
  1145. //
  1146. static inline const Matrix4 scale( const Vector3 & scaleVec );
  1147. // Construct a 4x4 matrix to perform translation
  1148. //
  1149. static inline const Matrix4 translation( const Vector3 & translateVec );
  1150. // Construct viewing matrix based on eye position, position looked at, and up direction
  1151. //
  1152. static inline const Matrix4 lookAt( const Point3 & eyePos, const Point3 & lookAtPos, const Vector3 & upVec );
  1153. // Construct a perspective projection matrix
  1154. //
  1155. static inline const Matrix4 perspective( float fovyRadians, float aspect, float zNear, float zFar );
  1156. // Construct a perspective projection matrix based on frustum
  1157. //
  1158. static inline const Matrix4 frustum( float left, float right, float bottom, float top, float zNear, float zFar );
  1159. // Construct an orthographic projection matrix
  1160. //
  1161. static inline const Matrix4 orthographic( float left, float right, float bottom, float top, float zNear, float zFar );
  1162. };
  1163. // Multiply a 4x4 matrix by a scalar
  1164. //
  1165. inline const Matrix4 operator *( float scalar, const Matrix4 & mat );
  1166. // Append (post-multiply) a scale transformation to a 4x4 matrix
  1167. // NOTE:
  1168. // Faster than creating and multiplying a scale transformation matrix.
  1169. //
  1170. inline const Matrix4 appendScale( const Matrix4 & mat, const Vector3 & scaleVec );
  1171. // Prepend (pre-multiply) a scale transformation to a 4x4 matrix
  1172. // NOTE:
  1173. // Faster than creating and multiplying a scale transformation matrix.
  1174. //
  1175. inline const Matrix4 prependScale( const Vector3 & scaleVec, const Matrix4 & mat );
  1176. // Multiply two 4x4 matrices per element
  1177. //
  1178. inline const Matrix4 mulPerElem( const Matrix4 & mat0, const Matrix4 & mat1 );
  1179. // Compute the absolute value of a 4x4 matrix per element
  1180. //
  1181. inline const Matrix4 absPerElem( const Matrix4 & mat );
  1182. // Transpose of a 4x4 matrix
  1183. //
  1184. inline const Matrix4 transpose( const Matrix4 & mat );
  1185. // Compute the inverse of a 4x4 matrix
  1186. // NOTE:
  1187. // Result is unpredictable when the determinant of mat is equal to or near 0.
  1188. //
  1189. inline const Matrix4 inverse( const Matrix4 & mat );
  1190. // Compute the inverse of a 4x4 matrix, which is expected to be an affine matrix
  1191. // NOTE:
  1192. // This can be used to achieve better performance than a general inverse when the specified 4x4 matrix meets the given restrictions. The result is unpredictable when the determinant of mat is equal to or near 0.
  1193. //
  1194. inline const Matrix4 affineInverse( const Matrix4 & mat );
  1195. // Compute the inverse of a 4x4 matrix, which is expected to be an affine matrix with an orthogonal upper-left 3x3 submatrix
  1196. // NOTE:
  1197. // This can be used to achieve better performance than a general inverse when the specified 4x4 matrix meets the given restrictions.
  1198. //
  1199. inline const Matrix4 orthoInverse( const Matrix4 & mat );
  1200. // Determinant of a 4x4 matrix
  1201. //
  1202. inline float determinant( const Matrix4 & mat );
  1203. // Conditionally select between two 4x4 matrices
  1204. //
  1205. inline const Matrix4 select( const Matrix4 & mat0, const Matrix4 & mat1, bool select1 );
  1206. #ifdef _VECTORMATH_DEBUG
  1207. // Print a 4x4 matrix
  1208. // NOTE:
  1209. // Function is only defined when _VECTORMATH_DEBUG is defined.
  1210. //
  1211. inline void print( const Matrix4 & mat );
  1212. // Print a 4x4 matrix and an associated string identifier
  1213. // NOTE:
  1214. // Function is only defined when _VECTORMATH_DEBUG is defined.
  1215. //
  1216. inline void print( const Matrix4 & mat, const char * name );
  1217. #endif
  1218. // A 3x4 transformation matrix in array-of-structures format
  1219. //
  1220. class Transform3
  1221. {
  1222. Vector3 mCol0;
  1223. Vector3 mCol1;
  1224. Vector3 mCol2;
  1225. Vector3 mCol3;
  1226. public:
  1227. // Default constructor; does no initialization
  1228. //
  1229. inline Transform3( ) { };
  1230. // Copy a 3x4 transformation matrix
  1231. //
  1232. inline Transform3( const Transform3 & tfrm );
  1233. // Construct a 3x4 transformation matrix containing the specified columns
  1234. //
  1235. inline Transform3( const Vector3 & col0, const Vector3 & col1, const Vector3 & col2, const Vector3 & col3 );
  1236. // Construct a 3x4 transformation matrix from a 3x3 matrix and a 3-D vector
  1237. //
  1238. inline Transform3( const Matrix3 & tfrm, const Vector3 & translateVec );
  1239. // Construct a 3x4 transformation matrix from a unit-length quaternion and a 3-D vector
  1240. //
  1241. inline Transform3( const Quat & unitQuat, const Vector3 & translateVec );
  1242. // Set all elements of a 3x4 transformation matrix to the same scalar value
  1243. //
  1244. explicit inline Transform3( float scalar );
  1245. // Assign one 3x4 transformation matrix to another
  1246. //
  1247. inline Transform3 & operator =( const Transform3 & tfrm );
  1248. // Set the upper-left 3x3 submatrix
  1249. //
  1250. inline Transform3 & setUpper3x3( const Matrix3 & mat3 );
  1251. // Get the upper-left 3x3 submatrix of a 3x4 transformation matrix
  1252. //
  1253. inline const Matrix3 getUpper3x3( ) const;
  1254. // Set translation component
  1255. //
  1256. inline Transform3 & setTranslation( const Vector3 & translateVec );
  1257. // Get the translation component of a 3x4 transformation matrix
  1258. //
  1259. inline const Vector3 getTranslation( ) const;
  1260. // Set column 0 of a 3x4 transformation matrix
  1261. //
  1262. inline Transform3 & setCol0( const Vector3 & col0 );
  1263. // Set column 1 of a 3x4 transformation matrix
  1264. //
  1265. inline Transform3 & setCol1( const Vector3 & col1 );
  1266. // Set column 2 of a 3x4 transformation matrix
  1267. //
  1268. inline Transform3 & setCol2( const Vector3 & col2 );
  1269. // Set column 3 of a 3x4 transformation matrix
  1270. //
  1271. inline Transform3 & setCol3( const Vector3 & col3 );
  1272. // Get column 0 of a 3x4 transformation matrix
  1273. //
  1274. inline const Vector3 getCol0( ) const;
  1275. // Get column 1 of a 3x4 transformation matrix
  1276. //
  1277. inline const Vector3 getCol1( ) const;
  1278. // Get column 2 of a 3x4 transformation matrix
  1279. //
  1280. inline const Vector3 getCol2( ) const;
  1281. // Get column 3 of a 3x4 transformation matrix
  1282. //
  1283. inline const Vector3 getCol3( ) const;
  1284. // Set the column of a 3x4 transformation matrix referred to by the specified index
  1285. //
  1286. inline Transform3 & setCol( int col, const Vector3 & vec );
  1287. // Set the row of a 3x4 transformation matrix referred to by the specified index
  1288. //
  1289. inline Transform3 & setRow( int row, const Vector4 & vec );
  1290. // Get the column of a 3x4 transformation matrix referred to by the specified index
  1291. //
  1292. inline const Vector3 getCol( int col ) const;
  1293. // Get the row of a 3x4 transformation matrix referred to by the specified index
  1294. //
  1295. inline const Vector4 getRow( int row ) const;
  1296. // Subscripting operator to set or get a column
  1297. //
  1298. inline Vector3 & operator []( int col );
  1299. // Subscripting operator to get a column
  1300. //
  1301. inline const Vector3 operator []( int col ) const;
  1302. // Set the element of a 3x4 transformation matrix referred to by column and row indices
  1303. //
  1304. inline Transform3 & setElem( int col, int row, float val );
  1305. // Get the element of a 3x4 transformation matrix referred to by column and row indices
  1306. //
  1307. inline float getElem( int col, int row ) const;
  1308. // Multiply a 3x4 transformation matrix by a 3-D vector
  1309. //
  1310. inline const Vector3 operator *( const Vector3 & vec ) const;
  1311. // Multiply a 3x4 transformation matrix by a 3-D point
  1312. //
  1313. inline const Point3 operator *( const Point3 & pnt ) const;
  1314. // Multiply two 3x4 transformation matrices
  1315. //
  1316. inline const Transform3 operator *( const Transform3 & tfrm ) const;
  1317. // Perform compound assignment and multiplication by a 3x4 transformation matrix
  1318. //
  1319. inline Transform3 & operator *=( const Transform3 & tfrm );
  1320. // Construct an identity 3x4 transformation matrix
  1321. //
  1322. static inline const Transform3 identity( );
  1323. // Construct a 3x4 transformation matrix to rotate around the x axis
  1324. //
  1325. static inline const Transform3 rotationX( float radians );
  1326. // Construct a 3x4 transformation matrix to rotate around the y axis
  1327. //
  1328. static inline const Transform3 rotationY( float radians );
  1329. // Construct a 3x4 transformation matrix to rotate around the z axis
  1330. //
  1331. static inline const Transform3 rotationZ( float radians );
  1332. // Construct a 3x4 transformation matrix to rotate around the x, y, and z axes
  1333. //
  1334. static inline const Transform3 rotationZYX( const Vector3 & radiansXYZ );
  1335. // Construct a 3x4 transformation matrix to rotate around a unit-length 3-D vector
  1336. //
  1337. static inline const Transform3 rotation( float radians, const Vector3 & unitVec );
  1338. // Construct a rotation matrix from a unit-length quaternion
  1339. //
  1340. static inline const Transform3 rotation( const Quat & unitQuat );
  1341. // Construct a 3x4 transformation matrix to perform scaling
  1342. //
  1343. static inline const Transform3 scale( const Vector3 & scaleVec );
  1344. // Construct a 3x4 transformation matrix to perform translation
  1345. //
  1346. static inline const Transform3 translation( const Vector3 & translateVec );
  1347. };
  1348. // Append (post-multiply) a scale transformation to a 3x4 transformation matrix
  1349. // NOTE:
  1350. // Faster than creating and multiplying a scale transformation matrix.
  1351. //
  1352. inline const Transform3 appendScale( const Transform3 & tfrm, const Vector3 & scaleVec );
  1353. // Prepend (pre-multiply) a scale transformation to a 3x4 transformation matrix
  1354. // NOTE:
  1355. // Faster than creating and multiplying a scale transformation matrix.
  1356. //
  1357. inline const Transform3 prependScale( const Vector3 & scaleVec, const Transform3 & tfrm );
  1358. // Multiply two 3x4 transformation matrices per element
  1359. //
  1360. inline const Transform3 mulPerElem( const Transform3 & tfrm0, const Transform3 & tfrm1 );
  1361. // Compute the absolute value of a 3x4 transformation matrix per element
  1362. //
  1363. inline const Transform3 absPerElem( const Transform3 & tfrm );
  1364. // Inverse of a 3x4 transformation matrix
  1365. // NOTE:
  1366. // Result is unpredictable when the determinant of the left 3x3 submatrix is equal to or near 0.
  1367. //
  1368. inline const Transform3 inverse( const Transform3 & tfrm );
  1369. // Compute the inverse of a 3x4 transformation matrix, expected to have an orthogonal upper-left 3x3 submatrix
  1370. // NOTE:
  1371. // This can be used to achieve better performance than a general inverse when the specified 3x4 transformation matrix meets the given restrictions.
  1372. //
  1373. inline const Transform3 orthoInverse( const Transform3 & tfrm );
  1374. // Conditionally select between two 3x4 transformation matrices
  1375. //
  1376. inline const Transform3 select( const Transform3 & tfrm0, const Transform3 & tfrm1, bool select1 );
  1377. #ifdef _VECTORMATH_DEBUG
  1378. // Print a 3x4 transformation matrix
  1379. // NOTE:
  1380. // Function is only defined when _VECTORMATH_DEBUG is defined.
  1381. //
  1382. inline void print( const Transform3 & tfrm );
  1383. // Print a 3x4 transformation matrix and an associated string identifier
  1384. // NOTE:
  1385. // Function is only defined when _VECTORMATH_DEBUG is defined.
  1386. //
  1387. inline void print( const Transform3 & tfrm, const char * name );
  1388. #endif
  1389. } // namespace Aos
  1390. } // namespace Vectormath
  1391. /*
  1392. Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
  1393. All rights reserved.
  1394. Redistribution and use in source and binary forms,
  1395. with or without modification, are permitted provided that the
  1396. following conditions are met:
  1397. * Redistributions of source code must retain the above copyright
  1398. notice, this list of conditions and the following disclaimer.
  1399. * Redistributions in binary form must reproduce the above copyright
  1400. notice, this list of conditions and the following disclaimer in the
  1401. documentation and/or other materials provided with the distribution.
  1402. * Neither the name of the Sony Computer Entertainment Inc nor the names
  1403. of its contributors may be used to endorse or promote products derived
  1404. from this software without specific prior written permission.
  1405. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  1406. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1407. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1408. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  1409. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  1410. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  1411. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  1412. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  1413. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  1414. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  1415. POSSIBILITY OF SUCH DAMAGE.
  1416. */
  1417. #ifndef _VECTORMATH_VEC_AOS_CPP_H
  1418. #define _VECTORMATH_VEC_AOS_CPP_H
  1419. //-----------------------------------------------------------------------------
  1420. // Constants
  1421. #define _VECTORMATH_SLERP_TOL 0.999f
  1422. //-----------------------------------------------------------------------------
  1423. // Definitions
  1424. #ifndef _VECTORMATH_INTERNAL_FUNCTIONS
  1425. #define _VECTORMATH_INTERNAL_FUNCTIONS
  1426. #endif
  1427. namespace Vectormath {
  1428. namespace Aos {
  1429. inline Vector3::Vector3( const Vector3 & vec )
  1430. {
  1431. mX = vec.mX;
  1432. mY = vec.mY;
  1433. mZ = vec.mZ;
  1434. }
  1435. inline Vector3::Vector3( float _x, float _y, float _z )
  1436. {
  1437. mX = _x;
  1438. mY = _y;
  1439. mZ = _z;
  1440. }
  1441. inline Vector3::Vector3( const Point3 & pnt )
  1442. {
  1443. mX = pnt.getX();
  1444. mY = pnt.getY();
  1445. mZ = pnt.getZ();
  1446. }
  1447. inline Vector3::Vector3( float scalar )
  1448. {
  1449. mX = scalar;
  1450. mY = scalar;
  1451. mZ = scalar;
  1452. }
  1453. inline const Vector3 Vector3::xAxis( )
  1454. {
  1455. return Vector3( 1.0f, 0.0f, 0.0f );
  1456. }
  1457. inline const Vector3 Vector3::yAxis( )
  1458. {
  1459. return Vector3( 0.0f, 1.0f, 0.0f );
  1460. }
  1461. inline const Vector3 Vector3::zAxis( )
  1462. {
  1463. return Vector3( 0.0f, 0.0f, 1.0f );
  1464. }
  1465. inline const Vector3 lerp( float t, const Vector3 & vec0, const Vector3 & vec1 )
  1466. {
  1467. return ( vec0 + ( ( vec1 - vec0 ) * t ) );
  1468. }
  1469. inline const Vector3 slerp( float t, const Vector3 & unitVec0, const Vector3 & unitVec1 )
  1470. {
  1471. float recipSinAngle, scale0, scale1, cosAngle, angle;
  1472. cosAngle = dot( unitVec0, unitVec1 );
  1473. if ( cosAngle < _VECTORMATH_SLERP_TOL ) {
  1474. angle = acosf( cosAngle );
  1475. recipSinAngle = ( 1.0f / sinf( angle ) );
  1476. scale0 = ( sinf( ( ( 1.0f - t ) * angle ) ) * recipSinAngle );
  1477. scale1 = ( sinf( ( t * angle ) ) * recipSinAngle );
  1478. } else {
  1479. scale0 = ( 1.0f - t );
  1480. scale1 = t;
  1481. }
  1482. return ( ( unitVec0 * scale0 ) + ( unitVec1 * scale1 ) );
  1483. }
  1484. inline Vector3 & Vector3::operator =( const Vector3 & vec )
  1485. {
  1486. mX = vec.mX;
  1487. mY = vec.mY;
  1488. mZ = vec.mZ;
  1489. return *this;
  1490. }
  1491. inline Vector3 & Vector3::setX( float _x )
  1492. {
  1493. mX = _x;
  1494. return *this;
  1495. }
  1496. inline float Vector3::getX( ) const
  1497. {
  1498. return mX;
  1499. }
  1500. inline Vector3 & Vector3::setY( float _y )
  1501. {
  1502. mY = _y;
  1503. return *this;
  1504. }
  1505. inline float Vector3::getY( ) const
  1506. {
  1507. return mY;
  1508. }
  1509. inline Vector3 & Vector3::setZ( float _z )
  1510. {
  1511. mZ = _z;
  1512. return *this;
  1513. }
  1514. inline float Vector3::getZ( ) const
  1515. {
  1516. return mZ;
  1517. }
  1518. inline Vector3 & Vector3::setElem( int idx, float value )
  1519. {
  1520. *(&mX + idx) = value;
  1521. return *this;
  1522. }
  1523. inline float Vector3::getElem( int idx ) const
  1524. {
  1525. return *(&mX + idx);
  1526. }
  1527. inline float & Vector3::operator []( int idx )
  1528. {
  1529. return *(&mX + idx);
  1530. }
  1531. inline float Vector3::operator []( int idx ) const
  1532. {
  1533. return *(&mX + idx);
  1534. }
  1535. inline const Vector3 Vector3::operator +( const Vector3 & vec ) const
  1536. {
  1537. return Vector3(
  1538. ( mX + vec.mX ),
  1539. ( mY + vec.mY ),
  1540. ( mZ + vec.mZ )
  1541. );
  1542. }
  1543. inline const Vector3 Vector3::operator -( const Vector3 & vec ) const
  1544. {
  1545. return Vector3(
  1546. ( mX - vec.mX ),
  1547. ( mY - vec.mY ),
  1548. ( mZ - vec.mZ )
  1549. );
  1550. }
  1551. inline const Point3 Vector3::operator +( const Point3 & pnt ) const
  1552. {
  1553. return Point3(
  1554. ( mX + pnt.getX() ),
  1555. ( mY + pnt.getY() ),
  1556. ( mZ + pnt.getZ() )
  1557. );
  1558. }
  1559. inline const Vector3 Vector3::operator *( float scalar ) const
  1560. {
  1561. return Vector3(
  1562. ( mX * scalar ),
  1563. ( mY * scalar ),
  1564. ( mZ * scalar )
  1565. );
  1566. }
  1567. inline Vector3 & Vector3::operator +=( const Vector3 & vec )
  1568. {
  1569. *this = *this + vec;
  1570. return *this;
  1571. }
  1572. inline Vector3 & Vector3::operator -=( const Vector3 & vec )
  1573. {
  1574. *this = *this - vec;
  1575. return *this;
  1576. }
  1577. inline Vector3 & Vector3::operator *=( float scalar )
  1578. {
  1579. *this = *this * scalar;
  1580. return *this;
  1581. }
  1582. inline const Vector3 Vector3::operator /( float scalar ) const
  1583. {
  1584. return Vector3(
  1585. ( mX / scalar ),
  1586. ( mY / scalar ),
  1587. ( mZ / scalar )
  1588. );
  1589. }
  1590. inline Vector3 & Vector3::operator /=( float scalar )
  1591. {
  1592. *this = *this / scalar;
  1593. return *this;
  1594. }
  1595. inline const Vector3 Vector3::operator -( ) const
  1596. {
  1597. return Vector3(
  1598. -mX,
  1599. -mY,
  1600. -mZ
  1601. );
  1602. }
  1603. inline const Vector3 operator *( float scalar, const Vector3 & vec )
  1604. {
  1605. return vec * scalar;
  1606. }
  1607. inline const Vector3 mulPerElem( const Vector3 & vec0, const Vector3 & vec1 )
  1608. {
  1609. return Vector3(
  1610. ( vec0.getX() * vec1.getX() ),
  1611. ( vec0.getY() * vec1.getY() ),
  1612. ( vec0.getZ() * vec1.getZ() )
  1613. );
  1614. }
  1615. inline const Vector3 divPerElem( const Vector3 & vec0, const Vector3 & vec1 )
  1616. {
  1617. return Vector3(
  1618. ( vec0.getX() / vec1.getX() ),
  1619. ( vec0.getY() / vec1.getY() ),
  1620. ( vec0.getZ() / vec1.getZ() )
  1621. );
  1622. }
  1623. inline const Vector3 recipPerElem( const Vector3 & vec )
  1624. {
  1625. return Vector3(
  1626. ( 1.0f / vec.getX() ),
  1627. ( 1.0f / vec.getY() ),
  1628. ( 1.0f / vec.getZ() )
  1629. );
  1630. }
  1631. inline const Vector3 sqrtPerElem( const Vector3 & vec )
  1632. {
  1633. return Vector3(
  1634. sqrtf( vec.getX() ),
  1635. sqrtf( vec.getY() ),
  1636. sqrtf( vec.getZ() )
  1637. );
  1638. }
  1639. inline const Vector3 rsqrtPerElem( const Vector3 & vec )
  1640. {
  1641. return Vector3(
  1642. ( 1.0f / sqrtf( vec.getX() ) ),
  1643. ( 1.0f / sqrtf( vec.getY() ) ),
  1644. ( 1.0f / sqrtf( vec.getZ() ) )
  1645. );
  1646. }
  1647. inline const Vector3 absPerElem( const Vector3 & vec )
  1648. {
  1649. return Vector3(
  1650. fabsf( vec.getX() ),
  1651. fabsf( vec.getY() ),
  1652. fabsf( vec.getZ() )
  1653. );
  1654. }
  1655. inline const Vector3 copySignPerElem( const Vector3 & vec0, const Vector3 & vec1 )
  1656. {
  1657. return Vector3(
  1658. ( vec1.getX() < 0.0f )? -fabsf( vec0.getX() ) : fabsf( vec0.getX() ),
  1659. ( vec1.getY() < 0.0f )? -fabsf( vec0.getY() ) : fabsf( vec0.getY() ),
  1660. ( vec1.getZ() < 0.0f )? -fabsf( vec0.getZ() ) : fabsf( vec0.getZ() )
  1661. );
  1662. }
  1663. inline const Vector3 maxPerElem( const Vector3 & vec0, const Vector3 & vec1 )
  1664. {
  1665. return Vector3(
  1666. (vec0.getX() > vec1.getX())? vec0.getX() : vec1.getX(),
  1667. (vec0.getY() > vec1.getY())? vec0.getY() : vec1.getY(),
  1668. (vec0.getZ() > vec1.getZ())? vec0.getZ() : vec1.getZ()
  1669. );
  1670. }
  1671. inline float maxElem( const Vector3 & vec )
  1672. {
  1673. float result;
  1674. result = (vec.getX() > vec.getY())? vec.getX() : vec.getY();
  1675. result = (vec.getZ() > result)? vec.getZ() : result;
  1676. return result;
  1677. }
  1678. inline const Vector3 minPerElem( const Vector3 & vec0, const Vector3 & vec1 )
  1679. {
  1680. return Vector3(
  1681. (vec0.getX() < vec1.getX())? vec0.getX() : vec1.getX(),
  1682. (vec0.getY() < vec1.getY())? vec0.getY() : vec1.getY(),
  1683. (vec0.getZ() < vec1.getZ())? vec0.getZ() : vec1.getZ()
  1684. );
  1685. }
  1686. inline float minElem( const Vector3 & vec )
  1687. {
  1688. float result;
  1689. result = (vec.getX() < vec.getY())? vec.getX() : vec.getY();
  1690. result = (vec.getZ() < result)? vec.getZ() : result;
  1691. return result;
  1692. }
  1693. inline float sum( const Vector3 & vec )
  1694. {
  1695. float result;
  1696. result = ( vec.getX() + vec.getY() );
  1697. result = ( result + vec.getZ() );
  1698. return result;
  1699. }
  1700. inline float dot( const Vector3 & vec0, const Vector3 & vec1 )
  1701. {
  1702. float result;
  1703. result = ( vec0.getX() * vec1.getX() );
  1704. result = ( result + ( vec0.getY() * vec1.getY() ) );
  1705. result = ( result + ( vec0.getZ() * vec1.getZ() ) );
  1706. return result;
  1707. }
  1708. inline float lengthSqr( const Vector3 & vec )
  1709. {
  1710. float result;
  1711. result = ( vec.getX() * vec.getX() );
  1712. result = ( result + ( vec.getY() * vec.getY() ) );
  1713. result = ( result + ( vec.getZ() * vec.getZ() ) );
  1714. return result;
  1715. }
  1716. inline float length( const Vector3 & vec )
  1717. {
  1718. return sqrtf( lengthSqr( vec ) );
  1719. }
  1720. inline const Vector3 normalize( const Vector3 & vec )
  1721. {
  1722. float lenSqr, lenInv;
  1723. lenSqr = lengthSqr( vec );
  1724. lenInv = ( 1.0f / sqrtf( lenSqr ) );
  1725. return Vector3(
  1726. ( vec.getX() * lenInv ),
  1727. ( vec.getY() * lenInv ),
  1728. ( vec.getZ() * lenInv )
  1729. );
  1730. }
  1731. inline const Vector3 cross( const Vector3 & vec0, const Vector3 & vec1 )
  1732. {
  1733. return Vector3(
  1734. ( ( vec0.getY() * vec1.getZ() ) - ( vec0.getZ() * vec1.getY() ) ),
  1735. ( ( vec0.getZ() * vec1.getX() ) - ( vec0.getX() * vec1.getZ() ) ),
  1736. ( ( vec0.getX() * vec1.getY() ) - ( vec0.getY() * vec1.getX() ) )
  1737. );
  1738. }
  1739. inline const Vector3 select( const Vector3 & vec0, const Vector3 & vec1, bool select1 )
  1740. {
  1741. return Vector3(
  1742. ( select1 )? vec1.getX() : vec0.getX(),
  1743. ( select1 )? vec1.getY() : vec0.getY(),
  1744. ( select1 )? vec1.getZ() : vec0.getZ()
  1745. );
  1746. }
  1747. #ifdef _VECTORMATH_DEBUG
  1748. inline void print( const Vector3 & vec )
  1749. {
  1750. printf( "( %f %f %f )\n", vec.getX(), vec.getY(), vec.getZ() );
  1751. }
  1752. inline void print( const Vector3 & vec, const char * name )
  1753. {
  1754. printf( "%s: ( %f %f %f )\n", name, vec.getX(), vec.getY(), vec.getZ() );
  1755. }
  1756. #endif
  1757. inline Vector4::Vector4( const Vector4 & vec )
  1758. {
  1759. mX = vec.mX;
  1760. mY = vec.mY;
  1761. mZ = vec.mZ;
  1762. mW = vec.mW;
  1763. }
  1764. inline Vector4::Vector4( float _x, float _y, float _z, float _w )
  1765. {
  1766. mX = _x;
  1767. mY = _y;
  1768. mZ = _z;
  1769. mW = _w;
  1770. }
  1771. inline Vector4::Vector4( const Vector3 & xyz, float _w )
  1772. {
  1773. this->setXYZ( xyz );
  1774. this->setW( _w );
  1775. }
  1776. inline Vector4::Vector4( const Vector3 & vec )
  1777. {
  1778. mX = vec.getX();
  1779. mY = vec.getY();
  1780. mZ = vec.getZ();
  1781. mW = 0.0f;
  1782. }
  1783. inline Vector4::Vector4( const Point3 & pnt )
  1784. {
  1785. mX = pnt.getX();
  1786. mY = pnt.getY();
  1787. mZ = pnt.getZ();
  1788. mW = 1.0f;
  1789. }
  1790. inline Vector4::Vector4( const Quat & quat )
  1791. {
  1792. mX = quat.getX();
  1793. mY = quat.getY();
  1794. mZ = quat.getZ();
  1795. mW = quat.getW();
  1796. }
  1797. inline Vector4::Vector4( float scalar )
  1798. {
  1799. mX = scalar;
  1800. mY = scalar;
  1801. mZ = scalar;
  1802. mW = scalar;
  1803. }
  1804. inline const Vector4 Vector4::xAxis( )
  1805. {
  1806. return Vector4( 1.0f, 0.0f, 0.0f, 0.0f );
  1807. }
  1808. inline const Vector4 Vector4::yAxis( )
  1809. {
  1810. return Vector4( 0.0f, 1.0f, 0.0f, 0.0f );
  1811. }
  1812. inline const Vector4 Vector4::zAxis( )
  1813. {
  1814. return Vector4( 0.0f, 0.0f, 1.0f, 0.0f );
  1815. }
  1816. inline const Vector4 Vector4::wAxis( )
  1817. {
  1818. return Vector4( 0.0f, 0.0f, 0.0f, 1.0f );
  1819. }
  1820. inline const Vector4 lerp( float t, const Vector4 & vec0, const Vector4 & vec1 )
  1821. {
  1822. return ( vec0 + ( ( vec1 - vec0 ) * t ) );
  1823. }
  1824. inline const Vector4 slerp( float t, const Vector4 & unitVec0, const Vector4 & unitVec1 )
  1825. {
  1826. float recipSinAngle, scale0, scale1, cosAngle, angle;
  1827. cosAngle = dot( unitVec0, unitVec1 );
  1828. if ( cosAngle < _VECTORMATH_SLERP_TOL ) {
  1829. angle = acosf( cosAngle );
  1830. recipSinAngle = ( 1.0f / sinf( angle ) );
  1831. scale0 = ( sinf( ( ( 1.0f - t ) * angle ) ) * recipSinAngle );
  1832. scale1 = ( sinf( ( t * angle ) ) * recipSinAngle );
  1833. } else {
  1834. scale0 = ( 1.0f - t );
  1835. scale1 = t;
  1836. }
  1837. return ( ( unitVec0 * scale0 ) + ( unitVec1 * scale1 ) );
  1838. }
  1839. inline Vector4 & Vector4::operator =( const Vector4 & vec )
  1840. {
  1841. mX = vec.mX;
  1842. mY = vec.mY;
  1843. mZ = vec.mZ;
  1844. mW = vec.mW;
  1845. return *this;
  1846. }
  1847. inline Vector4 & Vector4::setXYZ( const Vector3 & vec )
  1848. {
  1849. mX = vec.getX();
  1850. mY = vec.getY();
  1851. mZ = vec.getZ();
  1852. return *this;
  1853. }
  1854. inline const Vector3 Vector4::getXYZ( ) const
  1855. {
  1856. return Vector3( mX, mY, mZ );
  1857. }
  1858. inline Vector4 & Vector4::setX( float _x )
  1859. {
  1860. mX = _x;
  1861. return *this;
  1862. }
  1863. inline float Vector4::getX( ) const
  1864. {
  1865. return mX;
  1866. }
  1867. inline Vector4 & Vector4::setY( float _y )
  1868. {
  1869. mY = _y;
  1870. return *this;
  1871. }
  1872. inline float Vector4::getY( ) const
  1873. {
  1874. return mY;
  1875. }
  1876. inline Vector4 & Vector4::setZ( float _z )
  1877. {
  1878. mZ = _z;
  1879. return *this;
  1880. }
  1881. inline float Vector4::getZ( ) const
  1882. {
  1883. return mZ;
  1884. }
  1885. inline Vector4 & Vector4::setW( float _w )
  1886. {
  1887. mW = _w;
  1888. return *this;
  1889. }
  1890. inline float Vector4::getW( ) const
  1891. {
  1892. return mW;
  1893. }
  1894. inline Vector4 & Vector4::setElem( int idx, float value )
  1895. {
  1896. *(&mX + idx) = value;
  1897. return *this;
  1898. }
  1899. inline float Vector4::getElem( int idx ) const
  1900. {
  1901. return *(&mX + idx);
  1902. }
  1903. inline float & Vector4::operator []( int idx )
  1904. {
  1905. return *(&mX + idx);
  1906. }
  1907. inline float Vector4::operator []( int idx ) const
  1908. {
  1909. return *(&mX + idx);
  1910. }
  1911. inline const Vector4 Vector4::operator +( const Vector4 & vec ) const
  1912. {
  1913. return Vector4(
  1914. ( mX + vec.mX ),
  1915. ( mY + vec.mY ),
  1916. ( mZ + vec.mZ ),
  1917. ( mW + vec.mW )
  1918. );
  1919. }
  1920. inline const Vector4 Vector4::operator -( const Vector4 & vec ) const
  1921. {
  1922. return Vector4(
  1923. ( mX - vec.mX ),
  1924. ( mY - vec.mY ),
  1925. ( mZ - vec.mZ ),
  1926. ( mW - vec.mW )
  1927. );
  1928. }
  1929. inline const Vector4 Vector4::operator *( float scalar ) const
  1930. {
  1931. return Vector4(
  1932. ( mX * scalar ),
  1933. ( mY * scalar ),
  1934. ( mZ * scalar ),
  1935. ( mW * scalar )
  1936. );
  1937. }
  1938. inline Vector4 & Vector4::operator +=( const Vector4 & vec )
  1939. {
  1940. *this = *this + vec;
  1941. return *this;
  1942. }
  1943. inline Vector4 & Vector4::operator -=( const Vector4 & vec )
  1944. {
  1945. *this = *this - vec;
  1946. return *this;
  1947. }
  1948. inline Vector4 & Vector4::operator *=( float scalar )
  1949. {
  1950. *this = *this * scalar;
  1951. return *this;
  1952. }
  1953. inline const Vector4 Vector4::operator /( float scalar ) const
  1954. {
  1955. return Vector4(
  1956. ( mX / scalar ),
  1957. ( mY / scalar ),
  1958. ( mZ / scalar ),
  1959. ( mW / scalar )
  1960. );
  1961. }
  1962. inline Vector4 & Vector4::operator /=( float scalar )
  1963. {
  1964. *this = *this / scalar;
  1965. return *this;
  1966. }
  1967. inline const Vector4 Vector4::operator -( ) const
  1968. {
  1969. return Vector4(
  1970. -mX,
  1971. -mY,
  1972. -mZ,
  1973. -mW
  1974. );
  1975. }
  1976. inline const Vector4 operator *( float scalar, const Vector4 & vec )
  1977. {
  1978. return vec * scalar;
  1979. }
  1980. inline const Vector4 mulPerElem( const Vector4 & vec0, const Vector4 & vec1 )
  1981. {
  1982. return Vector4(
  1983. ( vec0.getX() * vec1.getX() ),
  1984. ( vec0.getY() * vec1.getY() ),
  1985. ( vec0.getZ() * vec1.getZ() ),
  1986. ( vec0.getW() * vec1.getW() )
  1987. );
  1988. }
  1989. inline const Vector4 divPerElem( const Vector4 & vec0, const Vector4 & vec1 )
  1990. {
  1991. return Vector4(
  1992. ( vec0.getX() / vec1.getX() ),
  1993. ( vec0.getY() / vec1.getY() ),
  1994. ( vec0.getZ() / vec1.getZ() ),
  1995. ( vec0.getW() / vec1.getW() )
  1996. );
  1997. }
  1998. inline const Vector4 recipPerElem( const Vector4 & vec )
  1999. {
  2000. return Vector4(
  2001. ( 1.0f / vec.getX() ),
  2002. ( 1.0f / vec.getY() ),
  2003. ( 1.0f / vec.getZ() ),
  2004. ( 1.0f / vec.getW() )
  2005. );
  2006. }
  2007. inline const Vector4 sqrtPerElem( const Vector4 & vec )
  2008. {
  2009. return Vector4(
  2010. sqrtf( vec.getX() ),
  2011. sqrtf( vec.getY() ),
  2012. sqrtf( vec.getZ() ),
  2013. sqrtf( vec.getW() )
  2014. );
  2015. }
  2016. inline const Vector4 rsqrtPerElem( const Vector4 & vec )
  2017. {
  2018. return Vector4(
  2019. ( 1.0f / sqrtf( vec.getX() ) ),
  2020. ( 1.0f / sqrtf( vec.getY() ) ),
  2021. ( 1.0f / sqrtf( vec.getZ() ) ),
  2022. ( 1.0f / sqrtf( vec.getW() ) )
  2023. );
  2024. }
  2025. inline const Vector4 absPerElem( const Vector4 & vec )
  2026. {
  2027. return Vector4(
  2028. fabsf( vec.getX() ),
  2029. fabsf( vec.getY() ),
  2030. fabsf( vec.getZ() ),
  2031. fabsf( vec.getW() )
  2032. );
  2033. }
  2034. inline const Vector4 copySignPerElem( const Vector4 & vec0, const Vector4 & vec1 )
  2035. {
  2036. return Vector4(
  2037. ( vec1.getX() < 0.0f )? -fabsf( vec0.getX() ) : fabsf( vec0.getX() ),
  2038. ( vec1.getY() < 0.0f )? -fabsf( vec0.getY() ) : fabsf( vec0.getY() ),
  2039. ( vec1.getZ() < 0.0f )? -fabsf( vec0.getZ() ) : fabsf( vec0.getZ() ),
  2040. ( vec1.getW() < 0.0f )? -fabsf( vec0.getW() ) : fabsf( vec0.getW() )
  2041. );
  2042. }
  2043. inline const Vector4 maxPerElem( const Vector4 & vec0, const Vector4 & vec1 )
  2044. {
  2045. return Vector4(
  2046. (vec0.getX() > vec1.getX())? vec0.getX() : vec1.getX(),
  2047. (vec0.getY() > vec1.getY())? vec0.getY() : vec1.getY(),
  2048. (vec0.getZ() > vec1.getZ())? vec0.getZ() : vec1.getZ(),
  2049. (vec0.getW() > vec1.getW())? vec0.getW() : vec1.getW()
  2050. );
  2051. }
  2052. inline float maxElem( const Vector4 & vec )
  2053. {
  2054. float result;
  2055. result = (vec.getX() > vec.getY())? vec.getX() : vec.getY();
  2056. result = (vec.getZ() > result)? vec.getZ() : result;
  2057. result = (vec.getW() > result)? vec.getW() : result;
  2058. return result;
  2059. }
  2060. inline const Vector4 minPerElem( const Vector4 & vec0, const Vector4 & vec1 )
  2061. {
  2062. return Vector4(
  2063. (vec0.getX() < vec1.getX())? vec0.getX() : vec1.getX(),
  2064. (vec0.getY() < vec1.getY())? vec0.getY() : vec1.getY(),
  2065. (vec0.getZ() < vec1.getZ())? vec0.getZ() : vec1.getZ(),
  2066. (vec0.getW() < vec1.getW())? vec0.getW() : vec1.getW()
  2067. );
  2068. }
  2069. inline float minElem( const Vector4 & vec )
  2070. {
  2071. float result;
  2072. result = (vec.getX() < vec.getY())? vec.getX() : vec.getY();
  2073. result = (vec.getZ() < result)? vec.getZ() : result;
  2074. result = (vec.getW() < result)? vec.getW() : result;
  2075. return result;
  2076. }
  2077. inline float sum( const Vector4 & vec )
  2078. {
  2079. float result;
  2080. result = ( vec.getX() + vec.getY() );
  2081. result = ( result + vec.getZ() );
  2082. result = ( result + vec.getW() );
  2083. return result;
  2084. }
  2085. inline float dot( const Vector4 & vec0, const Vector4 & vec1 )
  2086. {
  2087. float result;
  2088. result = ( vec0.getX() * vec1.getX() );
  2089. result = ( result + ( vec0.getY() * vec1.getY() ) );
  2090. result = ( result + ( vec0.getZ() * vec1.getZ() ) );
  2091. result = ( result + ( vec0.getW() * vec1.getW() ) );
  2092. return result;
  2093. }
  2094. inline float lengthSqr( const Vector4 & vec )
  2095. {
  2096. float result;
  2097. result = ( vec.getX() * vec.getX() );
  2098. result = ( result + ( vec.getY() * vec.getY() ) );
  2099. result = ( result + ( vec.getZ() * vec.getZ() ) );
  2100. result = ( result + ( vec.getW() * vec.getW() ) );
  2101. return result;
  2102. }
  2103. inline float length( const Vector4 & vec )
  2104. {
  2105. return sqrtf( lengthSqr( vec ) );
  2106. }
  2107. inline const Vector4 normalize( const Vector4 & vec )
  2108. {
  2109. float lenSqr, lenInv;
  2110. lenSqr = lengthSqr( vec );
  2111. lenInv = ( 1.0f / sqrtf( lenSqr ) );
  2112. return Vector4(
  2113. ( vec.getX() * lenInv ),
  2114. ( vec.getY() * lenInv ),
  2115. ( vec.getZ() * lenInv ),
  2116. ( vec.getW() * lenInv )
  2117. );
  2118. }
  2119. inline const Vector4 select( const Vector4 & vec0, const Vector4 & vec1, bool select1 )
  2120. {
  2121. return Vector4(
  2122. ( select1 )? vec1.getX() : vec0.getX(),
  2123. ( select1 )? vec1.getY() : vec0.getY(),
  2124. ( select1 )? vec1.getZ() : vec0.getZ(),
  2125. ( select1 )? vec1.getW() : vec0.getW()
  2126. );
  2127. }
  2128. #ifdef _VECTORMATH_DEBUG
  2129. inline void print( const Vector4 & vec )
  2130. {
  2131. printf( "( %f %f %f %f )\n", vec.getX(), vec.getY(), vec.getZ(), vec.getW() );
  2132. }
  2133. inline void print( const Vector4 & vec, const char * name )
  2134. {
  2135. printf( "%s: ( %f %f %f %f )\n", name, vec.getX(), vec.getY(), vec.getZ(), vec.getW() );
  2136. }
  2137. #endif
  2138. inline Point3::Point3( const Point3 & pnt )
  2139. {
  2140. mX = pnt.mX;
  2141. mY = pnt.mY;
  2142. mZ = pnt.mZ;
  2143. }
  2144. inline Point3::Point3( float _x, float _y, float _z )
  2145. {
  2146. mX = _x;
  2147. mY = _y;
  2148. mZ = _z;
  2149. }
  2150. inline Point3::Point3( const Vector3 & vec )
  2151. {
  2152. mX = vec.getX();
  2153. mY = vec.getY();
  2154. mZ = vec.getZ();
  2155. }
  2156. inline Point3::Point3( float scalar )
  2157. {
  2158. mX = scalar;
  2159. mY = scalar;
  2160. mZ = scalar;
  2161. }
  2162. inline const Point3 lerp( float t, const Point3 & pnt0, const Point3 & pnt1 )
  2163. {
  2164. return ( pnt0 + ( ( pnt1 - pnt0 ) * t ) );
  2165. }
  2166. inline Point3 & Point3::operator =( const Point3 & pnt )
  2167. {
  2168. mX = pnt.mX;
  2169. mY = pnt.mY;
  2170. mZ = pnt.mZ;
  2171. return *this;
  2172. }
  2173. inline Point3 & Point3::setX( float _x )
  2174. {
  2175. mX = _x;
  2176. return *this;
  2177. }
  2178. inline float Point3::getX( ) const
  2179. {
  2180. return mX;
  2181. }
  2182. inline Point3 & Point3::setY( float _y )
  2183. {
  2184. mY = _y;
  2185. return *this;
  2186. }
  2187. inline float Point3::getY( ) const
  2188. {
  2189. return mY;
  2190. }
  2191. inline Point3 & Point3::setZ( float _z )
  2192. {
  2193. mZ = _z;
  2194. return *this;
  2195. }
  2196. inline float Point3::getZ( ) const
  2197. {
  2198. return mZ;
  2199. }
  2200. inline Point3 & Point3::setElem( int idx, float value )
  2201. {
  2202. *(&mX + idx) = value;
  2203. return *this;
  2204. }
  2205. inline float Point3::getElem( int idx ) const
  2206. {
  2207. return *(&mX + idx);
  2208. }
  2209. inline float & Point3::operator []( int idx )
  2210. {
  2211. return *(&mX + idx);
  2212. }
  2213. inline float Point3::operator []( int idx ) const
  2214. {
  2215. return *(&mX + idx);
  2216. }
  2217. inline const Vector3 Point3::operator -( const Point3 & pnt ) const
  2218. {
  2219. return Vector3(
  2220. ( mX - pnt.mX ),
  2221. ( mY - pnt.mY ),
  2222. ( mZ - pnt.mZ )
  2223. );
  2224. }
  2225. inline const Point3 Point3::operator +( const Vector3 & vec ) const
  2226. {
  2227. return Point3(
  2228. ( mX + vec.getX() ),
  2229. ( mY + vec.getY() ),
  2230. ( mZ + vec.getZ() )
  2231. );
  2232. }
  2233. inline const Point3 Point3::operator -( const Vector3 & vec ) const
  2234. {
  2235. return Point3(
  2236. ( mX - vec.getX() ),
  2237. ( mY - vec.getY() ),
  2238. ( mZ - vec.getZ() )
  2239. );
  2240. }
  2241. inline Point3 & Point3::operator +=( const Vector3 & vec )
  2242. {
  2243. *this = *this + vec;
  2244. return *this;
  2245. }
  2246. inline Point3 & Point3::operator -=( const Vector3 & vec )
  2247. {
  2248. *this = *this - vec;
  2249. return *this;
  2250. }
  2251. inline const Point3 mulPerElem( const Point3 & pnt0, const Point3 & pnt1 )
  2252. {
  2253. return Point3(
  2254. ( pnt0.getX() * pnt1.getX() ),
  2255. ( pnt0.getY() * pnt1.getY() ),
  2256. ( pnt0.getZ() * pnt1.getZ() )
  2257. );
  2258. }
  2259. inline const Point3 divPerElem( const Point3 & pnt0, const Point3 & pnt1 )
  2260. {
  2261. return Point3(
  2262. ( pnt0.getX() / pnt1.getX() ),
  2263. ( pnt0.getY() / pnt1.getY() ),
  2264. ( pnt0.getZ() / pnt1.getZ() )
  2265. );
  2266. }
  2267. inline const Point3 recipPerElem( const Point3 & pnt )
  2268. {
  2269. return Point3(
  2270. ( 1.0f / pnt.getX() ),
  2271. ( 1.0f / pnt.getY() ),
  2272. ( 1.0f / pnt.getZ() )
  2273. );
  2274. }
  2275. inline const Point3 sqrtPerElem( const Point3 & pnt )
  2276. {
  2277. return Point3(
  2278. sqrtf( pnt.getX() ),
  2279. sqrtf( pnt.getY() ),
  2280. sqrtf( pnt.getZ() )
  2281. );
  2282. }
  2283. inline const Point3 rsqrtPerElem( const Point3 & pnt )
  2284. {
  2285. return Point3(
  2286. ( 1.0f / sqrtf( pnt.getX() ) ),
  2287. ( 1.0f / sqrtf( pnt.getY() ) ),
  2288. ( 1.0f / sqrtf( pnt.getZ() ) )
  2289. );
  2290. }
  2291. inline const Point3 absPerElem( const Point3 & pnt )
  2292. {
  2293. return Point3(
  2294. fabsf( pnt.getX() ),
  2295. fabsf( pnt.getY() ),
  2296. fabsf( pnt.getZ() )
  2297. );
  2298. }
  2299. inline const Point3 copySignPerElem( const Point3 & pnt0, const Point3 & pnt1 )
  2300. {
  2301. return Point3(
  2302. ( pnt1.getX() < 0.0f )? -fabsf( pnt0.getX() ) : fabsf( pnt0.getX() ),
  2303. ( pnt1.getY() < 0.0f )? -fabsf( pnt0.getY() ) : fabsf( pnt0.getY() ),
  2304. ( pnt1.getZ() < 0.0f )? -fabsf( pnt0.getZ() ) : fabsf( pnt0.getZ() )
  2305. );
  2306. }
  2307. inline const Point3 maxPerElem( const Point3 & pnt0, const Point3 & pnt1 )
  2308. {
  2309. return Point3(
  2310. (pnt0.getX() > pnt1.getX())? pnt0.getX() : pnt1.getX(),
  2311. (pnt0.getY() > pnt1.getY())? pnt0.getY() : pnt1.getY(),
  2312. (pnt0.getZ() > pnt1.getZ())? pnt0.getZ() : pnt1.getZ()
  2313. );
  2314. }
  2315. inline float maxElem( const Point3 & pnt )
  2316. {
  2317. float result;
  2318. result = (pnt.getX() > pnt.getY())? pnt.getX() : pnt.getY();
  2319. result = (pnt.getZ() > result)? pnt.getZ() : result;
  2320. return result;
  2321. }
  2322. inline const Point3 minPerElem( const Point3 & pnt0, const Point3 & pnt1 )
  2323. {
  2324. return Point3(
  2325. (pnt0.getX() < pnt1.getX())? pnt0.getX() : pnt1.getX(),
  2326. (pnt0.getY() < pnt1.getY())? pnt0.getY() : pnt1.getY(),
  2327. (pnt0.getZ() < pnt1.getZ())? pnt0.getZ() : pnt1.getZ()
  2328. );
  2329. }
  2330. inline float minElem( const Point3 & pnt )
  2331. {
  2332. float result;
  2333. result = (pnt.getX() < pnt.getY())? pnt.getX() : pnt.getY();
  2334. result = (pnt.getZ() < result)? pnt.getZ() : result;
  2335. return result;
  2336. }
  2337. inline float sum( const Point3 & pnt )
  2338. {
  2339. float result;
  2340. result = ( pnt.getX() + pnt.getY() );
  2341. result = ( result + pnt.getZ() );
  2342. return result;
  2343. }
  2344. inline const Point3 scale( const Point3 & pnt, float scaleVal )
  2345. {
  2346. return mulPerElem( pnt, Point3( scaleVal ) );
  2347. }
  2348. inline const Point3 scale( const Point3 & pnt, const Vector3 & scaleVec )
  2349. {
  2350. return mulPerElem( pnt, Point3( scaleVec ) );
  2351. }
  2352. inline float projection( const Point3 & pnt, const Vector3 & unitVec )
  2353. {
  2354. float result;
  2355. result = ( pnt.getX() * unitVec.getX() );
  2356. result = ( result + ( pnt.getY() * unitVec.getY() ) );
  2357. result = ( result + ( pnt.getZ() * unitVec.getZ() ) );
  2358. return result;
  2359. }
  2360. inline float distSqrFromOrigin( const Point3 & pnt )
  2361. {
  2362. return lengthSqr( Vector3( pnt ) );
  2363. }
  2364. inline float distFromOrigin( const Point3 & pnt )
  2365. {
  2366. return length( Vector3( pnt ) );
  2367. }
  2368. inline float distSqr( const Point3 & pnt0, const Point3 & pnt1 )
  2369. {
  2370. return lengthSqr( ( pnt1 - pnt0 ) );
  2371. }
  2372. inline float dist( const Point3 & pnt0, const Point3 & pnt1 )
  2373. {
  2374. return length( ( pnt1 - pnt0 ) );
  2375. }
  2376. inline const Point3 select( const Point3 & pnt0, const Point3 & pnt1, bool select1 )
  2377. {
  2378. return Point3(
  2379. ( select1 )? pnt1.getX() : pnt0.getX(),
  2380. ( select1 )? pnt1.getY() : pnt0.getY(),
  2381. ( select1 )? pnt1.getZ() : pnt0.getZ()
  2382. );
  2383. }
  2384. #ifdef _VECTORMATH_DEBUG
  2385. inline void print( const Point3 & pnt )
  2386. {
  2387. printf( "( %f %f %f )\n", pnt.getX(), pnt.getY(), pnt.getZ() );
  2388. }
  2389. inline void print( const Point3 & pnt, const char * name )
  2390. {
  2391. printf( "%s: ( %f %f %f )\n", name, pnt.getX(), pnt.getY(), pnt.getZ() );
  2392. }
  2393. #endif
  2394. } // namespace Aos
  2395. } // namespace Vectormath
  2396. #endif
  2397. /*
  2398. Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
  2399. All rights reserved.
  2400. Redistribution and use in source and binary forms,
  2401. with or without modification, are permitted provided that the
  2402. following conditions are met:
  2403. * Redistributions of source code must retain the above copyright
  2404. notice, this list of conditions and the following disclaimer.
  2405. * Redistributions in binary form must reproduce the above copyright
  2406. notice, this list of conditions and the following disclaimer in the
  2407. documentation and/or other materials provided with the distribution.
  2408. * Neither the name of the Sony Computer Entertainment Inc nor the names
  2409. of its contributors may be used to endorse or promote products derived
  2410. from this software without specific prior written permission.
  2411. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  2412. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2413. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2414. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  2415. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  2416. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  2417. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  2418. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  2419. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  2420. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  2421. POSSIBILITY OF SUCH DAMAGE.
  2422. */
  2423. #ifndef _VECTORMATH_QUAT_AOS_CPP_H
  2424. #define _VECTORMATH_QUAT_AOS_CPP_H
  2425. //-----------------------------------------------------------------------------
  2426. // Definitions
  2427. #ifndef _VECTORMATH_INTERNAL_FUNCTIONS
  2428. #define _VECTORMATH_INTERNAL_FUNCTIONS
  2429. #endif
  2430. namespace Vectormath {
  2431. namespace Aos {
  2432. inline Quat::Quat( const Quat & quat )
  2433. {
  2434. mX = quat.mX;
  2435. mY = quat.mY;
  2436. mZ = quat.mZ;
  2437. mW = quat.mW;
  2438. }
  2439. inline Quat::Quat( float _x, float _y, float _z, float _w )
  2440. {
  2441. mX = _x;
  2442. mY = _y;
  2443. mZ = _z;
  2444. mW = _w;
  2445. }
  2446. inline Quat::Quat( const Vector3 & xyz, float _w )
  2447. {
  2448. this->setXYZ( xyz );
  2449. this->setW( _w );
  2450. }
  2451. inline Quat::Quat( const Vector4 & vec )
  2452. {
  2453. mX = vec.getX();
  2454. mY = vec.getY();
  2455. mZ = vec.getZ();
  2456. mW = vec.getW();
  2457. }
  2458. inline Quat::Quat( float scalar )
  2459. {
  2460. mX = scalar;
  2461. mY = scalar;
  2462. mZ = scalar;
  2463. mW = scalar;
  2464. }
  2465. inline const Quat Quat::identity( )
  2466. {
  2467. return Quat( 0.0f, 0.0f, 0.0f, 1.0f );
  2468. }
  2469. inline const Quat lerp( float t, const Quat & quat0, const Quat & quat1 )
  2470. {
  2471. return ( quat0 + ( ( quat1 - quat0 ) * t ) );
  2472. }
  2473. inline const Quat slerp( float t, const Quat & unitQuat0, const Quat & unitQuat1 )
  2474. {
  2475. Quat start;
  2476. float recipSinAngle, scale0, scale1, cosAngle, angle;
  2477. cosAngle = dot( unitQuat0, unitQuat1 );
  2478. if ( cosAngle < 0.0f ) {
  2479. cosAngle = -cosAngle;
  2480. start = ( -unitQuat0 );
  2481. } else {
  2482. start = unitQuat0;
  2483. }
  2484. if ( cosAngle < _VECTORMATH_SLERP_TOL ) {
  2485. angle = acosf( cosAngle );
  2486. recipSinAngle = ( 1.0f / sinf( angle ) );
  2487. scale0 = ( sinf( ( ( 1.0f - t ) * angle ) ) * recipSinAngle );
  2488. scale1 = ( sinf( ( t * angle ) ) * recipSinAngle );
  2489. } else {
  2490. scale0 = ( 1.0f - t );
  2491. scale1 = t;
  2492. }
  2493. return ( ( start * scale0 ) + ( unitQuat1 * scale1 ) );
  2494. }
  2495. inline const Quat squad( float t, const Quat & unitQuat0, const Quat & unitQuat1, const Quat & unitQuat2, const Quat & unitQuat3 )
  2496. {
  2497. Quat tmp0, tmp1;
  2498. tmp0 = slerp( t, unitQuat0, unitQuat3 );
  2499. tmp1 = slerp( t, unitQuat1, unitQuat2 );
  2500. return slerp( ( ( 2.0f * t ) * ( 1.0f - t ) ), tmp0, tmp1 );
  2501. }
  2502. inline Quat & Quat::operator =( const Quat & quat )
  2503. {
  2504. mX = quat.mX;
  2505. mY = quat.mY;
  2506. mZ = quat.mZ;
  2507. mW = quat.mW;
  2508. return *this;
  2509. }
  2510. inline Quat & Quat::setXYZ( const Vector3 & vec )
  2511. {
  2512. mX = vec.getX();
  2513. mY = vec.getY();
  2514. mZ = vec.getZ();
  2515. return *this;
  2516. }
  2517. inline const Vector3 Quat::getXYZ( ) const
  2518. {
  2519. return Vector3( mX, mY, mZ );
  2520. }
  2521. inline Quat & Quat::setX( float _x )
  2522. {
  2523. mX = _x;
  2524. return *this;
  2525. }
  2526. inline float Quat::getX( ) const
  2527. {
  2528. return mX;
  2529. }
  2530. inline Quat & Quat::setY( float _y )
  2531. {
  2532. mY = _y;
  2533. return *this;
  2534. }
  2535. inline float Quat::getY( ) const
  2536. {
  2537. return mY;
  2538. }
  2539. inline Quat & Quat::setZ( float _z )
  2540. {
  2541. mZ = _z;
  2542. return *this;
  2543. }
  2544. inline float Quat::getZ( ) const
  2545. {
  2546. return mZ;
  2547. }
  2548. inline Quat & Quat::setW( float _w )
  2549. {
  2550. mW = _w;
  2551. return *this;
  2552. }
  2553. inline float Quat::getW( ) const
  2554. {
  2555. return mW;
  2556. }
  2557. inline Quat & Quat::setElem( int idx, float value )
  2558. {
  2559. *(&mX + idx) = value;
  2560. return *this;
  2561. }
  2562. inline float Quat::getElem( int idx ) const
  2563. {
  2564. return *(&mX + idx);
  2565. }
  2566. inline float & Quat::operator []( int idx )
  2567. {
  2568. return *(&mX + idx);
  2569. }
  2570. inline float Quat::operator []( int idx ) const
  2571. {
  2572. return *(&mX + idx);
  2573. }
  2574. inline const Quat Quat::operator +( const Quat & quat ) const
  2575. {
  2576. return Quat(
  2577. ( mX + quat.mX ),
  2578. ( mY + quat.mY ),
  2579. ( mZ + quat.mZ ),
  2580. ( mW + quat.mW )
  2581. );
  2582. }
  2583. inline const Quat Quat::operator -( const Quat & quat ) const
  2584. {
  2585. return Quat(
  2586. ( mX - quat.mX ),
  2587. ( mY - quat.mY ),
  2588. ( mZ - quat.mZ ),
  2589. ( mW - quat.mW )
  2590. );
  2591. }
  2592. inline const Quat Quat::operator *( float scalar ) const
  2593. {
  2594. return Quat(
  2595. ( mX * scalar ),
  2596. ( mY * scalar ),
  2597. ( mZ * scalar ),
  2598. ( mW * scalar )
  2599. );
  2600. }
  2601. inline Quat & Quat::operator +=( const Quat & quat )
  2602. {
  2603. *this = *this + quat;
  2604. return *this;
  2605. }
  2606. inline Quat & Quat::operator -=( const Quat & quat )
  2607. {
  2608. *this = *this - quat;
  2609. return *this;
  2610. }
  2611. inline Quat & Quat::operator *=( float scalar )
  2612. {
  2613. *this = *this * scalar;
  2614. return *this;
  2615. }
  2616. inline const Quat Quat::operator /( float scalar ) const
  2617. {
  2618. return Quat(
  2619. ( mX / scalar ),
  2620. ( mY / scalar ),
  2621. ( mZ / scalar ),
  2622. ( mW / scalar )
  2623. );
  2624. }
  2625. inline Quat & Quat::operator /=( float scalar )
  2626. {
  2627. *this = *this / scalar;
  2628. return *this;
  2629. }
  2630. inline const Quat Quat::operator -( ) const
  2631. {
  2632. return Quat(
  2633. -mX,
  2634. -mY,
  2635. -mZ,
  2636. -mW
  2637. );
  2638. }
  2639. inline const Quat operator *( float scalar, const Quat & quat )
  2640. {
  2641. return quat * scalar;
  2642. }
  2643. inline float dot( const Quat & quat0, const Quat & quat1 )
  2644. {
  2645. float result;
  2646. result = ( quat0.getX() * quat1.getX() );
  2647. result = ( result + ( quat0.getY() * quat1.getY() ) );
  2648. result = ( result + ( quat0.getZ() * quat1.getZ() ) );
  2649. result = ( result + ( quat0.getW() * quat1.getW() ) );
  2650. return result;
  2651. }
  2652. inline float norm( const Quat & quat )
  2653. {
  2654. float result;
  2655. result = ( quat.getX() * quat.getX() );
  2656. result = ( result + ( quat.getY() * quat.getY() ) );
  2657. result = ( result + ( quat.getZ() * quat.getZ() ) );
  2658. result = ( result + ( quat.getW() * quat.getW() ) );
  2659. return result;
  2660. }
  2661. inline float length( const Quat & quat )
  2662. {
  2663. return sqrtf( norm( quat ) );
  2664. }
  2665. inline const Quat normalize( const Quat & quat )
  2666. {
  2667. float lenSqr, lenInv;
  2668. lenSqr = norm( quat );
  2669. lenInv = ( 1.0f / sqrtf( lenSqr ) );
  2670. return Quat(
  2671. ( quat.getX() * lenInv ),
  2672. ( quat.getY() * lenInv ),
  2673. ( quat.getZ() * lenInv ),
  2674. ( quat.getW() * lenInv )
  2675. );
  2676. }
  2677. inline const Quat Quat::rotation( const Vector3 & unitVec0, const Vector3 & unitVec1 )
  2678. {
  2679. float cosHalfAngleX2, recipCosHalfAngleX2;
  2680. cosHalfAngleX2 = sqrtf( ( 2.0f * ( 1.0f + dot( unitVec0, unitVec1 ) ) ) );
  2681. recipCosHalfAngleX2 = ( 1.0f / cosHalfAngleX2 );
  2682. return Quat( ( cross( unitVec0, unitVec1 ) * recipCosHalfAngleX2 ), ( cosHalfAngleX2 * 0.5f ) );
  2683. }
  2684. inline const Quat Quat::rotation( float radians, const Vector3 & unitVec )
  2685. {
  2686. float s, c, angle;
  2687. angle = ( radians * 0.5f );
  2688. s = sinf( angle );
  2689. c = cosf( angle );
  2690. return Quat( ( unitVec * s ), c );
  2691. }
  2692. inline const Quat Quat::rotationX( float radians )
  2693. {
  2694. float s, c, angle;
  2695. angle = ( radians * 0.5f );
  2696. s = sinf( angle );
  2697. c = cosf( angle );
  2698. return Quat( s, 0.0f, 0.0f, c );
  2699. }
  2700. inline const Quat Quat::rotationY( float radians )
  2701. {
  2702. float s, c, angle;
  2703. angle = ( radians * 0.5f );
  2704. s = sinf( angle );
  2705. c = cosf( angle );
  2706. return Quat( 0.0f, s, 0.0f, c );
  2707. }
  2708. inline const Quat Quat::rotationZ( float radians )
  2709. {
  2710. float s, c, angle;
  2711. angle = ( radians * 0.5f );
  2712. s = sinf( angle );
  2713. c = cosf( angle );
  2714. return Quat( 0.0f, 0.0f, s, c );
  2715. }
  2716. inline const Quat Quat::operator *( const Quat & quat ) const
  2717. {
  2718. return Quat(
  2719. ( ( ( ( mW * quat.mX ) + ( mX * quat.mW ) ) + ( mY * quat.mZ ) ) - ( mZ * quat.mY ) ),
  2720. ( ( ( ( mW * quat.mY ) + ( mY * quat.mW ) ) + ( mZ * quat.mX ) ) - ( mX * quat.mZ ) ),
  2721. ( ( ( ( mW * quat.mZ ) + ( mZ * quat.mW ) ) + ( mX * quat.mY ) ) - ( mY * quat.mX ) ),
  2722. ( ( ( ( mW * quat.mW ) - ( mX * quat.mX ) ) - ( mY * quat.mY ) ) - ( mZ * quat.mZ ) )
  2723. );
  2724. }
  2725. inline Quat & Quat::operator *=( const Quat & quat )
  2726. {
  2727. *this = *this * quat;
  2728. return *this;
  2729. }
  2730. inline const Vector3 rotate( const Quat & quat, const Vector3 & vec )
  2731. {
  2732. float tmpX, tmpY, tmpZ, tmpW;
  2733. tmpX = ( ( ( quat.getW() * vec.getX() ) + ( quat.getY() * vec.getZ() ) ) - ( quat.getZ() * vec.getY() ) );
  2734. tmpY = ( ( ( quat.getW() * vec.getY() ) + ( quat.getZ() * vec.getX() ) ) - ( quat.getX() * vec.getZ() ) );
  2735. tmpZ = ( ( ( quat.getW() * vec.getZ() ) + ( quat.getX() * vec.getY() ) ) - ( quat.getY() * vec.getX() ) );
  2736. tmpW = ( ( ( quat.getX() * vec.getX() ) + ( quat.getY() * vec.getY() ) ) + ( quat.getZ() * vec.getZ() ) );
  2737. return Vector3(
  2738. ( ( ( ( tmpW * quat.getX() ) + ( tmpX * quat.getW() ) ) - ( tmpY * quat.getZ() ) ) + ( tmpZ * quat.getY() ) ),
  2739. ( ( ( ( tmpW * quat.getY() ) + ( tmpY * quat.getW() ) ) - ( tmpZ * quat.getX() ) ) + ( tmpX * quat.getZ() ) ),
  2740. ( ( ( ( tmpW * quat.getZ() ) + ( tmpZ * quat.getW() ) ) - ( tmpX * quat.getY() ) ) + ( tmpY * quat.getX() ) )
  2741. );
  2742. }
  2743. inline const Quat conj( const Quat & quat )
  2744. {
  2745. return Quat( -quat.getX(), -quat.getY(), -quat.getZ(), quat.getW() );
  2746. }
  2747. inline const Quat select( const Quat & quat0, const Quat & quat1, bool select1 )
  2748. {
  2749. return Quat(
  2750. ( select1 )? quat1.getX() : quat0.getX(),
  2751. ( select1 )? quat1.getY() : quat0.getY(),
  2752. ( select1 )? quat1.getZ() : quat0.getZ(),
  2753. ( select1 )? quat1.getW() : quat0.getW()
  2754. );
  2755. }
  2756. #ifdef _VECTORMATH_DEBUG
  2757. inline void print( const Quat & quat )
  2758. {
  2759. printf( "( %f %f %f %f )\n", quat.getX(), quat.getY(), quat.getZ(), quat.getW() );
  2760. }
  2761. inline void print( const Quat & quat, const char * name )
  2762. {
  2763. printf( "%s: ( %f %f %f %f )\n", name, quat.getX(), quat.getY(), quat.getZ(), quat.getW() );
  2764. }
  2765. #endif
  2766. } // namespace Aos
  2767. } // namespace Vectormath
  2768. #endif
  2769. /*
  2770. Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
  2771. All rights reserved.
  2772. Redistribution and use in source and binary forms,
  2773. with or without modification, are permitted provided that the
  2774. following conditions are met:
  2775. * Redistributions of source code must retain the above copyright
  2776. notice, this list of conditions and the following disclaimer.
  2777. * Redistributions in binary form must reproduce the above copyright
  2778. notice, this list of conditions and the following disclaimer in the
  2779. documentation and/or other materials provided with the distribution.
  2780. * Neither the name of the Sony Computer Entertainment Inc nor the names
  2781. of its contributors may be used to endorse or promote products derived
  2782. from this software without specific prior written permission.
  2783. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  2784. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2785. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2786. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  2787. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  2788. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  2789. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  2790. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  2791. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  2792. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  2793. POSSIBILITY OF SUCH DAMAGE.
  2794. */
  2795. namespace Vectormath {
  2796. namespace Aos {
  2797. //-----------------------------------------------------------------------------
  2798. // Constants
  2799. #define _VECTORMATH_PI_OVER_2 1.570796327f
  2800. //-----------------------------------------------------------------------------
  2801. // Definitions
  2802. inline Matrix3::Matrix3( const Matrix3 & mat )
  2803. {
  2804. mCol0 = mat.mCol0;
  2805. mCol1 = mat.mCol1;
  2806. mCol2 = mat.mCol2;
  2807. }
  2808. inline Matrix3::Matrix3( float scalar )
  2809. {
  2810. mCol0 = Vector3( scalar );
  2811. mCol1 = Vector3( scalar );
  2812. mCol2 = Vector3( scalar );
  2813. }
  2814. inline Matrix3::Matrix3( const Quat & unitQuat )
  2815. {
  2816. float qx, qy, qz, qw, qx2, qy2, qz2, qxqx2, qyqy2, qzqz2, qxqy2, qyqz2, qzqw2, qxqz2, qyqw2, qxqw2;
  2817. qx = unitQuat.getX();
  2818. qy = unitQuat.getY();
  2819. qz = unitQuat.getZ();
  2820. qw = unitQuat.getW();
  2821. qx2 = ( qx + qx );
  2822. qy2 = ( qy + qy );
  2823. qz2 = ( qz + qz );
  2824. qxqx2 = ( qx * qx2 );
  2825. qxqy2 = ( qx * qy2 );
  2826. qxqz2 = ( qx * qz2 );
  2827. qxqw2 = ( qw * qx2 );
  2828. qyqy2 = ( qy * qy2 );
  2829. qyqz2 = ( qy * qz2 );
  2830. qyqw2 = ( qw * qy2 );
  2831. qzqz2 = ( qz * qz2 );
  2832. qzqw2 = ( qw * qz2 );
  2833. mCol0 = Vector3( ( ( 1.0f - qyqy2 ) - qzqz2 ), ( qxqy2 + qzqw2 ), ( qxqz2 - qyqw2 ) );
  2834. mCol1 = Vector3( ( qxqy2 - qzqw2 ), ( ( 1.0f - qxqx2 ) - qzqz2 ), ( qyqz2 + qxqw2 ) );
  2835. mCol2 = Vector3( ( qxqz2 + qyqw2 ), ( qyqz2 - qxqw2 ), ( ( 1.0f - qxqx2 ) - qyqy2 ) );
  2836. }
  2837. inline Matrix3::Matrix3( const Vector3 & _col0, const Vector3 & _col1, const Vector3 & _col2 )
  2838. {
  2839. mCol0 = _col0;
  2840. mCol1 = _col1;
  2841. mCol2 = _col2;
  2842. }
  2843. inline Matrix3 & Matrix3::setCol0( const Vector3 & _col0 )
  2844. {
  2845. mCol0 = _col0;
  2846. return *this;
  2847. }
  2848. inline Matrix3 & Matrix3::setCol1( const Vector3 & _col1 )
  2849. {
  2850. mCol1 = _col1;
  2851. return *this;
  2852. }
  2853. inline Matrix3 & Matrix3::setCol2( const Vector3 & _col2 )
  2854. {
  2855. mCol2 = _col2;
  2856. return *this;
  2857. }
  2858. inline Matrix3 & Matrix3::setCol( int col, const Vector3 & vec )
  2859. {
  2860. *(&mCol0 + col) = vec;
  2861. return *this;
  2862. }
  2863. inline Matrix3 & Matrix3::setRow( int row, const Vector3 & vec )
  2864. {
  2865. mCol0.setElem( row, vec.getElem( 0 ) );
  2866. mCol1.setElem( row, vec.getElem( 1 ) );
  2867. mCol2.setElem( row, vec.getElem( 2 ) );
  2868. return *this;
  2869. }
  2870. inline Matrix3 & Matrix3::setElem( int col, int row, float val )
  2871. {
  2872. Vector3 tmpV3_0;
  2873. tmpV3_0 = this->getCol( col );
  2874. tmpV3_0.setElem( row, val );
  2875. this->setCol( col, tmpV3_0 );
  2876. return *this;
  2877. }
  2878. inline float Matrix3::getElem( int col, int row ) const
  2879. {
  2880. return this->getCol( col ).getElem( row );
  2881. }
  2882. inline const Vector3 Matrix3::getCol0( ) const
  2883. {
  2884. return mCol0;
  2885. }
  2886. inline const Vector3 Matrix3::getCol1( ) const
  2887. {
  2888. return mCol1;
  2889. }
  2890. inline const Vector3 Matrix3::getCol2( ) const
  2891. {
  2892. return mCol2;
  2893. }
  2894. inline const Vector3 Matrix3::getCol( int col ) const
  2895. {
  2896. return *(&mCol0 + col);
  2897. }
  2898. inline const Vector3 Matrix3::getRow( int row ) const
  2899. {
  2900. return Vector3( mCol0.getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ) );
  2901. }
  2902. inline Vector3 & Matrix3::operator []( int col )
  2903. {
  2904. return *(&mCol0 + col);
  2905. }
  2906. inline const Vector3 Matrix3::operator []( int col ) const
  2907. {
  2908. return *(&mCol0 + col);
  2909. }
  2910. inline Matrix3 & Matrix3::operator =( const Matrix3 & mat )
  2911. {
  2912. mCol0 = mat.mCol0;
  2913. mCol1 = mat.mCol1;
  2914. mCol2 = mat.mCol2;
  2915. return *this;
  2916. }
  2917. inline const Matrix3 transpose( const Matrix3 & mat )
  2918. {
  2919. return Matrix3(
  2920. Vector3( mat.getCol0().getX(), mat.getCol1().getX(), mat.getCol2().getX() ),
  2921. Vector3( mat.getCol0().getY(), mat.getCol1().getY(), mat.getCol2().getY() ),
  2922. Vector3( mat.getCol0().getZ(), mat.getCol1().getZ(), mat.getCol2().getZ() )
  2923. );
  2924. }
  2925. inline const Matrix3 inverse( const Matrix3 & mat )
  2926. {
  2927. Vector3 tmp0, tmp1, tmp2;
  2928. float detinv;
  2929. tmp0 = cross( mat.getCol1(), mat.getCol2() );
  2930. tmp1 = cross( mat.getCol2(), mat.getCol0() );
  2931. tmp2 = cross( mat.getCol0(), mat.getCol1() );
  2932. detinv = ( 1.0f / dot( mat.getCol2(), tmp2 ) );
  2933. return Matrix3(
  2934. Vector3( ( tmp0.getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) ),
  2935. Vector3( ( tmp0.getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) ),
  2936. Vector3( ( tmp0.getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) )
  2937. );
  2938. }
  2939. inline float determinant( const Matrix3 & mat )
  2940. {
  2941. return dot( mat.getCol2(), cross( mat.getCol0(), mat.getCol1() ) );
  2942. }
  2943. inline const Matrix3 Matrix3::operator +( const Matrix3 & mat ) const
  2944. {
  2945. return Matrix3(
  2946. ( mCol0 + mat.mCol0 ),
  2947. ( mCol1 + mat.mCol1 ),
  2948. ( mCol2 + mat.mCol2 )
  2949. );
  2950. }
  2951. inline const Matrix3 Matrix3::operator -( const Matrix3 & mat ) const
  2952. {
  2953. return Matrix3(
  2954. ( mCol0 - mat.mCol0 ),
  2955. ( mCol1 - mat.mCol1 ),
  2956. ( mCol2 - mat.mCol2 )
  2957. );
  2958. }
  2959. inline Matrix3 & Matrix3::operator +=( const Matrix3 & mat )
  2960. {
  2961. *this = *this + mat;
  2962. return *this;
  2963. }
  2964. inline Matrix3 & Matrix3::operator -=( const Matrix3 & mat )
  2965. {
  2966. *this = *this - mat;
  2967. return *this;
  2968. }
  2969. inline const Matrix3 Matrix3::operator -( ) const
  2970. {
  2971. return Matrix3(
  2972. ( -mCol0 ),
  2973. ( -mCol1 ),
  2974. ( -mCol2 )
  2975. );
  2976. }
  2977. inline const Matrix3 absPerElem( const Matrix3 & mat )
  2978. {
  2979. return Matrix3(
  2980. absPerElem( mat.getCol0() ),
  2981. absPerElem( mat.getCol1() ),
  2982. absPerElem( mat.getCol2() )
  2983. );
  2984. }
  2985. inline const Matrix3 Matrix3::operator *( float scalar ) const
  2986. {
  2987. return Matrix3(
  2988. ( mCol0 * scalar ),
  2989. ( mCol1 * scalar ),
  2990. ( mCol2 * scalar )
  2991. );
  2992. }
  2993. inline Matrix3 & Matrix3::operator *=( float scalar )
  2994. {
  2995. *this = *this * scalar;
  2996. return *this;
  2997. }
  2998. inline const Matrix3 operator *( float scalar, const Matrix3 & mat )
  2999. {
  3000. return mat * scalar;
  3001. }
  3002. inline const Vector3 Matrix3::operator *( const Vector3 & vec ) const
  3003. {
  3004. return Vector3(
  3005. ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
  3006. ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
  3007. ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) )
  3008. );
  3009. }
  3010. inline const Matrix3 Matrix3::operator *( const Matrix3 & mat ) const
  3011. {
  3012. return Matrix3(
  3013. ( *this * mat.mCol0 ),
  3014. ( *this * mat.mCol1 ),
  3015. ( *this * mat.mCol2 )
  3016. );
  3017. }
  3018. inline Matrix3 & Matrix3::operator *=( const Matrix3 & mat )
  3019. {
  3020. *this = *this * mat;
  3021. return *this;
  3022. }
  3023. inline const Matrix3 mulPerElem( const Matrix3 & mat0, const Matrix3 & mat1 )
  3024. {
  3025. return Matrix3(
  3026. mulPerElem( mat0.getCol0(), mat1.getCol0() ),
  3027. mulPerElem( mat0.getCol1(), mat1.getCol1() ),
  3028. mulPerElem( mat0.getCol2(), mat1.getCol2() )
  3029. );
  3030. }
  3031. inline const Matrix3 Matrix3::identity( )
  3032. {
  3033. return Matrix3(
  3034. Vector3::xAxis( ),
  3035. Vector3::yAxis( ),
  3036. Vector3::zAxis( )
  3037. );
  3038. }
  3039. inline const Matrix3 Matrix3::rotationX( float radians )
  3040. {
  3041. float s, c;
  3042. s = sinf( radians );
  3043. c = cosf( radians );
  3044. return Matrix3(
  3045. Vector3::xAxis( ),
  3046. Vector3( 0.0f, c, s ),
  3047. Vector3( 0.0f, -s, c )
  3048. );
  3049. }
  3050. inline const Matrix3 Matrix3::rotationY( float radians )
  3051. {
  3052. float s, c;
  3053. s = sinf( radians );
  3054. c = cosf( radians );
  3055. return Matrix3(
  3056. Vector3( c, 0.0f, -s ),
  3057. Vector3::yAxis( ),
  3058. Vector3( s, 0.0f, c )
  3059. );
  3060. }
  3061. inline const Matrix3 Matrix3::rotationZ( float radians )
  3062. {
  3063. float s, c;
  3064. s = sinf( radians );
  3065. c = cosf( radians );
  3066. return Matrix3(
  3067. Vector3( c, s, 0.0f ),
  3068. Vector3( -s, c, 0.0f ),
  3069. Vector3::zAxis( )
  3070. );
  3071. }
  3072. inline const Matrix3 Matrix3::rotationZYX( const Vector3 & radiansXYZ )
  3073. {
  3074. float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
  3075. sX = sinf( radiansXYZ.getX() );
  3076. cX = cosf( radiansXYZ.getX() );
  3077. sY = sinf( radiansXYZ.getY() );
  3078. cY = cosf( radiansXYZ.getY() );
  3079. sZ = sinf( radiansXYZ.getZ() );
  3080. cZ = cosf( radiansXYZ.getZ() );
  3081. tmp0 = ( cZ * sY );
  3082. tmp1 = ( sZ * sY );
  3083. return Matrix3(
  3084. Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
  3085. Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
  3086. Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) )
  3087. );
  3088. }
  3089. inline const Matrix3 Matrix3::rotation( float radians, const Vector3 & unitVec )
  3090. {
  3091. float x, y, z, s, c, oneMinusC, xy, yz, zx;
  3092. s = sinf( radians );
  3093. c = cosf( radians );
  3094. x = unitVec.getX();
  3095. y = unitVec.getY();
  3096. z = unitVec.getZ();
  3097. xy = ( x * y );
  3098. yz = ( y * z );
  3099. zx = ( z * x );
  3100. oneMinusC = ( 1.0f - c );
  3101. return Matrix3(
  3102. Vector3( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ) ),
  3103. Vector3( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ) ),
  3104. Vector3( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ) )
  3105. );
  3106. }
  3107. inline const Matrix3 Matrix3::rotation( const Quat & unitQuat )
  3108. {
  3109. return Matrix3( unitQuat );
  3110. }
  3111. inline const Matrix3 Matrix3::scale( const Vector3 & scaleVec )
  3112. {
  3113. return Matrix3(
  3114. Vector3( scaleVec.getX(), 0.0f, 0.0f ),
  3115. Vector3( 0.0f, scaleVec.getY(), 0.0f ),
  3116. Vector3( 0.0f, 0.0f, scaleVec.getZ() )
  3117. );
  3118. }
  3119. inline const Matrix3 appendScale( const Matrix3 & mat, const Vector3 & scaleVec )
  3120. {
  3121. return Matrix3(
  3122. ( mat.getCol0() * scaleVec.getX( ) ),
  3123. ( mat.getCol1() * scaleVec.getY( ) ),
  3124. ( mat.getCol2() * scaleVec.getZ( ) )
  3125. );
  3126. }
  3127. inline const Matrix3 prependScale( const Vector3 & scaleVec, const Matrix3 & mat )
  3128. {
  3129. return Matrix3(
  3130. mulPerElem( mat.getCol0(), scaleVec ),
  3131. mulPerElem( mat.getCol1(), scaleVec ),
  3132. mulPerElem( mat.getCol2(), scaleVec )
  3133. );
  3134. }
  3135. inline const Matrix3 select( const Matrix3 & mat0, const Matrix3 & mat1, bool select1 )
  3136. {
  3137. return Matrix3(
  3138. select( mat0.getCol0(), mat1.getCol0(), select1 ),
  3139. select( mat0.getCol1(), mat1.getCol1(), select1 ),
  3140. select( mat0.getCol2(), mat1.getCol2(), select1 )
  3141. );
  3142. }
  3143. #ifdef _VECTORMATH_DEBUG
  3144. inline void print( const Matrix3 & mat )
  3145. {
  3146. print( mat.getRow( 0 ) );
  3147. print( mat.getRow( 1 ) );
  3148. print( mat.getRow( 2 ) );
  3149. }
  3150. inline void print( const Matrix3 & mat, const char * name )
  3151. {
  3152. printf("%s:\n", name);
  3153. print( mat );
  3154. }
  3155. #endif
  3156. inline Matrix4::Matrix4( const Matrix4 & mat )
  3157. {
  3158. mCol0 = mat.mCol0;
  3159. mCol1 = mat.mCol1;
  3160. mCol2 = mat.mCol2;
  3161. mCol3 = mat.mCol3;
  3162. }
  3163. inline Matrix4::Matrix4( float scalar )
  3164. {
  3165. mCol0 = Vector4( scalar );
  3166. mCol1 = Vector4( scalar );
  3167. mCol2 = Vector4( scalar );
  3168. mCol3 = Vector4( scalar );
  3169. }
  3170. inline Matrix4::Matrix4( const Transform3 & mat )
  3171. {
  3172. mCol0 = Vector4( mat.getCol0(), 0.0f );
  3173. mCol1 = Vector4( mat.getCol1(), 0.0f );
  3174. mCol2 = Vector4( mat.getCol2(), 0.0f );
  3175. mCol3 = Vector4( mat.getCol3(), 1.0f );
  3176. }
  3177. inline Matrix4::Matrix4( const Vector4 & _col0, const Vector4 & _col1, const Vector4 & _col2, const Vector4 & _col3 )
  3178. {
  3179. mCol0 = _col0;
  3180. mCol1 = _col1;
  3181. mCol2 = _col2;
  3182. mCol3 = _col3;
  3183. }
  3184. inline Matrix4::Matrix4( const Matrix3 & mat, const Vector3 & translateVec )
  3185. {
  3186. mCol0 = Vector4( mat.getCol0(), 0.0f );
  3187. mCol1 = Vector4( mat.getCol1(), 0.0f );
  3188. mCol2 = Vector4( mat.getCol2(), 0.0f );
  3189. mCol3 = Vector4( translateVec, 1.0f );
  3190. }
  3191. inline Matrix4::Matrix4( const Quat & unitQuat, const Vector3 & translateVec )
  3192. {
  3193. Matrix3 mat;
  3194. mat = Matrix3( unitQuat );
  3195. mCol0 = Vector4( mat.getCol0(), 0.0f );
  3196. mCol1 = Vector4( mat.getCol1(), 0.0f );
  3197. mCol2 = Vector4( mat.getCol2(), 0.0f );
  3198. mCol3 = Vector4( translateVec, 1.0f );
  3199. }
  3200. inline Matrix4 & Matrix4::setCol0( const Vector4 & _col0 )
  3201. {
  3202. mCol0 = _col0;
  3203. return *this;
  3204. }
  3205. inline Matrix4 & Matrix4::setCol1( const Vector4 & _col1 )
  3206. {
  3207. mCol1 = _col1;
  3208. return *this;
  3209. }
  3210. inline Matrix4 & Matrix4::setCol2( const Vector4 & _col2 )
  3211. {
  3212. mCol2 = _col2;
  3213. return *this;
  3214. }
  3215. inline Matrix4 & Matrix4::setCol3( const Vector4 & _col3 )
  3216. {
  3217. mCol3 = _col3;
  3218. return *this;
  3219. }
  3220. inline Matrix4 & Matrix4::setCol( int col, const Vector4 & vec )
  3221. {
  3222. *(&mCol0 + col) = vec;
  3223. return *this;
  3224. }
  3225. inline Matrix4 & Matrix4::setRow( int row, const Vector4 & vec )
  3226. {
  3227. mCol0.setElem( row, vec.getElem( 0 ) );
  3228. mCol1.setElem( row, vec.getElem( 1 ) );
  3229. mCol2.setElem( row, vec.getElem( 2 ) );
  3230. mCol3.setElem( row, vec.getElem( 3 ) );
  3231. return *this;
  3232. }
  3233. inline Matrix4 & Matrix4::setElem( int col, int row, float val )
  3234. {
  3235. Vector4 tmpV3_0;
  3236. tmpV3_0 = this->getCol( col );
  3237. tmpV3_0.setElem( row, val );
  3238. this->setCol( col, tmpV3_0 );
  3239. return *this;
  3240. }
  3241. inline float Matrix4::getElem( int col, int row ) const
  3242. {
  3243. return this->getCol( col ).getElem( row );
  3244. }
  3245. inline const Vector4 Matrix4::getCol0( ) const
  3246. {
  3247. return mCol0;
  3248. }
  3249. inline const Vector4 Matrix4::getCol1( ) const
  3250. {
  3251. return mCol1;
  3252. }
  3253. inline const Vector4 Matrix4::getCol2( ) const
  3254. {
  3255. return mCol2;
  3256. }
  3257. inline const Vector4 Matrix4::getCol3( ) const
  3258. {
  3259. return mCol3;
  3260. }
  3261. inline const Vector4 Matrix4::getCol( int col ) const
  3262. {
  3263. return *(&mCol0 + col);
  3264. }
  3265. inline const Vector4 Matrix4::getRow( int row ) const
  3266. {
  3267. return Vector4( mCol0.getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ), mCol3.getElem( row ) );
  3268. }
  3269. inline Vector4 & Matrix4::operator []( int col )
  3270. {
  3271. return *(&mCol0 + col);
  3272. }
  3273. inline const Vector4 Matrix4::operator []( int col ) const
  3274. {
  3275. return *(&mCol0 + col);
  3276. }
  3277. inline Matrix4 & Matrix4::operator =( const Matrix4 & mat )
  3278. {
  3279. mCol0 = mat.mCol0;
  3280. mCol1 = mat.mCol1;
  3281. mCol2 = mat.mCol2;
  3282. mCol3 = mat.mCol3;
  3283. return *this;
  3284. }
  3285. inline const Matrix4 transpose( const Matrix4 & mat )
  3286. {
  3287. return Matrix4(
  3288. Vector4( mat.getCol0().getX(), mat.getCol1().getX(), mat.getCol2().getX(), mat.getCol3().getX() ),
  3289. Vector4( mat.getCol0().getY(), mat.getCol1().getY(), mat.getCol2().getY(), mat.getCol3().getY() ),
  3290. Vector4( mat.getCol0().getZ(), mat.getCol1().getZ(), mat.getCol2().getZ(), mat.getCol3().getZ() ),
  3291. Vector4( mat.getCol0().getW(), mat.getCol1().getW(), mat.getCol2().getW(), mat.getCol3().getW() )
  3292. );
  3293. }
  3294. inline const Matrix4 inverse( const Matrix4 & mat )
  3295. {
  3296. Vector4 res0, res1, res2, res3;
  3297. float mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, detInv;
  3298. mA = mat.getCol0().getX();
  3299. mB = mat.getCol0().getY();
  3300. mC = mat.getCol0().getZ();
  3301. mD = mat.getCol0().getW();
  3302. mE = mat.getCol1().getX();
  3303. mF = mat.getCol1().getY();
  3304. mG = mat.getCol1().getZ();
  3305. mH = mat.getCol1().getW();
  3306. mI = mat.getCol2().getX();
  3307. mJ = mat.getCol2().getY();
  3308. mK = mat.getCol2().getZ();
  3309. mL = mat.getCol2().getW();
  3310. mM = mat.getCol3().getX();
  3311. mN = mat.getCol3().getY();
  3312. mO = mat.getCol3().getZ();
  3313. mP = mat.getCol3().getW();
  3314. tmp0 = ( ( mK * mD ) - ( mC * mL ) );
  3315. tmp1 = ( ( mO * mH ) - ( mG * mP ) );
  3316. tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
  3317. tmp3 = ( ( mF * mO ) - ( mN * mG ) );
  3318. tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
  3319. tmp5 = ( ( mN * mH ) - ( mF * mP ) );
  3320. res0.setX( ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) ) );
  3321. res0.setY( ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) ) );
  3322. res0.setZ( ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) ) );
  3323. res0.setW( ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) ) );
  3324. detInv = ( 1.0f / ( ( ( ( mA * res0.getX() ) + ( mE * res0.getY() ) ) + ( mI * res0.getZ() ) ) + ( mM * res0.getW() ) ) );
  3325. res1.setX( ( mI * tmp1 ) );
  3326. res1.setY( ( mM * tmp0 ) );
  3327. res1.setZ( ( mA * tmp1 ) );
  3328. res1.setW( ( mE * tmp0 ) );
  3329. res3.setX( ( mI * tmp3 ) );
  3330. res3.setY( ( mM * tmp2 ) );
  3331. res3.setZ( ( mA * tmp3 ) );
  3332. res3.setW( ( mE * tmp2 ) );
  3333. res2.setX( ( mI * tmp5 ) );
  3334. res2.setY( ( mM * tmp4 ) );
  3335. res2.setZ( ( mA * tmp5 ) );
  3336. res2.setW( ( mE * tmp4 ) );
  3337. tmp0 = ( ( mI * mB ) - ( mA * mJ ) );
  3338. tmp1 = ( ( mM * mF ) - ( mE * mN ) );
  3339. tmp2 = ( ( mI * mD ) - ( mA * mL ) );
  3340. tmp3 = ( ( mM * mH ) - ( mE * mP ) );
  3341. tmp4 = ( ( mI * mC ) - ( mA * mK ) );
  3342. tmp5 = ( ( mM * mG ) - ( mE * mO ) );
  3343. res2.setX( ( ( ( mL * tmp1 ) - ( mJ * tmp3 ) ) + res2.getX() ) );
  3344. res2.setY( ( ( ( mP * tmp0 ) - ( mN * tmp2 ) ) + res2.getY() ) );
  3345. res2.setZ( ( ( ( mB * tmp3 ) - ( mD * tmp1 ) ) - res2.getZ() ) );
  3346. res2.setW( ( ( ( mF * tmp2 ) - ( mH * tmp0 ) ) - res2.getW() ) );
  3347. res3.setX( ( ( ( mJ * tmp5 ) - ( mK * tmp1 ) ) + res3.getX() ) );
  3348. res3.setY( ( ( ( mN * tmp4 ) - ( mO * tmp0 ) ) + res3.getY() ) );
  3349. res3.setZ( ( ( ( mC * tmp1 ) - ( mB * tmp5 ) ) - res3.getZ() ) );
  3350. res3.setW( ( ( ( mG * tmp0 ) - ( mF * tmp4 ) ) - res3.getW() ) );
  3351. res1.setX( ( ( ( mK * tmp3 ) - ( mL * tmp5 ) ) - res1.getX() ) );
  3352. res1.setY( ( ( ( mO * tmp2 ) - ( mP * tmp4 ) ) - res1.getY() ) );
  3353. res1.setZ( ( ( ( mD * tmp5 ) - ( mC * tmp3 ) ) + res1.getZ() ) );
  3354. res1.setW( ( ( ( mH * tmp4 ) - ( mG * tmp2 ) ) + res1.getW() ) );
  3355. return Matrix4(
  3356. ( res0 * detInv ),
  3357. ( res1 * detInv ),
  3358. ( res2 * detInv ),
  3359. ( res3 * detInv )
  3360. );
  3361. }
  3362. inline const Matrix4 affineInverse( const Matrix4 & mat )
  3363. {
  3364. Transform3 affineMat;
  3365. affineMat.setCol0( mat.getCol0().getXYZ( ) );
  3366. affineMat.setCol1( mat.getCol1().getXYZ( ) );
  3367. affineMat.setCol2( mat.getCol2().getXYZ( ) );
  3368. affineMat.setCol3( mat.getCol3().getXYZ( ) );
  3369. return Matrix4( inverse( affineMat ) );
  3370. }
  3371. inline const Matrix4 orthoInverse( const Matrix4 & mat )
  3372. {
  3373. Transform3 affineMat;
  3374. affineMat.setCol0( mat.getCol0().getXYZ( ) );
  3375. affineMat.setCol1( mat.getCol1().getXYZ( ) );
  3376. affineMat.setCol2( mat.getCol2().getXYZ( ) );
  3377. affineMat.setCol3( mat.getCol3().getXYZ( ) );
  3378. return Matrix4( orthoInverse( affineMat ) );
  3379. }
  3380. inline float determinant( const Matrix4 & mat )
  3381. {
  3382. float dx, dy, dz, dw, mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
  3383. mA = mat.getCol0().getX();
  3384. mB = mat.getCol0().getY();
  3385. mC = mat.getCol0().getZ();
  3386. mD = mat.getCol0().getW();
  3387. mE = mat.getCol1().getX();
  3388. mF = mat.getCol1().getY();
  3389. mG = mat.getCol1().getZ();
  3390. mH = mat.getCol1().getW();
  3391. mI = mat.getCol2().getX();
  3392. mJ = mat.getCol2().getY();
  3393. mK = mat.getCol2().getZ();
  3394. mL = mat.getCol2().getW();
  3395. mM = mat.getCol3().getX();
  3396. mN = mat.getCol3().getY();
  3397. mO = mat.getCol3().getZ();
  3398. mP = mat.getCol3().getW();
  3399. tmp0 = ( ( mK * mD ) - ( mC * mL ) );
  3400. tmp1 = ( ( mO * mH ) - ( mG * mP ) );
  3401. tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
  3402. tmp3 = ( ( mF * mO ) - ( mN * mG ) );
  3403. tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
  3404. tmp5 = ( ( mN * mH ) - ( mF * mP ) );
  3405. dx = ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) );
  3406. dy = ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) );
  3407. dz = ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) );
  3408. dw = ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) );
  3409. return ( ( ( ( mA * dx ) + ( mE * dy ) ) + ( mI * dz ) ) + ( mM * dw ) );
  3410. }
  3411. inline const Matrix4 Matrix4::operator +( const Matrix4 & mat ) const
  3412. {
  3413. return Matrix4(
  3414. ( mCol0 + mat.mCol0 ),
  3415. ( mCol1 + mat.mCol1 ),
  3416. ( mCol2 + mat.mCol2 ),
  3417. ( mCol3 + mat.mCol3 )
  3418. );
  3419. }
  3420. inline const Matrix4 Matrix4::operator -( const Matrix4 & mat ) const
  3421. {
  3422. return Matrix4(
  3423. ( mCol0 - mat.mCol0 ),
  3424. ( mCol1 - mat.mCol1 ),
  3425. ( mCol2 - mat.mCol2 ),
  3426. ( mCol3 - mat.mCol3 )
  3427. );
  3428. }
  3429. inline Matrix4 & Matrix4::operator +=( const Matrix4 & mat )
  3430. {
  3431. *this = *this + mat;
  3432. return *this;
  3433. }
  3434. inline Matrix4 & Matrix4::operator -=( const Matrix4 & mat )
  3435. {
  3436. *this = *this - mat;
  3437. return *this;
  3438. }
  3439. inline const Matrix4 Matrix4::operator -( ) const
  3440. {
  3441. return Matrix4(
  3442. ( -mCol0 ),
  3443. ( -mCol1 ),
  3444. ( -mCol2 ),
  3445. ( -mCol3 )
  3446. );
  3447. }
  3448. inline const Matrix4 absPerElem( const Matrix4 & mat )
  3449. {
  3450. return Matrix4(
  3451. absPerElem( mat.getCol0() ),
  3452. absPerElem( mat.getCol1() ),
  3453. absPerElem( mat.getCol2() ),
  3454. absPerElem( mat.getCol3() )
  3455. );
  3456. }
  3457. inline const Matrix4 Matrix4::operator *( float scalar ) const
  3458. {
  3459. return Matrix4(
  3460. ( mCol0 * scalar ),
  3461. ( mCol1 * scalar ),
  3462. ( mCol2 * scalar ),
  3463. ( mCol3 * scalar )
  3464. );
  3465. }
  3466. inline Matrix4 & Matrix4::operator *=( float scalar )
  3467. {
  3468. *this = *this * scalar;
  3469. return *this;
  3470. }
  3471. inline const Matrix4 operator *( float scalar, const Matrix4 & mat )
  3472. {
  3473. return mat * scalar;
  3474. }
  3475. inline const Vector4 Matrix4::operator *( const Vector4 & vec ) const
  3476. {
  3477. return Vector4(
  3478. ( ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ) + ( mCol3.getX() * vec.getW() ) ),
  3479. ( ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ) + ( mCol3.getY() * vec.getW() ) ),
  3480. ( ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) ) + ( mCol3.getZ() * vec.getW() ) ),
  3481. ( ( ( ( mCol0.getW() * vec.getX() ) + ( mCol1.getW() * vec.getY() ) ) + ( mCol2.getW() * vec.getZ() ) ) + ( mCol3.getW() * vec.getW() ) )
  3482. );
  3483. }
  3484. inline const Vector4 Matrix4::operator *( const Vector3 & vec ) const
  3485. {
  3486. return Vector4(
  3487. ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
  3488. ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
  3489. ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) ),
  3490. ( ( ( mCol0.getW() * vec.getX() ) + ( mCol1.getW() * vec.getY() ) ) + ( mCol2.getW() * vec.getZ() ) )
  3491. );
  3492. }
  3493. inline const Vector4 Matrix4::operator *( const Point3 & pnt ) const
  3494. {
  3495. return Vector4(
  3496. ( ( ( ( mCol0.getX() * pnt.getX() ) + ( mCol1.getX() * pnt.getY() ) ) + ( mCol2.getX() * pnt.getZ() ) ) + mCol3.getX() ),
  3497. ( ( ( ( mCol0.getY() * pnt.getX() ) + ( mCol1.getY() * pnt.getY() ) ) + ( mCol2.getY() * pnt.getZ() ) ) + mCol3.getY() ),
  3498. ( ( ( ( mCol0.getZ() * pnt.getX() ) + ( mCol1.getZ() * pnt.getY() ) ) + ( mCol2.getZ() * pnt.getZ() ) ) + mCol3.getZ() ),
  3499. ( ( ( ( mCol0.getW() * pnt.getX() ) + ( mCol1.getW() * pnt.getY() ) ) + ( mCol2.getW() * pnt.getZ() ) ) + mCol3.getW() )
  3500. );
  3501. }
  3502. inline const Matrix4 Matrix4::operator *( const Matrix4 & mat ) const
  3503. {
  3504. return Matrix4(
  3505. ( *this * mat.mCol0 ),
  3506. ( *this * mat.mCol1 ),
  3507. ( *this * mat.mCol2 ),
  3508. ( *this * mat.mCol3 )
  3509. );
  3510. }
  3511. inline Matrix4 & Matrix4::operator *=( const Matrix4 & mat )
  3512. {
  3513. *this = *this * mat;
  3514. return *this;
  3515. }
  3516. inline const Matrix4 Matrix4::operator *( const Transform3 & tfrm ) const
  3517. {
  3518. return Matrix4(
  3519. ( *this * tfrm.getCol0() ),
  3520. ( *this * tfrm.getCol1() ),
  3521. ( *this * tfrm.getCol2() ),
  3522. ( *this * Point3( tfrm.getCol3() ) )
  3523. );
  3524. }
  3525. inline Matrix4 & Matrix4::operator *=( const Transform3 & tfrm )
  3526. {
  3527. *this = *this * tfrm;
  3528. return *this;
  3529. }
  3530. inline const Matrix4 mulPerElem( const Matrix4 & mat0, const Matrix4 & mat1 )
  3531. {
  3532. return Matrix4(
  3533. mulPerElem( mat0.getCol0(), mat1.getCol0() ),
  3534. mulPerElem( mat0.getCol1(), mat1.getCol1() ),
  3535. mulPerElem( mat0.getCol2(), mat1.getCol2() ),
  3536. mulPerElem( mat0.getCol3(), mat1.getCol3() )
  3537. );
  3538. }
  3539. inline const Matrix4 Matrix4::identity( )
  3540. {
  3541. return Matrix4(
  3542. Vector4::xAxis( ),
  3543. Vector4::yAxis( ),
  3544. Vector4::zAxis( ),
  3545. Vector4::wAxis( )
  3546. );
  3547. }
  3548. inline Matrix4 & Matrix4::setUpper3x3( const Matrix3 & mat3 )
  3549. {
  3550. mCol0.setXYZ( mat3.getCol0() );
  3551. mCol1.setXYZ( mat3.getCol1() );
  3552. mCol2.setXYZ( mat3.getCol2() );
  3553. return *this;
  3554. }
  3555. inline const Matrix3 Matrix4::getUpper3x3( ) const
  3556. {
  3557. return Matrix3(
  3558. mCol0.getXYZ( ),
  3559. mCol1.getXYZ( ),
  3560. mCol2.getXYZ( )
  3561. );
  3562. }
  3563. inline Matrix4 & Matrix4::setTranslation( const Vector3 & translateVec )
  3564. {
  3565. mCol3.setXYZ( translateVec );
  3566. return *this;
  3567. }
  3568. inline const Vector3 Matrix4::getTranslation( ) const
  3569. {
  3570. return mCol3.getXYZ( );
  3571. }
  3572. inline const Matrix4 Matrix4::rotationX( float radians )
  3573. {
  3574. float s, c;
  3575. s = sinf( radians );
  3576. c = cosf( radians );
  3577. return Matrix4(
  3578. Vector4::xAxis( ),
  3579. Vector4( 0.0f, c, s, 0.0f ),
  3580. Vector4( 0.0f, -s, c, 0.0f ),
  3581. Vector4::wAxis( )
  3582. );
  3583. }
  3584. inline const Matrix4 Matrix4::rotationY( float radians )
  3585. {
  3586. float s, c;
  3587. s = sinf( radians );
  3588. c = cosf( radians );
  3589. return Matrix4(
  3590. Vector4( c, 0.0f, -s, 0.0f ),
  3591. Vector4::yAxis( ),
  3592. Vector4( s, 0.0f, c, 0.0f ),
  3593. Vector4::wAxis( )
  3594. );
  3595. }
  3596. inline const Matrix4 Matrix4::rotationZ( float radians )
  3597. {
  3598. float s, c;
  3599. s = sinf( radians );
  3600. c = cosf( radians );
  3601. return Matrix4(
  3602. Vector4( c, s, 0.0f, 0.0f ),
  3603. Vector4( -s, c, 0.0f, 0.0f ),
  3604. Vector4::zAxis( ),
  3605. Vector4::wAxis( )
  3606. );
  3607. }
  3608. inline const Matrix4 Matrix4::rotationZYX( const Vector3 & radiansXYZ )
  3609. {
  3610. float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
  3611. sX = sinf( radiansXYZ.getX() );
  3612. cX = cosf( radiansXYZ.getX() );
  3613. sY = sinf( radiansXYZ.getY() );
  3614. cY = cosf( radiansXYZ.getY() );
  3615. sZ = sinf( radiansXYZ.getZ() );
  3616. cZ = cosf( radiansXYZ.getZ() );
  3617. tmp0 = ( cZ * sY );
  3618. tmp1 = ( sZ * sY );
  3619. return Matrix4(
  3620. Vector4( ( cZ * cY ), ( sZ * cY ), -sY, 0.0f ),
  3621. Vector4( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ), 0.0f ),
  3622. Vector4( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ), 0.0f ),
  3623. Vector4::wAxis( )
  3624. );
  3625. }
  3626. inline const Matrix4 Matrix4::rotation( float radians, const Vector3 & unitVec )
  3627. {
  3628. float x, y, z, s, c, oneMinusC, xy, yz, zx;
  3629. s = sinf( radians );
  3630. c = cosf( radians );
  3631. x = unitVec.getX();
  3632. y = unitVec.getY();
  3633. z = unitVec.getZ();
  3634. xy = ( x * y );
  3635. yz = ( y * z );
  3636. zx = ( z * x );
  3637. oneMinusC = ( 1.0f - c );
  3638. return Matrix4(
  3639. Vector4( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ), 0.0f ),
  3640. Vector4( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ), 0.0f ),
  3641. Vector4( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ), 0.0f ),
  3642. Vector4::wAxis( )
  3643. );
  3644. }
  3645. inline const Matrix4 Matrix4::rotation( const Quat & unitQuat )
  3646. {
  3647. return Matrix4( Transform3::rotation( unitQuat ) );
  3648. }
  3649. inline const Matrix4 Matrix4::scale( const Vector3 & scaleVec )
  3650. {
  3651. return Matrix4(
  3652. Vector4( scaleVec.getX(), 0.0f, 0.0f, 0.0f ),
  3653. Vector4( 0.0f, scaleVec.getY(), 0.0f, 0.0f ),
  3654. Vector4( 0.0f, 0.0f, scaleVec.getZ(), 0.0f ),
  3655. Vector4::wAxis( )
  3656. );
  3657. }
  3658. inline const Matrix4 appendScale( const Matrix4 & mat, const Vector3 & scaleVec )
  3659. {
  3660. return Matrix4(
  3661. ( mat.getCol0() * scaleVec.getX( ) ),
  3662. ( mat.getCol1() * scaleVec.getY( ) ),
  3663. ( mat.getCol2() * scaleVec.getZ( ) ),
  3664. mat.getCol3()
  3665. );
  3666. }
  3667. inline const Matrix4 prependScale( const Vector3 & scaleVec, const Matrix4 & mat )
  3668. {
  3669. Vector4 scale4;
  3670. scale4 = Vector4( scaleVec, 1.0f );
  3671. return Matrix4(
  3672. mulPerElem( mat.getCol0(), scale4 ),
  3673. mulPerElem( mat.getCol1(), scale4 ),
  3674. mulPerElem( mat.getCol2(), scale4 ),
  3675. mulPerElem( mat.getCol3(), scale4 )
  3676. );
  3677. }
  3678. inline const Matrix4 Matrix4::translation( const Vector3 & translateVec )
  3679. {
  3680. return Matrix4(
  3681. Vector4::xAxis( ),
  3682. Vector4::yAxis( ),
  3683. Vector4::zAxis( ),
  3684. Vector4( translateVec, 1.0f )
  3685. );
  3686. }
  3687. inline const Matrix4 Matrix4::lookAt( const Point3 & eyePos, const Point3 & lookAtPos, const Vector3 & upVec )
  3688. {
  3689. Matrix4 m4EyeFrame;
  3690. Vector3 v3X, v3Y, v3Z;
  3691. v3Y = normalize( upVec );
  3692. v3Z = normalize( ( eyePos - lookAtPos ) );
  3693. v3X = normalize( cross( v3Y, v3Z ) );
  3694. v3Y = cross( v3Z, v3X );
  3695. m4EyeFrame = Matrix4( Vector4( v3X ), Vector4( v3Y ), Vector4( v3Z ), Vector4( eyePos ) );
  3696. return orthoInverse( m4EyeFrame );
  3697. }
  3698. inline const Matrix4 Matrix4::perspective( float fovyRadians, float aspect, float zNear, float zFar )
  3699. {
  3700. float f, rangeInv;
  3701. f = tanf( ( (float)( _VECTORMATH_PI_OVER_2 ) - ( 0.5f * fovyRadians ) ) );
  3702. rangeInv = ( 1.0f / ( zNear - zFar ) );
  3703. return Matrix4(
  3704. Vector4( ( f / aspect ), 0.0f, 0.0f, 0.0f ),
  3705. Vector4( 0.0f, f, 0.0f, 0.0f ),
  3706. Vector4( 0.0f, 0.0f, ( ( zNear + zFar ) * rangeInv ), -1.0f ),
  3707. Vector4( 0.0f, 0.0f, ( ( ( zNear * zFar ) * rangeInv ) * 2.0f ), 0.0f )
  3708. );
  3709. }
  3710. inline const Matrix4 Matrix4::frustum( float left, float right, float bottom, float top, float zNear, float zFar )
  3711. {
  3712. float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf, n2;
  3713. sum_rl = ( right + left );
  3714. sum_tb = ( top + bottom );
  3715. sum_nf = ( zNear + zFar );
  3716. inv_rl = ( 1.0f / ( right - left ) );
  3717. inv_tb = ( 1.0f / ( top - bottom ) );
  3718. inv_nf = ( 1.0f / ( zNear - zFar ) );
  3719. n2 = ( zNear + zNear );
  3720. return Matrix4(
  3721. Vector4( ( n2 * inv_rl ), 0.0f, 0.0f, 0.0f ),
  3722. Vector4( 0.0f, ( n2 * inv_tb ), 0.0f, 0.0f ),
  3723. Vector4( ( sum_rl * inv_rl ), ( sum_tb * inv_tb ), ( sum_nf * inv_nf ), -1.0f ),
  3724. Vector4( 0.0f, 0.0f, ( ( n2 * inv_nf ) * zFar ), 0.0f )
  3725. );
  3726. }
  3727. inline const Matrix4 Matrix4::orthographic( float left, float right, float bottom, float top, float zNear, float zFar )
  3728. {
  3729. float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf;
  3730. sum_rl = ( right + left );
  3731. sum_tb = ( top + bottom );
  3732. sum_nf = ( zNear + zFar );
  3733. inv_rl = ( 1.0f / ( right - left ) );
  3734. inv_tb = ( 1.0f / ( top - bottom ) );
  3735. inv_nf = ( 1.0f / ( zNear - zFar ) );
  3736. return Matrix4(
  3737. Vector4( ( inv_rl + inv_rl ), 0.0f, 0.0f, 0.0f ),
  3738. Vector4( 0.0f, ( inv_tb + inv_tb ), 0.0f, 0.0f ),
  3739. Vector4( 0.0f, 0.0f, ( inv_nf + inv_nf ), 0.0f ),
  3740. Vector4( ( -sum_rl * inv_rl ), ( -sum_tb * inv_tb ), ( sum_nf * inv_nf ), 1.0f )
  3741. );
  3742. }
  3743. inline const Matrix4 select( const Matrix4 & mat0, const Matrix4 & mat1, bool select1 )
  3744. {
  3745. return Matrix4(
  3746. select( mat0.getCol0(), mat1.getCol0(), select1 ),
  3747. select( mat0.getCol1(), mat1.getCol1(), select1 ),
  3748. select( mat0.getCol2(), mat1.getCol2(), select1 ),
  3749. select( mat0.getCol3(), mat1.getCol3(), select1 )
  3750. );
  3751. }
  3752. #ifdef _VECTORMATH_DEBUG
  3753. inline void print( const Matrix4 & mat )
  3754. {
  3755. print( mat.getRow( 0 ) );
  3756. print( mat.getRow( 1 ) );
  3757. print( mat.getRow( 2 ) );
  3758. print( mat.getRow( 3 ) );
  3759. }
  3760. inline void print( const Matrix4 & mat, const char * name )
  3761. {
  3762. printf("%s:\n", name);
  3763. print( mat );
  3764. }
  3765. #endif
  3766. inline Transform3::Transform3( const Transform3 & tfrm )
  3767. {
  3768. mCol0 = tfrm.mCol0;
  3769. mCol1 = tfrm.mCol1;
  3770. mCol2 = tfrm.mCol2;
  3771. mCol3 = tfrm.mCol3;
  3772. }
  3773. inline Transform3::Transform3( float scalar )
  3774. {
  3775. mCol0 = Vector3( scalar );
  3776. mCol1 = Vector3( scalar );
  3777. mCol2 = Vector3( scalar );
  3778. mCol3 = Vector3( scalar );
  3779. }
  3780. inline Transform3::Transform3( const Vector3 & _col0, const Vector3 & _col1, const Vector3 & _col2, const Vector3 & _col3 )
  3781. {
  3782. mCol0 = _col0;
  3783. mCol1 = _col1;
  3784. mCol2 = _col2;
  3785. mCol3 = _col3;
  3786. }
  3787. inline Transform3::Transform3( const Matrix3 & tfrm, const Vector3 & translateVec )
  3788. {
  3789. this->setUpper3x3( tfrm );
  3790. this->setTranslation( translateVec );
  3791. }
  3792. inline Transform3::Transform3( const Quat & unitQuat, const Vector3 & translateVec )
  3793. {
  3794. this->setUpper3x3( Matrix3( unitQuat ) );
  3795. this->setTranslation( translateVec );
  3796. }
  3797. inline Transform3 & Transform3::setCol0( const Vector3 & _col0 )
  3798. {
  3799. mCol0 = _col0;
  3800. return *this;
  3801. }
  3802. inline Transform3 & Transform3::setCol1( const Vector3 & _col1 )
  3803. {
  3804. mCol1 = _col1;
  3805. return *this;
  3806. }
  3807. inline Transform3 & Transform3::setCol2( const Vector3 & _col2 )
  3808. {
  3809. mCol2 = _col2;
  3810. return *this;
  3811. }
  3812. inline Transform3 & Transform3::setCol3( const Vector3 & _col3 )
  3813. {
  3814. mCol3 = _col3;
  3815. return *this;
  3816. }
  3817. inline Transform3 & Transform3::setCol( int col, const Vector3 & vec )
  3818. {
  3819. *(&mCol0 + col) = vec;
  3820. return *this;
  3821. }
  3822. inline Transform3 & Transform3::setRow( int row, const Vector4 & vec )
  3823. {
  3824. mCol0.setElem( row, vec.getElem( 0 ) );
  3825. mCol1.setElem( row, vec.getElem( 1 ) );
  3826. mCol2.setElem( row, vec.getElem( 2 ) );
  3827. mCol3.setElem( row, vec.getElem( 3 ) );
  3828. return *this;
  3829. }
  3830. inline Transform3 & Transform3::setElem( int col, int row, float val )
  3831. {
  3832. Vector3 tmpV3_0;
  3833. tmpV3_0 = this->getCol( col );
  3834. tmpV3_0.setElem( row, val );
  3835. this->setCol( col, tmpV3_0 );
  3836. return *this;
  3837. }
  3838. inline float Transform3::getElem( int col, int row ) const
  3839. {
  3840. return this->getCol( col ).getElem( row );
  3841. }
  3842. inline const Vector3 Transform3::getCol0( ) const
  3843. {
  3844. return mCol0;
  3845. }
  3846. inline const Vector3 Transform3::getCol1( ) const
  3847. {
  3848. return mCol1;
  3849. }
  3850. inline const Vector3 Transform3::getCol2( ) const
  3851. {
  3852. return mCol2;
  3853. }
  3854. inline const Vector3 Transform3::getCol3( ) const
  3855. {
  3856. return mCol3;
  3857. }
  3858. inline const Vector3 Transform3::getCol( int col ) const
  3859. {
  3860. return *(&mCol0 + col);
  3861. }
  3862. inline const Vector4 Transform3::getRow( int row ) const
  3863. {
  3864. return Vector4( mCol0.getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ), mCol3.getElem( row ) );
  3865. }
  3866. inline Vector3 & Transform3::operator []( int col )
  3867. {
  3868. return *(&mCol0 + col);
  3869. }
  3870. inline const Vector3 Transform3::operator []( int col ) const
  3871. {
  3872. return *(&mCol0 + col);
  3873. }
  3874. inline Transform3 & Transform3::operator =( const Transform3 & tfrm )
  3875. {
  3876. mCol0 = tfrm.mCol0;
  3877. mCol1 = tfrm.mCol1;
  3878. mCol2 = tfrm.mCol2;
  3879. mCol3 = tfrm.mCol3;
  3880. return *this;
  3881. }
  3882. inline const Transform3 inverse( const Transform3 & tfrm )
  3883. {
  3884. Vector3 tmp0, tmp1, tmp2, inv0, inv1, inv2;
  3885. float detinv;
  3886. tmp0 = cross( tfrm.getCol1(), tfrm.getCol2() );
  3887. tmp1 = cross( tfrm.getCol2(), tfrm.getCol0() );
  3888. tmp2 = cross( tfrm.getCol0(), tfrm.getCol1() );
  3889. detinv = ( 1.0f / dot( tfrm.getCol2(), tmp2 ) );
  3890. inv0 = Vector3( ( tmp0.getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) );
  3891. inv1 = Vector3( ( tmp0.getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) );
  3892. inv2 = Vector3( ( tmp0.getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) );
  3893. return Transform3(
  3894. inv0,
  3895. inv1,
  3896. inv2,
  3897. Vector3( ( -( ( inv0 * tfrm.getCol3().getX() ) + ( ( inv1 * tfrm.getCol3().getY() ) + ( inv2 * tfrm.getCol3().getZ() ) ) ) ) )
  3898. );
  3899. }
  3900. inline const Transform3 orthoInverse( const Transform3 & tfrm )
  3901. {
  3902. Vector3 inv0, inv1, inv2;
  3903. inv0 = Vector3( tfrm.getCol0().getX(), tfrm.getCol1().getX(), tfrm.getCol2().getX() );
  3904. inv1 = Vector3( tfrm.getCol0().getY(), tfrm.getCol1().getY(), tfrm.getCol2().getY() );
  3905. inv2 = Vector3( tfrm.getCol0().getZ(), tfrm.getCol1().getZ(), tfrm.getCol2().getZ() );
  3906. return Transform3(
  3907. inv0,
  3908. inv1,
  3909. inv2,
  3910. Vector3( ( -( ( inv0 * tfrm.getCol3().getX() ) + ( ( inv1 * tfrm.getCol3().getY() ) + ( inv2 * tfrm.getCol3().getZ() ) ) ) ) )
  3911. );
  3912. }
  3913. inline const Transform3 absPerElem( const Transform3 & tfrm )
  3914. {
  3915. return Transform3(
  3916. absPerElem( tfrm.getCol0() ),
  3917. absPerElem( tfrm.getCol1() ),
  3918. absPerElem( tfrm.getCol2() ),
  3919. absPerElem( tfrm.getCol3() )
  3920. );
  3921. }
  3922. inline const Vector3 Transform3::operator *( const Vector3 & vec ) const
  3923. {
  3924. return Vector3(
  3925. ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
  3926. ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
  3927. ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) )
  3928. );
  3929. }
  3930. inline const Point3 Transform3::operator *( const Point3 & pnt ) const
  3931. {
  3932. return Point3(
  3933. ( ( ( ( mCol0.getX() * pnt.getX() ) + ( mCol1.getX() * pnt.getY() ) ) + ( mCol2.getX() * pnt.getZ() ) ) + mCol3.getX() ),
  3934. ( ( ( ( mCol0.getY() * pnt.getX() ) + ( mCol1.getY() * pnt.getY() ) ) + ( mCol2.getY() * pnt.getZ() ) ) + mCol3.getY() ),
  3935. ( ( ( ( mCol0.getZ() * pnt.getX() ) + ( mCol1.getZ() * pnt.getY() ) ) + ( mCol2.getZ() * pnt.getZ() ) ) + mCol3.getZ() )
  3936. );
  3937. }
  3938. inline const Transform3 Transform3::operator *( const Transform3 & tfrm ) const
  3939. {
  3940. return Transform3(
  3941. ( *this * tfrm.mCol0 ),
  3942. ( *this * tfrm.mCol1 ),
  3943. ( *this * tfrm.mCol2 ),
  3944. Vector3( ( *this * Point3( tfrm.mCol3 ) ) )
  3945. );
  3946. }
  3947. inline Transform3 & Transform3::operator *=( const Transform3 & tfrm )
  3948. {
  3949. *this = *this * tfrm;
  3950. return *this;
  3951. }
  3952. inline const Transform3 mulPerElem( const Transform3 & tfrm0, const Transform3 & tfrm1 )
  3953. {
  3954. return Transform3(
  3955. mulPerElem( tfrm0.getCol0(), tfrm1.getCol0() ),
  3956. mulPerElem( tfrm0.getCol1(), tfrm1.getCol1() ),
  3957. mulPerElem( tfrm0.getCol2(), tfrm1.getCol2() ),
  3958. mulPerElem( tfrm0.getCol3(), tfrm1.getCol3() )
  3959. );
  3960. }
  3961. inline const Transform3 Transform3::identity( )
  3962. {
  3963. return Transform3(
  3964. Vector3::xAxis( ),
  3965. Vector3::yAxis( ),
  3966. Vector3::zAxis( ),
  3967. Vector3( 0.0f )
  3968. );
  3969. }
  3970. inline Transform3 & Transform3::setUpper3x3( const Matrix3 & tfrm )
  3971. {
  3972. mCol0 = tfrm.getCol0();
  3973. mCol1 = tfrm.getCol1();
  3974. mCol2 = tfrm.getCol2();
  3975. return *this;
  3976. }
  3977. inline const Matrix3 Transform3::getUpper3x3( ) const
  3978. {
  3979. return Matrix3( mCol0, mCol1, mCol2 );
  3980. }
  3981. inline Transform3 & Transform3::setTranslation( const Vector3 & translateVec )
  3982. {
  3983. mCol3 = translateVec;
  3984. return *this;
  3985. }
  3986. inline const Vector3 Transform3::getTranslation( ) const
  3987. {
  3988. return mCol3;
  3989. }
  3990. inline const Transform3 Transform3::rotationX( float radians )
  3991. {
  3992. float s, c;
  3993. s = sinf( radians );
  3994. c = cosf( radians );
  3995. return Transform3(
  3996. Vector3::xAxis( ),
  3997. Vector3( 0.0f, c, s ),
  3998. Vector3( 0.0f, -s, c ),
  3999. Vector3( 0.0f )
  4000. );
  4001. }
  4002. inline const Transform3 Transform3::rotationY( float radians )
  4003. {
  4004. float s, c;
  4005. s = sinf( radians );
  4006. c = cosf( radians );
  4007. return Transform3(
  4008. Vector3( c, 0.0f, -s ),
  4009. Vector3::yAxis( ),
  4010. Vector3( s, 0.0f, c ),
  4011. Vector3( 0.0f )
  4012. );
  4013. }
  4014. inline const Transform3 Transform3::rotationZ( float radians )
  4015. {
  4016. float s, c;
  4017. s = sinf( radians );
  4018. c = cosf( radians );
  4019. return Transform3(
  4020. Vector3( c, s, 0.0f ),
  4021. Vector3( -s, c, 0.0f ),
  4022. Vector3::zAxis( ),
  4023. Vector3( 0.0f )
  4024. );
  4025. }
  4026. inline const Transform3 Transform3::rotationZYX( const Vector3 & radiansXYZ )
  4027. {
  4028. float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
  4029. sX = sinf( radiansXYZ.getX() );
  4030. cX = cosf( radiansXYZ.getX() );
  4031. sY = sinf( radiansXYZ.getY() );
  4032. cY = cosf( radiansXYZ.getY() );
  4033. sZ = sinf( radiansXYZ.getZ() );
  4034. cZ = cosf( radiansXYZ.getZ() );
  4035. tmp0 = ( cZ * sY );
  4036. tmp1 = ( sZ * sY );
  4037. return Transform3(
  4038. Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
  4039. Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
  4040. Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) ),
  4041. Vector3( 0.0f )
  4042. );
  4043. }
  4044. inline const Transform3 Transform3::rotation( float radians, const Vector3 & unitVec )
  4045. {
  4046. return Transform3( Matrix3::rotation( radians, unitVec ), Vector3( 0.0f ) );
  4047. }
  4048. inline const Transform3 Transform3::rotation( const Quat & unitQuat )
  4049. {
  4050. return Transform3( Matrix3( unitQuat ), Vector3( 0.0f ) );
  4051. }
  4052. inline const Transform3 Transform3::scale( const Vector3 & scaleVec )
  4053. {
  4054. return Transform3(
  4055. Vector3( scaleVec.getX(), 0.0f, 0.0f ),
  4056. Vector3( 0.0f, scaleVec.getY(), 0.0f ),
  4057. Vector3( 0.0f, 0.0f, scaleVec.getZ() ),
  4058. Vector3( 0.0f )
  4059. );
  4060. }
  4061. inline const Transform3 appendScale( const Transform3 & tfrm, const Vector3 & scaleVec )
  4062. {
  4063. return Transform3(
  4064. ( tfrm.getCol0() * scaleVec.getX( ) ),
  4065. ( tfrm.getCol1() * scaleVec.getY( ) ),
  4066. ( tfrm.getCol2() * scaleVec.getZ( ) ),
  4067. tfrm.getCol3()
  4068. );
  4069. }
  4070. inline const Transform3 prependScale( const Vector3 & scaleVec, const Transform3 & tfrm )
  4071. {
  4072. return Transform3(
  4073. mulPerElem( tfrm.getCol0(), scaleVec ),
  4074. mulPerElem( tfrm.getCol1(), scaleVec ),
  4075. mulPerElem( tfrm.getCol2(), scaleVec ),
  4076. mulPerElem( tfrm.getCol3(), scaleVec )
  4077. );
  4078. }
  4079. inline const Transform3 Transform3::translation( const Vector3 & translateVec )
  4080. {
  4081. return Transform3(
  4082. Vector3::xAxis( ),
  4083. Vector3::yAxis( ),
  4084. Vector3::zAxis( ),
  4085. translateVec
  4086. );
  4087. }
  4088. inline const Transform3 select( const Transform3 & tfrm0, const Transform3 & tfrm1, bool select1 )
  4089. {
  4090. return Transform3(
  4091. select( tfrm0.getCol0(), tfrm1.getCol0(), select1 ),
  4092. select( tfrm0.getCol1(), tfrm1.getCol1(), select1 ),
  4093. select( tfrm0.getCol2(), tfrm1.getCol2(), select1 ),
  4094. select( tfrm0.getCol3(), tfrm1.getCol3(), select1 )
  4095. );
  4096. }
  4097. #ifdef _VECTORMATH_DEBUG
  4098. inline void print( const Transform3 & tfrm )
  4099. {
  4100. print( tfrm.getRow( 0 ) );
  4101. print( tfrm.getRow( 1 ) );
  4102. print( tfrm.getRow( 2 ) );
  4103. }
  4104. inline void print( const Transform3 & tfrm, const char * name )
  4105. {
  4106. printf("%s:\n", name);
  4107. print( tfrm );
  4108. }
  4109. #endif
  4110. inline Quat::Quat( const Matrix3 & tfrm )
  4111. {
  4112. float trace, radicand, scale, xx, yx, zx, xy, yy, zy, xz, yz, zz, tmpx, tmpy, tmpz, tmpw, qx, qy, qz, qw;
  4113. int negTrace, ZgtX, ZgtY, YgtX;
  4114. int largestXorY, largestYorZ, largestZorX;
  4115. xx = tfrm.getCol0().getX();
  4116. yx = tfrm.getCol0().getY();
  4117. zx = tfrm.getCol0().getZ();
  4118. xy = tfrm.getCol1().getX();
  4119. yy = tfrm.getCol1().getY();
  4120. zy = tfrm.getCol1().getZ();
  4121. xz = tfrm.getCol2().getX();
  4122. yz = tfrm.getCol2().getY();
  4123. zz = tfrm.getCol2().getZ();
  4124. trace = ( ( xx + yy ) + zz );
  4125. negTrace = ( trace < 0.0f );
  4126. ZgtX = zz > xx;
  4127. ZgtY = zz > yy;
  4128. YgtX = yy > xx;
  4129. largestXorY = ( !ZgtX || !ZgtY ) && negTrace;
  4130. largestYorZ = ( YgtX || ZgtX ) && negTrace;
  4131. largestZorX = ( ZgtY || !YgtX ) && negTrace;
  4132. if ( largestXorY )
  4133. {
  4134. zz = -zz;
  4135. xy = -xy;
  4136. }
  4137. if ( largestYorZ )
  4138. {
  4139. xx = -xx;
  4140. yz = -yz;
  4141. }
  4142. if ( largestZorX )
  4143. {
  4144. yy = -yy;
  4145. zx = -zx;
  4146. }
  4147. radicand = ( ( ( xx + yy ) + zz ) + 1.0f );
  4148. scale = ( 0.5f * ( 1.0f / sqrtf( radicand ) ) );
  4149. tmpx = ( ( zy - yz ) * scale );
  4150. tmpy = ( ( xz - zx ) * scale );
  4151. tmpz = ( ( yx - xy ) * scale );
  4152. tmpw = ( radicand * scale );
  4153. qx = tmpx;
  4154. qy = tmpy;
  4155. qz = tmpz;
  4156. qw = tmpw;
  4157. if ( largestXorY )
  4158. {
  4159. qx = tmpw;
  4160. qy = tmpz;
  4161. qz = tmpy;
  4162. qw = tmpx;
  4163. }
  4164. if ( largestYorZ )
  4165. {
  4166. tmpx = qx;
  4167. tmpz = qz;
  4168. qx = qy;
  4169. qy = tmpx;
  4170. qz = qw;
  4171. qw = tmpz;
  4172. }
  4173. mX = qx;
  4174. mY = qy;
  4175. mZ = qz;
  4176. mW = qw;
  4177. }
  4178. inline const Matrix3 outer( const Vector3 & tfrm0, const Vector3 & tfrm1 )
  4179. {
  4180. return Matrix3(
  4181. ( tfrm0 * tfrm1.getX( ) ),
  4182. ( tfrm0 * tfrm1.getY( ) ),
  4183. ( tfrm0 * tfrm1.getZ( ) )
  4184. );
  4185. }
  4186. inline const Matrix4 outer( const Vector4 & tfrm0, const Vector4 & tfrm1 )
  4187. {
  4188. return Matrix4(
  4189. ( tfrm0 * tfrm1.getX( ) ),
  4190. ( tfrm0 * tfrm1.getY( ) ),
  4191. ( tfrm0 * tfrm1.getZ( ) ),
  4192. ( tfrm0 * tfrm1.getW( ) )
  4193. );
  4194. }
  4195. inline const Vector3 rowMul( const Vector3 & vec, const Matrix3 & mat )
  4196. {
  4197. return Vector3(
  4198. ( ( ( vec.getX() * mat.getCol0().getX() ) + ( vec.getY() * mat.getCol0().getY() ) ) + ( vec.getZ() * mat.getCol0().getZ() ) ),
  4199. ( ( ( vec.getX() * mat.getCol1().getX() ) + ( vec.getY() * mat.getCol1().getY() ) ) + ( vec.getZ() * mat.getCol1().getZ() ) ),
  4200. ( ( ( vec.getX() * mat.getCol2().getX() ) + ( vec.getY() * mat.getCol2().getY() ) ) + ( vec.getZ() * mat.getCol2().getZ() ) )
  4201. );
  4202. }
  4203. inline const Matrix3 crossMatrix( const Vector3 & vec )
  4204. {
  4205. return Matrix3(
  4206. Vector3( 0.0f, vec.getZ(), -vec.getY() ),
  4207. Vector3( -vec.getZ(), 0.0f, vec.getX() ),
  4208. Vector3( vec.getY(), -vec.getX(), 0.0f )
  4209. );
  4210. }
  4211. inline const Matrix3 crossMatrixMul( const Vector3 & vec, const Matrix3 & mat )
  4212. {
  4213. return Matrix3( cross( vec, mat.getCol0() ), cross( vec, mat.getCol1() ), cross( vec, mat.getCol2() ) );
  4214. }
  4215. } // namespace Aos
  4216. } // namespace Vectormath
  4217. namespace vmath
  4218. {
  4219. using namespace Vectormath::Aos;
  4220. inline Point3 project(Point3 p, Point3 a, Point3 b)
  4221. {
  4222. float t = dot(b - a, p - a) / distSqr(a, b);
  4223. return a + t * (b - a);
  4224. }
  4225. inline Matrix4 pick_box(float centerX, float centerY, float width, float height, int viewport[4])
  4226. {
  4227. float sx = viewport[2] / width;
  4228. float sy = viewport[3] / height;
  4229. float tx = (viewport[2] + 2.0f * (viewport[0] - centerX)) / width;
  4230. float ty = (viewport[3] + 2.0f * (viewport[1] - centerY)) / height;
  4231. Vector4 c0(sx, 0, 0, tx);
  4232. Vector4 c1(0, sy, 0, ty);
  4233. Vector4 c2(0, 0, 1, 0);
  4234. Vector4 c3(0, 0, 0, 1);
  4235. return transpose(Matrix4(c0, c1, c2, c3));
  4236. }
  4237. inline Point3 perspective(Vector4 v)
  4238. {
  4239. return Point3(v.getX() / v.getW(), v.getY() / v.getW(), v.getZ() / v.getW());
  4240. }
  4241. inline Vector3 perp(Vector3 a)
  4242. {
  4243. Vector3 c = Vector3(1, 0, 0);
  4244. Vector3 b = cross(a, c);
  4245. if (lengthSqr(b) < 0.01f)
  4246. {
  4247. c = Vector3(0, 1, 0);
  4248. b = cross(a, c);
  4249. }
  4250. return b;
  4251. }
  4252. inline Quat rotate(Quat a, Quat b)
  4253. {
  4254. float w = a.getW() * b.getW() - a.getX() * b.getX() - a.getY() * b.getY() - a.getZ() * b.getZ();
  4255. float x = a.getW() * b.getX() + a.getX() * b.getW() + a.getY() * b.getZ() - a.getZ() * b.getY();
  4256. float y = a.getW() * b.getY() + a.getY() * b.getW() + a.getZ() * b.getX() - a.getX() * b.getZ();
  4257. float z = a.getW() * b.getZ() + a.getZ() * b.getW() + a.getX() * b.getY() - a.getY() * b.getX();
  4258. Quat q(x, y, z, w);
  4259. return normalize(q);
  4260. }
  4261. }