Matrix2-e1298525.js 232 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086
  1. define(['exports', './Matrix3-41c58dde', './Check-6ede7e26', './defaultValue-fe22d8c0', './Math-0a2ac845', './RuntimeError-ef395448'], (function (exports, Matrix3, Check, defaultValue, Math$1, RuntimeError) { 'use strict';
  2. /**
  3. * A 4D Cartesian point.
  4. * @alias Cartesian4
  5. * @constructor
  6. *
  7. * @param {number} [x=0.0] The X component.
  8. * @param {number} [y=0.0] The Y component.
  9. * @param {number} [z=0.0] The Z component.
  10. * @param {number} [w=0.0] The W component.
  11. *
  12. * @see Cartesian2
  13. * @see Cartesian3
  14. * @see Packable
  15. */
  16. function Cartesian4(x, y, z, w) {
  17. /**
  18. * The X component.
  19. * @type {number}
  20. * @default 0.0
  21. */
  22. this.x = defaultValue.defaultValue(x, 0.0);
  23. /**
  24. * The Y component.
  25. * @type {number}
  26. * @default 0.0
  27. */
  28. this.y = defaultValue.defaultValue(y, 0.0);
  29. /**
  30. * The Z component.
  31. * @type {number}
  32. * @default 0.0
  33. */
  34. this.z = defaultValue.defaultValue(z, 0.0);
  35. /**
  36. * The W component.
  37. * @type {number}
  38. * @default 0.0
  39. */
  40. this.w = defaultValue.defaultValue(w, 0.0);
  41. }
  42. /**
  43. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  44. *
  45. * @param {number} x The x coordinate.
  46. * @param {number} y The y coordinate.
  47. * @param {number} z The z coordinate.
  48. * @param {number} w The w coordinate.
  49. * @param {Cartesian4} [result] The object onto which to store the result.
  50. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  51. */
  52. Cartesian4.fromElements = function (x, y, z, w, result) {
  53. if (!defaultValue.defined(result)) {
  54. return new Cartesian4(x, y, z, w);
  55. }
  56. result.x = x;
  57. result.y = y;
  58. result.z = z;
  59. result.w = w;
  60. return result;
  61. };
  62. /**
  63. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  64. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  65. *
  66. * @param {Color} color The source color.
  67. * @param {Cartesian4} [result] The object onto which to store the result.
  68. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  69. */
  70. Cartesian4.fromColor = function (color, result) {
  71. //>>includeStart('debug', pragmas.debug);
  72. Check.Check.typeOf.object("color", color);
  73. //>>includeEnd('debug');
  74. if (!defaultValue.defined(result)) {
  75. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  76. }
  77. result.x = color.red;
  78. result.y = color.green;
  79. result.z = color.blue;
  80. result.w = color.alpha;
  81. return result;
  82. };
  83. /**
  84. * Duplicates a Cartesian4 instance.
  85. *
  86. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  87. * @param {Cartesian4} [result] The object onto which to store the result.
  88. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  89. */
  90. Cartesian4.clone = function (cartesian, result) {
  91. if (!defaultValue.defined(cartesian)) {
  92. return undefined;
  93. }
  94. if (!defaultValue.defined(result)) {
  95. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  96. }
  97. result.x = cartesian.x;
  98. result.y = cartesian.y;
  99. result.z = cartesian.z;
  100. result.w = cartesian.w;
  101. return result;
  102. };
  103. /**
  104. * The number of elements used to pack the object into an array.
  105. * @type {number}
  106. */
  107. Cartesian4.packedLength = 4;
  108. /**
  109. * Stores the provided instance into the provided array.
  110. *
  111. * @param {Cartesian4} value The value to pack.
  112. * @param {number[]} array The array to pack into.
  113. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  114. *
  115. * @returns {number[]} The array that was packed into
  116. */
  117. Cartesian4.pack = function (value, array, startingIndex) {
  118. //>>includeStart('debug', pragmas.debug);
  119. Check.Check.typeOf.object("value", value);
  120. Check.Check.defined("array", array);
  121. //>>includeEnd('debug');
  122. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  123. array[startingIndex++] = value.x;
  124. array[startingIndex++] = value.y;
  125. array[startingIndex++] = value.z;
  126. array[startingIndex] = value.w;
  127. return array;
  128. };
  129. /**
  130. * Retrieves an instance from a packed array.
  131. *
  132. * @param {number[]} array The packed array.
  133. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  134. * @param {Cartesian4} [result] The object into which to store the result.
  135. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  136. */
  137. Cartesian4.unpack = function (array, startingIndex, result) {
  138. //>>includeStart('debug', pragmas.debug);
  139. Check.Check.defined("array", array);
  140. //>>includeEnd('debug');
  141. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  142. if (!defaultValue.defined(result)) {
  143. result = new Cartesian4();
  144. }
  145. result.x = array[startingIndex++];
  146. result.y = array[startingIndex++];
  147. result.z = array[startingIndex++];
  148. result.w = array[startingIndex];
  149. return result;
  150. };
  151. /**
  152. * Flattens an array of Cartesian4s into an array of components.
  153. *
  154. * @param {Cartesian4[]} array The array of cartesians to pack.
  155. * @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 4 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 4) elements.
  156. * @returns {number[]} The packed array.
  157. */
  158. Cartesian4.packArray = function (array, result) {
  159. //>>includeStart('debug', pragmas.debug);
  160. Check.Check.defined("array", array);
  161. //>>includeEnd('debug');
  162. const length = array.length;
  163. const resultLength = length * 4;
  164. if (!defaultValue.defined(result)) {
  165. result = new Array(resultLength);
  166. } else if (!Array.isArray(result) && result.length !== resultLength) {
  167. //>>includeStart('debug', pragmas.debug);
  168. throw new Check.DeveloperError(
  169. "If result is a typed array, it must have exactly array.length * 4 elements"
  170. );
  171. //>>includeEnd('debug');
  172. } else if (result.length !== resultLength) {
  173. result.length = resultLength;
  174. }
  175. for (let i = 0; i < length; ++i) {
  176. Cartesian4.pack(array[i], result, i * 4);
  177. }
  178. return result;
  179. };
  180. /**
  181. * Unpacks an array of cartesian components into an array of Cartesian4s.
  182. *
  183. * @param {number[]} array The array of components to unpack.
  184. * @param {Cartesian4[]} [result] The array onto which to store the result.
  185. * @returns {Cartesian4[]} The unpacked array.
  186. */
  187. Cartesian4.unpackArray = function (array, result) {
  188. //>>includeStart('debug', pragmas.debug);
  189. Check.Check.defined("array", array);
  190. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  191. if (array.length % 4 !== 0) {
  192. throw new Check.DeveloperError("array length must be a multiple of 4.");
  193. }
  194. //>>includeEnd('debug');
  195. const length = array.length;
  196. if (!defaultValue.defined(result)) {
  197. result = new Array(length / 4);
  198. } else {
  199. result.length = length / 4;
  200. }
  201. for (let i = 0; i < length; i += 4) {
  202. const index = i / 4;
  203. result[index] = Cartesian4.unpack(array, i, result[index]);
  204. }
  205. return result;
  206. };
  207. /**
  208. * Creates a Cartesian4 from four consecutive elements in an array.
  209. * @function
  210. *
  211. * @param {number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  212. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  213. * @param {Cartesian4} [result] The object onto which to store the result.
  214. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  215. *
  216. * @example
  217. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  218. * const v = [1.0, 2.0, 3.0, 4.0];
  219. * const p = Cesium.Cartesian4.fromArray(v);
  220. *
  221. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  222. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  223. * const p2 = Cesium.Cartesian4.fromArray(v2, 2);
  224. */
  225. Cartesian4.fromArray = Cartesian4.unpack;
  226. /**
  227. * Computes the value of the maximum component for the supplied Cartesian.
  228. *
  229. * @param {Cartesian4} cartesian The cartesian to use.
  230. * @returns {number} The value of the maximum component.
  231. */
  232. Cartesian4.maximumComponent = function (cartesian) {
  233. //>>includeStart('debug', pragmas.debug);
  234. Check.Check.typeOf.object("cartesian", cartesian);
  235. //>>includeEnd('debug');
  236. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  237. };
  238. /**
  239. * Computes the value of the minimum component for the supplied Cartesian.
  240. *
  241. * @param {Cartesian4} cartesian The cartesian to use.
  242. * @returns {number} The value of the minimum component.
  243. */
  244. Cartesian4.minimumComponent = function (cartesian) {
  245. //>>includeStart('debug', pragmas.debug);
  246. Check.Check.typeOf.object("cartesian", cartesian);
  247. //>>includeEnd('debug');
  248. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  249. };
  250. /**
  251. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  252. *
  253. * @param {Cartesian4} first A cartesian to compare.
  254. * @param {Cartesian4} second A cartesian to compare.
  255. * @param {Cartesian4} result The object into which to store the result.
  256. * @returns {Cartesian4} A cartesian with the minimum components.
  257. */
  258. Cartesian4.minimumByComponent = function (first, second, result) {
  259. //>>includeStart('debug', pragmas.debug);
  260. Check.Check.typeOf.object("first", first);
  261. Check.Check.typeOf.object("second", second);
  262. Check.Check.typeOf.object("result", result);
  263. //>>includeEnd('debug');
  264. result.x = Math.min(first.x, second.x);
  265. result.y = Math.min(first.y, second.y);
  266. result.z = Math.min(first.z, second.z);
  267. result.w = Math.min(first.w, second.w);
  268. return result;
  269. };
  270. /**
  271. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  272. *
  273. * @param {Cartesian4} first A cartesian to compare.
  274. * @param {Cartesian4} second A cartesian to compare.
  275. * @param {Cartesian4} result The object into which to store the result.
  276. * @returns {Cartesian4} A cartesian with the maximum components.
  277. */
  278. Cartesian4.maximumByComponent = function (first, second, result) {
  279. //>>includeStart('debug', pragmas.debug);
  280. Check.Check.typeOf.object("first", first);
  281. Check.Check.typeOf.object("second", second);
  282. Check.Check.typeOf.object("result", result);
  283. //>>includeEnd('debug');
  284. result.x = Math.max(first.x, second.x);
  285. result.y = Math.max(first.y, second.y);
  286. result.z = Math.max(first.z, second.z);
  287. result.w = Math.max(first.w, second.w);
  288. return result;
  289. };
  290. /**
  291. * Constrain a value to lie between two values.
  292. *
  293. * @param {Cartesian4} value The value to clamp.
  294. * @param {Cartesian4} min The minimum bound.
  295. * @param {Cartesian4} max The maximum bound.
  296. * @param {Cartesian4} result The object into which to store the result.
  297. * @returns {Cartesian4} The clamped value such that min <= result <= max.
  298. */
  299. Cartesian4.clamp = function (value, min, max, result) {
  300. //>>includeStart('debug', pragmas.debug);
  301. Check.Check.typeOf.object("value", value);
  302. Check.Check.typeOf.object("min", min);
  303. Check.Check.typeOf.object("max", max);
  304. Check.Check.typeOf.object("result", result);
  305. //>>includeEnd('debug');
  306. const x = Math$1.CesiumMath.clamp(value.x, min.x, max.x);
  307. const y = Math$1.CesiumMath.clamp(value.y, min.y, max.y);
  308. const z = Math$1.CesiumMath.clamp(value.z, min.z, max.z);
  309. const w = Math$1.CesiumMath.clamp(value.w, min.w, max.w);
  310. result.x = x;
  311. result.y = y;
  312. result.z = z;
  313. result.w = w;
  314. return result;
  315. };
  316. /**
  317. * Computes the provided Cartesian's squared magnitude.
  318. *
  319. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  320. * @returns {number} The squared magnitude.
  321. */
  322. Cartesian4.magnitudeSquared = function (cartesian) {
  323. //>>includeStart('debug', pragmas.debug);
  324. Check.Check.typeOf.object("cartesian", cartesian);
  325. //>>includeEnd('debug');
  326. return (
  327. cartesian.x * cartesian.x +
  328. cartesian.y * cartesian.y +
  329. cartesian.z * cartesian.z +
  330. cartesian.w * cartesian.w
  331. );
  332. };
  333. /**
  334. * Computes the Cartesian's magnitude (length).
  335. *
  336. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  337. * @returns {number} The magnitude.
  338. */
  339. Cartesian4.magnitude = function (cartesian) {
  340. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  341. };
  342. const distanceScratch$1 = new Cartesian4();
  343. /**
  344. * Computes the 4-space distance between two points.
  345. *
  346. * @param {Cartesian4} left The first point to compute the distance from.
  347. * @param {Cartesian4} right The second point to compute the distance to.
  348. * @returns {number} The distance between two points.
  349. *
  350. * @example
  351. * // Returns 1.0
  352. * const d = Cesium.Cartesian4.distance(
  353. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  354. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  355. */
  356. Cartesian4.distance = function (left, right) {
  357. //>>includeStart('debug', pragmas.debug);
  358. Check.Check.typeOf.object("left", left);
  359. Check.Check.typeOf.object("right", right);
  360. //>>includeEnd('debug');
  361. Cartesian4.subtract(left, right, distanceScratch$1);
  362. return Cartesian4.magnitude(distanceScratch$1);
  363. };
  364. /**
  365. * Computes the squared distance between two points. Comparing squared distances
  366. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  367. *
  368. * @param {Cartesian4} left The first point to compute the distance from.
  369. * @param {Cartesian4} right The second point to compute the distance to.
  370. * @returns {number} The distance between two points.
  371. *
  372. * @example
  373. * // Returns 4.0, not 2.0
  374. * const d = Cesium.Cartesian4.distance(
  375. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  376. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  377. */
  378. Cartesian4.distanceSquared = function (left, right) {
  379. //>>includeStart('debug', pragmas.debug);
  380. Check.Check.typeOf.object("left", left);
  381. Check.Check.typeOf.object("right", right);
  382. //>>includeEnd('debug');
  383. Cartesian4.subtract(left, right, distanceScratch$1);
  384. return Cartesian4.magnitudeSquared(distanceScratch$1);
  385. };
  386. /**
  387. * Computes the normalized form of the supplied Cartesian.
  388. *
  389. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  390. * @param {Cartesian4} result The object onto which to store the result.
  391. * @returns {Cartesian4} The modified result parameter.
  392. */
  393. Cartesian4.normalize = function (cartesian, result) {
  394. //>>includeStart('debug', pragmas.debug);
  395. Check.Check.typeOf.object("cartesian", cartesian);
  396. Check.Check.typeOf.object("result", result);
  397. //>>includeEnd('debug');
  398. const magnitude = Cartesian4.magnitude(cartesian);
  399. result.x = cartesian.x / magnitude;
  400. result.y = cartesian.y / magnitude;
  401. result.z = cartesian.z / magnitude;
  402. result.w = cartesian.w / magnitude;
  403. //>>includeStart('debug', pragmas.debug);
  404. if (
  405. isNaN(result.x) ||
  406. isNaN(result.y) ||
  407. isNaN(result.z) ||
  408. isNaN(result.w)
  409. ) {
  410. throw new Check.DeveloperError("normalized result is not a number");
  411. }
  412. //>>includeEnd('debug');
  413. return result;
  414. };
  415. /**
  416. * Computes the dot (scalar) product of two Cartesians.
  417. *
  418. * @param {Cartesian4} left The first Cartesian.
  419. * @param {Cartesian4} right The second Cartesian.
  420. * @returns {number} The dot product.
  421. */
  422. Cartesian4.dot = function (left, right) {
  423. //>>includeStart('debug', pragmas.debug);
  424. Check.Check.typeOf.object("left", left);
  425. Check.Check.typeOf.object("right", right);
  426. //>>includeEnd('debug');
  427. return (
  428. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  429. );
  430. };
  431. /**
  432. * Computes the componentwise product of two Cartesians.
  433. *
  434. * @param {Cartesian4} left The first Cartesian.
  435. * @param {Cartesian4} right The second Cartesian.
  436. * @param {Cartesian4} result The object onto which to store the result.
  437. * @returns {Cartesian4} The modified result parameter.
  438. */
  439. Cartesian4.multiplyComponents = function (left, right, result) {
  440. //>>includeStart('debug', pragmas.debug);
  441. Check.Check.typeOf.object("left", left);
  442. Check.Check.typeOf.object("right", right);
  443. Check.Check.typeOf.object("result", result);
  444. //>>includeEnd('debug');
  445. result.x = left.x * right.x;
  446. result.y = left.y * right.y;
  447. result.z = left.z * right.z;
  448. result.w = left.w * right.w;
  449. return result;
  450. };
  451. /**
  452. * Computes the componentwise quotient of two Cartesians.
  453. *
  454. * @param {Cartesian4} left The first Cartesian.
  455. * @param {Cartesian4} right The second Cartesian.
  456. * @param {Cartesian4} result The object onto which to store the result.
  457. * @returns {Cartesian4} The modified result parameter.
  458. */
  459. Cartesian4.divideComponents = function (left, right, result) {
  460. //>>includeStart('debug', pragmas.debug);
  461. Check.Check.typeOf.object("left", left);
  462. Check.Check.typeOf.object("right", right);
  463. Check.Check.typeOf.object("result", result);
  464. //>>includeEnd('debug');
  465. result.x = left.x / right.x;
  466. result.y = left.y / right.y;
  467. result.z = left.z / right.z;
  468. result.w = left.w / right.w;
  469. return result;
  470. };
  471. /**
  472. * Computes the componentwise sum of two Cartesians.
  473. *
  474. * @param {Cartesian4} left The first Cartesian.
  475. * @param {Cartesian4} right The second Cartesian.
  476. * @param {Cartesian4} result The object onto which to store the result.
  477. * @returns {Cartesian4} The modified result parameter.
  478. */
  479. Cartesian4.add = function (left, right, result) {
  480. //>>includeStart('debug', pragmas.debug);
  481. Check.Check.typeOf.object("left", left);
  482. Check.Check.typeOf.object("right", right);
  483. Check.Check.typeOf.object("result", result);
  484. //>>includeEnd('debug');
  485. result.x = left.x + right.x;
  486. result.y = left.y + right.y;
  487. result.z = left.z + right.z;
  488. result.w = left.w + right.w;
  489. return result;
  490. };
  491. /**
  492. * Computes the componentwise difference of two Cartesians.
  493. *
  494. * @param {Cartesian4} left The first Cartesian.
  495. * @param {Cartesian4} right The second Cartesian.
  496. * @param {Cartesian4} result The object onto which to store the result.
  497. * @returns {Cartesian4} The modified result parameter.
  498. */
  499. Cartesian4.subtract = function (left, right, result) {
  500. //>>includeStart('debug', pragmas.debug);
  501. Check.Check.typeOf.object("left", left);
  502. Check.Check.typeOf.object("right", right);
  503. Check.Check.typeOf.object("result", result);
  504. //>>includeEnd('debug');
  505. result.x = left.x - right.x;
  506. result.y = left.y - right.y;
  507. result.z = left.z - right.z;
  508. result.w = left.w - right.w;
  509. return result;
  510. };
  511. /**
  512. * Multiplies the provided Cartesian componentwise by the provided scalar.
  513. *
  514. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  515. * @param {number} scalar The scalar to multiply with.
  516. * @param {Cartesian4} result The object onto which to store the result.
  517. * @returns {Cartesian4} The modified result parameter.
  518. */
  519. Cartesian4.multiplyByScalar = function (cartesian, scalar, result) {
  520. //>>includeStart('debug', pragmas.debug);
  521. Check.Check.typeOf.object("cartesian", cartesian);
  522. Check.Check.typeOf.number("scalar", scalar);
  523. Check.Check.typeOf.object("result", result);
  524. //>>includeEnd('debug');
  525. result.x = cartesian.x * scalar;
  526. result.y = cartesian.y * scalar;
  527. result.z = cartesian.z * scalar;
  528. result.w = cartesian.w * scalar;
  529. return result;
  530. };
  531. /**
  532. * Divides the provided Cartesian componentwise by the provided scalar.
  533. *
  534. * @param {Cartesian4} cartesian The Cartesian to be divided.
  535. * @param {number} scalar The scalar to divide by.
  536. * @param {Cartesian4} result The object onto which to store the result.
  537. * @returns {Cartesian4} The modified result parameter.
  538. */
  539. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  540. //>>includeStart('debug', pragmas.debug);
  541. Check.Check.typeOf.object("cartesian", cartesian);
  542. Check.Check.typeOf.number("scalar", scalar);
  543. Check.Check.typeOf.object("result", result);
  544. //>>includeEnd('debug');
  545. result.x = cartesian.x / scalar;
  546. result.y = cartesian.y / scalar;
  547. result.z = cartesian.z / scalar;
  548. result.w = cartesian.w / scalar;
  549. return result;
  550. };
  551. /**
  552. * Negates the provided Cartesian.
  553. *
  554. * @param {Cartesian4} cartesian The Cartesian to be negated.
  555. * @param {Cartesian4} result The object onto which to store the result.
  556. * @returns {Cartesian4} The modified result parameter.
  557. */
  558. Cartesian4.negate = function (cartesian, result) {
  559. //>>includeStart('debug', pragmas.debug);
  560. Check.Check.typeOf.object("cartesian", cartesian);
  561. Check.Check.typeOf.object("result", result);
  562. //>>includeEnd('debug');
  563. result.x = -cartesian.x;
  564. result.y = -cartesian.y;
  565. result.z = -cartesian.z;
  566. result.w = -cartesian.w;
  567. return result;
  568. };
  569. /**
  570. * Computes the absolute value of the provided Cartesian.
  571. *
  572. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  573. * @param {Cartesian4} result The object onto which to store the result.
  574. * @returns {Cartesian4} The modified result parameter.
  575. */
  576. Cartesian4.abs = function (cartesian, result) {
  577. //>>includeStart('debug', pragmas.debug);
  578. Check.Check.typeOf.object("cartesian", cartesian);
  579. Check.Check.typeOf.object("result", result);
  580. //>>includeEnd('debug');
  581. result.x = Math.abs(cartesian.x);
  582. result.y = Math.abs(cartesian.y);
  583. result.z = Math.abs(cartesian.z);
  584. result.w = Math.abs(cartesian.w);
  585. return result;
  586. };
  587. const lerpScratch$1 = new Cartesian4();
  588. /**
  589. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  590. *
  591. * @param {Cartesian4} start The value corresponding to t at 0.0.
  592. * @param {Cartesian4}end The value corresponding to t at 1.0.
  593. * @param {number} t The point along t at which to interpolate.
  594. * @param {Cartesian4} result The object onto which to store the result.
  595. * @returns {Cartesian4} The modified result parameter.
  596. */
  597. Cartesian4.lerp = function (start, end, t, result) {
  598. //>>includeStart('debug', pragmas.debug);
  599. Check.Check.typeOf.object("start", start);
  600. Check.Check.typeOf.object("end", end);
  601. Check.Check.typeOf.number("t", t);
  602. Check.Check.typeOf.object("result", result);
  603. //>>includeEnd('debug');
  604. Cartesian4.multiplyByScalar(end, t, lerpScratch$1);
  605. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  606. return Cartesian4.add(lerpScratch$1, result, result);
  607. };
  608. const mostOrthogonalAxisScratch$1 = new Cartesian4();
  609. /**
  610. * Returns the axis that is most orthogonal to the provided Cartesian.
  611. *
  612. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  613. * @param {Cartesian4} result The object onto which to store the result.
  614. * @returns {Cartesian4} The most orthogonal axis.
  615. */
  616. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  617. //>>includeStart('debug', pragmas.debug);
  618. Check.Check.typeOf.object("cartesian", cartesian);
  619. Check.Check.typeOf.object("result", result);
  620. //>>includeEnd('debug');
  621. const f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch$1);
  622. Cartesian4.abs(f, f);
  623. if (f.x <= f.y) {
  624. if (f.x <= f.z) {
  625. if (f.x <= f.w) {
  626. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  627. } else {
  628. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  629. }
  630. } else if (f.z <= f.w) {
  631. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  632. } else {
  633. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  634. }
  635. } else if (f.y <= f.z) {
  636. if (f.y <= f.w) {
  637. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  638. } else {
  639. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  640. }
  641. } else if (f.z <= f.w) {
  642. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  643. } else {
  644. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  645. }
  646. return result;
  647. };
  648. /**
  649. * Compares the provided Cartesians componentwise and returns
  650. * <code>true</code> if they are equal, <code>false</code> otherwise.
  651. *
  652. * @param {Cartesian4} [left] The first Cartesian.
  653. * @param {Cartesian4} [right] The second Cartesian.
  654. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  655. */
  656. Cartesian4.equals = function (left, right) {
  657. return (
  658. left === right ||
  659. (defaultValue.defined(left) &&
  660. defaultValue.defined(right) &&
  661. left.x === right.x &&
  662. left.y === right.y &&
  663. left.z === right.z &&
  664. left.w === right.w)
  665. );
  666. };
  667. /**
  668. * @private
  669. */
  670. Cartesian4.equalsArray = function (cartesian, array, offset) {
  671. return (
  672. cartesian.x === array[offset] &&
  673. cartesian.y === array[offset + 1] &&
  674. cartesian.z === array[offset + 2] &&
  675. cartesian.w === array[offset + 3]
  676. );
  677. };
  678. /**
  679. * Compares the provided Cartesians componentwise and returns
  680. * <code>true</code> if they pass an absolute or relative tolerance test,
  681. * <code>false</code> otherwise.
  682. *
  683. * @param {Cartesian4} [left] The first Cartesian.
  684. * @param {Cartesian4} [right] The second Cartesian.
  685. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  686. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  687. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  688. */
  689. Cartesian4.equalsEpsilon = function (
  690. left,
  691. right,
  692. relativeEpsilon,
  693. absoluteEpsilon
  694. ) {
  695. return (
  696. left === right ||
  697. (defaultValue.defined(left) &&
  698. defaultValue.defined(right) &&
  699. Math$1.CesiumMath.equalsEpsilon(
  700. left.x,
  701. right.x,
  702. relativeEpsilon,
  703. absoluteEpsilon
  704. ) &&
  705. Math$1.CesiumMath.equalsEpsilon(
  706. left.y,
  707. right.y,
  708. relativeEpsilon,
  709. absoluteEpsilon
  710. ) &&
  711. Math$1.CesiumMath.equalsEpsilon(
  712. left.z,
  713. right.z,
  714. relativeEpsilon,
  715. absoluteEpsilon
  716. ) &&
  717. Math$1.CesiumMath.equalsEpsilon(
  718. left.w,
  719. right.w,
  720. relativeEpsilon,
  721. absoluteEpsilon
  722. ))
  723. );
  724. };
  725. /**
  726. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  727. *
  728. * @type {Cartesian4}
  729. * @constant
  730. */
  731. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  732. /**
  733. * An immutable Cartesian4 instance initialized to (1.0, 1.0, 1.0, 1.0).
  734. *
  735. * @type {Cartesian4}
  736. * @constant
  737. */
  738. Cartesian4.ONE = Object.freeze(new Cartesian4(1.0, 1.0, 1.0, 1.0));
  739. /**
  740. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  741. *
  742. * @type {Cartesian4}
  743. * @constant
  744. */
  745. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  746. /**
  747. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  748. *
  749. * @type {Cartesian4}
  750. * @constant
  751. */
  752. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  753. /**
  754. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  755. *
  756. * @type {Cartesian4}
  757. * @constant
  758. */
  759. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  760. /**
  761. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  762. *
  763. * @type {Cartesian4}
  764. * @constant
  765. */
  766. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  767. /**
  768. * Duplicates this Cartesian4 instance.
  769. *
  770. * @param {Cartesian4} [result] The object onto which to store the result.
  771. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  772. */
  773. Cartesian4.prototype.clone = function (result) {
  774. return Cartesian4.clone(this, result);
  775. };
  776. /**
  777. * Compares this Cartesian against the provided Cartesian componentwise and returns
  778. * <code>true</code> if they are equal, <code>false</code> otherwise.
  779. *
  780. * @param {Cartesian4} [right] The right hand side Cartesian.
  781. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  782. */
  783. Cartesian4.prototype.equals = function (right) {
  784. return Cartesian4.equals(this, right);
  785. };
  786. /**
  787. * Compares this Cartesian against the provided Cartesian componentwise and returns
  788. * <code>true</code> if they pass an absolute or relative tolerance test,
  789. * <code>false</code> otherwise.
  790. *
  791. * @param {Cartesian4} [right] The right hand side Cartesian.
  792. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  793. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  794. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  795. */
  796. Cartesian4.prototype.equalsEpsilon = function (
  797. right,
  798. relativeEpsilon,
  799. absoluteEpsilon
  800. ) {
  801. return Cartesian4.equalsEpsilon(
  802. this,
  803. right,
  804. relativeEpsilon,
  805. absoluteEpsilon
  806. );
  807. };
  808. /**
  809. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  810. *
  811. * @returns {string} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  812. */
  813. Cartesian4.prototype.toString = function () {
  814. return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
  815. };
  816. // scratchU8Array and scratchF32Array are views into the same buffer
  817. const scratchF32Array = new Float32Array(1);
  818. const scratchU8Array = new Uint8Array(scratchF32Array.buffer);
  819. const testU32 = new Uint32Array([0x11223344]);
  820. const testU8 = new Uint8Array(testU32.buffer);
  821. const littleEndian = testU8[0] === 0x44;
  822. /**
  823. * Packs an arbitrary floating point value to 4 values representable using uint8.
  824. *
  825. * @param {number} value A floating point number.
  826. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  827. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  828. */
  829. Cartesian4.packFloat = function (value, result) {
  830. //>>includeStart('debug', pragmas.debug);
  831. Check.Check.typeOf.number("value", value);
  832. //>>includeEnd('debug');
  833. if (!defaultValue.defined(result)) {
  834. result = new Cartesian4();
  835. }
  836. // scratchU8Array and scratchF32Array are views into the same buffer
  837. scratchF32Array[0] = value;
  838. if (littleEndian) {
  839. result.x = scratchU8Array[0];
  840. result.y = scratchU8Array[1];
  841. result.z = scratchU8Array[2];
  842. result.w = scratchU8Array[3];
  843. } else {
  844. // convert from big-endian to little-endian
  845. result.x = scratchU8Array[3];
  846. result.y = scratchU8Array[2];
  847. result.z = scratchU8Array[1];
  848. result.w = scratchU8Array[0];
  849. }
  850. return result;
  851. };
  852. /**
  853. * Unpacks a float packed using Cartesian4.packFloat.
  854. *
  855. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  856. * @returns {number} The unpacked float.
  857. * @private
  858. */
  859. Cartesian4.unpackFloat = function (packedFloat) {
  860. //>>includeStart('debug', pragmas.debug);
  861. Check.Check.typeOf.object("packedFloat", packedFloat);
  862. //>>includeEnd('debug');
  863. // scratchU8Array and scratchF32Array are views into the same buffer
  864. if (littleEndian) {
  865. scratchU8Array[0] = packedFloat.x;
  866. scratchU8Array[1] = packedFloat.y;
  867. scratchU8Array[2] = packedFloat.z;
  868. scratchU8Array[3] = packedFloat.w;
  869. } else {
  870. // convert from little-endian to big-endian
  871. scratchU8Array[0] = packedFloat.w;
  872. scratchU8Array[1] = packedFloat.z;
  873. scratchU8Array[2] = packedFloat.y;
  874. scratchU8Array[3] = packedFloat.x;
  875. }
  876. return scratchF32Array[0];
  877. };
  878. /**
  879. * A 4x4 matrix, indexable as a column-major order array.
  880. * Constructor parameters are in row-major order for code readability.
  881. * @alias Matrix4
  882. * @constructor
  883. * @implements {ArrayLike<number>}
  884. *
  885. * @param {number} [column0Row0=0.0] The value for column 0, row 0.
  886. * @param {number} [column1Row0=0.0] The value for column 1, row 0.
  887. * @param {number} [column2Row0=0.0] The value for column 2, row 0.
  888. * @param {number} [column3Row0=0.0] The value for column 3, row 0.
  889. * @param {number} [column0Row1=0.0] The value for column 0, row 1.
  890. * @param {number} [column1Row1=0.0] The value for column 1, row 1.
  891. * @param {number} [column2Row1=0.0] The value for column 2, row 1.
  892. * @param {number} [column3Row1=0.0] The value for column 3, row 1.
  893. * @param {number} [column0Row2=0.0] The value for column 0, row 2.
  894. * @param {number} [column1Row2=0.0] The value for column 1, row 2.
  895. * @param {number} [column2Row2=0.0] The value for column 2, row 2.
  896. * @param {number} [column3Row2=0.0] The value for column 3, row 2.
  897. * @param {number} [column0Row3=0.0] The value for column 0, row 3.
  898. * @param {number} [column1Row3=0.0] The value for column 1, row 3.
  899. * @param {number} [column2Row3=0.0] The value for column 2, row 3.
  900. * @param {number} [column3Row3=0.0] The value for column 3, row 3.
  901. *
  902. * @see Matrix4.fromArray
  903. * @see Matrix4.fromColumnMajorArray
  904. * @see Matrix4.fromRowMajorArray
  905. * @see Matrix4.fromRotationTranslation
  906. * @see Matrix4.fromTranslationQuaternionRotationScale
  907. * @see Matrix4.fromTranslationRotationScale
  908. * @see Matrix4.fromTranslation
  909. * @see Matrix4.fromScale
  910. * @see Matrix4.fromUniformScale
  911. * @see Matrix4.fromRotation
  912. * @see Matrix4.fromCamera
  913. * @see Matrix4.computePerspectiveFieldOfView
  914. * @see Matrix4.computeOrthographicOffCenter
  915. * @see Matrix4.computePerspectiveOffCenter
  916. * @see Matrix4.computeInfinitePerspectiveOffCenter
  917. * @see Matrix4.computeViewportTransformation
  918. * @see Matrix4.computeView
  919. * @see Matrix2
  920. * @see Matrix3
  921. * @see Packable
  922. */
  923. function Matrix4(
  924. column0Row0,
  925. column1Row0,
  926. column2Row0,
  927. column3Row0,
  928. column0Row1,
  929. column1Row1,
  930. column2Row1,
  931. column3Row1,
  932. column0Row2,
  933. column1Row2,
  934. column2Row2,
  935. column3Row2,
  936. column0Row3,
  937. column1Row3,
  938. column2Row3,
  939. column3Row3
  940. ) {
  941. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  942. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  943. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  944. this[3] = defaultValue.defaultValue(column0Row3, 0.0);
  945. this[4] = defaultValue.defaultValue(column1Row0, 0.0);
  946. this[5] = defaultValue.defaultValue(column1Row1, 0.0);
  947. this[6] = defaultValue.defaultValue(column1Row2, 0.0);
  948. this[7] = defaultValue.defaultValue(column1Row3, 0.0);
  949. this[8] = defaultValue.defaultValue(column2Row0, 0.0);
  950. this[9] = defaultValue.defaultValue(column2Row1, 0.0);
  951. this[10] = defaultValue.defaultValue(column2Row2, 0.0);
  952. this[11] = defaultValue.defaultValue(column2Row3, 0.0);
  953. this[12] = defaultValue.defaultValue(column3Row0, 0.0);
  954. this[13] = defaultValue.defaultValue(column3Row1, 0.0);
  955. this[14] = defaultValue.defaultValue(column3Row2, 0.0);
  956. this[15] = defaultValue.defaultValue(column3Row3, 0.0);
  957. }
  958. /**
  959. * The number of elements used to pack the object into an array.
  960. * @type {number}
  961. */
  962. Matrix4.packedLength = 16;
  963. /**
  964. * Stores the provided instance into the provided array.
  965. *
  966. * @param {Matrix4} value The value to pack.
  967. * @param {number[]} array The array to pack into.
  968. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  969. *
  970. * @returns {number[]} The array that was packed into
  971. */
  972. Matrix4.pack = function (value, array, startingIndex) {
  973. //>>includeStart('debug', pragmas.debug);
  974. Check.Check.typeOf.object("value", value);
  975. Check.Check.defined("array", array);
  976. //>>includeEnd('debug');
  977. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  978. array[startingIndex++] = value[0];
  979. array[startingIndex++] = value[1];
  980. array[startingIndex++] = value[2];
  981. array[startingIndex++] = value[3];
  982. array[startingIndex++] = value[4];
  983. array[startingIndex++] = value[5];
  984. array[startingIndex++] = value[6];
  985. array[startingIndex++] = value[7];
  986. array[startingIndex++] = value[8];
  987. array[startingIndex++] = value[9];
  988. array[startingIndex++] = value[10];
  989. array[startingIndex++] = value[11];
  990. array[startingIndex++] = value[12];
  991. array[startingIndex++] = value[13];
  992. array[startingIndex++] = value[14];
  993. array[startingIndex] = value[15];
  994. return array;
  995. };
  996. /**
  997. * Retrieves an instance from a packed array.
  998. *
  999. * @param {number[]} array The packed array.
  1000. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  1001. * @param {Matrix4} [result] The object into which to store the result.
  1002. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  1003. */
  1004. Matrix4.unpack = function (array, startingIndex, result) {
  1005. //>>includeStart('debug', pragmas.debug);
  1006. Check.Check.defined("array", array);
  1007. //>>includeEnd('debug');
  1008. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1009. if (!defaultValue.defined(result)) {
  1010. result = new Matrix4();
  1011. }
  1012. result[0] = array[startingIndex++];
  1013. result[1] = array[startingIndex++];
  1014. result[2] = array[startingIndex++];
  1015. result[3] = array[startingIndex++];
  1016. result[4] = array[startingIndex++];
  1017. result[5] = array[startingIndex++];
  1018. result[6] = array[startingIndex++];
  1019. result[7] = array[startingIndex++];
  1020. result[8] = array[startingIndex++];
  1021. result[9] = array[startingIndex++];
  1022. result[10] = array[startingIndex++];
  1023. result[11] = array[startingIndex++];
  1024. result[12] = array[startingIndex++];
  1025. result[13] = array[startingIndex++];
  1026. result[14] = array[startingIndex++];
  1027. result[15] = array[startingIndex];
  1028. return result;
  1029. };
  1030. /**
  1031. * Flattens an array of Matrix4s into an array of components. The components
  1032. * are stored in column-major order.
  1033. *
  1034. * @param {Matrix4[]} array The array of matrices to pack.
  1035. * @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 16 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 16) elements.
  1036. * @returns {number[]} The packed array.
  1037. */
  1038. Matrix4.packArray = function (array, result) {
  1039. //>>includeStart('debug', pragmas.debug);
  1040. Check.Check.defined("array", array);
  1041. //>>includeEnd('debug');
  1042. const length = array.length;
  1043. const resultLength = length * 16;
  1044. if (!defaultValue.defined(result)) {
  1045. result = new Array(resultLength);
  1046. } else if (!Array.isArray(result) && result.length !== resultLength) {
  1047. //>>includeStart('debug', pragmas.debug);
  1048. throw new Check.DeveloperError(
  1049. "If result is a typed array, it must have exactly array.length * 16 elements"
  1050. );
  1051. //>>includeEnd('debug');
  1052. } else if (result.length !== resultLength) {
  1053. result.length = resultLength;
  1054. }
  1055. for (let i = 0; i < length; ++i) {
  1056. Matrix4.pack(array[i], result, i * 16);
  1057. }
  1058. return result;
  1059. };
  1060. /**
  1061. * Unpacks an array of column-major matrix components into an array of Matrix4s.
  1062. *
  1063. * @param {number[]} array The array of components to unpack.
  1064. * @param {Matrix4[]} [result] The array onto which to store the result.
  1065. * @returns {Matrix4[]} The unpacked array.
  1066. */
  1067. Matrix4.unpackArray = function (array, result) {
  1068. //>>includeStart('debug', pragmas.debug);
  1069. Check.Check.defined("array", array);
  1070. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 16);
  1071. if (array.length % 16 !== 0) {
  1072. throw new Check.DeveloperError("array length must be a multiple of 16.");
  1073. }
  1074. //>>includeEnd('debug');
  1075. const length = array.length;
  1076. if (!defaultValue.defined(result)) {
  1077. result = new Array(length / 16);
  1078. } else {
  1079. result.length = length / 16;
  1080. }
  1081. for (let i = 0; i < length; i += 16) {
  1082. const index = i / 16;
  1083. result[index] = Matrix4.unpack(array, i, result[index]);
  1084. }
  1085. return result;
  1086. };
  1087. /**
  1088. * Duplicates a Matrix4 instance.
  1089. *
  1090. * @param {Matrix4} matrix The matrix to duplicate.
  1091. * @param {Matrix4} [result] The object onto which to store the result.
  1092. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  1093. */
  1094. Matrix4.clone = function (matrix, result) {
  1095. if (!defaultValue.defined(matrix)) {
  1096. return undefined;
  1097. }
  1098. if (!defaultValue.defined(result)) {
  1099. return new Matrix4(
  1100. matrix[0],
  1101. matrix[4],
  1102. matrix[8],
  1103. matrix[12],
  1104. matrix[1],
  1105. matrix[5],
  1106. matrix[9],
  1107. matrix[13],
  1108. matrix[2],
  1109. matrix[6],
  1110. matrix[10],
  1111. matrix[14],
  1112. matrix[3],
  1113. matrix[7],
  1114. matrix[11],
  1115. matrix[15]
  1116. );
  1117. }
  1118. result[0] = matrix[0];
  1119. result[1] = matrix[1];
  1120. result[2] = matrix[2];
  1121. result[3] = matrix[3];
  1122. result[4] = matrix[4];
  1123. result[5] = matrix[5];
  1124. result[6] = matrix[6];
  1125. result[7] = matrix[7];
  1126. result[8] = matrix[8];
  1127. result[9] = matrix[9];
  1128. result[10] = matrix[10];
  1129. result[11] = matrix[11];
  1130. result[12] = matrix[12];
  1131. result[13] = matrix[13];
  1132. result[14] = matrix[14];
  1133. result[15] = matrix[15];
  1134. return result;
  1135. };
  1136. /**
  1137. * Creates a Matrix4 from 16 consecutive elements in an array.
  1138. * @function
  1139. *
  1140. * @param {number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  1141. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  1142. * @param {Matrix4} [result] The object onto which to store the result.
  1143. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  1144. *
  1145. * @example
  1146. * // Create the Matrix4:
  1147. * // [1.0, 2.0, 3.0, 4.0]
  1148. * // [1.0, 2.0, 3.0, 4.0]
  1149. * // [1.0, 2.0, 3.0, 4.0]
  1150. * // [1.0, 2.0, 3.0, 4.0]
  1151. *
  1152. * const v = [1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  1153. * const m = Cesium.Matrix4.fromArray(v);
  1154. *
  1155. * // Create same Matrix4 with using an offset into an array
  1156. * const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  1157. * const m2 = Cesium.Matrix4.fromArray(v2, 2);
  1158. */
  1159. Matrix4.fromArray = Matrix4.unpack;
  1160. /**
  1161. * Computes a Matrix4 instance from a column-major order array.
  1162. *
  1163. * @param {number[]} values The column-major order array.
  1164. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1165. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1166. */
  1167. Matrix4.fromColumnMajorArray = function (values, result) {
  1168. //>>includeStart('debug', pragmas.debug);
  1169. Check.Check.defined("values", values);
  1170. //>>includeEnd('debug');
  1171. return Matrix4.clone(values, result);
  1172. };
  1173. /**
  1174. * Computes a Matrix4 instance from a row-major order array.
  1175. * The resulting matrix will be in column-major order.
  1176. *
  1177. * @param {number[]} values The row-major order array.
  1178. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1179. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1180. */
  1181. Matrix4.fromRowMajorArray = function (values, result) {
  1182. //>>includeStart('debug', pragmas.debug);
  1183. Check.Check.defined("values", values);
  1184. //>>includeEnd('debug');
  1185. if (!defaultValue.defined(result)) {
  1186. return new Matrix4(
  1187. values[0],
  1188. values[1],
  1189. values[2],
  1190. values[3],
  1191. values[4],
  1192. values[5],
  1193. values[6],
  1194. values[7],
  1195. values[8],
  1196. values[9],
  1197. values[10],
  1198. values[11],
  1199. values[12],
  1200. values[13],
  1201. values[14],
  1202. values[15]
  1203. );
  1204. }
  1205. result[0] = values[0];
  1206. result[1] = values[4];
  1207. result[2] = values[8];
  1208. result[3] = values[12];
  1209. result[4] = values[1];
  1210. result[5] = values[5];
  1211. result[6] = values[9];
  1212. result[7] = values[13];
  1213. result[8] = values[2];
  1214. result[9] = values[6];
  1215. result[10] = values[10];
  1216. result[11] = values[14];
  1217. result[12] = values[3];
  1218. result[13] = values[7];
  1219. result[14] = values[11];
  1220. result[15] = values[15];
  1221. return result;
  1222. };
  1223. /**
  1224. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  1225. * and a Cartesian3 representing the translation.
  1226. *
  1227. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  1228. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  1229. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1230. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1231. */
  1232. Matrix4.fromRotationTranslation = function (rotation, translation, result) {
  1233. //>>includeStart('debug', pragmas.debug);
  1234. Check.Check.typeOf.object("rotation", rotation);
  1235. //>>includeEnd('debug');
  1236. translation = defaultValue.defaultValue(translation, Matrix3.Cartesian3.ZERO);
  1237. if (!defaultValue.defined(result)) {
  1238. return new Matrix4(
  1239. rotation[0],
  1240. rotation[3],
  1241. rotation[6],
  1242. translation.x,
  1243. rotation[1],
  1244. rotation[4],
  1245. rotation[7],
  1246. translation.y,
  1247. rotation[2],
  1248. rotation[5],
  1249. rotation[8],
  1250. translation.z,
  1251. 0.0,
  1252. 0.0,
  1253. 0.0,
  1254. 1.0
  1255. );
  1256. }
  1257. result[0] = rotation[0];
  1258. result[1] = rotation[1];
  1259. result[2] = rotation[2];
  1260. result[3] = 0.0;
  1261. result[4] = rotation[3];
  1262. result[5] = rotation[4];
  1263. result[6] = rotation[5];
  1264. result[7] = 0.0;
  1265. result[8] = rotation[6];
  1266. result[9] = rotation[7];
  1267. result[10] = rotation[8];
  1268. result[11] = 0.0;
  1269. result[12] = translation.x;
  1270. result[13] = translation.y;
  1271. result[14] = translation.z;
  1272. result[15] = 1.0;
  1273. return result;
  1274. };
  1275. /**
  1276. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  1277. * representation with the rotation represented as a quaternion.
  1278. *
  1279. * @param {Cartesian3} translation The translation transformation.
  1280. * @param {Quaternion} rotation The rotation transformation.
  1281. * @param {Cartesian3} scale The non-uniform scale transformation.
  1282. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1283. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1284. *
  1285. * @example
  1286. * const result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  1287. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  1288. * Cesium.Quaternion.IDENTITY, // rotation
  1289. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  1290. * result);
  1291. */
  1292. Matrix4.fromTranslationQuaternionRotationScale = function (
  1293. translation,
  1294. rotation,
  1295. scale,
  1296. result
  1297. ) {
  1298. //>>includeStart('debug', pragmas.debug);
  1299. Check.Check.typeOf.object("translation", translation);
  1300. Check.Check.typeOf.object("rotation", rotation);
  1301. Check.Check.typeOf.object("scale", scale);
  1302. //>>includeEnd('debug');
  1303. if (!defaultValue.defined(result)) {
  1304. result = new Matrix4();
  1305. }
  1306. const scaleX = scale.x;
  1307. const scaleY = scale.y;
  1308. const scaleZ = scale.z;
  1309. const x2 = rotation.x * rotation.x;
  1310. const xy = rotation.x * rotation.y;
  1311. const xz = rotation.x * rotation.z;
  1312. const xw = rotation.x * rotation.w;
  1313. const y2 = rotation.y * rotation.y;
  1314. const yz = rotation.y * rotation.z;
  1315. const yw = rotation.y * rotation.w;
  1316. const z2 = rotation.z * rotation.z;
  1317. const zw = rotation.z * rotation.w;
  1318. const w2 = rotation.w * rotation.w;
  1319. const m00 = x2 - y2 - z2 + w2;
  1320. const m01 = 2.0 * (xy - zw);
  1321. const m02 = 2.0 * (xz + yw);
  1322. const m10 = 2.0 * (xy + zw);
  1323. const m11 = -x2 + y2 - z2 + w2;
  1324. const m12 = 2.0 * (yz - xw);
  1325. const m20 = 2.0 * (xz - yw);
  1326. const m21 = 2.0 * (yz + xw);
  1327. const m22 = -x2 - y2 + z2 + w2;
  1328. result[0] = m00 * scaleX;
  1329. result[1] = m10 * scaleX;
  1330. result[2] = m20 * scaleX;
  1331. result[3] = 0.0;
  1332. result[4] = m01 * scaleY;
  1333. result[5] = m11 * scaleY;
  1334. result[6] = m21 * scaleY;
  1335. result[7] = 0.0;
  1336. result[8] = m02 * scaleZ;
  1337. result[9] = m12 * scaleZ;
  1338. result[10] = m22 * scaleZ;
  1339. result[11] = 0.0;
  1340. result[12] = translation.x;
  1341. result[13] = translation.y;
  1342. result[14] = translation.z;
  1343. result[15] = 1.0;
  1344. return result;
  1345. };
  1346. /**
  1347. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  1348. *
  1349. * @param {TranslationRotationScale} translationRotationScale The instance.
  1350. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1351. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1352. */
  1353. Matrix4.fromTranslationRotationScale = function (
  1354. translationRotationScale,
  1355. result
  1356. ) {
  1357. //>>includeStart('debug', pragmas.debug);
  1358. Check.Check.typeOf.object("translationRotationScale", translationRotationScale);
  1359. //>>includeEnd('debug');
  1360. return Matrix4.fromTranslationQuaternionRotationScale(
  1361. translationRotationScale.translation,
  1362. translationRotationScale.rotation,
  1363. translationRotationScale.scale,
  1364. result
  1365. );
  1366. };
  1367. /**
  1368. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  1369. *
  1370. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  1371. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1372. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1373. *
  1374. * @see Matrix4.multiplyByTranslation
  1375. */
  1376. Matrix4.fromTranslation = function (translation, result) {
  1377. //>>includeStart('debug', pragmas.debug);
  1378. Check.Check.typeOf.object("translation", translation);
  1379. //>>includeEnd('debug');
  1380. return Matrix4.fromRotationTranslation(Matrix3.Matrix3.IDENTITY, translation, result);
  1381. };
  1382. /**
  1383. * Computes a Matrix4 instance representing a non-uniform scale.
  1384. *
  1385. * @param {Cartesian3} scale The x, y, and z scale factors.
  1386. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1387. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1388. *
  1389. * @example
  1390. * // Creates
  1391. * // [7.0, 0.0, 0.0, 0.0]
  1392. * // [0.0, 8.0, 0.0, 0.0]
  1393. * // [0.0, 0.0, 9.0, 0.0]
  1394. * // [0.0, 0.0, 0.0, 1.0]
  1395. * const m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  1396. */
  1397. Matrix4.fromScale = function (scale, result) {
  1398. //>>includeStart('debug', pragmas.debug);
  1399. Check.Check.typeOf.object("scale", scale);
  1400. //>>includeEnd('debug');
  1401. if (!defaultValue.defined(result)) {
  1402. return new Matrix4(
  1403. scale.x,
  1404. 0.0,
  1405. 0.0,
  1406. 0.0,
  1407. 0.0,
  1408. scale.y,
  1409. 0.0,
  1410. 0.0,
  1411. 0.0,
  1412. 0.0,
  1413. scale.z,
  1414. 0.0,
  1415. 0.0,
  1416. 0.0,
  1417. 0.0,
  1418. 1.0
  1419. );
  1420. }
  1421. result[0] = scale.x;
  1422. result[1] = 0.0;
  1423. result[2] = 0.0;
  1424. result[3] = 0.0;
  1425. result[4] = 0.0;
  1426. result[5] = scale.y;
  1427. result[6] = 0.0;
  1428. result[7] = 0.0;
  1429. result[8] = 0.0;
  1430. result[9] = 0.0;
  1431. result[10] = scale.z;
  1432. result[11] = 0.0;
  1433. result[12] = 0.0;
  1434. result[13] = 0.0;
  1435. result[14] = 0.0;
  1436. result[15] = 1.0;
  1437. return result;
  1438. };
  1439. /**
  1440. * Computes a Matrix4 instance representing a uniform scale.
  1441. *
  1442. * @param {number} scale The uniform scale factor.
  1443. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1444. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1445. *
  1446. * @example
  1447. * // Creates
  1448. * // [2.0, 0.0, 0.0, 0.0]
  1449. * // [0.0, 2.0, 0.0, 0.0]
  1450. * // [0.0, 0.0, 2.0, 0.0]
  1451. * // [0.0, 0.0, 0.0, 1.0]
  1452. * const m = Cesium.Matrix4.fromUniformScale(2.0);
  1453. */
  1454. Matrix4.fromUniformScale = function (scale, result) {
  1455. //>>includeStart('debug', pragmas.debug);
  1456. Check.Check.typeOf.number("scale", scale);
  1457. //>>includeEnd('debug');
  1458. if (!defaultValue.defined(result)) {
  1459. return new Matrix4(
  1460. scale,
  1461. 0.0,
  1462. 0.0,
  1463. 0.0,
  1464. 0.0,
  1465. scale,
  1466. 0.0,
  1467. 0.0,
  1468. 0.0,
  1469. 0.0,
  1470. scale,
  1471. 0.0,
  1472. 0.0,
  1473. 0.0,
  1474. 0.0,
  1475. 1.0
  1476. );
  1477. }
  1478. result[0] = scale;
  1479. result[1] = 0.0;
  1480. result[2] = 0.0;
  1481. result[3] = 0.0;
  1482. result[4] = 0.0;
  1483. result[5] = scale;
  1484. result[6] = 0.0;
  1485. result[7] = 0.0;
  1486. result[8] = 0.0;
  1487. result[9] = 0.0;
  1488. result[10] = scale;
  1489. result[11] = 0.0;
  1490. result[12] = 0.0;
  1491. result[13] = 0.0;
  1492. result[14] = 0.0;
  1493. result[15] = 1.0;
  1494. return result;
  1495. };
  1496. /**
  1497. * Creates a rotation matrix.
  1498. *
  1499. * @param {Matrix3} rotation The rotation matrix.
  1500. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1501. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1502. */
  1503. Matrix4.fromRotation = function (rotation, result) {
  1504. //>>includeStart('debug', pragmas.debug);
  1505. Check.Check.typeOf.object("rotation", rotation);
  1506. //>>includeEnd('debug');
  1507. if (!defaultValue.defined(result)) {
  1508. result = new Matrix4();
  1509. }
  1510. result[0] = rotation[0];
  1511. result[1] = rotation[1];
  1512. result[2] = rotation[2];
  1513. result[3] = 0.0;
  1514. result[4] = rotation[3];
  1515. result[5] = rotation[4];
  1516. result[6] = rotation[5];
  1517. result[7] = 0.0;
  1518. result[8] = rotation[6];
  1519. result[9] = rotation[7];
  1520. result[10] = rotation[8];
  1521. result[11] = 0.0;
  1522. result[12] = 0.0;
  1523. result[13] = 0.0;
  1524. result[14] = 0.0;
  1525. result[15] = 1.0;
  1526. return result;
  1527. };
  1528. const fromCameraF = new Matrix3.Cartesian3();
  1529. const fromCameraR = new Matrix3.Cartesian3();
  1530. const fromCameraU = new Matrix3.Cartesian3();
  1531. /**
  1532. * Computes a Matrix4 instance from a Camera.
  1533. *
  1534. * @param {Camera} camera The camera to use.
  1535. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1536. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1537. */
  1538. Matrix4.fromCamera = function (camera, result) {
  1539. //>>includeStart('debug', pragmas.debug);
  1540. Check.Check.typeOf.object("camera", camera);
  1541. //>>includeEnd('debug');
  1542. const position = camera.position;
  1543. const direction = camera.direction;
  1544. const up = camera.up;
  1545. //>>includeStart('debug', pragmas.debug);
  1546. Check.Check.typeOf.object("camera.position", position);
  1547. Check.Check.typeOf.object("camera.direction", direction);
  1548. Check.Check.typeOf.object("camera.up", up);
  1549. //>>includeEnd('debug');
  1550. Matrix3.Cartesian3.normalize(direction, fromCameraF);
  1551. Matrix3.Cartesian3.normalize(
  1552. Matrix3.Cartesian3.cross(fromCameraF, up, fromCameraR),
  1553. fromCameraR
  1554. );
  1555. Matrix3.Cartesian3.normalize(
  1556. Matrix3.Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU),
  1557. fromCameraU
  1558. );
  1559. const sX = fromCameraR.x;
  1560. const sY = fromCameraR.y;
  1561. const sZ = fromCameraR.z;
  1562. const fX = fromCameraF.x;
  1563. const fY = fromCameraF.y;
  1564. const fZ = fromCameraF.z;
  1565. const uX = fromCameraU.x;
  1566. const uY = fromCameraU.y;
  1567. const uZ = fromCameraU.z;
  1568. const positionX = position.x;
  1569. const positionY = position.y;
  1570. const positionZ = position.z;
  1571. const t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
  1572. const t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
  1573. const t2 = fX * positionX + fY * positionY + fZ * positionZ;
  1574. // The code below this comment is an optimized
  1575. // version of the commented lines.
  1576. // Rather that create two matrices and then multiply,
  1577. // we just bake in the multiplcation as part of creation.
  1578. // const rotation = new Matrix4(
  1579. // sX, sY, sZ, 0.0,
  1580. // uX, uY, uZ, 0.0,
  1581. // -fX, -fY, -fZ, 0.0,
  1582. // 0.0, 0.0, 0.0, 1.0);
  1583. // const translation = new Matrix4(
  1584. // 1.0, 0.0, 0.0, -position.x,
  1585. // 0.0, 1.0, 0.0, -position.y,
  1586. // 0.0, 0.0, 1.0, -position.z,
  1587. // 0.0, 0.0, 0.0, 1.0);
  1588. // return rotation.multiply(translation);
  1589. if (!defaultValue.defined(result)) {
  1590. return new Matrix4(
  1591. sX,
  1592. sY,
  1593. sZ,
  1594. t0,
  1595. uX,
  1596. uY,
  1597. uZ,
  1598. t1,
  1599. -fX,
  1600. -fY,
  1601. -fZ,
  1602. t2,
  1603. 0.0,
  1604. 0.0,
  1605. 0.0,
  1606. 1.0
  1607. );
  1608. }
  1609. result[0] = sX;
  1610. result[1] = uX;
  1611. result[2] = -fX;
  1612. result[3] = 0.0;
  1613. result[4] = sY;
  1614. result[5] = uY;
  1615. result[6] = -fY;
  1616. result[7] = 0.0;
  1617. result[8] = sZ;
  1618. result[9] = uZ;
  1619. result[10] = -fZ;
  1620. result[11] = 0.0;
  1621. result[12] = t0;
  1622. result[13] = t1;
  1623. result[14] = t2;
  1624. result[15] = 1.0;
  1625. return result;
  1626. };
  1627. /**
  1628. * Computes a Matrix4 instance representing a perspective transformation matrix.
  1629. *
  1630. * @param {number} fovY The field of view along the Y axis in radians.
  1631. * @param {number} aspectRatio The aspect ratio.
  1632. * @param {number} near The distance to the near plane in meters.
  1633. * @param {number} far The distance to the far plane in meters.
  1634. * @param {Matrix4} result The object in which the result will be stored.
  1635. * @returns {Matrix4} The modified result parameter.
  1636. *
  1637. * @exception {DeveloperError} fovY must be in (0, PI].
  1638. * @exception {DeveloperError} aspectRatio must be greater than zero.
  1639. * @exception {DeveloperError} near must be greater than zero.
  1640. * @exception {DeveloperError} far must be greater than zero.
  1641. */
  1642. Matrix4.computePerspectiveFieldOfView = function (
  1643. fovY,
  1644. aspectRatio,
  1645. near,
  1646. far,
  1647. result
  1648. ) {
  1649. //>>includeStart('debug', pragmas.debug);
  1650. Check.Check.typeOf.number.greaterThan("fovY", fovY, 0.0);
  1651. Check.Check.typeOf.number.lessThan("fovY", fovY, Math.PI);
  1652. Check.Check.typeOf.number.greaterThan("near", near, 0.0);
  1653. Check.Check.typeOf.number.greaterThan("far", far, 0.0);
  1654. Check.Check.typeOf.object("result", result);
  1655. //>>includeEnd('debug');
  1656. const bottom = Math.tan(fovY * 0.5);
  1657. const column1Row1 = 1.0 / bottom;
  1658. const column0Row0 = column1Row1 / aspectRatio;
  1659. const column2Row2 = (far + near) / (near - far);
  1660. const column3Row2 = (2.0 * far * near) / (near - far);
  1661. result[0] = column0Row0;
  1662. result[1] = 0.0;
  1663. result[2] = 0.0;
  1664. result[3] = 0.0;
  1665. result[4] = 0.0;
  1666. result[5] = column1Row1;
  1667. result[6] = 0.0;
  1668. result[7] = 0.0;
  1669. result[8] = 0.0;
  1670. result[9] = 0.0;
  1671. result[10] = column2Row2;
  1672. result[11] = -1.0;
  1673. result[12] = 0.0;
  1674. result[13] = 0.0;
  1675. result[14] = column3Row2;
  1676. result[15] = 0.0;
  1677. return result;
  1678. };
  1679. /**
  1680. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  1681. *
  1682. * @param {number} left The number of meters to the left of the camera that will be in view.
  1683. * @param {number} right The number of meters to the right of the camera that will be in view.
  1684. * @param {number} bottom The number of meters below of the camera that will be in view.
  1685. * @param {number} top The number of meters above of the camera that will be in view.
  1686. * @param {number} near The distance to the near plane in meters.
  1687. * @param {number} far The distance to the far plane in meters.
  1688. * @param {Matrix4} result The object in which the result will be stored.
  1689. * @returns {Matrix4} The modified result parameter.
  1690. */
  1691. Matrix4.computeOrthographicOffCenter = function (
  1692. left,
  1693. right,
  1694. bottom,
  1695. top,
  1696. near,
  1697. far,
  1698. result
  1699. ) {
  1700. //>>includeStart('debug', pragmas.debug);
  1701. Check.Check.typeOf.number("left", left);
  1702. Check.Check.typeOf.number("right", right);
  1703. Check.Check.typeOf.number("bottom", bottom);
  1704. Check.Check.typeOf.number("top", top);
  1705. Check.Check.typeOf.number("near", near);
  1706. Check.Check.typeOf.number("far", far);
  1707. Check.Check.typeOf.object("result", result);
  1708. //>>includeEnd('debug');
  1709. let a = 1.0 / (right - left);
  1710. let b = 1.0 / (top - bottom);
  1711. let c = 1.0 / (far - near);
  1712. const tx = -(right + left) * a;
  1713. const ty = -(top + bottom) * b;
  1714. const tz = -(far + near) * c;
  1715. a *= 2.0;
  1716. b *= 2.0;
  1717. c *= -2.0;
  1718. result[0] = a;
  1719. result[1] = 0.0;
  1720. result[2] = 0.0;
  1721. result[3] = 0.0;
  1722. result[4] = 0.0;
  1723. result[5] = b;
  1724. result[6] = 0.0;
  1725. result[7] = 0.0;
  1726. result[8] = 0.0;
  1727. result[9] = 0.0;
  1728. result[10] = c;
  1729. result[11] = 0.0;
  1730. result[12] = tx;
  1731. result[13] = ty;
  1732. result[14] = tz;
  1733. result[15] = 1.0;
  1734. return result;
  1735. };
  1736. /**
  1737. * Computes a Matrix4 instance representing an off center perspective transformation.
  1738. *
  1739. * @param {number} left The number of meters to the left of the camera that will be in view.
  1740. * @param {number} right The number of meters to the right of the camera that will be in view.
  1741. * @param {number} bottom The number of meters below of the camera that will be in view.
  1742. * @param {number} top The number of meters above of the camera that will be in view.
  1743. * @param {number} near The distance to the near plane in meters.
  1744. * @param {number} far The distance to the far plane in meters.
  1745. * @param {Matrix4} result The object in which the result will be stored.
  1746. * @returns {Matrix4} The modified result parameter.
  1747. */
  1748. Matrix4.computePerspectiveOffCenter = function (
  1749. left,
  1750. right,
  1751. bottom,
  1752. top,
  1753. near,
  1754. far,
  1755. result
  1756. ) {
  1757. //>>includeStart('debug', pragmas.debug);
  1758. Check.Check.typeOf.number("left", left);
  1759. Check.Check.typeOf.number("right", right);
  1760. Check.Check.typeOf.number("bottom", bottom);
  1761. Check.Check.typeOf.number("top", top);
  1762. Check.Check.typeOf.number("near", near);
  1763. Check.Check.typeOf.number("far", far);
  1764. Check.Check.typeOf.object("result", result);
  1765. //>>includeEnd('debug');
  1766. const column0Row0 = (2.0 * near) / (right - left);
  1767. const column1Row1 = (2.0 * near) / (top - bottom);
  1768. const column2Row0 = (right + left) / (right - left);
  1769. const column2Row1 = (top + bottom) / (top - bottom);
  1770. const column2Row2 = -(far + near) / (far - near);
  1771. const column2Row3 = -1.0;
  1772. const column3Row2 = (-2.0 * far * near) / (far - near);
  1773. result[0] = column0Row0;
  1774. result[1] = 0.0;
  1775. result[2] = 0.0;
  1776. result[3] = 0.0;
  1777. result[4] = 0.0;
  1778. result[5] = column1Row1;
  1779. result[6] = 0.0;
  1780. result[7] = 0.0;
  1781. result[8] = column2Row0;
  1782. result[9] = column2Row1;
  1783. result[10] = column2Row2;
  1784. result[11] = column2Row3;
  1785. result[12] = 0.0;
  1786. result[13] = 0.0;
  1787. result[14] = column3Row2;
  1788. result[15] = 0.0;
  1789. return result;
  1790. };
  1791. /**
  1792. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  1793. *
  1794. * @param {number} left The number of meters to the left of the camera that will be in view.
  1795. * @param {number} right The number of meters to the right of the camera that will be in view.
  1796. * @param {number} bottom The number of meters below of the camera that will be in view.
  1797. * @param {number} top The number of meters above of the camera that will be in view.
  1798. * @param {number} near The distance to the near plane in meters.
  1799. * @param {Matrix4} result The object in which the result will be stored.
  1800. * @returns {Matrix4} The modified result parameter.
  1801. */
  1802. Matrix4.computeInfinitePerspectiveOffCenter = function (
  1803. left,
  1804. right,
  1805. bottom,
  1806. top,
  1807. near,
  1808. result
  1809. ) {
  1810. //>>includeStart('debug', pragmas.debug);
  1811. Check.Check.typeOf.number("left", left);
  1812. Check.Check.typeOf.number("right", right);
  1813. Check.Check.typeOf.number("bottom", bottom);
  1814. Check.Check.typeOf.number("top", top);
  1815. Check.Check.typeOf.number("near", near);
  1816. Check.Check.typeOf.object("result", result);
  1817. //>>includeEnd('debug');
  1818. const column0Row0 = (2.0 * near) / (right - left);
  1819. const column1Row1 = (2.0 * near) / (top - bottom);
  1820. const column2Row0 = (right + left) / (right - left);
  1821. const column2Row1 = (top + bottom) / (top - bottom);
  1822. const column2Row2 = -1.0;
  1823. const column2Row3 = -1.0;
  1824. const column3Row2 = -2.0 * near;
  1825. result[0] = column0Row0;
  1826. result[1] = 0.0;
  1827. result[2] = 0.0;
  1828. result[3] = 0.0;
  1829. result[4] = 0.0;
  1830. result[5] = column1Row1;
  1831. result[6] = 0.0;
  1832. result[7] = 0.0;
  1833. result[8] = column2Row0;
  1834. result[9] = column2Row1;
  1835. result[10] = column2Row2;
  1836. result[11] = column2Row3;
  1837. result[12] = 0.0;
  1838. result[13] = 0.0;
  1839. result[14] = column3Row2;
  1840. result[15] = 0.0;
  1841. return result;
  1842. };
  1843. /**
  1844. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  1845. *
  1846. * @param {object} [viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  1847. * @param {number} [nearDepthRange=0.0] The near plane distance in window coordinates.
  1848. * @param {number} [farDepthRange=1.0] The far plane distance in window coordinates.
  1849. * @param {Matrix4} [result] The object in which the result will be stored.
  1850. * @returns {Matrix4} The modified result parameter.
  1851. *
  1852. * @example
  1853. * // Create viewport transformation using an explicit viewport and depth range.
  1854. * const m = Cesium.Matrix4.computeViewportTransformation({
  1855. * x : 0.0,
  1856. * y : 0.0,
  1857. * width : 1024.0,
  1858. * height : 768.0
  1859. * }, 0.0, 1.0, new Cesium.Matrix4());
  1860. */
  1861. Matrix4.computeViewportTransformation = function (
  1862. viewport,
  1863. nearDepthRange,
  1864. farDepthRange,
  1865. result
  1866. ) {
  1867. if (!defaultValue.defined(result)) {
  1868. result = new Matrix4();
  1869. }
  1870. viewport = defaultValue.defaultValue(viewport, defaultValue.defaultValue.EMPTY_OBJECT);
  1871. const x = defaultValue.defaultValue(viewport.x, 0.0);
  1872. const y = defaultValue.defaultValue(viewport.y, 0.0);
  1873. const width = defaultValue.defaultValue(viewport.width, 0.0);
  1874. const height = defaultValue.defaultValue(viewport.height, 0.0);
  1875. nearDepthRange = defaultValue.defaultValue(nearDepthRange, 0.0);
  1876. farDepthRange = defaultValue.defaultValue(farDepthRange, 1.0);
  1877. const halfWidth = width * 0.5;
  1878. const halfHeight = height * 0.5;
  1879. const halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  1880. const column0Row0 = halfWidth;
  1881. const column1Row1 = halfHeight;
  1882. const column2Row2 = halfDepth;
  1883. const column3Row0 = x + halfWidth;
  1884. const column3Row1 = y + halfHeight;
  1885. const column3Row2 = nearDepthRange + halfDepth;
  1886. const column3Row3 = 1.0;
  1887. result[0] = column0Row0;
  1888. result[1] = 0.0;
  1889. result[2] = 0.0;
  1890. result[3] = 0.0;
  1891. result[4] = 0.0;
  1892. result[5] = column1Row1;
  1893. result[6] = 0.0;
  1894. result[7] = 0.0;
  1895. result[8] = 0.0;
  1896. result[9] = 0.0;
  1897. result[10] = column2Row2;
  1898. result[11] = 0.0;
  1899. result[12] = column3Row0;
  1900. result[13] = column3Row1;
  1901. result[14] = column3Row2;
  1902. result[15] = column3Row3;
  1903. return result;
  1904. };
  1905. /**
  1906. * Computes a Matrix4 instance that transforms from world space to view space.
  1907. *
  1908. * @param {Cartesian3} position The position of the camera.
  1909. * @param {Cartesian3} direction The forward direction.
  1910. * @param {Cartesian3} up The up direction.
  1911. * @param {Cartesian3} right The right direction.
  1912. * @param {Matrix4} result The object in which the result will be stored.
  1913. * @returns {Matrix4} The modified result parameter.
  1914. */
  1915. Matrix4.computeView = function (position, direction, up, right, result) {
  1916. //>>includeStart('debug', pragmas.debug);
  1917. Check.Check.typeOf.object("position", position);
  1918. Check.Check.typeOf.object("direction", direction);
  1919. Check.Check.typeOf.object("up", up);
  1920. Check.Check.typeOf.object("right", right);
  1921. Check.Check.typeOf.object("result", result);
  1922. //>>includeEnd('debug');
  1923. result[0] = right.x;
  1924. result[1] = up.x;
  1925. result[2] = -direction.x;
  1926. result[3] = 0.0;
  1927. result[4] = right.y;
  1928. result[5] = up.y;
  1929. result[6] = -direction.y;
  1930. result[7] = 0.0;
  1931. result[8] = right.z;
  1932. result[9] = up.z;
  1933. result[10] = -direction.z;
  1934. result[11] = 0.0;
  1935. result[12] = -Matrix3.Cartesian3.dot(right, position);
  1936. result[13] = -Matrix3.Cartesian3.dot(up, position);
  1937. result[14] = Matrix3.Cartesian3.dot(direction, position);
  1938. result[15] = 1.0;
  1939. return result;
  1940. };
  1941. /**
  1942. * Computes an Array from the provided Matrix4 instance.
  1943. * The array will be in column-major order.
  1944. *
  1945. * @param {Matrix4} matrix The matrix to use..
  1946. * @param {number[]} [result] The Array onto which to store the result.
  1947. * @returns {number[]} The modified Array parameter or a new Array instance if one was not provided.
  1948. *
  1949. * @example
  1950. * //create an array from an instance of Matrix4
  1951. * // m = [10.0, 14.0, 18.0, 22.0]
  1952. * // [11.0, 15.0, 19.0, 23.0]
  1953. * // [12.0, 16.0, 20.0, 24.0]
  1954. * // [13.0, 17.0, 21.0, 25.0]
  1955. * const a = Cesium.Matrix4.toArray(m);
  1956. *
  1957. * // m remains the same
  1958. * //creates a = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0]
  1959. */
  1960. Matrix4.toArray = function (matrix, result) {
  1961. //>>includeStart('debug', pragmas.debug);
  1962. Check.Check.typeOf.object("matrix", matrix);
  1963. //>>includeEnd('debug');
  1964. if (!defaultValue.defined(result)) {
  1965. return [
  1966. matrix[0],
  1967. matrix[1],
  1968. matrix[2],
  1969. matrix[3],
  1970. matrix[4],
  1971. matrix[5],
  1972. matrix[6],
  1973. matrix[7],
  1974. matrix[8],
  1975. matrix[9],
  1976. matrix[10],
  1977. matrix[11],
  1978. matrix[12],
  1979. matrix[13],
  1980. matrix[14],
  1981. matrix[15],
  1982. ];
  1983. }
  1984. result[0] = matrix[0];
  1985. result[1] = matrix[1];
  1986. result[2] = matrix[2];
  1987. result[3] = matrix[3];
  1988. result[4] = matrix[4];
  1989. result[5] = matrix[5];
  1990. result[6] = matrix[6];
  1991. result[7] = matrix[7];
  1992. result[8] = matrix[8];
  1993. result[9] = matrix[9];
  1994. result[10] = matrix[10];
  1995. result[11] = matrix[11];
  1996. result[12] = matrix[12];
  1997. result[13] = matrix[13];
  1998. result[14] = matrix[14];
  1999. result[15] = matrix[15];
  2000. return result;
  2001. };
  2002. /**
  2003. * Computes the array index of the element at the provided row and column.
  2004. *
  2005. * @param {number} row The zero-based index of the row.
  2006. * @param {number} column The zero-based index of the column.
  2007. * @returns {number} The index of the element at the provided row and column.
  2008. *
  2009. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  2010. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  2011. *
  2012. * @example
  2013. * const myMatrix = new Cesium.Matrix4();
  2014. * const column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  2015. * const column1Row0 = myMatrix[column1Row0Index];
  2016. * myMatrix[column1Row0Index] = 10.0;
  2017. */
  2018. Matrix4.getElementIndex = function (column, row) {
  2019. //>>includeStart('debug', pragmas.debug);
  2020. Check.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  2021. Check.Check.typeOf.number.lessThanOrEquals("row", row, 3);
  2022. Check.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  2023. Check.Check.typeOf.number.lessThanOrEquals("column", column, 3);
  2024. //>>includeEnd('debug');
  2025. return column * 4 + row;
  2026. };
  2027. /**
  2028. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  2029. *
  2030. * @param {Matrix4} matrix The matrix to use.
  2031. * @param {number} index The zero-based index of the column to retrieve.
  2032. * @param {Cartesian4} result The object onto which to store the result.
  2033. * @returns {Cartesian4} The modified result parameter.
  2034. *
  2035. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2036. *
  2037. * @example
  2038. * //returns a Cartesian4 instance with values from the specified column
  2039. * // m = [10.0, 11.0, 12.0, 13.0]
  2040. * // [14.0, 15.0, 16.0, 17.0]
  2041. * // [18.0, 19.0, 20.0, 21.0]
  2042. * // [22.0, 23.0, 24.0, 25.0]
  2043. *
  2044. * //Example 1: Creates an instance of Cartesian
  2045. * const a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  2046. *
  2047. * @example
  2048. * //Example 2: Sets values for Cartesian instance
  2049. * const a = new Cesium.Cartesian4();
  2050. * Cesium.Matrix4.getColumn(m, 2, a);
  2051. *
  2052. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  2053. */
  2054. Matrix4.getColumn = function (matrix, index, result) {
  2055. //>>includeStart('debug', pragmas.debug);
  2056. Check.Check.typeOf.object("matrix", matrix);
  2057. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2058. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  2059. Check.Check.typeOf.object("result", result);
  2060. //>>includeEnd('debug');
  2061. const startIndex = index * 4;
  2062. const x = matrix[startIndex];
  2063. const y = matrix[startIndex + 1];
  2064. const z = matrix[startIndex + 2];
  2065. const w = matrix[startIndex + 3];
  2066. result.x = x;
  2067. result.y = y;
  2068. result.z = z;
  2069. result.w = w;
  2070. return result;
  2071. };
  2072. /**
  2073. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  2074. *
  2075. * @param {Matrix4} matrix The matrix to use.
  2076. * @param {number} index The zero-based index of the column to set.
  2077. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  2078. * @param {Matrix4} result The object onto which to store the result.
  2079. * @returns {Matrix4} The modified result parameter.
  2080. *
  2081. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2082. *
  2083. * @example
  2084. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  2085. * // m = [10.0, 11.0, 12.0, 13.0]
  2086. * // [14.0, 15.0, 16.0, 17.0]
  2087. * // [18.0, 19.0, 20.0, 21.0]
  2088. * // [22.0, 23.0, 24.0, 25.0]
  2089. *
  2090. * const a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  2091. *
  2092. * // m remains the same
  2093. * // a = [10.0, 11.0, 99.0, 13.0]
  2094. * // [14.0, 15.0, 98.0, 17.0]
  2095. * // [18.0, 19.0, 97.0, 21.0]
  2096. * // [22.0, 23.0, 96.0, 25.0]
  2097. */
  2098. Matrix4.setColumn = function (matrix, index, cartesian, result) {
  2099. //>>includeStart('debug', pragmas.debug);
  2100. Check.Check.typeOf.object("matrix", matrix);
  2101. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2102. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  2103. Check.Check.typeOf.object("cartesian", cartesian);
  2104. Check.Check.typeOf.object("result", result);
  2105. //>>includeEnd('debug');
  2106. result = Matrix4.clone(matrix, result);
  2107. const startIndex = index * 4;
  2108. result[startIndex] = cartesian.x;
  2109. result[startIndex + 1] = cartesian.y;
  2110. result[startIndex + 2] = cartesian.z;
  2111. result[startIndex + 3] = cartesian.w;
  2112. return result;
  2113. };
  2114. /**
  2115. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  2116. *
  2117. * @param {Matrix4} matrix The matrix to use.
  2118. * @param {number} index The zero-based index of the row to retrieve.
  2119. * @param {Cartesian4} result The object onto which to store the result.
  2120. * @returns {Cartesian4} The modified result parameter.
  2121. *
  2122. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2123. *
  2124. * @example
  2125. * //returns a Cartesian4 instance with values from the specified column
  2126. * // m = [10.0, 11.0, 12.0, 13.0]
  2127. * // [14.0, 15.0, 16.0, 17.0]
  2128. * // [18.0, 19.0, 20.0, 21.0]
  2129. * // [22.0, 23.0, 24.0, 25.0]
  2130. *
  2131. * //Example 1: Returns an instance of Cartesian
  2132. * const a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  2133. *
  2134. * @example
  2135. * //Example 2: Sets values for a Cartesian instance
  2136. * const a = new Cesium.Cartesian4();
  2137. * Cesium.Matrix4.getRow(m, 2, a);
  2138. *
  2139. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  2140. */
  2141. Matrix4.getRow = function (matrix, index, result) {
  2142. //>>includeStart('debug', pragmas.debug);
  2143. Check.Check.typeOf.object("matrix", matrix);
  2144. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2145. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  2146. Check.Check.typeOf.object("result", result);
  2147. //>>includeEnd('debug');
  2148. const x = matrix[index];
  2149. const y = matrix[index + 4];
  2150. const z = matrix[index + 8];
  2151. const w = matrix[index + 12];
  2152. result.x = x;
  2153. result.y = y;
  2154. result.z = z;
  2155. result.w = w;
  2156. return result;
  2157. };
  2158. /**
  2159. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  2160. *
  2161. * @param {Matrix4} matrix The matrix to use.
  2162. * @param {number} index The zero-based index of the row to set.
  2163. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  2164. * @param {Matrix4} result The object onto which to store the result.
  2165. * @returns {Matrix4} The modified result parameter.
  2166. *
  2167. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2168. *
  2169. * @example
  2170. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  2171. * // m = [10.0, 11.0, 12.0, 13.0]
  2172. * // [14.0, 15.0, 16.0, 17.0]
  2173. * // [18.0, 19.0, 20.0, 21.0]
  2174. * // [22.0, 23.0, 24.0, 25.0]
  2175. *
  2176. * const a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  2177. *
  2178. * // m remains the same
  2179. * // a = [10.0, 11.0, 12.0, 13.0]
  2180. * // [14.0, 15.0, 16.0, 17.0]
  2181. * // [99.0, 98.0, 97.0, 96.0]
  2182. * // [22.0, 23.0, 24.0, 25.0]
  2183. */
  2184. Matrix4.setRow = function (matrix, index, cartesian, result) {
  2185. //>>includeStart('debug', pragmas.debug);
  2186. Check.Check.typeOf.object("matrix", matrix);
  2187. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2188. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  2189. Check.Check.typeOf.object("cartesian", cartesian);
  2190. Check.Check.typeOf.object("result", result);
  2191. //>>includeEnd('debug');
  2192. result = Matrix4.clone(matrix, result);
  2193. result[index] = cartesian.x;
  2194. result[index + 4] = cartesian.y;
  2195. result[index + 8] = cartesian.z;
  2196. result[index + 12] = cartesian.w;
  2197. return result;
  2198. };
  2199. /**
  2200. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  2201. * matrix with the provided translation. This assumes the matrix is an affine transformation.
  2202. *
  2203. * @param {Matrix4} matrix The matrix to use.
  2204. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  2205. * @param {Matrix4} result The object onto which to store the result.
  2206. * @returns {Matrix4} The modified result parameter.
  2207. */
  2208. Matrix4.setTranslation = function (matrix, translation, result) {
  2209. //>>includeStart('debug', pragmas.debug);
  2210. Check.Check.typeOf.object("matrix", matrix);
  2211. Check.Check.typeOf.object("translation", translation);
  2212. Check.Check.typeOf.object("result", result);
  2213. //>>includeEnd('debug');
  2214. result[0] = matrix[0];
  2215. result[1] = matrix[1];
  2216. result[2] = matrix[2];
  2217. result[3] = matrix[3];
  2218. result[4] = matrix[4];
  2219. result[5] = matrix[5];
  2220. result[6] = matrix[6];
  2221. result[7] = matrix[7];
  2222. result[8] = matrix[8];
  2223. result[9] = matrix[9];
  2224. result[10] = matrix[10];
  2225. result[11] = matrix[11];
  2226. result[12] = translation.x;
  2227. result[13] = translation.y;
  2228. result[14] = translation.z;
  2229. result[15] = matrix[15];
  2230. return result;
  2231. };
  2232. const scaleScratch1$1 = new Matrix3.Cartesian3();
  2233. /**
  2234. * Computes a new matrix that replaces the scale with the provided scale.
  2235. * This assumes the matrix is an affine transformation.
  2236. *
  2237. * @param {Matrix4} matrix The matrix to use.
  2238. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  2239. * @param {Matrix4} result The object onto which to store the result.
  2240. * @returns {Matrix4} The modified result parameter.
  2241. *
  2242. * @see Matrix4.setUniformScale
  2243. * @see Matrix4.fromScale
  2244. * @see Matrix4.fromUniformScale
  2245. * @see Matrix4.multiplyByScale
  2246. * @see Matrix4.multiplyByUniformScale
  2247. * @see Matrix4.getScale
  2248. */
  2249. Matrix4.setScale = function (matrix, scale, result) {
  2250. //>>includeStart('debug', pragmas.debug);
  2251. Check.Check.typeOf.object("matrix", matrix);
  2252. Check.Check.typeOf.object("scale", scale);
  2253. Check.Check.typeOf.object("result", result);
  2254. //>>includeEnd('debug');
  2255. const existingScale = Matrix4.getScale(matrix, scaleScratch1$1);
  2256. const scaleRatioX = scale.x / existingScale.x;
  2257. const scaleRatioY = scale.y / existingScale.y;
  2258. const scaleRatioZ = scale.z / existingScale.z;
  2259. result[0] = matrix[0] * scaleRatioX;
  2260. result[1] = matrix[1] * scaleRatioX;
  2261. result[2] = matrix[2] * scaleRatioX;
  2262. result[3] = matrix[3];
  2263. result[4] = matrix[4] * scaleRatioY;
  2264. result[5] = matrix[5] * scaleRatioY;
  2265. result[6] = matrix[6] * scaleRatioY;
  2266. result[7] = matrix[7];
  2267. result[8] = matrix[8] * scaleRatioZ;
  2268. result[9] = matrix[9] * scaleRatioZ;
  2269. result[10] = matrix[10] * scaleRatioZ;
  2270. result[11] = matrix[11];
  2271. result[12] = matrix[12];
  2272. result[13] = matrix[13];
  2273. result[14] = matrix[14];
  2274. result[15] = matrix[15];
  2275. return result;
  2276. };
  2277. const scaleScratch2$1 = new Matrix3.Cartesian3();
  2278. /**
  2279. * Computes a new matrix that replaces the scale with the provided uniform scale.
  2280. * This assumes the matrix is an affine transformation.
  2281. *
  2282. * @param {Matrix4} matrix The matrix to use.
  2283. * @param {number} scale The uniform scale that replaces the scale of the provided matrix.
  2284. * @param {Matrix4} result The object onto which to store the result.
  2285. * @returns {Matrix4} The modified result parameter.
  2286. *
  2287. * @see Matrix4.setScale
  2288. * @see Matrix4.fromScale
  2289. * @see Matrix4.fromUniformScale
  2290. * @see Matrix4.multiplyByScale
  2291. * @see Matrix4.multiplyByUniformScale
  2292. * @see Matrix4.getScale
  2293. */
  2294. Matrix4.setUniformScale = function (matrix, scale, result) {
  2295. //>>includeStart('debug', pragmas.debug);
  2296. Check.Check.typeOf.object("matrix", matrix);
  2297. Check.Check.typeOf.number("scale", scale);
  2298. Check.Check.typeOf.object("result", result);
  2299. //>>includeEnd('debug');
  2300. const existingScale = Matrix4.getScale(matrix, scaleScratch2$1);
  2301. const scaleRatioX = scale / existingScale.x;
  2302. const scaleRatioY = scale / existingScale.y;
  2303. const scaleRatioZ = scale / existingScale.z;
  2304. result[0] = matrix[0] * scaleRatioX;
  2305. result[1] = matrix[1] * scaleRatioX;
  2306. result[2] = matrix[2] * scaleRatioX;
  2307. result[3] = matrix[3];
  2308. result[4] = matrix[4] * scaleRatioY;
  2309. result[5] = matrix[5] * scaleRatioY;
  2310. result[6] = matrix[6] * scaleRatioY;
  2311. result[7] = matrix[7];
  2312. result[8] = matrix[8] * scaleRatioZ;
  2313. result[9] = matrix[9] * scaleRatioZ;
  2314. result[10] = matrix[10] * scaleRatioZ;
  2315. result[11] = matrix[11];
  2316. result[12] = matrix[12];
  2317. result[13] = matrix[13];
  2318. result[14] = matrix[14];
  2319. result[15] = matrix[15];
  2320. return result;
  2321. };
  2322. const scratchColumn$1 = new Matrix3.Cartesian3();
  2323. /**
  2324. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  2325. *
  2326. * @param {Matrix4} matrix The matrix.
  2327. * @param {Cartesian3} result The object onto which to store the result.
  2328. * @returns {Cartesian3} The modified result parameter
  2329. *
  2330. * @see Matrix4.multiplyByScale
  2331. * @see Matrix4.multiplyByUniformScale
  2332. * @see Matrix4.fromScale
  2333. * @see Matrix4.fromUniformScale
  2334. * @see Matrix4.setScale
  2335. * @see Matrix4.setUniformScale
  2336. */
  2337. Matrix4.getScale = function (matrix, result) {
  2338. //>>includeStart('debug', pragmas.debug);
  2339. Check.Check.typeOf.object("matrix", matrix);
  2340. Check.Check.typeOf.object("result", result);
  2341. //>>includeEnd('debug');
  2342. result.x = Matrix3.Cartesian3.magnitude(
  2343. Matrix3.Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$1)
  2344. );
  2345. result.y = Matrix3.Cartesian3.magnitude(
  2346. Matrix3.Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn$1)
  2347. );
  2348. result.z = Matrix3.Cartesian3.magnitude(
  2349. Matrix3.Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn$1)
  2350. );
  2351. return result;
  2352. };
  2353. const scaleScratch3$1 = new Matrix3.Cartesian3();
  2354. /**
  2355. * Computes the maximum scale assuming the matrix is an affine transformation.
  2356. * The maximum scale is the maximum length of the column vectors in the upper-left
  2357. * 3x3 matrix.
  2358. *
  2359. * @param {Matrix4} matrix The matrix.
  2360. * @returns {number} The maximum scale.
  2361. */
  2362. Matrix4.getMaximumScale = function (matrix) {
  2363. Matrix4.getScale(matrix, scaleScratch3$1);
  2364. return Matrix3.Cartesian3.maximumComponent(scaleScratch3$1);
  2365. };
  2366. const scaleScratch4$1 = new Matrix3.Cartesian3();
  2367. /**
  2368. * Sets the rotation assuming the matrix is an affine transformation.
  2369. *
  2370. * @param {Matrix4} matrix The matrix.
  2371. * @param {Matrix3} rotation The rotation matrix.
  2372. * @param {Matrix4} result The object onto which to store the result.
  2373. * @returns {Matrix4} The modified result parameter.
  2374. *
  2375. * @see Matrix4.fromRotation
  2376. * @see Matrix4.getRotation
  2377. */
  2378. Matrix4.setRotation = function (matrix, rotation, result) {
  2379. //>>includeStart('debug', pragmas.debug);
  2380. Check.Check.typeOf.object("matrix", matrix);
  2381. Check.Check.typeOf.object("result", result);
  2382. //>>includeEnd('debug');
  2383. const scale = Matrix4.getScale(matrix, scaleScratch4$1);
  2384. result[0] = rotation[0] * scale.x;
  2385. result[1] = rotation[1] * scale.x;
  2386. result[2] = rotation[2] * scale.x;
  2387. result[3] = matrix[3];
  2388. result[4] = rotation[3] * scale.y;
  2389. result[5] = rotation[4] * scale.y;
  2390. result[6] = rotation[5] * scale.y;
  2391. result[7] = matrix[7];
  2392. result[8] = rotation[6] * scale.z;
  2393. result[9] = rotation[7] * scale.z;
  2394. result[10] = rotation[8] * scale.z;
  2395. result[11] = matrix[11];
  2396. result[12] = matrix[12];
  2397. result[13] = matrix[13];
  2398. result[14] = matrix[14];
  2399. result[15] = matrix[15];
  2400. return result;
  2401. };
  2402. const scaleScratch5$1 = new Matrix3.Cartesian3();
  2403. /**
  2404. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  2405. *
  2406. * @param {Matrix4} matrix The matrix.
  2407. * @param {Matrix3} result The object onto which to store the result.
  2408. * @returns {Matrix3} The modified result parameter.
  2409. *
  2410. * @see Matrix4.setRotation
  2411. * @see Matrix4.fromRotation
  2412. */
  2413. Matrix4.getRotation = function (matrix, result) {
  2414. //>>includeStart('debug', pragmas.debug);
  2415. Check.Check.typeOf.object("matrix", matrix);
  2416. Check.Check.typeOf.object("result", result);
  2417. //>>includeEnd('debug');
  2418. const scale = Matrix4.getScale(matrix, scaleScratch5$1);
  2419. result[0] = matrix[0] / scale.x;
  2420. result[1] = matrix[1] / scale.x;
  2421. result[2] = matrix[2] / scale.x;
  2422. result[3] = matrix[4] / scale.y;
  2423. result[4] = matrix[5] / scale.y;
  2424. result[5] = matrix[6] / scale.y;
  2425. result[6] = matrix[8] / scale.z;
  2426. result[7] = matrix[9] / scale.z;
  2427. result[8] = matrix[10] / scale.z;
  2428. return result;
  2429. };
  2430. /**
  2431. * Computes the product of two matrices.
  2432. *
  2433. * @param {Matrix4} left The first matrix.
  2434. * @param {Matrix4} right The second matrix.
  2435. * @param {Matrix4} result The object onto which to store the result.
  2436. * @returns {Matrix4} The modified result parameter.
  2437. */
  2438. Matrix4.multiply = function (left, right, result) {
  2439. //>>includeStart('debug', pragmas.debug);
  2440. Check.Check.typeOf.object("left", left);
  2441. Check.Check.typeOf.object("right", right);
  2442. Check.Check.typeOf.object("result", result);
  2443. //>>includeEnd('debug');
  2444. const left0 = left[0];
  2445. const left1 = left[1];
  2446. const left2 = left[2];
  2447. const left3 = left[3];
  2448. const left4 = left[4];
  2449. const left5 = left[5];
  2450. const left6 = left[6];
  2451. const left7 = left[7];
  2452. const left8 = left[8];
  2453. const left9 = left[9];
  2454. const left10 = left[10];
  2455. const left11 = left[11];
  2456. const left12 = left[12];
  2457. const left13 = left[13];
  2458. const left14 = left[14];
  2459. const left15 = left[15];
  2460. const right0 = right[0];
  2461. const right1 = right[1];
  2462. const right2 = right[2];
  2463. const right3 = right[3];
  2464. const right4 = right[4];
  2465. const right5 = right[5];
  2466. const right6 = right[6];
  2467. const right7 = right[7];
  2468. const right8 = right[8];
  2469. const right9 = right[9];
  2470. const right10 = right[10];
  2471. const right11 = right[11];
  2472. const right12 = right[12];
  2473. const right13 = right[13];
  2474. const right14 = right[14];
  2475. const right15 = right[15];
  2476. const column0Row0 =
  2477. left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  2478. const column0Row1 =
  2479. left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  2480. const column0Row2 =
  2481. left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  2482. const column0Row3 =
  2483. left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  2484. const column1Row0 =
  2485. left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  2486. const column1Row1 =
  2487. left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  2488. const column1Row2 =
  2489. left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  2490. const column1Row3 =
  2491. left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  2492. const column2Row0 =
  2493. left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  2494. const column2Row1 =
  2495. left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  2496. const column2Row2 =
  2497. left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  2498. const column2Row3 =
  2499. left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  2500. const column3Row0 =
  2501. left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  2502. const column3Row1 =
  2503. left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  2504. const column3Row2 =
  2505. left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  2506. const column3Row3 =
  2507. left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  2508. result[0] = column0Row0;
  2509. result[1] = column0Row1;
  2510. result[2] = column0Row2;
  2511. result[3] = column0Row3;
  2512. result[4] = column1Row0;
  2513. result[5] = column1Row1;
  2514. result[6] = column1Row2;
  2515. result[7] = column1Row3;
  2516. result[8] = column2Row0;
  2517. result[9] = column2Row1;
  2518. result[10] = column2Row2;
  2519. result[11] = column2Row3;
  2520. result[12] = column3Row0;
  2521. result[13] = column3Row1;
  2522. result[14] = column3Row2;
  2523. result[15] = column3Row3;
  2524. return result;
  2525. };
  2526. /**
  2527. * Computes the sum of two matrices.
  2528. *
  2529. * @param {Matrix4} left The first matrix.
  2530. * @param {Matrix4} right The second matrix.
  2531. * @param {Matrix4} result The object onto which to store the result.
  2532. * @returns {Matrix4} The modified result parameter.
  2533. */
  2534. Matrix4.add = function (left, right, result) {
  2535. //>>includeStart('debug', pragmas.debug);
  2536. Check.Check.typeOf.object("left", left);
  2537. Check.Check.typeOf.object("right", right);
  2538. Check.Check.typeOf.object("result", result);
  2539. //>>includeEnd('debug');
  2540. result[0] = left[0] + right[0];
  2541. result[1] = left[1] + right[1];
  2542. result[2] = left[2] + right[2];
  2543. result[3] = left[3] + right[3];
  2544. result[4] = left[4] + right[4];
  2545. result[5] = left[5] + right[5];
  2546. result[6] = left[6] + right[6];
  2547. result[7] = left[7] + right[7];
  2548. result[8] = left[8] + right[8];
  2549. result[9] = left[9] + right[9];
  2550. result[10] = left[10] + right[10];
  2551. result[11] = left[11] + right[11];
  2552. result[12] = left[12] + right[12];
  2553. result[13] = left[13] + right[13];
  2554. result[14] = left[14] + right[14];
  2555. result[15] = left[15] + right[15];
  2556. return result;
  2557. };
  2558. /**
  2559. * Computes the difference of two matrices.
  2560. *
  2561. * @param {Matrix4} left The first matrix.
  2562. * @param {Matrix4} right The second matrix.
  2563. * @param {Matrix4} result The object onto which to store the result.
  2564. * @returns {Matrix4} The modified result parameter.
  2565. */
  2566. Matrix4.subtract = function (left, right, result) {
  2567. //>>includeStart('debug', pragmas.debug);
  2568. Check.Check.typeOf.object("left", left);
  2569. Check.Check.typeOf.object("right", right);
  2570. Check.Check.typeOf.object("result", result);
  2571. //>>includeEnd('debug');
  2572. result[0] = left[0] - right[0];
  2573. result[1] = left[1] - right[1];
  2574. result[2] = left[2] - right[2];
  2575. result[3] = left[3] - right[3];
  2576. result[4] = left[4] - right[4];
  2577. result[5] = left[5] - right[5];
  2578. result[6] = left[6] - right[6];
  2579. result[7] = left[7] - right[7];
  2580. result[8] = left[8] - right[8];
  2581. result[9] = left[9] - right[9];
  2582. result[10] = left[10] - right[10];
  2583. result[11] = left[11] - right[11];
  2584. result[12] = left[12] - right[12];
  2585. result[13] = left[13] - right[13];
  2586. result[14] = left[14] - right[14];
  2587. result[15] = left[15] - right[15];
  2588. return result;
  2589. };
  2590. /**
  2591. * Computes the product of two matrices assuming the matrices are affine transformation matrices,
  2592. * where the upper left 3x3 elements are any matrix, and
  2593. * the upper three elements in the fourth column are the translation.
  2594. * The bottom row is assumed to be [0, 0, 0, 1].
  2595. * The matrix is not verified to be in the proper form.
  2596. * This method is faster than computing the product for general 4x4
  2597. * matrices using {@link Matrix4.multiply}.
  2598. *
  2599. * @param {Matrix4} left The first matrix.
  2600. * @param {Matrix4} right The second matrix.
  2601. * @param {Matrix4} result The object onto which to store the result.
  2602. * @returns {Matrix4} The modified result parameter.
  2603. *
  2604. * @example
  2605. * const m1 = new Cesium.Matrix4(1.0, 6.0, 7.0, 0.0, 2.0, 5.0, 8.0, 0.0, 3.0, 4.0, 9.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  2606. * const m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  2607. * const m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  2608. */
  2609. Matrix4.multiplyTransformation = function (left, right, result) {
  2610. //>>includeStart('debug', pragmas.debug);
  2611. Check.Check.typeOf.object("left", left);
  2612. Check.Check.typeOf.object("right", right);
  2613. Check.Check.typeOf.object("result", result);
  2614. //>>includeEnd('debug');
  2615. const left0 = left[0];
  2616. const left1 = left[1];
  2617. const left2 = left[2];
  2618. const left4 = left[4];
  2619. const left5 = left[5];
  2620. const left6 = left[6];
  2621. const left8 = left[8];
  2622. const left9 = left[9];
  2623. const left10 = left[10];
  2624. const left12 = left[12];
  2625. const left13 = left[13];
  2626. const left14 = left[14];
  2627. const right0 = right[0];
  2628. const right1 = right[1];
  2629. const right2 = right[2];
  2630. const right4 = right[4];
  2631. const right5 = right[5];
  2632. const right6 = right[6];
  2633. const right8 = right[8];
  2634. const right9 = right[9];
  2635. const right10 = right[10];
  2636. const right12 = right[12];
  2637. const right13 = right[13];
  2638. const right14 = right[14];
  2639. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  2640. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  2641. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  2642. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  2643. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  2644. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  2645. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  2646. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  2647. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  2648. const column3Row0 =
  2649. left0 * right12 + left4 * right13 + left8 * right14 + left12;
  2650. const column3Row1 =
  2651. left1 * right12 + left5 * right13 + left9 * right14 + left13;
  2652. const column3Row2 =
  2653. left2 * right12 + left6 * right13 + left10 * right14 + left14;
  2654. result[0] = column0Row0;
  2655. result[1] = column0Row1;
  2656. result[2] = column0Row2;
  2657. result[3] = 0.0;
  2658. result[4] = column1Row0;
  2659. result[5] = column1Row1;
  2660. result[6] = column1Row2;
  2661. result[7] = 0.0;
  2662. result[8] = column2Row0;
  2663. result[9] = column2Row1;
  2664. result[10] = column2Row2;
  2665. result[11] = 0.0;
  2666. result[12] = column3Row0;
  2667. result[13] = column3Row1;
  2668. result[14] = column3Row2;
  2669. result[15] = 1.0;
  2670. return result;
  2671. };
  2672. /**
  2673. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  2674. * by a 3x3 rotation matrix. This is an optimization
  2675. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  2676. *
  2677. * @param {Matrix4} matrix The matrix on the left-hand side.
  2678. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  2679. * @param {Matrix4} result The object onto which to store the result.
  2680. * @returns {Matrix4} The modified result parameter.
  2681. *
  2682. * @example
  2683. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  2684. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  2685. */
  2686. Matrix4.multiplyByMatrix3 = function (matrix, rotation, result) {
  2687. //>>includeStart('debug', pragmas.debug);
  2688. Check.Check.typeOf.object("matrix", matrix);
  2689. Check.Check.typeOf.object("rotation", rotation);
  2690. Check.Check.typeOf.object("result", result);
  2691. //>>includeEnd('debug');
  2692. const left0 = matrix[0];
  2693. const left1 = matrix[1];
  2694. const left2 = matrix[2];
  2695. const left4 = matrix[4];
  2696. const left5 = matrix[5];
  2697. const left6 = matrix[6];
  2698. const left8 = matrix[8];
  2699. const left9 = matrix[9];
  2700. const left10 = matrix[10];
  2701. const right0 = rotation[0];
  2702. const right1 = rotation[1];
  2703. const right2 = rotation[2];
  2704. const right4 = rotation[3];
  2705. const right5 = rotation[4];
  2706. const right6 = rotation[5];
  2707. const right8 = rotation[6];
  2708. const right9 = rotation[7];
  2709. const right10 = rotation[8];
  2710. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  2711. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  2712. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  2713. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  2714. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  2715. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  2716. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  2717. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  2718. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  2719. result[0] = column0Row0;
  2720. result[1] = column0Row1;
  2721. result[2] = column0Row2;
  2722. result[3] = 0.0;
  2723. result[4] = column1Row0;
  2724. result[5] = column1Row1;
  2725. result[6] = column1Row2;
  2726. result[7] = 0.0;
  2727. result[8] = column2Row0;
  2728. result[9] = column2Row1;
  2729. result[10] = column2Row2;
  2730. result[11] = 0.0;
  2731. result[12] = matrix[12];
  2732. result[13] = matrix[13];
  2733. result[14] = matrix[14];
  2734. result[15] = matrix[15];
  2735. return result;
  2736. };
  2737. /**
  2738. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  2739. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  2740. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  2741. *
  2742. * @param {Matrix4} matrix The matrix on the left-hand side.
  2743. * @param {Cartesian3} translation The translation on the right-hand side.
  2744. * @param {Matrix4} result The object onto which to store the result.
  2745. * @returns {Matrix4} The modified result parameter.
  2746. *
  2747. * @example
  2748. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  2749. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  2750. */
  2751. Matrix4.multiplyByTranslation = function (matrix, translation, result) {
  2752. //>>includeStart('debug', pragmas.debug);
  2753. Check.Check.typeOf.object("matrix", matrix);
  2754. Check.Check.typeOf.object("translation", translation);
  2755. Check.Check.typeOf.object("result", result);
  2756. //>>includeEnd('debug');
  2757. const x = translation.x;
  2758. const y = translation.y;
  2759. const z = translation.z;
  2760. const tx = x * matrix[0] + y * matrix[4] + z * matrix[8] + matrix[12];
  2761. const ty = x * matrix[1] + y * matrix[5] + z * matrix[9] + matrix[13];
  2762. const tz = x * matrix[2] + y * matrix[6] + z * matrix[10] + matrix[14];
  2763. result[0] = matrix[0];
  2764. result[1] = matrix[1];
  2765. result[2] = matrix[2];
  2766. result[3] = matrix[3];
  2767. result[4] = matrix[4];
  2768. result[5] = matrix[5];
  2769. result[6] = matrix[6];
  2770. result[7] = matrix[7];
  2771. result[8] = matrix[8];
  2772. result[9] = matrix[9];
  2773. result[10] = matrix[10];
  2774. result[11] = matrix[11];
  2775. result[12] = tx;
  2776. result[13] = ty;
  2777. result[14] = tz;
  2778. result[15] = matrix[15];
  2779. return result;
  2780. };
  2781. /**
  2782. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  2783. * by an implicit non-uniform scale matrix. This is an optimization
  2784. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  2785. * <code>m</code> must be an affine matrix.
  2786. * This function performs fewer allocations and arithmetic operations.
  2787. *
  2788. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  2789. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  2790. * @param {Matrix4} result The object onto which to store the result.
  2791. * @returns {Matrix4} The modified result parameter.
  2792. *
  2793. *
  2794. * @example
  2795. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  2796. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  2797. *
  2798. * @see Matrix4.multiplyByUniformScale
  2799. * @see Matrix4.fromScale
  2800. * @see Matrix4.fromUniformScale
  2801. * @see Matrix4.setScale
  2802. * @see Matrix4.setUniformScale
  2803. * @see Matrix4.getScale
  2804. */
  2805. Matrix4.multiplyByScale = function (matrix, scale, result) {
  2806. //>>includeStart('debug', pragmas.debug);
  2807. Check.Check.typeOf.object("matrix", matrix);
  2808. Check.Check.typeOf.object("scale", scale);
  2809. Check.Check.typeOf.object("result", result);
  2810. //>>includeEnd('debug');
  2811. const scaleX = scale.x;
  2812. const scaleY = scale.y;
  2813. const scaleZ = scale.z;
  2814. // Faster than Cartesian3.equals
  2815. if (scaleX === 1.0 && scaleY === 1.0 && scaleZ === 1.0) {
  2816. return Matrix4.clone(matrix, result);
  2817. }
  2818. result[0] = scaleX * matrix[0];
  2819. result[1] = scaleX * matrix[1];
  2820. result[2] = scaleX * matrix[2];
  2821. result[3] = matrix[3];
  2822. result[4] = scaleY * matrix[4];
  2823. result[5] = scaleY * matrix[5];
  2824. result[6] = scaleY * matrix[6];
  2825. result[7] = matrix[7];
  2826. result[8] = scaleZ * matrix[8];
  2827. result[9] = scaleZ * matrix[9];
  2828. result[10] = scaleZ * matrix[10];
  2829. result[11] = matrix[11];
  2830. result[12] = matrix[12];
  2831. result[13] = matrix[13];
  2832. result[14] = matrix[14];
  2833. result[15] = matrix[15];
  2834. return result;
  2835. };
  2836. /**
  2837. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  2838. *
  2839. * @param {Matrix4} matrix The matrix on the left-hand side.
  2840. * @param {number} scale The uniform scale on the right-hand side.
  2841. * @param {Matrix4} result The object onto which to store the result.
  2842. * @returns {Matrix4} The modified result parameter.
  2843. *
  2844. * @example
  2845. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  2846. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  2847. *
  2848. * @see Matrix4.multiplyByScale
  2849. * @see Matrix4.fromScale
  2850. * @see Matrix4.fromUniformScale
  2851. * @see Matrix4.setScale
  2852. * @see Matrix4.setUniformScale
  2853. * @see Matrix4.getScale
  2854. */
  2855. Matrix4.multiplyByUniformScale = function (matrix, scale, result) {
  2856. //>>includeStart('debug', pragmas.debug);
  2857. Check.Check.typeOf.object("matrix", matrix);
  2858. Check.Check.typeOf.number("scale", scale);
  2859. Check.Check.typeOf.object("result", result);
  2860. //>>includeEnd('debug');
  2861. result[0] = matrix[0] * scale;
  2862. result[1] = matrix[1] * scale;
  2863. result[2] = matrix[2] * scale;
  2864. result[3] = matrix[3];
  2865. result[4] = matrix[4] * scale;
  2866. result[5] = matrix[5] * scale;
  2867. result[6] = matrix[6] * scale;
  2868. result[7] = matrix[7];
  2869. result[8] = matrix[8] * scale;
  2870. result[9] = matrix[9] * scale;
  2871. result[10] = matrix[10] * scale;
  2872. result[11] = matrix[11];
  2873. result[12] = matrix[12];
  2874. result[13] = matrix[13];
  2875. result[14] = matrix[14];
  2876. result[15] = matrix[15];
  2877. return result;
  2878. };
  2879. /**
  2880. * Computes the product of a matrix and a column vector.
  2881. *
  2882. * @param {Matrix4} matrix The matrix.
  2883. * @param {Cartesian4} cartesian The vector.
  2884. * @param {Cartesian4} result The object onto which to store the result.
  2885. * @returns {Cartesian4} The modified result parameter.
  2886. */
  2887. Matrix4.multiplyByVector = function (matrix, cartesian, result) {
  2888. //>>includeStart('debug', pragmas.debug);
  2889. Check.Check.typeOf.object("matrix", matrix);
  2890. Check.Check.typeOf.object("cartesian", cartesian);
  2891. Check.Check.typeOf.object("result", result);
  2892. //>>includeEnd('debug');
  2893. const vX = cartesian.x;
  2894. const vY = cartesian.y;
  2895. const vZ = cartesian.z;
  2896. const vW = cartesian.w;
  2897. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  2898. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  2899. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  2900. const w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  2901. result.x = x;
  2902. result.y = y;
  2903. result.z = z;
  2904. result.w = w;
  2905. return result;
  2906. };
  2907. /**
  2908. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  2909. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  2910. *
  2911. * @param {Matrix4} matrix The matrix.
  2912. * @param {Cartesian3} cartesian The point.
  2913. * @param {Cartesian3} result The object onto which to store the result.
  2914. * @returns {Cartesian3} The modified result parameter.
  2915. *
  2916. * @example
  2917. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  2918. * const result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  2919. * // A shortcut for
  2920. * // Cartesian3 p = ...
  2921. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  2922. */
  2923. Matrix4.multiplyByPointAsVector = function (matrix, cartesian, result) {
  2924. //>>includeStart('debug', pragmas.debug);
  2925. Check.Check.typeOf.object("matrix", matrix);
  2926. Check.Check.typeOf.object("cartesian", cartesian);
  2927. Check.Check.typeOf.object("result", result);
  2928. //>>includeEnd('debug');
  2929. const vX = cartesian.x;
  2930. const vY = cartesian.y;
  2931. const vZ = cartesian.z;
  2932. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  2933. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  2934. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  2935. result.x = x;
  2936. result.y = y;
  2937. result.z = z;
  2938. return result;
  2939. };
  2940. /**
  2941. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  2942. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  2943. *
  2944. * @param {Matrix4} matrix The matrix.
  2945. * @param {Cartesian3} cartesian The point.
  2946. * @param {Cartesian3} result The object onto which to store the result.
  2947. * @returns {Cartesian3} The modified result parameter.
  2948. *
  2949. * @example
  2950. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  2951. * const result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  2952. */
  2953. Matrix4.multiplyByPoint = function (matrix, cartesian, result) {
  2954. //>>includeStart('debug', pragmas.debug);
  2955. Check.Check.typeOf.object("matrix", matrix);
  2956. Check.Check.typeOf.object("cartesian", cartesian);
  2957. Check.Check.typeOf.object("result", result);
  2958. //>>includeEnd('debug');
  2959. const vX = cartesian.x;
  2960. const vY = cartesian.y;
  2961. const vZ = cartesian.z;
  2962. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  2963. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  2964. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  2965. result.x = x;
  2966. result.y = y;
  2967. result.z = z;
  2968. return result;
  2969. };
  2970. /**
  2971. * Computes the product of a matrix and a scalar.
  2972. *
  2973. * @param {Matrix4} matrix The matrix.
  2974. * @param {number} scalar The number to multiply by.
  2975. * @param {Matrix4} result The object onto which to store the result.
  2976. * @returns {Matrix4} The modified result parameter.
  2977. *
  2978. * @example
  2979. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  2980. * // m = [10.0, 11.0, 12.0, 13.0]
  2981. * // [14.0, 15.0, 16.0, 17.0]
  2982. * // [18.0, 19.0, 20.0, 21.0]
  2983. * // [22.0, 23.0, 24.0, 25.0]
  2984. *
  2985. * const a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  2986. *
  2987. * // m remains the same
  2988. * // a = [-20.0, -22.0, -24.0, -26.0]
  2989. * // [-28.0, -30.0, -32.0, -34.0]
  2990. * // [-36.0, -38.0, -40.0, -42.0]
  2991. * // [-44.0, -46.0, -48.0, -50.0]
  2992. */
  2993. Matrix4.multiplyByScalar = function (matrix, scalar, result) {
  2994. //>>includeStart('debug', pragmas.debug);
  2995. Check.Check.typeOf.object("matrix", matrix);
  2996. Check.Check.typeOf.number("scalar", scalar);
  2997. Check.Check.typeOf.object("result", result);
  2998. //>>includeEnd('debug');
  2999. result[0] = matrix[0] * scalar;
  3000. result[1] = matrix[1] * scalar;
  3001. result[2] = matrix[2] * scalar;
  3002. result[3] = matrix[3] * scalar;
  3003. result[4] = matrix[4] * scalar;
  3004. result[5] = matrix[5] * scalar;
  3005. result[6] = matrix[6] * scalar;
  3006. result[7] = matrix[7] * scalar;
  3007. result[8] = matrix[8] * scalar;
  3008. result[9] = matrix[9] * scalar;
  3009. result[10] = matrix[10] * scalar;
  3010. result[11] = matrix[11] * scalar;
  3011. result[12] = matrix[12] * scalar;
  3012. result[13] = matrix[13] * scalar;
  3013. result[14] = matrix[14] * scalar;
  3014. result[15] = matrix[15] * scalar;
  3015. return result;
  3016. };
  3017. /**
  3018. * Computes a negated copy of the provided matrix.
  3019. *
  3020. * @param {Matrix4} matrix The matrix to negate.
  3021. * @param {Matrix4} result The object onto which to store the result.
  3022. * @returns {Matrix4} The modified result parameter.
  3023. *
  3024. * @example
  3025. * //create a new Matrix4 instance which is a negation of a Matrix4
  3026. * // m = [10.0, 11.0, 12.0, 13.0]
  3027. * // [14.0, 15.0, 16.0, 17.0]
  3028. * // [18.0, 19.0, 20.0, 21.0]
  3029. * // [22.0, 23.0, 24.0, 25.0]
  3030. *
  3031. * const a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  3032. *
  3033. * // m remains the same
  3034. * // a = [-10.0, -11.0, -12.0, -13.0]
  3035. * // [-14.0, -15.0, -16.0, -17.0]
  3036. * // [-18.0, -19.0, -20.0, -21.0]
  3037. * // [-22.0, -23.0, -24.0, -25.0]
  3038. */
  3039. Matrix4.negate = function (matrix, result) {
  3040. //>>includeStart('debug', pragmas.debug);
  3041. Check.Check.typeOf.object("matrix", matrix);
  3042. Check.Check.typeOf.object("result", result);
  3043. //>>includeEnd('debug');
  3044. result[0] = -matrix[0];
  3045. result[1] = -matrix[1];
  3046. result[2] = -matrix[2];
  3047. result[3] = -matrix[3];
  3048. result[4] = -matrix[4];
  3049. result[5] = -matrix[5];
  3050. result[6] = -matrix[6];
  3051. result[7] = -matrix[7];
  3052. result[8] = -matrix[8];
  3053. result[9] = -matrix[9];
  3054. result[10] = -matrix[10];
  3055. result[11] = -matrix[11];
  3056. result[12] = -matrix[12];
  3057. result[13] = -matrix[13];
  3058. result[14] = -matrix[14];
  3059. result[15] = -matrix[15];
  3060. return result;
  3061. };
  3062. /**
  3063. * Computes the transpose of the provided matrix.
  3064. *
  3065. * @param {Matrix4} matrix The matrix to transpose.
  3066. * @param {Matrix4} result The object onto which to store the result.
  3067. * @returns {Matrix4} The modified result parameter.
  3068. *
  3069. * @example
  3070. * //returns transpose of a Matrix4
  3071. * // m = [10.0, 11.0, 12.0, 13.0]
  3072. * // [14.0, 15.0, 16.0, 17.0]
  3073. * // [18.0, 19.0, 20.0, 21.0]
  3074. * // [22.0, 23.0, 24.0, 25.0]
  3075. *
  3076. * const a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  3077. *
  3078. * // m remains the same
  3079. * // a = [10.0, 14.0, 18.0, 22.0]
  3080. * // [11.0, 15.0, 19.0, 23.0]
  3081. * // [12.0, 16.0, 20.0, 24.0]
  3082. * // [13.0, 17.0, 21.0, 25.0]
  3083. */
  3084. Matrix4.transpose = function (matrix, result) {
  3085. //>>includeStart('debug', pragmas.debug);
  3086. Check.Check.typeOf.object("matrix", matrix);
  3087. Check.Check.typeOf.object("result", result);
  3088. //>>includeEnd('debug');
  3089. const matrix1 = matrix[1];
  3090. const matrix2 = matrix[2];
  3091. const matrix3 = matrix[3];
  3092. const matrix6 = matrix[6];
  3093. const matrix7 = matrix[7];
  3094. const matrix11 = matrix[11];
  3095. result[0] = matrix[0];
  3096. result[1] = matrix[4];
  3097. result[2] = matrix[8];
  3098. result[3] = matrix[12];
  3099. result[4] = matrix1;
  3100. result[5] = matrix[5];
  3101. result[6] = matrix[9];
  3102. result[7] = matrix[13];
  3103. result[8] = matrix2;
  3104. result[9] = matrix6;
  3105. result[10] = matrix[10];
  3106. result[11] = matrix[14];
  3107. result[12] = matrix3;
  3108. result[13] = matrix7;
  3109. result[14] = matrix11;
  3110. result[15] = matrix[15];
  3111. return result;
  3112. };
  3113. /**
  3114. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  3115. *
  3116. * @param {Matrix4} matrix The matrix with signed elements.
  3117. * @param {Matrix4} result The object onto which to store the result.
  3118. * @returns {Matrix4} The modified result parameter.
  3119. */
  3120. Matrix4.abs = function (matrix, result) {
  3121. //>>includeStart('debug', pragmas.debug);
  3122. Check.Check.typeOf.object("matrix", matrix);
  3123. Check.Check.typeOf.object("result", result);
  3124. //>>includeEnd('debug');
  3125. result[0] = Math.abs(matrix[0]);
  3126. result[1] = Math.abs(matrix[1]);
  3127. result[2] = Math.abs(matrix[2]);
  3128. result[3] = Math.abs(matrix[3]);
  3129. result[4] = Math.abs(matrix[4]);
  3130. result[5] = Math.abs(matrix[5]);
  3131. result[6] = Math.abs(matrix[6]);
  3132. result[7] = Math.abs(matrix[7]);
  3133. result[8] = Math.abs(matrix[8]);
  3134. result[9] = Math.abs(matrix[9]);
  3135. result[10] = Math.abs(matrix[10]);
  3136. result[11] = Math.abs(matrix[11]);
  3137. result[12] = Math.abs(matrix[12]);
  3138. result[13] = Math.abs(matrix[13]);
  3139. result[14] = Math.abs(matrix[14]);
  3140. result[15] = Math.abs(matrix[15]);
  3141. return result;
  3142. };
  3143. /**
  3144. * Compares the provided matrices componentwise and returns
  3145. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3146. *
  3147. * @param {Matrix4} [left] The first matrix.
  3148. * @param {Matrix4} [right] The second matrix.
  3149. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3150. *
  3151. * @example
  3152. * //compares two Matrix4 instances
  3153. *
  3154. * // a = [10.0, 14.0, 18.0, 22.0]
  3155. * // [11.0, 15.0, 19.0, 23.0]
  3156. * // [12.0, 16.0, 20.0, 24.0]
  3157. * // [13.0, 17.0, 21.0, 25.0]
  3158. *
  3159. * // b = [10.0, 14.0, 18.0, 22.0]
  3160. * // [11.0, 15.0, 19.0, 23.0]
  3161. * // [12.0, 16.0, 20.0, 24.0]
  3162. * // [13.0, 17.0, 21.0, 25.0]
  3163. *
  3164. * if(Cesium.Matrix4.equals(a,b)) {
  3165. * console.log("Both matrices are equal");
  3166. * } else {
  3167. * console.log("They are not equal");
  3168. * }
  3169. *
  3170. * //Prints "Both matrices are equal" on the console
  3171. */
  3172. Matrix4.equals = function (left, right) {
  3173. // Given that most matrices will be transformation matrices, the elements
  3174. // are tested in order such that the test is likely to fail as early
  3175. // as possible. I _think_ this is just as friendly to the L1 cache
  3176. // as testing in index order. It is certainty faster in practice.
  3177. return (
  3178. left === right ||
  3179. (defaultValue.defined(left) &&
  3180. defaultValue.defined(right) &&
  3181. // Translation
  3182. left[12] === right[12] &&
  3183. left[13] === right[13] &&
  3184. left[14] === right[14] &&
  3185. // Rotation/scale
  3186. left[0] === right[0] &&
  3187. left[1] === right[1] &&
  3188. left[2] === right[2] &&
  3189. left[4] === right[4] &&
  3190. left[5] === right[5] &&
  3191. left[6] === right[6] &&
  3192. left[8] === right[8] &&
  3193. left[9] === right[9] &&
  3194. left[10] === right[10] &&
  3195. // Bottom row
  3196. left[3] === right[3] &&
  3197. left[7] === right[7] &&
  3198. left[11] === right[11] &&
  3199. left[15] === right[15])
  3200. );
  3201. };
  3202. /**
  3203. * Compares the provided matrices componentwise and returns
  3204. * <code>true</code> if they are within the provided epsilon,
  3205. * <code>false</code> otherwise.
  3206. *
  3207. * @param {Matrix4} [left] The first matrix.
  3208. * @param {Matrix4} [right] The second matrix.
  3209. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  3210. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3211. *
  3212. * @example
  3213. * //compares two Matrix4 instances
  3214. *
  3215. * // a = [10.5, 14.5, 18.5, 22.5]
  3216. * // [11.5, 15.5, 19.5, 23.5]
  3217. * // [12.5, 16.5, 20.5, 24.5]
  3218. * // [13.5, 17.5, 21.5, 25.5]
  3219. *
  3220. * // b = [10.0, 14.0, 18.0, 22.0]
  3221. * // [11.0, 15.0, 19.0, 23.0]
  3222. * // [12.0, 16.0, 20.0, 24.0]
  3223. * // [13.0, 17.0, 21.0, 25.0]
  3224. *
  3225. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  3226. * console.log("Difference between both the matrices is less than 0.1");
  3227. * } else {
  3228. * console.log("Difference between both the matrices is not less than 0.1");
  3229. * }
  3230. *
  3231. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  3232. */
  3233. Matrix4.equalsEpsilon = function (left, right, epsilon) {
  3234. epsilon = defaultValue.defaultValue(epsilon, 0);
  3235. return (
  3236. left === right ||
  3237. (defaultValue.defined(left) &&
  3238. defaultValue.defined(right) &&
  3239. Math.abs(left[0] - right[0]) <= epsilon &&
  3240. Math.abs(left[1] - right[1]) <= epsilon &&
  3241. Math.abs(left[2] - right[2]) <= epsilon &&
  3242. Math.abs(left[3] - right[3]) <= epsilon &&
  3243. Math.abs(left[4] - right[4]) <= epsilon &&
  3244. Math.abs(left[5] - right[5]) <= epsilon &&
  3245. Math.abs(left[6] - right[6]) <= epsilon &&
  3246. Math.abs(left[7] - right[7]) <= epsilon &&
  3247. Math.abs(left[8] - right[8]) <= epsilon &&
  3248. Math.abs(left[9] - right[9]) <= epsilon &&
  3249. Math.abs(left[10] - right[10]) <= epsilon &&
  3250. Math.abs(left[11] - right[11]) <= epsilon &&
  3251. Math.abs(left[12] - right[12]) <= epsilon &&
  3252. Math.abs(left[13] - right[13]) <= epsilon &&
  3253. Math.abs(left[14] - right[14]) <= epsilon &&
  3254. Math.abs(left[15] - right[15]) <= epsilon)
  3255. );
  3256. };
  3257. /**
  3258. * Gets the translation portion of the provided matrix, assuming the matrix is an affine transformation matrix.
  3259. *
  3260. * @param {Matrix4} matrix The matrix to use.
  3261. * @param {Cartesian3} result The object onto which to store the result.
  3262. * @returns {Cartesian3} The modified result parameter.
  3263. */
  3264. Matrix4.getTranslation = function (matrix, result) {
  3265. //>>includeStart('debug', pragmas.debug);
  3266. Check.Check.typeOf.object("matrix", matrix);
  3267. Check.Check.typeOf.object("result", result);
  3268. //>>includeEnd('debug');
  3269. result.x = matrix[12];
  3270. result.y = matrix[13];
  3271. result.z = matrix[14];
  3272. return result;
  3273. };
  3274. /**
  3275. * Gets the upper left 3x3 matrix of the provided matrix.
  3276. *
  3277. * @param {Matrix4} matrix The matrix to use.
  3278. * @param {Matrix3} result The object onto which to store the result.
  3279. * @returns {Matrix3} The modified result parameter.
  3280. *
  3281. * @example
  3282. * // returns a Matrix3 instance from a Matrix4 instance
  3283. *
  3284. * // m = [10.0, 14.0, 18.0, 22.0]
  3285. * // [11.0, 15.0, 19.0, 23.0]
  3286. * // [12.0, 16.0, 20.0, 24.0]
  3287. * // [13.0, 17.0, 21.0, 25.0]
  3288. *
  3289. * const b = new Cesium.Matrix3();
  3290. * Cesium.Matrix4.getMatrix3(m,b);
  3291. *
  3292. * // b = [10.0, 14.0, 18.0]
  3293. * // [11.0, 15.0, 19.0]
  3294. * // [12.0, 16.0, 20.0]
  3295. */
  3296. Matrix4.getMatrix3 = function (matrix, result) {
  3297. //>>includeStart('debug', pragmas.debug);
  3298. Check.Check.typeOf.object("matrix", matrix);
  3299. Check.Check.typeOf.object("result", result);
  3300. //>>includeEnd('debug');
  3301. result[0] = matrix[0];
  3302. result[1] = matrix[1];
  3303. result[2] = matrix[2];
  3304. result[3] = matrix[4];
  3305. result[4] = matrix[5];
  3306. result[5] = matrix[6];
  3307. result[6] = matrix[8];
  3308. result[7] = matrix[9];
  3309. result[8] = matrix[10];
  3310. return result;
  3311. };
  3312. const scratchInverseRotation = new Matrix3.Matrix3();
  3313. const scratchMatrix3Zero = new Matrix3.Matrix3();
  3314. const scratchBottomRow = new Cartesian4();
  3315. const scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  3316. /**
  3317. * Computes the inverse of the provided matrix using Cramers Rule.
  3318. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  3319. * If the matrix is a proper rigid transformation, it is more efficient
  3320. * to invert it with {@link Matrix4.inverseTransformation}.
  3321. *
  3322. * @param {Matrix4} matrix The matrix to invert.
  3323. * @param {Matrix4} result The object onto which to store the result.
  3324. * @returns {Matrix4} The modified result parameter.
  3325. *
  3326. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  3327. */
  3328. Matrix4.inverse = function (matrix, result) {
  3329. //>>includeStart('debug', pragmas.debug);
  3330. Check.Check.typeOf.object("matrix", matrix);
  3331. Check.Check.typeOf.object("result", result);
  3332. //>>includeEnd('debug');
  3333. //
  3334. // Ported from:
  3335. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  3336. //
  3337. const src0 = matrix[0];
  3338. const src1 = matrix[4];
  3339. const src2 = matrix[8];
  3340. const src3 = matrix[12];
  3341. const src4 = matrix[1];
  3342. const src5 = matrix[5];
  3343. const src6 = matrix[9];
  3344. const src7 = matrix[13];
  3345. const src8 = matrix[2];
  3346. const src9 = matrix[6];
  3347. const src10 = matrix[10];
  3348. const src11 = matrix[14];
  3349. const src12 = matrix[3];
  3350. const src13 = matrix[7];
  3351. const src14 = matrix[11];
  3352. const src15 = matrix[15];
  3353. // calculate pairs for first 8 elements (cofactors)
  3354. let tmp0 = src10 * src15;
  3355. let tmp1 = src11 * src14;
  3356. let tmp2 = src9 * src15;
  3357. let tmp3 = src11 * src13;
  3358. let tmp4 = src9 * src14;
  3359. let tmp5 = src10 * src13;
  3360. let tmp6 = src8 * src15;
  3361. let tmp7 = src11 * src12;
  3362. let tmp8 = src8 * src14;
  3363. let tmp9 = src10 * src12;
  3364. let tmp10 = src8 * src13;
  3365. let tmp11 = src9 * src12;
  3366. // calculate first 8 elements (cofactors)
  3367. const dst0 =
  3368. tmp0 * src5 +
  3369. tmp3 * src6 +
  3370. tmp4 * src7 -
  3371. (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  3372. const dst1 =
  3373. tmp1 * src4 +
  3374. tmp6 * src6 +
  3375. tmp9 * src7 -
  3376. (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  3377. const dst2 =
  3378. tmp2 * src4 +
  3379. tmp7 * src5 +
  3380. tmp10 * src7 -
  3381. (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  3382. const dst3 =
  3383. tmp5 * src4 +
  3384. tmp8 * src5 +
  3385. tmp11 * src6 -
  3386. (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  3387. const dst4 =
  3388. tmp1 * src1 +
  3389. tmp2 * src2 +
  3390. tmp5 * src3 -
  3391. (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  3392. const dst5 =
  3393. tmp0 * src0 +
  3394. tmp7 * src2 +
  3395. tmp8 * src3 -
  3396. (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  3397. const dst6 =
  3398. tmp3 * src0 +
  3399. tmp6 * src1 +
  3400. tmp11 * src3 -
  3401. (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  3402. const dst7 =
  3403. tmp4 * src0 +
  3404. tmp9 * src1 +
  3405. tmp10 * src2 -
  3406. (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  3407. // calculate pairs for second 8 elements (cofactors)
  3408. tmp0 = src2 * src7;
  3409. tmp1 = src3 * src6;
  3410. tmp2 = src1 * src7;
  3411. tmp3 = src3 * src5;
  3412. tmp4 = src1 * src6;
  3413. tmp5 = src2 * src5;
  3414. tmp6 = src0 * src7;
  3415. tmp7 = src3 * src4;
  3416. tmp8 = src0 * src6;
  3417. tmp9 = src2 * src4;
  3418. tmp10 = src0 * src5;
  3419. tmp11 = src1 * src4;
  3420. // calculate second 8 elements (cofactors)
  3421. const dst8 =
  3422. tmp0 * src13 +
  3423. tmp3 * src14 +
  3424. tmp4 * src15 -
  3425. (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  3426. const dst9 =
  3427. tmp1 * src12 +
  3428. tmp6 * src14 +
  3429. tmp9 * src15 -
  3430. (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  3431. const dst10 =
  3432. tmp2 * src12 +
  3433. tmp7 * src13 +
  3434. tmp10 * src15 -
  3435. (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  3436. const dst11 =
  3437. tmp5 * src12 +
  3438. tmp8 * src13 +
  3439. tmp11 * src14 -
  3440. (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  3441. const dst12 =
  3442. tmp2 * src10 +
  3443. tmp5 * src11 +
  3444. tmp1 * src9 -
  3445. (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  3446. const dst13 =
  3447. tmp8 * src11 +
  3448. tmp0 * src8 +
  3449. tmp7 * src10 -
  3450. (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  3451. const dst14 =
  3452. tmp6 * src9 +
  3453. tmp11 * src11 +
  3454. tmp3 * src8 -
  3455. (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  3456. const dst15 =
  3457. tmp10 * src10 +
  3458. tmp4 * src8 +
  3459. tmp9 * src9 -
  3460. (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  3461. // calculate determinant
  3462. let det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  3463. if (Math.abs(det) < Math$1.CesiumMath.EPSILON21) {
  3464. // Special case for a zero scale matrix that can occur, for example,
  3465. // when a model's node has a [0, 0, 0] scale.
  3466. if (
  3467. Matrix3.Matrix3.equalsEpsilon(
  3468. Matrix4.getMatrix3(matrix, scratchInverseRotation),
  3469. scratchMatrix3Zero,
  3470. Math$1.CesiumMath.EPSILON7
  3471. ) &&
  3472. Cartesian4.equals(
  3473. Matrix4.getRow(matrix, 3, scratchBottomRow),
  3474. scratchExpectedBottomRow
  3475. )
  3476. ) {
  3477. result[0] = 0.0;
  3478. result[1] = 0.0;
  3479. result[2] = 0.0;
  3480. result[3] = 0.0;
  3481. result[4] = 0.0;
  3482. result[5] = 0.0;
  3483. result[6] = 0.0;
  3484. result[7] = 0.0;
  3485. result[8] = 0.0;
  3486. result[9] = 0.0;
  3487. result[10] = 0.0;
  3488. result[11] = 0.0;
  3489. result[12] = -matrix[12];
  3490. result[13] = -matrix[13];
  3491. result[14] = -matrix[14];
  3492. result[15] = 1.0;
  3493. return result;
  3494. }
  3495. throw new RuntimeError.RuntimeError(
  3496. "matrix is not invertible because its determinate is zero."
  3497. );
  3498. }
  3499. // calculate matrix inverse
  3500. det = 1.0 / det;
  3501. result[0] = dst0 * det;
  3502. result[1] = dst1 * det;
  3503. result[2] = dst2 * det;
  3504. result[3] = dst3 * det;
  3505. result[4] = dst4 * det;
  3506. result[5] = dst5 * det;
  3507. result[6] = dst6 * det;
  3508. result[7] = dst7 * det;
  3509. result[8] = dst8 * det;
  3510. result[9] = dst9 * det;
  3511. result[10] = dst10 * det;
  3512. result[11] = dst11 * det;
  3513. result[12] = dst12 * det;
  3514. result[13] = dst13 * det;
  3515. result[14] = dst14 * det;
  3516. result[15] = dst15 * det;
  3517. return result;
  3518. };
  3519. /**
  3520. * Computes the inverse of the provided matrix assuming it is a proper rigid matrix,
  3521. * where the upper left 3x3 elements are a rotation matrix,
  3522. * and the upper three elements in the fourth column are the translation.
  3523. * The bottom row is assumed to be [0, 0, 0, 1].
  3524. * The matrix is not verified to be in the proper form.
  3525. * This method is faster than computing the inverse for a general 4x4
  3526. * matrix using {@link Matrix4.inverse}.
  3527. *
  3528. * @param {Matrix4} matrix The matrix to invert.
  3529. * @param {Matrix4} result The object onto which to store the result.
  3530. * @returns {Matrix4} The modified result parameter.
  3531. */
  3532. Matrix4.inverseTransformation = function (matrix, result) {
  3533. //>>includeStart('debug', pragmas.debug);
  3534. Check.Check.typeOf.object("matrix", matrix);
  3535. Check.Check.typeOf.object("result", result);
  3536. //>>includeEnd('debug');
  3537. //This function is an optimized version of the below 4 lines.
  3538. //const rT = Matrix3.transpose(Matrix4.getMatrix3(matrix));
  3539. //const rTN = Matrix3.negate(rT);
  3540. //const rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  3541. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  3542. const matrix0 = matrix[0];
  3543. const matrix1 = matrix[1];
  3544. const matrix2 = matrix[2];
  3545. const matrix4 = matrix[4];
  3546. const matrix5 = matrix[5];
  3547. const matrix6 = matrix[6];
  3548. const matrix8 = matrix[8];
  3549. const matrix9 = matrix[9];
  3550. const matrix10 = matrix[10];
  3551. const vX = matrix[12];
  3552. const vY = matrix[13];
  3553. const vZ = matrix[14];
  3554. const x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  3555. const y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  3556. const z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  3557. result[0] = matrix0;
  3558. result[1] = matrix4;
  3559. result[2] = matrix8;
  3560. result[3] = 0.0;
  3561. result[4] = matrix1;
  3562. result[5] = matrix5;
  3563. result[6] = matrix9;
  3564. result[7] = 0.0;
  3565. result[8] = matrix2;
  3566. result[9] = matrix6;
  3567. result[10] = matrix10;
  3568. result[11] = 0.0;
  3569. result[12] = x;
  3570. result[13] = y;
  3571. result[14] = z;
  3572. result[15] = 1.0;
  3573. return result;
  3574. };
  3575. const scratchTransposeMatrix = new Matrix4();
  3576. /**
  3577. * Computes the inverse transpose of a matrix.
  3578. *
  3579. * @param {Matrix4} matrix The matrix to transpose and invert.
  3580. * @param {Matrix4} result The object onto which to store the result.
  3581. * @returns {Matrix4} The modified result parameter.
  3582. */
  3583. Matrix4.inverseTranspose = function (matrix, result) {
  3584. //>>includeStart('debug', pragmas.debug);
  3585. Check.Check.typeOf.object("matrix", matrix);
  3586. Check.Check.typeOf.object("result", result);
  3587. //>>includeEnd('debug');
  3588. return Matrix4.inverse(
  3589. Matrix4.transpose(matrix, scratchTransposeMatrix),
  3590. result
  3591. );
  3592. };
  3593. /**
  3594. * An immutable Matrix4 instance initialized to the identity matrix.
  3595. *
  3596. * @type {Matrix4}
  3597. * @constant
  3598. */
  3599. Matrix4.IDENTITY = Object.freeze(
  3600. new Matrix4(
  3601. 1.0,
  3602. 0.0,
  3603. 0.0,
  3604. 0.0,
  3605. 0.0,
  3606. 1.0,
  3607. 0.0,
  3608. 0.0,
  3609. 0.0,
  3610. 0.0,
  3611. 1.0,
  3612. 0.0,
  3613. 0.0,
  3614. 0.0,
  3615. 0.0,
  3616. 1.0
  3617. )
  3618. );
  3619. /**
  3620. * An immutable Matrix4 instance initialized to the zero matrix.
  3621. *
  3622. * @type {Matrix4}
  3623. * @constant
  3624. */
  3625. Matrix4.ZERO = Object.freeze(
  3626. new Matrix4(
  3627. 0.0,
  3628. 0.0,
  3629. 0.0,
  3630. 0.0,
  3631. 0.0,
  3632. 0.0,
  3633. 0.0,
  3634. 0.0,
  3635. 0.0,
  3636. 0.0,
  3637. 0.0,
  3638. 0.0,
  3639. 0.0,
  3640. 0.0,
  3641. 0.0,
  3642. 0.0
  3643. )
  3644. );
  3645. /**
  3646. * The index into Matrix4 for column 0, row 0.
  3647. *
  3648. * @type {number}
  3649. * @constant
  3650. */
  3651. Matrix4.COLUMN0ROW0 = 0;
  3652. /**
  3653. * The index into Matrix4 for column 0, row 1.
  3654. *
  3655. * @type {number}
  3656. * @constant
  3657. */
  3658. Matrix4.COLUMN0ROW1 = 1;
  3659. /**
  3660. * The index into Matrix4 for column 0, row 2.
  3661. *
  3662. * @type {number}
  3663. * @constant
  3664. */
  3665. Matrix4.COLUMN0ROW2 = 2;
  3666. /**
  3667. * The index into Matrix4 for column 0, row 3.
  3668. *
  3669. * @type {number}
  3670. * @constant
  3671. */
  3672. Matrix4.COLUMN0ROW3 = 3;
  3673. /**
  3674. * The index into Matrix4 for column 1, row 0.
  3675. *
  3676. * @type {number}
  3677. * @constant
  3678. */
  3679. Matrix4.COLUMN1ROW0 = 4;
  3680. /**
  3681. * The index into Matrix4 for column 1, row 1.
  3682. *
  3683. * @type {number}
  3684. * @constant
  3685. */
  3686. Matrix4.COLUMN1ROW1 = 5;
  3687. /**
  3688. * The index into Matrix4 for column 1, row 2.
  3689. *
  3690. * @type {number}
  3691. * @constant
  3692. */
  3693. Matrix4.COLUMN1ROW2 = 6;
  3694. /**
  3695. * The index into Matrix4 for column 1, row 3.
  3696. *
  3697. * @type {number}
  3698. * @constant
  3699. */
  3700. Matrix4.COLUMN1ROW3 = 7;
  3701. /**
  3702. * The index into Matrix4 for column 2, row 0.
  3703. *
  3704. * @type {number}
  3705. * @constant
  3706. */
  3707. Matrix4.COLUMN2ROW0 = 8;
  3708. /**
  3709. * The index into Matrix4 for column 2, row 1.
  3710. *
  3711. * @type {number}
  3712. * @constant
  3713. */
  3714. Matrix4.COLUMN2ROW1 = 9;
  3715. /**
  3716. * The index into Matrix4 for column 2, row 2.
  3717. *
  3718. * @type {number}
  3719. * @constant
  3720. */
  3721. Matrix4.COLUMN2ROW2 = 10;
  3722. /**
  3723. * The index into Matrix4 for column 2, row 3.
  3724. *
  3725. * @type {number}
  3726. * @constant
  3727. */
  3728. Matrix4.COLUMN2ROW3 = 11;
  3729. /**
  3730. * The index into Matrix4 for column 3, row 0.
  3731. *
  3732. * @type {number}
  3733. * @constant
  3734. */
  3735. Matrix4.COLUMN3ROW0 = 12;
  3736. /**
  3737. * The index into Matrix4 for column 3, row 1.
  3738. *
  3739. * @type {number}
  3740. * @constant
  3741. */
  3742. Matrix4.COLUMN3ROW1 = 13;
  3743. /**
  3744. * The index into Matrix4 for column 3, row 2.
  3745. *
  3746. * @type {number}
  3747. * @constant
  3748. */
  3749. Matrix4.COLUMN3ROW2 = 14;
  3750. /**
  3751. * The index into Matrix4 for column 3, row 3.
  3752. *
  3753. * @type {number}
  3754. * @constant
  3755. */
  3756. Matrix4.COLUMN3ROW3 = 15;
  3757. Object.defineProperties(Matrix4.prototype, {
  3758. /**
  3759. * Gets the number of items in the collection.
  3760. * @memberof Matrix4.prototype
  3761. *
  3762. * @type {number}
  3763. */
  3764. length: {
  3765. get: function () {
  3766. return Matrix4.packedLength;
  3767. },
  3768. },
  3769. });
  3770. /**
  3771. * Duplicates the provided Matrix4 instance.
  3772. *
  3773. * @param {Matrix4} [result] The object onto which to store the result.
  3774. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  3775. */
  3776. Matrix4.prototype.clone = function (result) {
  3777. return Matrix4.clone(this, result);
  3778. };
  3779. /**
  3780. * Compares this matrix to the provided matrix componentwise and returns
  3781. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3782. *
  3783. * @param {Matrix4} [right] The right hand side matrix.
  3784. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3785. */
  3786. Matrix4.prototype.equals = function (right) {
  3787. return Matrix4.equals(this, right);
  3788. };
  3789. /**
  3790. * @private
  3791. */
  3792. Matrix4.equalsArray = function (matrix, array, offset) {
  3793. return (
  3794. matrix[0] === array[offset] &&
  3795. matrix[1] === array[offset + 1] &&
  3796. matrix[2] === array[offset + 2] &&
  3797. matrix[3] === array[offset + 3] &&
  3798. matrix[4] === array[offset + 4] &&
  3799. matrix[5] === array[offset + 5] &&
  3800. matrix[6] === array[offset + 6] &&
  3801. matrix[7] === array[offset + 7] &&
  3802. matrix[8] === array[offset + 8] &&
  3803. matrix[9] === array[offset + 9] &&
  3804. matrix[10] === array[offset + 10] &&
  3805. matrix[11] === array[offset + 11] &&
  3806. matrix[12] === array[offset + 12] &&
  3807. matrix[13] === array[offset + 13] &&
  3808. matrix[14] === array[offset + 14] &&
  3809. matrix[15] === array[offset + 15]
  3810. );
  3811. };
  3812. /**
  3813. * Compares this matrix to the provided matrix componentwise and returns
  3814. * <code>true</code> if they are within the provided epsilon,
  3815. * <code>false</code> otherwise.
  3816. *
  3817. * @param {Matrix4} [right] The right hand side matrix.
  3818. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  3819. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  3820. */
  3821. Matrix4.prototype.equalsEpsilon = function (right, epsilon) {
  3822. return Matrix4.equalsEpsilon(this, right, epsilon);
  3823. };
  3824. /**
  3825. * Computes a string representing this Matrix with each row being
  3826. * on a separate line and in the format '(column0, column1, column2, column3)'.
  3827. *
  3828. * @returns {string} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  3829. */
  3830. Matrix4.prototype.toString = function () {
  3831. return (
  3832. `(${this[0]}, ${this[4]}, ${this[8]}, ${this[12]})\n` +
  3833. `(${this[1]}, ${this[5]}, ${this[9]}, ${this[13]})\n` +
  3834. `(${this[2]}, ${this[6]}, ${this[10]}, ${this[14]})\n` +
  3835. `(${this[3]}, ${this[7]}, ${this[11]}, ${this[15]})`
  3836. );
  3837. };
  3838. /**
  3839. * A two dimensional region specified as longitude and latitude coordinates.
  3840. *
  3841. * @alias Rectangle
  3842. * @constructor
  3843. *
  3844. * @param {number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  3845. * @param {number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  3846. * @param {number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  3847. * @param {number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  3848. *
  3849. * @see Packable
  3850. */
  3851. function Rectangle(west, south, east, north) {
  3852. /**
  3853. * The westernmost longitude in radians in the range [-Pi, Pi].
  3854. *
  3855. * @type {number}
  3856. * @default 0.0
  3857. */
  3858. this.west = defaultValue.defaultValue(west, 0.0);
  3859. /**
  3860. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  3861. *
  3862. * @type {number}
  3863. * @default 0.0
  3864. */
  3865. this.south = defaultValue.defaultValue(south, 0.0);
  3866. /**
  3867. * The easternmost longitude in radians in the range [-Pi, Pi].
  3868. *
  3869. * @type {number}
  3870. * @default 0.0
  3871. */
  3872. this.east = defaultValue.defaultValue(east, 0.0);
  3873. /**
  3874. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  3875. *
  3876. * @type {number}
  3877. * @default 0.0
  3878. */
  3879. this.north = defaultValue.defaultValue(north, 0.0);
  3880. }
  3881. Object.defineProperties(Rectangle.prototype, {
  3882. /**
  3883. * Gets the width of the rectangle in radians.
  3884. * @memberof Rectangle.prototype
  3885. * @type {number}
  3886. * @readonly
  3887. */
  3888. width: {
  3889. get: function () {
  3890. return Rectangle.computeWidth(this);
  3891. },
  3892. },
  3893. /**
  3894. * Gets the height of the rectangle in radians.
  3895. * @memberof Rectangle.prototype
  3896. * @type {number}
  3897. * @readonly
  3898. */
  3899. height: {
  3900. get: function () {
  3901. return Rectangle.computeHeight(this);
  3902. },
  3903. },
  3904. });
  3905. /**
  3906. * The number of elements used to pack the object into an array.
  3907. * @type {number}
  3908. */
  3909. Rectangle.packedLength = 4;
  3910. /**
  3911. * Stores the provided instance into the provided array.
  3912. *
  3913. * @param {Rectangle} value The value to pack.
  3914. * @param {number[]} array The array to pack into.
  3915. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  3916. *
  3917. * @returns {number[]} The array that was packed into
  3918. */
  3919. Rectangle.pack = function (value, array, startingIndex) {
  3920. //>>includeStart('debug', pragmas.debug);
  3921. Check.Check.typeOf.object("value", value);
  3922. Check.Check.defined("array", array);
  3923. //>>includeEnd('debug');
  3924. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3925. array[startingIndex++] = value.west;
  3926. array[startingIndex++] = value.south;
  3927. array[startingIndex++] = value.east;
  3928. array[startingIndex] = value.north;
  3929. return array;
  3930. };
  3931. /**
  3932. * Retrieves an instance from a packed array.
  3933. *
  3934. * @param {number[]} array The packed array.
  3935. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  3936. * @param {Rectangle} [result] The object into which to store the result.
  3937. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  3938. */
  3939. Rectangle.unpack = function (array, startingIndex, result) {
  3940. //>>includeStart('debug', pragmas.debug);
  3941. Check.Check.defined("array", array);
  3942. //>>includeEnd('debug');
  3943. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3944. if (!defaultValue.defined(result)) {
  3945. result = new Rectangle();
  3946. }
  3947. result.west = array[startingIndex++];
  3948. result.south = array[startingIndex++];
  3949. result.east = array[startingIndex++];
  3950. result.north = array[startingIndex];
  3951. return result;
  3952. };
  3953. /**
  3954. * Computes the width of a rectangle in radians.
  3955. * @param {Rectangle} rectangle The rectangle to compute the width of.
  3956. * @returns {number} The width.
  3957. */
  3958. Rectangle.computeWidth = function (rectangle) {
  3959. //>>includeStart('debug', pragmas.debug);
  3960. Check.Check.typeOf.object("rectangle", rectangle);
  3961. //>>includeEnd('debug');
  3962. let east = rectangle.east;
  3963. const west = rectangle.west;
  3964. if (east < west) {
  3965. east += Math$1.CesiumMath.TWO_PI;
  3966. }
  3967. return east - west;
  3968. };
  3969. /**
  3970. * Computes the height of a rectangle in radians.
  3971. * @param {Rectangle} rectangle The rectangle to compute the height of.
  3972. * @returns {number} The height.
  3973. */
  3974. Rectangle.computeHeight = function (rectangle) {
  3975. //>>includeStart('debug', pragmas.debug);
  3976. Check.Check.typeOf.object("rectangle", rectangle);
  3977. //>>includeEnd('debug');
  3978. return rectangle.north - rectangle.south;
  3979. };
  3980. /**
  3981. * Creates a rectangle given the boundary longitude and latitude in degrees.
  3982. *
  3983. * @param {number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  3984. * @param {number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  3985. * @param {number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  3986. * @param {number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  3987. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  3988. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  3989. *
  3990. * @example
  3991. * const rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  3992. */
  3993. Rectangle.fromDegrees = function (west, south, east, north, result) {
  3994. west = Math$1.CesiumMath.toRadians(defaultValue.defaultValue(west, 0.0));
  3995. south = Math$1.CesiumMath.toRadians(defaultValue.defaultValue(south, 0.0));
  3996. east = Math$1.CesiumMath.toRadians(defaultValue.defaultValue(east, 0.0));
  3997. north = Math$1.CesiumMath.toRadians(defaultValue.defaultValue(north, 0.0));
  3998. if (!defaultValue.defined(result)) {
  3999. return new Rectangle(west, south, east, north);
  4000. }
  4001. result.west = west;
  4002. result.south = south;
  4003. result.east = east;
  4004. result.north = north;
  4005. return result;
  4006. };
  4007. /**
  4008. * Creates a rectangle given the boundary longitude and latitude in radians.
  4009. *
  4010. * @param {number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
  4011. * @param {number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  4012. * @param {number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
  4013. * @param {number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  4014. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  4015. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  4016. *
  4017. * @example
  4018. * const rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
  4019. */
  4020. Rectangle.fromRadians = function (west, south, east, north, result) {
  4021. if (!defaultValue.defined(result)) {
  4022. return new Rectangle(west, south, east, north);
  4023. }
  4024. result.west = defaultValue.defaultValue(west, 0.0);
  4025. result.south = defaultValue.defaultValue(south, 0.0);
  4026. result.east = defaultValue.defaultValue(east, 0.0);
  4027. result.north = defaultValue.defaultValue(north, 0.0);
  4028. return result;
  4029. };
  4030. /**
  4031. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  4032. *
  4033. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  4034. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  4035. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  4036. */
  4037. Rectangle.fromCartographicArray = function (cartographics, result) {
  4038. //>>includeStart('debug', pragmas.debug);
  4039. Check.Check.defined("cartographics", cartographics);
  4040. //>>includeEnd('debug');
  4041. let west = Number.MAX_VALUE;
  4042. let east = -Number.MAX_VALUE;
  4043. let westOverIDL = Number.MAX_VALUE;
  4044. let eastOverIDL = -Number.MAX_VALUE;
  4045. let south = Number.MAX_VALUE;
  4046. let north = -Number.MAX_VALUE;
  4047. for (let i = 0, len = cartographics.length; i < len; i++) {
  4048. const position = cartographics[i];
  4049. west = Math.min(west, position.longitude);
  4050. east = Math.max(east, position.longitude);
  4051. south = Math.min(south, position.latitude);
  4052. north = Math.max(north, position.latitude);
  4053. const lonAdjusted =
  4054. position.longitude >= 0
  4055. ? position.longitude
  4056. : position.longitude + Math$1.CesiumMath.TWO_PI;
  4057. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  4058. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  4059. }
  4060. if (east - west > eastOverIDL - westOverIDL) {
  4061. west = westOverIDL;
  4062. east = eastOverIDL;
  4063. if (east > Math$1.CesiumMath.PI) {
  4064. east = east - Math$1.CesiumMath.TWO_PI;
  4065. }
  4066. if (west > Math$1.CesiumMath.PI) {
  4067. west = west - Math$1.CesiumMath.TWO_PI;
  4068. }
  4069. }
  4070. if (!defaultValue.defined(result)) {
  4071. return new Rectangle(west, south, east, north);
  4072. }
  4073. result.west = west;
  4074. result.south = south;
  4075. result.east = east;
  4076. result.north = north;
  4077. return result;
  4078. };
  4079. /**
  4080. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  4081. *
  4082. * @param {Cartesian3[]} cartesians The list of Cartesian instances.
  4083. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  4084. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  4085. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  4086. */
  4087. Rectangle.fromCartesianArray = function (cartesians, ellipsoid, result) {
  4088. //>>includeStart('debug', pragmas.debug);
  4089. Check.Check.defined("cartesians", cartesians);
  4090. //>>includeEnd('debug');
  4091. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84);
  4092. let west = Number.MAX_VALUE;
  4093. let east = -Number.MAX_VALUE;
  4094. let westOverIDL = Number.MAX_VALUE;
  4095. let eastOverIDL = -Number.MAX_VALUE;
  4096. let south = Number.MAX_VALUE;
  4097. let north = -Number.MAX_VALUE;
  4098. for (let i = 0, len = cartesians.length; i < len; i++) {
  4099. const position = ellipsoid.cartesianToCartographic(cartesians[i]);
  4100. west = Math.min(west, position.longitude);
  4101. east = Math.max(east, position.longitude);
  4102. south = Math.min(south, position.latitude);
  4103. north = Math.max(north, position.latitude);
  4104. const lonAdjusted =
  4105. position.longitude >= 0
  4106. ? position.longitude
  4107. : position.longitude + Math$1.CesiumMath.TWO_PI;
  4108. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  4109. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  4110. }
  4111. if (east - west > eastOverIDL - westOverIDL) {
  4112. west = westOverIDL;
  4113. east = eastOverIDL;
  4114. if (east > Math$1.CesiumMath.PI) {
  4115. east = east - Math$1.CesiumMath.TWO_PI;
  4116. }
  4117. if (west > Math$1.CesiumMath.PI) {
  4118. west = west - Math$1.CesiumMath.TWO_PI;
  4119. }
  4120. }
  4121. if (!defaultValue.defined(result)) {
  4122. return new Rectangle(west, south, east, north);
  4123. }
  4124. result.west = west;
  4125. result.south = south;
  4126. result.east = east;
  4127. result.north = north;
  4128. return result;
  4129. };
  4130. /**
  4131. * Duplicates a Rectangle.
  4132. *
  4133. * @param {Rectangle} rectangle The rectangle to clone.
  4134. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  4135. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  4136. */
  4137. Rectangle.clone = function (rectangle, result) {
  4138. if (!defaultValue.defined(rectangle)) {
  4139. return undefined;
  4140. }
  4141. if (!defaultValue.defined(result)) {
  4142. return new Rectangle(
  4143. rectangle.west,
  4144. rectangle.south,
  4145. rectangle.east,
  4146. rectangle.north
  4147. );
  4148. }
  4149. result.west = rectangle.west;
  4150. result.south = rectangle.south;
  4151. result.east = rectangle.east;
  4152. result.north = rectangle.north;
  4153. return result;
  4154. };
  4155. /**
  4156. * Compares the provided Rectangles componentwise and returns
  4157. * <code>true</code> if they pass an absolute or relative tolerance test,
  4158. * <code>false</code> otherwise.
  4159. *
  4160. * @param {Rectangle} [left] The first Rectangle.
  4161. * @param {Rectangle} [right] The second Rectangle.
  4162. * @param {number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
  4163. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  4164. */
  4165. Rectangle.equalsEpsilon = function (left, right, absoluteEpsilon) {
  4166. absoluteEpsilon = defaultValue.defaultValue(absoluteEpsilon, 0);
  4167. return (
  4168. left === right ||
  4169. (defaultValue.defined(left) &&
  4170. defaultValue.defined(right) &&
  4171. Math.abs(left.west - right.west) <= absoluteEpsilon &&
  4172. Math.abs(left.south - right.south) <= absoluteEpsilon &&
  4173. Math.abs(left.east - right.east) <= absoluteEpsilon &&
  4174. Math.abs(left.north - right.north) <= absoluteEpsilon)
  4175. );
  4176. };
  4177. /**
  4178. * Duplicates this Rectangle.
  4179. *
  4180. * @param {Rectangle} [result] The object onto which to store the result.
  4181. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  4182. */
  4183. Rectangle.prototype.clone = function (result) {
  4184. return Rectangle.clone(this, result);
  4185. };
  4186. /**
  4187. * Compares the provided Rectangle with this Rectangle componentwise and returns
  4188. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4189. *
  4190. * @param {Rectangle} [other] The Rectangle to compare.
  4191. * @returns {boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  4192. */
  4193. Rectangle.prototype.equals = function (other) {
  4194. return Rectangle.equals(this, other);
  4195. };
  4196. /**
  4197. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  4198. * <code>false</code> otherwise.
  4199. *
  4200. * @param {Rectangle} [left] The first Rectangle.
  4201. * @param {Rectangle} [right] The second Rectangle.
  4202. * @returns {boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  4203. */
  4204. Rectangle.equals = function (left, right) {
  4205. return (
  4206. left === right ||
  4207. (defaultValue.defined(left) &&
  4208. defaultValue.defined(right) &&
  4209. left.west === right.west &&
  4210. left.south === right.south &&
  4211. left.east === right.east &&
  4212. left.north === right.north)
  4213. );
  4214. };
  4215. /**
  4216. * Compares the provided Rectangle with this Rectangle componentwise and returns
  4217. * <code>true</code> if they are within the provided epsilon,
  4218. * <code>false</code> otherwise.
  4219. *
  4220. * @param {Rectangle} [other] The Rectangle to compare.
  4221. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  4222. * @returns {boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  4223. */
  4224. Rectangle.prototype.equalsEpsilon = function (other, epsilon) {
  4225. return Rectangle.equalsEpsilon(this, other, epsilon);
  4226. };
  4227. /**
  4228. * Checks a Rectangle's properties and throws if they are not in valid ranges.
  4229. *
  4230. * @param {Rectangle} rectangle The rectangle to validate
  4231. *
  4232. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  4233. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  4234. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  4235. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  4236. */
  4237. Rectangle.validate = function (rectangle) {
  4238. //>>includeStart('debug', pragmas.debug);
  4239. Check.Check.typeOf.object("rectangle", rectangle);
  4240. const north = rectangle.north;
  4241. Check.Check.typeOf.number.greaterThanOrEquals(
  4242. "north",
  4243. north,
  4244. -Math$1.CesiumMath.PI_OVER_TWO
  4245. );
  4246. Check.Check.typeOf.number.lessThanOrEquals("north", north, Math$1.CesiumMath.PI_OVER_TWO);
  4247. const south = rectangle.south;
  4248. Check.Check.typeOf.number.greaterThanOrEquals(
  4249. "south",
  4250. south,
  4251. -Math$1.CesiumMath.PI_OVER_TWO
  4252. );
  4253. Check.Check.typeOf.number.lessThanOrEquals("south", south, Math$1.CesiumMath.PI_OVER_TWO);
  4254. const west = rectangle.west;
  4255. Check.Check.typeOf.number.greaterThanOrEquals("west", west, -Math.PI);
  4256. Check.Check.typeOf.number.lessThanOrEquals("west", west, Math.PI);
  4257. const east = rectangle.east;
  4258. Check.Check.typeOf.number.greaterThanOrEquals("east", east, -Math.PI);
  4259. Check.Check.typeOf.number.lessThanOrEquals("east", east, Math.PI);
  4260. //>>includeEnd('debug');
  4261. };
  4262. /**
  4263. * Computes the southwest corner of a rectangle.
  4264. *
  4265. * @param {Rectangle} rectangle The rectangle for which to find the corner
  4266. * @param {Cartographic} [result] The object onto which to store the result.
  4267. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  4268. */
  4269. Rectangle.southwest = function (rectangle, result) {
  4270. //>>includeStart('debug', pragmas.debug);
  4271. Check.Check.typeOf.object("rectangle", rectangle);
  4272. //>>includeEnd('debug');
  4273. if (!defaultValue.defined(result)) {
  4274. return new Matrix3.Cartographic(rectangle.west, rectangle.south);
  4275. }
  4276. result.longitude = rectangle.west;
  4277. result.latitude = rectangle.south;
  4278. result.height = 0.0;
  4279. return result;
  4280. };
  4281. /**
  4282. * Computes the northwest corner of a rectangle.
  4283. *
  4284. * @param {Rectangle} rectangle The rectangle for which to find the corner
  4285. * @param {Cartographic} [result] The object onto which to store the result.
  4286. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  4287. */
  4288. Rectangle.northwest = function (rectangle, result) {
  4289. //>>includeStart('debug', pragmas.debug);
  4290. Check.Check.typeOf.object("rectangle", rectangle);
  4291. //>>includeEnd('debug');
  4292. if (!defaultValue.defined(result)) {
  4293. return new Matrix3.Cartographic(rectangle.west, rectangle.north);
  4294. }
  4295. result.longitude = rectangle.west;
  4296. result.latitude = rectangle.north;
  4297. result.height = 0.0;
  4298. return result;
  4299. };
  4300. /**
  4301. * Computes the northeast corner of a rectangle.
  4302. *
  4303. * @param {Rectangle} rectangle The rectangle for which to find the corner
  4304. * @param {Cartographic} [result] The object onto which to store the result.
  4305. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  4306. */
  4307. Rectangle.northeast = function (rectangle, result) {
  4308. //>>includeStart('debug', pragmas.debug);
  4309. Check.Check.typeOf.object("rectangle", rectangle);
  4310. //>>includeEnd('debug');
  4311. if (!defaultValue.defined(result)) {
  4312. return new Matrix3.Cartographic(rectangle.east, rectangle.north);
  4313. }
  4314. result.longitude = rectangle.east;
  4315. result.latitude = rectangle.north;
  4316. result.height = 0.0;
  4317. return result;
  4318. };
  4319. /**
  4320. * Computes the southeast corner of a rectangle.
  4321. *
  4322. * @param {Rectangle} rectangle The rectangle for which to find the corner
  4323. * @param {Cartographic} [result] The object onto which to store the result.
  4324. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  4325. */
  4326. Rectangle.southeast = function (rectangle, result) {
  4327. //>>includeStart('debug', pragmas.debug);
  4328. Check.Check.typeOf.object("rectangle", rectangle);
  4329. //>>includeEnd('debug');
  4330. if (!defaultValue.defined(result)) {
  4331. return new Matrix3.Cartographic(rectangle.east, rectangle.south);
  4332. }
  4333. result.longitude = rectangle.east;
  4334. result.latitude = rectangle.south;
  4335. result.height = 0.0;
  4336. return result;
  4337. };
  4338. /**
  4339. * Computes the center of a rectangle.
  4340. *
  4341. * @param {Rectangle} rectangle The rectangle for which to find the center
  4342. * @param {Cartographic} [result] The object onto which to store the result.
  4343. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  4344. */
  4345. Rectangle.center = function (rectangle, result) {
  4346. //>>includeStart('debug', pragmas.debug);
  4347. Check.Check.typeOf.object("rectangle", rectangle);
  4348. //>>includeEnd('debug');
  4349. let east = rectangle.east;
  4350. const west = rectangle.west;
  4351. if (east < west) {
  4352. east += Math$1.CesiumMath.TWO_PI;
  4353. }
  4354. const longitude = Math$1.CesiumMath.negativePiToPi((west + east) * 0.5);
  4355. const latitude = (rectangle.south + rectangle.north) * 0.5;
  4356. if (!defaultValue.defined(result)) {
  4357. return new Matrix3.Cartographic(longitude, latitude);
  4358. }
  4359. result.longitude = longitude;
  4360. result.latitude = latitude;
  4361. result.height = 0.0;
  4362. return result;
  4363. };
  4364. /**
  4365. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  4366. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  4367. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  4368. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  4369. * coordinates, see {@link Rectangle.simpleIntersection}.
  4370. *
  4371. * @param {Rectangle} rectangle On rectangle to find an intersection
  4372. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  4373. * @param {Rectangle} [result] The object onto which to store the result.
  4374. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  4375. */
  4376. Rectangle.intersection = function (rectangle, otherRectangle, result) {
  4377. //>>includeStart('debug', pragmas.debug);
  4378. Check.Check.typeOf.object("rectangle", rectangle);
  4379. Check.Check.typeOf.object("otherRectangle", otherRectangle);
  4380. //>>includeEnd('debug');
  4381. let rectangleEast = rectangle.east;
  4382. let rectangleWest = rectangle.west;
  4383. let otherRectangleEast = otherRectangle.east;
  4384. let otherRectangleWest = otherRectangle.west;
  4385. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  4386. rectangleEast += Math$1.CesiumMath.TWO_PI;
  4387. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  4388. otherRectangleEast += Math$1.CesiumMath.TWO_PI;
  4389. }
  4390. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  4391. otherRectangleWest += Math$1.CesiumMath.TWO_PI;
  4392. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  4393. rectangleWest += Math$1.CesiumMath.TWO_PI;
  4394. }
  4395. const west = Math$1.CesiumMath.negativePiToPi(
  4396. Math.max(rectangleWest, otherRectangleWest)
  4397. );
  4398. const east = Math$1.CesiumMath.negativePiToPi(
  4399. Math.min(rectangleEast, otherRectangleEast)
  4400. );
  4401. if (
  4402. (rectangle.west < rectangle.east ||
  4403. otherRectangle.west < otherRectangle.east) &&
  4404. east <= west
  4405. ) {
  4406. return undefined;
  4407. }
  4408. const south = Math.max(rectangle.south, otherRectangle.south);
  4409. const north = Math.min(rectangle.north, otherRectangle.north);
  4410. if (south >= north) {
  4411. return undefined;
  4412. }
  4413. if (!defaultValue.defined(result)) {
  4414. return new Rectangle(west, south, east, north);
  4415. }
  4416. result.west = west;
  4417. result.south = south;
  4418. result.east = east;
  4419. result.north = north;
  4420. return result;
  4421. };
  4422. /**
  4423. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  4424. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  4425. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  4426. * and longitude (i.e. projected coordinates).
  4427. *
  4428. * @param {Rectangle} rectangle On rectangle to find an intersection
  4429. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  4430. * @param {Rectangle} [result] The object onto which to store the result.
  4431. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  4432. */
  4433. Rectangle.simpleIntersection = function (rectangle, otherRectangle, result) {
  4434. //>>includeStart('debug', pragmas.debug);
  4435. Check.Check.typeOf.object("rectangle", rectangle);
  4436. Check.Check.typeOf.object("otherRectangle", otherRectangle);
  4437. //>>includeEnd('debug');
  4438. const west = Math.max(rectangle.west, otherRectangle.west);
  4439. const south = Math.max(rectangle.south, otherRectangle.south);
  4440. const east = Math.min(rectangle.east, otherRectangle.east);
  4441. const north = Math.min(rectangle.north, otherRectangle.north);
  4442. if (south >= north || west >= east) {
  4443. return undefined;
  4444. }
  4445. if (!defaultValue.defined(result)) {
  4446. return new Rectangle(west, south, east, north);
  4447. }
  4448. result.west = west;
  4449. result.south = south;
  4450. result.east = east;
  4451. result.north = north;
  4452. return result;
  4453. };
  4454. /**
  4455. * Computes a rectangle that is the union of two rectangles.
  4456. *
  4457. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  4458. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  4459. * @param {Rectangle} [result] The object onto which to store the result.
  4460. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  4461. */
  4462. Rectangle.union = function (rectangle, otherRectangle, result) {
  4463. //>>includeStart('debug', pragmas.debug);
  4464. Check.Check.typeOf.object("rectangle", rectangle);
  4465. Check.Check.typeOf.object("otherRectangle", otherRectangle);
  4466. //>>includeEnd('debug');
  4467. if (!defaultValue.defined(result)) {
  4468. result = new Rectangle();
  4469. }
  4470. let rectangleEast = rectangle.east;
  4471. let rectangleWest = rectangle.west;
  4472. let otherRectangleEast = otherRectangle.east;
  4473. let otherRectangleWest = otherRectangle.west;
  4474. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  4475. rectangleEast += Math$1.CesiumMath.TWO_PI;
  4476. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  4477. otherRectangleEast += Math$1.CesiumMath.TWO_PI;
  4478. }
  4479. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  4480. otherRectangleWest += Math$1.CesiumMath.TWO_PI;
  4481. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  4482. rectangleWest += Math$1.CesiumMath.TWO_PI;
  4483. }
  4484. const west = Math$1.CesiumMath.negativePiToPi(
  4485. Math.min(rectangleWest, otherRectangleWest)
  4486. );
  4487. const east = Math$1.CesiumMath.negativePiToPi(
  4488. Math.max(rectangleEast, otherRectangleEast)
  4489. );
  4490. result.west = west;
  4491. result.south = Math.min(rectangle.south, otherRectangle.south);
  4492. result.east = east;
  4493. result.north = Math.max(rectangle.north, otherRectangle.north);
  4494. return result;
  4495. };
  4496. /**
  4497. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  4498. *
  4499. * @param {Rectangle} rectangle A rectangle to expand.
  4500. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  4501. * @param {Rectangle} [result] The object onto which to store the result.
  4502. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  4503. */
  4504. Rectangle.expand = function (rectangle, cartographic, result) {
  4505. //>>includeStart('debug', pragmas.debug);
  4506. Check.Check.typeOf.object("rectangle", rectangle);
  4507. Check.Check.typeOf.object("cartographic", cartographic);
  4508. //>>includeEnd('debug');
  4509. if (!defaultValue.defined(result)) {
  4510. result = new Rectangle();
  4511. }
  4512. result.west = Math.min(rectangle.west, cartographic.longitude);
  4513. result.south = Math.min(rectangle.south, cartographic.latitude);
  4514. result.east = Math.max(rectangle.east, cartographic.longitude);
  4515. result.north = Math.max(rectangle.north, cartographic.latitude);
  4516. return result;
  4517. };
  4518. /**
  4519. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  4520. *
  4521. * @param {Rectangle} rectangle The rectangle
  4522. * @param {Cartographic} cartographic The cartographic to test.
  4523. * @returns {boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  4524. */
  4525. Rectangle.contains = function (rectangle, cartographic) {
  4526. //>>includeStart('debug', pragmas.debug);
  4527. Check.Check.typeOf.object("rectangle", rectangle);
  4528. Check.Check.typeOf.object("cartographic", cartographic);
  4529. //>>includeEnd('debug');
  4530. let longitude = cartographic.longitude;
  4531. const latitude = cartographic.latitude;
  4532. const west = rectangle.west;
  4533. let east = rectangle.east;
  4534. if (east < west) {
  4535. east += Math$1.CesiumMath.TWO_PI;
  4536. if (longitude < 0.0) {
  4537. longitude += Math$1.CesiumMath.TWO_PI;
  4538. }
  4539. }
  4540. return (
  4541. (longitude > west ||
  4542. Math$1.CesiumMath.equalsEpsilon(longitude, west, Math$1.CesiumMath.EPSILON14)) &&
  4543. (longitude < east ||
  4544. Math$1.CesiumMath.equalsEpsilon(longitude, east, Math$1.CesiumMath.EPSILON14)) &&
  4545. latitude >= rectangle.south &&
  4546. latitude <= rectangle.north
  4547. );
  4548. };
  4549. const subsampleLlaScratch = new Matrix3.Cartographic();
  4550. /**
  4551. * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
  4552. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  4553. * for rectangles that cover the poles or cross the equator.
  4554. *
  4555. * @param {Rectangle} rectangle The rectangle to subsample.
  4556. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  4557. * @param {number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  4558. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  4559. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  4560. */
  4561. Rectangle.subsample = function (rectangle, ellipsoid, surfaceHeight, result) {
  4562. //>>includeStart('debug', pragmas.debug);
  4563. Check.Check.typeOf.object("rectangle", rectangle);
  4564. //>>includeEnd('debug');
  4565. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix3.Ellipsoid.WGS84);
  4566. surfaceHeight = defaultValue.defaultValue(surfaceHeight, 0.0);
  4567. if (!defaultValue.defined(result)) {
  4568. result = [];
  4569. }
  4570. let length = 0;
  4571. const north = rectangle.north;
  4572. const south = rectangle.south;
  4573. const east = rectangle.east;
  4574. const west = rectangle.west;
  4575. const lla = subsampleLlaScratch;
  4576. lla.height = surfaceHeight;
  4577. lla.longitude = west;
  4578. lla.latitude = north;
  4579. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4580. length++;
  4581. lla.longitude = east;
  4582. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4583. length++;
  4584. lla.latitude = south;
  4585. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4586. length++;
  4587. lla.longitude = west;
  4588. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4589. length++;
  4590. if (north < 0.0) {
  4591. lla.latitude = north;
  4592. } else if (south > 0.0) {
  4593. lla.latitude = south;
  4594. } else {
  4595. lla.latitude = 0.0;
  4596. }
  4597. for (let i = 1; i < 8; ++i) {
  4598. lla.longitude = -Math.PI + i * Math$1.CesiumMath.PI_OVER_TWO;
  4599. if (Rectangle.contains(rectangle, lla)) {
  4600. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4601. length++;
  4602. }
  4603. }
  4604. if (lla.latitude === 0.0) {
  4605. lla.longitude = west;
  4606. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4607. length++;
  4608. lla.longitude = east;
  4609. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  4610. length++;
  4611. }
  4612. result.length = length;
  4613. return result;
  4614. };
  4615. /**
  4616. * Computes a subsection of a rectangle from normalized coordinates in the range [0.0, 1.0].
  4617. *
  4618. * @param {Rectangle} rectangle The rectangle to subsection.
  4619. * @param {number} westLerp The west interpolation factor in the range [0.0, 1.0]. Must be less than or equal to eastLerp.
  4620. * @param {number} southLerp The south interpolation factor in the range [0.0, 1.0]. Must be less than or equal to northLerp.
  4621. * @param {number} eastLerp The east interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to westLerp.
  4622. * @param {number} northLerp The north interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to southLerp.
  4623. * @param {Rectangle} [result] The object onto which to store the result.
  4624. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  4625. */
  4626. Rectangle.subsection = function (
  4627. rectangle,
  4628. westLerp,
  4629. southLerp,
  4630. eastLerp,
  4631. northLerp,
  4632. result
  4633. ) {
  4634. //>>includeStart('debug', pragmas.debug);
  4635. Check.Check.typeOf.object("rectangle", rectangle);
  4636. Check.Check.typeOf.number.greaterThanOrEquals("westLerp", westLerp, 0.0);
  4637. Check.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, 1.0);
  4638. Check.Check.typeOf.number.greaterThanOrEquals("southLerp", southLerp, 0.0);
  4639. Check.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, 1.0);
  4640. Check.Check.typeOf.number.greaterThanOrEquals("eastLerp", eastLerp, 0.0);
  4641. Check.Check.typeOf.number.lessThanOrEquals("eastLerp", eastLerp, 1.0);
  4642. Check.Check.typeOf.number.greaterThanOrEquals("northLerp", northLerp, 0.0);
  4643. Check.Check.typeOf.number.lessThanOrEquals("northLerp", northLerp, 1.0);
  4644. Check.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, eastLerp);
  4645. Check.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, northLerp);
  4646. //>>includeEnd('debug');
  4647. if (!defaultValue.defined(result)) {
  4648. result = new Rectangle();
  4649. }
  4650. // This function doesn't use CesiumMath.lerp because it has floating point precision problems
  4651. // when the start and end values are the same but the t changes.
  4652. if (rectangle.west <= rectangle.east) {
  4653. const width = rectangle.east - rectangle.west;
  4654. result.west = rectangle.west + westLerp * width;
  4655. result.east = rectangle.west + eastLerp * width;
  4656. } else {
  4657. const width = Math$1.CesiumMath.TWO_PI + rectangle.east - rectangle.west;
  4658. result.west = Math$1.CesiumMath.negativePiToPi(rectangle.west + westLerp * width);
  4659. result.east = Math$1.CesiumMath.negativePiToPi(rectangle.west + eastLerp * width);
  4660. }
  4661. const height = rectangle.north - rectangle.south;
  4662. result.south = rectangle.south + southLerp * height;
  4663. result.north = rectangle.south + northLerp * height;
  4664. // Fix floating point precision problems when t = 1
  4665. if (westLerp === 1.0) {
  4666. result.west = rectangle.east;
  4667. }
  4668. if (eastLerp === 1.0) {
  4669. result.east = rectangle.east;
  4670. }
  4671. if (southLerp === 1.0) {
  4672. result.south = rectangle.north;
  4673. }
  4674. if (northLerp === 1.0) {
  4675. result.north = rectangle.north;
  4676. }
  4677. return result;
  4678. };
  4679. /**
  4680. * The largest possible rectangle.
  4681. *
  4682. * @type {Rectangle}
  4683. * @constant
  4684. */
  4685. Rectangle.MAX_VALUE = Object.freeze(
  4686. new Rectangle(
  4687. -Math.PI,
  4688. -Math$1.CesiumMath.PI_OVER_TWO,
  4689. Math.PI,
  4690. Math$1.CesiumMath.PI_OVER_TWO
  4691. )
  4692. );
  4693. /**
  4694. * A 2D Cartesian point.
  4695. * @alias Cartesian2
  4696. * @constructor
  4697. *
  4698. * @param {number} [x=0.0] The X component.
  4699. * @param {number} [y=0.0] The Y component.
  4700. *
  4701. * @see Cartesian3
  4702. * @see Cartesian4
  4703. * @see Packable
  4704. */
  4705. function Cartesian2(x, y) {
  4706. /**
  4707. * The X component.
  4708. * @type {number}
  4709. * @default 0.0
  4710. */
  4711. this.x = defaultValue.defaultValue(x, 0.0);
  4712. /**
  4713. * The Y component.
  4714. * @type {number}
  4715. * @default 0.0
  4716. */
  4717. this.y = defaultValue.defaultValue(y, 0.0);
  4718. }
  4719. /**
  4720. * Creates a Cartesian2 instance from x and y coordinates.
  4721. *
  4722. * @param {number} x The x coordinate.
  4723. * @param {number} y The y coordinate.
  4724. * @param {Cartesian2} [result] The object onto which to store the result.
  4725. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  4726. */
  4727. Cartesian2.fromElements = function (x, y, result) {
  4728. if (!defaultValue.defined(result)) {
  4729. return new Cartesian2(x, y);
  4730. }
  4731. result.x = x;
  4732. result.y = y;
  4733. return result;
  4734. };
  4735. /**
  4736. * Duplicates a Cartesian2 instance.
  4737. *
  4738. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  4739. * @param {Cartesian2} [result] The object onto which to store the result.
  4740. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  4741. */
  4742. Cartesian2.clone = function (cartesian, result) {
  4743. if (!defaultValue.defined(cartesian)) {
  4744. return undefined;
  4745. }
  4746. if (!defaultValue.defined(result)) {
  4747. return new Cartesian2(cartesian.x, cartesian.y);
  4748. }
  4749. result.x = cartesian.x;
  4750. result.y = cartesian.y;
  4751. return result;
  4752. };
  4753. /**
  4754. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  4755. * x and y properties of the Cartesian3 and drops z.
  4756. * @function
  4757. *
  4758. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  4759. * @param {Cartesian2} [result] The object onto which to store the result.
  4760. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  4761. */
  4762. Cartesian2.fromCartesian3 = Cartesian2.clone;
  4763. /**
  4764. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  4765. * x and y properties of the Cartesian4 and drops z and w.
  4766. * @function
  4767. *
  4768. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  4769. * @param {Cartesian2} [result] The object onto which to store the result.
  4770. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  4771. */
  4772. Cartesian2.fromCartesian4 = Cartesian2.clone;
  4773. /**
  4774. * The number of elements used to pack the object into an array.
  4775. * @type {number}
  4776. */
  4777. Cartesian2.packedLength = 2;
  4778. /**
  4779. * Stores the provided instance into the provided array.
  4780. *
  4781. * @param {Cartesian2} value The value to pack.
  4782. * @param {number[]} array The array to pack into.
  4783. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  4784. *
  4785. * @returns {number[]} The array that was packed into
  4786. */
  4787. Cartesian2.pack = function (value, array, startingIndex) {
  4788. //>>includeStart('debug', pragmas.debug);
  4789. Check.Check.typeOf.object("value", value);
  4790. Check.Check.defined("array", array);
  4791. //>>includeEnd('debug');
  4792. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4793. array[startingIndex++] = value.x;
  4794. array[startingIndex] = value.y;
  4795. return array;
  4796. };
  4797. /**
  4798. * Retrieves an instance from a packed array.
  4799. *
  4800. * @param {number[]} array The packed array.
  4801. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  4802. * @param {Cartesian2} [result] The object into which to store the result.
  4803. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  4804. */
  4805. Cartesian2.unpack = function (array, startingIndex, result) {
  4806. //>>includeStart('debug', pragmas.debug);
  4807. Check.Check.defined("array", array);
  4808. //>>includeEnd('debug');
  4809. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4810. if (!defaultValue.defined(result)) {
  4811. result = new Cartesian2();
  4812. }
  4813. result.x = array[startingIndex++];
  4814. result.y = array[startingIndex];
  4815. return result;
  4816. };
  4817. /**
  4818. * Flattens an array of Cartesian2s into an array of components.
  4819. *
  4820. * @param {Cartesian2[]} array The array of cartesians to pack.
  4821. * @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 2 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 2) elements.
  4822. * @returns {number[]} The packed array.
  4823. */
  4824. Cartesian2.packArray = function (array, result) {
  4825. //>>includeStart('debug', pragmas.debug);
  4826. Check.Check.defined("array", array);
  4827. //>>includeEnd('debug');
  4828. const length = array.length;
  4829. const resultLength = length * 2;
  4830. if (!defaultValue.defined(result)) {
  4831. result = new Array(resultLength);
  4832. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4833. //>>includeStart('debug', pragmas.debug);
  4834. throw new Check.DeveloperError(
  4835. "If result is a typed array, it must have exactly array.length * 2 elements"
  4836. );
  4837. //>>includeEnd('debug');
  4838. } else if (result.length !== resultLength) {
  4839. result.length = resultLength;
  4840. }
  4841. for (let i = 0; i < length; ++i) {
  4842. Cartesian2.pack(array[i], result, i * 2);
  4843. }
  4844. return result;
  4845. };
  4846. /**
  4847. * Unpacks an array of cartesian components into an array of Cartesian2s.
  4848. *
  4849. * @param {number[]} array The array of components to unpack.
  4850. * @param {Cartesian2[]} [result] The array onto which to store the result.
  4851. * @returns {Cartesian2[]} The unpacked array.
  4852. */
  4853. Cartesian2.unpackArray = function (array, result) {
  4854. //>>includeStart('debug', pragmas.debug);
  4855. Check.Check.defined("array", array);
  4856. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 2);
  4857. if (array.length % 2 !== 0) {
  4858. throw new Check.DeveloperError("array length must be a multiple of 2.");
  4859. }
  4860. //>>includeEnd('debug');
  4861. const length = array.length;
  4862. if (!defaultValue.defined(result)) {
  4863. result = new Array(length / 2);
  4864. } else {
  4865. result.length = length / 2;
  4866. }
  4867. for (let i = 0; i < length; i += 2) {
  4868. const index = i / 2;
  4869. result[index] = Cartesian2.unpack(array, i, result[index]);
  4870. }
  4871. return result;
  4872. };
  4873. /**
  4874. * Creates a Cartesian2 from two consecutive elements in an array.
  4875. * @function
  4876. *
  4877. * @param {number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  4878. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  4879. * @param {Cartesian2} [result] The object onto which to store the result.
  4880. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  4881. *
  4882. * @example
  4883. * // Create a Cartesian2 with (1.0, 2.0)
  4884. * const v = [1.0, 2.0];
  4885. * const p = Cesium.Cartesian2.fromArray(v);
  4886. *
  4887. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  4888. * const v2 = [0.0, 0.0, 1.0, 2.0];
  4889. * const p2 = Cesium.Cartesian2.fromArray(v2, 2);
  4890. */
  4891. Cartesian2.fromArray = Cartesian2.unpack;
  4892. /**
  4893. * Computes the value of the maximum component for the supplied Cartesian.
  4894. *
  4895. * @param {Cartesian2} cartesian The cartesian to use.
  4896. * @returns {number} The value of the maximum component.
  4897. */
  4898. Cartesian2.maximumComponent = function (cartesian) {
  4899. //>>includeStart('debug', pragmas.debug);
  4900. Check.Check.typeOf.object("cartesian", cartesian);
  4901. //>>includeEnd('debug');
  4902. return Math.max(cartesian.x, cartesian.y);
  4903. };
  4904. /**
  4905. * Computes the value of the minimum component for the supplied Cartesian.
  4906. *
  4907. * @param {Cartesian2} cartesian The cartesian to use.
  4908. * @returns {number} The value of the minimum component.
  4909. */
  4910. Cartesian2.minimumComponent = function (cartesian) {
  4911. //>>includeStart('debug', pragmas.debug);
  4912. Check.Check.typeOf.object("cartesian", cartesian);
  4913. //>>includeEnd('debug');
  4914. return Math.min(cartesian.x, cartesian.y);
  4915. };
  4916. /**
  4917. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  4918. *
  4919. * @param {Cartesian2} first A cartesian to compare.
  4920. * @param {Cartesian2} second A cartesian to compare.
  4921. * @param {Cartesian2} result The object into which to store the result.
  4922. * @returns {Cartesian2} A cartesian with the minimum components.
  4923. */
  4924. Cartesian2.minimumByComponent = function (first, second, result) {
  4925. //>>includeStart('debug', pragmas.debug);
  4926. Check.Check.typeOf.object("first", first);
  4927. Check.Check.typeOf.object("second", second);
  4928. Check.Check.typeOf.object("result", result);
  4929. //>>includeEnd('debug');
  4930. result.x = Math.min(first.x, second.x);
  4931. result.y = Math.min(first.y, second.y);
  4932. return result;
  4933. };
  4934. /**
  4935. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  4936. *
  4937. * @param {Cartesian2} first A cartesian to compare.
  4938. * @param {Cartesian2} second A cartesian to compare.
  4939. * @param {Cartesian2} result The object into which to store the result.
  4940. * @returns {Cartesian2} A cartesian with the maximum components.
  4941. */
  4942. Cartesian2.maximumByComponent = function (first, second, result) {
  4943. //>>includeStart('debug', pragmas.debug);
  4944. Check.Check.typeOf.object("first", first);
  4945. Check.Check.typeOf.object("second", second);
  4946. Check.Check.typeOf.object("result", result);
  4947. //>>includeEnd('debug');
  4948. result.x = Math.max(first.x, second.x);
  4949. result.y = Math.max(first.y, second.y);
  4950. return result;
  4951. };
  4952. /**
  4953. * Constrain a value to lie between two values.
  4954. *
  4955. * @param {Cartesian2} value The value to clamp.
  4956. * @param {Cartesian2} min The minimum bound.
  4957. * @param {Cartesian2} max The maximum bound.
  4958. * @param {Cartesian2} result The object into which to store the result.
  4959. * @returns {Cartesian2} The clamped value such that min <= result <= max.
  4960. */
  4961. Cartesian2.clamp = function (value, min, max, result) {
  4962. //>>includeStart('debug', pragmas.debug);
  4963. Check.Check.typeOf.object("value", value);
  4964. Check.Check.typeOf.object("min", min);
  4965. Check.Check.typeOf.object("max", max);
  4966. Check.Check.typeOf.object("result", result);
  4967. //>>includeEnd('debug');
  4968. const x = Math$1.CesiumMath.clamp(value.x, min.x, max.x);
  4969. const y = Math$1.CesiumMath.clamp(value.y, min.y, max.y);
  4970. result.x = x;
  4971. result.y = y;
  4972. return result;
  4973. };
  4974. /**
  4975. * Computes the provided Cartesian's squared magnitude.
  4976. *
  4977. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  4978. * @returns {number} The squared magnitude.
  4979. */
  4980. Cartesian2.magnitudeSquared = function (cartesian) {
  4981. //>>includeStart('debug', pragmas.debug);
  4982. Check.Check.typeOf.object("cartesian", cartesian);
  4983. //>>includeEnd('debug');
  4984. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  4985. };
  4986. /**
  4987. * Computes the Cartesian's magnitude (length).
  4988. *
  4989. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  4990. * @returns {number} The magnitude.
  4991. */
  4992. Cartesian2.magnitude = function (cartesian) {
  4993. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  4994. };
  4995. const distanceScratch = new Cartesian2();
  4996. /**
  4997. * Computes the distance between two points.
  4998. *
  4999. * @param {Cartesian2} left The first point to compute the distance from.
  5000. * @param {Cartesian2} right The second point to compute the distance to.
  5001. * @returns {number} The distance between two points.
  5002. *
  5003. * @example
  5004. * // Returns 1.0
  5005. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  5006. */
  5007. Cartesian2.distance = function (left, right) {
  5008. //>>includeStart('debug', pragmas.debug);
  5009. Check.Check.typeOf.object("left", left);
  5010. Check.Check.typeOf.object("right", right);
  5011. //>>includeEnd('debug');
  5012. Cartesian2.subtract(left, right, distanceScratch);
  5013. return Cartesian2.magnitude(distanceScratch);
  5014. };
  5015. /**
  5016. * Computes the squared distance between two points. Comparing squared distances
  5017. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  5018. *
  5019. * @param {Cartesian2} left The first point to compute the distance from.
  5020. * @param {Cartesian2} right The second point to compute the distance to.
  5021. * @returns {number} The distance between two points.
  5022. *
  5023. * @example
  5024. * // Returns 4.0, not 2.0
  5025. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  5026. */
  5027. Cartesian2.distanceSquared = function (left, right) {
  5028. //>>includeStart('debug', pragmas.debug);
  5029. Check.Check.typeOf.object("left", left);
  5030. Check.Check.typeOf.object("right", right);
  5031. //>>includeEnd('debug');
  5032. Cartesian2.subtract(left, right, distanceScratch);
  5033. return Cartesian2.magnitudeSquared(distanceScratch);
  5034. };
  5035. /**
  5036. * Computes the normalized form of the supplied Cartesian.
  5037. *
  5038. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  5039. * @param {Cartesian2} result The object onto which to store the result.
  5040. * @returns {Cartesian2} The modified result parameter.
  5041. */
  5042. Cartesian2.normalize = function (cartesian, result) {
  5043. //>>includeStart('debug', pragmas.debug);
  5044. Check.Check.typeOf.object("cartesian", cartesian);
  5045. Check.Check.typeOf.object("result", result);
  5046. //>>includeEnd('debug');
  5047. const magnitude = Cartesian2.magnitude(cartesian);
  5048. result.x = cartesian.x / magnitude;
  5049. result.y = cartesian.y / magnitude;
  5050. //>>includeStart('debug', pragmas.debug);
  5051. if (isNaN(result.x) || isNaN(result.y)) {
  5052. throw new Check.DeveloperError("normalized result is not a number");
  5053. }
  5054. //>>includeEnd('debug');
  5055. return result;
  5056. };
  5057. /**
  5058. * Computes the dot (scalar) product of two Cartesians.
  5059. *
  5060. * @param {Cartesian2} left The first Cartesian.
  5061. * @param {Cartesian2} right The second Cartesian.
  5062. * @returns {number} The dot product.
  5063. */
  5064. Cartesian2.dot = function (left, right) {
  5065. //>>includeStart('debug', pragmas.debug);
  5066. Check.Check.typeOf.object("left", left);
  5067. Check.Check.typeOf.object("right", right);
  5068. //>>includeEnd('debug');
  5069. return left.x * right.x + left.y * right.y;
  5070. };
  5071. /**
  5072. * Computes the magnitude of the cross product that would result from implicitly setting the Z coordinate of the input vectors to 0
  5073. *
  5074. * @param {Cartesian2} left The first Cartesian.
  5075. * @param {Cartesian2} right The second Cartesian.
  5076. * @returns {number} The cross product.
  5077. */
  5078. Cartesian2.cross = function (left, right) {
  5079. //>>includeStart('debug', pragmas.debug);
  5080. Check.Check.typeOf.object("left", left);
  5081. Check.Check.typeOf.object("right", right);
  5082. //>>includeEnd('debug');
  5083. return left.x * right.y - left.y * right.x;
  5084. };
  5085. /**
  5086. * Computes the componentwise product of two Cartesians.
  5087. *
  5088. * @param {Cartesian2} left The first Cartesian.
  5089. * @param {Cartesian2} right The second Cartesian.
  5090. * @param {Cartesian2} result The object onto which to store the result.
  5091. * @returns {Cartesian2} The modified result parameter.
  5092. */
  5093. Cartesian2.multiplyComponents = function (left, right, result) {
  5094. //>>includeStart('debug', pragmas.debug);
  5095. Check.Check.typeOf.object("left", left);
  5096. Check.Check.typeOf.object("right", right);
  5097. Check.Check.typeOf.object("result", result);
  5098. //>>includeEnd('debug');
  5099. result.x = left.x * right.x;
  5100. result.y = left.y * right.y;
  5101. return result;
  5102. };
  5103. /**
  5104. * Computes the componentwise quotient of two Cartesians.
  5105. *
  5106. * @param {Cartesian2} left The first Cartesian.
  5107. * @param {Cartesian2} right The second Cartesian.
  5108. * @param {Cartesian2} result The object onto which to store the result.
  5109. * @returns {Cartesian2} The modified result parameter.
  5110. */
  5111. Cartesian2.divideComponents = function (left, right, result) {
  5112. //>>includeStart('debug', pragmas.debug);
  5113. Check.Check.typeOf.object("left", left);
  5114. Check.Check.typeOf.object("right", right);
  5115. Check.Check.typeOf.object("result", result);
  5116. //>>includeEnd('debug');
  5117. result.x = left.x / right.x;
  5118. result.y = left.y / right.y;
  5119. return result;
  5120. };
  5121. /**
  5122. * Computes the componentwise sum of two Cartesians.
  5123. *
  5124. * @param {Cartesian2} left The first Cartesian.
  5125. * @param {Cartesian2} right The second Cartesian.
  5126. * @param {Cartesian2} result The object onto which to store the result.
  5127. * @returns {Cartesian2} The modified result parameter.
  5128. */
  5129. Cartesian2.add = function (left, right, result) {
  5130. //>>includeStart('debug', pragmas.debug);
  5131. Check.Check.typeOf.object("left", left);
  5132. Check.Check.typeOf.object("right", right);
  5133. Check.Check.typeOf.object("result", result);
  5134. //>>includeEnd('debug');
  5135. result.x = left.x + right.x;
  5136. result.y = left.y + right.y;
  5137. return result;
  5138. };
  5139. /**
  5140. * Computes the componentwise difference of two Cartesians.
  5141. *
  5142. * @param {Cartesian2} left The first Cartesian.
  5143. * @param {Cartesian2} right The second Cartesian.
  5144. * @param {Cartesian2} result The object onto which to store the result.
  5145. * @returns {Cartesian2} The modified result parameter.
  5146. */
  5147. Cartesian2.subtract = function (left, right, result) {
  5148. //>>includeStart('debug', pragmas.debug);
  5149. Check.Check.typeOf.object("left", left);
  5150. Check.Check.typeOf.object("right", right);
  5151. Check.Check.typeOf.object("result", result);
  5152. //>>includeEnd('debug');
  5153. result.x = left.x - right.x;
  5154. result.y = left.y - right.y;
  5155. return result;
  5156. };
  5157. /**
  5158. * Multiplies the provided Cartesian componentwise by the provided scalar.
  5159. *
  5160. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  5161. * @param {number} scalar The scalar to multiply with.
  5162. * @param {Cartesian2} result The object onto which to store the result.
  5163. * @returns {Cartesian2} The modified result parameter.
  5164. */
  5165. Cartesian2.multiplyByScalar = function (cartesian, scalar, result) {
  5166. //>>includeStart('debug', pragmas.debug);
  5167. Check.Check.typeOf.object("cartesian", cartesian);
  5168. Check.Check.typeOf.number("scalar", scalar);
  5169. Check.Check.typeOf.object("result", result);
  5170. //>>includeEnd('debug');
  5171. result.x = cartesian.x * scalar;
  5172. result.y = cartesian.y * scalar;
  5173. return result;
  5174. };
  5175. /**
  5176. * Divides the provided Cartesian componentwise by the provided scalar.
  5177. *
  5178. * @param {Cartesian2} cartesian The Cartesian to be divided.
  5179. * @param {number} scalar The scalar to divide by.
  5180. * @param {Cartesian2} result The object onto which to store the result.
  5181. * @returns {Cartesian2} The modified result parameter.
  5182. */
  5183. Cartesian2.divideByScalar = function (cartesian, scalar, result) {
  5184. //>>includeStart('debug', pragmas.debug);
  5185. Check.Check.typeOf.object("cartesian", cartesian);
  5186. Check.Check.typeOf.number("scalar", scalar);
  5187. Check.Check.typeOf.object("result", result);
  5188. //>>includeEnd('debug');
  5189. result.x = cartesian.x / scalar;
  5190. result.y = cartesian.y / scalar;
  5191. return result;
  5192. };
  5193. /**
  5194. * Negates the provided Cartesian.
  5195. *
  5196. * @param {Cartesian2} cartesian The Cartesian to be negated.
  5197. * @param {Cartesian2} result The object onto which to store the result.
  5198. * @returns {Cartesian2} The modified result parameter.
  5199. */
  5200. Cartesian2.negate = function (cartesian, result) {
  5201. //>>includeStart('debug', pragmas.debug);
  5202. Check.Check.typeOf.object("cartesian", cartesian);
  5203. Check.Check.typeOf.object("result", result);
  5204. //>>includeEnd('debug');
  5205. result.x = -cartesian.x;
  5206. result.y = -cartesian.y;
  5207. return result;
  5208. };
  5209. /**
  5210. * Computes the absolute value of the provided Cartesian.
  5211. *
  5212. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  5213. * @param {Cartesian2} result The object onto which to store the result.
  5214. * @returns {Cartesian2} The modified result parameter.
  5215. */
  5216. Cartesian2.abs = function (cartesian, result) {
  5217. //>>includeStart('debug', pragmas.debug);
  5218. Check.Check.typeOf.object("cartesian", cartesian);
  5219. Check.Check.typeOf.object("result", result);
  5220. //>>includeEnd('debug');
  5221. result.x = Math.abs(cartesian.x);
  5222. result.y = Math.abs(cartesian.y);
  5223. return result;
  5224. };
  5225. const lerpScratch = new Cartesian2();
  5226. /**
  5227. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  5228. *
  5229. * @param {Cartesian2} start The value corresponding to t at 0.0.
  5230. * @param {Cartesian2} end The value corresponding to t at 1.0.
  5231. * @param {number} t The point along t at which to interpolate.
  5232. * @param {Cartesian2} result The object onto which to store the result.
  5233. * @returns {Cartesian2} The modified result parameter.
  5234. */
  5235. Cartesian2.lerp = function (start, end, t, result) {
  5236. //>>includeStart('debug', pragmas.debug);
  5237. Check.Check.typeOf.object("start", start);
  5238. Check.Check.typeOf.object("end", end);
  5239. Check.Check.typeOf.number("t", t);
  5240. Check.Check.typeOf.object("result", result);
  5241. //>>includeEnd('debug');
  5242. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  5243. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  5244. return Cartesian2.add(lerpScratch, result, result);
  5245. };
  5246. const angleBetweenScratch = new Cartesian2();
  5247. const angleBetweenScratch2 = new Cartesian2();
  5248. /**
  5249. * Returns the angle, in radians, between the provided Cartesians.
  5250. *
  5251. * @param {Cartesian2} left The first Cartesian.
  5252. * @param {Cartesian2} right The second Cartesian.
  5253. * @returns {number} The angle between the Cartesians.
  5254. */
  5255. Cartesian2.angleBetween = function (left, right) {
  5256. //>>includeStart('debug', pragmas.debug);
  5257. Check.Check.typeOf.object("left", left);
  5258. Check.Check.typeOf.object("right", right);
  5259. //>>includeEnd('debug');
  5260. Cartesian2.normalize(left, angleBetweenScratch);
  5261. Cartesian2.normalize(right, angleBetweenScratch2);
  5262. return Math$1.CesiumMath.acosClamped(
  5263. Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2)
  5264. );
  5265. };
  5266. const mostOrthogonalAxisScratch = new Cartesian2();
  5267. /**
  5268. * Returns the axis that is most orthogonal to the provided Cartesian.
  5269. *
  5270. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  5271. * @param {Cartesian2} result The object onto which to store the result.
  5272. * @returns {Cartesian2} The most orthogonal axis.
  5273. */
  5274. Cartesian2.mostOrthogonalAxis = function (cartesian, result) {
  5275. //>>includeStart('debug', pragmas.debug);
  5276. Check.Check.typeOf.object("cartesian", cartesian);
  5277. Check.Check.typeOf.object("result", result);
  5278. //>>includeEnd('debug');
  5279. const f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  5280. Cartesian2.abs(f, f);
  5281. if (f.x <= f.y) {
  5282. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  5283. } else {
  5284. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  5285. }
  5286. return result;
  5287. };
  5288. /**
  5289. * Compares the provided Cartesians componentwise and returns
  5290. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5291. *
  5292. * @param {Cartesian2} [left] The first Cartesian.
  5293. * @param {Cartesian2} [right] The second Cartesian.
  5294. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  5295. */
  5296. Cartesian2.equals = function (left, right) {
  5297. return (
  5298. left === right ||
  5299. (defaultValue.defined(left) &&
  5300. defaultValue.defined(right) &&
  5301. left.x === right.x &&
  5302. left.y === right.y)
  5303. );
  5304. };
  5305. /**
  5306. * @private
  5307. */
  5308. Cartesian2.equalsArray = function (cartesian, array, offset) {
  5309. return cartesian.x === array[offset] && cartesian.y === array[offset + 1];
  5310. };
  5311. /**
  5312. * Compares the provided Cartesians componentwise and returns
  5313. * <code>true</code> if they pass an absolute or relative tolerance test,
  5314. * <code>false</code> otherwise.
  5315. *
  5316. * @param {Cartesian2} [left] The first Cartesian.
  5317. * @param {Cartesian2} [right] The second Cartesian.
  5318. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  5319. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  5320. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  5321. */
  5322. Cartesian2.equalsEpsilon = function (
  5323. left,
  5324. right,
  5325. relativeEpsilon,
  5326. absoluteEpsilon
  5327. ) {
  5328. return (
  5329. left === right ||
  5330. (defaultValue.defined(left) &&
  5331. defaultValue.defined(right) &&
  5332. Math$1.CesiumMath.equalsEpsilon(
  5333. left.x,
  5334. right.x,
  5335. relativeEpsilon,
  5336. absoluteEpsilon
  5337. ) &&
  5338. Math$1.CesiumMath.equalsEpsilon(
  5339. left.y,
  5340. right.y,
  5341. relativeEpsilon,
  5342. absoluteEpsilon
  5343. ))
  5344. );
  5345. };
  5346. /**
  5347. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  5348. *
  5349. * @type {Cartesian2}
  5350. * @constant
  5351. */
  5352. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  5353. /**
  5354. * An immutable Cartesian2 instance initialized to (1.0, 1.0).
  5355. *
  5356. * @type {Cartesian2}
  5357. * @constant
  5358. */
  5359. Cartesian2.ONE = Object.freeze(new Cartesian2(1.0, 1.0));
  5360. /**
  5361. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  5362. *
  5363. * @type {Cartesian2}
  5364. * @constant
  5365. */
  5366. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  5367. /**
  5368. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  5369. *
  5370. * @type {Cartesian2}
  5371. * @constant
  5372. */
  5373. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  5374. /**
  5375. * Duplicates this Cartesian2 instance.
  5376. *
  5377. * @param {Cartesian2} [result] The object onto which to store the result.
  5378. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  5379. */
  5380. Cartesian2.prototype.clone = function (result) {
  5381. return Cartesian2.clone(this, result);
  5382. };
  5383. /**
  5384. * Compares this Cartesian against the provided Cartesian componentwise and returns
  5385. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5386. *
  5387. * @param {Cartesian2} [right] The right hand side Cartesian.
  5388. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  5389. */
  5390. Cartesian2.prototype.equals = function (right) {
  5391. return Cartesian2.equals(this, right);
  5392. };
  5393. /**
  5394. * Compares this Cartesian against the provided Cartesian componentwise and returns
  5395. * <code>true</code> if they pass an absolute or relative tolerance test,
  5396. * <code>false</code> otherwise.
  5397. *
  5398. * @param {Cartesian2} [right] The right hand side Cartesian.
  5399. * @param {number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  5400. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  5401. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  5402. */
  5403. Cartesian2.prototype.equalsEpsilon = function (
  5404. right,
  5405. relativeEpsilon,
  5406. absoluteEpsilon
  5407. ) {
  5408. return Cartesian2.equalsEpsilon(
  5409. this,
  5410. right,
  5411. relativeEpsilon,
  5412. absoluteEpsilon
  5413. );
  5414. };
  5415. /**
  5416. * Creates a string representing this Cartesian in the format '(x, y)'.
  5417. *
  5418. * @returns {string} A string representing the provided Cartesian in the format '(x, y)'.
  5419. */
  5420. Cartesian2.prototype.toString = function () {
  5421. return `(${this.x}, ${this.y})`;
  5422. };
  5423. /**
  5424. * A 2x2 matrix, indexable as a column-major order array.
  5425. * Constructor parameters are in row-major order for code readability.
  5426. * @alias Matrix2
  5427. * @constructor
  5428. * @implements {ArrayLike<number>}
  5429. *
  5430. * @param {number} [column0Row0=0.0] The value for column 0, row 0.
  5431. * @param {number} [column1Row0=0.0] The value for column 1, row 0.
  5432. * @param {number} [column0Row1=0.0] The value for column 0, row 1.
  5433. * @param {number} [column1Row1=0.0] The value for column 1, row 1.
  5434. *
  5435. * @see Matrix2.fromArray
  5436. * @see Matrix2.fromColumnMajorArray
  5437. * @see Matrix2.fromRowMajorArray
  5438. * @see Matrix2.fromScale
  5439. * @see Matrix2.fromUniformScale
  5440. * @see Matrix2.fromRotation
  5441. * @see Matrix3
  5442. * @see Matrix4
  5443. */
  5444. function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
  5445. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  5446. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  5447. this[2] = defaultValue.defaultValue(column1Row0, 0.0);
  5448. this[3] = defaultValue.defaultValue(column1Row1, 0.0);
  5449. }
  5450. /**
  5451. * The number of elements used to pack the object into an array.
  5452. * @type {number}
  5453. */
  5454. Matrix2.packedLength = 4;
  5455. /**
  5456. * Stores the provided instance into the provided array.
  5457. *
  5458. * @param {Matrix2} value The value to pack.
  5459. * @param {number[]} array The array to pack into.
  5460. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  5461. *
  5462. * @returns {number[]} The array that was packed into
  5463. */
  5464. Matrix2.pack = function (value, array, startingIndex) {
  5465. //>>includeStart('debug', pragmas.debug);
  5466. Check.Check.typeOf.object("value", value);
  5467. Check.Check.defined("array", array);
  5468. //>>includeEnd('debug');
  5469. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  5470. array[startingIndex++] = value[0];
  5471. array[startingIndex++] = value[1];
  5472. array[startingIndex++] = value[2];
  5473. array[startingIndex++] = value[3];
  5474. return array;
  5475. };
  5476. /**
  5477. * Retrieves an instance from a packed array.
  5478. *
  5479. * @param {number[]} array The packed array.
  5480. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  5481. * @param {Matrix2} [result] The object into which to store the result.
  5482. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  5483. */
  5484. Matrix2.unpack = function (array, startingIndex, result) {
  5485. //>>includeStart('debug', pragmas.debug);
  5486. Check.Check.defined("array", array);
  5487. //>>includeEnd('debug');
  5488. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  5489. if (!defaultValue.defined(result)) {
  5490. result = new Matrix2();
  5491. }
  5492. result[0] = array[startingIndex++];
  5493. result[1] = array[startingIndex++];
  5494. result[2] = array[startingIndex++];
  5495. result[3] = array[startingIndex++];
  5496. return result;
  5497. };
  5498. /**
  5499. * Flattens an array of Matrix2s into an array of components. The components
  5500. * are stored in column-major order.
  5501. *
  5502. * @param {Matrix2[]} array The array of matrices to pack.
  5503. * @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 4 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 4) elements.
  5504. * @returns {number[]} The packed array.
  5505. */
  5506. Matrix2.packArray = function (array, result) {
  5507. //>>includeStart('debug', pragmas.debug);
  5508. Check.Check.defined("array", array);
  5509. //>>includeEnd('debug');
  5510. const length = array.length;
  5511. const resultLength = length * 4;
  5512. if (!defaultValue.defined(result)) {
  5513. result = new Array(resultLength);
  5514. } else if (!Array.isArray(result) && result.length !== resultLength) {
  5515. //>>includeStart('debug', pragmas.debug);
  5516. throw new Check.DeveloperError(
  5517. "If result is a typed array, it must have exactly array.length * 4 elements"
  5518. );
  5519. //>>includeEnd('debug');
  5520. } else if (result.length !== resultLength) {
  5521. result.length = resultLength;
  5522. }
  5523. for (let i = 0; i < length; ++i) {
  5524. Matrix2.pack(array[i], result, i * 4);
  5525. }
  5526. return result;
  5527. };
  5528. /**
  5529. * Unpacks an array of column-major matrix components into an array of Matrix2s.
  5530. *
  5531. * @param {number[]} array The array of components to unpack.
  5532. * @param {Matrix2[]} [result] The array onto which to store the result.
  5533. * @returns {Matrix2[]} The unpacked array.
  5534. */
  5535. Matrix2.unpackArray = function (array, result) {
  5536. //>>includeStart('debug', pragmas.debug);
  5537. Check.Check.defined("array", array);
  5538. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  5539. if (array.length % 4 !== 0) {
  5540. throw new Check.DeveloperError("array length must be a multiple of 4.");
  5541. }
  5542. //>>includeEnd('debug');
  5543. const length = array.length;
  5544. if (!defaultValue.defined(result)) {
  5545. result = new Array(length / 4);
  5546. } else {
  5547. result.length = length / 4;
  5548. }
  5549. for (let i = 0; i < length; i += 4) {
  5550. const index = i / 4;
  5551. result[index] = Matrix2.unpack(array, i, result[index]);
  5552. }
  5553. return result;
  5554. };
  5555. /**
  5556. * Duplicates a Matrix2 instance.
  5557. *
  5558. * @param {Matrix2} matrix The matrix to duplicate.
  5559. * @param {Matrix2} [result] The object onto which to store the result.
  5560. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
  5561. */
  5562. Matrix2.clone = function (matrix, result) {
  5563. if (!defaultValue.defined(matrix)) {
  5564. return undefined;
  5565. }
  5566. if (!defaultValue.defined(result)) {
  5567. return new Matrix2(matrix[0], matrix[2], matrix[1], matrix[3]);
  5568. }
  5569. result[0] = matrix[0];
  5570. result[1] = matrix[1];
  5571. result[2] = matrix[2];
  5572. result[3] = matrix[3];
  5573. return result;
  5574. };
  5575. /**
  5576. * Creates a Matrix2 from 4 consecutive elements in an array.
  5577. *
  5578. * @function
  5579. * @param {number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  5580. * @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  5581. * @param {Matrix2} [result] The object onto which to store the result.
  5582. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  5583. *
  5584. * @example
  5585. * // Create the Matrix2:
  5586. * // [1.0, 2.0]
  5587. * // [1.0, 2.0]
  5588. *
  5589. * const v = [1.0, 1.0, 2.0, 2.0];
  5590. * const m = Cesium.Matrix2.fromArray(v);
  5591. *
  5592. * // Create same Matrix2 with using an offset into an array
  5593. * const v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
  5594. * const m2 = Cesium.Matrix2.fromArray(v2, 2);
  5595. */
  5596. Matrix2.fromArray = Matrix2.unpack;
  5597. /**
  5598. * Creates a Matrix2 instance from a column-major order array.
  5599. *
  5600. * @param {number[]} values The column-major order array.
  5601. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5602. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  5603. */
  5604. Matrix2.fromColumnMajorArray = function (values, result) {
  5605. //>>includeStart('debug', pragmas.debug);
  5606. Check.Check.defined("values", values);
  5607. //>>includeEnd('debug');
  5608. return Matrix2.clone(values, result);
  5609. };
  5610. /**
  5611. * Creates a Matrix2 instance from a row-major order array.
  5612. * The resulting matrix will be in column-major order.
  5613. *
  5614. * @param {number[]} values The row-major order array.
  5615. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5616. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  5617. */
  5618. Matrix2.fromRowMajorArray = function (values, result) {
  5619. //>>includeStart('debug', pragmas.debug);
  5620. Check.Check.defined("values", values);
  5621. //>>includeEnd('debug');
  5622. if (!defaultValue.defined(result)) {
  5623. return new Matrix2(values[0], values[1], values[2], values[3]);
  5624. }
  5625. result[0] = values[0];
  5626. result[1] = values[2];
  5627. result[2] = values[1];
  5628. result[3] = values[3];
  5629. return result;
  5630. };
  5631. /**
  5632. * Computes a Matrix2 instance representing a non-uniform scale.
  5633. *
  5634. * @param {Cartesian2} scale The x and y scale factors.
  5635. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5636. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  5637. *
  5638. * @example
  5639. * // Creates
  5640. * // [7.0, 0.0]
  5641. * // [0.0, 8.0]
  5642. * const m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
  5643. */
  5644. Matrix2.fromScale = function (scale, result) {
  5645. //>>includeStart('debug', pragmas.debug);
  5646. Check.Check.typeOf.object("scale", scale);
  5647. //>>includeEnd('debug');
  5648. if (!defaultValue.defined(result)) {
  5649. return new Matrix2(scale.x, 0.0, 0.0, scale.y);
  5650. }
  5651. result[0] = scale.x;
  5652. result[1] = 0.0;
  5653. result[2] = 0.0;
  5654. result[3] = scale.y;
  5655. return result;
  5656. };
  5657. /**
  5658. * Computes a Matrix2 instance representing a uniform scale.
  5659. *
  5660. * @param {number} scale The uniform scale factor.
  5661. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5662. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  5663. *
  5664. * @example
  5665. * // Creates
  5666. * // [2.0, 0.0]
  5667. * // [0.0, 2.0]
  5668. * const m = Cesium.Matrix2.fromUniformScale(2.0);
  5669. */
  5670. Matrix2.fromUniformScale = function (scale, result) {
  5671. //>>includeStart('debug', pragmas.debug);
  5672. Check.Check.typeOf.number("scale", scale);
  5673. //>>includeEnd('debug');
  5674. if (!defaultValue.defined(result)) {
  5675. return new Matrix2(scale, 0.0, 0.0, scale);
  5676. }
  5677. result[0] = scale;
  5678. result[1] = 0.0;
  5679. result[2] = 0.0;
  5680. result[3] = scale;
  5681. return result;
  5682. };
  5683. /**
  5684. * Creates a rotation matrix.
  5685. *
  5686. * @param {number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  5687. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5688. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  5689. *
  5690. * @example
  5691. * // Rotate a point 45 degrees counterclockwise.
  5692. * const p = new Cesium.Cartesian2(5, 6);
  5693. * const m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
  5694. * const rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
  5695. */
  5696. Matrix2.fromRotation = function (angle, result) {
  5697. //>>includeStart('debug', pragmas.debug);
  5698. Check.Check.typeOf.number("angle", angle);
  5699. //>>includeEnd('debug');
  5700. const cosAngle = Math.cos(angle);
  5701. const sinAngle = Math.sin(angle);
  5702. if (!defaultValue.defined(result)) {
  5703. return new Matrix2(cosAngle, -sinAngle, sinAngle, cosAngle);
  5704. }
  5705. result[0] = cosAngle;
  5706. result[1] = sinAngle;
  5707. result[2] = -sinAngle;
  5708. result[3] = cosAngle;
  5709. return result;
  5710. };
  5711. /**
  5712. * Creates an Array from the provided Matrix2 instance.
  5713. * The array will be in column-major order.
  5714. *
  5715. * @param {Matrix2} matrix The matrix to use..
  5716. * @param {number[]} [result] The Array onto which to store the result.
  5717. * @returns {number[]} The modified Array parameter or a new Array instance if one was not provided.
  5718. */
  5719. Matrix2.toArray = function (matrix, result) {
  5720. //>>includeStart('debug', pragmas.debug);
  5721. Check.Check.typeOf.object("matrix", matrix);
  5722. //>>includeEnd('debug');
  5723. if (!defaultValue.defined(result)) {
  5724. return [matrix[0], matrix[1], matrix[2], matrix[3]];
  5725. }
  5726. result[0] = matrix[0];
  5727. result[1] = matrix[1];
  5728. result[2] = matrix[2];
  5729. result[3] = matrix[3];
  5730. return result;
  5731. };
  5732. /**
  5733. * Computes the array index of the element at the provided row and column.
  5734. *
  5735. * @param {number} row The zero-based index of the row.
  5736. * @param {number} column The zero-based index of the column.
  5737. * @returns {number} The index of the element at the provided row and column.
  5738. *
  5739. * @exception {DeveloperError} row must be 0 or 1.
  5740. * @exception {DeveloperError} column must be 0 or 1.
  5741. *
  5742. * @example
  5743. * const myMatrix = new Cesium.Matrix2();
  5744. * const column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
  5745. * const column1Row0 = myMatrix[column1Row0Index]
  5746. * myMatrix[column1Row0Index] = 10.0;
  5747. */
  5748. Matrix2.getElementIndex = function (column, row) {
  5749. //>>includeStart('debug', pragmas.debug);
  5750. Check.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  5751. Check.Check.typeOf.number.lessThanOrEquals("row", row, 1);
  5752. Check.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  5753. Check.Check.typeOf.number.lessThanOrEquals("column", column, 1);
  5754. //>>includeEnd('debug');
  5755. return column * 2 + row;
  5756. };
  5757. /**
  5758. * Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
  5759. *
  5760. * @param {Matrix2} matrix The matrix to use.
  5761. * @param {number} index The zero-based index of the column to retrieve.
  5762. * @param {Cartesian2} result The object onto which to store the result.
  5763. * @returns {Cartesian2} The modified result parameter.
  5764. *
  5765. * @exception {DeveloperError} index must be 0 or 1.
  5766. */
  5767. Matrix2.getColumn = function (matrix, index, result) {
  5768. //>>includeStart('debug', pragmas.debug);
  5769. Check.Check.typeOf.object("matrix", matrix);
  5770. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5771. Check.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  5772. Check.Check.typeOf.object("result", result);
  5773. //>>includeEnd('debug');
  5774. const startIndex = index * 2;
  5775. const x = matrix[startIndex];
  5776. const y = matrix[startIndex + 1];
  5777. result.x = x;
  5778. result.y = y;
  5779. return result;
  5780. };
  5781. /**
  5782. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
  5783. *
  5784. * @param {Matrix2} matrix The matrix to use.
  5785. * @param {number} index The zero-based index of the column to set.
  5786. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
  5787. * @param {Cartesian2} result The object onto which to store the result.
  5788. * @returns {Matrix2} The modified result parameter.
  5789. *
  5790. * @exception {DeveloperError} index must be 0 or 1.
  5791. */
  5792. Matrix2.setColumn = function (matrix, index, cartesian, result) {
  5793. //>>includeStart('debug', pragmas.debug);
  5794. Check.Check.typeOf.object("matrix", matrix);
  5795. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5796. Check.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  5797. Check.Check.typeOf.object("cartesian", cartesian);
  5798. Check.Check.typeOf.object("result", result);
  5799. //>>includeEnd('debug');
  5800. result = Matrix2.clone(matrix, result);
  5801. const startIndex = index * 2;
  5802. result[startIndex] = cartesian.x;
  5803. result[startIndex + 1] = cartesian.y;
  5804. return result;
  5805. };
  5806. /**
  5807. * Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
  5808. *
  5809. * @param {Matrix2} matrix The matrix to use.
  5810. * @param {number} index The zero-based index of the row to retrieve.
  5811. * @param {Cartesian2} result The object onto which to store the result.
  5812. * @returns {Cartesian2} The modified result parameter.
  5813. *
  5814. * @exception {DeveloperError} index must be 0 or 1.
  5815. */
  5816. Matrix2.getRow = function (matrix, index, result) {
  5817. //>>includeStart('debug', pragmas.debug);
  5818. Check.Check.typeOf.object("matrix", matrix);
  5819. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5820. Check.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  5821. Check.Check.typeOf.object("result", result);
  5822. //>>includeEnd('debug');
  5823. const x = matrix[index];
  5824. const y = matrix[index + 2];
  5825. result.x = x;
  5826. result.y = y;
  5827. return result;
  5828. };
  5829. /**
  5830. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
  5831. *
  5832. * @param {Matrix2} matrix The matrix to use.
  5833. * @param {number} index The zero-based index of the row to set.
  5834. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
  5835. * @param {Matrix2} result The object onto which to store the result.
  5836. * @returns {Matrix2} The modified result parameter.
  5837. *
  5838. * @exception {DeveloperError} index must be 0 or 1.
  5839. */
  5840. Matrix2.setRow = function (matrix, index, cartesian, result) {
  5841. //>>includeStart('debug', pragmas.debug);
  5842. Check.Check.typeOf.object("matrix", matrix);
  5843. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5844. Check.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  5845. Check.Check.typeOf.object("cartesian", cartesian);
  5846. Check.Check.typeOf.object("result", result);
  5847. //>>includeEnd('debug');
  5848. result = Matrix2.clone(matrix, result);
  5849. result[index] = cartesian.x;
  5850. result[index + 2] = cartesian.y;
  5851. return result;
  5852. };
  5853. const scaleScratch1 = new Cartesian2();
  5854. /**
  5855. * Computes a new matrix that replaces the scale with the provided scale.
  5856. * This assumes the matrix is an affine transformation.
  5857. *
  5858. * @param {Matrix2} matrix The matrix to use.
  5859. * @param {Cartesian2} scale The scale that replaces the scale of the provided matrix.
  5860. * @param {Matrix2} result The object onto which to store the result.
  5861. * @returns {Matrix2} The modified result parameter.
  5862. *
  5863. * @see Matrix2.setUniformScale
  5864. * @see Matrix2.fromScale
  5865. * @see Matrix2.fromUniformScale
  5866. * @see Matrix2.multiplyByScale
  5867. * @see Matrix2.multiplyByUniformScale
  5868. * @see Matrix2.getScale
  5869. */
  5870. Matrix2.setScale = function (matrix, scale, result) {
  5871. //>>includeStart('debug', pragmas.debug);
  5872. Check.Check.typeOf.object("matrix", matrix);
  5873. Check.Check.typeOf.object("scale", scale);
  5874. Check.Check.typeOf.object("result", result);
  5875. //>>includeEnd('debug');
  5876. const existingScale = Matrix2.getScale(matrix, scaleScratch1);
  5877. const scaleRatioX = scale.x / existingScale.x;
  5878. const scaleRatioY = scale.y / existingScale.y;
  5879. result[0] = matrix[0] * scaleRatioX;
  5880. result[1] = matrix[1] * scaleRatioX;
  5881. result[2] = matrix[2] * scaleRatioY;
  5882. result[3] = matrix[3] * scaleRatioY;
  5883. return result;
  5884. };
  5885. const scaleScratch2 = new Cartesian2();
  5886. /**
  5887. * Computes a new matrix that replaces the scale with the provided uniform scale.
  5888. * This assumes the matrix is an affine transformation.
  5889. *
  5890. * @param {Matrix2} matrix The matrix to use.
  5891. * @param {number} scale The uniform scale that replaces the scale of the provided matrix.
  5892. * @param {Matrix2} result The object onto which to store the result.
  5893. * @returns {Matrix2} The modified result parameter.
  5894. *
  5895. * @see Matrix2.setScale
  5896. * @see Matrix2.fromScale
  5897. * @see Matrix2.fromUniformScale
  5898. * @see Matrix2.multiplyByScale
  5899. * @see Matrix2.multiplyByUniformScale
  5900. * @see Matrix2.getScale
  5901. */
  5902. Matrix2.setUniformScale = function (matrix, scale, result) {
  5903. //>>includeStart('debug', pragmas.debug);
  5904. Check.Check.typeOf.object("matrix", matrix);
  5905. Check.Check.typeOf.number("scale", scale);
  5906. Check.Check.typeOf.object("result", result);
  5907. //>>includeEnd('debug');
  5908. const existingScale = Matrix2.getScale(matrix, scaleScratch2);
  5909. const scaleRatioX = scale / existingScale.x;
  5910. const scaleRatioY = scale / existingScale.y;
  5911. result[0] = matrix[0] * scaleRatioX;
  5912. result[1] = matrix[1] * scaleRatioX;
  5913. result[2] = matrix[2] * scaleRatioY;
  5914. result[3] = matrix[3] * scaleRatioY;
  5915. return result;
  5916. };
  5917. const scratchColumn = new Cartesian2();
  5918. /**
  5919. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  5920. *
  5921. * @param {Matrix2} matrix The matrix.
  5922. * @param {Cartesian2} result The object onto which to store the result.
  5923. * @returns {Cartesian2} The modified result parameter.
  5924. *
  5925. * @see Matrix2.multiplyByScale
  5926. * @see Matrix2.multiplyByUniformScale
  5927. * @see Matrix2.fromScale
  5928. * @see Matrix2.fromUniformScale
  5929. * @see Matrix2.setScale
  5930. * @see Matrix2.setUniformScale
  5931. */
  5932. Matrix2.getScale = function (matrix, result) {
  5933. //>>includeStart('debug', pragmas.debug);
  5934. Check.Check.typeOf.object("matrix", matrix);
  5935. Check.Check.typeOf.object("result", result);
  5936. //>>includeEnd('debug');
  5937. result.x = Cartesian2.magnitude(
  5938. Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn)
  5939. );
  5940. result.y = Cartesian2.magnitude(
  5941. Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn)
  5942. );
  5943. return result;
  5944. };
  5945. const scaleScratch3 = new Cartesian2();
  5946. /**
  5947. * Computes the maximum scale assuming the matrix is an affine transformation.
  5948. * The maximum scale is the maximum length of the column vectors.
  5949. *
  5950. * @param {Matrix2} matrix The matrix.
  5951. * @returns {number} The maximum scale.
  5952. */
  5953. Matrix2.getMaximumScale = function (matrix) {
  5954. Matrix2.getScale(matrix, scaleScratch3);
  5955. return Cartesian2.maximumComponent(scaleScratch3);
  5956. };
  5957. const scaleScratch4 = new Cartesian2();
  5958. /**
  5959. * Sets the rotation assuming the matrix is an affine transformation.
  5960. *
  5961. * @param {Matrix2} matrix The matrix.
  5962. * @param {Matrix2} rotation The rotation matrix.
  5963. * @param {Matrix2} result The object onto which to store the result.
  5964. * @returns {Matrix2} The modified result parameter.
  5965. *
  5966. * @see Matrix2.fromRotation
  5967. * @see Matrix2.getRotation
  5968. */
  5969. Matrix2.setRotation = function (matrix, rotation, result) {
  5970. //>>includeStart('debug', pragmas.debug);
  5971. Check.Check.typeOf.object("matrix", matrix);
  5972. Check.Check.typeOf.object("result", result);
  5973. //>>includeEnd('debug');
  5974. const scale = Matrix2.getScale(matrix, scaleScratch4);
  5975. result[0] = rotation[0] * scale.x;
  5976. result[1] = rotation[1] * scale.x;
  5977. result[2] = rotation[2] * scale.y;
  5978. result[3] = rotation[3] * scale.y;
  5979. return result;
  5980. };
  5981. const scaleScratch5 = new Cartesian2();
  5982. /**
  5983. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  5984. *
  5985. * @param {Matrix2} matrix The matrix.
  5986. * @param {Matrix2} result The object onto which to store the result.
  5987. * @returns {Matrix2} The modified result parameter.
  5988. *
  5989. * @see Matrix2.setRotation
  5990. * @see Matrix2.fromRotation
  5991. */
  5992. Matrix2.getRotation = function (matrix, result) {
  5993. //>>includeStart('debug', pragmas.debug);
  5994. Check.Check.typeOf.object("matrix", matrix);
  5995. Check.Check.typeOf.object("result", result);
  5996. //>>includeEnd('debug');
  5997. const scale = Matrix2.getScale(matrix, scaleScratch5);
  5998. result[0] = matrix[0] / scale.x;
  5999. result[1] = matrix[1] / scale.x;
  6000. result[2] = matrix[2] / scale.y;
  6001. result[3] = matrix[3] / scale.y;
  6002. return result;
  6003. };
  6004. /**
  6005. * Computes the product of two matrices.
  6006. *
  6007. * @param {Matrix2} left The first matrix.
  6008. * @param {Matrix2} right The second matrix.
  6009. * @param {Matrix2} result The object onto which to store the result.
  6010. * @returns {Matrix2} The modified result parameter.
  6011. */
  6012. Matrix2.multiply = function (left, right, result) {
  6013. //>>includeStart('debug', pragmas.debug);
  6014. Check.Check.typeOf.object("left", left);
  6015. Check.Check.typeOf.object("right", right);
  6016. Check.Check.typeOf.object("result", result);
  6017. //>>includeEnd('debug');
  6018. const column0Row0 = left[0] * right[0] + left[2] * right[1];
  6019. const column1Row0 = left[0] * right[2] + left[2] * right[3];
  6020. const column0Row1 = left[1] * right[0] + left[3] * right[1];
  6021. const column1Row1 = left[1] * right[2] + left[3] * right[3];
  6022. result[0] = column0Row0;
  6023. result[1] = column0Row1;
  6024. result[2] = column1Row0;
  6025. result[3] = column1Row1;
  6026. return result;
  6027. };
  6028. /**
  6029. * Computes the sum of two matrices.
  6030. *
  6031. * @param {Matrix2} left The first matrix.
  6032. * @param {Matrix2} right The second matrix.
  6033. * @param {Matrix2} result The object onto which to store the result.
  6034. * @returns {Matrix2} The modified result parameter.
  6035. */
  6036. Matrix2.add = function (left, right, result) {
  6037. //>>includeStart('debug', pragmas.debug);
  6038. Check.Check.typeOf.object("left", left);
  6039. Check.Check.typeOf.object("right", right);
  6040. Check.Check.typeOf.object("result", result);
  6041. //>>includeEnd('debug');
  6042. result[0] = left[0] + right[0];
  6043. result[1] = left[1] + right[1];
  6044. result[2] = left[2] + right[2];
  6045. result[3] = left[3] + right[3];
  6046. return result;
  6047. };
  6048. /**
  6049. * Computes the difference of two matrices.
  6050. *
  6051. * @param {Matrix2} left The first matrix.
  6052. * @param {Matrix2} right The second matrix.
  6053. * @param {Matrix2} result The object onto which to store the result.
  6054. * @returns {Matrix2} The modified result parameter.
  6055. */
  6056. Matrix2.subtract = function (left, right, result) {
  6057. //>>includeStart('debug', pragmas.debug);
  6058. Check.Check.typeOf.object("left", left);
  6059. Check.Check.typeOf.object("right", right);
  6060. Check.Check.typeOf.object("result", result);
  6061. //>>includeEnd('debug');
  6062. result[0] = left[0] - right[0];
  6063. result[1] = left[1] - right[1];
  6064. result[2] = left[2] - right[2];
  6065. result[3] = left[3] - right[3];
  6066. return result;
  6067. };
  6068. /**
  6069. * Computes the product of a matrix and a column vector.
  6070. *
  6071. * @param {Matrix2} matrix The matrix.
  6072. * @param {Cartesian2} cartesian The column.
  6073. * @param {Cartesian2} result The object onto which to store the result.
  6074. * @returns {Cartesian2} The modified result parameter.
  6075. */
  6076. Matrix2.multiplyByVector = function (matrix, cartesian, result) {
  6077. //>>includeStart('debug', pragmas.debug);
  6078. Check.Check.typeOf.object("matrix", matrix);
  6079. Check.Check.typeOf.object("cartesian", cartesian);
  6080. Check.Check.typeOf.object("result", result);
  6081. //>>includeEnd('debug');
  6082. const x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
  6083. const y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
  6084. result.x = x;
  6085. result.y = y;
  6086. return result;
  6087. };
  6088. /**
  6089. * Computes the product of a matrix and a scalar.
  6090. *
  6091. * @param {Matrix2} matrix The matrix.
  6092. * @param {number} scalar The number to multiply by.
  6093. * @param {Matrix2} result The object onto which to store the result.
  6094. * @returns {Matrix2} The modified result parameter.
  6095. */
  6096. Matrix2.multiplyByScalar = function (matrix, scalar, result) {
  6097. //>>includeStart('debug', pragmas.debug);
  6098. Check.Check.typeOf.object("matrix", matrix);
  6099. Check.Check.typeOf.number("scalar", scalar);
  6100. Check.Check.typeOf.object("result", result);
  6101. //>>includeEnd('debug');
  6102. result[0] = matrix[0] * scalar;
  6103. result[1] = matrix[1] * scalar;
  6104. result[2] = matrix[2] * scalar;
  6105. result[3] = matrix[3] * scalar;
  6106. return result;
  6107. };
  6108. /**
  6109. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  6110. *
  6111. * @param {Matrix2} matrix The matrix on the left-hand side.
  6112. * @param {Cartesian2} scale The non-uniform scale on the right-hand side.
  6113. * @param {Matrix2} result The object onto which to store the result.
  6114. * @returns {Matrix2} The modified result parameter.
  6115. *
  6116. *
  6117. * @example
  6118. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
  6119. * Cesium.Matrix2.multiplyByScale(m, scale, m);
  6120. *
  6121. * @see Matrix2.multiplyByUniformScale
  6122. * @see Matrix2.fromScale
  6123. * @see Matrix2.fromUniformScale
  6124. * @see Matrix2.setScale
  6125. * @see Matrix2.setUniformScale
  6126. * @see Matrix2.getScale
  6127. */
  6128. Matrix2.multiplyByScale = function (matrix, scale, result) {
  6129. //>>includeStart('debug', pragmas.debug);
  6130. Check.Check.typeOf.object("matrix", matrix);
  6131. Check.Check.typeOf.object("scale", scale);
  6132. Check.Check.typeOf.object("result", result);
  6133. //>>includeEnd('debug');
  6134. result[0] = matrix[0] * scale.x;
  6135. result[1] = matrix[1] * scale.x;
  6136. result[2] = matrix[2] * scale.y;
  6137. result[3] = matrix[3] * scale.y;
  6138. return result;
  6139. };
  6140. /**
  6141. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  6142. *
  6143. * @param {Matrix2} matrix The matrix on the left-hand side.
  6144. * @param {number} scale The uniform scale on the right-hand side.
  6145. * @param {Matrix2} result The object onto which to store the result.
  6146. * @returns {Matrix2} The modified result parameter.
  6147. *
  6148. * @example
  6149. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromUniformScale(scale), m);
  6150. * Cesium.Matrix2.multiplyByUniformScale(m, scale, m);
  6151. *
  6152. * @see Matrix2.multiplyByScale
  6153. * @see Matrix2.fromScale
  6154. * @see Matrix2.fromUniformScale
  6155. * @see Matrix2.setScale
  6156. * @see Matrix2.setUniformScale
  6157. * @see Matrix2.getScale
  6158. */
  6159. Matrix2.multiplyByUniformScale = function (matrix, scale, result) {
  6160. //>>includeStart('debug', pragmas.debug);
  6161. Check.Check.typeOf.object("matrix", matrix);
  6162. Check.Check.typeOf.number("scale", scale);
  6163. Check.Check.typeOf.object("result", result);
  6164. //>>includeEnd('debug');
  6165. result[0] = matrix[0] * scale;
  6166. result[1] = matrix[1] * scale;
  6167. result[2] = matrix[2] * scale;
  6168. result[3] = matrix[3] * scale;
  6169. return result;
  6170. };
  6171. /**
  6172. * Creates a negated copy of the provided matrix.
  6173. *
  6174. * @param {Matrix2} matrix The matrix to negate.
  6175. * @param {Matrix2} result The object onto which to store the result.
  6176. * @returns {Matrix2} The modified result parameter.
  6177. */
  6178. Matrix2.negate = function (matrix, result) {
  6179. //>>includeStart('debug', pragmas.debug);
  6180. Check.Check.typeOf.object("matrix", matrix);
  6181. Check.Check.typeOf.object("result", result);
  6182. //>>includeEnd('debug');
  6183. result[0] = -matrix[0];
  6184. result[1] = -matrix[1];
  6185. result[2] = -matrix[2];
  6186. result[3] = -matrix[3];
  6187. return result;
  6188. };
  6189. /**
  6190. * Computes the transpose of the provided matrix.
  6191. *
  6192. * @param {Matrix2} matrix The matrix to transpose.
  6193. * @param {Matrix2} result The object onto which to store the result.
  6194. * @returns {Matrix2} The modified result parameter.
  6195. */
  6196. Matrix2.transpose = function (matrix, result) {
  6197. //>>includeStart('debug', pragmas.debug);
  6198. Check.Check.typeOf.object("matrix", matrix);
  6199. Check.Check.typeOf.object("result", result);
  6200. //>>includeEnd('debug');
  6201. const column0Row0 = matrix[0];
  6202. const column0Row1 = matrix[2];
  6203. const column1Row0 = matrix[1];
  6204. const column1Row1 = matrix[3];
  6205. result[0] = column0Row0;
  6206. result[1] = column0Row1;
  6207. result[2] = column1Row0;
  6208. result[3] = column1Row1;
  6209. return result;
  6210. };
  6211. /**
  6212. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  6213. *
  6214. * @param {Matrix2} matrix The matrix with signed elements.
  6215. * @param {Matrix2} result The object onto which to store the result.
  6216. * @returns {Matrix2} The modified result parameter.
  6217. */
  6218. Matrix2.abs = function (matrix, result) {
  6219. //>>includeStart('debug', pragmas.debug);
  6220. Check.Check.typeOf.object("matrix", matrix);
  6221. Check.Check.typeOf.object("result", result);
  6222. //>>includeEnd('debug');
  6223. result[0] = Math.abs(matrix[0]);
  6224. result[1] = Math.abs(matrix[1]);
  6225. result[2] = Math.abs(matrix[2]);
  6226. result[3] = Math.abs(matrix[3]);
  6227. return result;
  6228. };
  6229. /**
  6230. * Compares the provided matrices componentwise and returns
  6231. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6232. *
  6233. * @param {Matrix2} [left] The first matrix.
  6234. * @param {Matrix2} [right] The second matrix.
  6235. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  6236. */
  6237. Matrix2.equals = function (left, right) {
  6238. return (
  6239. left === right ||
  6240. (defaultValue.defined(left) &&
  6241. defaultValue.defined(right) &&
  6242. left[0] === right[0] &&
  6243. left[1] === right[1] &&
  6244. left[2] === right[2] &&
  6245. left[3] === right[3])
  6246. );
  6247. };
  6248. /**
  6249. * @private
  6250. */
  6251. Matrix2.equalsArray = function (matrix, array, offset) {
  6252. return (
  6253. matrix[0] === array[offset] &&
  6254. matrix[1] === array[offset + 1] &&
  6255. matrix[2] === array[offset + 2] &&
  6256. matrix[3] === array[offset + 3]
  6257. );
  6258. };
  6259. /**
  6260. * Compares the provided matrices componentwise and returns
  6261. * <code>true</code> if they are within the provided epsilon,
  6262. * <code>false</code> otherwise.
  6263. *
  6264. * @param {Matrix2} [left] The first matrix.
  6265. * @param {Matrix2} [right] The second matrix.
  6266. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  6267. * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  6268. */
  6269. Matrix2.equalsEpsilon = function (left, right, epsilon) {
  6270. epsilon = defaultValue.defaultValue(epsilon, 0);
  6271. return (
  6272. left === right ||
  6273. (defaultValue.defined(left) &&
  6274. defaultValue.defined(right) &&
  6275. Math.abs(left[0] - right[0]) <= epsilon &&
  6276. Math.abs(left[1] - right[1]) <= epsilon &&
  6277. Math.abs(left[2] - right[2]) <= epsilon &&
  6278. Math.abs(left[3] - right[3]) <= epsilon)
  6279. );
  6280. };
  6281. /**
  6282. * An immutable Matrix2 instance initialized to the identity matrix.
  6283. *
  6284. * @type {Matrix2}
  6285. * @constant
  6286. */
  6287. Matrix2.IDENTITY = Object.freeze(new Matrix2(1.0, 0.0, 0.0, 1.0));
  6288. /**
  6289. * An immutable Matrix2 instance initialized to the zero matrix.
  6290. *
  6291. * @type {Matrix2}
  6292. * @constant
  6293. */
  6294. Matrix2.ZERO = Object.freeze(new Matrix2(0.0, 0.0, 0.0, 0.0));
  6295. /**
  6296. * The index into Matrix2 for column 0, row 0.
  6297. *
  6298. * @type {number}
  6299. * @constant
  6300. *
  6301. * @example
  6302. * const matrix = new Cesium.Matrix2();
  6303. * matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
  6304. */
  6305. Matrix2.COLUMN0ROW0 = 0;
  6306. /**
  6307. * The index into Matrix2 for column 0, row 1.
  6308. *
  6309. * @type {number}
  6310. * @constant
  6311. *
  6312. * @example
  6313. * const matrix = new Cesium.Matrix2();
  6314. * matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
  6315. */
  6316. Matrix2.COLUMN0ROW1 = 1;
  6317. /**
  6318. * The index into Matrix2 for column 1, row 0.
  6319. *
  6320. * @type {number}
  6321. * @constant
  6322. *
  6323. * @example
  6324. * const matrix = new Cesium.Matrix2();
  6325. * matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
  6326. */
  6327. Matrix2.COLUMN1ROW0 = 2;
  6328. /**
  6329. * The index into Matrix2 for column 1, row 1.
  6330. *
  6331. * @type {number}
  6332. * @constant
  6333. *
  6334. * @example
  6335. * const matrix = new Cesium.Matrix2();
  6336. * matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
  6337. */
  6338. Matrix2.COLUMN1ROW1 = 3;
  6339. Object.defineProperties(Matrix2.prototype, {
  6340. /**
  6341. * Gets the number of items in the collection.
  6342. * @memberof Matrix2.prototype
  6343. *
  6344. * @type {number}
  6345. */
  6346. length: {
  6347. get: function () {
  6348. return Matrix2.packedLength;
  6349. },
  6350. },
  6351. });
  6352. /**
  6353. * Duplicates the provided Matrix2 instance.
  6354. *
  6355. * @param {Matrix2} [result] The object onto which to store the result.
  6356. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  6357. */
  6358. Matrix2.prototype.clone = function (result) {
  6359. return Matrix2.clone(this, result);
  6360. };
  6361. /**
  6362. * Compares this matrix to the provided matrix componentwise and returns
  6363. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6364. *
  6365. * @param {Matrix2} [right] The right hand side matrix.
  6366. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  6367. */
  6368. Matrix2.prototype.equals = function (right) {
  6369. return Matrix2.equals(this, right);
  6370. };
  6371. /**
  6372. * Compares this matrix to the provided matrix componentwise and returns
  6373. * <code>true</code> if they are within the provided epsilon,
  6374. * <code>false</code> otherwise.
  6375. *
  6376. * @param {Matrix2} [right] The right hand side matrix.
  6377. * @param {number} [epsilon=0] The epsilon to use for equality testing.
  6378. * @returns {boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  6379. */
  6380. Matrix2.prototype.equalsEpsilon = function (right, epsilon) {
  6381. return Matrix2.equalsEpsilon(this, right, epsilon);
  6382. };
  6383. /**
  6384. * Creates a string representing this Matrix with each row being
  6385. * on a separate line and in the format '(column0, column1)'.
  6386. *
  6387. * @returns {string} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
  6388. */
  6389. Matrix2.prototype.toString = function () {
  6390. return `(${this[0]}, ${this[2]})\n` + `(${this[1]}, ${this[3]})`;
  6391. };
  6392. exports.Cartesian2 = Cartesian2;
  6393. exports.Cartesian4 = Cartesian4;
  6394. exports.Matrix2 = Matrix2;
  6395. exports.Matrix4 = Matrix4;
  6396. exports.Rectangle = Rectangle;
  6397. }));