| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512 |
- /******************************************************************************/
- #include "stdafx.h"
- namespace EE{
- /******************************************************************************
- ObjMatrix is combined with CamMatrix to:
- -be able to support large distances (because matrix calculations can be done in Dbl precision on the Cpu side, and stored as Flt precision in view space)
- -reduce number of matrix operations in shaders
- /******************************************************************************/
- Matrix3 CamMatrixInvMotionScale;
- MatrixM ObjMatrix(1),
- CamMatrix,
- CamMatrixInv,
- EyeMatrix[2];
- Matrix4 ProjMatrix;
- Flt ProjMatrixEyeOffset[2];
- const Matrix MatrixIdentity (1);
- const MatrixM MatrixMIdentity(1);
- GpuMatrix *ViewMatrix;
- #if MAY_NEED_BONE_SPLITS
- static Vec GObjVel [MAX_MATRIX_SW], GFurVel[MAX_MATRIX_SW];
- static GpuMatrix GObjMatrix[MAX_MATRIX_SW];
- #endif
- #if DX9
- Vec2 PixelOffset;
- #endif
- /******************************************************************************/
- Matrix3& Matrix3::operator*=(Flt f)
- {
- x*=f;
- y*=f;
- z*=f;
- return T;
- }
- MatrixD3& MatrixD3::operator*=(Dbl f)
- {
- x*=f;
- y*=f;
- z*=f;
- return T;
- }
- Matrix3& Matrix3::operator/=(Flt f)
- {
- x/=f;
- y/=f;
- z/=f;
- return T;
- }
- MatrixD3& MatrixD3::operator/=(Dbl f)
- {
- x/=f;
- y/=f;
- z/=f;
- return T;
- }
- Matrix& Matrix::operator*=(Flt f)
- {
- x *=f;
- y *=f;
- z *=f;
- pos*=f;
- return T;
- }
- MatrixM& MatrixM::operator*=(Flt f)
- {
- x *=f;
- y *=f;
- z *=f;
- pos*=f;
- return T;
- }
- MatrixD& MatrixD::operator*=(Dbl f)
- {
- x *=f;
- y *=f;
- z *=f;
- pos*=f;
- return T;
- }
- Matrix& Matrix::operator/=(Flt f)
- {
- x /=f;
- y /=f;
- z /=f;
- pos/=f;
- return T;
- }
- MatrixM& MatrixM::operator/=(Flt f)
- {
- x /=f;
- y /=f;
- z /=f;
- pos/=f;
- return T;
- }
- MatrixD& MatrixD::operator/=(Dbl f)
- {
- x /=f;
- y /=f;
- z /=f;
- pos/=f;
- return T;
- }
- Matrix3& Matrix3::operator*=(C Vec &v)
- {
- x*=v;
- y*=v;
- z*=v;
- return T;
- }
- MatrixD3& MatrixD3::operator*=(C VecD &v)
- {
- x*=v;
- y*=v;
- z*=v;
- return T;
- }
- Matrix3& Matrix3::operator/=(C Vec &v)
- {
- x/=v;
- y/=v;
- z/=v;
- return T;
- }
- MatrixD3& MatrixD3::operator/=(C VecD &v)
- {
- x/=v;
- y/=v;
- z/=v;
- return T;
- }
- Matrix& Matrix::operator*=(C Vec &v)
- {
- x *=v;
- y *=v;
- z *=v;
- pos*=v;
- return T;
- }
- MatrixM& MatrixM::operator*=(C Vec &v)
- {
- x *=v;
- y *=v;
- z *=v;
- pos*=v;
- return T;
- }
- MatrixD& MatrixD::operator*=(C VecD &v)
- {
- x *=v;
- y *=v;
- z *=v;
- pos*=v;
- return T;
- }
- Matrix& Matrix::operator/=(C Vec &v)
- {
- x /=v;
- y /=v;
- z /=v;
- pos/=v;
- return T;
- }
- MatrixM& MatrixM::operator/=(C Vec &v)
- {
- x /=v;
- y /=v;
- z /=v;
- pos/=v;
- return T;
- }
- MatrixD& MatrixD::operator/=(C VecD &v)
- {
- x /=v;
- y /=v;
- z /=v;
- pos/=v;
- return T;
- }
- Matrix3& Matrix3::operator+=(C Matrix3 &m)
- {
- x+=m.x;
- y+=m.y;
- z+=m.z;
- return T;
- }
- MatrixD3& MatrixD3::operator+=(C MatrixD3 &m)
- {
- x+=m.x;
- y+=m.y;
- z+=m.z;
- return T;
- }
- Matrix3& Matrix3::operator-=(C Matrix3 &m)
- {
- x-=m.x;
- y-=m.y;
- z-=m.z;
- return T;
- }
- MatrixD3& MatrixD3::operator-=(C MatrixD3 &m)
- {
- x-=m.x;
- y-=m.y;
- z-=m.z;
- return T;
- }
- Matrix& Matrix::operator+=(C Matrix &m)
- {
- x +=m.x ;
- y +=m.y ;
- z +=m.z ;
- pos+=m.pos;
- return T;
- }
- MatrixM& MatrixM::operator+=(C MatrixM &m)
- {
- x +=m.x ;
- y +=m.y ;
- z +=m.z ;
- pos+=m.pos;
- return T;
- }
- MatrixD& MatrixD::operator+=(C MatrixD &m)
- {
- x +=m.x ;
- y +=m.y ;
- z +=m.z ;
- pos+=m.pos;
- return T;
- }
- Matrix& Matrix::operator-=(C Matrix &m)
- {
- x -=m.x ;
- y -=m.y ;
- z -=m.z ;
- pos-=m.pos;
- return T;
- }
- MatrixM& MatrixM::operator-=(C MatrixM &m)
- {
- x -=m.x ;
- y -=m.y ;
- z -=m.z ;
- pos-=m.pos;
- return T;
- }
- MatrixD& MatrixD::operator-=(C MatrixD &m)
- {
- x -=m.x ;
- y -=m.y ;
- z -=m.z ;
- pos-=m.pos;
- return T;
- }
- /******************************************************************************/
- Bool Matrix3 ::operator==(C Matrix3 &m)C {return x==m.x && y==m.y && z==m.z;}
- Bool Matrix3 ::operator!=(C Matrix3 &m)C {return x!=m.x || y!=m.y || z!=m.z;}
- Bool MatrixD3::operator==(C MatrixD3 &m)C {return x==m.x && y==m.y && z==m.z;}
- Bool MatrixD3::operator!=(C MatrixD3 &m)C {return x!=m.x || y!=m.y || z!=m.z;}
- Bool Matrix ::operator==(C Matrix &m)C {return x==m.x && y==m.y && z==m.z && pos==m.pos;}
- Bool Matrix ::operator!=(C Matrix &m)C {return x!=m.x || y!=m.y || z!=m.z || pos!=m.pos;}
- Bool MatrixM ::operator==(C MatrixM &m)C {return x==m.x && y==m.y && z==m.z && pos==m.pos;}
- Bool MatrixM ::operator!=(C MatrixM &m)C {return x!=m.x || y!=m.y || z!=m.z || pos!=m.pos;}
- Bool MatrixD ::operator==(C MatrixD &m)C {return x==m.x && y==m.y && z==m.z && pos==m.pos;}
- Bool MatrixD ::operator!=(C MatrixD &m)C {return x!=m.x || y!=m.y || z!=m.z || pos!=m.pos;}
- /******************************************************************************/
- void Matrix3::mul(C Matrix3 &m, Matrix3 &dest)C
- {
- Flt x, y, z;
- if(&dest==&m)
- {
- x=m.x.x; y=m.y.x; z=m.z.x;
- dest.x.x=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.x=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.x=x*T.z.x + y*T.z.y + z*T.z.z;
- x=m.x.y; y=m.y.y; z=m.z.y;
- dest.x.y=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.y=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.y=x*T.z.x + y*T.z.y + z*T.z.z;
- x=m.x.z; y=m.y.z; z=m.z.z;
- dest.x.z=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.z=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.z=x*T.z.x + y*T.z.y + z*T.z.z;
- }else
- {
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
- }
- }
- void MatrixD3::mul(C Matrix3 &m, MatrixD3 &dest)C
- {
- Dbl x, y, z;
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
- }
- void MatrixD3::mul(C MatrixD3 &m, MatrixD3 &dest)C
- {
- Dbl x, y, z;
- if(&dest==&m)
- {
- x=m.x.x; y=m.y.x; z=m.z.x;
- dest.x.x=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.x=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.x=x*T.z.x + y*T.z.y + z*T.z.z;
- x=m.x.y; y=m.y.y; z=m.z.y;
- dest.x.y=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.y=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.y=x*T.z.x + y*T.z.y + z*T.z.z;
- x=m.x.z; y=m.y.z; z=m.z.z;
- dest.x.z=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.z=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.z=x*T.z.x + y*T.z.y + z*T.z.z;
- }else
- {
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
- }
- }
- /******************************************************************************/
- #if 0 // performance is nearly the same so don't use
- void mulNormalized(C Matrix3 &matrix, Matrix3 &dest)C; // multiply self by 'matrix' and store result in 'dest', this method assumes that both self and 'matrix' are normalized, this is faster than 'mul'
- void Matrix3::mulNormalized(C Matrix3 &m, Matrix3 &dest)C
- {
- Flt x, y, z;
- if(&dest==&m)
- {
- x=m.x.x; y=m.y.x; z=m.z.x;
- //dest.x.x=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.x=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.x=x*T.z.x + y*T.z.y + z*T.z.z;
- x=m.x.y; y=m.y.y; z=m.z.y;
- //dest.x.y=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.y=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.y=x*T.z.x + y*T.z.y + z*T.z.z;
- x=m.x.z; y=m.y.z; z=m.z.z;
- //dest.x.z=x*T.x.x + y*T.x.y + z*T.x.z;
- dest.y.z=x*T.y.x + y*T.y.y + z*T.y.z;
- dest.z.z=x*T.z.x + y*T.z.y + z*T.z.z;
- }else
- {
- /*x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;*/
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
- }
- //dest.x=Cross(dest.y, dest.z);
- dest.x.x=dest.y.y*dest.z.z - dest.y.z*dest.z.y;
- dest.x.y=dest.y.z*dest.z.x - dest.y.x*dest.z.z;
- dest.x.z=dest.y.x*dest.z.y - dest.y.y*dest.z.x;
- }
- #endif
- /******************************************************************************/
- void Matrix::mul(C Matrix3 &m, Matrix &dest)C
- {
- Flt x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
- super::mul(m, dest.orn());
- }
- void MatrixM::mul(C Matrix3 &m, MatrixM &dest)C
- {
- Dbl x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
- super::mul(m, dest.orn());
- }
- void MatrixD::mul(C MatrixD3 &m, MatrixD &dest)C
- {
- Dbl x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
- super::mul(m, dest.orn());
- }
- /******************************************************************************
- void mulNormalized(C Matrix3 &matrix, Matrix &dest)C; // multiply self by 'matrix' and store result in 'dest', this method assumes that both self and 'matrix' are normalized, this is faster than 'mul'
- void Matrix::mulNormalized(C Matrix3 &m, Matrix &dest)C
- {
- Flt x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
- super::mulNormalized(m, dest.orn());
- }
- /******************************************************************************/
- void Matrix::mul(C Matrix &m, Matrix &dest)C
- {
- Flt x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mul(m.orn(), dest.orn());
- }
- void Matrix::mul(C MatrixM &m, Matrix &dest)C
- {
- Flt x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mul(m.orn(), dest.orn());
- }
- void MatrixM::mul(C MatrixM &m, Matrix &dest)C
- {
- Dbl x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mul(m.orn(), dest.orn());
- }
- void MatrixM::mul(C Matrix &m, MatrixM &dest)C
- {
- Dbl x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mul(m.orn(), dest.orn());
- }
- void MatrixM::mul(C MatrixM &m, MatrixM &dest)C
- {
- Dbl x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mul(m.orn(), dest.orn());
- }
- void MatrixD::mul(C MatrixD &m, MatrixD &dest)C
- {
- Dbl x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mul(m.orn(), dest.orn());
- }
- /******************************************************************************
- void Matrix::mulNormalized(C Matrix &m, Matrix &dest)C
- {
- Flt x=pos.x , y=pos.y , z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- super::mulNormalized(m.orn(), dest.orn());
- }
- /******************************************************************************/
- void Matrix::mul(C Matrix &m, Matrix4 &dest)C
- {
- Flt x, y, z;
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
- dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
- dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
- x=pos.x; y=pos.y; z=pos.z;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
- dest.x.w=dest.y.w=dest.z.w=0;
- dest.pos.w=1;
- }
- void Matrix::mul(C Matrix4 &m, Matrix4 &dest)C
- {
- Flt x, y, z;
- x=m.x.x; y=m.y.x; z=m.z.x;
- dest.x .x=T.x.x*x + T.x.y*y + T.x.z*z;
- dest.y .x=T.y.x*x + T.y.y*y + T.y.z*z;
- dest.z .x=T.z.x*x + T.z.y*y + T.z.z*z;
- dest.pos.x=pos.x*x + pos.y*y + pos.z*z + m.pos.x;
- x=m.x.y; y=m.y.y; z=m.z.y;
- dest.x .y=T.x.x*x + T.x.y*y + T.x.z*z;
- dest.y .y=T.y.x*x + T.y.y*y + T.y.z*z;
- dest.z .y=T.z.x*x + T.z.y*y + T.z.z*z;
- dest.pos.y=pos.x*x + pos.y*y + pos.z*z + m.pos.y;
- x=m.x.z; y=m.y.z; z=m.z.z;
- dest.x .z=T.x.x*x + T.x.y*y + T.x.z*z;
- dest.y .z=T.y.x*x + T.y.y*y + T.y.z*z;
- dest.z .z=T.z.x*x + T.z.y*y + T.z.z*z;
- dest.pos.z=pos.x*x + pos.y*y + pos.z*z + m.pos.z;
- x=m.x.w; y=m.y.w; z=m.z.w;
- dest.x .w=T.x.x*x + T.x.y*y + T.x.z*z;
- dest.y .w=T.y.x*x + T.y.y*y + T.y.z*z;
- dest.z .w=T.z.x*x + T.z.y*y + T.z.z*z;
- dest.pos.w=pos.x*x + pos.y*y + pos.z*z + m.pos.w;
- }
- void Matrix4::mul(C Matrix4 &m, Matrix4 &dest)C
- {
- Flt x, y, z, w;
- if(&dest==&m)
- {
- x=m.x.x; y=m.y.x; z=m.z.x; w=m.pos.x;
- dest.x .x=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
- dest.y .x=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
- dest.z .x=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
- dest.pos.x=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
- x=m.x.y; y=m.y.y; z=m.z.y; w=m.pos.y;
- dest.x .y=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
- dest.y .y=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
- dest.z .y=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
- dest.pos.y=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
- x=m.x.z; y=m.y.z; z=m.z.z; w=m.pos.z;
- dest.x .z=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
- dest.y .z=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
- dest.z .z=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
- dest.pos.z=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
- x=m.x.w; y=m.y.w; z=m.z.w; w=m.pos.w;
- dest.x .w=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
- dest.y .w=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
- dest.z .w=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
- dest.pos.w=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
- }else
- {
- x=T.x.x; y=T.x.y; z=T.x.z; w=T. x.w;
- dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
- dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
- dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
- dest.x.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
- x=T.y.x; y=T.y.y; z=T.y.z; w=T. y.w;
- dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
- dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
- dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
- dest.y.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
- x=T.z.x; y=T.z.y; z=T.z.z; w=T. z.w;
- dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
- dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
- dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
- dest.z.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
- x= pos.x; y=pos.y; z=pos.z; w=pos.w;
- dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
- dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
- dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
- dest.pos.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
- }
- }
- /******************************************************************************/
- void Matrix::mulTimes(Int n, C Matrix &matrix, Matrix &dest)C
- {
- switch(n)
- {
- case 0: dest=T; break;
- case 1: mul(matrix, dest ); break;
- default:
- {
- if(Equal(matrix.orn(), MatrixIdentity.orn())) // no rotation and no scale - we can just move by "n*pos"
- {
- dest.orn()=T.orn();
- dest.pos =T.pos+matrix.pos*n;
- }else
- if(!matrix.pos.any() && Equal(matrix.scale2(), VecOne)) // no position and no scale - we can just rotate by "n*angle"
- {
- Vec axis; Flt angle=matrix.axisAngle(axis);
- Matrix matrix_n; matrix_n.setRotate(axis, angle*n);
- mul(matrix_n, dest);
- }else
- {
- Matrix temp=matrix; if(n<0){CHS(n); temp.inverse();}
- FREPA(PrimeNumbers) // optimize using prime numbers
- {
- Int pm=PrimeNumbers[i]; if(n<pm)break;
- for(; n%pm==0; n/=pm){Matrix single=temp; REP(pm-1)temp*=single;}
- }
- mul(temp, dest); REP(n-1)dest*=temp; // make the first mul from this->dest, and remaining mul's from dest->dest
- }
- }break;
- }
- }
- void Matrix::mulTimes(Int n, C RevMatrix &matrix, Matrix &dest)C
- {
- switch(n)
- {
- case 0: dest=T; break;
- case 1: mul(matrix, dest ); break;
- default:
- {
- if(Equal(matrix.orn(), MatrixIdentity.orn())) // no rotation and no scale - we can just move by "n*pos"
- {
- dest.orn()=T.orn();
- dest.pos =T.pos+(matrix.pos*n)*T.orn();
- }else
- if(!matrix.pos.any() && Equal(matrix.scale2(), VecOne)) // no position and no scale - we can just rotate by "n*angle"
- {
- Vec axis; Flt angle=matrix.axisAngle(axis);
- RevMatrix matrix_n; matrix_n.setRotate(axis, angle*n);
- mul(matrix_n, dest);
- }else
- {
- RevMatrix temp=matrix; if(n<0){CHS(n); temp.inverse();}
- FREPA(PrimeNumbers) // optimize using prime numbers
- {
- Int pm=PrimeNumbers[i]; if(n<pm)break;
- for(; n%pm==0; n/=pm){Matrix single=temp; REP(pm-1)temp*=single;} // in this case it doesn't matter if 'single' is 'Matrix' or 'RevMatrix'
- }
- mul(temp, dest); REP(n-1)dest*=temp; // make the first mul from this->dest, and remaining mul's from dest->dest
- }
- }break;
- }
- }
- /******************************************************************************/
- void Matrix3::divNormalized(C Matrix3 &m, Matrix3 &dest)C
- {
- if(&dest==&m)
- {
- Flt x_x=m.x.x, x_y=m.x.y, x_z=m.x.z;
- dest.x.x=x_x*T.x.x + x_y*T.x.y + x_z*T.x.z;
- Flt y_x=m.y.x; dest.y.x=x_x*T.y.x + x_y*T.y.y + x_z*T.y.z;
- Flt z_x=m.z.x; dest.z.x=x_x*T.z.x + x_y*T.z.y + x_z*T.z.z;
- Flt y_y=m.y.y, y_z=m.y.z;
- dest.x.y=y_x*T.x.x + y_y*T.x.y + y_z*T.x.z;
- dest.y.y=y_x*T.y.x + y_y*T.y.y + y_z*T.y.z;
- Flt z_y=m.z.y; dest.z.y=y_x*T.z.x + y_y*T.z.y + y_z*T.z.z;
- Flt z_z=m.z.z;
- dest.x.z=z_x*T.x.x + z_y*T.x.y + z_z*T.x.z;
- dest.y.z=z_x*T.y.x + z_y*T.y.y + z_z*T.y.z;
- dest.z.z=z_x*T.z.x + z_y*T.z.y + z_z*T.z.z;
- }else
- {
- Flt x, y, z;
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.x.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.x.z=x*m.z.x + y*m.z.y + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.y.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.y.z=x*m.z.x + y*m.z.y + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.z.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.z.z=x*m.z.x + y*m.z.y + z*m.z.z;
- }
- }
- void MatrixD3::divNormalized(C Matrix3 &m, MatrixD3 &dest)C
- {
- Dbl x, y, z;
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.x.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.x.z=x*m.z.x + y*m.z.y + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.y.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.y.z=x*m.z.x + y*m.z.y + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.z.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.z.z=x*m.z.x + y*m.z.y + z*m.z.z;
- }
- void MatrixD3::divNormalized(C MatrixD3 &m, MatrixD3 &dest)C
- {
- if(&dest==&m)
- {
- Dbl x_x=m.x.x, x_y=m.x.y, x_z=m.x.z;
- dest.x.x=x_x*T.x.x + x_y*T.x.y + x_z*T.x.z;
- Dbl y_x=m.y.x; dest.y.x=x_x*T.y.x + x_y*T.y.y + x_z*T.y.z;
- Dbl z_x=m.z.x; dest.z.x=x_x*T.z.x + x_y*T.z.y + x_z*T.z.z;
- Dbl y_y=m.y.y, y_z=m.y.z;
- dest.x.y=y_x*T.x.x + y_y*T.x.y + y_z*T.x.z;
- dest.y.y=y_x*T.y.x + y_y*T.y.y + y_z*T.y.z;
- Dbl z_y=m.z.y; dest.z.y=y_x*T.z.x + y_y*T.z.y + y_z*T.z.z;
- Dbl z_z=m.z.z;
- dest.x.z=z_x*T.x.x + z_y*T.x.y + z_z*T.x.z;
- dest.y.z=z_x*T.y.x + z_y*T.y.y + z_z*T.y.z;
- dest.z.z=z_x*T.z.x + z_y*T.z.y + z_z*T.z.z;
- }else
- {
- Dbl x, y, z;
- x=T.x.x; y=T.x.y; z=T.x.z;
- dest.x.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.x.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.x.z=x*m.z.x + y*m.z.y + z*m.z.z;
- x=T.y.x; y=T.y.y; z=T.y.z;
- dest.y.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.y.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.y.z=x*m.z.x + y*m.z.y + z*m.z.z;
- x=T.z.x; y=T.z.y; z=T.z.z;
- dest.z.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.z.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.z.z=x*m.z.x + y*m.z.y + z*m.z.z;
- }
- }
- /******************************************************************************/
- void Matrix::divNormalized(C Matrix3 &m, Matrix &dest)C
- {
- Flt x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void MatrixM::divNormalized(C Matrix3 &m, MatrixM &dest)C
- {
- Dbl x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void MatrixD::divNormalized(C MatrixD3 &m, MatrixD &dest)C
- {
- Dbl x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void Matrix::divNormalized(C Matrix &m, Matrix &dest)C
- {
- Flt x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void Matrix::divNormalized(C MatrixM &m, Matrix &dest)C
- {
- Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void MatrixM::divNormalized(C MatrixM &m, Matrix &dest)C
- {
- Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void MatrixM::divNormalized(C Matrix &m, MatrixM &dest)C
- {
- Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void MatrixM::divNormalized(C MatrixM &m, MatrixM &dest)C
- {
- Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- void MatrixD::divNormalized(C MatrixD &m, MatrixD &dest)C
- {
- Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
- dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
- dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
- dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
- super::divNormalized(m, dest);
- }
- /******************************************************************************/
- void Matrix3 ::div(C Matrix3 &m, Matrix3 &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
- void MatrixD3::div(C Matrix3 &m, MatrixD3 &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
- void MatrixD3::div(C MatrixD3 &m, MatrixD3 &dest)C {MatrixD3 temp; m.inverse(temp); mul(temp, dest);}
- void Matrix ::div(C Matrix3 &m, Matrix &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
- void MatrixM ::div(C Matrix3 &m, MatrixM &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
- void MatrixD ::div(C MatrixD3 &m, MatrixD &dest)C {MatrixD3 temp; m.inverse(temp); mul(temp, dest);}
- void Matrix ::div(C Matrix &m, Matrix &dest)C {Matrix temp; m.inverse(temp); mul(temp, dest);}
- void Matrix ::div(C MatrixM &m, Matrix &dest)C {MatrixM temp; m.inverse(temp); mul(temp, dest);}
- void MatrixM ::div(C MatrixM &m, Matrix &dest)C {MatrixM temp; m.inverse(temp); mul(temp, dest);}
- void MatrixM ::div(C Matrix &m, MatrixM &dest)C {Matrix temp; m.inverse(temp); mul(temp, dest);}
- void MatrixM ::div(C MatrixM &m, MatrixM &dest)C {MatrixM temp; m.inverse(temp); mul(temp, dest);}
- void MatrixD ::div(C MatrixD &m, MatrixD &dest)C {MatrixD temp; m.inverse(temp); mul(temp, dest);}
- /******************************************************************************/
- Matrix3& Matrix3::inverseScale()
- {
- if(Flt l=x.length2())x/=l;
- if(Flt l=y.length2())y/=l;
- if(Flt l=z.length2())z/=l;
- return T;
- }
- MatrixD3& MatrixD3::inverseScale()
- {
- if(Dbl l=x.length2())x/=l;
- if(Dbl l=y.length2())y/=l;
- if(Dbl l=z.length2())z/=l;
- return T;
- }
- void Matrix3::inverse(Matrix3 &dest, Bool normalized)C
- {
- //dest=transpose(T);
- //dest.inverseScale();
- if(&dest!=this)
- {
- dest.x.x=x.x;
- dest.y.y=y.y;
- dest.z.z=z.z;
- }
- Flt temp;
- temp=x.y; dest.x.y=y.x; dest.y.x=temp;
- temp=x.z; dest.x.z=z.x; dest.z.x=temp;
- temp=y.z; dest.y.z=z.y; dest.z.y=temp;
- if(!normalized)dest.inverseScale();
- }
- void MatrixD3::inverse(MatrixD3 &dest, Bool normalized)C
- {
- if(&dest!=this)
- {
- dest.x.x=x.x;
- dest.y.y=y.y;
- dest.z.z=z.z;
- }
- Dbl temp;
- temp=x.y; dest.x.y=y.x; dest.y.x=temp;
- temp=x.z; dest.x.z=z.x; dest.z.x=temp;
- temp=y.z; dest.y.z=z.y; dest.z.y=temp;
- if(!normalized)dest.inverseScale();
- }
- void Matrix3::inverseNonOrthogonal(Matrix3 &dest)C
- {
- Flt f1=(y.y*z.z - z.y*y.z),
- f2=(z.y*x.z - x.y*z.z),
- f3=(x.y*y.z - y.y*x.z),
- det=x.x*f1 + y.x*f2 + z.x*f3;
- if(det)
- {
- det=1/det;
- if(&dest==this)
- {
- Matrix3 temp;
- temp.x.x=det*f1;
- temp.x.y=det*f2;
- temp.x.z=det*f3;
- temp.y.x=det*(z.x*y.z - y.x*z.z);
- temp.y.y=det*(x.x*z.z - z.x*x.z);
- temp.y.z=det*(y.x*x.z - x.x*y.z);
- temp.z.x=det*(y.x*z.y - z.x*y.y);
- temp.z.y=det*(z.x*x.y - x.x*z.y);
- temp.z.z=det*(x.x*y.y - y.x*x.y);
- dest=temp;
- }else
- {
- dest.x.x=det*f1;
- dest.x.y=det*f2;
- dest.x.z=det*f3;
- dest.y.x=det*(z.x*y.z - y.x*z.z);
- dest.y.y=det*(x.x*z.z - z.x*x.z);
- dest.y.z=det*(y.x*x.z - x.x*y.z);
- dest.z.x=det*(y.x*z.y - z.x*y.y);
- dest.z.y=det*(z.x*x.y - x.x*z.y);
- dest.z.z=det*(x.x*y.y - y.x*x.y);
- }
- }
- }
- void MatrixD3::inverseNonOrthogonal(MatrixD3 &dest)C
- {
- Dbl f1=(y.y*z.z - z.y*y.z),
- f2=(z.y*x.z - x.y*z.z),
- f3=(x.y*y.z - y.y*x.z),
- det=x.x*f1 + y.x*f2 + z.x*f3;
- if(det)
- {
- det=1/det;
- if(&dest==this)
- {
- MatrixD3 temp;
- temp.x.x=det*f1;
- temp.x.y=det*f2;
- temp.x.z=det*f3;
- temp.y.x=det*(z.x*y.z - y.x*z.z);
- temp.y.y=det*(x.x*z.z - z.x*x.z);
- temp.y.z=det*(y.x*x.z - x.x*y.z);
- temp.z.x=det*(y.x*z.y - z.x*y.y);
- temp.z.y=det*(z.x*x.y - x.x*z.y);
- temp.z.z=det*(x.x*y.y - y.x*x.y);
- dest=temp;
- }else
- {
- dest.x.x=det*f1;
- dest.x.y=det*f2;
- dest.x.z=det*f3;
- dest.y.x=det*(z.x*y.z - y.x*z.z);
- dest.y.y=det*(x.x*z.z - z.x*x.z);
- dest.y.z=det*(y.x*x.z - x.x*y.z);
- dest.z.x=det*(y.x*z.y - z.x*y.y);
- dest.z.y=det*(z.x*x.y - x.x*z.y);
- dest.z.z=det*(x.x*y.y - y.x*x.y);
- }
- }
- }
- void Matrix::inverse(Matrix &dest, Bool normalized)C
- {
- super::inverse(dest.orn(), normalized);
- Flt x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
- dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
- dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
- }
- void MatrixM::inverse(MatrixM &dest, Bool normalized)C
- {
- super::inverse(dest.orn(), normalized);
- Dbl x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
- dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
- dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
- }
- void MatrixD::inverse(MatrixD &dest, Bool normalized)C
- {
- super::inverse(dest.orn(), normalized);
- Dbl x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
- dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
- dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
- }
- void Matrix::inverseNonOrthogonal(Matrix &dest)C
- {
- super::inverseNonOrthogonal(dest.orn());
- Flt x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
- dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
- dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
- }
- void MatrixM::inverseNonOrthogonal(MatrixM &dest)C
- {
- super::inverseNonOrthogonal(dest.orn());
- Dbl x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
- dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
- dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
- }
- void MatrixD::inverseNonOrthogonal(MatrixD &dest)C
- {
- super::inverseNonOrthogonal(dest.orn());
- Dbl x=pos.x, y=pos.y, z=pos.z;
- dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
- dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
- dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
- }
- Matrix4& Matrix4::inverse()
- {
- if(Flt det=determinant())
- {
- Matrix4 t;
- t.x .x=y.z*z.w*pos.y - y.w*z.z*pos.y + y.w*z.y*pos.z - y.y*z.w*pos.z - y.z*z.y*pos.w + y.y*z.z*pos.w;
- t.x .y=x.w*z.z*pos.y - x.z*z.w*pos.y - x.w*z.y*pos.z + x.y*z.w*pos.z + x.z*z.y*pos.w - x.y*z.z*pos.w;
- t.x .z=x.z*y.w*pos.y - x.w*y.z*pos.y + x.w*y.y*pos.z - x.y*y.w*pos.z - x.z*y.y*pos.w + x.y*y.z*pos.w;
- t.x .w=x.w*y.z* z.y - x.z*y.w* z.y - x.w*y.y* z.z + x.y*y.w* z.z + x.z*y.y* z.w - x.y*y.z* z.w;
- t.y .x=y.w*z.z*pos.x - y.z*z.w*pos.x - y.w*z.x*pos.z + y.x*z.w*pos.z + y.z*z.x*pos.w - y.x*z.z*pos.w;
- t.y .y=x.z*z.w*pos.x - x.w*z.z*pos.x + x.w*z.x*pos.z - x.x*z.w*pos.z - x.z*z.x*pos.w + x.x*z.z*pos.w;
- t.y .z=x.w*y.z*pos.x - x.z*y.w*pos.x - x.w*y.x*pos.z + x.x*y.w*pos.z + x.z*y.x*pos.w - x.x*y.z*pos.w;
- t.y .w=x.z*y.w* z.x - x.w*y.z* z.x + x.w*y.x* z.z - x.x*y.w* z.z - x.z*y.x* z.w + x.x*y.z* z.w;
- t.z .x=y.y*z.w*pos.x - y.w*z.y*pos.x + y.w*z.x*pos.y - y.x*z.w*pos.y - y.y*z.x*pos.w + y.x*z.y*pos.w;
- t.z .y=x.w*z.y*pos.x - x.y*z.w*pos.x - x.w*z.x*pos.y + x.x*z.w*pos.y + x.y*z.x*pos.w - x.x*z.y*pos.w;
- t.z .z=x.y*y.w*pos.x - x.w*y.y*pos.x + x.w*y.x*pos.y - x.x*y.w*pos.y - x.y*y.x*pos.w + x.x*y.y*pos.w;
- t.z .w=x.w*y.y* z.x - x.y*y.w* z.x - x.w*y.x* z.y + x.x*y.w* z.y + x.y*y.x* z.w - x.x*y.y* z.w;
- t.pos.x=y.z*z.y*pos.x - y.y*z.z*pos.x - y.z*z.x*pos.y + y.x*z.z*pos.y + y.y*z.x*pos.z - y.x*z.y*pos.z;
- t.pos.y=x.y*z.z*pos.x - x.z*z.y*pos.x + x.z*z.x*pos.y - x.x*z.z*pos.y - x.y*z.x*pos.z + x.x*z.y*pos.z;
- t.pos.z=x.z*y.y*pos.x - x.y*y.z*pos.x - x.z*y.x*pos.y + x.x*y.z*pos.y + x.y*y.x*pos.z - x.x*y.y*pos.z;
- t.pos.w=x.y*y.z* z.x - x.z*y.y* z.x + x.z*y.x* z.y - x.x*y.z* z.y - x.y*y.x* z.z + x.x*y.y* z.z;
- det=1/det;
- x =t.x *det;
- y =t.y *det;
- z =t.z *det;
- pos=t.pos*det;
- }
- return T;
- }
- Matrix4& Matrix4::transpose()
- {
- Swap(x.y, y.x);
- Swap(x.z, z.x);
- Swap(x.w, pos.x);
- Swap(y.z, z.y);
- Swap(y.w, pos.y);
- Swap(z.w, pos.z);
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::normalize()
- {
- if(!x.normalize()
- || !y.normalize()
- || !z.normalize())identity();
- return T;
- }
- MatrixD3& MatrixD3::normalize()
- {
- if(!x.normalize()
- || !y.normalize()
- || !z.normalize())identity();
- return T;
- }
- Matrix3& Matrix3::normalize(Flt scale)
- {
- if(!x.setLength(scale)
- || !y.setLength(scale)
- || !z.setLength(scale))setScale(scale);
- return T;
- }
- MatrixD3& MatrixD3::normalize(Dbl scale)
- {
- if(!x.setLength(scale)
- || !y.setLength(scale)
- || !z.setLength(scale))setScale(scale);
- return T;
- }
- Matrix3& Matrix3::normalize(C Vec &scale)
- {
- if(!x.setLength(scale.x)
- || !y.setLength(scale.y)
- || !z.setLength(scale.z))setScale(scale);
- return T;
- }
- MatrixD3& MatrixD3::normalize(C VecD &scale)
- {
- if(!x.setLength(scale.x)
- || !y.setLength(scale.y)
- || !z.setLength(scale.z))setScale(scale);
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::scaleL(C Vec &scale)
- {
- x*=scale.x;
- y*=scale.y;
- z*=scale.z;
- return T;
- }
- Matrix& Matrix::scaleL(C Vec &scale)
- {
- // adjust position before scaling axes, in case 'scale' is zero and would destroy them
- Vec dir; Flt d;
- if(d=scale.x-1){dir=x; dir.normalize(); pos+=dir*(DistPointPlane(pos, dir)*d);}
- if(d=scale.y-1){dir=y; dir.normalize(); pos+=dir*(DistPointPlane(pos, dir)*d);}
- if(d=scale.z-1){dir=z; dir.normalize(); pos+=dir*(DistPointPlane(pos, dir)*d);}
- super::scaleL(scale);
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::scale(C Vec &dir, Flt scale)
- {
- //Flt plane_dist=DistPointPlane(axis, dir); Flt wanted_plane_dist=plane_dist*scale; axis+=dir*(wanted_plane_dist-plane_dist);
- //axis+=dir*(DistPointPlane(axis, dir)*(scale-1));
- scale--;
- x+=dir*(DistPointPlane(x, dir)*scale);
- y+=dir*(DistPointPlane(y, dir)*scale);
- z+=dir*(DistPointPlane(z, dir)*scale);
- return T;
- }
- MatrixD3& MatrixD3::scale(C VecD &dir, Dbl scale)
- {
- scale--;
- x+=dir*(DistPointPlane(x, dir)*scale);
- y+=dir*(DistPointPlane(y, dir)*scale);
- z+=dir*(DistPointPlane(z, dir)*scale);
- return T;
- }
- Matrix& Matrix::scale(C Vec &dir, Flt scale)
- {
- scale--;
- x +=dir*(DistPointPlane(x , dir)*scale);
- y +=dir*(DistPointPlane(y , dir)*scale);
- z +=dir*(DistPointPlane(z , dir)*scale);
- pos+=dir*(DistPointPlane(pos, dir)*scale);
- return T;
- }
- MatrixD& MatrixD::scale(C VecD &dir, Dbl scale)
- {
- scale--;
- x +=dir*(DistPointPlane(x , dir)*scale);
- y +=dir*(DistPointPlane(y , dir)*scale);
- z +=dir*(DistPointPlane(z , dir)*scale);
- pos+=dir*(DistPointPlane(pos, dir)*scale);
- return T;
- }
- Matrix3& Matrix3::scalePlane(C Vec &nrm, Flt scale)
- {
- T.scale(scale ); // first scale globally
- if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
- return T;
- }
- MatrixD3& MatrixD3::scalePlane(C VecD &nrm, Dbl scale)
- {
- T.scale(scale ); // first scale globally
- if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
- return T;
- }
- Matrix& Matrix::scalePlane(C Vec &nrm, Flt scale)
- {
- T.scale(scale ); // first scale globally
- if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
- return T;
- }
- MatrixD& MatrixD::scalePlane(C VecD &nrm, Dbl scale)
- {
- T.scale(scale ); // first scale globally
- if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::rotateX(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt y=T.x.y, z=T.x.z;
- T.x.y=y*cos - z*sin;
- T.x.z=y*sin + z*cos;
- y=T.y.y; z=T.y.z;
- T.y.y=y*cos - z*sin;
- T.y.z=y*sin + z*cos;
- y=T.z.y; z=T.z.z;
- T.z.y=y*cos - z*sin;
- T.z.z=y*sin + z*cos;
- }
- return T;
- }
- Matrix3& Matrix3::rotateY(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt x=T.x.x, z=T.x.z;
- T.x.x=x*cos + z*sin;
- T.x.z=z*cos - x*sin;
- x=T.y.x; z=T.y.z;
- T.y.x=x*cos + z*sin;
- T.y.z=z*cos - x*sin;
- x=T.z.x; z=T.z.z;
- T.z.x=x*cos + z*sin;
- T.z.z=z*cos - x*sin;
- }
- return T;
- }
- Matrix3& Matrix3::rotateZ(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt x=T.x.x, y=T.x.y;
- T.x.x=x*cos - y*sin;
- T.x.y=y*cos + x*sin;
- x=T.y.x; y=T.y.y;
- T.y.x=x*cos - y*sin;
- T.y.y=y*cos + x*sin;
- x=T.z.x; y=T.z.y;
- T.z.x=x*cos - y*sin;
- T.z.y=y*cos + x*sin;
- }
- return T;
- }
- /******************************************************************************/
- MatrixD3& MatrixD3::rotateX(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- Dbl y=T.x.y, z=T.x.z;
- T.x.y=y*cos - z*sin;
- T.x.z=y*sin + z*cos;
- y=T.y.y; z=T.y.z;
- T.y.y=y*cos - z*sin;
- T.y.z=y*sin + z*cos;
- y=T.z.y; z=T.z.z;
- T.z.y=y*cos - z*sin;
- T.z.z=y*sin + z*cos;
- }
- return T;
- }
- MatrixD3& MatrixD3::rotateY(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- Dbl x=T.x.x, z=T.x.z;
- T.x.x=x*cos + z*sin;
- T.x.z=z*cos - x*sin;
- x=T.y.x; z=T.y.z;
- T.y.x=x*cos + z*sin;
- T.y.z=z*cos - x*sin;
- x=T.z.x; z=T.z.z;
- T.z.x=x*cos + z*sin;
- T.z.z=z*cos - x*sin;
- }
- return T;
- }
- MatrixD3& MatrixD3::rotateZ(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- Dbl x=T.x.x, y=T.x.y;
- T.x.x=x*cos - y*sin;
- T.x.y=y*cos + x*sin;
- x=T.y.x; y=T.y.y;
- T.y.x=x*cos - y*sin;
- T.y.y=y*cos + x*sin;
- x=T.z.x; y=T.z.y;
- T.z.x=x*cos - y*sin;
- T.z.y=y*cos + x*sin;
- }
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::rotateX(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt y=T.x.y, z=T.x.z;
- T.x.y=y*cos - z*sin;
- T.x.z=y*sin + z*cos;
- y=T.y.y; z=T.y.z;
- T.y.y=y*cos - z*sin;
- T.y.z=y*sin + z*cos;
- y=T.z.y; z=T.z.z;
- T.z.y=y*cos - z*sin;
- T.z.z=y*sin + z*cos;
- y=pos.y; z=pos.z;
- pos.y=y*cos - z*sin;
- pos.z=y*sin + z*cos;
- }
- return T;
- }
- Matrix& Matrix::rotateY(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt x=T.x.x, z=T.x.z;
- T.x.x=x*cos + z*sin;
- T.x.z=z*cos - x*sin;
- x=T.y.x; z=T.y.z;
- T.y.x=x*cos + z*sin;
- T.y.z=z*cos - x*sin;
- x=T.z.x; z=T.z.z;
- T.z.x=x*cos + z*sin;
- T.z.z=z*cos - x*sin;
- x=pos.x; z=pos.z;
- pos.x=x*cos + z*sin;
- pos.z=z*cos - x*sin;
- }
- return T;
- }
- Matrix& Matrix::rotateZ(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt x=T.x.x, y=T.x.y;
- T.x.x=x*cos - y*sin;
- T.x.y=y*cos + x*sin;
- x=T.y.x; y=T.y.y;
- T.y.x=x*cos - y*sin;
- T.y.y=y*cos + x*sin;
- x=T.z.x; y=T.z.y;
- T.z.x=x*cos - y*sin;
- T.z.y=y*cos + x*sin;
- x=pos.x; y=pos.y;
- pos.x=x*cos - y*sin;
- pos.y=y*cos + x*sin;
- }
- return T;
- }
- /******************************************************************************/
- MatrixM& MatrixM::rotateX(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt y=T.x.y, z=T.x.z;
- T.x.y=y*cos - z*sin;
- T.x.z=y*sin + z*cos;
- y=T.y.y; z=T.y.z;
- T.y.y=y*cos - z*sin;
- T.y.z=y*sin + z*cos;
- y=T.z.y; z=T.z.z;
- T.z.y=y*cos - z*sin;
- T.z.z=y*sin + z*cos;
- {
- Dbl y=pos.y, z=pos.z;
- pos.y=y*cos - z*sin;
- pos.z=y*sin + z*cos;
- }
- }
- return T;
- }
- MatrixM& MatrixM::rotateY(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt x=T.x.x, z=T.x.z;
- T.x.x=x*cos + z*sin;
- T.x.z=z*cos - x*sin;
- x=T.y.x; z=T.y.z;
- T.y.x=x*cos + z*sin;
- T.y.z=z*cos - x*sin;
- x=T.z.x; z=T.z.z;
- T.z.x=x*cos + z*sin;
- T.z.z=z*cos - x*sin;
- {
- Dbl x=pos.x, z=pos.z;
- pos.x=x*cos + z*sin;
- pos.z=z*cos - x*sin;
- }
- }
- return T;
- }
- MatrixM& MatrixM::rotateZ(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Flt x=T.x.x, y=T.x.y;
- T.x.x=x*cos - y*sin;
- T.x.y=y*cos + x*sin;
- x=T.y.x; y=T.y.y;
- T.y.x=x*cos - y*sin;
- T.y.y=y*cos + x*sin;
- x=T.z.x; y=T.z.y;
- T.z.x=x*cos - y*sin;
- T.z.y=y*cos + x*sin;
- {
- Dbl x=pos.x, y=pos.y;
- pos.x=x*cos - y*sin;
- pos.y=y*cos + x*sin;
- }
- }
- return T;
- }
- /******************************************************************************/
- MatrixD& MatrixD::rotateX(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- Dbl y=T.x.y, z=T.x.z;
- T.x.y=y*cos - z*sin;
- T.x.z=y*sin + z*cos;
- y=T.y.y; z=T.y.z;
- T.y.y=y*cos - z*sin;
- T.y.z=y*sin + z*cos;
- y=T.z.y; z=T.z.z;
- T.z.y=y*cos - z*sin;
- T.z.z=y*sin + z*cos;
- y=pos.y; z=pos.z;
- pos.y=y*cos - z*sin;
- pos.z=y*sin + z*cos;
- }
- return T;
- }
- MatrixD& MatrixD::rotateY(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- Dbl x=T.x.x, z=T.x.z;
- T.x.x=x*cos + z*sin;
- T.x.z=z*cos - x*sin;
- x=T.y.x; z=T.y.z;
- T.y.x=x*cos + z*sin;
- T.y.z=z*cos - x*sin;
- x=T.z.x; z=T.z.z;
- T.z.x=x*cos + z*sin;
- T.z.z=z*cos - x*sin;
- x=pos.x; z=pos.z;
- pos.x=x*cos + z*sin;
- pos.z=z*cos - x*sin;
- }
- return T;
- }
- MatrixD& MatrixD::rotateZ(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- Dbl x=T.x.x, y=T.x.y;
- T.x.x=x*cos - y*sin;
- T.x.y=y*cos + x*sin;
- x=T.y.x; y=T.y.y;
- T.y.x=x*cos - y*sin;
- T.y.y=y*cos + x*sin;
- x=T.z.x; y=T.z.y;
- T.z.x=x*cos - y*sin;
- T.z.y=y*cos + x*sin;
- x=pos.x; y=pos.y;
- pos.x=x*cos - y*sin;
- pos.y=y*cos + x*sin;
- }
- return T;
- }
- /******************************************************************************/
- Matrix3 & Matrix3 ::rotateXY(Flt x, Flt y) {if(Any(x, y ))T*=Matrix3 ().setRotateXY(x, y); return T;}
- MatrixD3& MatrixD3::rotateXY(Dbl x, Dbl y) {if(Any(x, y ))T*=MatrixD3().setRotateXY(x, y); return T;}
- Matrix & Matrix ::rotateXY(Flt x, Flt y) {if(Any(x, y ))T*=Matrix3 ().setRotateXY(x, y); return T;}
- MatrixM & MatrixM ::rotateXY(Flt x, Flt y) {if(Any(x, y ))T*=Matrix3 ().setRotateXY(x, y); return T;}
- MatrixD & MatrixD ::rotateXY(Dbl x, Dbl y) {if(Any(x, y ))T*=MatrixD3().setRotateXY(x, y); return T;}
- Matrix3 & Matrix3 ::rotate(C Vec &axis, Flt angle) {if(Any(angle))T*=Matrix3 ().setRotate(axis, angle); return T;}
- MatrixD3& MatrixD3::rotate(C VecD &axis, Dbl angle) {if(Any(angle))T*=MatrixD3().setRotate(axis, angle); return T;}
- Matrix & Matrix ::rotate(C Vec &axis, Flt angle) {if(Any(angle))T*=Matrix3 ().setRotate(axis, angle); return T;}
- MatrixM & MatrixM ::rotate(C Vec &axis, Flt angle) {if(Any(angle))T*=Matrix3 ().setRotate(axis, angle); return T;}
- MatrixD & MatrixD ::rotate(C VecD &axis, Dbl angle) {if(Any(angle))T*=MatrixD3().setRotate(axis, angle); return T;}
- Matrix3& Matrix3::rotateXL(Flt angle) {if(Any(angle))rotate(!x, angle); return T;}
- Matrix3& Matrix3::rotateYL(Flt angle) {if(Any(angle))rotate(!y, angle); return T;}
- Matrix3& Matrix3::rotateZL(Flt angle) {if(Any(angle))rotate(!z, angle); return T;}
- MatrixD3& MatrixD3::rotateXL(Dbl angle) {if(Any(angle))rotate(!x, angle); return T;}
- MatrixD3& MatrixD3::rotateYL(Dbl angle) {if(Any(angle))rotate(!y, angle); return T;}
- MatrixD3& MatrixD3::rotateZL(Dbl angle) {if(Any(angle))rotate(!z, angle); return T;}
- /******************************************************************************/
- Matrix3& Matrix3::rotateXLOrthoNormalized(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Vec z=T.z;
- T.z=z*cos - y*sin;
- T.y=z*sin + y*cos;
- }
- return T;
- }
- Matrix3& Matrix3::rotateXLOrthoNormalized(Flt cos, Flt sin)
- {
- Vec z=T.z;
- T.z=z*cos - y*sin;
- T.y=z*sin + y*cos;
- return T;
- }
- Matrix3& Matrix3::rotateYLOrthoNormalized(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Vec x=T.x;
- T.x=x*cos - z*sin;
- T.z=x*sin + z*cos;
- }
- return T;
- }
- Matrix3& Matrix3::rotateZLOrthoNormalized(Flt angle)
- {
- if(Any(angle))
- {
- Flt cos, sin; CosSin(cos, sin, angle);
- Vec y=T.y;
- T.y=y*cos - x*sin;
- T.x=y*sin + x*cos;
- }
- return T;
- }
- /******************************************************************************/
- MatrixD3& MatrixD3::rotateXLOrthoNormalized(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- VecD z=T.z;
- T.z=z*cos - y*sin;
- T.y=z*sin + y*cos;
- }
- return T;
- }
- MatrixD3& MatrixD3::rotateYLOrthoNormalized(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- VecD x=T.x;
- T.x=x*cos - z*sin;
- T.z=x*sin + z*cos;
- }
- return T;
- }
- MatrixD3& MatrixD3::rotateZLOrthoNormalized(Dbl angle)
- {
- if(Any(angle))
- {
- Dbl cos, sin; CosSin(cos, sin, angle);
- VecD y=T.y;
- T.y=y*cos - x*sin;
- T.x=y*sin + x*cos;
- }
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::zero()
- {
- x.zero();
- y.zero();
- z.zero();
- return T;
- }
- MatrixD3& MatrixD3::zero()
- {
- x.zero();
- y.zero();
- z.zero();
- return T;
- }
- Matrix& Matrix::zero()
- {
- x .zero();
- y .zero();
- z .zero();
- pos.zero();
- return T;
- }
- MatrixM& MatrixM::zero()
- {
- x .zero();
- y .zero();
- z .zero();
- pos.zero();
- return T;
- }
- MatrixD& MatrixD::zero()
- {
- x .zero();
- y .zero();
- z .zero();
- pos.zero();
- return T;
- }
- Matrix4& Matrix4::zero()
- {
- x .zero();
- y .zero();
- z .zero();
- pos.zero();
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::identity()
- {
- x.set(1, 0, 0);
- y.set(0, 1, 0);
- z.set(0, 0, 1);
- return T;
- }
- MatrixD3& MatrixD3::identity()
- {
- x.set(1, 0, 0);
- y.set(0, 1, 0);
- z.set(0, 0, 1);
- return T;
- }
- Matrix& Matrix::identity()
- {
- x .set (1, 0, 0);
- y .set (0, 1, 0);
- z .set (0, 0, 1);
- pos.zero( );
- return T;
- }
- MatrixM& MatrixM::identity()
- {
- x .set (1, 0, 0);
- y .set (0, 1, 0);
- z .set (0, 0, 1);
- pos.zero( );
- return T;
- }
- MatrixD& MatrixD::identity()
- {
- x .set (1, 0, 0);
- y .set (0, 1, 0);
- z .set (0, 0, 1);
- pos.zero( );
- return T;
- }
- Matrix4& Matrix4::identity()
- {
- x .set(1, 0, 0, 0);
- y .set(0, 1, 0, 0);
- z .set(0, 0, 1, 0);
- pos.set(0, 0, 0, 1);
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::identity(Flt blend)
- {
- if(blend>0)
- {
- if(blend>=1)identity();else
- {
- Flt blend1=1-blend;
- // orientation
- Vec axis; Flt angle=axisAngle(axis);
- // scale
- Vec scale=T.scale();
- REPA(scale)scale.c[i]=ScaleFactor(ScaleFactorR(scale.c[i])*blend1);
- setRotate(axis, angle*blend1).scaleL(scale);
- }
- }
- return T;
- }
- Matrix& Matrix::identity(Flt blend)
- {
- if(blend>0)
- {
- if(blend>=1)identity();else
- {
- pos*=1-blend;
- super::identity(blend);
- }
- }
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setPos(Flt x, Flt y, Flt z)
- {
- T.pos.set(x, y, z);
- T.x .set(1, 0, 0);
- T.y .set(0, 1, 0);
- T.z .set(0, 0, 1);
- return T;
- }
- MatrixM& MatrixM::setPos(Dbl x, Dbl y, Dbl z)
- {
- T.pos.set(x, y, z);
- T.x .set(1, 0, 0);
- T.y .set(0, 1, 0);
- T.z .set(0, 0, 1);
- return T;
- }
- MatrixD& MatrixD::setPos(Dbl x, Dbl y, Dbl z)
- {
- T.pos.set(x, y, z);
- T.x .set(1, 0, 0);
- T.y .set(0, 1, 0);
- T.z .set(0, 0, 1);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setPos(C Vec2 &pos)
- {
- T.pos.set(pos , 0);
- x .set(1, 0, 0);
- y .set(0, 1, 0);
- z .set(0, 0, 1);
- return T;
- }
- MatrixM& MatrixM::setPos(C VecD2 &pos)
- {
- T.pos.set(pos , 0);
- x .set(1, 0, 0);
- y .set(0, 1, 0);
- z .set(0, 0, 1);
- return T;
- }
- MatrixD& MatrixD::setPos(C VecD2 &pos)
- {
- T.pos.set(pos , 0);
- x .set(1, 0, 0);
- y .set(0, 1, 0);
- z .set(0, 0, 1);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setPos(C Vec &pos)
- {
- T.pos=pos;
- x .set(1, 0, 0);
- y .set(0, 1, 0);
- z .set(0, 0, 1);
- return T;
- }
- MatrixM& MatrixM::setPos(C VecD &pos)
- {
- T.pos=pos;
- x .set(1, 0, 0);
- y .set(0, 1, 0);
- z .set(0, 0, 1);
- return T;
- }
- MatrixD& MatrixD::setPos(C VecD &pos)
- {
- T.pos=pos;
- x .set(1, 0, 0);
- y .set(0, 1, 0);
- z .set(0, 0, 1);
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::setScale(Flt scale)
- {
- x.set(scale, 0, 0);
- y.set(0, scale, 0);
- z.set(0, 0, scale);
- return T;
- }
- MatrixD3& MatrixD3::setScale(Dbl scale)
- {
- x.set(scale, 0, 0);
- y.set(0, scale, 0);
- z.set(0, 0, scale);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setScale(Flt scale)
- {
- x .set (scale, 0, 0);
- y .set (0, scale, 0);
- z .set (0, 0, scale);
- pos.zero( );
- return T;
- }
- MatrixM& MatrixM::setScale(Flt scale)
- {
- x .set (scale, 0, 0);
- y .set (0, scale, 0);
- z .set (0, 0, scale);
- pos.zero( );
- return T;
- }
- MatrixD& MatrixD::setScale(Dbl scale)
- {
- x .set (scale, 0, 0);
- y .set (0, scale, 0);
- z .set (0, 0, scale);
- pos.zero( );
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::setScale(C Vec &scale)
- {
- x.set(scale.x, 0, 0);
- y.set(0, scale.y, 0);
- z.set(0, 0, scale.z);
- return T;
- }
- MatrixD3& MatrixD3::setScale(C VecD &scale)
- {
- x.set(scale.x, 0, 0);
- y.set(0, scale.y, 0);
- z.set(0, 0, scale.z);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setScale(C Vec &scale)
- {
- x .set (scale.x, 0, 0);
- y .set (0, scale.y, 0);
- z .set (0, 0, scale.z);
- pos.zero( );
- return T;
- }
- MatrixM& MatrixM::setScale(C Vec &scale)
- {
- x .set (scale.x, 0, 0);
- y .set (0, scale.y, 0);
- z .set (0, 0, scale.z);
- pos.zero( );
- return T;
- }
- MatrixD& MatrixD::setScale(C VecD &scale)
- {
- x .set (scale.x, 0, 0);
- y .set (0, scale.y, 0);
- z .set (0, 0, scale.z);
- pos.zero( );
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setPosScale(C Vec &pos, Flt scale)
- {
- T.pos.set(pos.x*scale, pos.y*scale, pos.z*scale);
- x .set(scale, 0, 0);
- y .set(0, scale, 0);
- z .set(0, 0, scale);
- return T;
- }
- MatrixM& MatrixM::setPosScale(C VecD &pos, Flt scale)
- {
- T.pos.set(pos.x*scale, pos.y*scale, pos.z*scale);
- x .set(scale, 0, 0);
- y .set(0, scale, 0);
- z .set(0, 0, scale);
- return T;
- }
- MatrixD& MatrixD::setPosScale(C VecD &pos, Dbl scale)
- {
- T.pos.set(pos.x*scale, pos.y*scale, pos.z*scale);
- x .set(scale, 0, 0);
- y .set(0, scale, 0);
- z .set(0, 0, scale);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setPosScale(C Vec &pos, C Vec &scale)
- {
- T.pos.set(pos.x*scale.x, pos.y*scale.y, pos.z*scale.z);
- x .set(scale.x, 0, 0);
- y .set(0, scale.y, 0);
- z .set(0, 0, scale.z);
- return T;
- }
- MatrixM& MatrixM::setPosScale(C VecD &pos, C Vec &scale)
- {
- T.pos.set(pos.x*scale.x, pos.y*scale.y, pos.z*scale.z);
- x .set(scale.x, 0, 0);
- y .set(0, scale.y, 0);
- z .set(0, 0, scale.z);
- return T;
- }
- MatrixD& MatrixD::setPosScale(C VecD &pos, C VecD &scale)
- {
- T.pos.set(pos.x*scale.x, pos.y*scale.y, pos.z*scale.z);
- x .set(scale.x, 0, 0);
- y .set(0, scale.y, 0);
- z .set(0, 0, scale.z);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setScalePos(Flt scale, C Vec &pos)
- {
- T.pos=pos;
- x .set(scale, 0, 0);
- y .set(0, scale, 0);
- z .set(0, 0, scale);
- return T;
- }
- MatrixM& MatrixM::setScalePos(Flt scale, C VecD &pos)
- {
- T.pos=pos;
- x .set(scale, 0, 0);
- y .set(0, scale, 0);
- z .set(0, 0, scale);
- return T;
- }
- MatrixD& MatrixD::setScalePos(Dbl scale, C VecD &pos)
- {
- T.pos=pos;
- x .set(scale, 0, 0);
- y .set(0, scale, 0);
- z .set(0, 0, scale);
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setScalePos(C Vec &scale, C Vec &pos)
- {
- T.pos=pos;
- x .set(scale.x, 0, 0);
- y .set(0, scale.y, 0);
- z .set(0, 0, scale.z);
- return T;
- }
- MatrixM& MatrixM::setScalePos(C Vec &scale, C VecD &pos)
- {
- T.pos=pos;
- x .set(scale.x, 0, 0);
- y .set(0, scale.y, 0);
- z .set(0, 0, scale.z);
- return T;
- }
- MatrixD& MatrixD::setScalePos(C VecD &scale, C VecD &pos)
- {
- T.pos=pos;
- x .set(scale.x, 0, 0);
- y .set(0, scale.y, 0);
- z .set(0, 0, scale.z);
- return T;
- }
- /******************************************************************************/
- Matrix3& Matrix3::setRotateX(Flt angle)
- {
- Flt c, s; CosSin(c, s, angle);
- x.set( 1, 0, 0);
- y.set( 0, c, s);
- z.set( 0,-s, c);
- return T;
- }
- MatrixD3& MatrixD3::setRotateX(Dbl angle)
- {
- Dbl c, s; CosSin(c, s, angle);
- x.set( 1, 0, 0);
- y.set( 0, c, s);
- z.set( 0,-s, c);
- return T;
- }
- Matrix3& Matrix3::setRotateY(Flt angle)
- {
- Flt c, s; CosSin(c, s, angle);
- x.set( c, 0,-s);
- y.set( 0, 1, 0);
- z.set( s, 0, c);
- return T;
- }
- MatrixD3& MatrixD3::setRotateY(Dbl angle)
- {
- Dbl c, s; CosSin(c, s, angle);
- x.set( c, 0,-s);
- y.set( 0, 1, 0);
- z.set( s, 0, c);
- return T;
- }
- Matrix3& Matrix3::setRotateZ(Flt angle)
- {
- Flt c, s; CosSin(c, s, angle);
- x.set( c, s, 0);
- y.set(-s, c, 0);
- z.set( 0, 0, 1);
- return T;
- }
- MatrixD3& MatrixD3::setRotateZ(Dbl angle)
- {
- Dbl c, s; CosSin(c, s, angle);
- x.set( c, s, 0);
- y.set(-s, c, 0);
- z.set( 0, 0, 1);
- return T;
- }
- Matrix3& Matrix3::setRotateXY(Flt pitch, Flt yaw)
- {
- Flt cp, sp; CosSin(cp, sp, pitch);
- Flt cy, sy; CosSin(cy, sy, yaw );
- y.x= sp*sy;
- y.z= sp*cy;
- z.x= cp*sy;
- z.z= cp*cy;
- y.y= cp;
- x.x= cy;
- z.y=-sp;
- x.z=-sy;
- x.y= 0;
- return T;
- }
- MatrixD3& MatrixD3::setRotateXY(Dbl pitch, Dbl yaw)
- {
- Dbl cp, sp; CosSin(cp, sp, pitch);
- Dbl cy, sy; CosSin(cy, sy, yaw );
- y.x= sp*sy;
- y.z= sp*cy;
- z.x= cp*sy;
- z.z= cp*cy;
- y.y= cp;
- x.x= cy;
- z.y=-sp;
- x.z=-sy;
- x.y= 0;
- return T;
- }
- Matrix3& Matrix3::setRotate(C Vec &axis, Flt angle)
- {
- Flt c, s; CosSin(c, s, -angle); // !! this must match 'setRotateCosSin' !!
- Flt x =axis.x,
- y =axis.y,
- z =axis.z,
- cc = 1-c,
- ccx=cc*x,
- ccy=cc*y,
- ccz=cc*z,
- p, q;
- T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
- p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
- p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
- p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
- return T;
- }
- MatrixD3& MatrixD3::setRotate(C VecD &axis, Dbl angle)
- {
- Dbl c, s; CosSin(c, s, -angle); // !! this must match 'setRotateCosSin' !!
- Dbl x =axis.x,
- y =axis.y,
- z =axis.z,
- cc = 1-c,
- ccx=cc*x,
- ccy=cc*y,
- ccz=cc*z,
- p, q;
- T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
- p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
- p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
- p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
- return T;
- }
- Matrix3& Matrix3::setRotateCosSin(C Vec &axis, Flt cos, Flt sin)
- {
- Flt c=cos, s=-sin; // !! this must match 'setRotate' !!
- Flt x =axis.x,
- y =axis.y,
- z =axis.z,
- cc = 1-c,
- ccx=cc*x,
- ccy=cc*y,
- ccz=cc*z,
- p, q;
- T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
- p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
- p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
- p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
- return T;
- }
- MatrixD3& MatrixD3::setRotateCosSin(C VecD &axis, Dbl cos, Dbl sin)
- {
- Dbl c=cos, s=-sin; // !! this must match 'setRotate' !!
- Dbl x =axis.x,
- y =axis.y,
- z =axis.z,
- cc = 1-c,
- ccx=cc*x,
- ccy=cc*y,
- ccz=cc*z,
- p, q;
- T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
- p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
- p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
- p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::setRotateX ( Flt angle) {super::setRotateX ( angle); pos.zero(); return T;}
- Matrix& Matrix::setRotateY ( Flt angle) {super::setRotateY ( angle); pos.zero(); return T;}
- Matrix& Matrix::setRotateZ ( Flt angle) {super::setRotateZ ( angle); pos.zero(); return T;}
- Matrix& Matrix::setRotateXY( Flt x , Flt y ) {super::setRotateXY(x , y ); pos.zero(); return T;}
- Matrix& Matrix::setRotate (C Vec &axis, Flt angle) {super::setRotate (axis, angle); pos.zero(); return T;}
- MatrixM& MatrixM::setRotateX ( Flt angle) {super::setRotateX ( angle); pos.zero(); return T;}
- MatrixM& MatrixM::setRotateY ( Flt angle) {super::setRotateY ( angle); pos.zero(); return T;}
- MatrixM& MatrixM::setRotateZ ( Flt angle) {super::setRotateZ ( angle); pos.zero(); return T;}
- MatrixM& MatrixM::setRotateXY( Flt x , Flt y ) {super::setRotateXY(x , y ); pos.zero(); return T;}
- MatrixM& MatrixM::setRotate (C Vec &axis, Flt angle) {super::setRotate (axis, angle); pos.zero(); return T;}
- MatrixD& MatrixD::setRotateX ( Dbl angle) {super::setRotateX ( angle); pos.zero(); return T;}
- MatrixD& MatrixD::setRotateY ( Dbl angle) {super::setRotateY ( angle); pos.zero(); return T;}
- MatrixD& MatrixD::setRotateZ ( Dbl angle) {super::setRotateZ ( angle); pos.zero(); return T;}
- MatrixD& MatrixD::setRotateXY( Dbl x , Dbl y ) {super::setRotateXY(x , y ); pos.zero(); return T;}
- MatrixD& MatrixD::setRotate (C VecD &axis, Dbl angle) {super::setRotate (axis, angle); pos.zero(); return T;}
- /******************************************************************************/
- Matrix3& Matrix3::setOrient(DIR_ENUM dir)
- {
- Zero(T); switch(dir)
- {
- default : x.x= 1; y.y= 1; z.z= 1; break;
- case DIR_BACK : x.x=-1; y.y= 1; z.z=-1; break;
- case DIR_UP : x.x= 1; y.z=-1; z.y= 1; break;
- case DIR_DOWN : x.x= 1; y.z= 1; z.y=-1; break;
- case DIR_RIGHT: x.z=-1; y.y= 1; z.x= 1; break;
- case DIR_LEFT : x.z= 1; y.y= 1; z.x=-1; break;
- }
- return T;
- }
- MatrixD3& MatrixD3::setOrient(DIR_ENUM dir)
- {
- Zero(T); switch(dir)
- {
- default : x.x= 1; y.y= 1; z.z= 1; break;
- case DIR_BACK : x.x=-1; y.y= 1; z.z=-1; break;
- case DIR_UP : x.x= 1; y.z=-1; z.y= 1; break;
- case DIR_DOWN : x.x= 1; y.z= 1; z.y=-1; break;
- case DIR_RIGHT: x.z=-1; y.y= 1; z.x= 1; break;
- case DIR_LEFT : x.z= 1; y.y= 1; z.x=-1; break;
- }
- return T;
- }
- Matrix3 & Matrix3 ::setRight(C Vec &right ) {x=right; y=PerpN(x); z=Cross(x, y); return T;}
- MatrixD3& MatrixD3::setRight(C VecD &right ) {x=right; y=PerpN(x); z=Cross(x, y); return T;}
- Matrix3 & Matrix3 ::setUp (C Vec &up ) {y=up ; z=PerpN(y); x=Cross(y, z); return T;}
- MatrixD3& MatrixD3::setUp (C VecD &up ) {y=up ; z=PerpN(y); x=Cross(y, z); return T;}
- Matrix3 & Matrix3 ::setDir (C Vec &dir ) {z=dir ; y=PerpN(z); x=Cross(y, z); return T;}
- MatrixD3& MatrixD3::setDir (C VecD &dir ) {z=dir ; y=PerpN(z); x=Cross(y, z); return T;}
- Matrix3 & Matrix3 ::setDir (C Vec &dir, C Vec &up ) {z=dir ; y=up; x=Cross(y, z); return T;}
- MatrixD3& MatrixD3::setDir (C VecD &dir, C VecD &up ) {z=dir ; y=up; x=Cross(y, z); return T;}
- Matrix3 & Matrix3 ::setDir (C Vec &dir, C Vec &up, C Vec &right) {z=dir ; y=up; x=right ; return T;}
- MatrixD3& MatrixD3::setDir (C VecD &dir, C VecD &up, C VecD &right) {z=dir ; y=up; x=right ; return T;}
- Matrix & Matrix ::setPosOrient(C Vec &pos, DIR_ENUM dir ) {super::setOrient(dir ); T.pos=pos; return T;}
- MatrixM& MatrixM::setPosOrient(C VecD &pos, DIR_ENUM dir ) {super::setOrient(dir ); T.pos=pos; return T;}
- MatrixD& MatrixD::setPosOrient(C VecD &pos, DIR_ENUM dir ) {super::setOrient(dir ); T.pos=pos; return T;}
- Matrix & Matrix ::setPosRight (C Vec &pos, C Vec &right ) {super::setRight (right ); T.pos=pos; return T;}
- MatrixM& MatrixM::setPosRight (C VecD &pos, C Vec &right ) {super::setRight (right ); T.pos=pos; return T;}
- MatrixD& MatrixD::setPosRight (C VecD &pos, C VecD &right ) {super::setRight (right ); T.pos=pos; return T;}
- Matrix & Matrix ::setPosUp (C Vec &pos, C Vec &up ) {super::setUp (up ); T.pos=pos; return T;}
- MatrixM& MatrixM::setPosUp (C VecD &pos, C Vec &up ) {super::setUp (up ); T.pos=pos; return T;}
- MatrixD& MatrixD::setPosUp (C VecD &pos, C VecD &up ) {super::setUp (up ); T.pos=pos; return T;}
- Matrix & Matrix ::setPosDir (C Vec &pos, C Vec &dir ) {super::setDir (dir ); T.pos=pos; return T;}
- MatrixM& MatrixM::setPosDir (C VecD &pos, C Vec &dir ) {super::setDir (dir ); T.pos=pos; return T;}
- MatrixD& MatrixD::setPosDir (C VecD &pos, C VecD &dir ) {super::setDir (dir ); T.pos=pos; return T;}
- Matrix & Matrix ::setPosDir (C Vec &pos, C Vec &dir, C Vec &up ) {super::setDir (dir, up); T.pos=pos; return T;}
- MatrixM& MatrixM::setPosDir (C VecD &pos, C Vec &dir, C Vec &up ) {super::setDir (dir, up); T.pos=pos; return T;}
- MatrixD& MatrixD::setPosDir (C VecD &pos, C VecD &dir, C VecD &up ) {super::setDir (dir, up); T.pos=pos; return T;}
- Matrix & Matrix ::setPosDir (C Vec &pos, C Vec &dir, C Vec &up, C Vec &right) { z=dir; y=up; x=right; T.pos=pos; return T;}
- MatrixM& MatrixM::setPosDir (C VecD &pos, C Vec &dir, C Vec &up, C Vec &right) { z=dir; y=up; x=right; T.pos=pos; return T;}
- MatrixD& MatrixD::setPosDir (C VecD &pos, C VecD &dir, C VecD &up, C VecD &right) { z=dir; y=up; x=right; T.pos=pos; return T;}
- /******************************************************************************/
- Matrix3& Matrix3::setRotation(C Vec &dir_from, C Vec &dir_to)
- {
- Vec cross=Cross(dir_from, dir_to); if(Flt sin=cross.normalize())setRotateCosSin(cross, Dot(dir_from, dir_to), sin);else identity();
- return T;
- }
- MatrixD3& MatrixD3::setRotation(C VecD &dir_from, C VecD &dir_to)
- {
- VecD cross=Cross(dir_from, dir_to); if(Dbl sin=cross.normalize())setRotateCosSin(cross, Dot(dir_from, dir_to), sin);else identity();
- return T;
- }
- Matrix3& Matrix3::setRotation(C Vec &dir_from, C Vec &dir_to, Flt blend)
- {
- Vec cross=Cross(dir_from, dir_to); if(Flt sin=cross.normalize())setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
- return T;
- }
- MatrixD3& MatrixD3::setRotation(C VecD &dir_from, C VecD &dir_to, Dbl blend)
- {
- VecD cross=Cross(dir_from, dir_to); if(Dbl sin=cross.normalize())setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
- return T;
- }
- Matrix3& Matrix3::setRotation(C Vec &dir_from, C Vec &dir_to, Flt blend, Flt roll)
- {
- Vec cross=Cross(dir_from, dir_to); Flt sin=cross.normalize();
- if(roll)
- {
- setRotate(dir_from, roll);
- if(sin)rotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);
- }else
- {
- if(sin)setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
- }
- return T;
- }
- MatrixD3& MatrixD3::setRotation(C VecD &dir_from, C VecD &dir_to, Dbl blend, Dbl roll)
- {
- VecD cross=Cross(dir_from, dir_to); Dbl sin=cross.normalize();
- if(roll)
- {
- setRotate(dir_from, roll);
- if(sin)rotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);
- }else
- {
- if(sin)setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
- }
- return T;
- }
- Matrix& Matrix::setRotation(C Vec &pos, C Vec &dir_from, C Vec &dir_to, Flt blend)
- {
- Vec cross=Cross(dir_from, dir_to); if(Flt sin=cross.normalize()){orn().setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend); anchor(pos);}else identity();
- return T;
- }
- /******************************************************************************/
- Matrix& Matrix::set(C Box &src, C Box &dest)
- {
- Vec scale(dest.w()/src.w(),
- dest.h()/src.h(),
- dest.d()/src.d());
- super::setScale(scale);
- pos=dest.min-src.min*scale;
- return T;
- }
- Matrix& Matrix::setNormalizeX(C Box &box) {Flt d=box.w(); setPosScale(-box.center(), (d>EPS) ? 1/d : 1); return T;}
- Matrix& Matrix::setNormalizeY(C Box &box) {Flt d=box.h(); setPosScale(-box.center(), (d>EPS) ? 1/d : 1); return T;}
- Matrix& Matrix::setNormalizeZ(C Box &box) {Flt d=box.d(); setPosScale(-box.center(), (d>EPS) ? 1/d : 1); return T;}
- Matrix& Matrix::setNormalize (C Box &box)
- {
- Vec size=box.size();
- if( size.x>=size.y && size.x>=size.z)return setNormalizeX(box); // x is the biggest
- if( size.y>=size.z)return setNormalizeY(box); // y is the biggest
- return setNormalizeZ(box); // z is the biggest
- }
- /******************************************************************************/
- Matrix3& Matrix3::mirrorX()
- {
- CHS(x.x);
- CHS(y.x);
- CHS(z.x);
- return T;
- }
- Matrix3& Matrix3::mirrorY()
- {
- CHS(x.y);
- CHS(y.y);
- CHS(z.y);
- return T;
- }
- Matrix3& Matrix3::mirrorZ()
- {
- CHS(x.z);
- CHS(y.z);
- CHS(z.z);
- return T;
- }
- MatrixD3& MatrixD3::mirrorX()
- {
- CHS(x.x);
- CHS(y.x);
- CHS(z.x);
- return T;
- }
- MatrixD3& MatrixD3::mirrorY()
- {
- CHS(x.y);
- CHS(y.y);
- CHS(z.y);
- return T;
- }
- MatrixD3& MatrixD3::mirrorZ()
- {
- CHS(x.z);
- CHS(y.z);
- CHS(z.z);
- return T;
- }
- Matrix3& Matrix3::mirror(C Vec &normal)
- {
- x=Mirror(x, normal);
- y=Mirror(y, normal);
- z=Mirror(z, normal);
- return T;
- }
- MatrixD3& MatrixD3::mirror(C VecD &normal)
- {
- x=Mirror(x, normal);
- y=Mirror(y, normal);
- z=Mirror(z, normal);
- return T;
- }
- Matrix & Matrix ::mirrorX() {super::mirrorX(); CHS(pos.x); return T;}
- Matrix & Matrix ::mirrorY() {super::mirrorY(); CHS(pos.y); return T;}
- Matrix & Matrix ::mirrorZ() {super::mirrorZ(); CHS(pos.z); return T;}
- MatrixD& MatrixD::mirrorX() {super::mirrorX(); CHS(pos.x); return T;}
- MatrixD& MatrixD::mirrorY() {super::mirrorY(); CHS(pos.y); return T;}
- MatrixD& MatrixD::mirrorZ() {super::mirrorZ(); CHS(pos.z); return T;}
- Matrix & Matrix ::mirror(C Plane &plane) {super::mirror(plane.normal); pos=Mirror(pos, plane.pos, plane.normal); return T;}
- MatrixM& MatrixM::mirror(C PlaneM &plane) {super::mirror(plane.normal); pos=Mirror(pos, plane.pos, plane.normal); return T;}
- MatrixD& MatrixD::mirror(C PlaneD &plane) {super::mirror(plane.normal); pos=Mirror(pos, plane.pos, plane.normal); return T;}
- /******************************************************************************/
- Matrix3& Matrix3::swapYZ()
- {
- x.swapYZ();
- y.swapYZ();
- z.swapYZ();
- return T;
- }
- Matrix& Matrix::swapYZ()
- {
- super::swapYZ();
- pos .swapYZ();
- return T;
- }
- /******************************************************************************/
- Vec Matrix3 :: scale ()C {return Vec (x.length (), y.length (), z.length ()) ;}
- VecD MatrixD3:: scale ()C {return VecD(x.length (), y.length (), z.length ()) ;}
- Vec Matrix3 :: scale2()C {return Vec (x.length2(), y.length2(), z.length2()) ;}
- VecD MatrixD3:: scale2()C {return VecD(x.length2(), y.length2(), z.length2()) ;}
- Flt Matrix3 ::avgScale ()C {return Avg (x.length (), y.length (), z.length ()) ;}
- Dbl MatrixD3::avgScale ()C {return Avg (x.length (), y.length (), z.length ()) ;}
- Flt Matrix3 ::maxScale ()C {return SqrtFast(Max (x.length2(), y.length2(), z.length2()));}
- Dbl MatrixD3::maxScale ()C {return SqrtFast(Max (x.length2(), y.length2(), z.length2()));}
- Vec Matrix3::angles()C
- {
- Vec O;
- Matrix3 temp=T;
- O.y= Angle(temp.z.zx()); temp.rotateY(-O.y);
- O.x=-Angle(temp.z.zy()); temp.rotateX(-O.x);
- O.z= Angle(temp.x.xy );
- //if(){O.x=PI-O.x; O.y=O.y-PI; O.z=O.z-PI;} generates alternative version
- return O;
- }
- VecD MatrixD3::angles()C
- {
- VecD O;
- MatrixD3 temp=T;
- O.y= Angle(temp.z.zx()); temp.rotateY(-O.y);
- O.x=-Angle(temp.z.zy()); temp.rotateX(-O.x);
- O.z= Angle(temp.x.xy );
- return O;
- }
- Vec Matrix3::axis(Bool normalized)C
- {
- Matrix3 temp;
- C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- Vec axis(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- if(axis.normalize()<=EPS)
- {
- // singularity
- if(m->x.x>=EPS_COS)axis.set(1, 0, 0);else // identity
- {
- Flt xx=(m->x.x+1)*0.5f,
- yy=(m->y.y+1)*0.5f,
- zz=(m->z.z+1)*0.5f,
- xy=(m->x.y+m->y.x)*0.25f,
- xz=(m->x.z+m->z.x)*0.25f,
- yz=(m->y.z+m->z.y)*0.25f;
- if(xx>=yy && xx>=zz)
- {
- if(xx<=EPS)axis.set(0, SQRT2_2, SQRT2_2);else
- {
- axis.x=SqrtFast(xx);
- axis.y=xy/axis.x;
- axis.z=xz/axis.x;
- }
- }else
- if(yy>=zz)
- {
- if(yy<=EPS)axis.set(SQRT2_2, 0, SQRT2_2);else
- {
- axis.y=SqrtFast(yy);
- axis.x=xy/axis.y;
- axis.z=yz/axis.y;
- }
- }else
- {
- if(zz<=EPS)axis.set(SQRT2_2, SQRT2_2, 0);else
- {
- axis.z=SqrtFast(zz);
- axis.x=xz/axis.z;
- axis.y=yz/axis.z;
- }
- }
- }
- }
- return axis;
- }
- VecD MatrixD3::axis(Bool normalized)C
- {
- MatrixD3 temp;
- C MatrixD3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- VecD axis(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- if(axis.normalize()<=EPSD)
- {
- // singularity
- if(m->x.x>=EPSD_COS)axis.set(1, 0, 0);else // identity
- {
- Dbl xx=(m->x.x+1)*0.5,
- yy=(m->y.y+1)*0.5,
- zz=(m->z.z+1)*0.5,
- xy=(m->x.y+m->y.x)*0.25,
- xz=(m->x.z+m->z.x)*0.25,
- yz=(m->y.z+m->z.y)*0.25;
- if(xx>=yy && xx>=zz)
- {
- if(xx<=EPSD)axis.set(0, SQRT2_2, SQRT2_2);else
- {
- axis.x=SqrtFast(xx);
- axis.y=xy/axis.x;
- axis.z=xz/axis.x;
- }
- }else
- if(yy>=zz)
- {
- if(yy<=EPSD)axis.set(SQRT2_2, 0, SQRT2_2);else
- {
- axis.y=SqrtFast(yy);
- axis.x=xy/axis.y;
- axis.z=yz/axis.y;
- }
- }else
- {
- if(zz<=EPSD)axis.set(SQRT2_2, SQRT2_2, 0);else
- {
- axis.z=SqrtFast(zz);
- axis.x=xz/axis.z;
- axis.y=yz/axis.z;
- }
- }
- }
- }
- return axis;
- }
- Flt Matrix3::angle(Bool normalized)C
- {
- Matrix3 temp;
- C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- Vec axis(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- if(axis.length()> EPS )return Acos((m->x.x + m->y.y + m->z.z - 1)*0.5f);
- if(m->x.x >=EPS_COS)return 0;
- return PI;
- }
- Dbl MatrixD3::angle(Bool normalized)C
- {
- MatrixD3 temp;
- C MatrixD3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- VecD axis(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- if(axis.length()> EPSD )return Acos((m->x.x + m->y.y + m->z.z - 1)*0.5);
- if(m->x.x >=EPSD_COS)return 0;
- return PID;
- }
- Flt Matrix3::angleY(Bool normalized)C
- {
- Matrix3 temp;
- C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- Vec axis(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- if(axis.normalize()> EPS )return axis.y*Acos((m->x.x + m->y.y + m->z.z - 1)*0.5f);
- if(m->x.x >=EPS_COS)return 0; // identity
- Flt xx=m->x.x+1,
- yy=m->y.y+1,
- zz=m->z.z+1;
- if(xx>=yy && xx>=zz)return (xx<=EPS) ? SQRT2_2*PI : (m->x.y+m->y.x)/SqrtFast(xx)*(PI_4*SQRT2);
- if(yy>=zz )return (yy<=EPS) ? 0 : SqrtFast(yy)*(PI /SQRT2);
- return (zz<=EPS) ? SQRT2_2*PI : (m->y.z+m->z.y)/SqrtFast(zz)*(PI_4*SQRT2);
- }
- Flt Matrix3::axisAngle(Vec &axis, Bool normalized)C
- {
- Matrix3 temp;
- C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- axis.set(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- Flt angle;
- if(axis.normalize()>EPS)angle=Acos((m->x.x + m->y.y + m->z.z - 1)*0.5f);else
- {
- // singularity
- if(m->x.x>=EPS_COS) // identity
- {
- axis.set(1, 0, 0);
- angle=0;
- }else
- {
- Flt xx=(m->x.x+1)*0.5f,
- yy=(m->y.y+1)*0.5f,
- zz=(m->z.z+1)*0.5f,
- xy=(m->x.y+m->y.x)*0.25f,
- xz=(m->x.z+m->z.x)*0.25f,
- yz=(m->y.z+m->z.y)*0.25f;
- if(xx>=yy && xx>=zz)
- {
- if(xx<=EPS)axis.set(0, SQRT2_2, SQRT2_2);else
- {
- axis.x=SqrtFast(xx);
- axis.y=xy/axis.x;
- axis.z=xz/axis.x;
- }
- }else
- if(yy>=zz)
- {
- if(yy<=EPS)axis.set(SQRT2_2, 0, SQRT2_2);else
- {
- axis.y=SqrtFast(yy);
- axis.x=xy/axis.y;
- axis.z=yz/axis.y;
- }
- }else
- {
- if(zz<=EPS)axis.set(SQRT2_2, SQRT2_2, 0);else
- {
- axis.z=SqrtFast(zz);
- axis.x=xz/axis.z;
- axis.y=yz/axis.z;
- }
- }
- angle=PI;
- }
- }
- return angle;
- }
- Dbl MatrixD3::axisAngle(VecD &axis, Bool normalized)C
- {
- MatrixD3 temp;
- C MatrixD3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
- axis.set(m->y.z - m->z.y,
- m->z.x - m->x.z,
- m->x.y - m->y.x);
- Dbl angle;
- if(axis.normalize()>EPSD)angle=Acos((m->x.x + m->y.y + m->z.z - 1)*0.5);else
- {
- // singularity
- if(m->x.x>=EPSD_COS) // identity
- {
- axis.set(1, 0, 0);
- angle=0;
- }else
- {
- Dbl xx=(m->x.x+1)*0.5,
- yy=(m->y.y+1)*0.5,
- zz=(m->z.z+1)*0.5,
- xy=(m->x.y+m->y.x)*0.25,
- xz=(m->x.z+m->z.x)*0.25,
- yz=(m->y.z+m->z.y)*0.25;
- if(xx>=yy && xx>=zz)
- {
- if(xx<=EPSD)axis.set(0, SQRT2_2, SQRT2_2);else
- {
- axis.x=SqrtFast(xx);
- axis.y=xy/axis.x;
- axis.z=xz/axis.x;
- }
- }else
- if(yy>=zz)
- {
- if(yy<=EPSD)axis.set(SQRT2_2, 0, SQRT2_2);else
- {
- axis.y=SqrtFast(yy);
- axis.x=xy/axis.y;
- axis.z=yz/axis.y;
- }
- }else
- {
- if(zz<=EPSD)axis.set(SQRT2_2, SQRT2_2, 0);else
- {
- axis.z=SqrtFast(zz);
- axis.x=xz/axis.z;
- axis.y=yz/axis.z;
- }
- }
- angle=PID;
- }
- }
- return angle;
- }
- Vec Matrix3 ::axisAngle(Bool normalized)C {Vec axis; Flt angle=axisAngle(axis, normalized); return axis*=angle;}
- VecD MatrixD3::axisAngle(Bool normalized)C {VecD axis; Dbl angle=axisAngle(axis, normalized); return axis*=angle;}
- /******************************************************************************/
- Vec2 Matrix3::convert(C Vec &src, Bool normalized)C
- {
- Vec2 ret(Dot(src, x),
- Dot(src, y));
- if(!normalized)
- {
- ret.x/=x.length2();
- ret.y/=y.length2();
- }
- return ret;
- }
- VecD2 MatrixD3::convert(C VecD &src, Bool normalized)C
- {
- VecD2 ret(Dot(src, x),
- Dot(src, y));
- if(!normalized)
- {
- ret.x/=x.length2();
- ret.y/=y.length2();
- }
- return ret;
- }
- Vec Matrix3 ::convert(C Vec2 &src )C {return src.x*x + src.y*y ;}
- VecD MatrixD3::convert(C VecD2 &src )C {return src.x*x + src.y*y ;}
- Vec2 Matrix ::convert(C Vec &src, Bool normalized)C {return super::convert(src -pos, normalized);}
- VecD2 MatrixD ::convert(C VecD &src, Bool normalized)C {return super::convert(src -pos, normalized);}
- Vec Matrix ::convert(C Vec2 &src )C {return super::convert(src)+pos ;}
- VecD MatrixD ::convert(C VecD2 &src )C {return super::convert(src)+pos ;}
- Edge2 Matrix3 ::convert(C Edge &src, Bool normalized)C {return Edge2 (convert(src.p[0], normalized), convert(src.p[1], normalized));}
- EdgeD2 MatrixD3::convert(C EdgeD &src, Bool normalized)C {return EdgeD2(convert(src.p[0], normalized), convert(src.p[1], normalized));}
- Edge Matrix3 ::convert(C Edge2 &src )C {return Edge (convert(src.p[0] ), convert(src.p[1] ));}
- EdgeD MatrixD3::convert(C EdgeD2 &src )C {return EdgeD (convert(src.p[0] ), convert(src.p[1] ));}
- Edge2 Matrix ::convert(C Edge &src, Bool normalized)C {return Edge2 (convert(src.p[0], normalized), convert(src.p[1], normalized));}
- EdgeD2 MatrixD ::convert(C EdgeD &src, Bool normalized)C {return EdgeD2(convert(src.p[0], normalized), convert(src.p[1], normalized));}
- Edge Matrix ::convert(C Edge2 &src )C {return Edge (convert(src.p[0] ), convert(src.p[1] ));}
- EdgeD MatrixD ::convert(C EdgeD2 &src )C {return EdgeD (convert(src.p[0] ), convert(src.p[1] ));}
- /******************************************************************************/
- Matrix& Matrix::anchor(C Vec &anchor)
- {
- Flt x=anchor.x, y=anchor.y, z=anchor.z;
- pos.set(x - x*T.x.x - y*T.y.x - z*T.z.x,
- y - x*T.x.y - y*T.y.y - z*T.z.y,
- z - x*T.x.z - y*T.y.z - z*T.z.z);
- return T;
- }
- MatrixM& MatrixM::anchor(C VecD &anchor)
- {
- Dbl x=anchor.x, y=anchor.y, z=anchor.z;
- pos.set(x - x*T.x.x - y*T.y.x - z*T.z.x,
- y - x*T.x.y - y*T.y.y - z*T.z.y,
- z - x*T.x.z - y*T.y.z - z*T.z.z);
- return T;
- }
- MatrixD& MatrixD::anchor(C VecD &anchor)
- {
- Dbl x=anchor.x, y=anchor.y, z=anchor.z;
- pos.set(x - x*T.x.x - y*T.y.x - z*T.z.x,
- y - x*T.x.y - y*T.y.y - z*T.z.y,
- z - x*T.x.z - y*T.y.z - z*T.z.z);
- return T;
- }
- Matrix& Matrix::setTransformAtPos(C Vec &pos, C Matrix3 &matrix) {orn()=matrix; anchor(pos); return T;}
- Matrix& Matrix::setTransformAtPos(C Vec &pos, C Matrix &matrix) {orn()=matrix; anchor(pos); move(matrix.pos); return T;}
- Matrix& Matrix:: transformAtPos(C Vec &pos, C Matrix3 &matrix) {return moveBack(pos).mul(matrix).move(pos);}
- Matrix& Matrix:: transformAtPos(C Vec &pos, C Matrix &matrix) {return moveBack(pos).mul(matrix).move(pos);}
- MatrixD& MatrixD::setTransformAtPos(C VecD &pos, C MatrixD3 &matrix) {orn()=matrix; anchor(pos); return T;}
- MatrixD& MatrixD::setTransformAtPos(C VecD &pos, C MatrixD &matrix) {orn()=matrix; anchor(pos); move(matrix.pos); return T;}
- MatrixD& MatrixD:: transformAtPos(C VecD &pos, C MatrixD3 &matrix) {return moveBack(pos).mul(matrix).move(pos);}
- MatrixD& MatrixD:: transformAtPos(C VecD &pos, C MatrixD &matrix) {return moveBack(pos).mul(matrix).move(pos);}
- /******************************************************************************/
- Matrix3::Matrix3(C Matrix &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- }
- Matrix3::Matrix3(C MatrixD3 &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- }
- Matrix3::Matrix3(C Matrix4 &matrix)
- {
- x=matrix.x.xyz;
- y=matrix.y.xyz;
- z=matrix.z.xyz;
- }
- Matrix3::Matrix3(C Orient &orient)
- {
- x=orient.cross();
- y=orient.perp;
- z=orient.dir;
- }
- /******************************************************************************/
- MatrixD3::MatrixD3(C Matrix3 &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- }
- MatrixD3::MatrixD3(C MatrixD &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- }
- MatrixD3::MatrixD3(C Orient &orient) : MatrixD3(OrientD(orient)) {}
- MatrixD3::MatrixD3(C OrientD &orient)
- {
- x=orient.cross();
- y=orient.perp;
- z=orient.dir;
- }
- /******************************************************************************/
- Matrix::Matrix(C Matrix3 &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- pos.zero();
- }
- Matrix::Matrix(C MatrixD3 &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- pos.zero();
- }
- Matrix::Matrix(C MatrixM &matrix)
- {
- x =matrix.x ;
- y =matrix.y ;
- z =matrix.z ;
- pos=matrix.pos;
- }
- Matrix::Matrix(C MatrixD &matrix)
- {
- x =matrix.x ;
- y =matrix.y ;
- z =matrix.z ;
- pos=matrix.pos;
- }
- Matrix::Matrix(C Matrix4 &matrix)
- {
- x =matrix.x .xyz;
- y =matrix.y .xyz;
- z =matrix.z .xyz;
- pos=matrix.pos.xyz;
- }
- Matrix::Matrix(C OrientP &orient)
- {
- x =orient.cross();
- y =orient.perp;
- z =orient.dir;
- pos=orient.pos;
- }
- MatrixM::MatrixM(C Matrix3 &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- pos.zero();
- }
- MatrixM::MatrixM(C Matrix &matrix)
- {
- x =matrix.x ;
- y =matrix.y ;
- z =matrix.z ;
- pos=matrix.pos;
- }
- MatrixM::MatrixM(C OrientM &orient)
- {
- x =orient.cross();
- y =orient.perp;
- z =orient.dir;
- pos=orient.pos;
- }
- /******************************************************************************/
- MatrixD::MatrixD(C MatrixD3 &matrix)
- {
- x=matrix.x;
- y=matrix.y;
- z=matrix.z;
- pos.zero();
- }
- MatrixD::MatrixD(C Matrix &matrix)
- {
- x =matrix.x ;
- y =matrix.y ;
- z =matrix.z ;
- pos=matrix.pos;
- }
- MatrixD::MatrixD(C OrientP &o) : MatrixD3(o)
- {
- pos=o.pos;
- }
- /******************************************************************************/
- Matrix4::Matrix4(C Matrix3 &matrix)
- {
- x .set(matrix.x, 0);
- y .set(matrix.y, 0);
- z .set(matrix.z, 0);
- pos.set(0, 0, 0, 1);
- }
- Matrix4::Matrix4(C Matrix &matrix)
- {
- x .set(matrix.x , 0);
- y .set(matrix.y , 0);
- z .set(matrix.z , 0);
- pos.set(matrix.pos, 1);
- }
- /******************************************************************************/
- Flt Matrix3::determinant()C
- {
- // return Dot(x, Cross(y, z));
- // return x.x*y.y*z.z + x.y*y.z*z.x + x.z*y.x*z.y
- // - x.x*y.z*z.y - x.y*y.x*z.z - x.z*y.y*z.x;
- return x.x*(y.y*z.z - y.z*z.y)
- + x.y*(y.z*z.x - y.x*z.z)
- + x.z*(y.x*z.y - y.y*z.x);
- }
- Dbl MatrixD3::determinant()C
- {
- // return Dot(x, Cross(y, z));
- // return x.x*y.y*z.z + x.y*y.z*z.x + x.z*y.x*z.y
- // - x.x*y.z*z.y - x.y*y.x*z.z - x.z*y.y*z.x;
- return x.x*(y.y*z.z - y.z*z.y)
- + x.y*(y.z*z.x - y.x*z.z)
- + x.z*(y.x*z.y - y.y*z.x);
- }
- Flt Matrix4::determinant()C
- {
- return
- x.w*y.z*z.y*pos.x - x.z*y.w*z.y*pos.x - x.w*y.y*z.z*pos.x + x.y*y.w*z.z*pos.x
- +x.z*y.y*z.w*pos.x - x.y*y.z*z.w*pos.x - x.w*y.z*z.x*pos.y + x.z*y.w*z.x*pos.y
- +x.w*y.x*z.z*pos.y - x.x*y.w*z.z*pos.y - x.z*y.x*z.w*pos.y + x.x*y.z*z.w*pos.y
- +x.w*y.y*z.x*pos.z - x.y*y.w*z.x*pos.z - x.w*y.x*z.y*pos.z + x.x*y.w*z.y*pos.z
- +x.y*y.x*z.w*pos.z - x.x*y.y*z.w*pos.z - x.z*y.y*z.x*pos.w + x.y*y.z*z.x*pos.w
- +x.z*y.x*z.y*pos.w - x.x*y.z*z.y*pos.w - x.y*y.x*z.z*pos.w + x.x*y.y*z.z*pos.w;
- }
- /******************************************************************************/
- void Matrix3::draw(C Vec &pos, C Color &x_color, C Color &y_color, C Color &z_color, Bool arrow)C
- {
- if(arrow)
- {
- DrawArrow(x_color, pos, pos+x);
- DrawArrow(y_color, pos, pos+y);
- DrawArrow(z_color, pos, pos+z);
- }else
- {
- VI.line(x_color, pos, pos+x);
- VI.line(y_color, pos, pos+y);
- VI.line(z_color, pos, pos+z);
- VI.end ();
- }
- }
- void MatrixD3::draw(C VecD &pos, C Color &x_color, C Color &y_color, C Color &z_color, Bool arrow)C
- {
- if(arrow)
- {
- DrawArrow(x_color, pos, pos+x);
- DrawArrow(y_color, pos, pos+y);
- DrawArrow(z_color, pos, pos+z);
- }else
- {
- VI.line(x_color, pos, pos+x);
- VI.line(y_color, pos, pos+y);
- VI.line(z_color, pos, pos+z);
- VI.end ();
- }
- }
- /******************************************************************************/
- // GPU MATRIX
- /******************************************************************************/
- GpuMatrix::GpuMatrix(C Matrix &m)
- {
- T.xx=m.x.x; T.yx=m.y.x; T.zx=m.z.x; T._x=m.pos.x;
- T.xy=m.x.y; T.yy=m.y.y; T.zy=m.z.y; T._y=m.pos.y;
- T.xz=m.x.z; T.yz=m.y.z; T.zz=m.z.z; T._z=m.pos.z;
- }
- GpuMatrix::GpuMatrix(C MatrixM &m)
- {
- T.xx=m.x.x; T.yx=m.y.x; T.zx=m.z.x; T._x=m.pos.x;
- T.xy=m.x.y; T.yy=m.y.y; T.zy=m.z.y; T._y=m.pos.y;
- T.xz=m.x.z; T.yz=m.y.z; T.zz=m.z.z; T._z=m.pos.z;
- }
- /******************************************************************************/
- GpuMatrix& GpuMatrix::fromMul(C Matrix &a, C Matrix &b)
- {
- Flt x, y, z;
- x=b.x.x; y=b.y.x; z=b.z.x;
- T.xx=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yx=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zx=x*a.z.x + y*a.z.y + z*a.z.z;
- x=b.x.y; y=b.y.y; z=b.z.y;
- T.xy=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yy=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zy=x*a.z.x + y*a.z.y + z*a.z.z;
- x=b.x.z; y=b.y.z; z=b.z.z;
- T.xz=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yz=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zz=x*a.z.x + y*a.z.y + z*a.z.z;
- x=a.pos.x; y=a.pos.y; z=a.pos.z;
- T._x=x*b.x.x + y*b.y.x + z*b.z.x + b.pos.x;
- T._y=x*b.x.y + y*b.y.y + z*b.z.y + b.pos.y;
- T._z=x*b.x.z + y*b.y.z + z*b.z.z + b.pos.z;
- return T;
- }
- GpuMatrix& GpuMatrix::fromMul(C Matrix &a, C MatrixM &b)
- {
- Flt x, y, z;
- x=b.x.x; y=b.y.x; z=b.z.x;
- T.xx=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yx=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zx=x*a.z.x + y*a.z.y + z*a.z.z;
- x=b.x.y; y=b.y.y; z=b.z.y;
- T.xy=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yy=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zy=x*a.z.x + y*a.z.y + z*a.z.z;
- x=b.x.z; y=b.y.z; z=b.z.z;
- T.xz=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yz=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zz=x*a.z.x + y*a.z.y + z*a.z.z;
- x=a.pos.x; y=a.pos.y; z=a.pos.z;
- T._x=x*b.x.x + y*b.y.x + z*b.z.x + b.pos.x;
- T._y=x*b.x.y + y*b.y.y + z*b.z.y + b.pos.y;
- T._z=x*b.x.z + y*b.y.z + z*b.z.z + b.pos.z;
- return T;
- }
- GpuMatrix& GpuMatrix::fromMul(C MatrixM &a, C MatrixM &b)
- {
- {
- Flt x, y, z;
- x=b.x.x; y=b.y.x; z=b.z.x;
- T.xx=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yx=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zx=x*a.z.x + y*a.z.y + z*a.z.z;
- x=b.x.y; y=b.y.y; z=b.z.y;
- T.xy=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yy=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zy=x*a.z.x + y*a.z.y + z*a.z.z;
- x=b.x.z; y=b.y.z; z=b.z.z;
- T.xz=x*a.x.x + y*a.x.y + z*a.x.z;
- T.yz=x*a.y.x + y*a.y.y + z*a.y.z;
- T.zz=x*a.z.x + y*a.z.y + z*a.z.z;
- }
- {
- Dbl x, y, z;
- x=a.pos.x; y=a.pos.y; z=a.pos.z;
- T._x=x*b.x.x + y*b.y.x + z*b.z.x + b.pos.x;
- T._y=x*b.x.y + y*b.y.y + z*b.z.y + b.pos.y;
- T._z=x*b.x.z + y*b.y.z + z*b.z.z + b.pos.z;
- }
- return T;
- }
- /******************************************************************************/
- // MAIN
- /******************************************************************************/
- Bool Equal(C Matrix3 &a, C Matrix3 &b, Flt eps ) {return Equal(a.x, b.x, eps) && Equal(a.y, b.y, eps) && Equal(a.z, b.z, eps);}
- Bool Equal(C MatrixD3 &a, C MatrixD3 &b, Dbl eps ) {return Equal(a.x, b.x, eps) && Equal(a.y, b.y, eps) && Equal(a.z, b.z, eps);}
- Bool Equal(C Matrix4 &a, C Matrix4 &b, Flt eps ) {return Equal(a.x, b.x, eps) && Equal(a.y, b.y, eps) && Equal(a.z, b.z, eps) && Equal(a.pos, b.pos, eps);}
- Bool Equal(C Matrix &a, C Matrix &b, Flt eps, Flt pos_eps) {return Equal(a.orn(), b.orn(), eps) && Equal(a.pos, b.pos, pos_eps);}
- Bool Equal(C MatrixM &a, C MatrixM &b, Flt eps, Dbl pos_eps) {return Equal(a.orn(), b.orn(), eps) && Equal(a.pos, b.pos, pos_eps);}
- Bool Equal(C MatrixD &a, C MatrixD &b, Dbl eps, Dbl pos_eps) {return Equal(a.orn(), b.orn(), eps) && Equal(a.pos, b.pos, pos_eps);}
- /******************************************************************************/
- void GetTransform(Matrix3 &transform, C Orient &start, C Orient &result)
- {
- #if 0 // unoptimized
- start.inverse(transform); transform.mul(Matrix3(result));
- #else // optimized
- Vec start_cross= start.cross(),
- result_cross=result.cross();
- // this inverses 'start' into T
- #define T_x_x start_cross.x
- #define T_x_y start.perp .x
- #define T_x_z start.dir .x
- #define T_y_x start_cross.y
- #define T_y_y start.perp .y
- #define T_y_z start.dir .y
- #define T_z_x start_cross.z
- #define T_z_y start.perp .z
- #define T_z_z start.dir .z
- #define m_x result_cross
- #define m_y result.perp
- #define m_z result.dir
- // following code is from 'Matrix3.mul'
- Flt x, y, z;
- x=T_y_x; y=T_y_y; z=T_y_z;
- transform.y.x=x*m_x.x + y*m_y.x + z*m_z.x;
- transform.y.y=x*m_x.y + y*m_y.y + z*m_z.y;
- transform.y.z=x*m_x.z + y*m_y.z + z*m_z.z;
- x=T_z_x; y=T_z_y; z=T_z_z;
- transform.z.x=x*m_x.x + y*m_y.x + z*m_z.x;
- transform.z.y=x*m_x.y + y*m_y.y + z*m_z.y;
- transform.z.z=x*m_x.z + y*m_y.z + z*m_z.z;
- #if 1 // this has more calculations but they can be done independently, performance in tests was the same as below, yet this has better precision because original values are used, so choose this version
- x=T_x_x; y=T_x_y; z=T_x_z;
- transform.x.x=x*m_x.x + y*m_y.x + z*m_z.x;
- transform.x.y=x*m_x.y + y*m_y.y + z*m_z.y;
- transform.x.z=x*m_x.z + y*m_y.z + z*m_z.z;
- #else
- //transform.x=Cross(transform.y, transform.z);
- transform.x.x=transform.y.y*transform.z.z - transform.y.z*transform.z.y;
- transform.x.y=transform.y.z*transform.z.x - transform.y.x*transform.z.z;
- transform.x.z=transform.y.x*transform.z.y - transform.y.y*transform.z.x;
- #endif
- #undef T_x_x
- #undef T_x_y
- #undef T_x_z
- #undef T_y_x
- #undef T_y_y
- #undef T_y_z
- #undef T_z_x
- #undef T_z_y
- #undef T_z_z
- #undef m_x
- #undef m_y
- #undef m_z
- #endif
- }
- void GetTransform(Matrix3 &transform, C Matrix3 &start, C Matrix3 &result) {Matrix3 temp ; start.inverse(temp ); temp.mul(result, transform);} // use 'temp' in case "&transform==&result"
- void GetTransform(MatrixD3 &transform, C MatrixD3 &start, C MatrixD3 &result) {MatrixD3 temp ; start.inverse(temp ); temp.mul(result, transform);} // use 'temp' in case "&transform==&result"
- void GetTransform(Matrix &transform, C Matrix &start, C Matrix &result) {Matrix temp ; start.inverse(temp ); temp.mul(result, transform);} // use 'temp' in case "&transform==&result"
- void GetTransform(MatrixD &transform, C MatrixD &start, C MatrixD &result) {MatrixD temp ; start.inverse(temp ); temp.mul(result, transform);} // use 'temp' in case "&transform==&result"
- Matrix3 GetTransform( C Matrix3 &start, C Matrix3 &result) {Matrix3 transform; start.inverse(transform); transform*=result; return transform;}
- MatrixD3 GetTransform( C MatrixD3 &start, C MatrixD3 &result) {MatrixD3 transform; start.inverse(transform); transform*=result; return transform;}
- Matrix GetTransform( C Matrix &start, C Matrix &result) {Matrix transform; start.inverse(transform); transform*=result; return transform;}
- MatrixD GetTransform( C MatrixD &start, C MatrixD &result) {MatrixD transform; start.inverse(transform); transform*=result; return transform;}
- Matrix3 GetTransform( C Orient &start, C Orient &result) {Matrix3 transform; GetTransform(transform, start, result); return transform;}
- /******************************************************************************/
- static inline Vec GetRotation(C Vec &from, C Vec &to)
- {
- Vec cross=Cross(from, to); if(Flt sin=cross.normalize())return cross*ACosSin(Dot(from, to), sin);
- return VecZero;
- }
- static inline VecD GetRotation(C VecD &from, C VecD &to)
- {
- VecD cross=Cross(from, to); if(Dbl sin=cross.normalize())return cross*ACosSin(Dot(from, to), sin);
- return VecZero;
- }
- /******************************************************************************/
- void GetDelta(Vec &pos, Vec &angle, C Matrix &from, C Matrix &to)
- {
- // pos
- pos=to.pos-from.pos;
- // angle
- Vec cross=Cross(from.z, to.z); if(Flt sin=cross.normalize())
- {
- Flt cos=Dot(from.z, to.z);
- angle=cross*ACosSin(cos, sin)
- +GetRotation(from.y*Matrix3().setRotateCosSin(cross, cos, sin), to.y);
- }else angle=GetRotation(from.y , to.y);
- }
- void GetDelta(Vec &pos, Vec &angle, C MatrixM &from, C MatrixM &to)
- {
- // pos
- pos=to.pos-from.pos;
- // angle
- Vec cross=Cross(from.z, to.z); if(Flt sin=cross.normalize())
- {
- Flt cos=Dot(from.z, to.z);
- angle=cross*ACosSin(cos, sin)
- +GetRotation(from.y*Matrix3().setRotateCosSin(cross, cos, sin), to.y);
- }else angle=GetRotation(from.y , to.y);
- }
- void GetDelta(VecD &pos, VecD &angle, C MatrixD &from, C MatrixD &to)
- {
- // pos
- pos=to.pos-from.pos;
- // angle
- VecD cross=Cross(from.z, to.z); if(Dbl sin=cross.normalize())
- {
- Dbl cos=Dot(from.z, to.z);
- angle=cross*ACosSin(cos, sin)
- +GetRotation(from.y*MatrixD3().setRotateCosSin(cross, cos, sin), to.y);
- }else angle=GetRotation(from.y , to.y);
- }
- /******************************************************************************/
- void GetVel(Vec &vel, Vec &ang_vel, C Matrix &from, C Matrix &to, Flt dt)
- {
- if(dt>EPS)
- {
- GetDelta(vel, ang_vel, from, to);
- vel/=dt;
- ang_vel/=dt;
- }else
- {
- vel.zero();
- ang_vel.zero();
- }
- }
- void GetVel(Vec &vel, Vec &ang_vel, C MatrixM &from, C MatrixM &to, Flt dt)
- {
- if(dt>EPS)
- {
- GetDelta(vel, ang_vel, from, to);
- vel/=dt;
- ang_vel/=dt;
- }else
- {
- vel.zero();
- ang_vel.zero();
- }
- }
- void GetVel(VecD &vel, VecD &ang_vel, C MatrixD &from, C MatrixD &to, Dbl dt)
- {
- if(dt>EPSD)
- {
- GetDelta(vel, ang_vel, from, to);
- vel/=dt;
- ang_vel/=dt;
- }else
- {
- vel.zero();
- ang_vel.zero();
- }
- }
- /******************************************************************************/
- // SHADER
- /******************************************************************************/
- void AnimatedSkeleton::setMatrix()C
- {
- Int matrixes=Min(bones.elms(), MAX_MATRIX_SW-VIRTUAL_ROOT_BONE); // this is the amount of matrixes for bones (without the virtual), leave room for root bone
- SetMatrixCount(VIRTUAL_ROOT_BONE+matrixes); // root + bones
- ObjMatrix=matrix(); // 'Mesh.drawBlend' makes use of the 'ObjMatrix' so it must be set
- if(Renderer._mesh_shader_vel) // we need to process velocities
- {
- Vec v; if(VIRTUAL_ROOT_BONE){v=vel()-ActiveCam.vel; v*=CamMatrixInvMotionScale;}
- if(!D.meshBoneSplit() || matrixes<=MAX_MATRIX_HWMIN-VIRTUAL_ROOT_BONE) // bone matrixes + VIRTUAL_ROOT_BONE <= MAX_MATRIX_HWMIN
- {
- if(VIRTUAL_ROOT_BONE)
- {
- Sh.h_ViewMatrix->fromMul(matrix(), CamMatrixInv);
- Sh.h_ObjVel ->set (v );
- }
- REP(matrixes)
- {
- C AnimSkelBone &bone=bones[i];
- v=bone._vel-ActiveCam.vel; v*=CamMatrixInvMotionScale;
- Sh.h_ViewMatrix->fromMul(bone._matrix, CamMatrixInv, VIRTUAL_ROOT_BONE+i);
- Sh.h_ObjVel ->set (v , VIRTUAL_ROOT_BONE+i);
- }
- }else
- {
- #if MAY_NEED_BONE_SPLITS
- if(VIRTUAL_ROOT_BONE)
- {
- Sh.h_ViewMatrix->set(GObjMatrix[0].fromMul(matrix(), CamMatrixInv));
- Sh.h_ObjVel ->set(GObjVel [0]=v );
- }
- REP(matrixes)
- {
- C AnimSkelBone &bone=bones[i];
- v=bone._vel-ActiveCam.vel; v*=CamMatrixInvMotionScale;
- Sh.h_ViewMatrix->set(GObjMatrix[VIRTUAL_ROOT_BONE+i].fromMul(bone._matrix, CamMatrixInv), VIRTUAL_ROOT_BONE+i);
- Sh.h_ObjVel ->set(GObjVel [VIRTUAL_ROOT_BONE+i]=v , VIRTUAL_ROOT_BONE+i);
- }
- #endif
- }
- SetFastAngVel(); // 'SetFastAngVel' is not fully supported, because every bone has its own linear velocities (and that would also require their own angular velocity for each bone separately which wouldn't be efficient)
- }else
- {
- if(!D.meshBoneSplit() || matrixes<=MAX_MATRIX_HWMIN-VIRTUAL_ROOT_BONE) // bone matrixes + VIRTUAL_ROOT_BONE <= MAX_MATRIX_HWMIN
- {
- if(VIRTUAL_ROOT_BONE)Sh.h_ViewMatrix->fromMul( matrix(), CamMatrixInv);
- REP(matrixes)Sh.h_ViewMatrix->fromMul(bones[i]._matrix , CamMatrixInv, VIRTUAL_ROOT_BONE+i);
- }else
- {
- #if MAY_NEED_BONE_SPLITS
- if(VIRTUAL_ROOT_BONE)Sh.h_ViewMatrix->set(GObjMatrix[0 ].fromMul( matrix(), CamMatrixInv));
- REP(matrixes)Sh.h_ViewMatrix->set(GObjMatrix[VIRTUAL_ROOT_BONE+i].fromMul(bones[i]._matrix , CamMatrixInv), VIRTUAL_ROOT_BONE+i);
- #endif
- }
- }
- }
- /******************************************************************************/
- void SetVelFur(C Matrix3 &view_matrix, C Vec &vel)
- {
- Vec v=vel*D.furStaticVelScale(); v.y+=D.furStaticGravity();
- v.clipLength(0.92f);
- #if DEBUG && 0
- Matrix3 obj_matrix=view_matrix*CamMatrix.orn(); obj_matrix.normalize(); Vec v2=v/obj_matrix; // 'v2' should be the same as 'v' below, this can be used for testing
- #endif
- v*=CamMatrixInv.orn(); // below we should divide by 'object_matrix', however we only have 'view_matrix', applying this transformation will provide the same result
- v.divNormalized(view_matrix)/=view_matrix.x.length(); // this is equal to dividing by normalized matrix, v/=matrix.normalize(), as a faster approximation because we use only 'x.length' ignoring y and z, yes in this case it should be 'length' and not 'length2'
- Sh.h_FurVel->set(v);
- }
- INLINE Vec FurVelShader(C Vec &vel, C Matrix3 &matrix) {return Vec().fromDivNormalized(vel, matrix)/=matrix.x.length();} // this is equal to dividing by normalized matrix, v/=matrix.normalize(), as a faster approximation because we use only 'x.length' ignoring y and z, yes in this case it should be 'length' and not 'length2'
- void AnimatedSkeleton::setFurVel()C
- {
- Int matrixes=Min(bones.elms(), MAX_MATRIX_SW-VIRTUAL_ROOT_BONE); // this is the amount of matrixes for bones (without the virtual), leave room for root bone
- SetFurVelCount(VIRTUAL_ROOT_BONE+matrixes); // root + bones
- if(!D.meshBoneSplit() || matrixes<=MAX_MATRIX_HWMIN-VIRTUAL_ROOT_BONE) // bone matrixes + VIRTUAL_ROOT_BONE <= MAX_MATRIX_HWMIN
- {
- if(VIRTUAL_ROOT_BONE)Sh.h_FurVel->set(FurVelShader( root._fur_vel, matrix()));
- REP(matrixes)Sh.h_FurVel->set(FurVelShader(bones[i]._fur_vel, bones[i]._matrix ), VIRTUAL_ROOT_BONE+i);
- }else
- {
- #if MAY_NEED_BONE_SPLITS
- if(VIRTUAL_ROOT_BONE)Sh.h_FurVel->set(GFurVel[0 ]=FurVelShader( root._fur_vel, matrix()));
- REP(matrixes)Sh.h_FurVel->set(GFurVel[VIRTUAL_ROOT_BONE+i]=FurVelShader(bones[i]._fur_vel, bones[i]._matrix ), VIRTUAL_ROOT_BONE+i);
- #endif
- }
- }
- /******************************************************************************/
- #if MAY_NEED_BONE_SPLITS
- void SetMatrixVelRestore() // this function restores full set of matrixes/velocities to GPU
- {
- Int m=Min(Matrixes, MAX_MATRIX_HWMIN);
- Sh.h_ViewMatrix->set(GObjMatrix, m); // matrixes
- if(Renderer._mesh_shader_vel)Sh.h_ObjVel ->set(GObjVel , m); // velocities
- }
- void SetMatrixVelSplit(Byte *matrix, Int matrixes) // this function sets matrixes/velocities to GPU for a particular BoneSplit
- {
- REP(matrixes)Sh.h_ViewMatrix->set(GObjMatrix[matrix[i]], i); // matrixes
- if(Renderer._mesh_shader_vel)REP(matrixes)Sh.h_ObjVel ->set(GObjVel [matrix[i]], i); // velocities
- }
- // FUR
- void SetMatrixFurVelRestore() // this function restores full set of matrixes/velocities to GPU
- {
- Int m=Min(Matrixes, MAX_MATRIX_HWMIN);
- Sh.h_ViewMatrix->set(GObjMatrix, m); // matrixes
- Sh.h_FurVel ->set(GFurVel , m); // velocities
- }
- void SetMatrixFurVelSplit(Byte *matrix, Int matrixes) // this function sets matrixes/velocities to GPU for a particular BoneSplit
- {
- REP(matrixes)
- {
- Byte m=matrix[i];
- Sh.h_ViewMatrix->set(GObjMatrix[m], i); // matrixes
- Sh.h_FurVel ->set(GFurVel [m], i); // velocities
- }
- }
- #endif
- /******************************************************************************/
- void SetFastViewMatrix( C Matrix &view_matrix ) {Sh.h_ViewMatrix->set ( view_matrix );}
- void SetFastMatrix ( ) {Sh.h_ViewMatrix->set ( CamMatrixInv );}
- void SetFastMatrix ( C Matrix & matrix ) {Sh.h_ViewMatrix->fromMul ( matrix, CamMatrixInv );}
- void SetFastMatrix ( C MatrixM & matrix ) {Sh.h_ViewMatrix->fromMul ( matrix, CamMatrixInv );}
- void SetFastVel ( ) {Sh.h_ObjVel ->setConditional(( -ActiveCam.vel)*=CamMatrixInvMotionScale );}
- void SetFastVel ( C Vec & vel ) {Sh.h_ObjVel ->setConditional((vel-ActiveCam.vel)*=CamMatrixInvMotionScale );}
- void SetFastVel (Byte i, C Vec & vel ) {Sh.h_ObjVel ->setConditional((vel-ActiveCam.vel)*=CamMatrixInvMotionScale, i);}
- void SetFastAngVel ( C Vec &ang_vel_shader) {Sh.h_ObjAngVel ->setConditional( ang_vel_shader );} // !! 'ang_vel_shader' must come from 'SetAngVelShader' !!
- void SetFastAngVel ( ) {Sh.h_ObjAngVel ->setConditional( VecZero);}
- /******************************************************************************/
- // To be used for drawing without any velocities
- void SetOneMatrix()
- {
- SetMatrixCount();
- SetFastMatrix ();
- }
- void SetOneMatrix(C Matrix &matrix)
- {
- SetMatrixCount();
- SetFastMatrix (matrix);
- }
- void SetOneMatrix(C MatrixM &matrix)
- {
- SetMatrixCount();
- SetFastMatrix (matrix);
- }
- /******************************************************************************/
- // To be used by the user
- void SetMatrix(C Matrix &matrix)
- {
- ObjMatrix=matrix;
- SetMatrixCount();
- SetFastMatrix (matrix);
- SetFastVel ();
- SetFastAngVel ();
- }
- void SetMatrix(C MatrixM &matrix)
- {
- ObjMatrix=matrix;
- SetMatrixCount();
- SetFastMatrix (matrix);
- SetFastVel ();
- SetFastAngVel ();
- }
- void SetMatrix(C Matrix &matrix, C Vec &vel, C Vec &ang_vel)
- {
- Vec ang_vel_shader; SetAngVelShader(ang_vel_shader, ang_vel, matrix);
- ObjMatrix=matrix;
- SetMatrixCount();
- SetFastMatrix (matrix);
- SetFastVel (vel);
- SetFastAngVel (ang_vel_shader);
- }
- void SetMatrix(C MatrixM &matrix, C Vec &vel, C Vec &ang_vel)
- {
- Vec ang_vel_shader; SetAngVelShader(ang_vel_shader, ang_vel, matrix);
- ObjMatrix=matrix;
- SetMatrixCount();
- SetFastMatrix (matrix);
- SetFastVel (vel);
- SetFastAngVel (ang_vel_shader);
- }
- /******************************************************************************/
- void SetProjMatrix() // this needs to be additionally called when changing 'PixelOffset' on DX9, or switching between '_main' and some other RT on OpenGL
- {
- if(Sh.h_ProjMatrix)
- {
- #if DX9 // in DirectX 9 adjust projection 2D offset to match DirectX 10+ and OpenGL
- Matrix4 m=ProjMatrix;
- m.x .x+=m.x .w*PixelOffset.x;
- m.y .x+=m.y .w*PixelOffset.x;
- m.z .x+=m.z .w*PixelOffset.x;
- m.pos.x+=m.pos.w*PixelOffset.x;
- m.x .y+=m.x .w*PixelOffset.y;
- m.y .y+=m.y .w*PixelOffset.y;
- m.z .y+=m.z .w*PixelOffset.y;
- m.pos.y+=m.pos.w*PixelOffset.y;
- Sh.h_ProjMatrix->set(m);
- #elif GL
- if(D.mainFBO())Sh.h_ProjMatrix->set(ProjMatrix);else
- {
- Matrix4 m=ProjMatrix; CHS(m.y.y); Sh.h_ProjMatrix->set(m); // in OpenGL when drawing to a RenderTarget the 'dest.pos.y' must be flipped
- }
- #else
- Sh.h_ProjMatrix->set(ProjMatrix);
- #endif
- }
- }
- void SetProjMatrix(Flt proj_offset)
- {
- //if(Sh.h_ProjMatrix) this is called always when display is already created
- {
- Matrix4 m=ProjMatrix;
- #if DX9 // in DirectX 9 adjust projection matrix 2D offset to match DirectX 10+ and OpenGL
- Flt o=PixelOffset.x+proj_offset;
- m.x .x+=m.x .w*o;
- m.y .x+=m.y .w*o;
- m.z .x+=m.z .w*o;
- m.pos.x+=m.pos.w*o;
- m.x .y+=m.x .w*PixelOffset.y;
- m.y .y+=m.y .w*PixelOffset.y;
- m.z .y+=m.z .w*PixelOffset.y;
- m.pos.y+=m.pos.w*PixelOffset.y;
- #else
- m.x .x+=m.x .w*proj_offset;
- m.y .x+=m.y .w*proj_offset;
- m.z .x+=m.z .w*proj_offset;
- m.pos.x+=m.pos.w*proj_offset;
- #endif
- //Flt cam_offset; m.pos.x+=m.x.x*cam_offset; // this matches "m=Matrix().setPos(cam_offset, 0, 0)*m"
- #if GL
- if(!D.mainFBO())CHS(m.y.y); // in OpenGL when drawing to a RenderTarget the 'dest.pos.y' must be flipped
- #endif
- Sh.h_ProjMatrix->set(m);
- }
- }
- /******************************************************************************/
- // LOD
- /******************************************************************************/
- Flt GetLodDist2(C Vec &lod_center, C Matrix &matrix)
- {
- Flt dist2=D._lod_current_factor/matrix.x.length2();
- if(FovPerspective(D.viewFovMode()))dist2*=Dist2(lod_center*matrix, ActiveCam.matrix.pos); // has to be 'ActiveCam.matrix' and not 'CamMatrix' because we need the same LOD for shadows as for normal
- return dist2;
- }
- Flt GetLodDist2(C Vec &lod_center, C MatrixM &matrix)
- {
- Flt dist2=D._lod_current_factor/matrix.x.length2();
- if(FovPerspective(D.viewFovMode()))dist2*=Dist2(lod_center*matrix, ActiveCam.matrix.pos); // has to be 'ActiveCam.matrix' and not 'CamMatrix' because we need the same LOD for shadows as for normal
- return dist2;
- }
- /******************************************************************************/
- }
- /******************************************************************************/
|