Matrix.cpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************
  5. ObjMatrix is combined with CamMatrix to:
  6. -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)
  7. -reduce number of matrix operations in shaders
  8. /******************************************************************************/
  9. Matrix3 CamMatrixInvMotionScale;
  10. MatrixM ObjMatrix(1),
  11. CamMatrix,
  12. CamMatrixInv,
  13. EyeMatrix[2];
  14. Matrix4 ProjMatrix;
  15. Flt ProjMatrixEyeOffset[2];
  16. const Matrix MatrixIdentity (1);
  17. const MatrixM MatrixMIdentity(1);
  18. GpuMatrix *ViewMatrix;
  19. #if MAY_NEED_BONE_SPLITS
  20. static Vec GObjVel [MAX_MATRIX_SW], GFurVel[MAX_MATRIX_SW];
  21. static GpuMatrix GObjMatrix[MAX_MATRIX_SW];
  22. #endif
  23. #if DX9
  24. Vec2 PixelOffset;
  25. #endif
  26. /******************************************************************************/
  27. Matrix3& Matrix3::operator*=(Flt f)
  28. {
  29. x*=f;
  30. y*=f;
  31. z*=f;
  32. return T;
  33. }
  34. MatrixD3& MatrixD3::operator*=(Dbl f)
  35. {
  36. x*=f;
  37. y*=f;
  38. z*=f;
  39. return T;
  40. }
  41. Matrix3& Matrix3::operator/=(Flt f)
  42. {
  43. x/=f;
  44. y/=f;
  45. z/=f;
  46. return T;
  47. }
  48. MatrixD3& MatrixD3::operator/=(Dbl f)
  49. {
  50. x/=f;
  51. y/=f;
  52. z/=f;
  53. return T;
  54. }
  55. Matrix& Matrix::operator*=(Flt f)
  56. {
  57. x *=f;
  58. y *=f;
  59. z *=f;
  60. pos*=f;
  61. return T;
  62. }
  63. MatrixM& MatrixM::operator*=(Flt f)
  64. {
  65. x *=f;
  66. y *=f;
  67. z *=f;
  68. pos*=f;
  69. return T;
  70. }
  71. MatrixD& MatrixD::operator*=(Dbl f)
  72. {
  73. x *=f;
  74. y *=f;
  75. z *=f;
  76. pos*=f;
  77. return T;
  78. }
  79. Matrix& Matrix::operator/=(Flt f)
  80. {
  81. x /=f;
  82. y /=f;
  83. z /=f;
  84. pos/=f;
  85. return T;
  86. }
  87. MatrixM& MatrixM::operator/=(Flt f)
  88. {
  89. x /=f;
  90. y /=f;
  91. z /=f;
  92. pos/=f;
  93. return T;
  94. }
  95. MatrixD& MatrixD::operator/=(Dbl f)
  96. {
  97. x /=f;
  98. y /=f;
  99. z /=f;
  100. pos/=f;
  101. return T;
  102. }
  103. Matrix3& Matrix3::operator*=(C Vec &v)
  104. {
  105. x*=v;
  106. y*=v;
  107. z*=v;
  108. return T;
  109. }
  110. MatrixD3& MatrixD3::operator*=(C VecD &v)
  111. {
  112. x*=v;
  113. y*=v;
  114. z*=v;
  115. return T;
  116. }
  117. Matrix3& Matrix3::operator/=(C Vec &v)
  118. {
  119. x/=v;
  120. y/=v;
  121. z/=v;
  122. return T;
  123. }
  124. MatrixD3& MatrixD3::operator/=(C VecD &v)
  125. {
  126. x/=v;
  127. y/=v;
  128. z/=v;
  129. return T;
  130. }
  131. Matrix& Matrix::operator*=(C Vec &v)
  132. {
  133. x *=v;
  134. y *=v;
  135. z *=v;
  136. pos*=v;
  137. return T;
  138. }
  139. MatrixM& MatrixM::operator*=(C Vec &v)
  140. {
  141. x *=v;
  142. y *=v;
  143. z *=v;
  144. pos*=v;
  145. return T;
  146. }
  147. MatrixD& MatrixD::operator*=(C VecD &v)
  148. {
  149. x *=v;
  150. y *=v;
  151. z *=v;
  152. pos*=v;
  153. return T;
  154. }
  155. Matrix& Matrix::operator/=(C Vec &v)
  156. {
  157. x /=v;
  158. y /=v;
  159. z /=v;
  160. pos/=v;
  161. return T;
  162. }
  163. MatrixM& MatrixM::operator/=(C Vec &v)
  164. {
  165. x /=v;
  166. y /=v;
  167. z /=v;
  168. pos/=v;
  169. return T;
  170. }
  171. MatrixD& MatrixD::operator/=(C VecD &v)
  172. {
  173. x /=v;
  174. y /=v;
  175. z /=v;
  176. pos/=v;
  177. return T;
  178. }
  179. Matrix3& Matrix3::operator+=(C Matrix3 &m)
  180. {
  181. x+=m.x;
  182. y+=m.y;
  183. z+=m.z;
  184. return T;
  185. }
  186. MatrixD3& MatrixD3::operator+=(C MatrixD3 &m)
  187. {
  188. x+=m.x;
  189. y+=m.y;
  190. z+=m.z;
  191. return T;
  192. }
  193. Matrix3& Matrix3::operator-=(C Matrix3 &m)
  194. {
  195. x-=m.x;
  196. y-=m.y;
  197. z-=m.z;
  198. return T;
  199. }
  200. MatrixD3& MatrixD3::operator-=(C MatrixD3 &m)
  201. {
  202. x-=m.x;
  203. y-=m.y;
  204. z-=m.z;
  205. return T;
  206. }
  207. Matrix& Matrix::operator+=(C Matrix &m)
  208. {
  209. x +=m.x ;
  210. y +=m.y ;
  211. z +=m.z ;
  212. pos+=m.pos;
  213. return T;
  214. }
  215. MatrixM& MatrixM::operator+=(C MatrixM &m)
  216. {
  217. x +=m.x ;
  218. y +=m.y ;
  219. z +=m.z ;
  220. pos+=m.pos;
  221. return T;
  222. }
  223. MatrixD& MatrixD::operator+=(C MatrixD &m)
  224. {
  225. x +=m.x ;
  226. y +=m.y ;
  227. z +=m.z ;
  228. pos+=m.pos;
  229. return T;
  230. }
  231. Matrix& Matrix::operator-=(C Matrix &m)
  232. {
  233. x -=m.x ;
  234. y -=m.y ;
  235. z -=m.z ;
  236. pos-=m.pos;
  237. return T;
  238. }
  239. MatrixM& MatrixM::operator-=(C MatrixM &m)
  240. {
  241. x -=m.x ;
  242. y -=m.y ;
  243. z -=m.z ;
  244. pos-=m.pos;
  245. return T;
  246. }
  247. MatrixD& MatrixD::operator-=(C MatrixD &m)
  248. {
  249. x -=m.x ;
  250. y -=m.y ;
  251. z -=m.z ;
  252. pos-=m.pos;
  253. return T;
  254. }
  255. /******************************************************************************/
  256. Bool Matrix3 ::operator==(C Matrix3 &m)C {return x==m.x && y==m.y && z==m.z;}
  257. Bool Matrix3 ::operator!=(C Matrix3 &m)C {return x!=m.x || y!=m.y || z!=m.z;}
  258. Bool MatrixD3::operator==(C MatrixD3 &m)C {return x==m.x && y==m.y && z==m.z;}
  259. Bool MatrixD3::operator!=(C MatrixD3 &m)C {return x!=m.x || y!=m.y || z!=m.z;}
  260. Bool Matrix ::operator==(C Matrix &m)C {return x==m.x && y==m.y && z==m.z && pos==m.pos;}
  261. Bool Matrix ::operator!=(C Matrix &m)C {return x!=m.x || y!=m.y || z!=m.z || pos!=m.pos;}
  262. Bool MatrixM ::operator==(C MatrixM &m)C {return x==m.x && y==m.y && z==m.z && pos==m.pos;}
  263. Bool MatrixM ::operator!=(C MatrixM &m)C {return x!=m.x || y!=m.y || z!=m.z || pos!=m.pos;}
  264. Bool MatrixD ::operator==(C MatrixD &m)C {return x==m.x && y==m.y && z==m.z && pos==m.pos;}
  265. Bool MatrixD ::operator!=(C MatrixD &m)C {return x!=m.x || y!=m.y || z!=m.z || pos!=m.pos;}
  266. /******************************************************************************/
  267. void Matrix3::mul(C Matrix3 &m, Matrix3 &dest)C
  268. {
  269. Flt x, y, z;
  270. if(&dest==&m)
  271. {
  272. x=m.x.x; y=m.y.x; z=m.z.x;
  273. dest.x.x=x*T.x.x + y*T.x.y + z*T.x.z;
  274. dest.y.x=x*T.y.x + y*T.y.y + z*T.y.z;
  275. dest.z.x=x*T.z.x + y*T.z.y + z*T.z.z;
  276. x=m.x.y; y=m.y.y; z=m.z.y;
  277. dest.x.y=x*T.x.x + y*T.x.y + z*T.x.z;
  278. dest.y.y=x*T.y.x + y*T.y.y + z*T.y.z;
  279. dest.z.y=x*T.z.x + y*T.z.y + z*T.z.z;
  280. x=m.x.z; y=m.y.z; z=m.z.z;
  281. dest.x.z=x*T.x.x + y*T.x.y + z*T.x.z;
  282. dest.y.z=x*T.y.x + y*T.y.y + z*T.y.z;
  283. dest.z.z=x*T.z.x + y*T.z.y + z*T.z.z;
  284. }else
  285. {
  286. x=T.x.x; y=T.x.y; z=T.x.z;
  287. dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
  288. dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
  289. dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
  290. x=T.y.x; y=T.y.y; z=T.y.z;
  291. dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
  292. dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
  293. dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
  294. x=T.z.x; y=T.z.y; z=T.z.z;
  295. dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
  296. dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
  297. dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
  298. }
  299. }
  300. void MatrixD3::mul(C Matrix3 &m, MatrixD3 &dest)C
  301. {
  302. Dbl x, y, z;
  303. x=T.x.x; y=T.x.y; z=T.x.z;
  304. dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
  305. dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
  306. dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
  307. x=T.y.x; y=T.y.y; z=T.y.z;
  308. dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
  309. dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
  310. dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
  311. x=T.z.x; y=T.z.y; z=T.z.z;
  312. dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
  313. dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
  314. dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
  315. }
  316. void MatrixD3::mul(C MatrixD3 &m, MatrixD3 &dest)C
  317. {
  318. Dbl x, y, z;
  319. if(&dest==&m)
  320. {
  321. x=m.x.x; y=m.y.x; z=m.z.x;
  322. dest.x.x=x*T.x.x + y*T.x.y + z*T.x.z;
  323. dest.y.x=x*T.y.x + y*T.y.y + z*T.y.z;
  324. dest.z.x=x*T.z.x + y*T.z.y + z*T.z.z;
  325. x=m.x.y; y=m.y.y; z=m.z.y;
  326. dest.x.y=x*T.x.x + y*T.x.y + z*T.x.z;
  327. dest.y.y=x*T.y.x + y*T.y.y + z*T.y.z;
  328. dest.z.y=x*T.z.x + y*T.z.y + z*T.z.z;
  329. x=m.x.z; y=m.y.z; z=m.z.z;
  330. dest.x.z=x*T.x.x + y*T.x.y + z*T.x.z;
  331. dest.y.z=x*T.y.x + y*T.y.y + z*T.y.z;
  332. dest.z.z=x*T.z.x + y*T.z.y + z*T.z.z;
  333. }else
  334. {
  335. x=T.x.x; y=T.x.y; z=T.x.z;
  336. dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
  337. dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
  338. dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
  339. x=T.y.x; y=T.y.y; z=T.y.z;
  340. dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
  341. dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
  342. dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
  343. x=T.z.x; y=T.z.y; z=T.z.z;
  344. dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
  345. dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
  346. dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
  347. }
  348. }
  349. /******************************************************************************/
  350. #if 0 // performance is nearly the same so don't use
  351. 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'
  352. void Matrix3::mulNormalized(C Matrix3 &m, Matrix3 &dest)C
  353. {
  354. Flt x, y, z;
  355. if(&dest==&m)
  356. {
  357. x=m.x.x; y=m.y.x; z=m.z.x;
  358. //dest.x.x=x*T.x.x + y*T.x.y + z*T.x.z;
  359. dest.y.x=x*T.y.x + y*T.y.y + z*T.y.z;
  360. dest.z.x=x*T.z.x + y*T.z.y + z*T.z.z;
  361. x=m.x.y; y=m.y.y; z=m.z.y;
  362. //dest.x.y=x*T.x.x + y*T.x.y + z*T.x.z;
  363. dest.y.y=x*T.y.x + y*T.y.y + z*T.y.z;
  364. dest.z.y=x*T.z.x + y*T.z.y + z*T.z.z;
  365. x=m.x.z; y=m.y.z; z=m.z.z;
  366. //dest.x.z=x*T.x.x + y*T.x.y + z*T.x.z;
  367. dest.y.z=x*T.y.x + y*T.y.y + z*T.y.z;
  368. dest.z.z=x*T.z.x + y*T.z.y + z*T.z.z;
  369. }else
  370. {
  371. /*x=T.x.x; y=T.x.y; z=T.x.z;
  372. dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
  373. dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
  374. dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;*/
  375. x=T.y.x; y=T.y.y; z=T.y.z;
  376. dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
  377. dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
  378. dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
  379. x=T.z.x; y=T.z.y; z=T.z.z;
  380. dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
  381. dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
  382. dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
  383. }
  384. //dest.x=Cross(dest.y, dest.z);
  385. dest.x.x=dest.y.y*dest.z.z - dest.y.z*dest.z.y;
  386. dest.x.y=dest.y.z*dest.z.x - dest.y.x*dest.z.z;
  387. dest.x.z=dest.y.x*dest.z.y - dest.y.y*dest.z.x;
  388. }
  389. #endif
  390. /******************************************************************************/
  391. void Matrix::mul(C Matrix3 &m, Matrix &dest)C
  392. {
  393. Flt x=pos.x , y=pos.y , z=pos.z;
  394. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
  395. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
  396. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
  397. super::mul(m, dest.orn());
  398. }
  399. void MatrixM::mul(C Matrix3 &m, MatrixM &dest)C
  400. {
  401. Dbl x=pos.x , y=pos.y , z=pos.z;
  402. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
  403. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
  404. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
  405. super::mul(m, dest.orn());
  406. }
  407. void MatrixD::mul(C MatrixD3 &m, MatrixD &dest)C
  408. {
  409. Dbl x=pos.x , y=pos.y , z=pos.z;
  410. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
  411. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
  412. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
  413. super::mul(m, dest.orn());
  414. }
  415. /******************************************************************************
  416. 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'
  417. void Matrix::mulNormalized(C Matrix3 &m, Matrix &dest)C
  418. {
  419. Flt x=pos.x , y=pos.y , z=pos.z;
  420. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x;
  421. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y;
  422. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z;
  423. super::mulNormalized(m, dest.orn());
  424. }
  425. /******************************************************************************/
  426. void Matrix::mul(C Matrix &m, Matrix &dest)C
  427. {
  428. Flt x=pos.x , y=pos.y , z=pos.z;
  429. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  430. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  431. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  432. super::mul(m.orn(), dest.orn());
  433. }
  434. void Matrix::mul(C MatrixM &m, Matrix &dest)C
  435. {
  436. Flt x=pos.x , y=pos.y , z=pos.z;
  437. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  438. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  439. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  440. super::mul(m.orn(), dest.orn());
  441. }
  442. void MatrixM::mul(C MatrixM &m, Matrix &dest)C
  443. {
  444. Dbl x=pos.x , y=pos.y , z=pos.z;
  445. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  446. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  447. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  448. super::mul(m.orn(), dest.orn());
  449. }
  450. void MatrixM::mul(C Matrix &m, MatrixM &dest)C
  451. {
  452. Dbl x=pos.x , y=pos.y , z=pos.z;
  453. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  454. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  455. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  456. super::mul(m.orn(), dest.orn());
  457. }
  458. void MatrixM::mul(C MatrixM &m, MatrixM &dest)C
  459. {
  460. Dbl x=pos.x , y=pos.y , z=pos.z;
  461. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  462. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  463. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  464. super::mul(m.orn(), dest.orn());
  465. }
  466. void MatrixD::mul(C MatrixD &m, MatrixD &dest)C
  467. {
  468. Dbl x=pos.x , y=pos.y , z=pos.z;
  469. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  470. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  471. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  472. super::mul(m.orn(), dest.orn());
  473. }
  474. /******************************************************************************
  475. void Matrix::mulNormalized(C Matrix &m, Matrix &dest)C
  476. {
  477. Flt x=pos.x , y=pos.y , z=pos.z;
  478. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  479. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  480. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  481. super::mulNormalized(m.orn(), dest.orn());
  482. }
  483. /******************************************************************************/
  484. void Matrix::mul(C Matrix &m, Matrix4 &dest)C
  485. {
  486. Flt x, y, z;
  487. x=T.x.x; y=T.x.y; z=T.x.z;
  488. dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x;
  489. dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y;
  490. dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z;
  491. x=T.y.x; y=T.y.y; z=T.y.z;
  492. dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x;
  493. dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y;
  494. dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z;
  495. x=T.z.x; y=T.z.y; z=T.z.z;
  496. dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x;
  497. dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y;
  498. dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z;
  499. x=pos.x; y=pos.y; z=pos.z;
  500. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + m.pos.x;
  501. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + m.pos.y;
  502. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + m.pos.z;
  503. dest.x.w=dest.y.w=dest.z.w=0;
  504. dest.pos.w=1;
  505. }
  506. void Matrix::mul(C Matrix4 &m, Matrix4 &dest)C
  507. {
  508. Flt x, y, z;
  509. x=m.x.x; y=m.y.x; z=m.z.x;
  510. dest.x .x=T.x.x*x + T.x.y*y + T.x.z*z;
  511. dest.y .x=T.y.x*x + T.y.y*y + T.y.z*z;
  512. dest.z .x=T.z.x*x + T.z.y*y + T.z.z*z;
  513. dest.pos.x=pos.x*x + pos.y*y + pos.z*z + m.pos.x;
  514. x=m.x.y; y=m.y.y; z=m.z.y;
  515. dest.x .y=T.x.x*x + T.x.y*y + T.x.z*z;
  516. dest.y .y=T.y.x*x + T.y.y*y + T.y.z*z;
  517. dest.z .y=T.z.x*x + T.z.y*y + T.z.z*z;
  518. dest.pos.y=pos.x*x + pos.y*y + pos.z*z + m.pos.y;
  519. x=m.x.z; y=m.y.z; z=m.z.z;
  520. dest.x .z=T.x.x*x + T.x.y*y + T.x.z*z;
  521. dest.y .z=T.y.x*x + T.y.y*y + T.y.z*z;
  522. dest.z .z=T.z.x*x + T.z.y*y + T.z.z*z;
  523. dest.pos.z=pos.x*x + pos.y*y + pos.z*z + m.pos.z;
  524. x=m.x.w; y=m.y.w; z=m.z.w;
  525. dest.x .w=T.x.x*x + T.x.y*y + T.x.z*z;
  526. dest.y .w=T.y.x*x + T.y.y*y + T.y.z*z;
  527. dest.z .w=T.z.x*x + T.z.y*y + T.z.z*z;
  528. dest.pos.w=pos.x*x + pos.y*y + pos.z*z + m.pos.w;
  529. }
  530. void Matrix4::mul(C Matrix4 &m, Matrix4 &dest)C
  531. {
  532. Flt x, y, z, w;
  533. if(&dest==&m)
  534. {
  535. x=m.x.x; y=m.y.x; z=m.z.x; w=m.pos.x;
  536. dest.x .x=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
  537. dest.y .x=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
  538. dest.z .x=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
  539. dest.pos.x=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
  540. x=m.x.y; y=m.y.y; z=m.z.y; w=m.pos.y;
  541. dest.x .y=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
  542. dest.y .y=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
  543. dest.z .y=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
  544. dest.pos.y=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
  545. x=m.x.z; y=m.y.z; z=m.z.z; w=m.pos.z;
  546. dest.x .z=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
  547. dest.y .z=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
  548. dest.z .z=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
  549. dest.pos.z=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
  550. x=m.x.w; y=m.y.w; z=m.z.w; w=m.pos.w;
  551. dest.x .w=T.x.x*x + T.x.y*y + T.x.z*z + T.x.w*w;
  552. dest.y .w=T.y.x*x + T.y.y*y + T.y.z*z + T.y.w*w;
  553. dest.z .w=T.z.x*x + T.z.y*y + T.z.z*z + T.z.w*w;
  554. dest.pos.w=pos.x*x + pos.y*y + pos.z*z + pos.w*w;
  555. }else
  556. {
  557. x=T.x.x; y=T.x.y; z=T.x.z; w=T. x.w;
  558. dest.x.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
  559. dest.x.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
  560. dest.x.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
  561. dest.x.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
  562. x=T.y.x; y=T.y.y; z=T.y.z; w=T. y.w;
  563. dest.y.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
  564. dest.y.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
  565. dest.y.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
  566. dest.y.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
  567. x=T.z.x; y=T.z.y; z=T.z.z; w=T. z.w;
  568. dest.z.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
  569. dest.z.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
  570. dest.z.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
  571. dest.z.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
  572. x= pos.x; y=pos.y; z=pos.z; w=pos.w;
  573. dest.pos.x=x*m.x.x + y*m.y.x + z*m.z.x + w*m.pos.x;
  574. dest.pos.y=x*m.x.y + y*m.y.y + z*m.z.y + w*m.pos.y;
  575. dest.pos.z=x*m.x.z + y*m.y.z + z*m.z.z + w*m.pos.z;
  576. dest.pos.w=x*m.x.w + y*m.y.w + z*m.z.w + w*m.pos.w;
  577. }
  578. }
  579. /******************************************************************************/
  580. void Matrix::mulTimes(Int n, C Matrix &matrix, Matrix &dest)C
  581. {
  582. switch(n)
  583. {
  584. case 0: dest=T; break;
  585. case 1: mul(matrix, dest ); break;
  586. default:
  587. {
  588. if(Equal(matrix.orn(), MatrixIdentity.orn())) // no rotation and no scale - we can just move by "n*pos"
  589. {
  590. dest.orn()=T.orn();
  591. dest.pos =T.pos+matrix.pos*n;
  592. }else
  593. if(!matrix.pos.any() && Equal(matrix.scale2(), VecOne)) // no position and no scale - we can just rotate by "n*angle"
  594. {
  595. Vec axis; Flt angle=matrix.axisAngle(axis);
  596. Matrix matrix_n; matrix_n.setRotate(axis, angle*n);
  597. mul(matrix_n, dest);
  598. }else
  599. {
  600. Matrix temp=matrix; if(n<0){CHS(n); temp.inverse();}
  601. FREPA(PrimeNumbers) // optimize using prime numbers
  602. {
  603. Int pm=PrimeNumbers[i]; if(n<pm)break;
  604. for(; n%pm==0; n/=pm){Matrix single=temp; REP(pm-1)temp*=single;}
  605. }
  606. mul(temp, dest); REP(n-1)dest*=temp; // make the first mul from this->dest, and remaining mul's from dest->dest
  607. }
  608. }break;
  609. }
  610. }
  611. void Matrix::mulTimes(Int n, C RevMatrix &matrix, Matrix &dest)C
  612. {
  613. switch(n)
  614. {
  615. case 0: dest=T; break;
  616. case 1: mul(matrix, dest ); break;
  617. default:
  618. {
  619. if(Equal(matrix.orn(), MatrixIdentity.orn())) // no rotation and no scale - we can just move by "n*pos"
  620. {
  621. dest.orn()=T.orn();
  622. dest.pos =T.pos+(matrix.pos*n)*T.orn();
  623. }else
  624. if(!matrix.pos.any() && Equal(matrix.scale2(), VecOne)) // no position and no scale - we can just rotate by "n*angle"
  625. {
  626. Vec axis; Flt angle=matrix.axisAngle(axis);
  627. RevMatrix matrix_n; matrix_n.setRotate(axis, angle*n);
  628. mul(matrix_n, dest);
  629. }else
  630. {
  631. RevMatrix temp=matrix; if(n<0){CHS(n); temp.inverse();}
  632. FREPA(PrimeNumbers) // optimize using prime numbers
  633. {
  634. Int pm=PrimeNumbers[i]; if(n<pm)break;
  635. 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'
  636. }
  637. mul(temp, dest); REP(n-1)dest*=temp; // make the first mul from this->dest, and remaining mul's from dest->dest
  638. }
  639. }break;
  640. }
  641. }
  642. /******************************************************************************/
  643. void Matrix3::divNormalized(C Matrix3 &m, Matrix3 &dest)C
  644. {
  645. if(&dest==&m)
  646. {
  647. Flt x_x=m.x.x, x_y=m.x.y, x_z=m.x.z;
  648. dest.x.x=x_x*T.x.x + x_y*T.x.y + x_z*T.x.z;
  649. Flt y_x=m.y.x; dest.y.x=x_x*T.y.x + x_y*T.y.y + x_z*T.y.z;
  650. Flt z_x=m.z.x; dest.z.x=x_x*T.z.x + x_y*T.z.y + x_z*T.z.z;
  651. Flt y_y=m.y.y, y_z=m.y.z;
  652. dest.x.y=y_x*T.x.x + y_y*T.x.y + y_z*T.x.z;
  653. dest.y.y=y_x*T.y.x + y_y*T.y.y + y_z*T.y.z;
  654. Flt z_y=m.z.y; dest.z.y=y_x*T.z.x + y_y*T.z.y + y_z*T.z.z;
  655. Flt z_z=m.z.z;
  656. dest.x.z=z_x*T.x.x + z_y*T.x.y + z_z*T.x.z;
  657. dest.y.z=z_x*T.y.x + z_y*T.y.y + z_z*T.y.z;
  658. dest.z.z=z_x*T.z.x + z_y*T.z.y + z_z*T.z.z;
  659. }else
  660. {
  661. Flt x, y, z;
  662. x=T.x.x; y=T.x.y; z=T.x.z;
  663. dest.x.x=x*m.x.x + y*m.x.y + z*m.x.z;
  664. dest.x.y=x*m.y.x + y*m.y.y + z*m.y.z;
  665. dest.x.z=x*m.z.x + y*m.z.y + z*m.z.z;
  666. x=T.y.x; y=T.y.y; z=T.y.z;
  667. dest.y.x=x*m.x.x + y*m.x.y + z*m.x.z;
  668. dest.y.y=x*m.y.x + y*m.y.y + z*m.y.z;
  669. dest.y.z=x*m.z.x + y*m.z.y + z*m.z.z;
  670. x=T.z.x; y=T.z.y; z=T.z.z;
  671. dest.z.x=x*m.x.x + y*m.x.y + z*m.x.z;
  672. dest.z.y=x*m.y.x + y*m.y.y + z*m.y.z;
  673. dest.z.z=x*m.z.x + y*m.z.y + z*m.z.z;
  674. }
  675. }
  676. void MatrixD3::divNormalized(C Matrix3 &m, MatrixD3 &dest)C
  677. {
  678. Dbl x, y, z;
  679. x=T.x.x; y=T.x.y; z=T.x.z;
  680. dest.x.x=x*m.x.x + y*m.x.y + z*m.x.z;
  681. dest.x.y=x*m.y.x + y*m.y.y + z*m.y.z;
  682. dest.x.z=x*m.z.x + y*m.z.y + z*m.z.z;
  683. x=T.y.x; y=T.y.y; z=T.y.z;
  684. dest.y.x=x*m.x.x + y*m.x.y + z*m.x.z;
  685. dest.y.y=x*m.y.x + y*m.y.y + z*m.y.z;
  686. dest.y.z=x*m.z.x + y*m.z.y + z*m.z.z;
  687. x=T.z.x; y=T.z.y; z=T.z.z;
  688. dest.z.x=x*m.x.x + y*m.x.y + z*m.x.z;
  689. dest.z.y=x*m.y.x + y*m.y.y + z*m.y.z;
  690. dest.z.z=x*m.z.x + y*m.z.y + z*m.z.z;
  691. }
  692. void MatrixD3::divNormalized(C MatrixD3 &m, MatrixD3 &dest)C
  693. {
  694. if(&dest==&m)
  695. {
  696. Dbl x_x=m.x.x, x_y=m.x.y, x_z=m.x.z;
  697. dest.x.x=x_x*T.x.x + x_y*T.x.y + x_z*T.x.z;
  698. Dbl y_x=m.y.x; dest.y.x=x_x*T.y.x + x_y*T.y.y + x_z*T.y.z;
  699. Dbl z_x=m.z.x; dest.z.x=x_x*T.z.x + x_y*T.z.y + x_z*T.z.z;
  700. Dbl y_y=m.y.y, y_z=m.y.z;
  701. dest.x.y=y_x*T.x.x + y_y*T.x.y + y_z*T.x.z;
  702. dest.y.y=y_x*T.y.x + y_y*T.y.y + y_z*T.y.z;
  703. Dbl z_y=m.z.y; dest.z.y=y_x*T.z.x + y_y*T.z.y + y_z*T.z.z;
  704. Dbl z_z=m.z.z;
  705. dest.x.z=z_x*T.x.x + z_y*T.x.y + z_z*T.x.z;
  706. dest.y.z=z_x*T.y.x + z_y*T.y.y + z_z*T.y.z;
  707. dest.z.z=z_x*T.z.x + z_y*T.z.y + z_z*T.z.z;
  708. }else
  709. {
  710. Dbl x, y, z;
  711. x=T.x.x; y=T.x.y; z=T.x.z;
  712. dest.x.x=x*m.x.x + y*m.x.y + z*m.x.z;
  713. dest.x.y=x*m.y.x + y*m.y.y + z*m.y.z;
  714. dest.x.z=x*m.z.x + y*m.z.y + z*m.z.z;
  715. x=T.y.x; y=T.y.y; z=T.y.z;
  716. dest.y.x=x*m.x.x + y*m.x.y + z*m.x.z;
  717. dest.y.y=x*m.y.x + y*m.y.y + z*m.y.z;
  718. dest.y.z=x*m.z.x + y*m.z.y + z*m.z.z;
  719. x=T.z.x; y=T.z.y; z=T.z.z;
  720. dest.z.x=x*m.x.x + y*m.x.y + z*m.x.z;
  721. dest.z.y=x*m.y.x + y*m.y.y + z*m.y.z;
  722. dest.z.z=x*m.z.x + y*m.z.y + z*m.z.z;
  723. }
  724. }
  725. /******************************************************************************/
  726. void Matrix::divNormalized(C Matrix3 &m, Matrix &dest)C
  727. {
  728. Flt x=pos.x, y=pos.y, z=pos.z;
  729. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  730. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  731. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  732. super::divNormalized(m, dest);
  733. }
  734. void MatrixM::divNormalized(C Matrix3 &m, MatrixM &dest)C
  735. {
  736. Dbl x=pos.x, y=pos.y, z=pos.z;
  737. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  738. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  739. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  740. super::divNormalized(m, dest);
  741. }
  742. void MatrixD::divNormalized(C MatrixD3 &m, MatrixD &dest)C
  743. {
  744. Dbl x=pos.x, y=pos.y, z=pos.z;
  745. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  746. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  747. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  748. super::divNormalized(m, dest);
  749. }
  750. void Matrix::divNormalized(C Matrix &m, Matrix &dest)C
  751. {
  752. Flt x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
  753. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  754. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  755. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  756. super::divNormalized(m, dest);
  757. }
  758. void Matrix::divNormalized(C MatrixM &m, Matrix &dest)C
  759. {
  760. Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
  761. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  762. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  763. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  764. super::divNormalized(m, dest);
  765. }
  766. void MatrixM::divNormalized(C MatrixM &m, Matrix &dest)C
  767. {
  768. Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
  769. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  770. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  771. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  772. super::divNormalized(m, dest);
  773. }
  774. void MatrixM::divNormalized(C Matrix &m, MatrixM &dest)C
  775. {
  776. Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
  777. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  778. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  779. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  780. super::divNormalized(m, dest);
  781. }
  782. void MatrixM::divNormalized(C MatrixM &m, MatrixM &dest)C
  783. {
  784. Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
  785. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  786. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  787. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  788. super::divNormalized(m, dest);
  789. }
  790. void MatrixD::divNormalized(C MatrixD &m, MatrixD &dest)C
  791. {
  792. Dbl x=pos.x-m.pos.x, y=pos.y-m.pos.y, z=pos.z-m.pos.z;
  793. dest.pos.x=x*m.x.x + y*m.x.y + z*m.x.z;
  794. dest.pos.y=x*m.y.x + y*m.y.y + z*m.y.z;
  795. dest.pos.z=x*m.z.x + y*m.z.y + z*m.z.z;
  796. super::divNormalized(m, dest);
  797. }
  798. /******************************************************************************/
  799. void Matrix3 ::div(C Matrix3 &m, Matrix3 &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
  800. void MatrixD3::div(C Matrix3 &m, MatrixD3 &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
  801. void MatrixD3::div(C MatrixD3 &m, MatrixD3 &dest)C {MatrixD3 temp; m.inverse(temp); mul(temp, dest);}
  802. void Matrix ::div(C Matrix3 &m, Matrix &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
  803. void MatrixM ::div(C Matrix3 &m, MatrixM &dest)C {Matrix3 temp; m.inverse(temp); mul(temp, dest);}
  804. void MatrixD ::div(C MatrixD3 &m, MatrixD &dest)C {MatrixD3 temp; m.inverse(temp); mul(temp, dest);}
  805. void Matrix ::div(C Matrix &m, Matrix &dest)C {Matrix temp; m.inverse(temp); mul(temp, dest);}
  806. void Matrix ::div(C MatrixM &m, Matrix &dest)C {MatrixM temp; m.inverse(temp); mul(temp, dest);}
  807. void MatrixM ::div(C MatrixM &m, Matrix &dest)C {MatrixM temp; m.inverse(temp); mul(temp, dest);}
  808. void MatrixM ::div(C Matrix &m, MatrixM &dest)C {Matrix temp; m.inverse(temp); mul(temp, dest);}
  809. void MatrixM ::div(C MatrixM &m, MatrixM &dest)C {MatrixM temp; m.inverse(temp); mul(temp, dest);}
  810. void MatrixD ::div(C MatrixD &m, MatrixD &dest)C {MatrixD temp; m.inverse(temp); mul(temp, dest);}
  811. /******************************************************************************/
  812. Matrix3& Matrix3::inverseScale()
  813. {
  814. if(Flt l=x.length2())x/=l;
  815. if(Flt l=y.length2())y/=l;
  816. if(Flt l=z.length2())z/=l;
  817. return T;
  818. }
  819. MatrixD3& MatrixD3::inverseScale()
  820. {
  821. if(Dbl l=x.length2())x/=l;
  822. if(Dbl l=y.length2())y/=l;
  823. if(Dbl l=z.length2())z/=l;
  824. return T;
  825. }
  826. void Matrix3::inverse(Matrix3 &dest, Bool normalized)C
  827. {
  828. //dest=transpose(T);
  829. //dest.inverseScale();
  830. if(&dest!=this)
  831. {
  832. dest.x.x=x.x;
  833. dest.y.y=y.y;
  834. dest.z.z=z.z;
  835. }
  836. Flt temp;
  837. temp=x.y; dest.x.y=y.x; dest.y.x=temp;
  838. temp=x.z; dest.x.z=z.x; dest.z.x=temp;
  839. temp=y.z; dest.y.z=z.y; dest.z.y=temp;
  840. if(!normalized)dest.inverseScale();
  841. }
  842. void MatrixD3::inverse(MatrixD3 &dest, Bool normalized)C
  843. {
  844. if(&dest!=this)
  845. {
  846. dest.x.x=x.x;
  847. dest.y.y=y.y;
  848. dest.z.z=z.z;
  849. }
  850. Dbl temp;
  851. temp=x.y; dest.x.y=y.x; dest.y.x=temp;
  852. temp=x.z; dest.x.z=z.x; dest.z.x=temp;
  853. temp=y.z; dest.y.z=z.y; dest.z.y=temp;
  854. if(!normalized)dest.inverseScale();
  855. }
  856. void Matrix3::inverseNonOrthogonal(Matrix3 &dest)C
  857. {
  858. Flt f1=(y.y*z.z - z.y*y.z),
  859. f2=(z.y*x.z - x.y*z.z),
  860. f3=(x.y*y.z - y.y*x.z),
  861. det=x.x*f1 + y.x*f2 + z.x*f3;
  862. if(det)
  863. {
  864. det=1/det;
  865. if(&dest==this)
  866. {
  867. Matrix3 temp;
  868. temp.x.x=det*f1;
  869. temp.x.y=det*f2;
  870. temp.x.z=det*f3;
  871. temp.y.x=det*(z.x*y.z - y.x*z.z);
  872. temp.y.y=det*(x.x*z.z - z.x*x.z);
  873. temp.y.z=det*(y.x*x.z - x.x*y.z);
  874. temp.z.x=det*(y.x*z.y - z.x*y.y);
  875. temp.z.y=det*(z.x*x.y - x.x*z.y);
  876. temp.z.z=det*(x.x*y.y - y.x*x.y);
  877. dest=temp;
  878. }else
  879. {
  880. dest.x.x=det*f1;
  881. dest.x.y=det*f2;
  882. dest.x.z=det*f3;
  883. dest.y.x=det*(z.x*y.z - y.x*z.z);
  884. dest.y.y=det*(x.x*z.z - z.x*x.z);
  885. dest.y.z=det*(y.x*x.z - x.x*y.z);
  886. dest.z.x=det*(y.x*z.y - z.x*y.y);
  887. dest.z.y=det*(z.x*x.y - x.x*z.y);
  888. dest.z.z=det*(x.x*y.y - y.x*x.y);
  889. }
  890. }
  891. }
  892. void MatrixD3::inverseNonOrthogonal(MatrixD3 &dest)C
  893. {
  894. Dbl f1=(y.y*z.z - z.y*y.z),
  895. f2=(z.y*x.z - x.y*z.z),
  896. f3=(x.y*y.z - y.y*x.z),
  897. det=x.x*f1 + y.x*f2 + z.x*f3;
  898. if(det)
  899. {
  900. det=1/det;
  901. if(&dest==this)
  902. {
  903. MatrixD3 temp;
  904. temp.x.x=det*f1;
  905. temp.x.y=det*f2;
  906. temp.x.z=det*f3;
  907. temp.y.x=det*(z.x*y.z - y.x*z.z);
  908. temp.y.y=det*(x.x*z.z - z.x*x.z);
  909. temp.y.z=det*(y.x*x.z - x.x*y.z);
  910. temp.z.x=det*(y.x*z.y - z.x*y.y);
  911. temp.z.y=det*(z.x*x.y - x.x*z.y);
  912. temp.z.z=det*(x.x*y.y - y.x*x.y);
  913. dest=temp;
  914. }else
  915. {
  916. dest.x.x=det*f1;
  917. dest.x.y=det*f2;
  918. dest.x.z=det*f3;
  919. dest.y.x=det*(z.x*y.z - y.x*z.z);
  920. dest.y.y=det*(x.x*z.z - z.x*x.z);
  921. dest.y.z=det*(y.x*x.z - x.x*y.z);
  922. dest.z.x=det*(y.x*z.y - z.x*y.y);
  923. dest.z.y=det*(z.x*x.y - x.x*z.y);
  924. dest.z.z=det*(x.x*y.y - y.x*x.y);
  925. }
  926. }
  927. }
  928. void Matrix::inverse(Matrix &dest, Bool normalized)C
  929. {
  930. super::inverse(dest.orn(), normalized);
  931. Flt x=pos.x, y=pos.y, z=pos.z;
  932. dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
  933. dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
  934. dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
  935. }
  936. void MatrixM::inverse(MatrixM &dest, Bool normalized)C
  937. {
  938. super::inverse(dest.orn(), normalized);
  939. Dbl x=pos.x, y=pos.y, z=pos.z;
  940. dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
  941. dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
  942. dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
  943. }
  944. void MatrixD::inverse(MatrixD &dest, Bool normalized)C
  945. {
  946. super::inverse(dest.orn(), normalized);
  947. Dbl x=pos.x, y=pos.y, z=pos.z;
  948. dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
  949. dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
  950. dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
  951. }
  952. void Matrix::inverseNonOrthogonal(Matrix &dest)C
  953. {
  954. super::inverseNonOrthogonal(dest.orn());
  955. Flt x=pos.x, y=pos.y, z=pos.z;
  956. dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
  957. dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
  958. dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
  959. }
  960. void MatrixM::inverseNonOrthogonal(MatrixM &dest)C
  961. {
  962. super::inverseNonOrthogonal(dest.orn());
  963. Dbl x=pos.x, y=pos.y, z=pos.z;
  964. dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
  965. dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
  966. dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
  967. }
  968. void MatrixD::inverseNonOrthogonal(MatrixD &dest)C
  969. {
  970. super::inverseNonOrthogonal(dest.orn());
  971. Dbl x=pos.x, y=pos.y, z=pos.z;
  972. dest.pos.x=-(x*dest.x.x + y*dest.y.x + z*dest.z.x);
  973. dest.pos.y=-(x*dest.x.y + y*dest.y.y + z*dest.z.y);
  974. dest.pos.z=-(x*dest.x.z + y*dest.y.z + z*dest.z.z);
  975. }
  976. Matrix4& Matrix4::inverse()
  977. {
  978. if(Flt det=determinant())
  979. {
  980. Matrix4 t;
  981. 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;
  982. 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;
  983. 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;
  984. 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;
  985. 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;
  986. 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;
  987. 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;
  988. 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;
  989. 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;
  990. 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;
  991. 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;
  992. 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;
  993. 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;
  994. 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;
  995. 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;
  996. 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;
  997. det=1/det;
  998. x =t.x *det;
  999. y =t.y *det;
  1000. z =t.z *det;
  1001. pos=t.pos*det;
  1002. }
  1003. return T;
  1004. }
  1005. Matrix4& Matrix4::transpose()
  1006. {
  1007. Swap(x.y, y.x);
  1008. Swap(x.z, z.x);
  1009. Swap(x.w, pos.x);
  1010. Swap(y.z, z.y);
  1011. Swap(y.w, pos.y);
  1012. Swap(z.w, pos.z);
  1013. return T;
  1014. }
  1015. /******************************************************************************/
  1016. Matrix3& Matrix3::normalize()
  1017. {
  1018. if(!x.normalize()
  1019. || !y.normalize()
  1020. || !z.normalize())identity();
  1021. return T;
  1022. }
  1023. MatrixD3& MatrixD3::normalize()
  1024. {
  1025. if(!x.normalize()
  1026. || !y.normalize()
  1027. || !z.normalize())identity();
  1028. return T;
  1029. }
  1030. Matrix3& Matrix3::normalize(Flt scale)
  1031. {
  1032. if(!x.setLength(scale)
  1033. || !y.setLength(scale)
  1034. || !z.setLength(scale))setScale(scale);
  1035. return T;
  1036. }
  1037. MatrixD3& MatrixD3::normalize(Dbl scale)
  1038. {
  1039. if(!x.setLength(scale)
  1040. || !y.setLength(scale)
  1041. || !z.setLength(scale))setScale(scale);
  1042. return T;
  1043. }
  1044. Matrix3& Matrix3::normalize(C Vec &scale)
  1045. {
  1046. if(!x.setLength(scale.x)
  1047. || !y.setLength(scale.y)
  1048. || !z.setLength(scale.z))setScale(scale);
  1049. return T;
  1050. }
  1051. MatrixD3& MatrixD3::normalize(C VecD &scale)
  1052. {
  1053. if(!x.setLength(scale.x)
  1054. || !y.setLength(scale.y)
  1055. || !z.setLength(scale.z))setScale(scale);
  1056. return T;
  1057. }
  1058. /******************************************************************************/
  1059. Matrix3& Matrix3::scaleL(C Vec &scale)
  1060. {
  1061. x*=scale.x;
  1062. y*=scale.y;
  1063. z*=scale.z;
  1064. return T;
  1065. }
  1066. Matrix& Matrix::scaleL(C Vec &scale)
  1067. {
  1068. // adjust position before scaling axes, in case 'scale' is zero and would destroy them
  1069. Vec dir; Flt d;
  1070. if(d=scale.x-1){dir=x; dir.normalize(); pos+=dir*(DistPointPlane(pos, dir)*d);}
  1071. if(d=scale.y-1){dir=y; dir.normalize(); pos+=dir*(DistPointPlane(pos, dir)*d);}
  1072. if(d=scale.z-1){dir=z; dir.normalize(); pos+=dir*(DistPointPlane(pos, dir)*d);}
  1073. super::scaleL(scale);
  1074. return T;
  1075. }
  1076. /******************************************************************************/
  1077. Matrix3& Matrix3::scale(C Vec &dir, Flt scale)
  1078. {
  1079. //Flt plane_dist=DistPointPlane(axis, dir); Flt wanted_plane_dist=plane_dist*scale; axis+=dir*(wanted_plane_dist-plane_dist);
  1080. //axis+=dir*(DistPointPlane(axis, dir)*(scale-1));
  1081. scale--;
  1082. x+=dir*(DistPointPlane(x, dir)*scale);
  1083. y+=dir*(DistPointPlane(y, dir)*scale);
  1084. z+=dir*(DistPointPlane(z, dir)*scale);
  1085. return T;
  1086. }
  1087. MatrixD3& MatrixD3::scale(C VecD &dir, Dbl scale)
  1088. {
  1089. scale--;
  1090. x+=dir*(DistPointPlane(x, dir)*scale);
  1091. y+=dir*(DistPointPlane(y, dir)*scale);
  1092. z+=dir*(DistPointPlane(z, dir)*scale);
  1093. return T;
  1094. }
  1095. Matrix& Matrix::scale(C Vec &dir, Flt scale)
  1096. {
  1097. scale--;
  1098. x +=dir*(DistPointPlane(x , dir)*scale);
  1099. y +=dir*(DistPointPlane(y , dir)*scale);
  1100. z +=dir*(DistPointPlane(z , dir)*scale);
  1101. pos+=dir*(DistPointPlane(pos, dir)*scale);
  1102. return T;
  1103. }
  1104. MatrixD& MatrixD::scale(C VecD &dir, Dbl scale)
  1105. {
  1106. scale--;
  1107. x +=dir*(DistPointPlane(x , dir)*scale);
  1108. y +=dir*(DistPointPlane(y , dir)*scale);
  1109. z +=dir*(DistPointPlane(z , dir)*scale);
  1110. pos+=dir*(DistPointPlane(pos, dir)*scale);
  1111. return T;
  1112. }
  1113. Matrix3& Matrix3::scalePlane(C Vec &nrm, Flt scale)
  1114. {
  1115. T.scale(scale ); // first scale globally
  1116. if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
  1117. return T;
  1118. }
  1119. MatrixD3& MatrixD3::scalePlane(C VecD &nrm, Dbl scale)
  1120. {
  1121. T.scale(scale ); // first scale globally
  1122. if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
  1123. return T;
  1124. }
  1125. Matrix& Matrix::scalePlane(C Vec &nrm, Flt scale)
  1126. {
  1127. T.scale(scale ); // first scale globally
  1128. if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
  1129. return T;
  1130. }
  1131. MatrixD& MatrixD::scalePlane(C VecD &nrm, Dbl scale)
  1132. {
  1133. T.scale(scale ); // first scale globally
  1134. if(scale)T.scale(nrm, 1/scale); // then perform axis-scale by reverse of 'scale'
  1135. return T;
  1136. }
  1137. /******************************************************************************/
  1138. Matrix3& Matrix3::rotateX(Flt angle)
  1139. {
  1140. if(Any(angle))
  1141. {
  1142. Flt cos, sin; CosSin(cos, sin, angle);
  1143. Flt y=T.x.y, z=T.x.z;
  1144. T.x.y=y*cos - z*sin;
  1145. T.x.z=y*sin + z*cos;
  1146. y=T.y.y; z=T.y.z;
  1147. T.y.y=y*cos - z*sin;
  1148. T.y.z=y*sin + z*cos;
  1149. y=T.z.y; z=T.z.z;
  1150. T.z.y=y*cos - z*sin;
  1151. T.z.z=y*sin + z*cos;
  1152. }
  1153. return T;
  1154. }
  1155. Matrix3& Matrix3::rotateY(Flt angle)
  1156. {
  1157. if(Any(angle))
  1158. {
  1159. Flt cos, sin; CosSin(cos, sin, angle);
  1160. Flt x=T.x.x, z=T.x.z;
  1161. T.x.x=x*cos + z*sin;
  1162. T.x.z=z*cos - x*sin;
  1163. x=T.y.x; z=T.y.z;
  1164. T.y.x=x*cos + z*sin;
  1165. T.y.z=z*cos - x*sin;
  1166. x=T.z.x; z=T.z.z;
  1167. T.z.x=x*cos + z*sin;
  1168. T.z.z=z*cos - x*sin;
  1169. }
  1170. return T;
  1171. }
  1172. Matrix3& Matrix3::rotateZ(Flt angle)
  1173. {
  1174. if(Any(angle))
  1175. {
  1176. Flt cos, sin; CosSin(cos, sin, angle);
  1177. Flt x=T.x.x, y=T.x.y;
  1178. T.x.x=x*cos - y*sin;
  1179. T.x.y=y*cos + x*sin;
  1180. x=T.y.x; y=T.y.y;
  1181. T.y.x=x*cos - y*sin;
  1182. T.y.y=y*cos + x*sin;
  1183. x=T.z.x; y=T.z.y;
  1184. T.z.x=x*cos - y*sin;
  1185. T.z.y=y*cos + x*sin;
  1186. }
  1187. return T;
  1188. }
  1189. /******************************************************************************/
  1190. MatrixD3& MatrixD3::rotateX(Dbl angle)
  1191. {
  1192. if(Any(angle))
  1193. {
  1194. Dbl cos, sin; CosSin(cos, sin, angle);
  1195. Dbl y=T.x.y, z=T.x.z;
  1196. T.x.y=y*cos - z*sin;
  1197. T.x.z=y*sin + z*cos;
  1198. y=T.y.y; z=T.y.z;
  1199. T.y.y=y*cos - z*sin;
  1200. T.y.z=y*sin + z*cos;
  1201. y=T.z.y; z=T.z.z;
  1202. T.z.y=y*cos - z*sin;
  1203. T.z.z=y*sin + z*cos;
  1204. }
  1205. return T;
  1206. }
  1207. MatrixD3& MatrixD3::rotateY(Dbl angle)
  1208. {
  1209. if(Any(angle))
  1210. {
  1211. Dbl cos, sin; CosSin(cos, sin, angle);
  1212. Dbl x=T.x.x, z=T.x.z;
  1213. T.x.x=x*cos + z*sin;
  1214. T.x.z=z*cos - x*sin;
  1215. x=T.y.x; z=T.y.z;
  1216. T.y.x=x*cos + z*sin;
  1217. T.y.z=z*cos - x*sin;
  1218. x=T.z.x; z=T.z.z;
  1219. T.z.x=x*cos + z*sin;
  1220. T.z.z=z*cos - x*sin;
  1221. }
  1222. return T;
  1223. }
  1224. MatrixD3& MatrixD3::rotateZ(Dbl angle)
  1225. {
  1226. if(Any(angle))
  1227. {
  1228. Dbl cos, sin; CosSin(cos, sin, angle);
  1229. Dbl x=T.x.x, y=T.x.y;
  1230. T.x.x=x*cos - y*sin;
  1231. T.x.y=y*cos + x*sin;
  1232. x=T.y.x; y=T.y.y;
  1233. T.y.x=x*cos - y*sin;
  1234. T.y.y=y*cos + x*sin;
  1235. x=T.z.x; y=T.z.y;
  1236. T.z.x=x*cos - y*sin;
  1237. T.z.y=y*cos + x*sin;
  1238. }
  1239. return T;
  1240. }
  1241. /******************************************************************************/
  1242. Matrix& Matrix::rotateX(Flt angle)
  1243. {
  1244. if(Any(angle))
  1245. {
  1246. Flt cos, sin; CosSin(cos, sin, angle);
  1247. Flt y=T.x.y, z=T.x.z;
  1248. T.x.y=y*cos - z*sin;
  1249. T.x.z=y*sin + z*cos;
  1250. y=T.y.y; z=T.y.z;
  1251. T.y.y=y*cos - z*sin;
  1252. T.y.z=y*sin + z*cos;
  1253. y=T.z.y; z=T.z.z;
  1254. T.z.y=y*cos - z*sin;
  1255. T.z.z=y*sin + z*cos;
  1256. y=pos.y; z=pos.z;
  1257. pos.y=y*cos - z*sin;
  1258. pos.z=y*sin + z*cos;
  1259. }
  1260. return T;
  1261. }
  1262. Matrix& Matrix::rotateY(Flt angle)
  1263. {
  1264. if(Any(angle))
  1265. {
  1266. Flt cos, sin; CosSin(cos, sin, angle);
  1267. Flt x=T.x.x, z=T.x.z;
  1268. T.x.x=x*cos + z*sin;
  1269. T.x.z=z*cos - x*sin;
  1270. x=T.y.x; z=T.y.z;
  1271. T.y.x=x*cos + z*sin;
  1272. T.y.z=z*cos - x*sin;
  1273. x=T.z.x; z=T.z.z;
  1274. T.z.x=x*cos + z*sin;
  1275. T.z.z=z*cos - x*sin;
  1276. x=pos.x; z=pos.z;
  1277. pos.x=x*cos + z*sin;
  1278. pos.z=z*cos - x*sin;
  1279. }
  1280. return T;
  1281. }
  1282. Matrix& Matrix::rotateZ(Flt angle)
  1283. {
  1284. if(Any(angle))
  1285. {
  1286. Flt cos, sin; CosSin(cos, sin, angle);
  1287. Flt x=T.x.x, y=T.x.y;
  1288. T.x.x=x*cos - y*sin;
  1289. T.x.y=y*cos + x*sin;
  1290. x=T.y.x; y=T.y.y;
  1291. T.y.x=x*cos - y*sin;
  1292. T.y.y=y*cos + x*sin;
  1293. x=T.z.x; y=T.z.y;
  1294. T.z.x=x*cos - y*sin;
  1295. T.z.y=y*cos + x*sin;
  1296. x=pos.x; y=pos.y;
  1297. pos.x=x*cos - y*sin;
  1298. pos.y=y*cos + x*sin;
  1299. }
  1300. return T;
  1301. }
  1302. /******************************************************************************/
  1303. MatrixM& MatrixM::rotateX(Flt angle)
  1304. {
  1305. if(Any(angle))
  1306. {
  1307. Flt cos, sin; CosSin(cos, sin, angle);
  1308. Flt y=T.x.y, z=T.x.z;
  1309. T.x.y=y*cos - z*sin;
  1310. T.x.z=y*sin + z*cos;
  1311. y=T.y.y; z=T.y.z;
  1312. T.y.y=y*cos - z*sin;
  1313. T.y.z=y*sin + z*cos;
  1314. y=T.z.y; z=T.z.z;
  1315. T.z.y=y*cos - z*sin;
  1316. T.z.z=y*sin + z*cos;
  1317. {
  1318. Dbl y=pos.y, z=pos.z;
  1319. pos.y=y*cos - z*sin;
  1320. pos.z=y*sin + z*cos;
  1321. }
  1322. }
  1323. return T;
  1324. }
  1325. MatrixM& MatrixM::rotateY(Flt angle)
  1326. {
  1327. if(Any(angle))
  1328. {
  1329. Flt cos, sin; CosSin(cos, sin, angle);
  1330. Flt x=T.x.x, z=T.x.z;
  1331. T.x.x=x*cos + z*sin;
  1332. T.x.z=z*cos - x*sin;
  1333. x=T.y.x; z=T.y.z;
  1334. T.y.x=x*cos + z*sin;
  1335. T.y.z=z*cos - x*sin;
  1336. x=T.z.x; z=T.z.z;
  1337. T.z.x=x*cos + z*sin;
  1338. T.z.z=z*cos - x*sin;
  1339. {
  1340. Dbl x=pos.x, z=pos.z;
  1341. pos.x=x*cos + z*sin;
  1342. pos.z=z*cos - x*sin;
  1343. }
  1344. }
  1345. return T;
  1346. }
  1347. MatrixM& MatrixM::rotateZ(Flt angle)
  1348. {
  1349. if(Any(angle))
  1350. {
  1351. Flt cos, sin; CosSin(cos, sin, angle);
  1352. Flt x=T.x.x, y=T.x.y;
  1353. T.x.x=x*cos - y*sin;
  1354. T.x.y=y*cos + x*sin;
  1355. x=T.y.x; y=T.y.y;
  1356. T.y.x=x*cos - y*sin;
  1357. T.y.y=y*cos + x*sin;
  1358. x=T.z.x; y=T.z.y;
  1359. T.z.x=x*cos - y*sin;
  1360. T.z.y=y*cos + x*sin;
  1361. {
  1362. Dbl x=pos.x, y=pos.y;
  1363. pos.x=x*cos - y*sin;
  1364. pos.y=y*cos + x*sin;
  1365. }
  1366. }
  1367. return T;
  1368. }
  1369. /******************************************************************************/
  1370. MatrixD& MatrixD::rotateX(Dbl angle)
  1371. {
  1372. if(Any(angle))
  1373. {
  1374. Dbl cos, sin; CosSin(cos, sin, angle);
  1375. Dbl y=T.x.y, z=T.x.z;
  1376. T.x.y=y*cos - z*sin;
  1377. T.x.z=y*sin + z*cos;
  1378. y=T.y.y; z=T.y.z;
  1379. T.y.y=y*cos - z*sin;
  1380. T.y.z=y*sin + z*cos;
  1381. y=T.z.y; z=T.z.z;
  1382. T.z.y=y*cos - z*sin;
  1383. T.z.z=y*sin + z*cos;
  1384. y=pos.y; z=pos.z;
  1385. pos.y=y*cos - z*sin;
  1386. pos.z=y*sin + z*cos;
  1387. }
  1388. return T;
  1389. }
  1390. MatrixD& MatrixD::rotateY(Dbl angle)
  1391. {
  1392. if(Any(angle))
  1393. {
  1394. Dbl cos, sin; CosSin(cos, sin, angle);
  1395. Dbl x=T.x.x, z=T.x.z;
  1396. T.x.x=x*cos + z*sin;
  1397. T.x.z=z*cos - x*sin;
  1398. x=T.y.x; z=T.y.z;
  1399. T.y.x=x*cos + z*sin;
  1400. T.y.z=z*cos - x*sin;
  1401. x=T.z.x; z=T.z.z;
  1402. T.z.x=x*cos + z*sin;
  1403. T.z.z=z*cos - x*sin;
  1404. x=pos.x; z=pos.z;
  1405. pos.x=x*cos + z*sin;
  1406. pos.z=z*cos - x*sin;
  1407. }
  1408. return T;
  1409. }
  1410. MatrixD& MatrixD::rotateZ(Dbl angle)
  1411. {
  1412. if(Any(angle))
  1413. {
  1414. Dbl cos, sin; CosSin(cos, sin, angle);
  1415. Dbl x=T.x.x, y=T.x.y;
  1416. T.x.x=x*cos - y*sin;
  1417. T.x.y=y*cos + x*sin;
  1418. x=T.y.x; y=T.y.y;
  1419. T.y.x=x*cos - y*sin;
  1420. T.y.y=y*cos + x*sin;
  1421. x=T.z.x; y=T.z.y;
  1422. T.z.x=x*cos - y*sin;
  1423. T.z.y=y*cos + x*sin;
  1424. x=pos.x; y=pos.y;
  1425. pos.x=x*cos - y*sin;
  1426. pos.y=y*cos + x*sin;
  1427. }
  1428. return T;
  1429. }
  1430. /******************************************************************************/
  1431. Matrix3 & Matrix3 ::rotateXY(Flt x, Flt y) {if(Any(x, y ))T*=Matrix3 ().setRotateXY(x, y); return T;}
  1432. MatrixD3& MatrixD3::rotateXY(Dbl x, Dbl y) {if(Any(x, y ))T*=MatrixD3().setRotateXY(x, y); return T;}
  1433. Matrix & Matrix ::rotateXY(Flt x, Flt y) {if(Any(x, y ))T*=Matrix3 ().setRotateXY(x, y); return T;}
  1434. MatrixM & MatrixM ::rotateXY(Flt x, Flt y) {if(Any(x, y ))T*=Matrix3 ().setRotateXY(x, y); return T;}
  1435. MatrixD & MatrixD ::rotateXY(Dbl x, Dbl y) {if(Any(x, y ))T*=MatrixD3().setRotateXY(x, y); return T;}
  1436. Matrix3 & Matrix3 ::rotate(C Vec &axis, Flt angle) {if(Any(angle))T*=Matrix3 ().setRotate(axis, angle); return T;}
  1437. MatrixD3& MatrixD3::rotate(C VecD &axis, Dbl angle) {if(Any(angle))T*=MatrixD3().setRotate(axis, angle); return T;}
  1438. Matrix & Matrix ::rotate(C Vec &axis, Flt angle) {if(Any(angle))T*=Matrix3 ().setRotate(axis, angle); return T;}
  1439. MatrixM & MatrixM ::rotate(C Vec &axis, Flt angle) {if(Any(angle))T*=Matrix3 ().setRotate(axis, angle); return T;}
  1440. MatrixD & MatrixD ::rotate(C VecD &axis, Dbl angle) {if(Any(angle))T*=MatrixD3().setRotate(axis, angle); return T;}
  1441. Matrix3& Matrix3::rotateXL(Flt angle) {if(Any(angle))rotate(!x, angle); return T;}
  1442. Matrix3& Matrix3::rotateYL(Flt angle) {if(Any(angle))rotate(!y, angle); return T;}
  1443. Matrix3& Matrix3::rotateZL(Flt angle) {if(Any(angle))rotate(!z, angle); return T;}
  1444. MatrixD3& MatrixD3::rotateXL(Dbl angle) {if(Any(angle))rotate(!x, angle); return T;}
  1445. MatrixD3& MatrixD3::rotateYL(Dbl angle) {if(Any(angle))rotate(!y, angle); return T;}
  1446. MatrixD3& MatrixD3::rotateZL(Dbl angle) {if(Any(angle))rotate(!z, angle); return T;}
  1447. /******************************************************************************/
  1448. Matrix3& Matrix3::rotateXLOrthoNormalized(Flt angle)
  1449. {
  1450. if(Any(angle))
  1451. {
  1452. Flt cos, sin; CosSin(cos, sin, angle);
  1453. Vec z=T.z;
  1454. T.z=z*cos - y*sin;
  1455. T.y=z*sin + y*cos;
  1456. }
  1457. return T;
  1458. }
  1459. Matrix3& Matrix3::rotateXLOrthoNormalized(Flt cos, Flt sin)
  1460. {
  1461. Vec z=T.z;
  1462. T.z=z*cos - y*sin;
  1463. T.y=z*sin + y*cos;
  1464. return T;
  1465. }
  1466. Matrix3& Matrix3::rotateYLOrthoNormalized(Flt angle)
  1467. {
  1468. if(Any(angle))
  1469. {
  1470. Flt cos, sin; CosSin(cos, sin, angle);
  1471. Vec x=T.x;
  1472. T.x=x*cos - z*sin;
  1473. T.z=x*sin + z*cos;
  1474. }
  1475. return T;
  1476. }
  1477. Matrix3& Matrix3::rotateZLOrthoNormalized(Flt angle)
  1478. {
  1479. if(Any(angle))
  1480. {
  1481. Flt cos, sin; CosSin(cos, sin, angle);
  1482. Vec y=T.y;
  1483. T.y=y*cos - x*sin;
  1484. T.x=y*sin + x*cos;
  1485. }
  1486. return T;
  1487. }
  1488. /******************************************************************************/
  1489. MatrixD3& MatrixD3::rotateXLOrthoNormalized(Dbl angle)
  1490. {
  1491. if(Any(angle))
  1492. {
  1493. Dbl cos, sin; CosSin(cos, sin, angle);
  1494. VecD z=T.z;
  1495. T.z=z*cos - y*sin;
  1496. T.y=z*sin + y*cos;
  1497. }
  1498. return T;
  1499. }
  1500. MatrixD3& MatrixD3::rotateYLOrthoNormalized(Dbl angle)
  1501. {
  1502. if(Any(angle))
  1503. {
  1504. Dbl cos, sin; CosSin(cos, sin, angle);
  1505. VecD x=T.x;
  1506. T.x=x*cos - z*sin;
  1507. T.z=x*sin + z*cos;
  1508. }
  1509. return T;
  1510. }
  1511. MatrixD3& MatrixD3::rotateZLOrthoNormalized(Dbl angle)
  1512. {
  1513. if(Any(angle))
  1514. {
  1515. Dbl cos, sin; CosSin(cos, sin, angle);
  1516. VecD y=T.y;
  1517. T.y=y*cos - x*sin;
  1518. T.x=y*sin + x*cos;
  1519. }
  1520. return T;
  1521. }
  1522. /******************************************************************************/
  1523. Matrix3& Matrix3::zero()
  1524. {
  1525. x.zero();
  1526. y.zero();
  1527. z.zero();
  1528. return T;
  1529. }
  1530. MatrixD3& MatrixD3::zero()
  1531. {
  1532. x.zero();
  1533. y.zero();
  1534. z.zero();
  1535. return T;
  1536. }
  1537. Matrix& Matrix::zero()
  1538. {
  1539. x .zero();
  1540. y .zero();
  1541. z .zero();
  1542. pos.zero();
  1543. return T;
  1544. }
  1545. MatrixM& MatrixM::zero()
  1546. {
  1547. x .zero();
  1548. y .zero();
  1549. z .zero();
  1550. pos.zero();
  1551. return T;
  1552. }
  1553. MatrixD& MatrixD::zero()
  1554. {
  1555. x .zero();
  1556. y .zero();
  1557. z .zero();
  1558. pos.zero();
  1559. return T;
  1560. }
  1561. Matrix4& Matrix4::zero()
  1562. {
  1563. x .zero();
  1564. y .zero();
  1565. z .zero();
  1566. pos.zero();
  1567. return T;
  1568. }
  1569. /******************************************************************************/
  1570. Matrix3& Matrix3::identity()
  1571. {
  1572. x.set(1, 0, 0);
  1573. y.set(0, 1, 0);
  1574. z.set(0, 0, 1);
  1575. return T;
  1576. }
  1577. MatrixD3& MatrixD3::identity()
  1578. {
  1579. x.set(1, 0, 0);
  1580. y.set(0, 1, 0);
  1581. z.set(0, 0, 1);
  1582. return T;
  1583. }
  1584. Matrix& Matrix::identity()
  1585. {
  1586. x .set (1, 0, 0);
  1587. y .set (0, 1, 0);
  1588. z .set (0, 0, 1);
  1589. pos.zero( );
  1590. return T;
  1591. }
  1592. MatrixM& MatrixM::identity()
  1593. {
  1594. x .set (1, 0, 0);
  1595. y .set (0, 1, 0);
  1596. z .set (0, 0, 1);
  1597. pos.zero( );
  1598. return T;
  1599. }
  1600. MatrixD& MatrixD::identity()
  1601. {
  1602. x .set (1, 0, 0);
  1603. y .set (0, 1, 0);
  1604. z .set (0, 0, 1);
  1605. pos.zero( );
  1606. return T;
  1607. }
  1608. Matrix4& Matrix4::identity()
  1609. {
  1610. x .set(1, 0, 0, 0);
  1611. y .set(0, 1, 0, 0);
  1612. z .set(0, 0, 1, 0);
  1613. pos.set(0, 0, 0, 1);
  1614. return T;
  1615. }
  1616. /******************************************************************************/
  1617. Matrix3& Matrix3::identity(Flt blend)
  1618. {
  1619. if(blend>0)
  1620. {
  1621. if(blend>=1)identity();else
  1622. {
  1623. Flt blend1=1-blend;
  1624. // orientation
  1625. Vec axis; Flt angle=axisAngle(axis);
  1626. // scale
  1627. Vec scale=T.scale();
  1628. REPA(scale)scale.c[i]=ScaleFactor(ScaleFactorR(scale.c[i])*blend1);
  1629. setRotate(axis, angle*blend1).scaleL(scale);
  1630. }
  1631. }
  1632. return T;
  1633. }
  1634. Matrix& Matrix::identity(Flt blend)
  1635. {
  1636. if(blend>0)
  1637. {
  1638. if(blend>=1)identity();else
  1639. {
  1640. pos*=1-blend;
  1641. super::identity(blend);
  1642. }
  1643. }
  1644. return T;
  1645. }
  1646. /******************************************************************************/
  1647. Matrix& Matrix::setPos(Flt x, Flt y, Flt z)
  1648. {
  1649. T.pos.set(x, y, z);
  1650. T.x .set(1, 0, 0);
  1651. T.y .set(0, 1, 0);
  1652. T.z .set(0, 0, 1);
  1653. return T;
  1654. }
  1655. MatrixM& MatrixM::setPos(Dbl x, Dbl y, Dbl z)
  1656. {
  1657. T.pos.set(x, y, z);
  1658. T.x .set(1, 0, 0);
  1659. T.y .set(0, 1, 0);
  1660. T.z .set(0, 0, 1);
  1661. return T;
  1662. }
  1663. MatrixD& MatrixD::setPos(Dbl x, Dbl y, Dbl z)
  1664. {
  1665. T.pos.set(x, y, z);
  1666. T.x .set(1, 0, 0);
  1667. T.y .set(0, 1, 0);
  1668. T.z .set(0, 0, 1);
  1669. return T;
  1670. }
  1671. /******************************************************************************/
  1672. Matrix& Matrix::setPos(C Vec2 &pos)
  1673. {
  1674. T.pos.set(pos , 0);
  1675. x .set(1, 0, 0);
  1676. y .set(0, 1, 0);
  1677. z .set(0, 0, 1);
  1678. return T;
  1679. }
  1680. MatrixM& MatrixM::setPos(C VecD2 &pos)
  1681. {
  1682. T.pos.set(pos , 0);
  1683. x .set(1, 0, 0);
  1684. y .set(0, 1, 0);
  1685. z .set(0, 0, 1);
  1686. return T;
  1687. }
  1688. MatrixD& MatrixD::setPos(C VecD2 &pos)
  1689. {
  1690. T.pos.set(pos , 0);
  1691. x .set(1, 0, 0);
  1692. y .set(0, 1, 0);
  1693. z .set(0, 0, 1);
  1694. return T;
  1695. }
  1696. /******************************************************************************/
  1697. Matrix& Matrix::setPos(C Vec &pos)
  1698. {
  1699. T.pos=pos;
  1700. x .set(1, 0, 0);
  1701. y .set(0, 1, 0);
  1702. z .set(0, 0, 1);
  1703. return T;
  1704. }
  1705. MatrixM& MatrixM::setPos(C VecD &pos)
  1706. {
  1707. T.pos=pos;
  1708. x .set(1, 0, 0);
  1709. y .set(0, 1, 0);
  1710. z .set(0, 0, 1);
  1711. return T;
  1712. }
  1713. MatrixD& MatrixD::setPos(C VecD &pos)
  1714. {
  1715. T.pos=pos;
  1716. x .set(1, 0, 0);
  1717. y .set(0, 1, 0);
  1718. z .set(0, 0, 1);
  1719. return T;
  1720. }
  1721. /******************************************************************************/
  1722. Matrix3& Matrix3::setScale(Flt scale)
  1723. {
  1724. x.set(scale, 0, 0);
  1725. y.set(0, scale, 0);
  1726. z.set(0, 0, scale);
  1727. return T;
  1728. }
  1729. MatrixD3& MatrixD3::setScale(Dbl scale)
  1730. {
  1731. x.set(scale, 0, 0);
  1732. y.set(0, scale, 0);
  1733. z.set(0, 0, scale);
  1734. return T;
  1735. }
  1736. /******************************************************************************/
  1737. Matrix& Matrix::setScale(Flt scale)
  1738. {
  1739. x .set (scale, 0, 0);
  1740. y .set (0, scale, 0);
  1741. z .set (0, 0, scale);
  1742. pos.zero( );
  1743. return T;
  1744. }
  1745. MatrixM& MatrixM::setScale(Flt scale)
  1746. {
  1747. x .set (scale, 0, 0);
  1748. y .set (0, scale, 0);
  1749. z .set (0, 0, scale);
  1750. pos.zero( );
  1751. return T;
  1752. }
  1753. MatrixD& MatrixD::setScale(Dbl scale)
  1754. {
  1755. x .set (scale, 0, 0);
  1756. y .set (0, scale, 0);
  1757. z .set (0, 0, scale);
  1758. pos.zero( );
  1759. return T;
  1760. }
  1761. /******************************************************************************/
  1762. Matrix3& Matrix3::setScale(C Vec &scale)
  1763. {
  1764. x.set(scale.x, 0, 0);
  1765. y.set(0, scale.y, 0);
  1766. z.set(0, 0, scale.z);
  1767. return T;
  1768. }
  1769. MatrixD3& MatrixD3::setScale(C VecD &scale)
  1770. {
  1771. x.set(scale.x, 0, 0);
  1772. y.set(0, scale.y, 0);
  1773. z.set(0, 0, scale.z);
  1774. return T;
  1775. }
  1776. /******************************************************************************/
  1777. Matrix& Matrix::setScale(C Vec &scale)
  1778. {
  1779. x .set (scale.x, 0, 0);
  1780. y .set (0, scale.y, 0);
  1781. z .set (0, 0, scale.z);
  1782. pos.zero( );
  1783. return T;
  1784. }
  1785. MatrixM& MatrixM::setScale(C Vec &scale)
  1786. {
  1787. x .set (scale.x, 0, 0);
  1788. y .set (0, scale.y, 0);
  1789. z .set (0, 0, scale.z);
  1790. pos.zero( );
  1791. return T;
  1792. }
  1793. MatrixD& MatrixD::setScale(C VecD &scale)
  1794. {
  1795. x .set (scale.x, 0, 0);
  1796. y .set (0, scale.y, 0);
  1797. z .set (0, 0, scale.z);
  1798. pos.zero( );
  1799. return T;
  1800. }
  1801. /******************************************************************************/
  1802. Matrix& Matrix::setPosScale(C Vec &pos, Flt scale)
  1803. {
  1804. T.pos.set(pos.x*scale, pos.y*scale, pos.z*scale);
  1805. x .set(scale, 0, 0);
  1806. y .set(0, scale, 0);
  1807. z .set(0, 0, scale);
  1808. return T;
  1809. }
  1810. MatrixM& MatrixM::setPosScale(C VecD &pos, Flt scale)
  1811. {
  1812. T.pos.set(pos.x*scale, pos.y*scale, pos.z*scale);
  1813. x .set(scale, 0, 0);
  1814. y .set(0, scale, 0);
  1815. z .set(0, 0, scale);
  1816. return T;
  1817. }
  1818. MatrixD& MatrixD::setPosScale(C VecD &pos, Dbl scale)
  1819. {
  1820. T.pos.set(pos.x*scale, pos.y*scale, pos.z*scale);
  1821. x .set(scale, 0, 0);
  1822. y .set(0, scale, 0);
  1823. z .set(0, 0, scale);
  1824. return T;
  1825. }
  1826. /******************************************************************************/
  1827. Matrix& Matrix::setPosScale(C Vec &pos, C Vec &scale)
  1828. {
  1829. T.pos.set(pos.x*scale.x, pos.y*scale.y, pos.z*scale.z);
  1830. x .set(scale.x, 0, 0);
  1831. y .set(0, scale.y, 0);
  1832. z .set(0, 0, scale.z);
  1833. return T;
  1834. }
  1835. MatrixM& MatrixM::setPosScale(C VecD &pos, C Vec &scale)
  1836. {
  1837. T.pos.set(pos.x*scale.x, pos.y*scale.y, pos.z*scale.z);
  1838. x .set(scale.x, 0, 0);
  1839. y .set(0, scale.y, 0);
  1840. z .set(0, 0, scale.z);
  1841. return T;
  1842. }
  1843. MatrixD& MatrixD::setPosScale(C VecD &pos, C VecD &scale)
  1844. {
  1845. T.pos.set(pos.x*scale.x, pos.y*scale.y, pos.z*scale.z);
  1846. x .set(scale.x, 0, 0);
  1847. y .set(0, scale.y, 0);
  1848. z .set(0, 0, scale.z);
  1849. return T;
  1850. }
  1851. /******************************************************************************/
  1852. Matrix& Matrix::setScalePos(Flt scale, C Vec &pos)
  1853. {
  1854. T.pos=pos;
  1855. x .set(scale, 0, 0);
  1856. y .set(0, scale, 0);
  1857. z .set(0, 0, scale);
  1858. return T;
  1859. }
  1860. MatrixM& MatrixM::setScalePos(Flt scale, C VecD &pos)
  1861. {
  1862. T.pos=pos;
  1863. x .set(scale, 0, 0);
  1864. y .set(0, scale, 0);
  1865. z .set(0, 0, scale);
  1866. return T;
  1867. }
  1868. MatrixD& MatrixD::setScalePos(Dbl scale, C VecD &pos)
  1869. {
  1870. T.pos=pos;
  1871. x .set(scale, 0, 0);
  1872. y .set(0, scale, 0);
  1873. z .set(0, 0, scale);
  1874. return T;
  1875. }
  1876. /******************************************************************************/
  1877. Matrix& Matrix::setScalePos(C Vec &scale, C Vec &pos)
  1878. {
  1879. T.pos=pos;
  1880. x .set(scale.x, 0, 0);
  1881. y .set(0, scale.y, 0);
  1882. z .set(0, 0, scale.z);
  1883. return T;
  1884. }
  1885. MatrixM& MatrixM::setScalePos(C Vec &scale, C VecD &pos)
  1886. {
  1887. T.pos=pos;
  1888. x .set(scale.x, 0, 0);
  1889. y .set(0, scale.y, 0);
  1890. z .set(0, 0, scale.z);
  1891. return T;
  1892. }
  1893. MatrixD& MatrixD::setScalePos(C VecD &scale, C VecD &pos)
  1894. {
  1895. T.pos=pos;
  1896. x .set(scale.x, 0, 0);
  1897. y .set(0, scale.y, 0);
  1898. z .set(0, 0, scale.z);
  1899. return T;
  1900. }
  1901. /******************************************************************************/
  1902. Matrix3& Matrix3::setRotateX(Flt angle)
  1903. {
  1904. Flt c, s; CosSin(c, s, angle);
  1905. x.set( 1, 0, 0);
  1906. y.set( 0, c, s);
  1907. z.set( 0,-s, c);
  1908. return T;
  1909. }
  1910. MatrixD3& MatrixD3::setRotateX(Dbl angle)
  1911. {
  1912. Dbl c, s; CosSin(c, s, angle);
  1913. x.set( 1, 0, 0);
  1914. y.set( 0, c, s);
  1915. z.set( 0,-s, c);
  1916. return T;
  1917. }
  1918. Matrix3& Matrix3::setRotateY(Flt angle)
  1919. {
  1920. Flt c, s; CosSin(c, s, angle);
  1921. x.set( c, 0,-s);
  1922. y.set( 0, 1, 0);
  1923. z.set( s, 0, c);
  1924. return T;
  1925. }
  1926. MatrixD3& MatrixD3::setRotateY(Dbl angle)
  1927. {
  1928. Dbl c, s; CosSin(c, s, angle);
  1929. x.set( c, 0,-s);
  1930. y.set( 0, 1, 0);
  1931. z.set( s, 0, c);
  1932. return T;
  1933. }
  1934. Matrix3& Matrix3::setRotateZ(Flt angle)
  1935. {
  1936. Flt c, s; CosSin(c, s, angle);
  1937. x.set( c, s, 0);
  1938. y.set(-s, c, 0);
  1939. z.set( 0, 0, 1);
  1940. return T;
  1941. }
  1942. MatrixD3& MatrixD3::setRotateZ(Dbl angle)
  1943. {
  1944. Dbl c, s; CosSin(c, s, angle);
  1945. x.set( c, s, 0);
  1946. y.set(-s, c, 0);
  1947. z.set( 0, 0, 1);
  1948. return T;
  1949. }
  1950. Matrix3& Matrix3::setRotateXY(Flt pitch, Flt yaw)
  1951. {
  1952. Flt cp, sp; CosSin(cp, sp, pitch);
  1953. Flt cy, sy; CosSin(cy, sy, yaw );
  1954. y.x= sp*sy;
  1955. y.z= sp*cy;
  1956. z.x= cp*sy;
  1957. z.z= cp*cy;
  1958. y.y= cp;
  1959. x.x= cy;
  1960. z.y=-sp;
  1961. x.z=-sy;
  1962. x.y= 0;
  1963. return T;
  1964. }
  1965. MatrixD3& MatrixD3::setRotateXY(Dbl pitch, Dbl yaw)
  1966. {
  1967. Dbl cp, sp; CosSin(cp, sp, pitch);
  1968. Dbl cy, sy; CosSin(cy, sy, yaw );
  1969. y.x= sp*sy;
  1970. y.z= sp*cy;
  1971. z.x= cp*sy;
  1972. z.z= cp*cy;
  1973. y.y= cp;
  1974. x.x= cy;
  1975. z.y=-sp;
  1976. x.z=-sy;
  1977. x.y= 0;
  1978. return T;
  1979. }
  1980. Matrix3& Matrix3::setRotate(C Vec &axis, Flt angle)
  1981. {
  1982. Flt c, s; CosSin(c, s, -angle); // !! this must match 'setRotateCosSin' !!
  1983. Flt x =axis.x,
  1984. y =axis.y,
  1985. z =axis.z,
  1986. cc = 1-c,
  1987. ccx=cc*x,
  1988. ccy=cc*y,
  1989. ccz=cc*z,
  1990. p, q;
  1991. T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
  1992. p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
  1993. p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
  1994. p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
  1995. return T;
  1996. }
  1997. MatrixD3& MatrixD3::setRotate(C VecD &axis, Dbl angle)
  1998. {
  1999. Dbl c, s; CosSin(c, s, -angle); // !! this must match 'setRotateCosSin' !!
  2000. Dbl x =axis.x,
  2001. y =axis.y,
  2002. z =axis.z,
  2003. cc = 1-c,
  2004. ccx=cc*x,
  2005. ccy=cc*y,
  2006. ccz=cc*z,
  2007. p, q;
  2008. T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
  2009. p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
  2010. p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
  2011. p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
  2012. return T;
  2013. }
  2014. Matrix3& Matrix3::setRotateCosSin(C Vec &axis, Flt cos, Flt sin)
  2015. {
  2016. Flt c=cos, s=-sin; // !! this must match 'setRotate' !!
  2017. Flt x =axis.x,
  2018. y =axis.y,
  2019. z =axis.z,
  2020. cc = 1-c,
  2021. ccx=cc*x,
  2022. ccy=cc*y,
  2023. ccz=cc*z,
  2024. p, q;
  2025. T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
  2026. p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
  2027. p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
  2028. p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
  2029. return T;
  2030. }
  2031. MatrixD3& MatrixD3::setRotateCosSin(C VecD &axis, Dbl cos, Dbl sin)
  2032. {
  2033. Dbl c=cos, s=-sin; // !! this must match 'setRotate' !!
  2034. Dbl x =axis.x,
  2035. y =axis.y,
  2036. z =axis.z,
  2037. cc = 1-c,
  2038. ccx=cc*x,
  2039. ccy=cc*y,
  2040. ccz=cc*z,
  2041. p, q;
  2042. T.x.x=ccx*x+c; T.y.y=ccy*y+c; T.z.z=ccz*z+c;
  2043. p=x*s; T.z.y=ccz*y+p; T.y.z=ccy*z-p;
  2044. p=y*s; q=ccx*z; T.x.z= q+p; T.z.x= q-p;
  2045. p=z*s; q=ccx*y; T.y.x= q+p; T.x.y= q-p;
  2046. return T;
  2047. }
  2048. /******************************************************************************/
  2049. Matrix& Matrix::setRotateX ( Flt angle) {super::setRotateX ( angle); pos.zero(); return T;}
  2050. Matrix& Matrix::setRotateY ( Flt angle) {super::setRotateY ( angle); pos.zero(); return T;}
  2051. Matrix& Matrix::setRotateZ ( Flt angle) {super::setRotateZ ( angle); pos.zero(); return T;}
  2052. Matrix& Matrix::setRotateXY( Flt x , Flt y ) {super::setRotateXY(x , y ); pos.zero(); return T;}
  2053. Matrix& Matrix::setRotate (C Vec &axis, Flt angle) {super::setRotate (axis, angle); pos.zero(); return T;}
  2054. MatrixM& MatrixM::setRotateX ( Flt angle) {super::setRotateX ( angle); pos.zero(); return T;}
  2055. MatrixM& MatrixM::setRotateY ( Flt angle) {super::setRotateY ( angle); pos.zero(); return T;}
  2056. MatrixM& MatrixM::setRotateZ ( Flt angle) {super::setRotateZ ( angle); pos.zero(); return T;}
  2057. MatrixM& MatrixM::setRotateXY( Flt x , Flt y ) {super::setRotateXY(x , y ); pos.zero(); return T;}
  2058. MatrixM& MatrixM::setRotate (C Vec &axis, Flt angle) {super::setRotate (axis, angle); pos.zero(); return T;}
  2059. MatrixD& MatrixD::setRotateX ( Dbl angle) {super::setRotateX ( angle); pos.zero(); return T;}
  2060. MatrixD& MatrixD::setRotateY ( Dbl angle) {super::setRotateY ( angle); pos.zero(); return T;}
  2061. MatrixD& MatrixD::setRotateZ ( Dbl angle) {super::setRotateZ ( angle); pos.zero(); return T;}
  2062. MatrixD& MatrixD::setRotateXY( Dbl x , Dbl y ) {super::setRotateXY(x , y ); pos.zero(); return T;}
  2063. MatrixD& MatrixD::setRotate (C VecD &axis, Dbl angle) {super::setRotate (axis, angle); pos.zero(); return T;}
  2064. /******************************************************************************/
  2065. Matrix3& Matrix3::setOrient(DIR_ENUM dir)
  2066. {
  2067. Zero(T); switch(dir)
  2068. {
  2069. default : x.x= 1; y.y= 1; z.z= 1; break;
  2070. case DIR_BACK : x.x=-1; y.y= 1; z.z=-1; break;
  2071. case DIR_UP : x.x= 1; y.z=-1; z.y= 1; break;
  2072. case DIR_DOWN : x.x= 1; y.z= 1; z.y=-1; break;
  2073. case DIR_RIGHT: x.z=-1; y.y= 1; z.x= 1; break;
  2074. case DIR_LEFT : x.z= 1; y.y= 1; z.x=-1; break;
  2075. }
  2076. return T;
  2077. }
  2078. MatrixD3& MatrixD3::setOrient(DIR_ENUM dir)
  2079. {
  2080. Zero(T); switch(dir)
  2081. {
  2082. default : x.x= 1; y.y= 1; z.z= 1; break;
  2083. case DIR_BACK : x.x=-1; y.y= 1; z.z=-1; break;
  2084. case DIR_UP : x.x= 1; y.z=-1; z.y= 1; break;
  2085. case DIR_DOWN : x.x= 1; y.z= 1; z.y=-1; break;
  2086. case DIR_RIGHT: x.z=-1; y.y= 1; z.x= 1; break;
  2087. case DIR_LEFT : x.z= 1; y.y= 1; z.x=-1; break;
  2088. }
  2089. return T;
  2090. }
  2091. Matrix3 & Matrix3 ::setRight(C Vec &right ) {x=right; y=PerpN(x); z=Cross(x, y); return T;}
  2092. MatrixD3& MatrixD3::setRight(C VecD &right ) {x=right; y=PerpN(x); z=Cross(x, y); return T;}
  2093. Matrix3 & Matrix3 ::setUp (C Vec &up ) {y=up ; z=PerpN(y); x=Cross(y, z); return T;}
  2094. MatrixD3& MatrixD3::setUp (C VecD &up ) {y=up ; z=PerpN(y); x=Cross(y, z); return T;}
  2095. Matrix3 & Matrix3 ::setDir (C Vec &dir ) {z=dir ; y=PerpN(z); x=Cross(y, z); return T;}
  2096. MatrixD3& MatrixD3::setDir (C VecD &dir ) {z=dir ; y=PerpN(z); x=Cross(y, z); return T;}
  2097. Matrix3 & Matrix3 ::setDir (C Vec &dir, C Vec &up ) {z=dir ; y=up; x=Cross(y, z); return T;}
  2098. MatrixD3& MatrixD3::setDir (C VecD &dir, C VecD &up ) {z=dir ; y=up; x=Cross(y, z); return T;}
  2099. Matrix3 & Matrix3 ::setDir (C Vec &dir, C Vec &up, C Vec &right) {z=dir ; y=up; x=right ; return T;}
  2100. MatrixD3& MatrixD3::setDir (C VecD &dir, C VecD &up, C VecD &right) {z=dir ; y=up; x=right ; return T;}
  2101. Matrix & Matrix ::setPosOrient(C Vec &pos, DIR_ENUM dir ) {super::setOrient(dir ); T.pos=pos; return T;}
  2102. MatrixM& MatrixM::setPosOrient(C VecD &pos, DIR_ENUM dir ) {super::setOrient(dir ); T.pos=pos; return T;}
  2103. MatrixD& MatrixD::setPosOrient(C VecD &pos, DIR_ENUM dir ) {super::setOrient(dir ); T.pos=pos; return T;}
  2104. Matrix & Matrix ::setPosRight (C Vec &pos, C Vec &right ) {super::setRight (right ); T.pos=pos; return T;}
  2105. MatrixM& MatrixM::setPosRight (C VecD &pos, C Vec &right ) {super::setRight (right ); T.pos=pos; return T;}
  2106. MatrixD& MatrixD::setPosRight (C VecD &pos, C VecD &right ) {super::setRight (right ); T.pos=pos; return T;}
  2107. Matrix & Matrix ::setPosUp (C Vec &pos, C Vec &up ) {super::setUp (up ); T.pos=pos; return T;}
  2108. MatrixM& MatrixM::setPosUp (C VecD &pos, C Vec &up ) {super::setUp (up ); T.pos=pos; return T;}
  2109. MatrixD& MatrixD::setPosUp (C VecD &pos, C VecD &up ) {super::setUp (up ); T.pos=pos; return T;}
  2110. Matrix & Matrix ::setPosDir (C Vec &pos, C Vec &dir ) {super::setDir (dir ); T.pos=pos; return T;}
  2111. MatrixM& MatrixM::setPosDir (C VecD &pos, C Vec &dir ) {super::setDir (dir ); T.pos=pos; return T;}
  2112. MatrixD& MatrixD::setPosDir (C VecD &pos, C VecD &dir ) {super::setDir (dir ); T.pos=pos; return T;}
  2113. Matrix & Matrix ::setPosDir (C Vec &pos, C Vec &dir, C Vec &up ) {super::setDir (dir, up); T.pos=pos; return T;}
  2114. MatrixM& MatrixM::setPosDir (C VecD &pos, C Vec &dir, C Vec &up ) {super::setDir (dir, up); T.pos=pos; return T;}
  2115. MatrixD& MatrixD::setPosDir (C VecD &pos, C VecD &dir, C VecD &up ) {super::setDir (dir, up); T.pos=pos; return T;}
  2116. 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;}
  2117. 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;}
  2118. 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;}
  2119. /******************************************************************************/
  2120. Matrix3& Matrix3::setRotation(C Vec &dir_from, C Vec &dir_to)
  2121. {
  2122. Vec cross=Cross(dir_from, dir_to); if(Flt sin=cross.normalize())setRotateCosSin(cross, Dot(dir_from, dir_to), sin);else identity();
  2123. return T;
  2124. }
  2125. MatrixD3& MatrixD3::setRotation(C VecD &dir_from, C VecD &dir_to)
  2126. {
  2127. VecD cross=Cross(dir_from, dir_to); if(Dbl sin=cross.normalize())setRotateCosSin(cross, Dot(dir_from, dir_to), sin);else identity();
  2128. return T;
  2129. }
  2130. Matrix3& Matrix3::setRotation(C Vec &dir_from, C Vec &dir_to, Flt blend)
  2131. {
  2132. Vec cross=Cross(dir_from, dir_to); if(Flt sin=cross.normalize())setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
  2133. return T;
  2134. }
  2135. MatrixD3& MatrixD3::setRotation(C VecD &dir_from, C VecD &dir_to, Dbl blend)
  2136. {
  2137. VecD cross=Cross(dir_from, dir_to); if(Dbl sin=cross.normalize())setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
  2138. return T;
  2139. }
  2140. Matrix3& Matrix3::setRotation(C Vec &dir_from, C Vec &dir_to, Flt blend, Flt roll)
  2141. {
  2142. Vec cross=Cross(dir_from, dir_to); Flt sin=cross.normalize();
  2143. if(roll)
  2144. {
  2145. setRotate(dir_from, roll);
  2146. if(sin)rotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);
  2147. }else
  2148. {
  2149. if(sin)setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
  2150. }
  2151. return T;
  2152. }
  2153. MatrixD3& MatrixD3::setRotation(C VecD &dir_from, C VecD &dir_to, Dbl blend, Dbl roll)
  2154. {
  2155. VecD cross=Cross(dir_from, dir_to); Dbl sin=cross.normalize();
  2156. if(roll)
  2157. {
  2158. setRotate(dir_from, roll);
  2159. if(sin)rotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);
  2160. }else
  2161. {
  2162. if(sin)setRotate(cross, ACosSin(Dot(dir_from, dir_to), sin)*blend);else identity();
  2163. }
  2164. return T;
  2165. }
  2166. Matrix& Matrix::setRotation(C Vec &pos, C Vec &dir_from, C Vec &dir_to, Flt blend)
  2167. {
  2168. 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();
  2169. return T;
  2170. }
  2171. /******************************************************************************/
  2172. Matrix& Matrix::set(C Box &src, C Box &dest)
  2173. {
  2174. Vec scale(dest.w()/src.w(),
  2175. dest.h()/src.h(),
  2176. dest.d()/src.d());
  2177. super::setScale(scale);
  2178. pos=dest.min-src.min*scale;
  2179. return T;
  2180. }
  2181. Matrix& Matrix::setNormalizeX(C Box &box) {Flt d=box.w(); setPosScale(-box.center(), (d>EPS) ? 1/d : 1); return T;}
  2182. Matrix& Matrix::setNormalizeY(C Box &box) {Flt d=box.h(); setPosScale(-box.center(), (d>EPS) ? 1/d : 1); return T;}
  2183. Matrix& Matrix::setNormalizeZ(C Box &box) {Flt d=box.d(); setPosScale(-box.center(), (d>EPS) ? 1/d : 1); return T;}
  2184. Matrix& Matrix::setNormalize (C Box &box)
  2185. {
  2186. Vec size=box.size();
  2187. if( size.x>=size.y && size.x>=size.z)return setNormalizeX(box); // x is the biggest
  2188. if( size.y>=size.z)return setNormalizeY(box); // y is the biggest
  2189. return setNormalizeZ(box); // z is the biggest
  2190. }
  2191. /******************************************************************************/
  2192. Matrix3& Matrix3::mirrorX()
  2193. {
  2194. CHS(x.x);
  2195. CHS(y.x);
  2196. CHS(z.x);
  2197. return T;
  2198. }
  2199. Matrix3& Matrix3::mirrorY()
  2200. {
  2201. CHS(x.y);
  2202. CHS(y.y);
  2203. CHS(z.y);
  2204. return T;
  2205. }
  2206. Matrix3& Matrix3::mirrorZ()
  2207. {
  2208. CHS(x.z);
  2209. CHS(y.z);
  2210. CHS(z.z);
  2211. return T;
  2212. }
  2213. MatrixD3& MatrixD3::mirrorX()
  2214. {
  2215. CHS(x.x);
  2216. CHS(y.x);
  2217. CHS(z.x);
  2218. return T;
  2219. }
  2220. MatrixD3& MatrixD3::mirrorY()
  2221. {
  2222. CHS(x.y);
  2223. CHS(y.y);
  2224. CHS(z.y);
  2225. return T;
  2226. }
  2227. MatrixD3& MatrixD3::mirrorZ()
  2228. {
  2229. CHS(x.z);
  2230. CHS(y.z);
  2231. CHS(z.z);
  2232. return T;
  2233. }
  2234. Matrix3& Matrix3::mirror(C Vec &normal)
  2235. {
  2236. x=Mirror(x, normal);
  2237. y=Mirror(y, normal);
  2238. z=Mirror(z, normal);
  2239. return T;
  2240. }
  2241. MatrixD3& MatrixD3::mirror(C VecD &normal)
  2242. {
  2243. x=Mirror(x, normal);
  2244. y=Mirror(y, normal);
  2245. z=Mirror(z, normal);
  2246. return T;
  2247. }
  2248. Matrix & Matrix ::mirrorX() {super::mirrorX(); CHS(pos.x); return T;}
  2249. Matrix & Matrix ::mirrorY() {super::mirrorY(); CHS(pos.y); return T;}
  2250. Matrix & Matrix ::mirrorZ() {super::mirrorZ(); CHS(pos.z); return T;}
  2251. MatrixD& MatrixD::mirrorX() {super::mirrorX(); CHS(pos.x); return T;}
  2252. MatrixD& MatrixD::mirrorY() {super::mirrorY(); CHS(pos.y); return T;}
  2253. MatrixD& MatrixD::mirrorZ() {super::mirrorZ(); CHS(pos.z); return T;}
  2254. Matrix & Matrix ::mirror(C Plane &plane) {super::mirror(plane.normal); pos=Mirror(pos, plane.pos, plane.normal); return T;}
  2255. MatrixM& MatrixM::mirror(C PlaneM &plane) {super::mirror(plane.normal); pos=Mirror(pos, plane.pos, plane.normal); return T;}
  2256. MatrixD& MatrixD::mirror(C PlaneD &plane) {super::mirror(plane.normal); pos=Mirror(pos, plane.pos, plane.normal); return T;}
  2257. /******************************************************************************/
  2258. Matrix3& Matrix3::swapYZ()
  2259. {
  2260. x.swapYZ();
  2261. y.swapYZ();
  2262. z.swapYZ();
  2263. return T;
  2264. }
  2265. Matrix& Matrix::swapYZ()
  2266. {
  2267. super::swapYZ();
  2268. pos .swapYZ();
  2269. return T;
  2270. }
  2271. /******************************************************************************/
  2272. Vec Matrix3 :: scale ()C {return Vec (x.length (), y.length (), z.length ()) ;}
  2273. VecD MatrixD3:: scale ()C {return VecD(x.length (), y.length (), z.length ()) ;}
  2274. Vec Matrix3 :: scale2()C {return Vec (x.length2(), y.length2(), z.length2()) ;}
  2275. VecD MatrixD3:: scale2()C {return VecD(x.length2(), y.length2(), z.length2()) ;}
  2276. Flt Matrix3 ::avgScale ()C {return Avg (x.length (), y.length (), z.length ()) ;}
  2277. Dbl MatrixD3::avgScale ()C {return Avg (x.length (), y.length (), z.length ()) ;}
  2278. Flt Matrix3 ::maxScale ()C {return SqrtFast(Max (x.length2(), y.length2(), z.length2()));}
  2279. Dbl MatrixD3::maxScale ()C {return SqrtFast(Max (x.length2(), y.length2(), z.length2()));}
  2280. Vec Matrix3::angles()C
  2281. {
  2282. Vec O;
  2283. Matrix3 temp=T;
  2284. O.y= Angle(temp.z.zx()); temp.rotateY(-O.y);
  2285. O.x=-Angle(temp.z.zy()); temp.rotateX(-O.x);
  2286. O.z= Angle(temp.x.xy );
  2287. //if(){O.x=PI-O.x; O.y=O.y-PI; O.z=O.z-PI;} generates alternative version
  2288. return O;
  2289. }
  2290. VecD MatrixD3::angles()C
  2291. {
  2292. VecD O;
  2293. MatrixD3 temp=T;
  2294. O.y= Angle(temp.z.zx()); temp.rotateY(-O.y);
  2295. O.x=-Angle(temp.z.zy()); temp.rotateX(-O.x);
  2296. O.z= Angle(temp.x.xy );
  2297. return O;
  2298. }
  2299. Vec Matrix3::axis(Bool normalized)C
  2300. {
  2301. Matrix3 temp;
  2302. C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2303. Vec axis(m->y.z - m->z.y,
  2304. m->z.x - m->x.z,
  2305. m->x.y - m->y.x);
  2306. if(axis.normalize()<=EPS)
  2307. {
  2308. // singularity
  2309. if(m->x.x>=EPS_COS)axis.set(1, 0, 0);else // identity
  2310. {
  2311. Flt xx=(m->x.x+1)*0.5f,
  2312. yy=(m->y.y+1)*0.5f,
  2313. zz=(m->z.z+1)*0.5f,
  2314. xy=(m->x.y+m->y.x)*0.25f,
  2315. xz=(m->x.z+m->z.x)*0.25f,
  2316. yz=(m->y.z+m->z.y)*0.25f;
  2317. if(xx>=yy && xx>=zz)
  2318. {
  2319. if(xx<=EPS)axis.set(0, SQRT2_2, SQRT2_2);else
  2320. {
  2321. axis.x=SqrtFast(xx);
  2322. axis.y=xy/axis.x;
  2323. axis.z=xz/axis.x;
  2324. }
  2325. }else
  2326. if(yy>=zz)
  2327. {
  2328. if(yy<=EPS)axis.set(SQRT2_2, 0, SQRT2_2);else
  2329. {
  2330. axis.y=SqrtFast(yy);
  2331. axis.x=xy/axis.y;
  2332. axis.z=yz/axis.y;
  2333. }
  2334. }else
  2335. {
  2336. if(zz<=EPS)axis.set(SQRT2_2, SQRT2_2, 0);else
  2337. {
  2338. axis.z=SqrtFast(zz);
  2339. axis.x=xz/axis.z;
  2340. axis.y=yz/axis.z;
  2341. }
  2342. }
  2343. }
  2344. }
  2345. return axis;
  2346. }
  2347. VecD MatrixD3::axis(Bool normalized)C
  2348. {
  2349. MatrixD3 temp;
  2350. C MatrixD3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2351. VecD axis(m->y.z - m->z.y,
  2352. m->z.x - m->x.z,
  2353. m->x.y - m->y.x);
  2354. if(axis.normalize()<=EPSD)
  2355. {
  2356. // singularity
  2357. if(m->x.x>=EPSD_COS)axis.set(1, 0, 0);else // identity
  2358. {
  2359. Dbl xx=(m->x.x+1)*0.5,
  2360. yy=(m->y.y+1)*0.5,
  2361. zz=(m->z.z+1)*0.5,
  2362. xy=(m->x.y+m->y.x)*0.25,
  2363. xz=(m->x.z+m->z.x)*0.25,
  2364. yz=(m->y.z+m->z.y)*0.25;
  2365. if(xx>=yy && xx>=zz)
  2366. {
  2367. if(xx<=EPSD)axis.set(0, SQRT2_2, SQRT2_2);else
  2368. {
  2369. axis.x=SqrtFast(xx);
  2370. axis.y=xy/axis.x;
  2371. axis.z=xz/axis.x;
  2372. }
  2373. }else
  2374. if(yy>=zz)
  2375. {
  2376. if(yy<=EPSD)axis.set(SQRT2_2, 0, SQRT2_2);else
  2377. {
  2378. axis.y=SqrtFast(yy);
  2379. axis.x=xy/axis.y;
  2380. axis.z=yz/axis.y;
  2381. }
  2382. }else
  2383. {
  2384. if(zz<=EPSD)axis.set(SQRT2_2, SQRT2_2, 0);else
  2385. {
  2386. axis.z=SqrtFast(zz);
  2387. axis.x=xz/axis.z;
  2388. axis.y=yz/axis.z;
  2389. }
  2390. }
  2391. }
  2392. }
  2393. return axis;
  2394. }
  2395. Flt Matrix3::angle(Bool normalized)C
  2396. {
  2397. Matrix3 temp;
  2398. C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2399. Vec axis(m->y.z - m->z.y,
  2400. m->z.x - m->x.z,
  2401. m->x.y - m->y.x);
  2402. if(axis.length()> EPS )return Acos((m->x.x + m->y.y + m->z.z - 1)*0.5f);
  2403. if(m->x.x >=EPS_COS)return 0;
  2404. return PI;
  2405. }
  2406. Dbl MatrixD3::angle(Bool normalized)C
  2407. {
  2408. MatrixD3 temp;
  2409. C MatrixD3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2410. VecD axis(m->y.z - m->z.y,
  2411. m->z.x - m->x.z,
  2412. m->x.y - m->y.x);
  2413. if(axis.length()> EPSD )return Acos((m->x.x + m->y.y + m->z.z - 1)*0.5);
  2414. if(m->x.x >=EPSD_COS)return 0;
  2415. return PID;
  2416. }
  2417. Flt Matrix3::angleY(Bool normalized)C
  2418. {
  2419. Matrix3 temp;
  2420. C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2421. Vec axis(m->y.z - m->z.y,
  2422. m->z.x - m->x.z,
  2423. m->x.y - m->y.x);
  2424. if(axis.normalize()> EPS )return axis.y*Acos((m->x.x + m->y.y + m->z.z - 1)*0.5f);
  2425. if(m->x.x >=EPS_COS)return 0; // identity
  2426. Flt xx=m->x.x+1,
  2427. yy=m->y.y+1,
  2428. zz=m->z.z+1;
  2429. if(xx>=yy && xx>=zz)return (xx<=EPS) ? SQRT2_2*PI : (m->x.y+m->y.x)/SqrtFast(xx)*(PI_4*SQRT2);
  2430. if(yy>=zz )return (yy<=EPS) ? 0 : SqrtFast(yy)*(PI /SQRT2);
  2431. return (zz<=EPS) ? SQRT2_2*PI : (m->y.z+m->z.y)/SqrtFast(zz)*(PI_4*SQRT2);
  2432. }
  2433. Flt Matrix3::axisAngle(Vec &axis, Bool normalized)C
  2434. {
  2435. Matrix3 temp;
  2436. C Matrix3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2437. axis.set(m->y.z - m->z.y,
  2438. m->z.x - m->x.z,
  2439. m->x.y - m->y.x);
  2440. Flt angle;
  2441. if(axis.normalize()>EPS)angle=Acos((m->x.x + m->y.y + m->z.z - 1)*0.5f);else
  2442. {
  2443. // singularity
  2444. if(m->x.x>=EPS_COS) // identity
  2445. {
  2446. axis.set(1, 0, 0);
  2447. angle=0;
  2448. }else
  2449. {
  2450. Flt xx=(m->x.x+1)*0.5f,
  2451. yy=(m->y.y+1)*0.5f,
  2452. zz=(m->z.z+1)*0.5f,
  2453. xy=(m->x.y+m->y.x)*0.25f,
  2454. xz=(m->x.z+m->z.x)*0.25f,
  2455. yz=(m->y.z+m->z.y)*0.25f;
  2456. if(xx>=yy && xx>=zz)
  2457. {
  2458. if(xx<=EPS)axis.set(0, SQRT2_2, SQRT2_2);else
  2459. {
  2460. axis.x=SqrtFast(xx);
  2461. axis.y=xy/axis.x;
  2462. axis.z=xz/axis.x;
  2463. }
  2464. }else
  2465. if(yy>=zz)
  2466. {
  2467. if(yy<=EPS)axis.set(SQRT2_2, 0, SQRT2_2);else
  2468. {
  2469. axis.y=SqrtFast(yy);
  2470. axis.x=xy/axis.y;
  2471. axis.z=yz/axis.y;
  2472. }
  2473. }else
  2474. {
  2475. if(zz<=EPS)axis.set(SQRT2_2, SQRT2_2, 0);else
  2476. {
  2477. axis.z=SqrtFast(zz);
  2478. axis.x=xz/axis.z;
  2479. axis.y=yz/axis.z;
  2480. }
  2481. }
  2482. angle=PI;
  2483. }
  2484. }
  2485. return angle;
  2486. }
  2487. Dbl MatrixD3::axisAngle(VecD &axis, Bool normalized)C
  2488. {
  2489. MatrixD3 temp;
  2490. C MatrixD3 *m=this; if(!normalized){temp=T; temp.normalize(); m=&temp;}
  2491. axis.set(m->y.z - m->z.y,
  2492. m->z.x - m->x.z,
  2493. m->x.y - m->y.x);
  2494. Dbl angle;
  2495. if(axis.normalize()>EPSD)angle=Acos((m->x.x + m->y.y + m->z.z - 1)*0.5);else
  2496. {
  2497. // singularity
  2498. if(m->x.x>=EPSD_COS) // identity
  2499. {
  2500. axis.set(1, 0, 0);
  2501. angle=0;
  2502. }else
  2503. {
  2504. Dbl xx=(m->x.x+1)*0.5,
  2505. yy=(m->y.y+1)*0.5,
  2506. zz=(m->z.z+1)*0.5,
  2507. xy=(m->x.y+m->y.x)*0.25,
  2508. xz=(m->x.z+m->z.x)*0.25,
  2509. yz=(m->y.z+m->z.y)*0.25;
  2510. if(xx>=yy && xx>=zz)
  2511. {
  2512. if(xx<=EPSD)axis.set(0, SQRT2_2, SQRT2_2);else
  2513. {
  2514. axis.x=SqrtFast(xx);
  2515. axis.y=xy/axis.x;
  2516. axis.z=xz/axis.x;
  2517. }
  2518. }else
  2519. if(yy>=zz)
  2520. {
  2521. if(yy<=EPSD)axis.set(SQRT2_2, 0, SQRT2_2);else
  2522. {
  2523. axis.y=SqrtFast(yy);
  2524. axis.x=xy/axis.y;
  2525. axis.z=yz/axis.y;
  2526. }
  2527. }else
  2528. {
  2529. if(zz<=EPSD)axis.set(SQRT2_2, SQRT2_2, 0);else
  2530. {
  2531. axis.z=SqrtFast(zz);
  2532. axis.x=xz/axis.z;
  2533. axis.y=yz/axis.z;
  2534. }
  2535. }
  2536. angle=PID;
  2537. }
  2538. }
  2539. return angle;
  2540. }
  2541. Vec Matrix3 ::axisAngle(Bool normalized)C {Vec axis; Flt angle=axisAngle(axis, normalized); return axis*=angle;}
  2542. VecD MatrixD3::axisAngle(Bool normalized)C {VecD axis; Dbl angle=axisAngle(axis, normalized); return axis*=angle;}
  2543. /******************************************************************************/
  2544. Vec2 Matrix3::convert(C Vec &src, Bool normalized)C
  2545. {
  2546. Vec2 ret(Dot(src, x),
  2547. Dot(src, y));
  2548. if(!normalized)
  2549. {
  2550. ret.x/=x.length2();
  2551. ret.y/=y.length2();
  2552. }
  2553. return ret;
  2554. }
  2555. VecD2 MatrixD3::convert(C VecD &src, Bool normalized)C
  2556. {
  2557. VecD2 ret(Dot(src, x),
  2558. Dot(src, y));
  2559. if(!normalized)
  2560. {
  2561. ret.x/=x.length2();
  2562. ret.y/=y.length2();
  2563. }
  2564. return ret;
  2565. }
  2566. Vec Matrix3 ::convert(C Vec2 &src )C {return src.x*x + src.y*y ;}
  2567. VecD MatrixD3::convert(C VecD2 &src )C {return src.x*x + src.y*y ;}
  2568. Vec2 Matrix ::convert(C Vec &src, Bool normalized)C {return super::convert(src -pos, normalized);}
  2569. VecD2 MatrixD ::convert(C VecD &src, Bool normalized)C {return super::convert(src -pos, normalized);}
  2570. Vec Matrix ::convert(C Vec2 &src )C {return super::convert(src)+pos ;}
  2571. VecD MatrixD ::convert(C VecD2 &src )C {return super::convert(src)+pos ;}
  2572. Edge2 Matrix3 ::convert(C Edge &src, Bool normalized)C {return Edge2 (convert(src.p[0], normalized), convert(src.p[1], normalized));}
  2573. EdgeD2 MatrixD3::convert(C EdgeD &src, Bool normalized)C {return EdgeD2(convert(src.p[0], normalized), convert(src.p[1], normalized));}
  2574. Edge Matrix3 ::convert(C Edge2 &src )C {return Edge (convert(src.p[0] ), convert(src.p[1] ));}
  2575. EdgeD MatrixD3::convert(C EdgeD2 &src )C {return EdgeD (convert(src.p[0] ), convert(src.p[1] ));}
  2576. Edge2 Matrix ::convert(C Edge &src, Bool normalized)C {return Edge2 (convert(src.p[0], normalized), convert(src.p[1], normalized));}
  2577. EdgeD2 MatrixD ::convert(C EdgeD &src, Bool normalized)C {return EdgeD2(convert(src.p[0], normalized), convert(src.p[1], normalized));}
  2578. Edge Matrix ::convert(C Edge2 &src )C {return Edge (convert(src.p[0] ), convert(src.p[1] ));}
  2579. EdgeD MatrixD ::convert(C EdgeD2 &src )C {return EdgeD (convert(src.p[0] ), convert(src.p[1] ));}
  2580. /******************************************************************************/
  2581. Matrix& Matrix::anchor(C Vec &anchor)
  2582. {
  2583. Flt x=anchor.x, y=anchor.y, z=anchor.z;
  2584. pos.set(x - x*T.x.x - y*T.y.x - z*T.z.x,
  2585. y - x*T.x.y - y*T.y.y - z*T.z.y,
  2586. z - x*T.x.z - y*T.y.z - z*T.z.z);
  2587. return T;
  2588. }
  2589. MatrixM& MatrixM::anchor(C VecD &anchor)
  2590. {
  2591. Dbl x=anchor.x, y=anchor.y, z=anchor.z;
  2592. pos.set(x - x*T.x.x - y*T.y.x - z*T.z.x,
  2593. y - x*T.x.y - y*T.y.y - z*T.z.y,
  2594. z - x*T.x.z - y*T.y.z - z*T.z.z);
  2595. return T;
  2596. }
  2597. MatrixD& MatrixD::anchor(C VecD &anchor)
  2598. {
  2599. Dbl x=anchor.x, y=anchor.y, z=anchor.z;
  2600. pos.set(x - x*T.x.x - y*T.y.x - z*T.z.x,
  2601. y - x*T.x.y - y*T.y.y - z*T.z.y,
  2602. z - x*T.x.z - y*T.y.z - z*T.z.z);
  2603. return T;
  2604. }
  2605. Matrix& Matrix::setTransformAtPos(C Vec &pos, C Matrix3 &matrix) {orn()=matrix; anchor(pos); return T;}
  2606. Matrix& Matrix::setTransformAtPos(C Vec &pos, C Matrix &matrix) {orn()=matrix; anchor(pos); move(matrix.pos); return T;}
  2607. Matrix& Matrix:: transformAtPos(C Vec &pos, C Matrix3 &matrix) {return moveBack(pos).mul(matrix).move(pos);}
  2608. Matrix& Matrix:: transformAtPos(C Vec &pos, C Matrix &matrix) {return moveBack(pos).mul(matrix).move(pos);}
  2609. MatrixD& MatrixD::setTransformAtPos(C VecD &pos, C MatrixD3 &matrix) {orn()=matrix; anchor(pos); return T;}
  2610. MatrixD& MatrixD::setTransformAtPos(C VecD &pos, C MatrixD &matrix) {orn()=matrix; anchor(pos); move(matrix.pos); return T;}
  2611. MatrixD& MatrixD:: transformAtPos(C VecD &pos, C MatrixD3 &matrix) {return moveBack(pos).mul(matrix).move(pos);}
  2612. MatrixD& MatrixD:: transformAtPos(C VecD &pos, C MatrixD &matrix) {return moveBack(pos).mul(matrix).move(pos);}
  2613. /******************************************************************************/
  2614. Matrix3::Matrix3(C Matrix &matrix)
  2615. {
  2616. x=matrix.x;
  2617. y=matrix.y;
  2618. z=matrix.z;
  2619. }
  2620. Matrix3::Matrix3(C MatrixD3 &matrix)
  2621. {
  2622. x=matrix.x;
  2623. y=matrix.y;
  2624. z=matrix.z;
  2625. }
  2626. Matrix3::Matrix3(C Matrix4 &matrix)
  2627. {
  2628. x=matrix.x.xyz;
  2629. y=matrix.y.xyz;
  2630. z=matrix.z.xyz;
  2631. }
  2632. Matrix3::Matrix3(C Orient &orient)
  2633. {
  2634. x=orient.cross();
  2635. y=orient.perp;
  2636. z=orient.dir;
  2637. }
  2638. /******************************************************************************/
  2639. MatrixD3::MatrixD3(C Matrix3 &matrix)
  2640. {
  2641. x=matrix.x;
  2642. y=matrix.y;
  2643. z=matrix.z;
  2644. }
  2645. MatrixD3::MatrixD3(C MatrixD &matrix)
  2646. {
  2647. x=matrix.x;
  2648. y=matrix.y;
  2649. z=matrix.z;
  2650. }
  2651. MatrixD3::MatrixD3(C Orient &orient) : MatrixD3(OrientD(orient)) {}
  2652. MatrixD3::MatrixD3(C OrientD &orient)
  2653. {
  2654. x=orient.cross();
  2655. y=orient.perp;
  2656. z=orient.dir;
  2657. }
  2658. /******************************************************************************/
  2659. Matrix::Matrix(C Matrix3 &matrix)
  2660. {
  2661. x=matrix.x;
  2662. y=matrix.y;
  2663. z=matrix.z;
  2664. pos.zero();
  2665. }
  2666. Matrix::Matrix(C MatrixD3 &matrix)
  2667. {
  2668. x=matrix.x;
  2669. y=matrix.y;
  2670. z=matrix.z;
  2671. pos.zero();
  2672. }
  2673. Matrix::Matrix(C MatrixM &matrix)
  2674. {
  2675. x =matrix.x ;
  2676. y =matrix.y ;
  2677. z =matrix.z ;
  2678. pos=matrix.pos;
  2679. }
  2680. Matrix::Matrix(C MatrixD &matrix)
  2681. {
  2682. x =matrix.x ;
  2683. y =matrix.y ;
  2684. z =matrix.z ;
  2685. pos=matrix.pos;
  2686. }
  2687. Matrix::Matrix(C Matrix4 &matrix)
  2688. {
  2689. x =matrix.x .xyz;
  2690. y =matrix.y .xyz;
  2691. z =matrix.z .xyz;
  2692. pos=matrix.pos.xyz;
  2693. }
  2694. Matrix::Matrix(C OrientP &orient)
  2695. {
  2696. x =orient.cross();
  2697. y =orient.perp;
  2698. z =orient.dir;
  2699. pos=orient.pos;
  2700. }
  2701. MatrixM::MatrixM(C Matrix3 &matrix)
  2702. {
  2703. x=matrix.x;
  2704. y=matrix.y;
  2705. z=matrix.z;
  2706. pos.zero();
  2707. }
  2708. MatrixM::MatrixM(C Matrix &matrix)
  2709. {
  2710. x =matrix.x ;
  2711. y =matrix.y ;
  2712. z =matrix.z ;
  2713. pos=matrix.pos;
  2714. }
  2715. MatrixM::MatrixM(C OrientM &orient)
  2716. {
  2717. x =orient.cross();
  2718. y =orient.perp;
  2719. z =orient.dir;
  2720. pos=orient.pos;
  2721. }
  2722. /******************************************************************************/
  2723. MatrixD::MatrixD(C MatrixD3 &matrix)
  2724. {
  2725. x=matrix.x;
  2726. y=matrix.y;
  2727. z=matrix.z;
  2728. pos.zero();
  2729. }
  2730. MatrixD::MatrixD(C Matrix &matrix)
  2731. {
  2732. x =matrix.x ;
  2733. y =matrix.y ;
  2734. z =matrix.z ;
  2735. pos=matrix.pos;
  2736. }
  2737. MatrixD::MatrixD(C OrientP &o) : MatrixD3(o)
  2738. {
  2739. pos=o.pos;
  2740. }
  2741. /******************************************************************************/
  2742. Matrix4::Matrix4(C Matrix3 &matrix)
  2743. {
  2744. x .set(matrix.x, 0);
  2745. y .set(matrix.y, 0);
  2746. z .set(matrix.z, 0);
  2747. pos.set(0, 0, 0, 1);
  2748. }
  2749. Matrix4::Matrix4(C Matrix &matrix)
  2750. {
  2751. x .set(matrix.x , 0);
  2752. y .set(matrix.y , 0);
  2753. z .set(matrix.z , 0);
  2754. pos.set(matrix.pos, 1);
  2755. }
  2756. /******************************************************************************/
  2757. Flt Matrix3::determinant()C
  2758. {
  2759. // return Dot(x, Cross(y, z));
  2760. // return x.x*y.y*z.z + x.y*y.z*z.x + x.z*y.x*z.y
  2761. // - x.x*y.z*z.y - x.y*y.x*z.z - x.z*y.y*z.x;
  2762. return x.x*(y.y*z.z - y.z*z.y)
  2763. + x.y*(y.z*z.x - y.x*z.z)
  2764. + x.z*(y.x*z.y - y.y*z.x);
  2765. }
  2766. Dbl MatrixD3::determinant()C
  2767. {
  2768. // return Dot(x, Cross(y, z));
  2769. // return x.x*y.y*z.z + x.y*y.z*z.x + x.z*y.x*z.y
  2770. // - x.x*y.z*z.y - x.y*y.x*z.z - x.z*y.y*z.x;
  2771. return x.x*(y.y*z.z - y.z*z.y)
  2772. + x.y*(y.z*z.x - y.x*z.z)
  2773. + x.z*(y.x*z.y - y.y*z.x);
  2774. }
  2775. Flt Matrix4::determinant()C
  2776. {
  2777. return
  2778. 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
  2779. +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
  2780. +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
  2781. +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
  2782. +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
  2783. +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;
  2784. }
  2785. /******************************************************************************/
  2786. void Matrix3::draw(C Vec &pos, C Color &x_color, C Color &y_color, C Color &z_color, Bool arrow)C
  2787. {
  2788. if(arrow)
  2789. {
  2790. DrawArrow(x_color, pos, pos+x);
  2791. DrawArrow(y_color, pos, pos+y);
  2792. DrawArrow(z_color, pos, pos+z);
  2793. }else
  2794. {
  2795. VI.line(x_color, pos, pos+x);
  2796. VI.line(y_color, pos, pos+y);
  2797. VI.line(z_color, pos, pos+z);
  2798. VI.end ();
  2799. }
  2800. }
  2801. void MatrixD3::draw(C VecD &pos, C Color &x_color, C Color &y_color, C Color &z_color, Bool arrow)C
  2802. {
  2803. if(arrow)
  2804. {
  2805. DrawArrow(x_color, pos, pos+x);
  2806. DrawArrow(y_color, pos, pos+y);
  2807. DrawArrow(z_color, pos, pos+z);
  2808. }else
  2809. {
  2810. VI.line(x_color, pos, pos+x);
  2811. VI.line(y_color, pos, pos+y);
  2812. VI.line(z_color, pos, pos+z);
  2813. VI.end ();
  2814. }
  2815. }
  2816. /******************************************************************************/
  2817. // GPU MATRIX
  2818. /******************************************************************************/
  2819. GpuMatrix::GpuMatrix(C Matrix &m)
  2820. {
  2821. T.xx=m.x.x; T.yx=m.y.x; T.zx=m.z.x; T._x=m.pos.x;
  2822. T.xy=m.x.y; T.yy=m.y.y; T.zy=m.z.y; T._y=m.pos.y;
  2823. T.xz=m.x.z; T.yz=m.y.z; T.zz=m.z.z; T._z=m.pos.z;
  2824. }
  2825. GpuMatrix::GpuMatrix(C MatrixM &m)
  2826. {
  2827. T.xx=m.x.x; T.yx=m.y.x; T.zx=m.z.x; T._x=m.pos.x;
  2828. T.xy=m.x.y; T.yy=m.y.y; T.zy=m.z.y; T._y=m.pos.y;
  2829. T.xz=m.x.z; T.yz=m.y.z; T.zz=m.z.z; T._z=m.pos.z;
  2830. }
  2831. /******************************************************************************/
  2832. GpuMatrix& GpuMatrix::fromMul(C Matrix &a, C Matrix &b)
  2833. {
  2834. Flt x, y, z;
  2835. x=b.x.x; y=b.y.x; z=b.z.x;
  2836. T.xx=x*a.x.x + y*a.x.y + z*a.x.z;
  2837. T.yx=x*a.y.x + y*a.y.y + z*a.y.z;
  2838. T.zx=x*a.z.x + y*a.z.y + z*a.z.z;
  2839. x=b.x.y; y=b.y.y; z=b.z.y;
  2840. T.xy=x*a.x.x + y*a.x.y + z*a.x.z;
  2841. T.yy=x*a.y.x + y*a.y.y + z*a.y.z;
  2842. T.zy=x*a.z.x + y*a.z.y + z*a.z.z;
  2843. x=b.x.z; y=b.y.z; z=b.z.z;
  2844. T.xz=x*a.x.x + y*a.x.y + z*a.x.z;
  2845. T.yz=x*a.y.x + y*a.y.y + z*a.y.z;
  2846. T.zz=x*a.z.x + y*a.z.y + z*a.z.z;
  2847. x=a.pos.x; y=a.pos.y; z=a.pos.z;
  2848. T._x=x*b.x.x + y*b.y.x + z*b.z.x + b.pos.x;
  2849. T._y=x*b.x.y + y*b.y.y + z*b.z.y + b.pos.y;
  2850. T._z=x*b.x.z + y*b.y.z + z*b.z.z + b.pos.z;
  2851. return T;
  2852. }
  2853. GpuMatrix& GpuMatrix::fromMul(C Matrix &a, C MatrixM &b)
  2854. {
  2855. Flt x, y, z;
  2856. x=b.x.x; y=b.y.x; z=b.z.x;
  2857. T.xx=x*a.x.x + y*a.x.y + z*a.x.z;
  2858. T.yx=x*a.y.x + y*a.y.y + z*a.y.z;
  2859. T.zx=x*a.z.x + y*a.z.y + z*a.z.z;
  2860. x=b.x.y; y=b.y.y; z=b.z.y;
  2861. T.xy=x*a.x.x + y*a.x.y + z*a.x.z;
  2862. T.yy=x*a.y.x + y*a.y.y + z*a.y.z;
  2863. T.zy=x*a.z.x + y*a.z.y + z*a.z.z;
  2864. x=b.x.z; y=b.y.z; z=b.z.z;
  2865. T.xz=x*a.x.x + y*a.x.y + z*a.x.z;
  2866. T.yz=x*a.y.x + y*a.y.y + z*a.y.z;
  2867. T.zz=x*a.z.x + y*a.z.y + z*a.z.z;
  2868. x=a.pos.x; y=a.pos.y; z=a.pos.z;
  2869. T._x=x*b.x.x + y*b.y.x + z*b.z.x + b.pos.x;
  2870. T._y=x*b.x.y + y*b.y.y + z*b.z.y + b.pos.y;
  2871. T._z=x*b.x.z + y*b.y.z + z*b.z.z + b.pos.z;
  2872. return T;
  2873. }
  2874. GpuMatrix& GpuMatrix::fromMul(C MatrixM &a, C MatrixM &b)
  2875. {
  2876. {
  2877. Flt x, y, z;
  2878. x=b.x.x; y=b.y.x; z=b.z.x;
  2879. T.xx=x*a.x.x + y*a.x.y + z*a.x.z;
  2880. T.yx=x*a.y.x + y*a.y.y + z*a.y.z;
  2881. T.zx=x*a.z.x + y*a.z.y + z*a.z.z;
  2882. x=b.x.y; y=b.y.y; z=b.z.y;
  2883. T.xy=x*a.x.x + y*a.x.y + z*a.x.z;
  2884. T.yy=x*a.y.x + y*a.y.y + z*a.y.z;
  2885. T.zy=x*a.z.x + y*a.z.y + z*a.z.z;
  2886. x=b.x.z; y=b.y.z; z=b.z.z;
  2887. T.xz=x*a.x.x + y*a.x.y + z*a.x.z;
  2888. T.yz=x*a.y.x + y*a.y.y + z*a.y.z;
  2889. T.zz=x*a.z.x + y*a.z.y + z*a.z.z;
  2890. }
  2891. {
  2892. Dbl x, y, z;
  2893. x=a.pos.x; y=a.pos.y; z=a.pos.z;
  2894. T._x=x*b.x.x + y*b.y.x + z*b.z.x + b.pos.x;
  2895. T._y=x*b.x.y + y*b.y.y + z*b.z.y + b.pos.y;
  2896. T._z=x*b.x.z + y*b.y.z + z*b.z.z + b.pos.z;
  2897. }
  2898. return T;
  2899. }
  2900. /******************************************************************************/
  2901. // MAIN
  2902. /******************************************************************************/
  2903. 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);}
  2904. 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);}
  2905. 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);}
  2906. 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);}
  2907. 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);}
  2908. 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);}
  2909. /******************************************************************************/
  2910. void GetTransform(Matrix3 &transform, C Orient &start, C Orient &result)
  2911. {
  2912. #if 0 // unoptimized
  2913. start.inverse(transform); transform.mul(Matrix3(result));
  2914. #else // optimized
  2915. Vec start_cross= start.cross(),
  2916. result_cross=result.cross();
  2917. // this inverses 'start' into T
  2918. #define T_x_x start_cross.x
  2919. #define T_x_y start.perp .x
  2920. #define T_x_z start.dir .x
  2921. #define T_y_x start_cross.y
  2922. #define T_y_y start.perp .y
  2923. #define T_y_z start.dir .y
  2924. #define T_z_x start_cross.z
  2925. #define T_z_y start.perp .z
  2926. #define T_z_z start.dir .z
  2927. #define m_x result_cross
  2928. #define m_y result.perp
  2929. #define m_z result.dir
  2930. // following code is from 'Matrix3.mul'
  2931. Flt x, y, z;
  2932. x=T_y_x; y=T_y_y; z=T_y_z;
  2933. transform.y.x=x*m_x.x + y*m_y.x + z*m_z.x;
  2934. transform.y.y=x*m_x.y + y*m_y.y + z*m_z.y;
  2935. transform.y.z=x*m_x.z + y*m_y.z + z*m_z.z;
  2936. x=T_z_x; y=T_z_y; z=T_z_z;
  2937. transform.z.x=x*m_x.x + y*m_y.x + z*m_z.x;
  2938. transform.z.y=x*m_x.y + y*m_y.y + z*m_z.y;
  2939. transform.z.z=x*m_x.z + y*m_y.z + z*m_z.z;
  2940. #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
  2941. x=T_x_x; y=T_x_y; z=T_x_z;
  2942. transform.x.x=x*m_x.x + y*m_y.x + z*m_z.x;
  2943. transform.x.y=x*m_x.y + y*m_y.y + z*m_z.y;
  2944. transform.x.z=x*m_x.z + y*m_y.z + z*m_z.z;
  2945. #else
  2946. //transform.x=Cross(transform.y, transform.z);
  2947. transform.x.x=transform.y.y*transform.z.z - transform.y.z*transform.z.y;
  2948. transform.x.y=transform.y.z*transform.z.x - transform.y.x*transform.z.z;
  2949. transform.x.z=transform.y.x*transform.z.y - transform.y.y*transform.z.x;
  2950. #endif
  2951. #undef T_x_x
  2952. #undef T_x_y
  2953. #undef T_x_z
  2954. #undef T_y_x
  2955. #undef T_y_y
  2956. #undef T_y_z
  2957. #undef T_z_x
  2958. #undef T_z_y
  2959. #undef T_z_z
  2960. #undef m_x
  2961. #undef m_y
  2962. #undef m_z
  2963. #endif
  2964. }
  2965. 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"
  2966. 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"
  2967. 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"
  2968. 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"
  2969. Matrix3 GetTransform( C Matrix3 &start, C Matrix3 &result) {Matrix3 transform; start.inverse(transform); transform*=result; return transform;}
  2970. MatrixD3 GetTransform( C MatrixD3 &start, C MatrixD3 &result) {MatrixD3 transform; start.inverse(transform); transform*=result; return transform;}
  2971. Matrix GetTransform( C Matrix &start, C Matrix &result) {Matrix transform; start.inverse(transform); transform*=result; return transform;}
  2972. MatrixD GetTransform( C MatrixD &start, C MatrixD &result) {MatrixD transform; start.inverse(transform); transform*=result; return transform;}
  2973. Matrix3 GetTransform( C Orient &start, C Orient &result) {Matrix3 transform; GetTransform(transform, start, result); return transform;}
  2974. /******************************************************************************/
  2975. static inline Vec GetRotation(C Vec &from, C Vec &to)
  2976. {
  2977. Vec cross=Cross(from, to); if(Flt sin=cross.normalize())return cross*ACosSin(Dot(from, to), sin);
  2978. return VecZero;
  2979. }
  2980. static inline VecD GetRotation(C VecD &from, C VecD &to)
  2981. {
  2982. VecD cross=Cross(from, to); if(Dbl sin=cross.normalize())return cross*ACosSin(Dot(from, to), sin);
  2983. return VecZero;
  2984. }
  2985. /******************************************************************************/
  2986. void GetDelta(Vec &pos, Vec &angle, C Matrix &from, C Matrix &to)
  2987. {
  2988. // pos
  2989. pos=to.pos-from.pos;
  2990. // angle
  2991. Vec cross=Cross(from.z, to.z); if(Flt sin=cross.normalize())
  2992. {
  2993. Flt cos=Dot(from.z, to.z);
  2994. angle=cross*ACosSin(cos, sin)
  2995. +GetRotation(from.y*Matrix3().setRotateCosSin(cross, cos, sin), to.y);
  2996. }else angle=GetRotation(from.y , to.y);
  2997. }
  2998. void GetDelta(Vec &pos, Vec &angle, C MatrixM &from, C MatrixM &to)
  2999. {
  3000. // pos
  3001. pos=to.pos-from.pos;
  3002. // angle
  3003. Vec cross=Cross(from.z, to.z); if(Flt sin=cross.normalize())
  3004. {
  3005. Flt cos=Dot(from.z, to.z);
  3006. angle=cross*ACosSin(cos, sin)
  3007. +GetRotation(from.y*Matrix3().setRotateCosSin(cross, cos, sin), to.y);
  3008. }else angle=GetRotation(from.y , to.y);
  3009. }
  3010. void GetDelta(VecD &pos, VecD &angle, C MatrixD &from, C MatrixD &to)
  3011. {
  3012. // pos
  3013. pos=to.pos-from.pos;
  3014. // angle
  3015. VecD cross=Cross(from.z, to.z); if(Dbl sin=cross.normalize())
  3016. {
  3017. Dbl cos=Dot(from.z, to.z);
  3018. angle=cross*ACosSin(cos, sin)
  3019. +GetRotation(from.y*MatrixD3().setRotateCosSin(cross, cos, sin), to.y);
  3020. }else angle=GetRotation(from.y , to.y);
  3021. }
  3022. /******************************************************************************/
  3023. void GetVel(Vec &vel, Vec &ang_vel, C Matrix &from, C Matrix &to, Flt dt)
  3024. {
  3025. if(dt>EPS)
  3026. {
  3027. GetDelta(vel, ang_vel, from, to);
  3028. vel/=dt;
  3029. ang_vel/=dt;
  3030. }else
  3031. {
  3032. vel.zero();
  3033. ang_vel.zero();
  3034. }
  3035. }
  3036. void GetVel(Vec &vel, Vec &ang_vel, C MatrixM &from, C MatrixM &to, Flt dt)
  3037. {
  3038. if(dt>EPS)
  3039. {
  3040. GetDelta(vel, ang_vel, from, to);
  3041. vel/=dt;
  3042. ang_vel/=dt;
  3043. }else
  3044. {
  3045. vel.zero();
  3046. ang_vel.zero();
  3047. }
  3048. }
  3049. void GetVel(VecD &vel, VecD &ang_vel, C MatrixD &from, C MatrixD &to, Dbl dt)
  3050. {
  3051. if(dt>EPSD)
  3052. {
  3053. GetDelta(vel, ang_vel, from, to);
  3054. vel/=dt;
  3055. ang_vel/=dt;
  3056. }else
  3057. {
  3058. vel.zero();
  3059. ang_vel.zero();
  3060. }
  3061. }
  3062. /******************************************************************************/
  3063. // SHADER
  3064. /******************************************************************************/
  3065. void AnimatedSkeleton::setMatrix()C
  3066. {
  3067. 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
  3068. SetMatrixCount(VIRTUAL_ROOT_BONE+matrixes); // root + bones
  3069. ObjMatrix=matrix(); // 'Mesh.drawBlend' makes use of the 'ObjMatrix' so it must be set
  3070. if(Renderer._mesh_shader_vel) // we need to process velocities
  3071. {
  3072. Vec v; if(VIRTUAL_ROOT_BONE){v=vel()-ActiveCam.vel; v*=CamMatrixInvMotionScale;}
  3073. if(!D.meshBoneSplit() || matrixes<=MAX_MATRIX_HWMIN-VIRTUAL_ROOT_BONE) // bone matrixes + VIRTUAL_ROOT_BONE <= MAX_MATRIX_HWMIN
  3074. {
  3075. if(VIRTUAL_ROOT_BONE)
  3076. {
  3077. Sh.h_ViewMatrix->fromMul(matrix(), CamMatrixInv);
  3078. Sh.h_ObjVel ->set (v );
  3079. }
  3080. REP(matrixes)
  3081. {
  3082. C AnimSkelBone &bone=bones[i];
  3083. v=bone._vel-ActiveCam.vel; v*=CamMatrixInvMotionScale;
  3084. Sh.h_ViewMatrix->fromMul(bone._matrix, CamMatrixInv, VIRTUAL_ROOT_BONE+i);
  3085. Sh.h_ObjVel ->set (v , VIRTUAL_ROOT_BONE+i);
  3086. }
  3087. }else
  3088. {
  3089. #if MAY_NEED_BONE_SPLITS
  3090. if(VIRTUAL_ROOT_BONE)
  3091. {
  3092. Sh.h_ViewMatrix->set(GObjMatrix[0].fromMul(matrix(), CamMatrixInv));
  3093. Sh.h_ObjVel ->set(GObjVel [0]=v );
  3094. }
  3095. REP(matrixes)
  3096. {
  3097. C AnimSkelBone &bone=bones[i];
  3098. v=bone._vel-ActiveCam.vel; v*=CamMatrixInvMotionScale;
  3099. Sh.h_ViewMatrix->set(GObjMatrix[VIRTUAL_ROOT_BONE+i].fromMul(bone._matrix, CamMatrixInv), VIRTUAL_ROOT_BONE+i);
  3100. Sh.h_ObjVel ->set(GObjVel [VIRTUAL_ROOT_BONE+i]=v , VIRTUAL_ROOT_BONE+i);
  3101. }
  3102. #endif
  3103. }
  3104. 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)
  3105. }else
  3106. {
  3107. if(!D.meshBoneSplit() || matrixes<=MAX_MATRIX_HWMIN-VIRTUAL_ROOT_BONE) // bone matrixes + VIRTUAL_ROOT_BONE <= MAX_MATRIX_HWMIN
  3108. {
  3109. if(VIRTUAL_ROOT_BONE)Sh.h_ViewMatrix->fromMul( matrix(), CamMatrixInv);
  3110. REP(matrixes)Sh.h_ViewMatrix->fromMul(bones[i]._matrix , CamMatrixInv, VIRTUAL_ROOT_BONE+i);
  3111. }else
  3112. {
  3113. #if MAY_NEED_BONE_SPLITS
  3114. if(VIRTUAL_ROOT_BONE)Sh.h_ViewMatrix->set(GObjMatrix[0 ].fromMul( matrix(), CamMatrixInv));
  3115. REP(matrixes)Sh.h_ViewMatrix->set(GObjMatrix[VIRTUAL_ROOT_BONE+i].fromMul(bones[i]._matrix , CamMatrixInv), VIRTUAL_ROOT_BONE+i);
  3116. #endif
  3117. }
  3118. }
  3119. }
  3120. /******************************************************************************/
  3121. void SetVelFur(C Matrix3 &view_matrix, C Vec &vel)
  3122. {
  3123. Vec v=vel*D.furStaticVelScale(); v.y+=D.furStaticGravity();
  3124. v.clipLength(0.92f);
  3125. #if DEBUG && 0
  3126. 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
  3127. #endif
  3128. v*=CamMatrixInv.orn(); // below we should divide by 'object_matrix', however we only have 'view_matrix', applying this transformation will provide the same result
  3129. 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'
  3130. Sh.h_FurVel->set(v);
  3131. }
  3132. 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'
  3133. void AnimatedSkeleton::setFurVel()C
  3134. {
  3135. 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
  3136. SetFurVelCount(VIRTUAL_ROOT_BONE+matrixes); // root + bones
  3137. if(!D.meshBoneSplit() || matrixes<=MAX_MATRIX_HWMIN-VIRTUAL_ROOT_BONE) // bone matrixes + VIRTUAL_ROOT_BONE <= MAX_MATRIX_HWMIN
  3138. {
  3139. if(VIRTUAL_ROOT_BONE)Sh.h_FurVel->set(FurVelShader( root._fur_vel, matrix()));
  3140. REP(matrixes)Sh.h_FurVel->set(FurVelShader(bones[i]._fur_vel, bones[i]._matrix ), VIRTUAL_ROOT_BONE+i);
  3141. }else
  3142. {
  3143. #if MAY_NEED_BONE_SPLITS
  3144. if(VIRTUAL_ROOT_BONE)Sh.h_FurVel->set(GFurVel[0 ]=FurVelShader( root._fur_vel, matrix()));
  3145. REP(matrixes)Sh.h_FurVel->set(GFurVel[VIRTUAL_ROOT_BONE+i]=FurVelShader(bones[i]._fur_vel, bones[i]._matrix ), VIRTUAL_ROOT_BONE+i);
  3146. #endif
  3147. }
  3148. }
  3149. /******************************************************************************/
  3150. #if MAY_NEED_BONE_SPLITS
  3151. void SetMatrixVelRestore() // this function restores full set of matrixes/velocities to GPU
  3152. {
  3153. Int m=Min(Matrixes, MAX_MATRIX_HWMIN);
  3154. Sh.h_ViewMatrix->set(GObjMatrix, m); // matrixes
  3155. if(Renderer._mesh_shader_vel)Sh.h_ObjVel ->set(GObjVel , m); // velocities
  3156. }
  3157. void SetMatrixVelSplit(Byte *matrix, Int matrixes) // this function sets matrixes/velocities to GPU for a particular BoneSplit
  3158. {
  3159. REP(matrixes)Sh.h_ViewMatrix->set(GObjMatrix[matrix[i]], i); // matrixes
  3160. if(Renderer._mesh_shader_vel)REP(matrixes)Sh.h_ObjVel ->set(GObjVel [matrix[i]], i); // velocities
  3161. }
  3162. // FUR
  3163. void SetMatrixFurVelRestore() // this function restores full set of matrixes/velocities to GPU
  3164. {
  3165. Int m=Min(Matrixes, MAX_MATRIX_HWMIN);
  3166. Sh.h_ViewMatrix->set(GObjMatrix, m); // matrixes
  3167. Sh.h_FurVel ->set(GFurVel , m); // velocities
  3168. }
  3169. void SetMatrixFurVelSplit(Byte *matrix, Int matrixes) // this function sets matrixes/velocities to GPU for a particular BoneSplit
  3170. {
  3171. REP(matrixes)
  3172. {
  3173. Byte m=matrix[i];
  3174. Sh.h_ViewMatrix->set(GObjMatrix[m], i); // matrixes
  3175. Sh.h_FurVel ->set(GFurVel [m], i); // velocities
  3176. }
  3177. }
  3178. #endif
  3179. /******************************************************************************/
  3180. void SetFastViewMatrix( C Matrix &view_matrix ) {Sh.h_ViewMatrix->set ( view_matrix );}
  3181. void SetFastMatrix ( ) {Sh.h_ViewMatrix->set ( CamMatrixInv );}
  3182. void SetFastMatrix ( C Matrix & matrix ) {Sh.h_ViewMatrix->fromMul ( matrix, CamMatrixInv );}
  3183. void SetFastMatrix ( C MatrixM & matrix ) {Sh.h_ViewMatrix->fromMul ( matrix, CamMatrixInv );}
  3184. void SetFastVel ( ) {Sh.h_ObjVel ->setConditional(( -ActiveCam.vel)*=CamMatrixInvMotionScale );}
  3185. void SetFastVel ( C Vec & vel ) {Sh.h_ObjVel ->setConditional((vel-ActiveCam.vel)*=CamMatrixInvMotionScale );}
  3186. void SetFastVel (Byte i, C Vec & vel ) {Sh.h_ObjVel ->setConditional((vel-ActiveCam.vel)*=CamMatrixInvMotionScale, i);}
  3187. void SetFastAngVel ( C Vec &ang_vel_shader) {Sh.h_ObjAngVel ->setConditional( ang_vel_shader );} // !! 'ang_vel_shader' must come from 'SetAngVelShader' !!
  3188. void SetFastAngVel ( ) {Sh.h_ObjAngVel ->setConditional( VecZero);}
  3189. /******************************************************************************/
  3190. // To be used for drawing without any velocities
  3191. void SetOneMatrix()
  3192. {
  3193. SetMatrixCount();
  3194. SetFastMatrix ();
  3195. }
  3196. void SetOneMatrix(C Matrix &matrix)
  3197. {
  3198. SetMatrixCount();
  3199. SetFastMatrix (matrix);
  3200. }
  3201. void SetOneMatrix(C MatrixM &matrix)
  3202. {
  3203. SetMatrixCount();
  3204. SetFastMatrix (matrix);
  3205. }
  3206. /******************************************************************************/
  3207. // To be used by the user
  3208. void SetMatrix(C Matrix &matrix)
  3209. {
  3210. ObjMatrix=matrix;
  3211. SetMatrixCount();
  3212. SetFastMatrix (matrix);
  3213. SetFastVel ();
  3214. SetFastAngVel ();
  3215. }
  3216. void SetMatrix(C MatrixM &matrix)
  3217. {
  3218. ObjMatrix=matrix;
  3219. SetMatrixCount();
  3220. SetFastMatrix (matrix);
  3221. SetFastVel ();
  3222. SetFastAngVel ();
  3223. }
  3224. void SetMatrix(C Matrix &matrix, C Vec &vel, C Vec &ang_vel)
  3225. {
  3226. Vec ang_vel_shader; SetAngVelShader(ang_vel_shader, ang_vel, matrix);
  3227. ObjMatrix=matrix;
  3228. SetMatrixCount();
  3229. SetFastMatrix (matrix);
  3230. SetFastVel (vel);
  3231. SetFastAngVel (ang_vel_shader);
  3232. }
  3233. void SetMatrix(C MatrixM &matrix, C Vec &vel, C Vec &ang_vel)
  3234. {
  3235. Vec ang_vel_shader; SetAngVelShader(ang_vel_shader, ang_vel, matrix);
  3236. ObjMatrix=matrix;
  3237. SetMatrixCount();
  3238. SetFastMatrix (matrix);
  3239. SetFastVel (vel);
  3240. SetFastAngVel (ang_vel_shader);
  3241. }
  3242. /******************************************************************************/
  3243. void SetProjMatrix() // this needs to be additionally called when changing 'PixelOffset' on DX9, or switching between '_main' and some other RT on OpenGL
  3244. {
  3245. if(Sh.h_ProjMatrix)
  3246. {
  3247. #if DX9 // in DirectX 9 adjust projection 2D offset to match DirectX 10+ and OpenGL
  3248. Matrix4 m=ProjMatrix;
  3249. m.x .x+=m.x .w*PixelOffset.x;
  3250. m.y .x+=m.y .w*PixelOffset.x;
  3251. m.z .x+=m.z .w*PixelOffset.x;
  3252. m.pos.x+=m.pos.w*PixelOffset.x;
  3253. m.x .y+=m.x .w*PixelOffset.y;
  3254. m.y .y+=m.y .w*PixelOffset.y;
  3255. m.z .y+=m.z .w*PixelOffset.y;
  3256. m.pos.y+=m.pos.w*PixelOffset.y;
  3257. Sh.h_ProjMatrix->set(m);
  3258. #elif GL
  3259. if(D.mainFBO())Sh.h_ProjMatrix->set(ProjMatrix);else
  3260. {
  3261. 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
  3262. }
  3263. #else
  3264. Sh.h_ProjMatrix->set(ProjMatrix);
  3265. #endif
  3266. }
  3267. }
  3268. void SetProjMatrix(Flt proj_offset)
  3269. {
  3270. //if(Sh.h_ProjMatrix) this is called always when display is already created
  3271. {
  3272. Matrix4 m=ProjMatrix;
  3273. #if DX9 // in DirectX 9 adjust projection matrix 2D offset to match DirectX 10+ and OpenGL
  3274. Flt o=PixelOffset.x+proj_offset;
  3275. m.x .x+=m.x .w*o;
  3276. m.y .x+=m.y .w*o;
  3277. m.z .x+=m.z .w*o;
  3278. m.pos.x+=m.pos.w*o;
  3279. m.x .y+=m.x .w*PixelOffset.y;
  3280. m.y .y+=m.y .w*PixelOffset.y;
  3281. m.z .y+=m.z .w*PixelOffset.y;
  3282. m.pos.y+=m.pos.w*PixelOffset.y;
  3283. #else
  3284. m.x .x+=m.x .w*proj_offset;
  3285. m.y .x+=m.y .w*proj_offset;
  3286. m.z .x+=m.z .w*proj_offset;
  3287. m.pos.x+=m.pos.w*proj_offset;
  3288. #endif
  3289. //Flt cam_offset; m.pos.x+=m.x.x*cam_offset; // this matches "m=Matrix().setPos(cam_offset, 0, 0)*m"
  3290. #if GL
  3291. if(!D.mainFBO())CHS(m.y.y); // in OpenGL when drawing to a RenderTarget the 'dest.pos.y' must be flipped
  3292. #endif
  3293. Sh.h_ProjMatrix->set(m);
  3294. }
  3295. }
  3296. /******************************************************************************/
  3297. // LOD
  3298. /******************************************************************************/
  3299. Flt GetLodDist2(C Vec &lod_center, C Matrix &matrix)
  3300. {
  3301. Flt dist2=D._lod_current_factor/matrix.x.length2();
  3302. 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
  3303. return dist2;
  3304. }
  3305. Flt GetLodDist2(C Vec &lod_center, C MatrixM &matrix)
  3306. {
  3307. Flt dist2=D._lod_current_factor/matrix.x.length2();
  3308. 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
  3309. return dist2;
  3310. }
  3311. /******************************************************************************/
  3312. }
  3313. /******************************************************************************/