Matrix2-9e1c22e2.js 379 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './RuntimeError-4f8ec8a2', './defaultValue-97284df2', './ComponentDatatype-4eeb6d9b'], (function (exports, RuntimeError, defaultValue, ComponentDatatype) { 'use strict';
  26. /**
  27. * A 3D Cartesian point.
  28. * @alias Cartesian3
  29. * @constructor
  30. *
  31. * @param {Number} [x=0.0] The X component.
  32. * @param {Number} [y=0.0] The Y component.
  33. * @param {Number} [z=0.0] The Z component.
  34. *
  35. * @see Cartesian2
  36. * @see Cartesian4
  37. * @see Packable
  38. */
  39. function Cartesian3(x, y, z) {
  40. /**
  41. * The X component.
  42. * @type {Number}
  43. * @default 0.0
  44. */
  45. this.x = defaultValue.defaultValue(x, 0.0);
  46. /**
  47. * The Y component.
  48. * @type {Number}
  49. * @default 0.0
  50. */
  51. this.y = defaultValue.defaultValue(y, 0.0);
  52. /**
  53. * The Z component.
  54. * @type {Number}
  55. * @default 0.0
  56. */
  57. this.z = defaultValue.defaultValue(z, 0.0);
  58. }
  59. /**
  60. * Converts the provided Spherical into Cartesian3 coordinates.
  61. *
  62. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  63. * @param {Cartesian3} [result] The object onto which to store the result.
  64. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  65. */
  66. Cartesian3.fromSpherical = function (spherical, result) {
  67. //>>includeStart('debug', pragmas.debug);
  68. RuntimeError.Check.typeOf.object("spherical", spherical);
  69. //>>includeEnd('debug');
  70. if (!defaultValue.defined(result)) {
  71. result = new Cartesian3();
  72. }
  73. const clock = spherical.clock;
  74. const cone = spherical.cone;
  75. const magnitude = defaultValue.defaultValue(spherical.magnitude, 1.0);
  76. const radial = magnitude * Math.sin(cone);
  77. result.x = radial * Math.cos(clock);
  78. result.y = radial * Math.sin(clock);
  79. result.z = magnitude * Math.cos(cone);
  80. return result;
  81. };
  82. /**
  83. * Creates a Cartesian3 instance from x, y and z coordinates.
  84. *
  85. * @param {Number} x The x coordinate.
  86. * @param {Number} y The y coordinate.
  87. * @param {Number} z The z coordinate.
  88. * @param {Cartesian3} [result] The object onto which to store the result.
  89. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  90. */
  91. Cartesian3.fromElements = function (x, y, z, result) {
  92. if (!defaultValue.defined(result)) {
  93. return new Cartesian3(x, y, z);
  94. }
  95. result.x = x;
  96. result.y = y;
  97. result.z = z;
  98. return result;
  99. };
  100. /**
  101. * Duplicates a Cartesian3 instance.
  102. *
  103. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  104. * @param {Cartesian3} [result] The object onto which to store the result.
  105. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  106. */
  107. Cartesian3.clone = function (cartesian, result) {
  108. if (!defaultValue.defined(cartesian)) {
  109. return undefined;
  110. }
  111. if (!defaultValue.defined(result)) {
  112. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  113. }
  114. result.x = cartesian.x;
  115. result.y = cartesian.y;
  116. result.z = cartesian.z;
  117. return result;
  118. };
  119. /**
  120. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  121. * x, y, and z properties of the Cartesian4 and drops w.
  122. * @function
  123. *
  124. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  125. * @param {Cartesian3} [result] The object onto which to store the result.
  126. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  127. */
  128. Cartesian3.fromCartesian4 = Cartesian3.clone;
  129. /**
  130. * The number of elements used to pack the object into an array.
  131. * @type {Number}
  132. */
  133. Cartesian3.packedLength = 3;
  134. /**
  135. * Stores the provided instance into the provided array.
  136. *
  137. * @param {Cartesian3} value The value to pack.
  138. * @param {Number[]} array The array to pack into.
  139. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  140. *
  141. * @returns {Number[]} The array that was packed into
  142. */
  143. Cartesian3.pack = function (value, array, startingIndex) {
  144. //>>includeStart('debug', pragmas.debug);
  145. RuntimeError.Check.typeOf.object("value", value);
  146. RuntimeError.Check.defined("array", array);
  147. //>>includeEnd('debug');
  148. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  149. array[startingIndex++] = value.x;
  150. array[startingIndex++] = value.y;
  151. array[startingIndex] = value.z;
  152. return array;
  153. };
  154. /**
  155. * Retrieves an instance from a packed array.
  156. *
  157. * @param {Number[]} array The packed array.
  158. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  159. * @param {Cartesian3} [result] The object into which to store the result.
  160. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  161. */
  162. Cartesian3.unpack = function (array, startingIndex, result) {
  163. //>>includeStart('debug', pragmas.debug);
  164. RuntimeError.Check.defined("array", array);
  165. //>>includeEnd('debug');
  166. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  167. if (!defaultValue.defined(result)) {
  168. result = new Cartesian3();
  169. }
  170. result.x = array[startingIndex++];
  171. result.y = array[startingIndex++];
  172. result.z = array[startingIndex];
  173. return result;
  174. };
  175. /**
  176. * Flattens an array of Cartesian3s into an array of components.
  177. *
  178. * @param {Cartesian3[]} array The array of cartesians to pack.
  179. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 3 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 3) elements.
  180. * @returns {Number[]} The packed array.
  181. */
  182. Cartesian3.packArray = function (array, result) {
  183. //>>includeStart('debug', pragmas.debug);
  184. RuntimeError.Check.defined("array", array);
  185. //>>includeEnd('debug');
  186. const length = array.length;
  187. const resultLength = length * 3;
  188. if (!defaultValue.defined(result)) {
  189. result = new Array(resultLength);
  190. } else if (!Array.isArray(result) && result.length !== resultLength) {
  191. //>>includeStart('debug', pragmas.debug);
  192. throw new RuntimeError.DeveloperError(
  193. "If result is a typed array, it must have exactly array.length * 3 elements"
  194. );
  195. //>>includeEnd('debug');
  196. } else if (result.length !== resultLength) {
  197. result.length = resultLength;
  198. }
  199. for (let i = 0; i < length; ++i) {
  200. Cartesian3.pack(array[i], result, i * 3);
  201. }
  202. return result;
  203. };
  204. /**
  205. * Unpacks an array of cartesian components into an array of Cartesian3s.
  206. *
  207. * @param {Number[]} array The array of components to unpack.
  208. * @param {Cartesian3[]} [result] The array onto which to store the result.
  209. * @returns {Cartesian3[]} The unpacked array.
  210. */
  211. Cartesian3.unpackArray = function (array, result) {
  212. //>>includeStart('debug', pragmas.debug);
  213. RuntimeError.Check.defined("array", array);
  214. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 3);
  215. if (array.length % 3 !== 0) {
  216. throw new RuntimeError.DeveloperError("array length must be a multiple of 3.");
  217. }
  218. //>>includeEnd('debug');
  219. const length = array.length;
  220. if (!defaultValue.defined(result)) {
  221. result = new Array(length / 3);
  222. } else {
  223. result.length = length / 3;
  224. }
  225. for (let i = 0; i < length; i += 3) {
  226. const index = i / 3;
  227. result[index] = Cartesian3.unpack(array, i, result[index]);
  228. }
  229. return result;
  230. };
  231. /**
  232. * Creates a Cartesian3 from three consecutive elements in an array.
  233. * @function
  234. *
  235. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  236. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  237. * @param {Cartesian3} [result] The object onto which to store the result.
  238. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  239. *
  240. * @example
  241. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  242. * const v = [1.0, 2.0, 3.0];
  243. * const p = Cesium.Cartesian3.fromArray(v);
  244. *
  245. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  246. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  247. * const p2 = Cesium.Cartesian3.fromArray(v2, 2);
  248. */
  249. Cartesian3.fromArray = Cartesian3.unpack;
  250. /**
  251. * Computes the value of the maximum component for the supplied Cartesian.
  252. *
  253. * @param {Cartesian3} cartesian The cartesian to use.
  254. * @returns {Number} The value of the maximum component.
  255. */
  256. Cartesian3.maximumComponent = function (cartesian) {
  257. //>>includeStart('debug', pragmas.debug);
  258. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  259. //>>includeEnd('debug');
  260. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  261. };
  262. /**
  263. * Computes the value of the minimum component for the supplied Cartesian.
  264. *
  265. * @param {Cartesian3} cartesian The cartesian to use.
  266. * @returns {Number} The value of the minimum component.
  267. */
  268. Cartesian3.minimumComponent = function (cartesian) {
  269. //>>includeStart('debug', pragmas.debug);
  270. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  271. //>>includeEnd('debug');
  272. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  273. };
  274. /**
  275. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  276. *
  277. * @param {Cartesian3} first A cartesian to compare.
  278. * @param {Cartesian3} second A cartesian to compare.
  279. * @param {Cartesian3} result The object into which to store the result.
  280. * @returns {Cartesian3} A cartesian with the minimum components.
  281. */
  282. Cartesian3.minimumByComponent = function (first, second, result) {
  283. //>>includeStart('debug', pragmas.debug);
  284. RuntimeError.Check.typeOf.object("first", first);
  285. RuntimeError.Check.typeOf.object("second", second);
  286. RuntimeError.Check.typeOf.object("result", result);
  287. //>>includeEnd('debug');
  288. result.x = Math.min(first.x, second.x);
  289. result.y = Math.min(first.y, second.y);
  290. result.z = Math.min(first.z, second.z);
  291. return result;
  292. };
  293. /**
  294. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  295. *
  296. * @param {Cartesian3} first A cartesian to compare.
  297. * @param {Cartesian3} second A cartesian to compare.
  298. * @param {Cartesian3} result The object into which to store the result.
  299. * @returns {Cartesian3} A cartesian with the maximum components.
  300. */
  301. Cartesian3.maximumByComponent = function (first, second, result) {
  302. //>>includeStart('debug', pragmas.debug);
  303. RuntimeError.Check.typeOf.object("first", first);
  304. RuntimeError.Check.typeOf.object("second", second);
  305. RuntimeError.Check.typeOf.object("result", result);
  306. //>>includeEnd('debug');
  307. result.x = Math.max(first.x, second.x);
  308. result.y = Math.max(first.y, second.y);
  309. result.z = Math.max(first.z, second.z);
  310. return result;
  311. };
  312. /**
  313. * Constrain a value to lie between two values.
  314. *
  315. * @param {Cartesian3} cartesian The value to clamp.
  316. * @param {Cartesian3} min The minimum bound.
  317. * @param {Cartesian3} max The maximum bound.
  318. * @param {Cartesian3} result The object into which to store the result.
  319. * @returns {Cartesian3} The clamped value such that min <= value <= max.
  320. */
  321. Cartesian3.clamp = function (value, min, max, result) {
  322. //>>includeStart('debug', pragmas.debug);
  323. RuntimeError.Check.typeOf.object("value", value);
  324. RuntimeError.Check.typeOf.object("min", min);
  325. RuntimeError.Check.typeOf.object("max", max);
  326. RuntimeError.Check.typeOf.object("result", result);
  327. //>>includeEnd('debug');
  328. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  329. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  330. const z = ComponentDatatype.CesiumMath.clamp(value.z, min.z, max.z);
  331. result.x = x;
  332. result.y = y;
  333. result.z = z;
  334. return result;
  335. };
  336. /**
  337. * Computes the provided Cartesian's squared magnitude.
  338. *
  339. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  340. * @returns {Number} The squared magnitude.
  341. */
  342. Cartesian3.magnitudeSquared = function (cartesian) {
  343. //>>includeStart('debug', pragmas.debug);
  344. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  345. //>>includeEnd('debug');
  346. return (
  347. cartesian.x * cartesian.x +
  348. cartesian.y * cartesian.y +
  349. cartesian.z * cartesian.z
  350. );
  351. };
  352. /**
  353. * Computes the Cartesian's magnitude (length).
  354. *
  355. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  356. * @returns {Number} The magnitude.
  357. */
  358. Cartesian3.magnitude = function (cartesian) {
  359. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  360. };
  361. const distanceScratch$2 = new Cartesian3();
  362. /**
  363. * Computes the distance between two points.
  364. *
  365. * @param {Cartesian3} left The first point to compute the distance from.
  366. * @param {Cartesian3} right The second point to compute the distance to.
  367. * @returns {Number} The distance between two points.
  368. *
  369. * @example
  370. * // Returns 1.0
  371. * const d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  372. */
  373. Cartesian3.distance = function (left, right) {
  374. //>>includeStart('debug', pragmas.debug);
  375. RuntimeError.Check.typeOf.object("left", left);
  376. RuntimeError.Check.typeOf.object("right", right);
  377. //>>includeEnd('debug');
  378. Cartesian3.subtract(left, right, distanceScratch$2);
  379. return Cartesian3.magnitude(distanceScratch$2);
  380. };
  381. /**
  382. * Computes the squared distance between two points. Comparing squared distances
  383. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  384. *
  385. * @param {Cartesian3} left The first point to compute the distance from.
  386. * @param {Cartesian3} right The second point to compute the distance to.
  387. * @returns {Number} The distance between two points.
  388. *
  389. * @example
  390. * // Returns 4.0, not 2.0
  391. * const d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  392. */
  393. Cartesian3.distanceSquared = function (left, right) {
  394. //>>includeStart('debug', pragmas.debug);
  395. RuntimeError.Check.typeOf.object("left", left);
  396. RuntimeError.Check.typeOf.object("right", right);
  397. //>>includeEnd('debug');
  398. Cartesian3.subtract(left, right, distanceScratch$2);
  399. return Cartesian3.magnitudeSquared(distanceScratch$2);
  400. };
  401. /**
  402. * Computes the normalized form of the supplied Cartesian.
  403. *
  404. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  405. * @param {Cartesian3} result The object onto which to store the result.
  406. * @returns {Cartesian3} The modified result parameter.
  407. */
  408. Cartesian3.normalize = function (cartesian, result) {
  409. //>>includeStart('debug', pragmas.debug);
  410. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  411. RuntimeError.Check.typeOf.object("result", result);
  412. //>>includeEnd('debug');
  413. const magnitude = Cartesian3.magnitude(cartesian);
  414. result.x = cartesian.x / magnitude;
  415. result.y = cartesian.y / magnitude;
  416. result.z = cartesian.z / magnitude;
  417. //>>includeStart('debug', pragmas.debug);
  418. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  419. throw new RuntimeError.DeveloperError("normalized result is not a number");
  420. }
  421. //>>includeEnd('debug');
  422. return result;
  423. };
  424. /**
  425. * Computes the dot (scalar) product of two Cartesians.
  426. *
  427. * @param {Cartesian3} left The first Cartesian.
  428. * @param {Cartesian3} right The second Cartesian.
  429. * @returns {Number} The dot product.
  430. */
  431. Cartesian3.dot = function (left, right) {
  432. //>>includeStart('debug', pragmas.debug);
  433. RuntimeError.Check.typeOf.object("left", left);
  434. RuntimeError.Check.typeOf.object("right", right);
  435. //>>includeEnd('debug');
  436. return left.x * right.x + left.y * right.y + left.z * right.z;
  437. };
  438. /**
  439. * Computes the componentwise product of two Cartesians.
  440. *
  441. * @param {Cartesian3} left The first Cartesian.
  442. * @param {Cartesian3} right The second Cartesian.
  443. * @param {Cartesian3} result The object onto which to store the result.
  444. * @returns {Cartesian3} The modified result parameter.
  445. */
  446. Cartesian3.multiplyComponents = function (left, right, result) {
  447. //>>includeStart('debug', pragmas.debug);
  448. RuntimeError.Check.typeOf.object("left", left);
  449. RuntimeError.Check.typeOf.object("right", right);
  450. RuntimeError.Check.typeOf.object("result", result);
  451. //>>includeEnd('debug');
  452. result.x = left.x * right.x;
  453. result.y = left.y * right.y;
  454. result.z = left.z * right.z;
  455. return result;
  456. };
  457. /**
  458. * Computes the componentwise quotient of two Cartesians.
  459. *
  460. * @param {Cartesian3} left The first Cartesian.
  461. * @param {Cartesian3} right The second Cartesian.
  462. * @param {Cartesian3} result The object onto which to store the result.
  463. * @returns {Cartesian3} The modified result parameter.
  464. */
  465. Cartesian3.divideComponents = function (left, right, result) {
  466. //>>includeStart('debug', pragmas.debug);
  467. RuntimeError.Check.typeOf.object("left", left);
  468. RuntimeError.Check.typeOf.object("right", right);
  469. RuntimeError.Check.typeOf.object("result", result);
  470. //>>includeEnd('debug');
  471. result.x = left.x / right.x;
  472. result.y = left.y / right.y;
  473. result.z = left.z / right.z;
  474. return result;
  475. };
  476. /**
  477. * Computes the componentwise sum of two Cartesians.
  478. *
  479. * @param {Cartesian3} left The first Cartesian.
  480. * @param {Cartesian3} right The second Cartesian.
  481. * @param {Cartesian3} result The object onto which to store the result.
  482. * @returns {Cartesian3} The modified result parameter.
  483. */
  484. Cartesian3.add = function (left, right, result) {
  485. //>>includeStart('debug', pragmas.debug);
  486. RuntimeError.Check.typeOf.object("left", left);
  487. RuntimeError.Check.typeOf.object("right", right);
  488. RuntimeError.Check.typeOf.object("result", result);
  489. //>>includeEnd('debug');
  490. result.x = left.x + right.x;
  491. result.y = left.y + right.y;
  492. result.z = left.z + right.z;
  493. return result;
  494. };
  495. /**
  496. * Computes the componentwise difference of two Cartesians.
  497. *
  498. * @param {Cartesian3} left The first Cartesian.
  499. * @param {Cartesian3} right The second Cartesian.
  500. * @param {Cartesian3} result The object onto which to store the result.
  501. * @returns {Cartesian3} The modified result parameter.
  502. */
  503. Cartesian3.subtract = function (left, right, result) {
  504. //>>includeStart('debug', pragmas.debug);
  505. RuntimeError.Check.typeOf.object("left", left);
  506. RuntimeError.Check.typeOf.object("right", right);
  507. RuntimeError.Check.typeOf.object("result", result);
  508. //>>includeEnd('debug');
  509. result.x = left.x - right.x;
  510. result.y = left.y - right.y;
  511. result.z = left.z - right.z;
  512. return result;
  513. };
  514. /**
  515. * Multiplies the provided Cartesian componentwise by the provided scalar.
  516. *
  517. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  518. * @param {Number} scalar The scalar to multiply with.
  519. * @param {Cartesian3} result The object onto which to store the result.
  520. * @returns {Cartesian3} The modified result parameter.
  521. */
  522. Cartesian3.multiplyByScalar = function (cartesian, scalar, result) {
  523. //>>includeStart('debug', pragmas.debug);
  524. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  525. RuntimeError.Check.typeOf.number("scalar", scalar);
  526. RuntimeError.Check.typeOf.object("result", result);
  527. //>>includeEnd('debug');
  528. result.x = cartesian.x * scalar;
  529. result.y = cartesian.y * scalar;
  530. result.z = cartesian.z * scalar;
  531. return result;
  532. };
  533. /**
  534. * Divides the provided Cartesian componentwise by the provided scalar.
  535. *
  536. * @param {Cartesian3} cartesian The Cartesian to be divided.
  537. * @param {Number} scalar The scalar to divide by.
  538. * @param {Cartesian3} result The object onto which to store the result.
  539. * @returns {Cartesian3} The modified result parameter.
  540. */
  541. Cartesian3.divideByScalar = function (cartesian, scalar, result) {
  542. //>>includeStart('debug', pragmas.debug);
  543. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  544. RuntimeError.Check.typeOf.number("scalar", scalar);
  545. RuntimeError.Check.typeOf.object("result", result);
  546. //>>includeEnd('debug');
  547. result.x = cartesian.x / scalar;
  548. result.y = cartesian.y / scalar;
  549. result.z = cartesian.z / scalar;
  550. return result;
  551. };
  552. /**
  553. * Negates the provided Cartesian.
  554. *
  555. * @param {Cartesian3} cartesian The Cartesian to be negated.
  556. * @param {Cartesian3} result The object onto which to store the result.
  557. * @returns {Cartesian3} The modified result parameter.
  558. */
  559. Cartesian3.negate = function (cartesian, result) {
  560. //>>includeStart('debug', pragmas.debug);
  561. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  562. RuntimeError.Check.typeOf.object("result", result);
  563. //>>includeEnd('debug');
  564. result.x = -cartesian.x;
  565. result.y = -cartesian.y;
  566. result.z = -cartesian.z;
  567. return result;
  568. };
  569. /**
  570. * Computes the absolute value of the provided Cartesian.
  571. *
  572. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  573. * @param {Cartesian3} result The object onto which to store the result.
  574. * @returns {Cartesian3} The modified result parameter.
  575. */
  576. Cartesian3.abs = function (cartesian, result) {
  577. //>>includeStart('debug', pragmas.debug);
  578. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  579. RuntimeError.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. return result;
  585. };
  586. const lerpScratch$2 = new Cartesian3();
  587. /**
  588. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  589. *
  590. * @param {Cartesian3} start The value corresponding to t at 0.0.
  591. * @param {Cartesian3} end The value corresponding to t at 1.0.
  592. * @param {Number} t The point along t at which to interpolate.
  593. * @param {Cartesian3} result The object onto which to store the result.
  594. * @returns {Cartesian3} The modified result parameter.
  595. */
  596. Cartesian3.lerp = function (start, end, t, result) {
  597. //>>includeStart('debug', pragmas.debug);
  598. RuntimeError.Check.typeOf.object("start", start);
  599. RuntimeError.Check.typeOf.object("end", end);
  600. RuntimeError.Check.typeOf.number("t", t);
  601. RuntimeError.Check.typeOf.object("result", result);
  602. //>>includeEnd('debug');
  603. Cartesian3.multiplyByScalar(end, t, lerpScratch$2);
  604. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  605. return Cartesian3.add(lerpScratch$2, result, result);
  606. };
  607. const angleBetweenScratch$1 = new Cartesian3();
  608. const angleBetweenScratch2$1 = new Cartesian3();
  609. /**
  610. * Returns the angle, in radians, between the provided Cartesians.
  611. *
  612. * @param {Cartesian3} left The first Cartesian.
  613. * @param {Cartesian3} right The second Cartesian.
  614. * @returns {Number} The angle between the Cartesians.
  615. */
  616. Cartesian3.angleBetween = function (left, right) {
  617. //>>includeStart('debug', pragmas.debug);
  618. RuntimeError.Check.typeOf.object("left", left);
  619. RuntimeError.Check.typeOf.object("right", right);
  620. //>>includeEnd('debug');
  621. Cartesian3.normalize(left, angleBetweenScratch$1);
  622. Cartesian3.normalize(right, angleBetweenScratch2$1);
  623. const cosine = Cartesian3.dot(angleBetweenScratch$1, angleBetweenScratch2$1);
  624. const sine = Cartesian3.magnitude(
  625. Cartesian3.cross(
  626. angleBetweenScratch$1,
  627. angleBetweenScratch2$1,
  628. angleBetweenScratch$1
  629. )
  630. );
  631. return Math.atan2(sine, cosine);
  632. };
  633. const mostOrthogonalAxisScratch$2 = new Cartesian3();
  634. /**
  635. * Returns the axis that is most orthogonal to the provided Cartesian.
  636. *
  637. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  638. * @param {Cartesian3} result The object onto which to store the result.
  639. * @returns {Cartesian3} The most orthogonal axis.
  640. */
  641. Cartesian3.mostOrthogonalAxis = function (cartesian, result) {
  642. //>>includeStart('debug', pragmas.debug);
  643. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  644. RuntimeError.Check.typeOf.object("result", result);
  645. //>>includeEnd('debug');
  646. const f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch$2);
  647. Cartesian3.abs(f, f);
  648. if (f.x <= f.y) {
  649. if (f.x <= f.z) {
  650. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  651. } else {
  652. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  653. }
  654. } else if (f.y <= f.z) {
  655. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  656. } else {
  657. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  658. }
  659. return result;
  660. };
  661. /**
  662. * Projects vector a onto vector b
  663. * @param {Cartesian3} a The vector that needs projecting
  664. * @param {Cartesian3} b The vector to project onto
  665. * @param {Cartesian3} result The result cartesian
  666. * @returns {Cartesian3} The modified result parameter
  667. */
  668. Cartesian3.projectVector = function (a, b, result) {
  669. //>>includeStart('debug', pragmas.debug);
  670. RuntimeError.Check.defined("a", a);
  671. RuntimeError.Check.defined("b", b);
  672. RuntimeError.Check.defined("result", result);
  673. //>>includeEnd('debug');
  674. const scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
  675. return Cartesian3.multiplyByScalar(b, scalar, result);
  676. };
  677. /**
  678. * Compares the provided Cartesians componentwise and returns
  679. * <code>true</code> if they are equal, <code>false</code> otherwise.
  680. *
  681. * @param {Cartesian3} [left] The first Cartesian.
  682. * @param {Cartesian3} [right] The second Cartesian.
  683. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  684. */
  685. Cartesian3.equals = function (left, right) {
  686. return (
  687. left === right ||
  688. (defaultValue.defined(left) &&
  689. defaultValue.defined(right) &&
  690. left.x === right.x &&
  691. left.y === right.y &&
  692. left.z === right.z)
  693. );
  694. };
  695. /**
  696. * @private
  697. */
  698. Cartesian3.equalsArray = function (cartesian, array, offset) {
  699. return (
  700. cartesian.x === array[offset] &&
  701. cartesian.y === array[offset + 1] &&
  702. cartesian.z === array[offset + 2]
  703. );
  704. };
  705. /**
  706. * Compares the provided Cartesians componentwise and returns
  707. * <code>true</code> if they pass an absolute or relative tolerance test,
  708. * <code>false</code> otherwise.
  709. *
  710. * @param {Cartesian3} [left] The first Cartesian.
  711. * @param {Cartesian3} [right] The second Cartesian.
  712. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  713. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  714. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  715. */
  716. Cartesian3.equalsEpsilon = function (
  717. left,
  718. right,
  719. relativeEpsilon,
  720. absoluteEpsilon
  721. ) {
  722. return (
  723. left === right ||
  724. (defaultValue.defined(left) &&
  725. defaultValue.defined(right) &&
  726. ComponentDatatype.CesiumMath.equalsEpsilon(
  727. left.x,
  728. right.x,
  729. relativeEpsilon,
  730. absoluteEpsilon
  731. ) &&
  732. ComponentDatatype.CesiumMath.equalsEpsilon(
  733. left.y,
  734. right.y,
  735. relativeEpsilon,
  736. absoluteEpsilon
  737. ) &&
  738. ComponentDatatype.CesiumMath.equalsEpsilon(
  739. left.z,
  740. right.z,
  741. relativeEpsilon,
  742. absoluteEpsilon
  743. ))
  744. );
  745. };
  746. /**
  747. * Computes the cross (outer) product of two Cartesians.
  748. *
  749. * @param {Cartesian3} left The first Cartesian.
  750. * @param {Cartesian3} right The second Cartesian.
  751. * @param {Cartesian3} result The object onto which to store the result.
  752. * @returns {Cartesian3} The cross product.
  753. */
  754. Cartesian3.cross = function (left, right, result) {
  755. //>>includeStart('debug', pragmas.debug);
  756. RuntimeError.Check.typeOf.object("left", left);
  757. RuntimeError.Check.typeOf.object("right", right);
  758. RuntimeError.Check.typeOf.object("result", result);
  759. //>>includeEnd('debug');
  760. const leftX = left.x;
  761. const leftY = left.y;
  762. const leftZ = left.z;
  763. const rightX = right.x;
  764. const rightY = right.y;
  765. const rightZ = right.z;
  766. const x = leftY * rightZ - leftZ * rightY;
  767. const y = leftZ * rightX - leftX * rightZ;
  768. const z = leftX * rightY - leftY * rightX;
  769. result.x = x;
  770. result.y = y;
  771. result.z = z;
  772. return result;
  773. };
  774. /**
  775. * Computes the midpoint between the right and left Cartesian.
  776. * @param {Cartesian3} left The first Cartesian.
  777. * @param {Cartesian3} right The second Cartesian.
  778. * @param {Cartesian3} result The object onto which to store the result.
  779. * @returns {Cartesian3} The midpoint.
  780. */
  781. Cartesian3.midpoint = function (left, right, result) {
  782. //>>includeStart('debug', pragmas.debug);
  783. RuntimeError.Check.typeOf.object("left", left);
  784. RuntimeError.Check.typeOf.object("right", right);
  785. RuntimeError.Check.typeOf.object("result", result);
  786. //>>includeEnd('debug');
  787. result.x = (left.x + right.x) * 0.5;
  788. result.y = (left.y + right.y) * 0.5;
  789. result.z = (left.z + right.z) * 0.5;
  790. return result;
  791. };
  792. /**
  793. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  794. *
  795. * @param {Number} longitude The longitude, in degrees
  796. * @param {Number} latitude The latitude, in degrees
  797. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  798. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  799. * @param {Cartesian3} [result] The object onto which to store the result.
  800. * @returns {Cartesian3} The position
  801. *
  802. * @example
  803. * const position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  804. */
  805. Cartesian3.fromDegrees = function (
  806. longitude,
  807. latitude,
  808. height,
  809. ellipsoid,
  810. result
  811. ) {
  812. //>>includeStart('debug', pragmas.debug);
  813. RuntimeError.Check.typeOf.number("longitude", longitude);
  814. RuntimeError.Check.typeOf.number("latitude", latitude);
  815. //>>includeEnd('debug');
  816. longitude = ComponentDatatype.CesiumMath.toRadians(longitude);
  817. latitude = ComponentDatatype.CesiumMath.toRadians(latitude);
  818. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  819. };
  820. let scratchN = new Cartesian3();
  821. let scratchK = new Cartesian3();
  822. const wgs84RadiiSquared = new Cartesian3(
  823. 6378137.0 * 6378137.0,
  824. 6378137.0 * 6378137.0,
  825. 6356752.3142451793 * 6356752.3142451793
  826. );
  827. /**
  828. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  829. *
  830. * @param {Number} longitude The longitude, in radians
  831. * @param {Number} latitude The latitude, in radians
  832. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  833. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  834. * @param {Cartesian3} [result] The object onto which to store the result.
  835. * @returns {Cartesian3} The position
  836. *
  837. * @example
  838. * const position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  839. */
  840. Cartesian3.fromRadians = function (
  841. longitude,
  842. latitude,
  843. height,
  844. ellipsoid,
  845. result
  846. ) {
  847. //>>includeStart('debug', pragmas.debug);
  848. RuntimeError.Check.typeOf.number("longitude", longitude);
  849. RuntimeError.Check.typeOf.number("latitude", latitude);
  850. //>>includeEnd('debug');
  851. height = defaultValue.defaultValue(height, 0.0);
  852. const radiiSquared = defaultValue.defined(ellipsoid)
  853. ? ellipsoid.radiiSquared
  854. : wgs84RadiiSquared;
  855. const cosLatitude = Math.cos(latitude);
  856. scratchN.x = cosLatitude * Math.cos(longitude);
  857. scratchN.y = cosLatitude * Math.sin(longitude);
  858. scratchN.z = Math.sin(latitude);
  859. scratchN = Cartesian3.normalize(scratchN, scratchN);
  860. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  861. const gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  862. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  863. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  864. if (!defaultValue.defined(result)) {
  865. result = new Cartesian3();
  866. }
  867. return Cartesian3.add(scratchK, scratchN, result);
  868. };
  869. /**
  870. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  871. *
  872. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  873. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  874. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  875. * @returns {Cartesian3[]} The array of positions.
  876. *
  877. * @example
  878. * const positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  879. */
  880. Cartesian3.fromDegreesArray = function (coordinates, ellipsoid, result) {
  881. //>>includeStart('debug', pragmas.debug);
  882. RuntimeError.Check.defined("coordinates", coordinates);
  883. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  884. throw new RuntimeError.DeveloperError(
  885. "the number of coordinates must be a multiple of 2 and at least 2"
  886. );
  887. }
  888. //>>includeEnd('debug');
  889. const length = coordinates.length;
  890. if (!defaultValue.defined(result)) {
  891. result = new Array(length / 2);
  892. } else {
  893. result.length = length / 2;
  894. }
  895. for (let i = 0; i < length; i += 2) {
  896. const longitude = coordinates[i];
  897. const latitude = coordinates[i + 1];
  898. const index = i / 2;
  899. result[index] = Cartesian3.fromDegrees(
  900. longitude,
  901. latitude,
  902. 0,
  903. ellipsoid,
  904. result[index]
  905. );
  906. }
  907. return result;
  908. };
  909. /**
  910. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  911. *
  912. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  913. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  914. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  915. * @returns {Cartesian3[]} The array of positions.
  916. *
  917. * @example
  918. * const positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  919. */
  920. Cartesian3.fromRadiansArray = function (coordinates, ellipsoid, result) {
  921. //>>includeStart('debug', pragmas.debug);
  922. RuntimeError.Check.defined("coordinates", coordinates);
  923. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  924. throw new RuntimeError.DeveloperError(
  925. "the number of coordinates must be a multiple of 2 and at least 2"
  926. );
  927. }
  928. //>>includeEnd('debug');
  929. const length = coordinates.length;
  930. if (!defaultValue.defined(result)) {
  931. result = new Array(length / 2);
  932. } else {
  933. result.length = length / 2;
  934. }
  935. for (let i = 0; i < length; i += 2) {
  936. const longitude = coordinates[i];
  937. const latitude = coordinates[i + 1];
  938. const index = i / 2;
  939. result[index] = Cartesian3.fromRadians(
  940. longitude,
  941. latitude,
  942. 0,
  943. ellipsoid,
  944. result[index]
  945. );
  946. }
  947. return result;
  948. };
  949. /**
  950. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  951. *
  952. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  953. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  954. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  955. * @returns {Cartesian3[]} The array of positions.
  956. *
  957. * @example
  958. * const positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  959. */
  960. Cartesian3.fromDegreesArrayHeights = function (coordinates, ellipsoid, result) {
  961. //>>includeStart('debug', pragmas.debug);
  962. RuntimeError.Check.defined("coordinates", coordinates);
  963. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  964. throw new RuntimeError.DeveloperError(
  965. "the number of coordinates must be a multiple of 3 and at least 3"
  966. );
  967. }
  968. //>>includeEnd('debug');
  969. const length = coordinates.length;
  970. if (!defaultValue.defined(result)) {
  971. result = new Array(length / 3);
  972. } else {
  973. result.length = length / 3;
  974. }
  975. for (let i = 0; i < length; i += 3) {
  976. const longitude = coordinates[i];
  977. const latitude = coordinates[i + 1];
  978. const height = coordinates[i + 2];
  979. const index = i / 3;
  980. result[index] = Cartesian3.fromDegrees(
  981. longitude,
  982. latitude,
  983. height,
  984. ellipsoid,
  985. result[index]
  986. );
  987. }
  988. return result;
  989. };
  990. /**
  991. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  992. *
  993. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  994. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  995. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  996. * @returns {Cartesian3[]} The array of positions.
  997. *
  998. * @example
  999. * const positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  1000. */
  1001. Cartesian3.fromRadiansArrayHeights = function (coordinates, ellipsoid, result) {
  1002. //>>includeStart('debug', pragmas.debug);
  1003. RuntimeError.Check.defined("coordinates", coordinates);
  1004. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  1005. throw new RuntimeError.DeveloperError(
  1006. "the number of coordinates must be a multiple of 3 and at least 3"
  1007. );
  1008. }
  1009. //>>includeEnd('debug');
  1010. const length = coordinates.length;
  1011. if (!defaultValue.defined(result)) {
  1012. result = new Array(length / 3);
  1013. } else {
  1014. result.length = length / 3;
  1015. }
  1016. for (let i = 0; i < length; i += 3) {
  1017. const longitude = coordinates[i];
  1018. const latitude = coordinates[i + 1];
  1019. const height = coordinates[i + 2];
  1020. const index = i / 3;
  1021. result[index] = Cartesian3.fromRadians(
  1022. longitude,
  1023. latitude,
  1024. height,
  1025. ellipsoid,
  1026. result[index]
  1027. );
  1028. }
  1029. return result;
  1030. };
  1031. /**
  1032. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  1033. *
  1034. * @type {Cartesian3}
  1035. * @constant
  1036. */
  1037. Cartesian3.ZERO = Object.freeze(new Cartesian3(0.0, 0.0, 0.0));
  1038. /**
  1039. * An immutable Cartesian3 instance initialized to (1.0, 1.0, 1.0).
  1040. *
  1041. * @type {Cartesian3}
  1042. * @constant
  1043. */
  1044. Cartesian3.ONE = Object.freeze(new Cartesian3(1.0, 1.0, 1.0));
  1045. /**
  1046. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  1047. *
  1048. * @type {Cartesian3}
  1049. * @constant
  1050. */
  1051. Cartesian3.UNIT_X = Object.freeze(new Cartesian3(1.0, 0.0, 0.0));
  1052. /**
  1053. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  1054. *
  1055. * @type {Cartesian3}
  1056. * @constant
  1057. */
  1058. Cartesian3.UNIT_Y = Object.freeze(new Cartesian3(0.0, 1.0, 0.0));
  1059. /**
  1060. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  1061. *
  1062. * @type {Cartesian3}
  1063. * @constant
  1064. */
  1065. Cartesian3.UNIT_Z = Object.freeze(new Cartesian3(0.0, 0.0, 1.0));
  1066. /**
  1067. * Duplicates this Cartesian3 instance.
  1068. *
  1069. * @param {Cartesian3} [result] The object onto which to store the result.
  1070. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1071. */
  1072. Cartesian3.prototype.clone = function (result) {
  1073. return Cartesian3.clone(this, result);
  1074. };
  1075. /**
  1076. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1077. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1078. *
  1079. * @param {Cartesian3} [right] The right hand side Cartesian.
  1080. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1081. */
  1082. Cartesian3.prototype.equals = function (right) {
  1083. return Cartesian3.equals(this, right);
  1084. };
  1085. /**
  1086. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1087. * <code>true</code> if they pass an absolute or relative tolerance test,
  1088. * <code>false</code> otherwise.
  1089. *
  1090. * @param {Cartesian3} [right] The right hand side Cartesian.
  1091. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  1092. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1093. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1094. */
  1095. Cartesian3.prototype.equalsEpsilon = function (
  1096. right,
  1097. relativeEpsilon,
  1098. absoluteEpsilon
  1099. ) {
  1100. return Cartesian3.equalsEpsilon(
  1101. this,
  1102. right,
  1103. relativeEpsilon,
  1104. absoluteEpsilon
  1105. );
  1106. };
  1107. /**
  1108. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  1109. *
  1110. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  1111. */
  1112. Cartesian3.prototype.toString = function () {
  1113. return `(${this.x}, ${this.y}, ${this.z})`;
  1114. };
  1115. const scaleToGeodeticSurfaceIntersection = new Cartesian3();
  1116. const scaleToGeodeticSurfaceGradient = new Cartesian3();
  1117. /**
  1118. * Scales the provided Cartesian position along the geodetic surface normal
  1119. * so that it is on the surface of this ellipsoid. If the position is
  1120. * at the center of the ellipsoid, this function returns undefined.
  1121. *
  1122. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1123. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  1124. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  1125. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  1126. * @param {Cartesian3} [result] The object onto which to store the result.
  1127. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1128. *
  1129. * @function scaleToGeodeticSurface
  1130. *
  1131. * @private
  1132. */
  1133. function scaleToGeodeticSurface(
  1134. cartesian,
  1135. oneOverRadii,
  1136. oneOverRadiiSquared,
  1137. centerToleranceSquared,
  1138. result
  1139. ) {
  1140. //>>includeStart('debug', pragmas.debug);
  1141. if (!defaultValue.defined(cartesian)) {
  1142. throw new RuntimeError.DeveloperError("cartesian is required.");
  1143. }
  1144. if (!defaultValue.defined(oneOverRadii)) {
  1145. throw new RuntimeError.DeveloperError("oneOverRadii is required.");
  1146. }
  1147. if (!defaultValue.defined(oneOverRadiiSquared)) {
  1148. throw new RuntimeError.DeveloperError("oneOverRadiiSquared is required.");
  1149. }
  1150. if (!defaultValue.defined(centerToleranceSquared)) {
  1151. throw new RuntimeError.DeveloperError("centerToleranceSquared is required.");
  1152. }
  1153. //>>includeEnd('debug');
  1154. const positionX = cartesian.x;
  1155. const positionY = cartesian.y;
  1156. const positionZ = cartesian.z;
  1157. const oneOverRadiiX = oneOverRadii.x;
  1158. const oneOverRadiiY = oneOverRadii.y;
  1159. const oneOverRadiiZ = oneOverRadii.z;
  1160. const x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  1161. const y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  1162. const z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  1163. // Compute the squared ellipsoid norm.
  1164. const squaredNorm = x2 + y2 + z2;
  1165. const ratio = Math.sqrt(1.0 / squaredNorm);
  1166. // As an initial approximation, assume that the radial intersection is the projection point.
  1167. const intersection = Cartesian3.multiplyByScalar(
  1168. cartesian,
  1169. ratio,
  1170. scaleToGeodeticSurfaceIntersection
  1171. );
  1172. // If the position is near the center, the iteration will not converge.
  1173. if (squaredNorm < centerToleranceSquared) {
  1174. return !isFinite(ratio)
  1175. ? undefined
  1176. : Cartesian3.clone(intersection, result);
  1177. }
  1178. const oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  1179. const oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  1180. const oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  1181. // Use the gradient at the intersection point in place of the true unit normal.
  1182. // The difference in magnitude will be absorbed in the multiplier.
  1183. const gradient = scaleToGeodeticSurfaceGradient;
  1184. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  1185. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  1186. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  1187. // Compute the initial guess at the normal vector multiplier, lambda.
  1188. let lambda =
  1189. ((1.0 - ratio) * Cartesian3.magnitude(cartesian)) /
  1190. (0.5 * Cartesian3.magnitude(gradient));
  1191. let correction = 0.0;
  1192. let func;
  1193. let denominator;
  1194. let xMultiplier;
  1195. let yMultiplier;
  1196. let zMultiplier;
  1197. let xMultiplier2;
  1198. let yMultiplier2;
  1199. let zMultiplier2;
  1200. let xMultiplier3;
  1201. let yMultiplier3;
  1202. let zMultiplier3;
  1203. do {
  1204. lambda -= correction;
  1205. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  1206. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  1207. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  1208. xMultiplier2 = xMultiplier * xMultiplier;
  1209. yMultiplier2 = yMultiplier * yMultiplier;
  1210. zMultiplier2 = zMultiplier * zMultiplier;
  1211. xMultiplier3 = xMultiplier2 * xMultiplier;
  1212. yMultiplier3 = yMultiplier2 * yMultiplier;
  1213. zMultiplier3 = zMultiplier2 * zMultiplier;
  1214. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  1215. // "denominator" here refers to the use of this expression in the velocity and acceleration
  1216. // computations in the sections to follow.
  1217. denominator =
  1218. x2 * xMultiplier3 * oneOverRadiiSquaredX +
  1219. y2 * yMultiplier3 * oneOverRadiiSquaredY +
  1220. z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  1221. const derivative = -2.0 * denominator;
  1222. correction = func / derivative;
  1223. } while (Math.abs(func) > ComponentDatatype.CesiumMath.EPSILON12);
  1224. if (!defaultValue.defined(result)) {
  1225. return new Cartesian3(
  1226. positionX * xMultiplier,
  1227. positionY * yMultiplier,
  1228. positionZ * zMultiplier
  1229. );
  1230. }
  1231. result.x = positionX * xMultiplier;
  1232. result.y = positionY * yMultiplier;
  1233. result.z = positionZ * zMultiplier;
  1234. return result;
  1235. }
  1236. /**
  1237. * A position defined by longitude, latitude, and height.
  1238. * @alias Cartographic
  1239. * @constructor
  1240. *
  1241. * @param {Number} [longitude=0.0] The longitude, in radians.
  1242. * @param {Number} [latitude=0.0] The latitude, in radians.
  1243. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1244. *
  1245. * @see Ellipsoid
  1246. */
  1247. function Cartographic(longitude, latitude, height) {
  1248. /**
  1249. * The longitude, in radians.
  1250. * @type {Number}
  1251. * @default 0.0
  1252. */
  1253. this.longitude = defaultValue.defaultValue(longitude, 0.0);
  1254. /**
  1255. * The latitude, in radians.
  1256. * @type {Number}
  1257. * @default 0.0
  1258. */
  1259. this.latitude = defaultValue.defaultValue(latitude, 0.0);
  1260. /**
  1261. * The height, in meters, above the ellipsoid.
  1262. * @type {Number}
  1263. * @default 0.0
  1264. */
  1265. this.height = defaultValue.defaultValue(height, 0.0);
  1266. }
  1267. /**
  1268. * Creates a new Cartographic instance from longitude and latitude
  1269. * specified in radians.
  1270. *
  1271. * @param {Number} longitude The longitude, in radians.
  1272. * @param {Number} latitude The latitude, in radians.
  1273. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1274. * @param {Cartographic} [result] The object onto which to store the result.
  1275. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1276. */
  1277. Cartographic.fromRadians = function (longitude, latitude, height, result) {
  1278. //>>includeStart('debug', pragmas.debug);
  1279. RuntimeError.Check.typeOf.number("longitude", longitude);
  1280. RuntimeError.Check.typeOf.number("latitude", latitude);
  1281. //>>includeEnd('debug');
  1282. height = defaultValue.defaultValue(height, 0.0);
  1283. if (!defaultValue.defined(result)) {
  1284. return new Cartographic(longitude, latitude, height);
  1285. }
  1286. result.longitude = longitude;
  1287. result.latitude = latitude;
  1288. result.height = height;
  1289. return result;
  1290. };
  1291. /**
  1292. * Creates a new Cartographic instance from longitude and latitude
  1293. * specified in degrees. The values in the resulting object will
  1294. * be in radians.
  1295. *
  1296. * @param {Number} longitude The longitude, in degrees.
  1297. * @param {Number} latitude The latitude, in degrees.
  1298. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1299. * @param {Cartographic} [result] The object onto which to store the result.
  1300. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1301. */
  1302. Cartographic.fromDegrees = function (longitude, latitude, height, result) {
  1303. //>>includeStart('debug', pragmas.debug);
  1304. RuntimeError.Check.typeOf.number("longitude", longitude);
  1305. RuntimeError.Check.typeOf.number("latitude", latitude);
  1306. //>>includeEnd('debug');
  1307. longitude = ComponentDatatype.CesiumMath.toRadians(longitude);
  1308. latitude = ComponentDatatype.CesiumMath.toRadians(latitude);
  1309. return Cartographic.fromRadians(longitude, latitude, height, result);
  1310. };
  1311. const cartesianToCartographicN$1 = new Cartesian3();
  1312. const cartesianToCartographicP$1 = new Cartesian3();
  1313. const cartesianToCartographicH$1 = new Cartesian3();
  1314. const wgs84OneOverRadii = new Cartesian3(
  1315. 1.0 / 6378137.0,
  1316. 1.0 / 6378137.0,
  1317. 1.0 / 6356752.3142451793
  1318. );
  1319. const wgs84OneOverRadiiSquared = new Cartesian3(
  1320. 1.0 / (6378137.0 * 6378137.0),
  1321. 1.0 / (6378137.0 * 6378137.0),
  1322. 1.0 / (6356752.3142451793 * 6356752.3142451793)
  1323. );
  1324. const wgs84CenterToleranceSquared = ComponentDatatype.CesiumMath.EPSILON1;
  1325. /**
  1326. * Creates a new Cartographic instance from a Cartesian position. The values in the
  1327. * resulting object will be in radians.
  1328. *
  1329. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1330. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1331. * @param {Cartographic} [result] The object onto which to store the result.
  1332. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  1333. */
  1334. Cartographic.fromCartesian = function (cartesian, ellipsoid, result) {
  1335. const oneOverRadii = defaultValue.defined(ellipsoid)
  1336. ? ellipsoid.oneOverRadii
  1337. : wgs84OneOverRadii;
  1338. const oneOverRadiiSquared = defaultValue.defined(ellipsoid)
  1339. ? ellipsoid.oneOverRadiiSquared
  1340. : wgs84OneOverRadiiSquared;
  1341. const centerToleranceSquared = defaultValue.defined(ellipsoid)
  1342. ? ellipsoid._centerToleranceSquared
  1343. : wgs84CenterToleranceSquared;
  1344. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1345. const p = scaleToGeodeticSurface(
  1346. cartesian,
  1347. oneOverRadii,
  1348. oneOverRadiiSquared,
  1349. centerToleranceSquared,
  1350. cartesianToCartographicP$1
  1351. );
  1352. if (!defaultValue.defined(p)) {
  1353. return undefined;
  1354. }
  1355. let n = Cartesian3.multiplyComponents(
  1356. p,
  1357. oneOverRadiiSquared,
  1358. cartesianToCartographicN$1
  1359. );
  1360. n = Cartesian3.normalize(n, n);
  1361. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH$1);
  1362. const longitude = Math.atan2(n.y, n.x);
  1363. const latitude = Math.asin(n.z);
  1364. const height =
  1365. ComponentDatatype.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1366. if (!defaultValue.defined(result)) {
  1367. return new Cartographic(longitude, latitude, height);
  1368. }
  1369. result.longitude = longitude;
  1370. result.latitude = latitude;
  1371. result.height = height;
  1372. return result;
  1373. };
  1374. /**
  1375. * Creates a new Cartesian3 instance from a Cartographic input. The values in the inputted
  1376. * object should be in radians.
  1377. *
  1378. * @param {Cartographic} cartographic Input to be converted into a Cartesian3 output.
  1379. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1380. * @param {Cartesian3} [result] The object onto which to store the result.
  1381. * @returns {Cartesian3} The position
  1382. */
  1383. Cartographic.toCartesian = function (cartographic, ellipsoid, result) {
  1384. //>>includeStart('debug', pragmas.debug);
  1385. RuntimeError.Check.defined("cartographic", cartographic);
  1386. //>>includeEnd('debug');
  1387. return Cartesian3.fromRadians(
  1388. cartographic.longitude,
  1389. cartographic.latitude,
  1390. cartographic.height,
  1391. ellipsoid,
  1392. result
  1393. );
  1394. };
  1395. /**
  1396. * Duplicates a Cartographic instance.
  1397. *
  1398. * @param {Cartographic} cartographic The cartographic to duplicate.
  1399. * @param {Cartographic} [result] The object onto which to store the result.
  1400. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  1401. */
  1402. Cartographic.clone = function (cartographic, result) {
  1403. if (!defaultValue.defined(cartographic)) {
  1404. return undefined;
  1405. }
  1406. if (!defaultValue.defined(result)) {
  1407. return new Cartographic(
  1408. cartographic.longitude,
  1409. cartographic.latitude,
  1410. cartographic.height
  1411. );
  1412. }
  1413. result.longitude = cartographic.longitude;
  1414. result.latitude = cartographic.latitude;
  1415. result.height = cartographic.height;
  1416. return result;
  1417. };
  1418. /**
  1419. * Compares the provided cartographics componentwise and returns
  1420. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1421. *
  1422. * @param {Cartographic} [left] The first cartographic.
  1423. * @param {Cartographic} [right] The second cartographic.
  1424. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1425. */
  1426. Cartographic.equals = function (left, right) {
  1427. return (
  1428. left === right ||
  1429. (defaultValue.defined(left) &&
  1430. defaultValue.defined(right) &&
  1431. left.longitude === right.longitude &&
  1432. left.latitude === right.latitude &&
  1433. left.height === right.height)
  1434. );
  1435. };
  1436. /**
  1437. * Compares the provided cartographics componentwise and returns
  1438. * <code>true</code> if they are within the provided epsilon,
  1439. * <code>false</code> otherwise.
  1440. *
  1441. * @param {Cartographic} [left] The first cartographic.
  1442. * @param {Cartographic} [right] The second cartographic.
  1443. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1444. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1445. */
  1446. Cartographic.equalsEpsilon = function (left, right, epsilon) {
  1447. epsilon = defaultValue.defaultValue(epsilon, 0);
  1448. return (
  1449. left === right ||
  1450. (defaultValue.defined(left) &&
  1451. defaultValue.defined(right) &&
  1452. Math.abs(left.longitude - right.longitude) <= epsilon &&
  1453. Math.abs(left.latitude - right.latitude) <= epsilon &&
  1454. Math.abs(left.height - right.height) <= epsilon)
  1455. );
  1456. };
  1457. /**
  1458. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  1459. *
  1460. * @type {Cartographic}
  1461. * @constant
  1462. */
  1463. Cartographic.ZERO = Object.freeze(new Cartographic(0.0, 0.0, 0.0));
  1464. /**
  1465. * Duplicates this instance.
  1466. *
  1467. * @param {Cartographic} [result] The object onto which to store the result.
  1468. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1469. */
  1470. Cartographic.prototype.clone = function (result) {
  1471. return Cartographic.clone(this, result);
  1472. };
  1473. /**
  1474. * Compares the provided against this cartographic componentwise and returns
  1475. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1476. *
  1477. * @param {Cartographic} [right] The second cartographic.
  1478. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1479. */
  1480. Cartographic.prototype.equals = function (right) {
  1481. return Cartographic.equals(this, right);
  1482. };
  1483. /**
  1484. * Compares the provided against this cartographic componentwise and returns
  1485. * <code>true</code> if they are within the provided epsilon,
  1486. * <code>false</code> otherwise.
  1487. *
  1488. * @param {Cartographic} [right] The second cartographic.
  1489. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1490. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1491. */
  1492. Cartographic.prototype.equalsEpsilon = function (right, epsilon) {
  1493. return Cartographic.equalsEpsilon(this, right, epsilon);
  1494. };
  1495. /**
  1496. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  1497. *
  1498. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  1499. */
  1500. Cartographic.prototype.toString = function () {
  1501. return `(${this.longitude}, ${this.latitude}, ${this.height})`;
  1502. };
  1503. function initialize(ellipsoid, x, y, z) {
  1504. x = defaultValue.defaultValue(x, 0.0);
  1505. y = defaultValue.defaultValue(y, 0.0);
  1506. z = defaultValue.defaultValue(z, 0.0);
  1507. //>>includeStart('debug', pragmas.debug);
  1508. RuntimeError.Check.typeOf.number.greaterThanOrEquals("x", x, 0.0);
  1509. RuntimeError.Check.typeOf.number.greaterThanOrEquals("y", y, 0.0);
  1510. RuntimeError.Check.typeOf.number.greaterThanOrEquals("z", z, 0.0);
  1511. //>>includeEnd('debug');
  1512. ellipsoid._radii = new Cartesian3(x, y, z);
  1513. ellipsoid._radiiSquared = new Cartesian3(x * x, y * y, z * z);
  1514. ellipsoid._radiiToTheFourth = new Cartesian3(
  1515. x * x * x * x,
  1516. y * y * y * y,
  1517. z * z * z * z
  1518. );
  1519. ellipsoid._oneOverRadii = new Cartesian3(
  1520. x === 0.0 ? 0.0 : 1.0 / x,
  1521. y === 0.0 ? 0.0 : 1.0 / y,
  1522. z === 0.0 ? 0.0 : 1.0 / z
  1523. );
  1524. ellipsoid._oneOverRadiiSquared = new Cartesian3(
  1525. x === 0.0 ? 0.0 : 1.0 / (x * x),
  1526. y === 0.0 ? 0.0 : 1.0 / (y * y),
  1527. z === 0.0 ? 0.0 : 1.0 / (z * z)
  1528. );
  1529. ellipsoid._minimumRadius = Math.min(x, y, z);
  1530. ellipsoid._maximumRadius = Math.max(x, y, z);
  1531. ellipsoid._centerToleranceSquared = ComponentDatatype.CesiumMath.EPSILON1;
  1532. if (ellipsoid._radiiSquared.z !== 0) {
  1533. ellipsoid._squaredXOverSquaredZ =
  1534. ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  1535. }
  1536. }
  1537. /**
  1538. * A quadratic surface defined in Cartesian coordinates by the equation
  1539. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  1540. * by Cesium to represent the shape of planetary bodies.
  1541. *
  1542. * Rather than constructing this object directly, one of the provided
  1543. * constants is normally used.
  1544. * @alias Ellipsoid
  1545. * @constructor
  1546. *
  1547. * @param {Number} [x=0] The radius in the x direction.
  1548. * @param {Number} [y=0] The radius in the y direction.
  1549. * @param {Number} [z=0] The radius in the z direction.
  1550. *
  1551. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1552. *
  1553. * @see Ellipsoid.fromCartesian3
  1554. * @see Ellipsoid.WGS84
  1555. * @see Ellipsoid.UNIT_SPHERE
  1556. */
  1557. function Ellipsoid(x, y, z) {
  1558. this._radii = undefined;
  1559. this._radiiSquared = undefined;
  1560. this._radiiToTheFourth = undefined;
  1561. this._oneOverRadii = undefined;
  1562. this._oneOverRadiiSquared = undefined;
  1563. this._minimumRadius = undefined;
  1564. this._maximumRadius = undefined;
  1565. this._centerToleranceSquared = undefined;
  1566. this._squaredXOverSquaredZ = undefined;
  1567. initialize(this, x, y, z);
  1568. }
  1569. Object.defineProperties(Ellipsoid.prototype, {
  1570. /**
  1571. * Gets the radii of the ellipsoid.
  1572. * @memberof Ellipsoid.prototype
  1573. * @type {Cartesian3}
  1574. * @readonly
  1575. */
  1576. radii: {
  1577. get: function () {
  1578. return this._radii;
  1579. },
  1580. },
  1581. /**
  1582. * Gets the squared radii of the ellipsoid.
  1583. * @memberof Ellipsoid.prototype
  1584. * @type {Cartesian3}
  1585. * @readonly
  1586. */
  1587. radiiSquared: {
  1588. get: function () {
  1589. return this._radiiSquared;
  1590. },
  1591. },
  1592. /**
  1593. * Gets the radii of the ellipsoid raise to the fourth power.
  1594. * @memberof Ellipsoid.prototype
  1595. * @type {Cartesian3}
  1596. * @readonly
  1597. */
  1598. radiiToTheFourth: {
  1599. get: function () {
  1600. return this._radiiToTheFourth;
  1601. },
  1602. },
  1603. /**
  1604. * Gets one over the radii of the ellipsoid.
  1605. * @memberof Ellipsoid.prototype
  1606. * @type {Cartesian3}
  1607. * @readonly
  1608. */
  1609. oneOverRadii: {
  1610. get: function () {
  1611. return this._oneOverRadii;
  1612. },
  1613. },
  1614. /**
  1615. * Gets one over the squared radii of the ellipsoid.
  1616. * @memberof Ellipsoid.prototype
  1617. * @type {Cartesian3}
  1618. * @readonly
  1619. */
  1620. oneOverRadiiSquared: {
  1621. get: function () {
  1622. return this._oneOverRadiiSquared;
  1623. },
  1624. },
  1625. /**
  1626. * Gets the minimum radius of the ellipsoid.
  1627. * @memberof Ellipsoid.prototype
  1628. * @type {Number}
  1629. * @readonly
  1630. */
  1631. minimumRadius: {
  1632. get: function () {
  1633. return this._minimumRadius;
  1634. },
  1635. },
  1636. /**
  1637. * Gets the maximum radius of the ellipsoid.
  1638. * @memberof Ellipsoid.prototype
  1639. * @type {Number}
  1640. * @readonly
  1641. */
  1642. maximumRadius: {
  1643. get: function () {
  1644. return this._maximumRadius;
  1645. },
  1646. },
  1647. });
  1648. /**
  1649. * Duplicates an Ellipsoid instance.
  1650. *
  1651. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  1652. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1653. * instance should be created.
  1654. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  1655. */
  1656. Ellipsoid.clone = function (ellipsoid, result) {
  1657. if (!defaultValue.defined(ellipsoid)) {
  1658. return undefined;
  1659. }
  1660. const radii = ellipsoid._radii;
  1661. if (!defaultValue.defined(result)) {
  1662. return new Ellipsoid(radii.x, radii.y, radii.z);
  1663. }
  1664. Cartesian3.clone(radii, result._radii);
  1665. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  1666. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  1667. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  1668. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  1669. result._minimumRadius = ellipsoid._minimumRadius;
  1670. result._maximumRadius = ellipsoid._maximumRadius;
  1671. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  1672. return result;
  1673. };
  1674. /**
  1675. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  1676. *
  1677. * @param {Cartesian3} [cartesian=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  1678. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1679. * instance should be created.
  1680. * @returns {Ellipsoid} A new Ellipsoid instance.
  1681. *
  1682. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1683. *
  1684. * @see Ellipsoid.WGS84
  1685. * @see Ellipsoid.UNIT_SPHERE
  1686. */
  1687. Ellipsoid.fromCartesian3 = function (cartesian, result) {
  1688. if (!defaultValue.defined(result)) {
  1689. result = new Ellipsoid();
  1690. }
  1691. if (!defaultValue.defined(cartesian)) {
  1692. return result;
  1693. }
  1694. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  1695. return result;
  1696. };
  1697. /**
  1698. * An Ellipsoid instance initialized to the WGS84 standard.
  1699. *
  1700. * @type {Ellipsoid}
  1701. * @constant
  1702. */
  1703. Ellipsoid.WGS84 = Object.freeze(
  1704. new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793)
  1705. );
  1706. /**
  1707. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  1708. *
  1709. * @type {Ellipsoid}
  1710. * @constant
  1711. */
  1712. Ellipsoid.UNIT_SPHERE = Object.freeze(new Ellipsoid(1.0, 1.0, 1.0));
  1713. /**
  1714. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  1715. *
  1716. * @type {Ellipsoid}
  1717. * @constant
  1718. */
  1719. Ellipsoid.MOON = Object.freeze(
  1720. new Ellipsoid(
  1721. ComponentDatatype.CesiumMath.LUNAR_RADIUS,
  1722. ComponentDatatype.CesiumMath.LUNAR_RADIUS,
  1723. ComponentDatatype.CesiumMath.LUNAR_RADIUS
  1724. )
  1725. );
  1726. /**
  1727. * Duplicates an Ellipsoid instance.
  1728. *
  1729. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1730. * instance should be created.
  1731. * @returns {Ellipsoid} The cloned Ellipsoid.
  1732. */
  1733. Ellipsoid.prototype.clone = function (result) {
  1734. return Ellipsoid.clone(this, result);
  1735. };
  1736. /**
  1737. * The number of elements used to pack the object into an array.
  1738. * @type {Number}
  1739. */
  1740. Ellipsoid.packedLength = Cartesian3.packedLength;
  1741. /**
  1742. * Stores the provided instance into the provided array.
  1743. *
  1744. * @param {Ellipsoid} value The value to pack.
  1745. * @param {Number[]} array The array to pack into.
  1746. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1747. *
  1748. * @returns {Number[]} The array that was packed into
  1749. */
  1750. Ellipsoid.pack = function (value, array, startingIndex) {
  1751. //>>includeStart('debug', pragmas.debug);
  1752. RuntimeError.Check.typeOf.object("value", value);
  1753. RuntimeError.Check.defined("array", array);
  1754. //>>includeEnd('debug');
  1755. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1756. Cartesian3.pack(value._radii, array, startingIndex);
  1757. return array;
  1758. };
  1759. /**
  1760. * Retrieves an instance from a packed array.
  1761. *
  1762. * @param {Number[]} array The packed array.
  1763. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1764. * @param {Ellipsoid} [result] The object into which to store the result.
  1765. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  1766. */
  1767. Ellipsoid.unpack = function (array, startingIndex, result) {
  1768. //>>includeStart('debug', pragmas.debug);
  1769. RuntimeError.Check.defined("array", array);
  1770. //>>includeEnd('debug');
  1771. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1772. const radii = Cartesian3.unpack(array, startingIndex);
  1773. return Ellipsoid.fromCartesian3(radii, result);
  1774. };
  1775. /**
  1776. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  1777. * @function
  1778. *
  1779. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  1780. * @param {Cartesian3} [result] The object onto which to store the result.
  1781. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1782. */
  1783. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  1784. /**
  1785. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1786. *
  1787. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  1788. * @param {Cartesian3} [result] The object onto which to store the result.
  1789. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1790. */
  1791. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function (
  1792. cartographic,
  1793. result
  1794. ) {
  1795. //>>includeStart('debug', pragmas.debug);
  1796. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  1797. //>>includeEnd('debug');
  1798. const longitude = cartographic.longitude;
  1799. const latitude = cartographic.latitude;
  1800. const cosLatitude = Math.cos(latitude);
  1801. const x = cosLatitude * Math.cos(longitude);
  1802. const y = cosLatitude * Math.sin(longitude);
  1803. const z = Math.sin(latitude);
  1804. if (!defaultValue.defined(result)) {
  1805. result = new Cartesian3();
  1806. }
  1807. result.x = x;
  1808. result.y = y;
  1809. result.z = z;
  1810. return Cartesian3.normalize(result, result);
  1811. };
  1812. /**
  1813. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1814. *
  1815. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  1816. * @param {Cartesian3} [result] The object onto which to store the result.
  1817. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided, or undefined if a normal cannot be found.
  1818. */
  1819. Ellipsoid.prototype.geodeticSurfaceNormal = function (cartesian, result) {
  1820. if (
  1821. Cartesian3.equalsEpsilon(cartesian, Cartesian3.ZERO, ComponentDatatype.CesiumMath.EPSILON14)
  1822. ) {
  1823. return undefined;
  1824. }
  1825. if (!defaultValue.defined(result)) {
  1826. result = new Cartesian3();
  1827. }
  1828. result = Cartesian3.multiplyComponents(
  1829. cartesian,
  1830. this._oneOverRadiiSquared,
  1831. result
  1832. );
  1833. return Cartesian3.normalize(result, result);
  1834. };
  1835. const cartographicToCartesianNormal = new Cartesian3();
  1836. const cartographicToCartesianK = new Cartesian3();
  1837. /**
  1838. * Converts the provided cartographic to Cartesian representation.
  1839. *
  1840. * @param {Cartographic} cartographic The cartographic position.
  1841. * @param {Cartesian3} [result] The object onto which to store the result.
  1842. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1843. *
  1844. * @example
  1845. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  1846. * const position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  1847. * const cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  1848. */
  1849. Ellipsoid.prototype.cartographicToCartesian = function (cartographic, result) {
  1850. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  1851. const n = cartographicToCartesianNormal;
  1852. const k = cartographicToCartesianK;
  1853. this.geodeticSurfaceNormalCartographic(cartographic, n);
  1854. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  1855. const gamma = Math.sqrt(Cartesian3.dot(n, k));
  1856. Cartesian3.divideByScalar(k, gamma, k);
  1857. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  1858. if (!defaultValue.defined(result)) {
  1859. result = new Cartesian3();
  1860. }
  1861. return Cartesian3.add(k, n, result);
  1862. };
  1863. /**
  1864. * Converts the provided array of cartographics to an array of Cartesians.
  1865. *
  1866. * @param {Cartographic[]} cartographics An array of cartographic positions.
  1867. * @param {Cartesian3[]} [result] The object onto which to store the result.
  1868. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  1869. *
  1870. * @example
  1871. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  1872. * const positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  1873. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  1874. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  1875. * const cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  1876. */
  1877. Ellipsoid.prototype.cartographicArrayToCartesianArray = function (
  1878. cartographics,
  1879. result
  1880. ) {
  1881. //>>includeStart('debug', pragmas.debug);
  1882. RuntimeError.Check.defined("cartographics", cartographics);
  1883. //>>includeEnd('debug')
  1884. const length = cartographics.length;
  1885. if (!defaultValue.defined(result)) {
  1886. result = new Array(length);
  1887. } else {
  1888. result.length = length;
  1889. }
  1890. for (let i = 0; i < length; i++) {
  1891. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  1892. }
  1893. return result;
  1894. };
  1895. const cartesianToCartographicN = new Cartesian3();
  1896. const cartesianToCartographicP = new Cartesian3();
  1897. const cartesianToCartographicH = new Cartesian3();
  1898. /**
  1899. * Converts the provided cartesian to cartographic representation.
  1900. * The cartesian is undefined at the center of the ellipsoid.
  1901. *
  1902. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1903. * @param {Cartographic} [result] The object onto which to store the result.
  1904. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  1905. *
  1906. * @example
  1907. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  1908. * const position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  1909. * const cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  1910. */
  1911. Ellipsoid.prototype.cartesianToCartographic = function (cartesian, result) {
  1912. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1913. const p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  1914. if (!defaultValue.defined(p)) {
  1915. return undefined;
  1916. }
  1917. const n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  1918. const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  1919. const longitude = Math.atan2(n.y, n.x);
  1920. const latitude = Math.asin(n.z);
  1921. const height =
  1922. ComponentDatatype.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1923. if (!defaultValue.defined(result)) {
  1924. return new Cartographic(longitude, latitude, height);
  1925. }
  1926. result.longitude = longitude;
  1927. result.latitude = latitude;
  1928. result.height = height;
  1929. return result;
  1930. };
  1931. /**
  1932. * Converts the provided array of cartesians to an array of cartographics.
  1933. *
  1934. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  1935. * @param {Cartographic[]} [result] The object onto which to store the result.
  1936. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  1937. *
  1938. * @example
  1939. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  1940. * const positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  1941. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  1942. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  1943. * const cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  1944. */
  1945. Ellipsoid.prototype.cartesianArrayToCartographicArray = function (
  1946. cartesians,
  1947. result
  1948. ) {
  1949. //>>includeStart('debug', pragmas.debug);
  1950. RuntimeError.Check.defined("cartesians", cartesians);
  1951. //>>includeEnd('debug');
  1952. const length = cartesians.length;
  1953. if (!defaultValue.defined(result)) {
  1954. result = new Array(length);
  1955. } else {
  1956. result.length = length;
  1957. }
  1958. for (let i = 0; i < length; ++i) {
  1959. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  1960. }
  1961. return result;
  1962. };
  1963. /**
  1964. * Scales the provided Cartesian position along the geodetic surface normal
  1965. * so that it is on the surface of this ellipsoid. If the position is
  1966. * at the center of the ellipsoid, this function returns undefined.
  1967. *
  1968. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1969. * @param {Cartesian3} [result] The object onto which to store the result.
  1970. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1971. */
  1972. Ellipsoid.prototype.scaleToGeodeticSurface = function (cartesian, result) {
  1973. return scaleToGeodeticSurface(
  1974. cartesian,
  1975. this._oneOverRadii,
  1976. this._oneOverRadiiSquared,
  1977. this._centerToleranceSquared,
  1978. result
  1979. );
  1980. };
  1981. /**
  1982. * Scales the provided Cartesian position along the geocentric surface normal
  1983. * so that it is on the surface of this ellipsoid.
  1984. *
  1985. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1986. * @param {Cartesian3} [result] The object onto which to store the result.
  1987. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1988. */
  1989. Ellipsoid.prototype.scaleToGeocentricSurface = function (cartesian, result) {
  1990. //>>includeStart('debug', pragmas.debug);
  1991. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  1992. //>>includeEnd('debug');
  1993. if (!defaultValue.defined(result)) {
  1994. result = new Cartesian3();
  1995. }
  1996. const positionX = cartesian.x;
  1997. const positionY = cartesian.y;
  1998. const positionZ = cartesian.z;
  1999. const oneOverRadiiSquared = this._oneOverRadiiSquared;
  2000. const beta =
  2001. 1.0 /
  2002. Math.sqrt(
  2003. positionX * positionX * oneOverRadiiSquared.x +
  2004. positionY * positionY * oneOverRadiiSquared.y +
  2005. positionZ * positionZ * oneOverRadiiSquared.z
  2006. );
  2007. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  2008. };
  2009. /**
  2010. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  2011. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  2012. *
  2013. * @param {Cartesian3} position The position to transform.
  2014. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2015. * return a new instance.
  2016. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  2017. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2018. */
  2019. Ellipsoid.prototype.transformPositionToScaledSpace = function (
  2020. position,
  2021. result
  2022. ) {
  2023. if (!defaultValue.defined(result)) {
  2024. result = new Cartesian3();
  2025. }
  2026. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  2027. };
  2028. /**
  2029. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  2030. * its components by the result of {@link Ellipsoid#radii}.
  2031. *
  2032. * @param {Cartesian3} position The position to transform.
  2033. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2034. * return a new instance.
  2035. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2036. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2037. */
  2038. Ellipsoid.prototype.transformPositionFromScaledSpace = function (
  2039. position,
  2040. result
  2041. ) {
  2042. if (!defaultValue.defined(result)) {
  2043. result = new Cartesian3();
  2044. }
  2045. return Cartesian3.multiplyComponents(position, this._radii, result);
  2046. };
  2047. /**
  2048. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2049. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2050. *
  2051. * @param {Ellipsoid} [right] The other Ellipsoid.
  2052. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2053. */
  2054. Ellipsoid.prototype.equals = function (right) {
  2055. return (
  2056. this === right ||
  2057. (defaultValue.defined(right) && Cartesian3.equals(this._radii, right._radii))
  2058. );
  2059. };
  2060. /**
  2061. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2062. *
  2063. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2064. */
  2065. Ellipsoid.prototype.toString = function () {
  2066. return this._radii.toString();
  2067. };
  2068. /**
  2069. * Computes a point which is the intersection of the surface normal with the z-axis.
  2070. *
  2071. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  2072. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  2073. * In earth case, with common earth datums, there is no need for this buffer since the intersection point is always (relatively) very close to the center.
  2074. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  2075. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  2076. * @param {Cartesian3} [result] The cartesian to which to copy the result, or undefined to create and
  2077. * return a new instance.
  2078. * @returns {Cartesian3 | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  2079. *
  2080. * @exception {DeveloperError} position is required.
  2081. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  2082. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  2083. */
  2084. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function (
  2085. position,
  2086. buffer,
  2087. result
  2088. ) {
  2089. //>>includeStart('debug', pragmas.debug);
  2090. RuntimeError.Check.typeOf.object("position", position);
  2091. if (
  2092. !ComponentDatatype.CesiumMath.equalsEpsilon(
  2093. this._radii.x,
  2094. this._radii.y,
  2095. ComponentDatatype.CesiumMath.EPSILON15
  2096. )
  2097. ) {
  2098. throw new RuntimeError.DeveloperError(
  2099. "Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)"
  2100. );
  2101. }
  2102. RuntimeError.Check.typeOf.number.greaterThan("Ellipsoid.radii.z", this._radii.z, 0);
  2103. //>>includeEnd('debug');
  2104. buffer = defaultValue.defaultValue(buffer, 0.0);
  2105. const squaredXOverSquaredZ = this._squaredXOverSquaredZ;
  2106. if (!defaultValue.defined(result)) {
  2107. result = new Cartesian3();
  2108. }
  2109. result.x = 0.0;
  2110. result.y = 0.0;
  2111. result.z = position.z * (1 - squaredXOverSquaredZ);
  2112. if (Math.abs(result.z) >= this._radii.z - buffer) {
  2113. return undefined;
  2114. }
  2115. return result;
  2116. };
  2117. const abscissas = [
  2118. 0.14887433898163,
  2119. 0.43339539412925,
  2120. 0.67940956829902,
  2121. 0.86506336668898,
  2122. 0.97390652851717,
  2123. 0.0,
  2124. ];
  2125. const weights = [
  2126. 0.29552422471475,
  2127. 0.26926671930999,
  2128. 0.21908636251598,
  2129. 0.14945134915058,
  2130. 0.066671344308684,
  2131. 0.0,
  2132. ];
  2133. /**
  2134. * Compute the 10th order Gauss-Legendre Quadrature of the given definite integral.
  2135. *
  2136. * @param {Number} a The lower bound for the integration.
  2137. * @param {Number} b The upper bound for the integration.
  2138. * @param {Ellipsoid~RealValuedScalarFunction} func The function to integrate.
  2139. * @returns {Number} The value of the integral of the given function over the given domain.
  2140. *
  2141. * @private
  2142. */
  2143. function gaussLegendreQuadrature(a, b, func) {
  2144. //>>includeStart('debug', pragmas.debug);
  2145. RuntimeError.Check.typeOf.number("a", a);
  2146. RuntimeError.Check.typeOf.number("b", b);
  2147. RuntimeError.Check.typeOf.func("func", func);
  2148. //>>includeEnd('debug');
  2149. // The range is half of the normal range since the five weights add to one (ten weights add to two).
  2150. // The values of the abscissas are multiplied by two to account for this.
  2151. const xMean = 0.5 * (b + a);
  2152. const xRange = 0.5 * (b - a);
  2153. let sum = 0.0;
  2154. for (let i = 0; i < 5; i++) {
  2155. const dx = xRange * abscissas[i];
  2156. sum += weights[i] * (func(xMean + dx) + func(xMean - dx));
  2157. }
  2158. // Scale the sum to the range of x.
  2159. sum *= xRange;
  2160. return sum;
  2161. }
  2162. /**
  2163. * A real valued scalar function.
  2164. * @callback Ellipsoid~RealValuedScalarFunction
  2165. *
  2166. * @param {Number} x The value used to evaluate the function.
  2167. * @returns {Number} The value of the function at x.
  2168. *
  2169. * @private
  2170. */
  2171. /**
  2172. * Computes an approximation of the surface area of a rectangle on the surface of an ellipsoid using
  2173. * Gauss-Legendre 10th order quadrature.
  2174. *
  2175. * @param {Rectangle} rectangle The rectangle used for computing the surface area.
  2176. * @returns {Number} The approximate area of the rectangle on the surface of this ellipsoid.
  2177. */
  2178. Ellipsoid.prototype.surfaceArea = function (rectangle) {
  2179. //>>includeStart('debug', pragmas.debug);
  2180. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  2181. //>>includeEnd('debug');
  2182. const minLongitude = rectangle.west;
  2183. let maxLongitude = rectangle.east;
  2184. const minLatitude = rectangle.south;
  2185. const maxLatitude = rectangle.north;
  2186. while (maxLongitude < minLongitude) {
  2187. maxLongitude += ComponentDatatype.CesiumMath.TWO_PI;
  2188. }
  2189. const radiiSquared = this._radiiSquared;
  2190. const a2 = radiiSquared.x;
  2191. const b2 = radiiSquared.y;
  2192. const c2 = radiiSquared.z;
  2193. const a2b2 = a2 * b2;
  2194. return gaussLegendreQuadrature(minLatitude, maxLatitude, function (lat) {
  2195. // phi represents the angle measured from the north pole
  2196. // sin(phi) = sin(pi / 2 - lat) = cos(lat), cos(phi) is similar
  2197. const sinPhi = Math.cos(lat);
  2198. const cosPhi = Math.sin(lat);
  2199. return (
  2200. Math.cos(lat) *
  2201. gaussLegendreQuadrature(minLongitude, maxLongitude, function (lon) {
  2202. const cosTheta = Math.cos(lon);
  2203. const sinTheta = Math.sin(lon);
  2204. return Math.sqrt(
  2205. a2b2 * cosPhi * cosPhi +
  2206. c2 *
  2207. (b2 * cosTheta * cosTheta + a2 * sinTheta * sinTheta) *
  2208. sinPhi *
  2209. sinPhi
  2210. );
  2211. })
  2212. );
  2213. });
  2214. };
  2215. /**
  2216. * A 3x3 matrix, indexable as a column-major order array.
  2217. * Constructor parameters are in row-major order for code readability.
  2218. * @alias Matrix3
  2219. * @constructor
  2220. * @implements {ArrayLike<number>}
  2221. *
  2222. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  2223. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  2224. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  2225. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  2226. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  2227. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  2228. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  2229. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  2230. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  2231. *
  2232. * @see Matrix3.fromArray
  2233. * @see Matrix3.fromColumnMajorArray
  2234. * @see Matrix3.fromRowMajorArray
  2235. * @see Matrix3.fromQuaternion
  2236. * @see Matrix3.fromHeadingPitchRoll
  2237. * @see Matrix3.fromScale
  2238. * @see Matrix3.fromUniformScale
  2239. * @see Matrix3.fromCrossProduct
  2240. * @see Matrix3.fromRotationX
  2241. * @see Matrix3.fromRotationY
  2242. * @see Matrix3.fromRotationZ
  2243. * @see Matrix2
  2244. * @see Matrix4
  2245. */
  2246. function Matrix3(
  2247. column0Row0,
  2248. column1Row0,
  2249. column2Row0,
  2250. column0Row1,
  2251. column1Row1,
  2252. column2Row1,
  2253. column0Row2,
  2254. column1Row2,
  2255. column2Row2
  2256. ) {
  2257. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  2258. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  2259. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  2260. this[3] = defaultValue.defaultValue(column1Row0, 0.0);
  2261. this[4] = defaultValue.defaultValue(column1Row1, 0.0);
  2262. this[5] = defaultValue.defaultValue(column1Row2, 0.0);
  2263. this[6] = defaultValue.defaultValue(column2Row0, 0.0);
  2264. this[7] = defaultValue.defaultValue(column2Row1, 0.0);
  2265. this[8] = defaultValue.defaultValue(column2Row2, 0.0);
  2266. }
  2267. /**
  2268. * The number of elements used to pack the object into an array.
  2269. * @type {Number}
  2270. */
  2271. Matrix3.packedLength = 9;
  2272. /**
  2273. * Stores the provided instance into the provided array.
  2274. *
  2275. * @param {Matrix3} value The value to pack.
  2276. * @param {Number[]} array The array to pack into.
  2277. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2278. *
  2279. * @returns {Number[]} The array that was packed into
  2280. */
  2281. Matrix3.pack = function (value, array, startingIndex) {
  2282. //>>includeStart('debug', pragmas.debug);
  2283. RuntimeError.Check.typeOf.object("value", value);
  2284. RuntimeError.Check.defined("array", array);
  2285. //>>includeEnd('debug');
  2286. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2287. array[startingIndex++] = value[0];
  2288. array[startingIndex++] = value[1];
  2289. array[startingIndex++] = value[2];
  2290. array[startingIndex++] = value[3];
  2291. array[startingIndex++] = value[4];
  2292. array[startingIndex++] = value[5];
  2293. array[startingIndex++] = value[6];
  2294. array[startingIndex++] = value[7];
  2295. array[startingIndex++] = value[8];
  2296. return array;
  2297. };
  2298. /**
  2299. * Retrieves an instance from a packed array.
  2300. *
  2301. * @param {Number[]} array The packed array.
  2302. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2303. * @param {Matrix3} [result] The object into which to store the result.
  2304. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2305. */
  2306. Matrix3.unpack = function (array, startingIndex, result) {
  2307. //>>includeStart('debug', pragmas.debug);
  2308. RuntimeError.Check.defined("array", array);
  2309. //>>includeEnd('debug');
  2310. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  2311. if (!defaultValue.defined(result)) {
  2312. result = new Matrix3();
  2313. }
  2314. result[0] = array[startingIndex++];
  2315. result[1] = array[startingIndex++];
  2316. result[2] = array[startingIndex++];
  2317. result[3] = array[startingIndex++];
  2318. result[4] = array[startingIndex++];
  2319. result[5] = array[startingIndex++];
  2320. result[6] = array[startingIndex++];
  2321. result[7] = array[startingIndex++];
  2322. result[8] = array[startingIndex++];
  2323. return result;
  2324. };
  2325. /**
  2326. * Flattens an array of Matrix3s into an array of components. The components
  2327. * are stored in column-major order.
  2328. *
  2329. * @param {Matrix3[]} array The array of matrices to pack.
  2330. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 9 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 9) elements.
  2331. * @returns {Number[]} The packed array.
  2332. */
  2333. Matrix3.packArray = function (array, result) {
  2334. //>>includeStart('debug', pragmas.debug);
  2335. RuntimeError.Check.defined("array", array);
  2336. //>>includeEnd('debug');
  2337. const length = array.length;
  2338. const resultLength = length * 9;
  2339. if (!defaultValue.defined(result)) {
  2340. result = new Array(resultLength);
  2341. } else if (!Array.isArray(result) && result.length !== resultLength) {
  2342. //>>includeStart('debug', pragmas.debug);
  2343. throw new RuntimeError.DeveloperError(
  2344. "If result is a typed array, it must have exactly array.length * 9 elements"
  2345. );
  2346. //>>includeEnd('debug');
  2347. } else if (result.length !== resultLength) {
  2348. result.length = resultLength;
  2349. }
  2350. for (let i = 0; i < length; ++i) {
  2351. Matrix3.pack(array[i], result, i * 9);
  2352. }
  2353. return result;
  2354. };
  2355. /**
  2356. * Unpacks an array of column-major matrix components into an array of Matrix3s.
  2357. *
  2358. * @param {Number[]} array The array of components to unpack.
  2359. * @param {Matrix3[]} [result] The array onto which to store the result.
  2360. * @returns {Matrix3[]} The unpacked array.
  2361. */
  2362. Matrix3.unpackArray = function (array, result) {
  2363. //>>includeStart('debug', pragmas.debug);
  2364. RuntimeError.Check.defined("array", array);
  2365. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 9);
  2366. if (array.length % 9 !== 0) {
  2367. throw new RuntimeError.DeveloperError("array length must be a multiple of 9.");
  2368. }
  2369. //>>includeEnd('debug');
  2370. const length = array.length;
  2371. if (!defaultValue.defined(result)) {
  2372. result = new Array(length / 9);
  2373. } else {
  2374. result.length = length / 9;
  2375. }
  2376. for (let i = 0; i < length; i += 9) {
  2377. const index = i / 9;
  2378. result[index] = Matrix3.unpack(array, i, result[index]);
  2379. }
  2380. return result;
  2381. };
  2382. /**
  2383. * Duplicates a Matrix3 instance.
  2384. *
  2385. * @param {Matrix3} matrix The matrix to duplicate.
  2386. * @param {Matrix3} [result] The object onto which to store the result.
  2387. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  2388. */
  2389. Matrix3.clone = function (matrix, result) {
  2390. if (!defaultValue.defined(matrix)) {
  2391. return undefined;
  2392. }
  2393. if (!defaultValue.defined(result)) {
  2394. return new Matrix3(
  2395. matrix[0],
  2396. matrix[3],
  2397. matrix[6],
  2398. matrix[1],
  2399. matrix[4],
  2400. matrix[7],
  2401. matrix[2],
  2402. matrix[5],
  2403. matrix[8]
  2404. );
  2405. }
  2406. result[0] = matrix[0];
  2407. result[1] = matrix[1];
  2408. result[2] = matrix[2];
  2409. result[3] = matrix[3];
  2410. result[4] = matrix[4];
  2411. result[5] = matrix[5];
  2412. result[6] = matrix[6];
  2413. result[7] = matrix[7];
  2414. result[8] = matrix[8];
  2415. return result;
  2416. };
  2417. /**
  2418. * Creates a Matrix3 from 9 consecutive elements in an array.
  2419. *
  2420. * @function
  2421. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  2422. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  2423. * @param {Matrix3} [result] The object onto which to store the result.
  2424. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  2425. *
  2426. * @example
  2427. * // Create the Matrix3:
  2428. * // [1.0, 2.0, 3.0]
  2429. * // [1.0, 2.0, 3.0]
  2430. * // [1.0, 2.0, 3.0]
  2431. *
  2432. * const v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2433. * const m = Cesium.Matrix3.fromArray(v);
  2434. *
  2435. * // Create same Matrix3 with using an offset into an array
  2436. * const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  2437. * const m2 = Cesium.Matrix3.fromArray(v2, 2);
  2438. */
  2439. Matrix3.fromArray = Matrix3.unpack;
  2440. /**
  2441. * Creates a Matrix3 instance from a column-major order array.
  2442. *
  2443. * @param {Number[]} values The column-major order array.
  2444. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2445. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2446. */
  2447. Matrix3.fromColumnMajorArray = function (values, result) {
  2448. //>>includeStart('debug', pragmas.debug);
  2449. RuntimeError.Check.defined("values", values);
  2450. //>>includeEnd('debug');
  2451. return Matrix3.clone(values, result);
  2452. };
  2453. /**
  2454. * Creates a Matrix3 instance from a row-major order array.
  2455. * The resulting matrix will be in column-major order.
  2456. *
  2457. * @param {Number[]} values The row-major order array.
  2458. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2459. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2460. */
  2461. Matrix3.fromRowMajorArray = function (values, result) {
  2462. //>>includeStart('debug', pragmas.debug);
  2463. RuntimeError.Check.defined("values", values);
  2464. //>>includeEnd('debug');
  2465. if (!defaultValue.defined(result)) {
  2466. return new Matrix3(
  2467. values[0],
  2468. values[1],
  2469. values[2],
  2470. values[3],
  2471. values[4],
  2472. values[5],
  2473. values[6],
  2474. values[7],
  2475. values[8]
  2476. );
  2477. }
  2478. result[0] = values[0];
  2479. result[1] = values[3];
  2480. result[2] = values[6];
  2481. result[3] = values[1];
  2482. result[4] = values[4];
  2483. result[5] = values[7];
  2484. result[6] = values[2];
  2485. result[7] = values[5];
  2486. result[8] = values[8];
  2487. return result;
  2488. };
  2489. /**
  2490. * Computes a 3x3 rotation matrix from the provided quaternion.
  2491. *
  2492. * @param {Quaternion} quaternion the quaternion to use.
  2493. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2494. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  2495. */
  2496. Matrix3.fromQuaternion = function (quaternion, result) {
  2497. //>>includeStart('debug', pragmas.debug);
  2498. RuntimeError.Check.typeOf.object("quaternion", quaternion);
  2499. //>>includeEnd('debug');
  2500. const x2 = quaternion.x * quaternion.x;
  2501. const xy = quaternion.x * quaternion.y;
  2502. const xz = quaternion.x * quaternion.z;
  2503. const xw = quaternion.x * quaternion.w;
  2504. const y2 = quaternion.y * quaternion.y;
  2505. const yz = quaternion.y * quaternion.z;
  2506. const yw = quaternion.y * quaternion.w;
  2507. const z2 = quaternion.z * quaternion.z;
  2508. const zw = quaternion.z * quaternion.w;
  2509. const w2 = quaternion.w * quaternion.w;
  2510. const m00 = x2 - y2 - z2 + w2;
  2511. const m01 = 2.0 * (xy - zw);
  2512. const m02 = 2.0 * (xz + yw);
  2513. const m10 = 2.0 * (xy + zw);
  2514. const m11 = -x2 + y2 - z2 + w2;
  2515. const m12 = 2.0 * (yz - xw);
  2516. const m20 = 2.0 * (xz - yw);
  2517. const m21 = 2.0 * (yz + xw);
  2518. const m22 = -x2 - y2 + z2 + w2;
  2519. if (!defaultValue.defined(result)) {
  2520. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2521. }
  2522. result[0] = m00;
  2523. result[1] = m10;
  2524. result[2] = m20;
  2525. result[3] = m01;
  2526. result[4] = m11;
  2527. result[5] = m21;
  2528. result[6] = m02;
  2529. result[7] = m12;
  2530. result[8] = m22;
  2531. return result;
  2532. };
  2533. /**
  2534. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  2535. *
  2536. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  2537. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2538. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  2539. */
  2540. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  2541. //>>includeStart('debug', pragmas.debug);
  2542. RuntimeError.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  2543. //>>includeEnd('debug');
  2544. const cosTheta = Math.cos(-headingPitchRoll.pitch);
  2545. const cosPsi = Math.cos(-headingPitchRoll.heading);
  2546. const cosPhi = Math.cos(headingPitchRoll.roll);
  2547. const sinTheta = Math.sin(-headingPitchRoll.pitch);
  2548. const sinPsi = Math.sin(-headingPitchRoll.heading);
  2549. const sinPhi = Math.sin(headingPitchRoll.roll);
  2550. const m00 = cosTheta * cosPsi;
  2551. const m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  2552. const m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  2553. const m10 = cosTheta * sinPsi;
  2554. const m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  2555. const m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  2556. const m20 = -sinTheta;
  2557. const m21 = sinPhi * cosTheta;
  2558. const m22 = cosPhi * cosTheta;
  2559. if (!defaultValue.defined(result)) {
  2560. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  2561. }
  2562. result[0] = m00;
  2563. result[1] = m10;
  2564. result[2] = m20;
  2565. result[3] = m01;
  2566. result[4] = m11;
  2567. result[5] = m21;
  2568. result[6] = m02;
  2569. result[7] = m12;
  2570. result[8] = m22;
  2571. return result;
  2572. };
  2573. /**
  2574. * Computes a Matrix3 instance representing a non-uniform scale.
  2575. *
  2576. * @param {Cartesian3} scale The x, y, and z scale factors.
  2577. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2578. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2579. *
  2580. * @example
  2581. * // Creates
  2582. * // [7.0, 0.0, 0.0]
  2583. * // [0.0, 8.0, 0.0]
  2584. * // [0.0, 0.0, 9.0]
  2585. * const m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2586. */
  2587. Matrix3.fromScale = function (scale, result) {
  2588. //>>includeStart('debug', pragmas.debug);
  2589. RuntimeError.Check.typeOf.object("scale", scale);
  2590. //>>includeEnd('debug');
  2591. if (!defaultValue.defined(result)) {
  2592. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  2593. }
  2594. result[0] = scale.x;
  2595. result[1] = 0.0;
  2596. result[2] = 0.0;
  2597. result[3] = 0.0;
  2598. result[4] = scale.y;
  2599. result[5] = 0.0;
  2600. result[6] = 0.0;
  2601. result[7] = 0.0;
  2602. result[8] = scale.z;
  2603. return result;
  2604. };
  2605. /**
  2606. * Computes a Matrix3 instance representing a uniform scale.
  2607. *
  2608. * @param {Number} scale The uniform scale factor.
  2609. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2610. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2611. *
  2612. * @example
  2613. * // Creates
  2614. * // [2.0, 0.0, 0.0]
  2615. * // [0.0, 2.0, 0.0]
  2616. * // [0.0, 0.0, 2.0]
  2617. * const m = Cesium.Matrix3.fromUniformScale(2.0);
  2618. */
  2619. Matrix3.fromUniformScale = function (scale, result) {
  2620. //>>includeStart('debug', pragmas.debug);
  2621. RuntimeError.Check.typeOf.number("scale", scale);
  2622. //>>includeEnd('debug');
  2623. if (!defaultValue.defined(result)) {
  2624. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  2625. }
  2626. result[0] = scale;
  2627. result[1] = 0.0;
  2628. result[2] = 0.0;
  2629. result[3] = 0.0;
  2630. result[4] = scale;
  2631. result[5] = 0.0;
  2632. result[6] = 0.0;
  2633. result[7] = 0.0;
  2634. result[8] = scale;
  2635. return result;
  2636. };
  2637. /**
  2638. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  2639. *
  2640. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  2641. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2642. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2643. *
  2644. * @example
  2645. * // Creates
  2646. * // [0.0, -9.0, 8.0]
  2647. * // [9.0, 0.0, -7.0]
  2648. * // [-8.0, 7.0, 0.0]
  2649. * const m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2650. */
  2651. Matrix3.fromCrossProduct = function (vector, result) {
  2652. //>>includeStart('debug', pragmas.debug);
  2653. RuntimeError.Check.typeOf.object("vector", vector);
  2654. //>>includeEnd('debug');
  2655. if (!defaultValue.defined(result)) {
  2656. return new Matrix3(
  2657. 0.0,
  2658. -vector.z,
  2659. vector.y,
  2660. vector.z,
  2661. 0.0,
  2662. -vector.x,
  2663. -vector.y,
  2664. vector.x,
  2665. 0.0
  2666. );
  2667. }
  2668. result[0] = 0.0;
  2669. result[1] = vector.z;
  2670. result[2] = -vector.y;
  2671. result[3] = -vector.z;
  2672. result[4] = 0.0;
  2673. result[5] = vector.x;
  2674. result[6] = vector.y;
  2675. result[7] = -vector.x;
  2676. result[8] = 0.0;
  2677. return result;
  2678. };
  2679. /**
  2680. * Creates a rotation matrix around the x-axis.
  2681. *
  2682. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2683. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2684. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2685. *
  2686. * @example
  2687. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  2688. * const p = new Cesium.Cartesian3(5, 6, 7);
  2689. * const m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  2690. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2691. */
  2692. Matrix3.fromRotationX = function (angle, result) {
  2693. //>>includeStart('debug', pragmas.debug);
  2694. RuntimeError.Check.typeOf.number("angle", angle);
  2695. //>>includeEnd('debug');
  2696. const cosAngle = Math.cos(angle);
  2697. const sinAngle = Math.sin(angle);
  2698. if (!defaultValue.defined(result)) {
  2699. return new Matrix3(
  2700. 1.0,
  2701. 0.0,
  2702. 0.0,
  2703. 0.0,
  2704. cosAngle,
  2705. -sinAngle,
  2706. 0.0,
  2707. sinAngle,
  2708. cosAngle
  2709. );
  2710. }
  2711. result[0] = 1.0;
  2712. result[1] = 0.0;
  2713. result[2] = 0.0;
  2714. result[3] = 0.0;
  2715. result[4] = cosAngle;
  2716. result[5] = sinAngle;
  2717. result[6] = 0.0;
  2718. result[7] = -sinAngle;
  2719. result[8] = cosAngle;
  2720. return result;
  2721. };
  2722. /**
  2723. * Creates a rotation matrix around the y-axis.
  2724. *
  2725. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2726. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2727. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2728. *
  2729. * @example
  2730. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  2731. * const p = new Cesium.Cartesian3(5, 6, 7);
  2732. * const m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  2733. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2734. */
  2735. Matrix3.fromRotationY = function (angle, result) {
  2736. //>>includeStart('debug', pragmas.debug);
  2737. RuntimeError.Check.typeOf.number("angle", angle);
  2738. //>>includeEnd('debug');
  2739. const cosAngle = Math.cos(angle);
  2740. const sinAngle = Math.sin(angle);
  2741. if (!defaultValue.defined(result)) {
  2742. return new Matrix3(
  2743. cosAngle,
  2744. 0.0,
  2745. sinAngle,
  2746. 0.0,
  2747. 1.0,
  2748. 0.0,
  2749. -sinAngle,
  2750. 0.0,
  2751. cosAngle
  2752. );
  2753. }
  2754. result[0] = cosAngle;
  2755. result[1] = 0.0;
  2756. result[2] = -sinAngle;
  2757. result[3] = 0.0;
  2758. result[4] = 1.0;
  2759. result[5] = 0.0;
  2760. result[6] = sinAngle;
  2761. result[7] = 0.0;
  2762. result[8] = cosAngle;
  2763. return result;
  2764. };
  2765. /**
  2766. * Creates a rotation matrix around the z-axis.
  2767. *
  2768. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  2769. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2770. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  2771. *
  2772. * @example
  2773. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  2774. * const p = new Cesium.Cartesian3(5, 6, 7);
  2775. * const m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  2776. * const rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  2777. */
  2778. Matrix3.fromRotationZ = function (angle, result) {
  2779. //>>includeStart('debug', pragmas.debug);
  2780. RuntimeError.Check.typeOf.number("angle", angle);
  2781. //>>includeEnd('debug');
  2782. const cosAngle = Math.cos(angle);
  2783. const sinAngle = Math.sin(angle);
  2784. if (!defaultValue.defined(result)) {
  2785. return new Matrix3(
  2786. cosAngle,
  2787. -sinAngle,
  2788. 0.0,
  2789. sinAngle,
  2790. cosAngle,
  2791. 0.0,
  2792. 0.0,
  2793. 0.0,
  2794. 1.0
  2795. );
  2796. }
  2797. result[0] = cosAngle;
  2798. result[1] = sinAngle;
  2799. result[2] = 0.0;
  2800. result[3] = -sinAngle;
  2801. result[4] = cosAngle;
  2802. result[5] = 0.0;
  2803. result[6] = 0.0;
  2804. result[7] = 0.0;
  2805. result[8] = 1.0;
  2806. return result;
  2807. };
  2808. /**
  2809. * Creates an Array from the provided Matrix3 instance.
  2810. * The array will be in column-major order.
  2811. *
  2812. * @param {Matrix3} matrix The matrix to use..
  2813. * @param {Number[]} [result] The Array onto which to store the result.
  2814. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  2815. */
  2816. Matrix3.toArray = function (matrix, result) {
  2817. //>>includeStart('debug', pragmas.debug);
  2818. RuntimeError.Check.typeOf.object("matrix", matrix);
  2819. //>>includeEnd('debug');
  2820. if (!defaultValue.defined(result)) {
  2821. return [
  2822. matrix[0],
  2823. matrix[1],
  2824. matrix[2],
  2825. matrix[3],
  2826. matrix[4],
  2827. matrix[5],
  2828. matrix[6],
  2829. matrix[7],
  2830. matrix[8],
  2831. ];
  2832. }
  2833. result[0] = matrix[0];
  2834. result[1] = matrix[1];
  2835. result[2] = matrix[2];
  2836. result[3] = matrix[3];
  2837. result[4] = matrix[4];
  2838. result[5] = matrix[5];
  2839. result[6] = matrix[6];
  2840. result[7] = matrix[7];
  2841. result[8] = matrix[8];
  2842. return result;
  2843. };
  2844. /**
  2845. * Computes the array index of the element at the provided row and column.
  2846. *
  2847. * @param {Number} column The zero-based index of the column.
  2848. * @param {Number} row The zero-based index of the row.
  2849. * @returns {Number} The index of the element at the provided row and column.
  2850. *
  2851. * @exception {DeveloperError} row must be 0, 1, or 2.
  2852. * @exception {DeveloperError} column must be 0, 1, or 2.
  2853. *
  2854. * @example
  2855. * const myMatrix = new Cesium.Matrix3();
  2856. * const column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  2857. * const column1Row0 = myMatrix[column1Row0Index]
  2858. * myMatrix[column1Row0Index] = 10.0;
  2859. */
  2860. Matrix3.getElementIndex = function (column, row) {
  2861. //>>includeStart('debug', pragmas.debug);
  2862. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  2863. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 2);
  2864. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  2865. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 2);
  2866. //>>includeEnd('debug');
  2867. return column * 3 + row;
  2868. };
  2869. /**
  2870. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  2871. *
  2872. * @param {Matrix3} matrix The matrix to use.
  2873. * @param {Number} index The zero-based index of the column to retrieve.
  2874. * @param {Cartesian3} result The object onto which to store the result.
  2875. * @returns {Cartesian3} The modified result parameter.
  2876. *
  2877. * @exception {DeveloperError} index must be 0, 1, or 2.
  2878. */
  2879. Matrix3.getColumn = function (matrix, index, result) {
  2880. //>>includeStart('debug', pragmas.debug);
  2881. RuntimeError.Check.typeOf.object("matrix", matrix);
  2882. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2883. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2884. RuntimeError.Check.typeOf.object("result", result);
  2885. //>>includeEnd('debug');
  2886. const startIndex = index * 3;
  2887. const x = matrix[startIndex];
  2888. const y = matrix[startIndex + 1];
  2889. const z = matrix[startIndex + 2];
  2890. result.x = x;
  2891. result.y = y;
  2892. result.z = z;
  2893. return result;
  2894. };
  2895. /**
  2896. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  2897. *
  2898. * @param {Matrix3} matrix The matrix to use.
  2899. * @param {Number} index The zero-based index of the column to set.
  2900. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  2901. * @param {Matrix3} result The object onto which to store the result.
  2902. * @returns {Matrix3} The modified result parameter.
  2903. *
  2904. * @exception {DeveloperError} index must be 0, 1, or 2.
  2905. */
  2906. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  2907. //>>includeStart('debug', pragmas.debug);
  2908. RuntimeError.Check.typeOf.object("matrix", matrix);
  2909. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2910. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2911. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  2912. RuntimeError.Check.typeOf.object("result", result);
  2913. //>>includeEnd('debug');
  2914. result = Matrix3.clone(matrix, result);
  2915. const startIndex = index * 3;
  2916. result[startIndex] = cartesian.x;
  2917. result[startIndex + 1] = cartesian.y;
  2918. result[startIndex + 2] = cartesian.z;
  2919. return result;
  2920. };
  2921. /**
  2922. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  2923. *
  2924. * @param {Matrix3} matrix The matrix to use.
  2925. * @param {Number} index The zero-based index of the row to retrieve.
  2926. * @param {Cartesian3} result The object onto which to store the result.
  2927. * @returns {Cartesian3} The modified result parameter.
  2928. *
  2929. * @exception {DeveloperError} index must be 0, 1, or 2.
  2930. */
  2931. Matrix3.getRow = function (matrix, index, result) {
  2932. //>>includeStart('debug', pragmas.debug);
  2933. RuntimeError.Check.typeOf.object("matrix", matrix);
  2934. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2935. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2936. RuntimeError.Check.typeOf.object("result", result);
  2937. //>>includeEnd('debug');
  2938. const x = matrix[index];
  2939. const y = matrix[index + 3];
  2940. const z = matrix[index + 6];
  2941. result.x = x;
  2942. result.y = y;
  2943. result.z = z;
  2944. return result;
  2945. };
  2946. /**
  2947. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  2948. *
  2949. * @param {Matrix3} matrix The matrix to use.
  2950. * @param {Number} index The zero-based index of the row to set.
  2951. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  2952. * @param {Matrix3} result The object onto which to store the result.
  2953. * @returns {Matrix3} The modified result parameter.
  2954. *
  2955. * @exception {DeveloperError} index must be 0, 1, or 2.
  2956. */
  2957. Matrix3.setRow = function (matrix, index, cartesian, result) {
  2958. //>>includeStart('debug', pragmas.debug);
  2959. RuntimeError.Check.typeOf.object("matrix", matrix);
  2960. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  2961. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  2962. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  2963. RuntimeError.Check.typeOf.object("result", result);
  2964. //>>includeEnd('debug');
  2965. result = Matrix3.clone(matrix, result);
  2966. result[index] = cartesian.x;
  2967. result[index + 3] = cartesian.y;
  2968. result[index + 6] = cartesian.z;
  2969. return result;
  2970. };
  2971. const scaleScratch1$2 = new Cartesian3();
  2972. /**
  2973. * Computes a new matrix that replaces the scale with the provided scale.
  2974. * This assumes the matrix is an affine transformation.
  2975. *
  2976. * @param {Matrix3} matrix The matrix to use.
  2977. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  2978. * @param {Matrix3} result The object onto which to store the result.
  2979. * @returns {Matrix3} The modified result parameter.
  2980. *
  2981. * @see Matrix3.setUniformScale
  2982. * @see Matrix3.fromScale
  2983. * @see Matrix3.fromUniformScale
  2984. * @see Matrix3.multiplyByScale
  2985. * @see Matrix3.multiplyByUniformScale
  2986. * @see Matrix3.getScale
  2987. */
  2988. Matrix3.setScale = function (matrix, scale, result) {
  2989. //>>includeStart('debug', pragmas.debug);
  2990. RuntimeError.Check.typeOf.object("matrix", matrix);
  2991. RuntimeError.Check.typeOf.object("scale", scale);
  2992. RuntimeError.Check.typeOf.object("result", result);
  2993. //>>includeEnd('debug');
  2994. const existingScale = Matrix3.getScale(matrix, scaleScratch1$2);
  2995. const scaleRatioX = scale.x / existingScale.x;
  2996. const scaleRatioY = scale.y / existingScale.y;
  2997. const scaleRatioZ = scale.z / existingScale.z;
  2998. result[0] = matrix[0] * scaleRatioX;
  2999. result[1] = matrix[1] * scaleRatioX;
  3000. result[2] = matrix[2] * scaleRatioX;
  3001. result[3] = matrix[3] * scaleRatioY;
  3002. result[4] = matrix[4] * scaleRatioY;
  3003. result[5] = matrix[5] * scaleRatioY;
  3004. result[6] = matrix[6] * scaleRatioZ;
  3005. result[7] = matrix[7] * scaleRatioZ;
  3006. result[8] = matrix[8] * scaleRatioZ;
  3007. return result;
  3008. };
  3009. const scaleScratch2$2 = new Cartesian3();
  3010. /**
  3011. * Computes a new matrix that replaces the scale with the provided uniform scale.
  3012. * This assumes the matrix is an affine transformation.
  3013. *
  3014. * @param {Matrix3} matrix The matrix to use.
  3015. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  3016. * @param {Matrix3} result The object onto which to store the result.
  3017. * @returns {Matrix3} The modified result parameter.
  3018. *
  3019. * @see Matrix3.setScale
  3020. * @see Matrix3.fromScale
  3021. * @see Matrix3.fromUniformScale
  3022. * @see Matrix3.multiplyByScale
  3023. * @see Matrix3.multiplyByUniformScale
  3024. * @see Matrix3.getScale
  3025. */
  3026. Matrix3.setUniformScale = function (matrix, scale, result) {
  3027. //>>includeStart('debug', pragmas.debug);
  3028. RuntimeError.Check.typeOf.object("matrix", matrix);
  3029. RuntimeError.Check.typeOf.number("scale", scale);
  3030. RuntimeError.Check.typeOf.object("result", result);
  3031. //>>includeEnd('debug');
  3032. const existingScale = Matrix3.getScale(matrix, scaleScratch2$2);
  3033. const scaleRatioX = scale / existingScale.x;
  3034. const scaleRatioY = scale / existingScale.y;
  3035. const scaleRatioZ = scale / existingScale.z;
  3036. result[0] = matrix[0] * scaleRatioX;
  3037. result[1] = matrix[1] * scaleRatioX;
  3038. result[2] = matrix[2] * scaleRatioX;
  3039. result[3] = matrix[3] * scaleRatioY;
  3040. result[4] = matrix[4] * scaleRatioY;
  3041. result[5] = matrix[5] * scaleRatioY;
  3042. result[6] = matrix[6] * scaleRatioZ;
  3043. result[7] = matrix[7] * scaleRatioZ;
  3044. result[8] = matrix[8] * scaleRatioZ;
  3045. return result;
  3046. };
  3047. const scratchColumn$2 = new Cartesian3();
  3048. /**
  3049. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  3050. *
  3051. * @param {Matrix3} matrix The matrix.
  3052. * @param {Cartesian3} result The object onto which to store the result.
  3053. * @returns {Cartesian3} The modified result parameter.
  3054. *
  3055. * @see Matrix3.multiplyByScale
  3056. * @see Matrix3.multiplyByUniformScale
  3057. * @see Matrix3.fromScale
  3058. * @see Matrix3.fromUniformScale
  3059. * @see Matrix3.setScale
  3060. * @see Matrix3.setUniformScale
  3061. */
  3062. Matrix3.getScale = function (matrix, result) {
  3063. //>>includeStart('debug', pragmas.debug);
  3064. RuntimeError.Check.typeOf.object("matrix", matrix);
  3065. RuntimeError.Check.typeOf.object("result", result);
  3066. //>>includeEnd('debug');
  3067. result.x = Cartesian3.magnitude(
  3068. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$2)
  3069. );
  3070. result.y = Cartesian3.magnitude(
  3071. Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn$2)
  3072. );
  3073. result.z = Cartesian3.magnitude(
  3074. Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn$2)
  3075. );
  3076. return result;
  3077. };
  3078. const scaleScratch3$2 = new Cartesian3();
  3079. /**
  3080. * Computes the maximum scale assuming the matrix is an affine transformation.
  3081. * The maximum scale is the maximum length of the column vectors.
  3082. *
  3083. * @param {Matrix3} matrix The matrix.
  3084. * @returns {Number} The maximum scale.
  3085. */
  3086. Matrix3.getMaximumScale = function (matrix) {
  3087. Matrix3.getScale(matrix, scaleScratch3$2);
  3088. return Cartesian3.maximumComponent(scaleScratch3$2);
  3089. };
  3090. const scaleScratch4$2 = new Cartesian3();
  3091. /**
  3092. * Sets the rotation assuming the matrix is an affine transformation.
  3093. *
  3094. * @param {Matrix3} matrix The matrix.
  3095. * @param {Matrix3} rotation The rotation matrix.
  3096. * @returns {Matrix3} The modified result parameter.
  3097. *
  3098. * @see Matrix3.getRotation
  3099. */
  3100. Matrix3.setRotation = function (matrix, rotation, result) {
  3101. //>>includeStart('debug', pragmas.debug);
  3102. RuntimeError.Check.typeOf.object("matrix", matrix);
  3103. RuntimeError.Check.typeOf.object("result", result);
  3104. //>>includeEnd('debug');
  3105. const scale = Matrix3.getScale(matrix, scaleScratch4$2);
  3106. result[0] = rotation[0] * scale.x;
  3107. result[1] = rotation[1] * scale.x;
  3108. result[2] = rotation[2] * scale.x;
  3109. result[3] = rotation[3] * scale.y;
  3110. result[4] = rotation[4] * scale.y;
  3111. result[5] = rotation[5] * scale.y;
  3112. result[6] = rotation[6] * scale.z;
  3113. result[7] = rotation[7] * scale.z;
  3114. result[8] = rotation[8] * scale.z;
  3115. return result;
  3116. };
  3117. const scaleScratch5$2 = new Cartesian3();
  3118. /**
  3119. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  3120. *
  3121. * @param {Matrix3} matrix The matrix.
  3122. * @param {Matrix3} result The object onto which to store the result.
  3123. * @returns {Matrix3} The modified result parameter.
  3124. *
  3125. * @see Matrix3.setRotation
  3126. */
  3127. Matrix3.getRotation = function (matrix, result) {
  3128. //>>includeStart('debug', pragmas.debug);
  3129. RuntimeError.Check.typeOf.object("matrix", matrix);
  3130. RuntimeError.Check.typeOf.object("result", result);
  3131. //>>includeEnd('debug');
  3132. const scale = Matrix3.getScale(matrix, scaleScratch5$2);
  3133. result[0] = matrix[0] / scale.x;
  3134. result[1] = matrix[1] / scale.x;
  3135. result[2] = matrix[2] / scale.x;
  3136. result[3] = matrix[3] / scale.y;
  3137. result[4] = matrix[4] / scale.y;
  3138. result[5] = matrix[5] / scale.y;
  3139. result[6] = matrix[6] / scale.z;
  3140. result[7] = matrix[7] / scale.z;
  3141. result[8] = matrix[8] / scale.z;
  3142. return result;
  3143. };
  3144. /**
  3145. * Computes the product of two matrices.
  3146. *
  3147. * @param {Matrix3} left The first matrix.
  3148. * @param {Matrix3} right The second matrix.
  3149. * @param {Matrix3} result The object onto which to store the result.
  3150. * @returns {Matrix3} The modified result parameter.
  3151. */
  3152. Matrix3.multiply = function (left, right, result) {
  3153. //>>includeStart('debug', pragmas.debug);
  3154. RuntimeError.Check.typeOf.object("left", left);
  3155. RuntimeError.Check.typeOf.object("right", right);
  3156. RuntimeError.Check.typeOf.object("result", result);
  3157. //>>includeEnd('debug');
  3158. const column0Row0 =
  3159. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  3160. const column0Row1 =
  3161. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  3162. const column0Row2 =
  3163. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  3164. const column1Row0 =
  3165. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  3166. const column1Row1 =
  3167. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  3168. const column1Row2 =
  3169. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  3170. const column2Row0 =
  3171. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  3172. const column2Row1 =
  3173. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  3174. const column2Row2 =
  3175. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  3176. result[0] = column0Row0;
  3177. result[1] = column0Row1;
  3178. result[2] = column0Row2;
  3179. result[3] = column1Row0;
  3180. result[4] = column1Row1;
  3181. result[5] = column1Row2;
  3182. result[6] = column2Row0;
  3183. result[7] = column2Row1;
  3184. result[8] = column2Row2;
  3185. return result;
  3186. };
  3187. /**
  3188. * Computes the sum of two matrices.
  3189. *
  3190. * @param {Matrix3} left The first matrix.
  3191. * @param {Matrix3} right The second matrix.
  3192. * @param {Matrix3} result The object onto which to store the result.
  3193. * @returns {Matrix3} The modified result parameter.
  3194. */
  3195. Matrix3.add = function (left, right, result) {
  3196. //>>includeStart('debug', pragmas.debug);
  3197. RuntimeError.Check.typeOf.object("left", left);
  3198. RuntimeError.Check.typeOf.object("right", right);
  3199. RuntimeError.Check.typeOf.object("result", result);
  3200. //>>includeEnd('debug');
  3201. result[0] = left[0] + right[0];
  3202. result[1] = left[1] + right[1];
  3203. result[2] = left[2] + right[2];
  3204. result[3] = left[3] + right[3];
  3205. result[4] = left[4] + right[4];
  3206. result[5] = left[5] + right[5];
  3207. result[6] = left[6] + right[6];
  3208. result[7] = left[7] + right[7];
  3209. result[8] = left[8] + right[8];
  3210. return result;
  3211. };
  3212. /**
  3213. * Computes the difference of two matrices.
  3214. *
  3215. * @param {Matrix3} left The first matrix.
  3216. * @param {Matrix3} right The second matrix.
  3217. * @param {Matrix3} result The object onto which to store the result.
  3218. * @returns {Matrix3} The modified result parameter.
  3219. */
  3220. Matrix3.subtract = function (left, right, result) {
  3221. //>>includeStart('debug', pragmas.debug);
  3222. RuntimeError.Check.typeOf.object("left", left);
  3223. RuntimeError.Check.typeOf.object("right", right);
  3224. RuntimeError.Check.typeOf.object("result", result);
  3225. //>>includeEnd('debug');
  3226. result[0] = left[0] - right[0];
  3227. result[1] = left[1] - right[1];
  3228. result[2] = left[2] - right[2];
  3229. result[3] = left[3] - right[3];
  3230. result[4] = left[4] - right[4];
  3231. result[5] = left[5] - right[5];
  3232. result[6] = left[6] - right[6];
  3233. result[7] = left[7] - right[7];
  3234. result[8] = left[8] - right[8];
  3235. return result;
  3236. };
  3237. /**
  3238. * Computes the product of a matrix and a column vector.
  3239. *
  3240. * @param {Matrix3} matrix The matrix.
  3241. * @param {Cartesian3} cartesian The column.
  3242. * @param {Cartesian3} result The object onto which to store the result.
  3243. * @returns {Cartesian3} The modified result parameter.
  3244. */
  3245. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  3246. //>>includeStart('debug', pragmas.debug);
  3247. RuntimeError.Check.typeOf.object("matrix", matrix);
  3248. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  3249. RuntimeError.Check.typeOf.object("result", result);
  3250. //>>includeEnd('debug');
  3251. const vX = cartesian.x;
  3252. const vY = cartesian.y;
  3253. const vZ = cartesian.z;
  3254. const x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  3255. const y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  3256. const z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  3257. result.x = x;
  3258. result.y = y;
  3259. result.z = z;
  3260. return result;
  3261. };
  3262. /**
  3263. * Computes the product of a matrix and a scalar.
  3264. *
  3265. * @param {Matrix3} matrix The matrix.
  3266. * @param {Number} scalar The number to multiply by.
  3267. * @param {Matrix3} result The object onto which to store the result.
  3268. * @returns {Matrix3} The modified result parameter.
  3269. */
  3270. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  3271. //>>includeStart('debug', pragmas.debug);
  3272. RuntimeError.Check.typeOf.object("matrix", matrix);
  3273. RuntimeError.Check.typeOf.number("scalar", scalar);
  3274. RuntimeError.Check.typeOf.object("result", result);
  3275. //>>includeEnd('debug');
  3276. result[0] = matrix[0] * scalar;
  3277. result[1] = matrix[1] * scalar;
  3278. result[2] = matrix[2] * scalar;
  3279. result[3] = matrix[3] * scalar;
  3280. result[4] = matrix[4] * scalar;
  3281. result[5] = matrix[5] * scalar;
  3282. result[6] = matrix[6] * scalar;
  3283. result[7] = matrix[7] * scalar;
  3284. result[8] = matrix[8] * scalar;
  3285. return result;
  3286. };
  3287. /**
  3288. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  3289. *
  3290. * @param {Matrix3} matrix The matrix on the left-hand side.
  3291. * @param {Number} scale The non-uniform scale on the right-hand side.
  3292. * @param {Matrix3} result The object onto which to store the result.
  3293. * @returns {Matrix3} The modified result parameter.
  3294. *
  3295. *
  3296. * @example
  3297. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  3298. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  3299. *
  3300. * @see Matrix3.multiplyByUniformScale
  3301. * @see Matrix3.fromScale
  3302. * @see Matrix3.fromUniformScale
  3303. * @see Matrix3.setScale
  3304. * @see Matrix3.setUniformScale
  3305. * @see Matrix3.getScale
  3306. */
  3307. Matrix3.multiplyByScale = function (matrix, scale, result) {
  3308. //>>includeStart('debug', pragmas.debug);
  3309. RuntimeError.Check.typeOf.object("matrix", matrix);
  3310. RuntimeError.Check.typeOf.object("scale", scale);
  3311. RuntimeError.Check.typeOf.object("result", result);
  3312. //>>includeEnd('debug');
  3313. result[0] = matrix[0] * scale.x;
  3314. result[1] = matrix[1] * scale.x;
  3315. result[2] = matrix[2] * scale.x;
  3316. result[3] = matrix[3] * scale.y;
  3317. result[4] = matrix[4] * scale.y;
  3318. result[5] = matrix[5] * scale.y;
  3319. result[6] = matrix[6] * scale.z;
  3320. result[7] = matrix[7] * scale.z;
  3321. result[8] = matrix[8] * scale.z;
  3322. return result;
  3323. };
  3324. /**
  3325. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  3326. *
  3327. * @param {Matrix3} matrix The matrix on the left-hand side.
  3328. * @param {Number} scale The uniform scale on the right-hand side.
  3329. * @param {Matrix3} result The object onto which to store the result.
  3330. * @returns {Matrix3} The modified result parameter.
  3331. *
  3332. * @example
  3333. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromUniformScale(scale), m);
  3334. * Cesium.Matrix3.multiplyByUniformScale(m, scale, m);
  3335. *
  3336. * @see Matrix3.multiplyByScale
  3337. * @see Matrix3.fromScale
  3338. * @see Matrix3.fromUniformScale
  3339. * @see Matrix3.setScale
  3340. * @see Matrix3.setUniformScale
  3341. * @see Matrix3.getScale
  3342. */
  3343. Matrix3.multiplyByUniformScale = function (matrix, scale, result) {
  3344. //>>includeStart('debug', pragmas.debug);
  3345. RuntimeError.Check.typeOf.object("matrix", matrix);
  3346. RuntimeError.Check.typeOf.number("scale", scale);
  3347. RuntimeError.Check.typeOf.object("result", result);
  3348. //>>includeEnd('debug');
  3349. result[0] = matrix[0] * scale;
  3350. result[1] = matrix[1] * scale;
  3351. result[2] = matrix[2] * scale;
  3352. result[3] = matrix[3] * scale;
  3353. result[4] = matrix[4] * scale;
  3354. result[5] = matrix[5] * scale;
  3355. result[6] = matrix[6] * scale;
  3356. result[7] = matrix[7] * scale;
  3357. result[8] = matrix[8] * scale;
  3358. return result;
  3359. };
  3360. /**
  3361. * Creates a negated copy of the provided matrix.
  3362. *
  3363. * @param {Matrix3} matrix The matrix to negate.
  3364. * @param {Matrix3} result The object onto which to store the result.
  3365. * @returns {Matrix3} The modified result parameter.
  3366. */
  3367. Matrix3.negate = function (matrix, result) {
  3368. //>>includeStart('debug', pragmas.debug);
  3369. RuntimeError.Check.typeOf.object("matrix", matrix);
  3370. RuntimeError.Check.typeOf.object("result", result);
  3371. //>>includeEnd('debug');
  3372. result[0] = -matrix[0];
  3373. result[1] = -matrix[1];
  3374. result[2] = -matrix[2];
  3375. result[3] = -matrix[3];
  3376. result[4] = -matrix[4];
  3377. result[5] = -matrix[5];
  3378. result[6] = -matrix[6];
  3379. result[7] = -matrix[7];
  3380. result[8] = -matrix[8];
  3381. return result;
  3382. };
  3383. /**
  3384. * Computes the transpose of the provided matrix.
  3385. *
  3386. * @param {Matrix3} matrix The matrix to transpose.
  3387. * @param {Matrix3} result The object onto which to store the result.
  3388. * @returns {Matrix3} The modified result parameter.
  3389. */
  3390. Matrix3.transpose = function (matrix, result) {
  3391. //>>includeStart('debug', pragmas.debug);
  3392. RuntimeError.Check.typeOf.object("matrix", matrix);
  3393. RuntimeError.Check.typeOf.object("result", result);
  3394. //>>includeEnd('debug');
  3395. const column0Row0 = matrix[0];
  3396. const column0Row1 = matrix[3];
  3397. const column0Row2 = matrix[6];
  3398. const column1Row0 = matrix[1];
  3399. const column1Row1 = matrix[4];
  3400. const column1Row2 = matrix[7];
  3401. const column2Row0 = matrix[2];
  3402. const column2Row1 = matrix[5];
  3403. const column2Row2 = matrix[8];
  3404. result[0] = column0Row0;
  3405. result[1] = column0Row1;
  3406. result[2] = column0Row2;
  3407. result[3] = column1Row0;
  3408. result[4] = column1Row1;
  3409. result[5] = column1Row2;
  3410. result[6] = column2Row0;
  3411. result[7] = column2Row1;
  3412. result[8] = column2Row2;
  3413. return result;
  3414. };
  3415. function computeFrobeniusNorm(matrix) {
  3416. let norm = 0.0;
  3417. for (let i = 0; i < 9; ++i) {
  3418. const temp = matrix[i];
  3419. norm += temp * temp;
  3420. }
  3421. return Math.sqrt(norm);
  3422. }
  3423. const rowVal = [1, 0, 0];
  3424. const colVal = [2, 2, 1];
  3425. function offDiagonalFrobeniusNorm(matrix) {
  3426. // Computes the "off-diagonal" Frobenius norm.
  3427. // Assumes matrix is symmetric.
  3428. let norm = 0.0;
  3429. for (let i = 0; i < 3; ++i) {
  3430. const temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  3431. norm += 2.0 * temp * temp;
  3432. }
  3433. return Math.sqrt(norm);
  3434. }
  3435. function shurDecomposition(matrix, result) {
  3436. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  3437. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  3438. //
  3439. // The routine takes a matrix, which is assumed to be symmetric, and
  3440. // finds the largest off-diagonal term, and then creates
  3441. // a matrix (result) which can be used to help reduce it
  3442. const tolerance = ComponentDatatype.CesiumMath.EPSILON15;
  3443. let maxDiagonal = 0.0;
  3444. let rotAxis = 1;
  3445. // find pivot (rotAxis) based on max diagonal of matrix
  3446. for (let i = 0; i < 3; ++i) {
  3447. const temp = Math.abs(
  3448. matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]
  3449. );
  3450. if (temp > maxDiagonal) {
  3451. rotAxis = i;
  3452. maxDiagonal = temp;
  3453. }
  3454. }
  3455. let c = 1.0;
  3456. let s = 0.0;
  3457. const p = rowVal[rotAxis];
  3458. const q = colVal[rotAxis];
  3459. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  3460. const qq = matrix[Matrix3.getElementIndex(q, q)];
  3461. const pp = matrix[Matrix3.getElementIndex(p, p)];
  3462. const qp = matrix[Matrix3.getElementIndex(q, p)];
  3463. const tau = (qq - pp) / 2.0 / qp;
  3464. let t;
  3465. if (tau < 0.0) {
  3466. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  3467. } else {
  3468. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  3469. }
  3470. c = 1.0 / Math.sqrt(1.0 + t * t);
  3471. s = t * c;
  3472. }
  3473. result = Matrix3.clone(Matrix3.IDENTITY, result);
  3474. result[Matrix3.getElementIndex(p, p)] = result[
  3475. Matrix3.getElementIndex(q, q)
  3476. ] = c;
  3477. result[Matrix3.getElementIndex(q, p)] = s;
  3478. result[Matrix3.getElementIndex(p, q)] = -s;
  3479. return result;
  3480. }
  3481. const jMatrix = new Matrix3();
  3482. const jMatrixTranspose = new Matrix3();
  3483. /**
  3484. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  3485. * <p>
  3486. * Returns a diagonal matrix and unitary matrix such that:
  3487. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  3488. * </p>
  3489. * <p>
  3490. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  3491. * of the unitary matrix are the corresponding eigenvectors.
  3492. * </p>
  3493. *
  3494. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  3495. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  3496. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  3497. *
  3498. * @example
  3499. * const a = //... symetric matrix
  3500. * const result = {
  3501. * unitary : new Cesium.Matrix3(),
  3502. * diagonal : new Cesium.Matrix3()
  3503. * };
  3504. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  3505. *
  3506. * const unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  3507. * const b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  3508. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  3509. *
  3510. * const lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  3511. * const v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  3512. * const c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  3513. */
  3514. Matrix3.computeEigenDecomposition = function (matrix, result) {
  3515. //>>includeStart('debug', pragmas.debug);
  3516. RuntimeError.Check.typeOf.object("matrix", matrix);
  3517. //>>includeEnd('debug');
  3518. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  3519. // section 8.4.3 The Classical Jacobi Algorithm
  3520. const tolerance = ComponentDatatype.CesiumMath.EPSILON20;
  3521. const maxSweeps = 10;
  3522. let count = 0;
  3523. let sweep = 0;
  3524. if (!defaultValue.defined(result)) {
  3525. result = {};
  3526. }
  3527. const unitaryMatrix = (result.unitary = Matrix3.clone(
  3528. Matrix3.IDENTITY,
  3529. result.unitary
  3530. ));
  3531. const diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  3532. const epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  3533. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  3534. shurDecomposition(diagMatrix, jMatrix);
  3535. Matrix3.transpose(jMatrix, jMatrixTranspose);
  3536. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  3537. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  3538. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  3539. if (++count > 2) {
  3540. ++sweep;
  3541. count = 0;
  3542. }
  3543. }
  3544. return result;
  3545. };
  3546. /**
  3547. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  3548. *
  3549. * @param {Matrix3} matrix The matrix with signed elements.
  3550. * @param {Matrix3} result The object onto which to store the result.
  3551. * @returns {Matrix3} The modified result parameter.
  3552. */
  3553. Matrix3.abs = function (matrix, result) {
  3554. //>>includeStart('debug', pragmas.debug);
  3555. RuntimeError.Check.typeOf.object("matrix", matrix);
  3556. RuntimeError.Check.typeOf.object("result", result);
  3557. //>>includeEnd('debug');
  3558. result[0] = Math.abs(matrix[0]);
  3559. result[1] = Math.abs(matrix[1]);
  3560. result[2] = Math.abs(matrix[2]);
  3561. result[3] = Math.abs(matrix[3]);
  3562. result[4] = Math.abs(matrix[4]);
  3563. result[5] = Math.abs(matrix[5]);
  3564. result[6] = Math.abs(matrix[6]);
  3565. result[7] = Math.abs(matrix[7]);
  3566. result[8] = Math.abs(matrix[8]);
  3567. return result;
  3568. };
  3569. /**
  3570. * Computes the determinant of the provided matrix.
  3571. *
  3572. * @param {Matrix3} matrix The matrix to use.
  3573. * @returns {Number} The value of the determinant of the matrix.
  3574. */
  3575. Matrix3.determinant = function (matrix) {
  3576. //>>includeStart('debug', pragmas.debug);
  3577. RuntimeError.Check.typeOf.object("matrix", matrix);
  3578. //>>includeEnd('debug');
  3579. const m11 = matrix[0];
  3580. const m21 = matrix[3];
  3581. const m31 = matrix[6];
  3582. const m12 = matrix[1];
  3583. const m22 = matrix[4];
  3584. const m32 = matrix[7];
  3585. const m13 = matrix[2];
  3586. const m23 = matrix[5];
  3587. const m33 = matrix[8];
  3588. return (
  3589. m11 * (m22 * m33 - m23 * m32) +
  3590. m12 * (m23 * m31 - m21 * m33) +
  3591. m13 * (m21 * m32 - m22 * m31)
  3592. );
  3593. };
  3594. /**
  3595. * Computes the inverse of the provided matrix.
  3596. *
  3597. * @param {Matrix3} matrix The matrix to invert.
  3598. * @param {Matrix3} result The object onto which to store the result.
  3599. * @returns {Matrix3} The modified result parameter.
  3600. *
  3601. * @exception {DeveloperError} matrix is not invertible.
  3602. */
  3603. Matrix3.inverse = function (matrix, result) {
  3604. //>>includeStart('debug', pragmas.debug);
  3605. RuntimeError.Check.typeOf.object("matrix", matrix);
  3606. RuntimeError.Check.typeOf.object("result", result);
  3607. //>>includeEnd('debug');
  3608. const m11 = matrix[0];
  3609. const m21 = matrix[1];
  3610. const m31 = matrix[2];
  3611. const m12 = matrix[3];
  3612. const m22 = matrix[4];
  3613. const m32 = matrix[5];
  3614. const m13 = matrix[6];
  3615. const m23 = matrix[7];
  3616. const m33 = matrix[8];
  3617. const determinant = Matrix3.determinant(matrix);
  3618. //>>includeStart('debug', pragmas.debug);
  3619. if (Math.abs(determinant) <= ComponentDatatype.CesiumMath.EPSILON15) {
  3620. throw new RuntimeError.DeveloperError("matrix is not invertible");
  3621. }
  3622. //>>includeEnd('debug');
  3623. result[0] = m22 * m33 - m23 * m32;
  3624. result[1] = m23 * m31 - m21 * m33;
  3625. result[2] = m21 * m32 - m22 * m31;
  3626. result[3] = m13 * m32 - m12 * m33;
  3627. result[4] = m11 * m33 - m13 * m31;
  3628. result[5] = m12 * m31 - m11 * m32;
  3629. result[6] = m12 * m23 - m13 * m22;
  3630. result[7] = m13 * m21 - m11 * m23;
  3631. result[8] = m11 * m22 - m12 * m21;
  3632. const scale = 1.0 / determinant;
  3633. return Matrix3.multiplyByScalar(result, scale, result);
  3634. };
  3635. const scratchTransposeMatrix$1 = new Matrix3();
  3636. /**
  3637. * Computes the inverse transpose of a matrix.
  3638. *
  3639. * @param {Matrix3} matrix The matrix to transpose and invert.
  3640. * @param {Matrix3} result The object onto which to store the result.
  3641. * @returns {Matrix3} The modified result parameter.
  3642. */
  3643. Matrix3.inverseTranspose = function (matrix, result) {
  3644. //>>includeStart('debug', pragmas.debug);
  3645. RuntimeError.Check.typeOf.object("matrix", matrix);
  3646. RuntimeError.Check.typeOf.object("result", result);
  3647. //>>includeEnd('debug');
  3648. return Matrix3.inverse(
  3649. Matrix3.transpose(matrix, scratchTransposeMatrix$1),
  3650. result
  3651. );
  3652. };
  3653. /**
  3654. * Compares the provided matrices componentwise and returns
  3655. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3656. *
  3657. * @param {Matrix3} [left] The first matrix.
  3658. * @param {Matrix3} [right] The second matrix.
  3659. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3660. */
  3661. Matrix3.equals = function (left, right) {
  3662. return (
  3663. left === right ||
  3664. (defaultValue.defined(left) &&
  3665. defaultValue.defined(right) &&
  3666. left[0] === right[0] &&
  3667. left[1] === right[1] &&
  3668. left[2] === right[2] &&
  3669. left[3] === right[3] &&
  3670. left[4] === right[4] &&
  3671. left[5] === right[5] &&
  3672. left[6] === right[6] &&
  3673. left[7] === right[7] &&
  3674. left[8] === right[8])
  3675. );
  3676. };
  3677. /**
  3678. * Compares the provided matrices componentwise and returns
  3679. * <code>true</code> if they are within the provided epsilon,
  3680. * <code>false</code> otherwise.
  3681. *
  3682. * @param {Matrix3} [left] The first matrix.
  3683. * @param {Matrix3} [right] The second matrix.
  3684. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  3685. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3686. */
  3687. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  3688. epsilon = defaultValue.defaultValue(epsilon, 0);
  3689. return (
  3690. left === right ||
  3691. (defaultValue.defined(left) &&
  3692. defaultValue.defined(right) &&
  3693. Math.abs(left[0] - right[0]) <= epsilon &&
  3694. Math.abs(left[1] - right[1]) <= epsilon &&
  3695. Math.abs(left[2] - right[2]) <= epsilon &&
  3696. Math.abs(left[3] - right[3]) <= epsilon &&
  3697. Math.abs(left[4] - right[4]) <= epsilon &&
  3698. Math.abs(left[5] - right[5]) <= epsilon &&
  3699. Math.abs(left[6] - right[6]) <= epsilon &&
  3700. Math.abs(left[7] - right[7]) <= epsilon &&
  3701. Math.abs(left[8] - right[8]) <= epsilon)
  3702. );
  3703. };
  3704. /**
  3705. * An immutable Matrix3 instance initialized to the identity matrix.
  3706. *
  3707. * @type {Matrix3}
  3708. * @constant
  3709. */
  3710. Matrix3.IDENTITY = Object.freeze(
  3711. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  3712. );
  3713. /**
  3714. * An immutable Matrix3 instance initialized to the zero matrix.
  3715. *
  3716. * @type {Matrix3}
  3717. * @constant
  3718. */
  3719. Matrix3.ZERO = Object.freeze(
  3720. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  3721. );
  3722. /**
  3723. * The index into Matrix3 for column 0, row 0.
  3724. *
  3725. * @type {Number}
  3726. * @constant
  3727. */
  3728. Matrix3.COLUMN0ROW0 = 0;
  3729. /**
  3730. * The index into Matrix3 for column 0, row 1.
  3731. *
  3732. * @type {Number}
  3733. * @constant
  3734. */
  3735. Matrix3.COLUMN0ROW1 = 1;
  3736. /**
  3737. * The index into Matrix3 for column 0, row 2.
  3738. *
  3739. * @type {Number}
  3740. * @constant
  3741. */
  3742. Matrix3.COLUMN0ROW2 = 2;
  3743. /**
  3744. * The index into Matrix3 for column 1, row 0.
  3745. *
  3746. * @type {Number}
  3747. * @constant
  3748. */
  3749. Matrix3.COLUMN1ROW0 = 3;
  3750. /**
  3751. * The index into Matrix3 for column 1, row 1.
  3752. *
  3753. * @type {Number}
  3754. * @constant
  3755. */
  3756. Matrix3.COLUMN1ROW1 = 4;
  3757. /**
  3758. * The index into Matrix3 for column 1, row 2.
  3759. *
  3760. * @type {Number}
  3761. * @constant
  3762. */
  3763. Matrix3.COLUMN1ROW2 = 5;
  3764. /**
  3765. * The index into Matrix3 for column 2, row 0.
  3766. *
  3767. * @type {Number}
  3768. * @constant
  3769. */
  3770. Matrix3.COLUMN2ROW0 = 6;
  3771. /**
  3772. * The index into Matrix3 for column 2, row 1.
  3773. *
  3774. * @type {Number}
  3775. * @constant
  3776. */
  3777. Matrix3.COLUMN2ROW1 = 7;
  3778. /**
  3779. * The index into Matrix3 for column 2, row 2.
  3780. *
  3781. * @type {Number}
  3782. * @constant
  3783. */
  3784. Matrix3.COLUMN2ROW2 = 8;
  3785. Object.defineProperties(Matrix3.prototype, {
  3786. /**
  3787. * Gets the number of items in the collection.
  3788. * @memberof Matrix3.prototype
  3789. *
  3790. * @type {Number}
  3791. */
  3792. length: {
  3793. get: function () {
  3794. return Matrix3.packedLength;
  3795. },
  3796. },
  3797. });
  3798. /**
  3799. * Duplicates the provided Matrix3 instance.
  3800. *
  3801. * @param {Matrix3} [result] The object onto which to store the result.
  3802. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  3803. */
  3804. Matrix3.prototype.clone = function (result) {
  3805. return Matrix3.clone(this, result);
  3806. };
  3807. /**
  3808. * Compares this matrix to the provided matrix componentwise and returns
  3809. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3810. *
  3811. * @param {Matrix3} [right] The right hand side matrix.
  3812. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3813. */
  3814. Matrix3.prototype.equals = function (right) {
  3815. return Matrix3.equals(this, right);
  3816. };
  3817. /**
  3818. * @private
  3819. */
  3820. Matrix3.equalsArray = function (matrix, array, offset) {
  3821. return (
  3822. matrix[0] === array[offset] &&
  3823. matrix[1] === array[offset + 1] &&
  3824. matrix[2] === array[offset + 2] &&
  3825. matrix[3] === array[offset + 3] &&
  3826. matrix[4] === array[offset + 4] &&
  3827. matrix[5] === array[offset + 5] &&
  3828. matrix[6] === array[offset + 6] &&
  3829. matrix[7] === array[offset + 7] &&
  3830. matrix[8] === array[offset + 8]
  3831. );
  3832. };
  3833. /**
  3834. * Compares this matrix to the provided matrix componentwise and returns
  3835. * <code>true</code> if they are within the provided epsilon,
  3836. * <code>false</code> otherwise.
  3837. *
  3838. * @param {Matrix3} [right] The right hand side matrix.
  3839. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  3840. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  3841. */
  3842. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  3843. return Matrix3.equalsEpsilon(this, right, epsilon);
  3844. };
  3845. /**
  3846. * Creates a string representing this Matrix with each row being
  3847. * on a separate line and in the format '(column0, column1, column2)'.
  3848. *
  3849. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  3850. */
  3851. Matrix3.prototype.toString = function () {
  3852. return (
  3853. `(${this[0]}, ${this[3]}, ${this[6]})\n` +
  3854. `(${this[1]}, ${this[4]}, ${this[7]})\n` +
  3855. `(${this[2]}, ${this[5]}, ${this[8]})`
  3856. );
  3857. };
  3858. /**
  3859. * A 4D Cartesian point.
  3860. * @alias Cartesian4
  3861. * @constructor
  3862. *
  3863. * @param {Number} [x=0.0] The X component.
  3864. * @param {Number} [y=0.0] The Y component.
  3865. * @param {Number} [z=0.0] The Z component.
  3866. * @param {Number} [w=0.0] The W component.
  3867. *
  3868. * @see Cartesian2
  3869. * @see Cartesian3
  3870. * @see Packable
  3871. */
  3872. function Cartesian4(x, y, z, w) {
  3873. /**
  3874. * The X component.
  3875. * @type {Number}
  3876. * @default 0.0
  3877. */
  3878. this.x = defaultValue.defaultValue(x, 0.0);
  3879. /**
  3880. * The Y component.
  3881. * @type {Number}
  3882. * @default 0.0
  3883. */
  3884. this.y = defaultValue.defaultValue(y, 0.0);
  3885. /**
  3886. * The Z component.
  3887. * @type {Number}
  3888. * @default 0.0
  3889. */
  3890. this.z = defaultValue.defaultValue(z, 0.0);
  3891. /**
  3892. * The W component.
  3893. * @type {Number}
  3894. * @default 0.0
  3895. */
  3896. this.w = defaultValue.defaultValue(w, 0.0);
  3897. }
  3898. /**
  3899. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  3900. *
  3901. * @param {Number} x The x coordinate.
  3902. * @param {Number} y The y coordinate.
  3903. * @param {Number} z The z coordinate.
  3904. * @param {Number} w The w coordinate.
  3905. * @param {Cartesian4} [result] The object onto which to store the result.
  3906. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3907. */
  3908. Cartesian4.fromElements = function (x, y, z, w, result) {
  3909. if (!defaultValue.defined(result)) {
  3910. return new Cartesian4(x, y, z, w);
  3911. }
  3912. result.x = x;
  3913. result.y = y;
  3914. result.z = z;
  3915. result.w = w;
  3916. return result;
  3917. };
  3918. /**
  3919. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  3920. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  3921. *
  3922. * @param {Color} color The source color.
  3923. * @param {Cartesian4} [result] The object onto which to store the result.
  3924. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3925. */
  3926. Cartesian4.fromColor = function (color, result) {
  3927. //>>includeStart('debug', pragmas.debug);
  3928. RuntimeError.Check.typeOf.object("color", color);
  3929. //>>includeEnd('debug');
  3930. if (!defaultValue.defined(result)) {
  3931. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  3932. }
  3933. result.x = color.red;
  3934. result.y = color.green;
  3935. result.z = color.blue;
  3936. result.w = color.alpha;
  3937. return result;
  3938. };
  3939. /**
  3940. * Duplicates a Cartesian4 instance.
  3941. *
  3942. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  3943. * @param {Cartesian4} [result] The object onto which to store the result.
  3944. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  3945. */
  3946. Cartesian4.clone = function (cartesian, result) {
  3947. if (!defaultValue.defined(cartesian)) {
  3948. return undefined;
  3949. }
  3950. if (!defaultValue.defined(result)) {
  3951. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  3952. }
  3953. result.x = cartesian.x;
  3954. result.y = cartesian.y;
  3955. result.z = cartesian.z;
  3956. result.w = cartesian.w;
  3957. return result;
  3958. };
  3959. /**
  3960. * The number of elements used to pack the object into an array.
  3961. * @type {Number}
  3962. */
  3963. Cartesian4.packedLength = 4;
  3964. /**
  3965. * Stores the provided instance into the provided array.
  3966. *
  3967. * @param {Cartesian4} value The value to pack.
  3968. * @param {Number[]} array The array to pack into.
  3969. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3970. *
  3971. * @returns {Number[]} The array that was packed into
  3972. */
  3973. Cartesian4.pack = function (value, array, startingIndex) {
  3974. //>>includeStart('debug', pragmas.debug);
  3975. RuntimeError.Check.typeOf.object("value", value);
  3976. RuntimeError.Check.defined("array", array);
  3977. //>>includeEnd('debug');
  3978. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3979. array[startingIndex++] = value.x;
  3980. array[startingIndex++] = value.y;
  3981. array[startingIndex++] = value.z;
  3982. array[startingIndex] = value.w;
  3983. return array;
  3984. };
  3985. /**
  3986. * Retrieves an instance from a packed array.
  3987. *
  3988. * @param {Number[]} array The packed array.
  3989. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3990. * @param {Cartesian4} [result] The object into which to store the result.
  3991. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  3992. */
  3993. Cartesian4.unpack = function (array, startingIndex, result) {
  3994. //>>includeStart('debug', pragmas.debug);
  3995. RuntimeError.Check.defined("array", array);
  3996. //>>includeEnd('debug');
  3997. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  3998. if (!defaultValue.defined(result)) {
  3999. result = new Cartesian4();
  4000. }
  4001. result.x = array[startingIndex++];
  4002. result.y = array[startingIndex++];
  4003. result.z = array[startingIndex++];
  4004. result.w = array[startingIndex];
  4005. return result;
  4006. };
  4007. /**
  4008. * Flattens an array of Cartesian4s into an array of components.
  4009. *
  4010. * @param {Cartesian4[]} array The array of cartesians to pack.
  4011. * @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.
  4012. * @returns {Number[]} The packed array.
  4013. */
  4014. Cartesian4.packArray = function (array, result) {
  4015. //>>includeStart('debug', pragmas.debug);
  4016. RuntimeError.Check.defined("array", array);
  4017. //>>includeEnd('debug');
  4018. const length = array.length;
  4019. const resultLength = length * 4;
  4020. if (!defaultValue.defined(result)) {
  4021. result = new Array(resultLength);
  4022. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4023. //>>includeStart('debug', pragmas.debug);
  4024. throw new RuntimeError.DeveloperError(
  4025. "If result is a typed array, it must have exactly array.length * 4 elements"
  4026. );
  4027. //>>includeEnd('debug');
  4028. } else if (result.length !== resultLength) {
  4029. result.length = resultLength;
  4030. }
  4031. for (let i = 0; i < length; ++i) {
  4032. Cartesian4.pack(array[i], result, i * 4);
  4033. }
  4034. return result;
  4035. };
  4036. /**
  4037. * Unpacks an array of cartesian components into an array of Cartesian4s.
  4038. *
  4039. * @param {Number[]} array The array of components to unpack.
  4040. * @param {Cartesian4[]} [result] The array onto which to store the result.
  4041. * @returns {Cartesian4[]} The unpacked array.
  4042. */
  4043. Cartesian4.unpackArray = function (array, result) {
  4044. //>>includeStart('debug', pragmas.debug);
  4045. RuntimeError.Check.defined("array", array);
  4046. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  4047. if (array.length % 4 !== 0) {
  4048. throw new RuntimeError.DeveloperError("array length must be a multiple of 4.");
  4049. }
  4050. //>>includeEnd('debug');
  4051. const length = array.length;
  4052. if (!defaultValue.defined(result)) {
  4053. result = new Array(length / 4);
  4054. } else {
  4055. result.length = length / 4;
  4056. }
  4057. for (let i = 0; i < length; i += 4) {
  4058. const index = i / 4;
  4059. result[index] = Cartesian4.unpack(array, i, result[index]);
  4060. }
  4061. return result;
  4062. };
  4063. /**
  4064. * Creates a Cartesian4 from four consecutive elements in an array.
  4065. * @function
  4066. *
  4067. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  4068. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  4069. * @param {Cartesian4} [result] The object onto which to store the result.
  4070. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4071. *
  4072. * @example
  4073. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  4074. * const v = [1.0, 2.0, 3.0, 4.0];
  4075. * const p = Cesium.Cartesian4.fromArray(v);
  4076. *
  4077. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  4078. * const v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  4079. * const p2 = Cesium.Cartesian4.fromArray(v2, 2);
  4080. */
  4081. Cartesian4.fromArray = Cartesian4.unpack;
  4082. /**
  4083. * Computes the value of the maximum component for the supplied Cartesian.
  4084. *
  4085. * @param {Cartesian4} cartesian The cartesian to use.
  4086. * @returns {Number} The value of the maximum component.
  4087. */
  4088. Cartesian4.maximumComponent = function (cartesian) {
  4089. //>>includeStart('debug', pragmas.debug);
  4090. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4091. //>>includeEnd('debug');
  4092. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4093. };
  4094. /**
  4095. * Computes the value of the minimum component for the supplied Cartesian.
  4096. *
  4097. * @param {Cartesian4} cartesian The cartesian to use.
  4098. * @returns {Number} The value of the minimum component.
  4099. */
  4100. Cartesian4.minimumComponent = function (cartesian) {
  4101. //>>includeStart('debug', pragmas.debug);
  4102. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4103. //>>includeEnd('debug');
  4104. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4105. };
  4106. /**
  4107. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  4108. *
  4109. * @param {Cartesian4} first A cartesian to compare.
  4110. * @param {Cartesian4} second A cartesian to compare.
  4111. * @param {Cartesian4} result The object into which to store the result.
  4112. * @returns {Cartesian4} A cartesian with the minimum components.
  4113. */
  4114. Cartesian4.minimumByComponent = function (first, second, result) {
  4115. //>>includeStart('debug', pragmas.debug);
  4116. RuntimeError.Check.typeOf.object("first", first);
  4117. RuntimeError.Check.typeOf.object("second", second);
  4118. RuntimeError.Check.typeOf.object("result", result);
  4119. //>>includeEnd('debug');
  4120. result.x = Math.min(first.x, second.x);
  4121. result.y = Math.min(first.y, second.y);
  4122. result.z = Math.min(first.z, second.z);
  4123. result.w = Math.min(first.w, second.w);
  4124. return result;
  4125. };
  4126. /**
  4127. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  4128. *
  4129. * @param {Cartesian4} first A cartesian to compare.
  4130. * @param {Cartesian4} second A cartesian to compare.
  4131. * @param {Cartesian4} result The object into which to store the result.
  4132. * @returns {Cartesian4} A cartesian with the maximum components.
  4133. */
  4134. Cartesian4.maximumByComponent = function (first, second, result) {
  4135. //>>includeStart('debug', pragmas.debug);
  4136. RuntimeError.Check.typeOf.object("first", first);
  4137. RuntimeError.Check.typeOf.object("second", second);
  4138. RuntimeError.Check.typeOf.object("result", result);
  4139. //>>includeEnd('debug');
  4140. result.x = Math.max(first.x, second.x);
  4141. result.y = Math.max(first.y, second.y);
  4142. result.z = Math.max(first.z, second.z);
  4143. result.w = Math.max(first.w, second.w);
  4144. return result;
  4145. };
  4146. /**
  4147. * Constrain a value to lie between two values.
  4148. *
  4149. * @param {Cartesian4} value The value to clamp.
  4150. * @param {Cartesian4} min The minimum bound.
  4151. * @param {Cartesian4} max The maximum bound.
  4152. * @param {Cartesian4} result The object into which to store the result.
  4153. * @returns {Cartesian4} The clamped value such that min <= result <= max.
  4154. */
  4155. Cartesian4.clamp = function (value, min, max, result) {
  4156. //>>includeStart('debug', pragmas.debug);
  4157. RuntimeError.Check.typeOf.object("value", value);
  4158. RuntimeError.Check.typeOf.object("min", min);
  4159. RuntimeError.Check.typeOf.object("max", max);
  4160. RuntimeError.Check.typeOf.object("result", result);
  4161. //>>includeEnd('debug');
  4162. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  4163. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  4164. const z = ComponentDatatype.CesiumMath.clamp(value.z, min.z, max.z);
  4165. const w = ComponentDatatype.CesiumMath.clamp(value.w, min.w, max.w);
  4166. result.x = x;
  4167. result.y = y;
  4168. result.z = z;
  4169. result.w = w;
  4170. return result;
  4171. };
  4172. /**
  4173. * Computes the provided Cartesian's squared magnitude.
  4174. *
  4175. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  4176. * @returns {Number} The squared magnitude.
  4177. */
  4178. Cartesian4.magnitudeSquared = function (cartesian) {
  4179. //>>includeStart('debug', pragmas.debug);
  4180. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4181. //>>includeEnd('debug');
  4182. return (
  4183. cartesian.x * cartesian.x +
  4184. cartesian.y * cartesian.y +
  4185. cartesian.z * cartesian.z +
  4186. cartesian.w * cartesian.w
  4187. );
  4188. };
  4189. /**
  4190. * Computes the Cartesian's magnitude (length).
  4191. *
  4192. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  4193. * @returns {Number} The magnitude.
  4194. */
  4195. Cartesian4.magnitude = function (cartesian) {
  4196. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  4197. };
  4198. const distanceScratch$1 = new Cartesian4();
  4199. /**
  4200. * Computes the 4-space distance between two points.
  4201. *
  4202. * @param {Cartesian4} left The first point to compute the distance from.
  4203. * @param {Cartesian4} right The second point to compute the distance to.
  4204. * @returns {Number} The distance between two points.
  4205. *
  4206. * @example
  4207. * // Returns 1.0
  4208. * const d = Cesium.Cartesian4.distance(
  4209. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4210. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  4211. */
  4212. Cartesian4.distance = function (left, right) {
  4213. //>>includeStart('debug', pragmas.debug);
  4214. RuntimeError.Check.typeOf.object("left", left);
  4215. RuntimeError.Check.typeOf.object("right", right);
  4216. //>>includeEnd('debug');
  4217. Cartesian4.subtract(left, right, distanceScratch$1);
  4218. return Cartesian4.magnitude(distanceScratch$1);
  4219. };
  4220. /**
  4221. * Computes the squared distance between two points. Comparing squared distances
  4222. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  4223. *
  4224. * @param {Cartesian4} left The first point to compute the distance from.
  4225. * @param {Cartesian4} right The second point to compute the distance to.
  4226. * @returns {Number} The distance between two points.
  4227. *
  4228. * @example
  4229. * // Returns 4.0, not 2.0
  4230. * const d = Cesium.Cartesian4.distance(
  4231. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4232. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  4233. */
  4234. Cartesian4.distanceSquared = function (left, right) {
  4235. //>>includeStart('debug', pragmas.debug);
  4236. RuntimeError.Check.typeOf.object("left", left);
  4237. RuntimeError.Check.typeOf.object("right", right);
  4238. //>>includeEnd('debug');
  4239. Cartesian4.subtract(left, right, distanceScratch$1);
  4240. return Cartesian4.magnitudeSquared(distanceScratch$1);
  4241. };
  4242. /**
  4243. * Computes the normalized form of the supplied Cartesian.
  4244. *
  4245. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  4246. * @param {Cartesian4} result The object onto which to store the result.
  4247. * @returns {Cartesian4} The modified result parameter.
  4248. */
  4249. Cartesian4.normalize = function (cartesian, result) {
  4250. //>>includeStart('debug', pragmas.debug);
  4251. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4252. RuntimeError.Check.typeOf.object("result", result);
  4253. //>>includeEnd('debug');
  4254. const magnitude = Cartesian4.magnitude(cartesian);
  4255. result.x = cartesian.x / magnitude;
  4256. result.y = cartesian.y / magnitude;
  4257. result.z = cartesian.z / magnitude;
  4258. result.w = cartesian.w / magnitude;
  4259. //>>includeStart('debug', pragmas.debug);
  4260. if (
  4261. isNaN(result.x) ||
  4262. isNaN(result.y) ||
  4263. isNaN(result.z) ||
  4264. isNaN(result.w)
  4265. ) {
  4266. throw new RuntimeError.DeveloperError("normalized result is not a number");
  4267. }
  4268. //>>includeEnd('debug');
  4269. return result;
  4270. };
  4271. /**
  4272. * Computes the dot (scalar) product of two Cartesians.
  4273. *
  4274. * @param {Cartesian4} left The first Cartesian.
  4275. * @param {Cartesian4} right The second Cartesian.
  4276. * @returns {Number} The dot product.
  4277. */
  4278. Cartesian4.dot = function (left, right) {
  4279. //>>includeStart('debug', pragmas.debug);
  4280. RuntimeError.Check.typeOf.object("left", left);
  4281. RuntimeError.Check.typeOf.object("right", right);
  4282. //>>includeEnd('debug');
  4283. return (
  4284. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  4285. );
  4286. };
  4287. /**
  4288. * Computes the componentwise product of two Cartesians.
  4289. *
  4290. * @param {Cartesian4} left The first Cartesian.
  4291. * @param {Cartesian4} right The second Cartesian.
  4292. * @param {Cartesian4} result The object onto which to store the result.
  4293. * @returns {Cartesian4} The modified result parameter.
  4294. */
  4295. Cartesian4.multiplyComponents = function (left, right, result) {
  4296. //>>includeStart('debug', pragmas.debug);
  4297. RuntimeError.Check.typeOf.object("left", left);
  4298. RuntimeError.Check.typeOf.object("right", right);
  4299. RuntimeError.Check.typeOf.object("result", result);
  4300. //>>includeEnd('debug');
  4301. result.x = left.x * right.x;
  4302. result.y = left.y * right.y;
  4303. result.z = left.z * right.z;
  4304. result.w = left.w * right.w;
  4305. return result;
  4306. };
  4307. /**
  4308. * Computes the componentwise quotient of two Cartesians.
  4309. *
  4310. * @param {Cartesian4} left The first Cartesian.
  4311. * @param {Cartesian4} right The second Cartesian.
  4312. * @param {Cartesian4} result The object onto which to store the result.
  4313. * @returns {Cartesian4} The modified result parameter.
  4314. */
  4315. Cartesian4.divideComponents = function (left, right, result) {
  4316. //>>includeStart('debug', pragmas.debug);
  4317. RuntimeError.Check.typeOf.object("left", left);
  4318. RuntimeError.Check.typeOf.object("right", right);
  4319. RuntimeError.Check.typeOf.object("result", result);
  4320. //>>includeEnd('debug');
  4321. result.x = left.x / right.x;
  4322. result.y = left.y / right.y;
  4323. result.z = left.z / right.z;
  4324. result.w = left.w / right.w;
  4325. return result;
  4326. };
  4327. /**
  4328. * Computes the componentwise sum of two Cartesians.
  4329. *
  4330. * @param {Cartesian4} left The first Cartesian.
  4331. * @param {Cartesian4} right The second Cartesian.
  4332. * @param {Cartesian4} result The object onto which to store the result.
  4333. * @returns {Cartesian4} The modified result parameter.
  4334. */
  4335. Cartesian4.add = function (left, right, result) {
  4336. //>>includeStart('debug', pragmas.debug);
  4337. RuntimeError.Check.typeOf.object("left", left);
  4338. RuntimeError.Check.typeOf.object("right", right);
  4339. RuntimeError.Check.typeOf.object("result", result);
  4340. //>>includeEnd('debug');
  4341. result.x = left.x + right.x;
  4342. result.y = left.y + right.y;
  4343. result.z = left.z + right.z;
  4344. result.w = left.w + right.w;
  4345. return result;
  4346. };
  4347. /**
  4348. * Computes the componentwise difference of two Cartesians.
  4349. *
  4350. * @param {Cartesian4} left The first Cartesian.
  4351. * @param {Cartesian4} right The second Cartesian.
  4352. * @param {Cartesian4} result The object onto which to store the result.
  4353. * @returns {Cartesian4} The modified result parameter.
  4354. */
  4355. Cartesian4.subtract = function (left, right, result) {
  4356. //>>includeStart('debug', pragmas.debug);
  4357. RuntimeError.Check.typeOf.object("left", left);
  4358. RuntimeError.Check.typeOf.object("right", right);
  4359. RuntimeError.Check.typeOf.object("result", result);
  4360. //>>includeEnd('debug');
  4361. result.x = left.x - right.x;
  4362. result.y = left.y - right.y;
  4363. result.z = left.z - right.z;
  4364. result.w = left.w - right.w;
  4365. return result;
  4366. };
  4367. /**
  4368. * Multiplies the provided Cartesian componentwise by the provided scalar.
  4369. *
  4370. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  4371. * @param {Number} scalar The scalar to multiply with.
  4372. * @param {Cartesian4} result The object onto which to store the result.
  4373. * @returns {Cartesian4} The modified result parameter.
  4374. */
  4375. Cartesian4.multiplyByScalar = function (cartesian, scalar, result) {
  4376. //>>includeStart('debug', pragmas.debug);
  4377. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4378. RuntimeError.Check.typeOf.number("scalar", scalar);
  4379. RuntimeError.Check.typeOf.object("result", result);
  4380. //>>includeEnd('debug');
  4381. result.x = cartesian.x * scalar;
  4382. result.y = cartesian.y * scalar;
  4383. result.z = cartesian.z * scalar;
  4384. result.w = cartesian.w * scalar;
  4385. return result;
  4386. };
  4387. /**
  4388. * Divides the provided Cartesian componentwise by the provided scalar.
  4389. *
  4390. * @param {Cartesian4} cartesian The Cartesian to be divided.
  4391. * @param {Number} scalar The scalar to divide by.
  4392. * @param {Cartesian4} result The object onto which to store the result.
  4393. * @returns {Cartesian4} The modified result parameter.
  4394. */
  4395. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  4396. //>>includeStart('debug', pragmas.debug);
  4397. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4398. RuntimeError.Check.typeOf.number("scalar", scalar);
  4399. RuntimeError.Check.typeOf.object("result", result);
  4400. //>>includeEnd('debug');
  4401. result.x = cartesian.x / scalar;
  4402. result.y = cartesian.y / scalar;
  4403. result.z = cartesian.z / scalar;
  4404. result.w = cartesian.w / scalar;
  4405. return result;
  4406. };
  4407. /**
  4408. * Negates the provided Cartesian.
  4409. *
  4410. * @param {Cartesian4} cartesian The Cartesian to be negated.
  4411. * @param {Cartesian4} result The object onto which to store the result.
  4412. * @returns {Cartesian4} The modified result parameter.
  4413. */
  4414. Cartesian4.negate = function (cartesian, result) {
  4415. //>>includeStart('debug', pragmas.debug);
  4416. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4417. RuntimeError.Check.typeOf.object("result", result);
  4418. //>>includeEnd('debug');
  4419. result.x = -cartesian.x;
  4420. result.y = -cartesian.y;
  4421. result.z = -cartesian.z;
  4422. result.w = -cartesian.w;
  4423. return result;
  4424. };
  4425. /**
  4426. * Computes the absolute value of the provided Cartesian.
  4427. *
  4428. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  4429. * @param {Cartesian4} result The object onto which to store the result.
  4430. * @returns {Cartesian4} The modified result parameter.
  4431. */
  4432. Cartesian4.abs = function (cartesian, result) {
  4433. //>>includeStart('debug', pragmas.debug);
  4434. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4435. RuntimeError.Check.typeOf.object("result", result);
  4436. //>>includeEnd('debug');
  4437. result.x = Math.abs(cartesian.x);
  4438. result.y = Math.abs(cartesian.y);
  4439. result.z = Math.abs(cartesian.z);
  4440. result.w = Math.abs(cartesian.w);
  4441. return result;
  4442. };
  4443. const lerpScratch$1 = new Cartesian4();
  4444. /**
  4445. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  4446. *
  4447. * @param {Cartesian4} start The value corresponding to t at 0.0.
  4448. * @param {Cartesian4}end The value corresponding to t at 1.0.
  4449. * @param {Number} t The point along t at which to interpolate.
  4450. * @param {Cartesian4} result The object onto which to store the result.
  4451. * @returns {Cartesian4} The modified result parameter.
  4452. */
  4453. Cartesian4.lerp = function (start, end, t, result) {
  4454. //>>includeStart('debug', pragmas.debug);
  4455. RuntimeError.Check.typeOf.object("start", start);
  4456. RuntimeError.Check.typeOf.object("end", end);
  4457. RuntimeError.Check.typeOf.number("t", t);
  4458. RuntimeError.Check.typeOf.object("result", result);
  4459. //>>includeEnd('debug');
  4460. Cartesian4.multiplyByScalar(end, t, lerpScratch$1);
  4461. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  4462. return Cartesian4.add(lerpScratch$1, result, result);
  4463. };
  4464. const mostOrthogonalAxisScratch$1 = new Cartesian4();
  4465. /**
  4466. * Returns the axis that is most orthogonal to the provided Cartesian.
  4467. *
  4468. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  4469. * @param {Cartesian4} result The object onto which to store the result.
  4470. * @returns {Cartesian4} The most orthogonal axis.
  4471. */
  4472. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  4473. //>>includeStart('debug', pragmas.debug);
  4474. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  4475. RuntimeError.Check.typeOf.object("result", result);
  4476. //>>includeEnd('debug');
  4477. const f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch$1);
  4478. Cartesian4.abs(f, f);
  4479. if (f.x <= f.y) {
  4480. if (f.x <= f.z) {
  4481. if (f.x <= f.w) {
  4482. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  4483. } else {
  4484. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4485. }
  4486. } else if (f.z <= f.w) {
  4487. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  4488. } else {
  4489. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4490. }
  4491. } else if (f.y <= f.z) {
  4492. if (f.y <= f.w) {
  4493. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  4494. } else {
  4495. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4496. }
  4497. } else if (f.z <= f.w) {
  4498. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  4499. } else {
  4500. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  4501. }
  4502. return result;
  4503. };
  4504. /**
  4505. * Compares the provided Cartesians componentwise and returns
  4506. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4507. *
  4508. * @param {Cartesian4} [left] The first Cartesian.
  4509. * @param {Cartesian4} [right] The second Cartesian.
  4510. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  4511. */
  4512. Cartesian4.equals = function (left, right) {
  4513. return (
  4514. left === right ||
  4515. (defaultValue.defined(left) &&
  4516. defaultValue.defined(right) &&
  4517. left.x === right.x &&
  4518. left.y === right.y &&
  4519. left.z === right.z &&
  4520. left.w === right.w)
  4521. );
  4522. };
  4523. /**
  4524. * @private
  4525. */
  4526. Cartesian4.equalsArray = function (cartesian, array, offset) {
  4527. return (
  4528. cartesian.x === array[offset] &&
  4529. cartesian.y === array[offset + 1] &&
  4530. cartesian.z === array[offset + 2] &&
  4531. cartesian.w === array[offset + 3]
  4532. );
  4533. };
  4534. /**
  4535. * Compares the provided Cartesians componentwise and returns
  4536. * <code>true</code> if they pass an absolute or relative tolerance test,
  4537. * <code>false</code> otherwise.
  4538. *
  4539. * @param {Cartesian4} [left] The first Cartesian.
  4540. * @param {Cartesian4} [right] The second Cartesian.
  4541. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  4542. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  4543. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  4544. */
  4545. Cartesian4.equalsEpsilon = function (
  4546. left,
  4547. right,
  4548. relativeEpsilon,
  4549. absoluteEpsilon
  4550. ) {
  4551. return (
  4552. left === right ||
  4553. (defaultValue.defined(left) &&
  4554. defaultValue.defined(right) &&
  4555. ComponentDatatype.CesiumMath.equalsEpsilon(
  4556. left.x,
  4557. right.x,
  4558. relativeEpsilon,
  4559. absoluteEpsilon
  4560. ) &&
  4561. ComponentDatatype.CesiumMath.equalsEpsilon(
  4562. left.y,
  4563. right.y,
  4564. relativeEpsilon,
  4565. absoluteEpsilon
  4566. ) &&
  4567. ComponentDatatype.CesiumMath.equalsEpsilon(
  4568. left.z,
  4569. right.z,
  4570. relativeEpsilon,
  4571. absoluteEpsilon
  4572. ) &&
  4573. ComponentDatatype.CesiumMath.equalsEpsilon(
  4574. left.w,
  4575. right.w,
  4576. relativeEpsilon,
  4577. absoluteEpsilon
  4578. ))
  4579. );
  4580. };
  4581. /**
  4582. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  4583. *
  4584. * @type {Cartesian4}
  4585. * @constant
  4586. */
  4587. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  4588. /**
  4589. * An immutable Cartesian4 instance initialized to (1.0, 1.0, 1.0, 1.0).
  4590. *
  4591. * @type {Cartesian4}
  4592. * @constant
  4593. */
  4594. Cartesian4.ONE = Object.freeze(new Cartesian4(1.0, 1.0, 1.0, 1.0));
  4595. /**
  4596. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  4597. *
  4598. * @type {Cartesian4}
  4599. * @constant
  4600. */
  4601. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  4602. /**
  4603. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  4604. *
  4605. * @type {Cartesian4}
  4606. * @constant
  4607. */
  4608. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  4609. /**
  4610. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  4611. *
  4612. * @type {Cartesian4}
  4613. * @constant
  4614. */
  4615. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  4616. /**
  4617. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  4618. *
  4619. * @type {Cartesian4}
  4620. * @constant
  4621. */
  4622. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  4623. /**
  4624. * Duplicates this Cartesian4 instance.
  4625. *
  4626. * @param {Cartesian4} [result] The object onto which to store the result.
  4627. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4628. */
  4629. Cartesian4.prototype.clone = function (result) {
  4630. return Cartesian4.clone(this, result);
  4631. };
  4632. /**
  4633. * Compares this Cartesian against the provided Cartesian componentwise and returns
  4634. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4635. *
  4636. * @param {Cartesian4} [right] The right hand side Cartesian.
  4637. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  4638. */
  4639. Cartesian4.prototype.equals = function (right) {
  4640. return Cartesian4.equals(this, right);
  4641. };
  4642. /**
  4643. * Compares this Cartesian against the provided Cartesian componentwise and returns
  4644. * <code>true</code> if they pass an absolute or relative tolerance test,
  4645. * <code>false</code> otherwise.
  4646. *
  4647. * @param {Cartesian4} [right] The right hand side Cartesian.
  4648. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  4649. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  4650. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  4651. */
  4652. Cartesian4.prototype.equalsEpsilon = function (
  4653. right,
  4654. relativeEpsilon,
  4655. absoluteEpsilon
  4656. ) {
  4657. return Cartesian4.equalsEpsilon(
  4658. this,
  4659. right,
  4660. relativeEpsilon,
  4661. absoluteEpsilon
  4662. );
  4663. };
  4664. /**
  4665. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  4666. *
  4667. * @returns {String} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  4668. */
  4669. Cartesian4.prototype.toString = function () {
  4670. return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
  4671. };
  4672. // scratchU8Array and scratchF32Array are views into the same buffer
  4673. const scratchF32Array = new Float32Array(1);
  4674. const scratchU8Array = new Uint8Array(scratchF32Array.buffer);
  4675. const testU32 = new Uint32Array([0x11223344]);
  4676. const testU8 = new Uint8Array(testU32.buffer);
  4677. const littleEndian = testU8[0] === 0x44;
  4678. /**
  4679. * Packs an arbitrary floating point value to 4 values representable using uint8.
  4680. *
  4681. * @param {Number} value A floating point number.
  4682. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  4683. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  4684. */
  4685. Cartesian4.packFloat = function (value, result) {
  4686. //>>includeStart('debug', pragmas.debug);
  4687. RuntimeError.Check.typeOf.number("value", value);
  4688. //>>includeEnd('debug');
  4689. if (!defaultValue.defined(result)) {
  4690. result = new Cartesian4();
  4691. }
  4692. // scratchU8Array and scratchF32Array are views into the same buffer
  4693. scratchF32Array[0] = value;
  4694. if (littleEndian) {
  4695. result.x = scratchU8Array[0];
  4696. result.y = scratchU8Array[1];
  4697. result.z = scratchU8Array[2];
  4698. result.w = scratchU8Array[3];
  4699. } else {
  4700. // convert from big-endian to little-endian
  4701. result.x = scratchU8Array[3];
  4702. result.y = scratchU8Array[2];
  4703. result.z = scratchU8Array[1];
  4704. result.w = scratchU8Array[0];
  4705. }
  4706. return result;
  4707. };
  4708. /**
  4709. * Unpacks a float packed using Cartesian4.packFloat.
  4710. *
  4711. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  4712. * @returns {Number} The unpacked float.
  4713. * @private
  4714. */
  4715. Cartesian4.unpackFloat = function (packedFloat) {
  4716. //>>includeStart('debug', pragmas.debug);
  4717. RuntimeError.Check.typeOf.object("packedFloat", packedFloat);
  4718. //>>includeEnd('debug');
  4719. // scratchU8Array and scratchF32Array are views into the same buffer
  4720. if (littleEndian) {
  4721. scratchU8Array[0] = packedFloat.x;
  4722. scratchU8Array[1] = packedFloat.y;
  4723. scratchU8Array[2] = packedFloat.z;
  4724. scratchU8Array[3] = packedFloat.w;
  4725. } else {
  4726. // convert from little-endian to big-endian
  4727. scratchU8Array[0] = packedFloat.w;
  4728. scratchU8Array[1] = packedFloat.z;
  4729. scratchU8Array[2] = packedFloat.y;
  4730. scratchU8Array[3] = packedFloat.x;
  4731. }
  4732. return scratchF32Array[0];
  4733. };
  4734. /**
  4735. * A 4x4 matrix, indexable as a column-major order array.
  4736. * Constructor parameters are in row-major order for code readability.
  4737. * @alias Matrix4
  4738. * @constructor
  4739. * @implements {ArrayLike<number>}
  4740. *
  4741. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  4742. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  4743. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  4744. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  4745. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  4746. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  4747. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  4748. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  4749. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  4750. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  4751. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  4752. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  4753. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  4754. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  4755. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  4756. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  4757. *
  4758. * @see Matrix4.fromArray
  4759. * @see Matrix4.fromColumnMajorArray
  4760. * @see Matrix4.fromRowMajorArray
  4761. * @see Matrix4.fromRotationTranslation
  4762. * @see Matrix4.fromTranslationQuaternionRotationScale
  4763. * @see Matrix4.fromTranslationRotationScale
  4764. * @see Matrix4.fromTranslation
  4765. * @see Matrix4.fromScale
  4766. * @see Matrix4.fromUniformScale
  4767. * @see Matrix4.fromRotation
  4768. * @see Matrix4.fromCamera
  4769. * @see Matrix4.computePerspectiveFieldOfView
  4770. * @see Matrix4.computeOrthographicOffCenter
  4771. * @see Matrix4.computePerspectiveOffCenter
  4772. * @see Matrix4.computeInfinitePerspectiveOffCenter
  4773. * @see Matrix4.computeViewportTransformation
  4774. * @see Matrix4.computeView
  4775. * @see Matrix2
  4776. * @see Matrix3
  4777. * @see Packable
  4778. */
  4779. function Matrix4(
  4780. column0Row0,
  4781. column1Row0,
  4782. column2Row0,
  4783. column3Row0,
  4784. column0Row1,
  4785. column1Row1,
  4786. column2Row1,
  4787. column3Row1,
  4788. column0Row2,
  4789. column1Row2,
  4790. column2Row2,
  4791. column3Row2,
  4792. column0Row3,
  4793. column1Row3,
  4794. column2Row3,
  4795. column3Row3
  4796. ) {
  4797. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  4798. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  4799. this[2] = defaultValue.defaultValue(column0Row2, 0.0);
  4800. this[3] = defaultValue.defaultValue(column0Row3, 0.0);
  4801. this[4] = defaultValue.defaultValue(column1Row0, 0.0);
  4802. this[5] = defaultValue.defaultValue(column1Row1, 0.0);
  4803. this[6] = defaultValue.defaultValue(column1Row2, 0.0);
  4804. this[7] = defaultValue.defaultValue(column1Row3, 0.0);
  4805. this[8] = defaultValue.defaultValue(column2Row0, 0.0);
  4806. this[9] = defaultValue.defaultValue(column2Row1, 0.0);
  4807. this[10] = defaultValue.defaultValue(column2Row2, 0.0);
  4808. this[11] = defaultValue.defaultValue(column2Row3, 0.0);
  4809. this[12] = defaultValue.defaultValue(column3Row0, 0.0);
  4810. this[13] = defaultValue.defaultValue(column3Row1, 0.0);
  4811. this[14] = defaultValue.defaultValue(column3Row2, 0.0);
  4812. this[15] = defaultValue.defaultValue(column3Row3, 0.0);
  4813. }
  4814. /**
  4815. * The number of elements used to pack the object into an array.
  4816. * @type {Number}
  4817. */
  4818. Matrix4.packedLength = 16;
  4819. /**
  4820. * Stores the provided instance into the provided array.
  4821. *
  4822. * @param {Matrix4} value The value to pack.
  4823. * @param {Number[]} array The array to pack into.
  4824. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4825. *
  4826. * @returns {Number[]} The array that was packed into
  4827. */
  4828. Matrix4.pack = function (value, array, startingIndex) {
  4829. //>>includeStart('debug', pragmas.debug);
  4830. RuntimeError.Check.typeOf.object("value", value);
  4831. RuntimeError.Check.defined("array", array);
  4832. //>>includeEnd('debug');
  4833. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4834. array[startingIndex++] = value[0];
  4835. array[startingIndex++] = value[1];
  4836. array[startingIndex++] = value[2];
  4837. array[startingIndex++] = value[3];
  4838. array[startingIndex++] = value[4];
  4839. array[startingIndex++] = value[5];
  4840. array[startingIndex++] = value[6];
  4841. array[startingIndex++] = value[7];
  4842. array[startingIndex++] = value[8];
  4843. array[startingIndex++] = value[9];
  4844. array[startingIndex++] = value[10];
  4845. array[startingIndex++] = value[11];
  4846. array[startingIndex++] = value[12];
  4847. array[startingIndex++] = value[13];
  4848. array[startingIndex++] = value[14];
  4849. array[startingIndex] = value[15];
  4850. return array;
  4851. };
  4852. /**
  4853. * Retrieves an instance from a packed array.
  4854. *
  4855. * @param {Number[]} array The packed array.
  4856. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4857. * @param {Matrix4} [result] The object into which to store the result.
  4858. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  4859. */
  4860. Matrix4.unpack = function (array, startingIndex, result) {
  4861. //>>includeStart('debug', pragmas.debug);
  4862. RuntimeError.Check.defined("array", array);
  4863. //>>includeEnd('debug');
  4864. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  4865. if (!defaultValue.defined(result)) {
  4866. result = new Matrix4();
  4867. }
  4868. result[0] = array[startingIndex++];
  4869. result[1] = array[startingIndex++];
  4870. result[2] = array[startingIndex++];
  4871. result[3] = array[startingIndex++];
  4872. result[4] = array[startingIndex++];
  4873. result[5] = array[startingIndex++];
  4874. result[6] = array[startingIndex++];
  4875. result[7] = array[startingIndex++];
  4876. result[8] = array[startingIndex++];
  4877. result[9] = array[startingIndex++];
  4878. result[10] = array[startingIndex++];
  4879. result[11] = array[startingIndex++];
  4880. result[12] = array[startingIndex++];
  4881. result[13] = array[startingIndex++];
  4882. result[14] = array[startingIndex++];
  4883. result[15] = array[startingIndex];
  4884. return result;
  4885. };
  4886. /**
  4887. * Flattens an array of Matrix4s into an array of components. The components
  4888. * are stored in column-major order.
  4889. *
  4890. * @param {Matrix4[]} array The array of matrices to pack.
  4891. * @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.
  4892. * @returns {Number[]} The packed array.
  4893. */
  4894. Matrix4.packArray = function (array, result) {
  4895. //>>includeStart('debug', pragmas.debug);
  4896. RuntimeError.Check.defined("array", array);
  4897. //>>includeEnd('debug');
  4898. const length = array.length;
  4899. const resultLength = length * 16;
  4900. if (!defaultValue.defined(result)) {
  4901. result = new Array(resultLength);
  4902. } else if (!Array.isArray(result) && result.length !== resultLength) {
  4903. //>>includeStart('debug', pragmas.debug);
  4904. throw new RuntimeError.DeveloperError(
  4905. "If result is a typed array, it must have exactly array.length * 16 elements"
  4906. );
  4907. //>>includeEnd('debug');
  4908. } else if (result.length !== resultLength) {
  4909. result.length = resultLength;
  4910. }
  4911. for (let i = 0; i < length; ++i) {
  4912. Matrix4.pack(array[i], result, i * 16);
  4913. }
  4914. return result;
  4915. };
  4916. /**
  4917. * Unpacks an array of column-major matrix components into an array of Matrix4s.
  4918. *
  4919. * @param {Number[]} array The array of components to unpack.
  4920. * @param {Matrix4[]} [result] The array onto which to store the result.
  4921. * @returns {Matrix4[]} The unpacked array.
  4922. */
  4923. Matrix4.unpackArray = function (array, result) {
  4924. //>>includeStart('debug', pragmas.debug);
  4925. RuntimeError.Check.defined("array", array);
  4926. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 16);
  4927. if (array.length % 16 !== 0) {
  4928. throw new RuntimeError.DeveloperError("array length must be a multiple of 16.");
  4929. }
  4930. //>>includeEnd('debug');
  4931. const length = array.length;
  4932. if (!defaultValue.defined(result)) {
  4933. result = new Array(length / 16);
  4934. } else {
  4935. result.length = length / 16;
  4936. }
  4937. for (let i = 0; i < length; i += 16) {
  4938. const index = i / 16;
  4939. result[index] = Matrix4.unpack(array, i, result[index]);
  4940. }
  4941. return result;
  4942. };
  4943. /**
  4944. * Duplicates a Matrix4 instance.
  4945. *
  4946. * @param {Matrix4} matrix The matrix to duplicate.
  4947. * @param {Matrix4} [result] The object onto which to store the result.
  4948. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  4949. */
  4950. Matrix4.clone = function (matrix, result) {
  4951. if (!defaultValue.defined(matrix)) {
  4952. return undefined;
  4953. }
  4954. if (!defaultValue.defined(result)) {
  4955. return new Matrix4(
  4956. matrix[0],
  4957. matrix[4],
  4958. matrix[8],
  4959. matrix[12],
  4960. matrix[1],
  4961. matrix[5],
  4962. matrix[9],
  4963. matrix[13],
  4964. matrix[2],
  4965. matrix[6],
  4966. matrix[10],
  4967. matrix[14],
  4968. matrix[3],
  4969. matrix[7],
  4970. matrix[11],
  4971. matrix[15]
  4972. );
  4973. }
  4974. result[0] = matrix[0];
  4975. result[1] = matrix[1];
  4976. result[2] = matrix[2];
  4977. result[3] = matrix[3];
  4978. result[4] = matrix[4];
  4979. result[5] = matrix[5];
  4980. result[6] = matrix[6];
  4981. result[7] = matrix[7];
  4982. result[8] = matrix[8];
  4983. result[9] = matrix[9];
  4984. result[10] = matrix[10];
  4985. result[11] = matrix[11];
  4986. result[12] = matrix[12];
  4987. result[13] = matrix[13];
  4988. result[14] = matrix[14];
  4989. result[15] = matrix[15];
  4990. return result;
  4991. };
  4992. /**
  4993. * Creates a Matrix4 from 16 consecutive elements in an array.
  4994. * @function
  4995. *
  4996. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  4997. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  4998. * @param {Matrix4} [result] The object onto which to store the result.
  4999. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  5000. *
  5001. * @example
  5002. * // Create the Matrix4:
  5003. * // [1.0, 2.0, 3.0, 4.0]
  5004. * // [1.0, 2.0, 3.0, 4.0]
  5005. * // [1.0, 2.0, 3.0, 4.0]
  5006. * // [1.0, 2.0, 3.0, 4.0]
  5007. *
  5008. * 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];
  5009. * const m = Cesium.Matrix4.fromArray(v);
  5010. *
  5011. * // Create same Matrix4 with using an offset into an array
  5012. * 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];
  5013. * const m2 = Cesium.Matrix4.fromArray(v2, 2);
  5014. */
  5015. Matrix4.fromArray = Matrix4.unpack;
  5016. /**
  5017. * Computes a Matrix4 instance from a column-major order array.
  5018. *
  5019. * @param {Number[]} values The column-major order array.
  5020. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5021. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5022. */
  5023. Matrix4.fromColumnMajorArray = function (values, result) {
  5024. //>>includeStart('debug', pragmas.debug);
  5025. RuntimeError.Check.defined("values", values);
  5026. //>>includeEnd('debug');
  5027. return Matrix4.clone(values, result);
  5028. };
  5029. /**
  5030. * Computes a Matrix4 instance from a row-major order array.
  5031. * The resulting matrix will be in column-major order.
  5032. *
  5033. * @param {Number[]} values The row-major order array.
  5034. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5035. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5036. */
  5037. Matrix4.fromRowMajorArray = function (values, result) {
  5038. //>>includeStart('debug', pragmas.debug);
  5039. RuntimeError.Check.defined("values", values);
  5040. //>>includeEnd('debug');
  5041. if (!defaultValue.defined(result)) {
  5042. return new Matrix4(
  5043. values[0],
  5044. values[1],
  5045. values[2],
  5046. values[3],
  5047. values[4],
  5048. values[5],
  5049. values[6],
  5050. values[7],
  5051. values[8],
  5052. values[9],
  5053. values[10],
  5054. values[11],
  5055. values[12],
  5056. values[13],
  5057. values[14],
  5058. values[15]
  5059. );
  5060. }
  5061. result[0] = values[0];
  5062. result[1] = values[4];
  5063. result[2] = values[8];
  5064. result[3] = values[12];
  5065. result[4] = values[1];
  5066. result[5] = values[5];
  5067. result[6] = values[9];
  5068. result[7] = values[13];
  5069. result[8] = values[2];
  5070. result[9] = values[6];
  5071. result[10] = values[10];
  5072. result[11] = values[14];
  5073. result[12] = values[3];
  5074. result[13] = values[7];
  5075. result[14] = values[11];
  5076. result[15] = values[15];
  5077. return result;
  5078. };
  5079. /**
  5080. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  5081. * and a Cartesian3 representing the translation.
  5082. *
  5083. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  5084. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  5085. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5086. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5087. */
  5088. Matrix4.fromRotationTranslation = function (rotation, translation, result) {
  5089. //>>includeStart('debug', pragmas.debug);
  5090. RuntimeError.Check.typeOf.object("rotation", rotation);
  5091. //>>includeEnd('debug');
  5092. translation = defaultValue.defaultValue(translation, Cartesian3.ZERO);
  5093. if (!defaultValue.defined(result)) {
  5094. return new Matrix4(
  5095. rotation[0],
  5096. rotation[3],
  5097. rotation[6],
  5098. translation.x,
  5099. rotation[1],
  5100. rotation[4],
  5101. rotation[7],
  5102. translation.y,
  5103. rotation[2],
  5104. rotation[5],
  5105. rotation[8],
  5106. translation.z,
  5107. 0.0,
  5108. 0.0,
  5109. 0.0,
  5110. 1.0
  5111. );
  5112. }
  5113. result[0] = rotation[0];
  5114. result[1] = rotation[1];
  5115. result[2] = rotation[2];
  5116. result[3] = 0.0;
  5117. result[4] = rotation[3];
  5118. result[5] = rotation[4];
  5119. result[6] = rotation[5];
  5120. result[7] = 0.0;
  5121. result[8] = rotation[6];
  5122. result[9] = rotation[7];
  5123. result[10] = rotation[8];
  5124. result[11] = 0.0;
  5125. result[12] = translation.x;
  5126. result[13] = translation.y;
  5127. result[14] = translation.z;
  5128. result[15] = 1.0;
  5129. return result;
  5130. };
  5131. /**
  5132. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  5133. * representation with the rotation represented as a quaternion.
  5134. *
  5135. * @param {Cartesian3} translation The translation transformation.
  5136. * @param {Quaternion} rotation The rotation transformation.
  5137. * @param {Cartesian3} scale The non-uniform scale transformation.
  5138. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5139. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5140. *
  5141. * @example
  5142. * const result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  5143. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  5144. * Cesium.Quaternion.IDENTITY, // rotation
  5145. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  5146. * result);
  5147. */
  5148. Matrix4.fromTranslationQuaternionRotationScale = function (
  5149. translation,
  5150. rotation,
  5151. scale,
  5152. result
  5153. ) {
  5154. //>>includeStart('debug', pragmas.debug);
  5155. RuntimeError.Check.typeOf.object("translation", translation);
  5156. RuntimeError.Check.typeOf.object("rotation", rotation);
  5157. RuntimeError.Check.typeOf.object("scale", scale);
  5158. //>>includeEnd('debug');
  5159. if (!defaultValue.defined(result)) {
  5160. result = new Matrix4();
  5161. }
  5162. const scaleX = scale.x;
  5163. const scaleY = scale.y;
  5164. const scaleZ = scale.z;
  5165. const x2 = rotation.x * rotation.x;
  5166. const xy = rotation.x * rotation.y;
  5167. const xz = rotation.x * rotation.z;
  5168. const xw = rotation.x * rotation.w;
  5169. const y2 = rotation.y * rotation.y;
  5170. const yz = rotation.y * rotation.z;
  5171. const yw = rotation.y * rotation.w;
  5172. const z2 = rotation.z * rotation.z;
  5173. const zw = rotation.z * rotation.w;
  5174. const w2 = rotation.w * rotation.w;
  5175. const m00 = x2 - y2 - z2 + w2;
  5176. const m01 = 2.0 * (xy - zw);
  5177. const m02 = 2.0 * (xz + yw);
  5178. const m10 = 2.0 * (xy + zw);
  5179. const m11 = -x2 + y2 - z2 + w2;
  5180. const m12 = 2.0 * (yz - xw);
  5181. const m20 = 2.0 * (xz - yw);
  5182. const m21 = 2.0 * (yz + xw);
  5183. const m22 = -x2 - y2 + z2 + w2;
  5184. result[0] = m00 * scaleX;
  5185. result[1] = m10 * scaleX;
  5186. result[2] = m20 * scaleX;
  5187. result[3] = 0.0;
  5188. result[4] = m01 * scaleY;
  5189. result[5] = m11 * scaleY;
  5190. result[6] = m21 * scaleY;
  5191. result[7] = 0.0;
  5192. result[8] = m02 * scaleZ;
  5193. result[9] = m12 * scaleZ;
  5194. result[10] = m22 * scaleZ;
  5195. result[11] = 0.0;
  5196. result[12] = translation.x;
  5197. result[13] = translation.y;
  5198. result[14] = translation.z;
  5199. result[15] = 1.0;
  5200. return result;
  5201. };
  5202. /**
  5203. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  5204. *
  5205. * @param {TranslationRotationScale} translationRotationScale The instance.
  5206. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5207. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5208. */
  5209. Matrix4.fromTranslationRotationScale = function (
  5210. translationRotationScale,
  5211. result
  5212. ) {
  5213. //>>includeStart('debug', pragmas.debug);
  5214. RuntimeError.Check.typeOf.object("translationRotationScale", translationRotationScale);
  5215. //>>includeEnd('debug');
  5216. return Matrix4.fromTranslationQuaternionRotationScale(
  5217. translationRotationScale.translation,
  5218. translationRotationScale.rotation,
  5219. translationRotationScale.scale,
  5220. result
  5221. );
  5222. };
  5223. /**
  5224. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  5225. *
  5226. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  5227. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5228. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5229. *
  5230. * @see Matrix4.multiplyByTranslation
  5231. */
  5232. Matrix4.fromTranslation = function (translation, result) {
  5233. //>>includeStart('debug', pragmas.debug);
  5234. RuntimeError.Check.typeOf.object("translation", translation);
  5235. //>>includeEnd('debug');
  5236. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  5237. };
  5238. /**
  5239. * Computes a Matrix4 instance representing a non-uniform scale.
  5240. *
  5241. * @param {Cartesian3} scale The x, y, and z scale factors.
  5242. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5243. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5244. *
  5245. * @example
  5246. * // Creates
  5247. * // [7.0, 0.0, 0.0, 0.0]
  5248. * // [0.0, 8.0, 0.0, 0.0]
  5249. * // [0.0, 0.0, 9.0, 0.0]
  5250. * // [0.0, 0.0, 0.0, 1.0]
  5251. * const m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  5252. */
  5253. Matrix4.fromScale = function (scale, result) {
  5254. //>>includeStart('debug', pragmas.debug);
  5255. RuntimeError.Check.typeOf.object("scale", scale);
  5256. //>>includeEnd('debug');
  5257. if (!defaultValue.defined(result)) {
  5258. return new Matrix4(
  5259. scale.x,
  5260. 0.0,
  5261. 0.0,
  5262. 0.0,
  5263. 0.0,
  5264. scale.y,
  5265. 0.0,
  5266. 0.0,
  5267. 0.0,
  5268. 0.0,
  5269. scale.z,
  5270. 0.0,
  5271. 0.0,
  5272. 0.0,
  5273. 0.0,
  5274. 1.0
  5275. );
  5276. }
  5277. result[0] = scale.x;
  5278. result[1] = 0.0;
  5279. result[2] = 0.0;
  5280. result[3] = 0.0;
  5281. result[4] = 0.0;
  5282. result[5] = scale.y;
  5283. result[6] = 0.0;
  5284. result[7] = 0.0;
  5285. result[8] = 0.0;
  5286. result[9] = 0.0;
  5287. result[10] = scale.z;
  5288. result[11] = 0.0;
  5289. result[12] = 0.0;
  5290. result[13] = 0.0;
  5291. result[14] = 0.0;
  5292. result[15] = 1.0;
  5293. return result;
  5294. };
  5295. /**
  5296. * Computes a Matrix4 instance representing a uniform scale.
  5297. *
  5298. * @param {Number} scale The uniform scale factor.
  5299. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5300. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5301. *
  5302. * @example
  5303. * // Creates
  5304. * // [2.0, 0.0, 0.0, 0.0]
  5305. * // [0.0, 2.0, 0.0, 0.0]
  5306. * // [0.0, 0.0, 2.0, 0.0]
  5307. * // [0.0, 0.0, 0.0, 1.0]
  5308. * const m = Cesium.Matrix4.fromUniformScale(2.0);
  5309. */
  5310. Matrix4.fromUniformScale = function (scale, result) {
  5311. //>>includeStart('debug', pragmas.debug);
  5312. RuntimeError.Check.typeOf.number("scale", scale);
  5313. //>>includeEnd('debug');
  5314. if (!defaultValue.defined(result)) {
  5315. return new Matrix4(
  5316. scale,
  5317. 0.0,
  5318. 0.0,
  5319. 0.0,
  5320. 0.0,
  5321. scale,
  5322. 0.0,
  5323. 0.0,
  5324. 0.0,
  5325. 0.0,
  5326. scale,
  5327. 0.0,
  5328. 0.0,
  5329. 0.0,
  5330. 0.0,
  5331. 1.0
  5332. );
  5333. }
  5334. result[0] = scale;
  5335. result[1] = 0.0;
  5336. result[2] = 0.0;
  5337. result[3] = 0.0;
  5338. result[4] = 0.0;
  5339. result[5] = scale;
  5340. result[6] = 0.0;
  5341. result[7] = 0.0;
  5342. result[8] = 0.0;
  5343. result[9] = 0.0;
  5344. result[10] = scale;
  5345. result[11] = 0.0;
  5346. result[12] = 0.0;
  5347. result[13] = 0.0;
  5348. result[14] = 0.0;
  5349. result[15] = 1.0;
  5350. return result;
  5351. };
  5352. /**
  5353. * Creates a rotation matrix.
  5354. *
  5355. * @param {Matrix3} rotation The rotation matrix.
  5356. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5357. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5358. */
  5359. Matrix4.fromRotation = function (rotation, result) {
  5360. //>>includeStart('debug', pragmas.debug);
  5361. RuntimeError.Check.typeOf.object("rotation", rotation);
  5362. //>>includeEnd('debug');
  5363. if (!defaultValue.defined(result)) {
  5364. result = new Matrix4();
  5365. }
  5366. result[0] = rotation[0];
  5367. result[1] = rotation[1];
  5368. result[2] = rotation[2];
  5369. result[3] = 0.0;
  5370. result[4] = rotation[3];
  5371. result[5] = rotation[4];
  5372. result[6] = rotation[5];
  5373. result[7] = 0.0;
  5374. result[8] = rotation[6];
  5375. result[9] = rotation[7];
  5376. result[10] = rotation[8];
  5377. result[11] = 0.0;
  5378. result[12] = 0.0;
  5379. result[13] = 0.0;
  5380. result[14] = 0.0;
  5381. result[15] = 1.0;
  5382. return result;
  5383. };
  5384. const fromCameraF = new Cartesian3();
  5385. const fromCameraR = new Cartesian3();
  5386. const fromCameraU = new Cartesian3();
  5387. /**
  5388. * Computes a Matrix4 instance from a Camera.
  5389. *
  5390. * @param {Camera} camera The camera to use.
  5391. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5392. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5393. */
  5394. Matrix4.fromCamera = function (camera, result) {
  5395. //>>includeStart('debug', pragmas.debug);
  5396. RuntimeError.Check.typeOf.object("camera", camera);
  5397. //>>includeEnd('debug');
  5398. const position = camera.position;
  5399. const direction = camera.direction;
  5400. const up = camera.up;
  5401. //>>includeStart('debug', pragmas.debug);
  5402. RuntimeError.Check.typeOf.object("camera.position", position);
  5403. RuntimeError.Check.typeOf.object("camera.direction", direction);
  5404. RuntimeError.Check.typeOf.object("camera.up", up);
  5405. //>>includeEnd('debug');
  5406. Cartesian3.normalize(direction, fromCameraF);
  5407. Cartesian3.normalize(
  5408. Cartesian3.cross(fromCameraF, up, fromCameraR),
  5409. fromCameraR
  5410. );
  5411. Cartesian3.normalize(
  5412. Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU),
  5413. fromCameraU
  5414. );
  5415. const sX = fromCameraR.x;
  5416. const sY = fromCameraR.y;
  5417. const sZ = fromCameraR.z;
  5418. const fX = fromCameraF.x;
  5419. const fY = fromCameraF.y;
  5420. const fZ = fromCameraF.z;
  5421. const uX = fromCameraU.x;
  5422. const uY = fromCameraU.y;
  5423. const uZ = fromCameraU.z;
  5424. const positionX = position.x;
  5425. const positionY = position.y;
  5426. const positionZ = position.z;
  5427. const t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
  5428. const t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
  5429. const t2 = fX * positionX + fY * positionY + fZ * positionZ;
  5430. // The code below this comment is an optimized
  5431. // version of the commented lines.
  5432. // Rather that create two matrices and then multiply,
  5433. // we just bake in the multiplcation as part of creation.
  5434. // const rotation = new Matrix4(
  5435. // sX, sY, sZ, 0.0,
  5436. // uX, uY, uZ, 0.0,
  5437. // -fX, -fY, -fZ, 0.0,
  5438. // 0.0, 0.0, 0.0, 1.0);
  5439. // const translation = new Matrix4(
  5440. // 1.0, 0.0, 0.0, -position.x,
  5441. // 0.0, 1.0, 0.0, -position.y,
  5442. // 0.0, 0.0, 1.0, -position.z,
  5443. // 0.0, 0.0, 0.0, 1.0);
  5444. // return rotation.multiply(translation);
  5445. if (!defaultValue.defined(result)) {
  5446. return new Matrix4(
  5447. sX,
  5448. sY,
  5449. sZ,
  5450. t0,
  5451. uX,
  5452. uY,
  5453. uZ,
  5454. t1,
  5455. -fX,
  5456. -fY,
  5457. -fZ,
  5458. t2,
  5459. 0.0,
  5460. 0.0,
  5461. 0.0,
  5462. 1.0
  5463. );
  5464. }
  5465. result[0] = sX;
  5466. result[1] = uX;
  5467. result[2] = -fX;
  5468. result[3] = 0.0;
  5469. result[4] = sY;
  5470. result[5] = uY;
  5471. result[6] = -fY;
  5472. result[7] = 0.0;
  5473. result[8] = sZ;
  5474. result[9] = uZ;
  5475. result[10] = -fZ;
  5476. result[11] = 0.0;
  5477. result[12] = t0;
  5478. result[13] = t1;
  5479. result[14] = t2;
  5480. result[15] = 1.0;
  5481. return result;
  5482. };
  5483. /**
  5484. * Computes a Matrix4 instance representing a perspective transformation matrix.
  5485. *
  5486. * @param {Number} fovY The field of view along the Y axis in radians.
  5487. * @param {Number} aspectRatio The aspect ratio.
  5488. * @param {Number} near The distance to the near plane in meters.
  5489. * @param {Number} far The distance to the far plane in meters.
  5490. * @param {Matrix4} result The object in which the result will be stored.
  5491. * @returns {Matrix4} The modified result parameter.
  5492. *
  5493. * @exception {DeveloperError} fovY must be in (0, PI].
  5494. * @exception {DeveloperError} aspectRatio must be greater than zero.
  5495. * @exception {DeveloperError} near must be greater than zero.
  5496. * @exception {DeveloperError} far must be greater than zero.
  5497. */
  5498. Matrix4.computePerspectiveFieldOfView = function (
  5499. fovY,
  5500. aspectRatio,
  5501. near,
  5502. far,
  5503. result
  5504. ) {
  5505. //>>includeStart('debug', pragmas.debug);
  5506. RuntimeError.Check.typeOf.number.greaterThan("fovY", fovY, 0.0);
  5507. RuntimeError.Check.typeOf.number.lessThan("fovY", fovY, Math.PI);
  5508. RuntimeError.Check.typeOf.number.greaterThan("near", near, 0.0);
  5509. RuntimeError.Check.typeOf.number.greaterThan("far", far, 0.0);
  5510. RuntimeError.Check.typeOf.object("result", result);
  5511. //>>includeEnd('debug');
  5512. const bottom = Math.tan(fovY * 0.5);
  5513. const column1Row1 = 1.0 / bottom;
  5514. const column0Row0 = column1Row1 / aspectRatio;
  5515. const column2Row2 = (far + near) / (near - far);
  5516. const column3Row2 = (2.0 * far * near) / (near - far);
  5517. result[0] = column0Row0;
  5518. result[1] = 0.0;
  5519. result[2] = 0.0;
  5520. result[3] = 0.0;
  5521. result[4] = 0.0;
  5522. result[5] = column1Row1;
  5523. result[6] = 0.0;
  5524. result[7] = 0.0;
  5525. result[8] = 0.0;
  5526. result[9] = 0.0;
  5527. result[10] = column2Row2;
  5528. result[11] = -1.0;
  5529. result[12] = 0.0;
  5530. result[13] = 0.0;
  5531. result[14] = column3Row2;
  5532. result[15] = 0.0;
  5533. return result;
  5534. };
  5535. /**
  5536. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  5537. *
  5538. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5539. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5540. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5541. * @param {Number} top The number of meters above of the camera that will be in view.
  5542. * @param {Number} near The distance to the near plane in meters.
  5543. * @param {Number} far The distance to the far plane in meters.
  5544. * @param {Matrix4} result The object in which the result will be stored.
  5545. * @returns {Matrix4} The modified result parameter.
  5546. */
  5547. Matrix4.computeOrthographicOffCenter = function (
  5548. left,
  5549. right,
  5550. bottom,
  5551. top,
  5552. near,
  5553. far,
  5554. result
  5555. ) {
  5556. //>>includeStart('debug', pragmas.debug);
  5557. RuntimeError.Check.typeOf.number("left", left);
  5558. RuntimeError.Check.typeOf.number("right", right);
  5559. RuntimeError.Check.typeOf.number("bottom", bottom);
  5560. RuntimeError.Check.typeOf.number("top", top);
  5561. RuntimeError.Check.typeOf.number("near", near);
  5562. RuntimeError.Check.typeOf.number("far", far);
  5563. RuntimeError.Check.typeOf.object("result", result);
  5564. //>>includeEnd('debug');
  5565. let a = 1.0 / (right - left);
  5566. let b = 1.0 / (top - bottom);
  5567. let c = 1.0 / (far - near);
  5568. const tx = -(right + left) * a;
  5569. const ty = -(top + bottom) * b;
  5570. const tz = -(far + near) * c;
  5571. a *= 2.0;
  5572. b *= 2.0;
  5573. c *= -2.0;
  5574. result[0] = a;
  5575. result[1] = 0.0;
  5576. result[2] = 0.0;
  5577. result[3] = 0.0;
  5578. result[4] = 0.0;
  5579. result[5] = b;
  5580. result[6] = 0.0;
  5581. result[7] = 0.0;
  5582. result[8] = 0.0;
  5583. result[9] = 0.0;
  5584. result[10] = c;
  5585. result[11] = 0.0;
  5586. result[12] = tx;
  5587. result[13] = ty;
  5588. result[14] = tz;
  5589. result[15] = 1.0;
  5590. return result;
  5591. };
  5592. /**
  5593. * Computes a Matrix4 instance representing an off center perspective transformation.
  5594. *
  5595. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5596. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5597. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5598. * @param {Number} top The number of meters above of the camera that will be in view.
  5599. * @param {Number} near The distance to the near plane in meters.
  5600. * @param {Number} far The distance to the far plane in meters.
  5601. * @param {Matrix4} result The object in which the result will be stored.
  5602. * @returns {Matrix4} The modified result parameter.
  5603. */
  5604. Matrix4.computePerspectiveOffCenter = function (
  5605. left,
  5606. right,
  5607. bottom,
  5608. top,
  5609. near,
  5610. far,
  5611. result
  5612. ) {
  5613. //>>includeStart('debug', pragmas.debug);
  5614. RuntimeError.Check.typeOf.number("left", left);
  5615. RuntimeError.Check.typeOf.number("right", right);
  5616. RuntimeError.Check.typeOf.number("bottom", bottom);
  5617. RuntimeError.Check.typeOf.number("top", top);
  5618. RuntimeError.Check.typeOf.number("near", near);
  5619. RuntimeError.Check.typeOf.number("far", far);
  5620. RuntimeError.Check.typeOf.object("result", result);
  5621. //>>includeEnd('debug');
  5622. const column0Row0 = (2.0 * near) / (right - left);
  5623. const column1Row1 = (2.0 * near) / (top - bottom);
  5624. const column2Row0 = (right + left) / (right - left);
  5625. const column2Row1 = (top + bottom) / (top - bottom);
  5626. const column2Row2 = -(far + near) / (far - near);
  5627. const column2Row3 = -1.0;
  5628. const column3Row2 = (-2.0 * far * near) / (far - near);
  5629. result[0] = column0Row0;
  5630. result[1] = 0.0;
  5631. result[2] = 0.0;
  5632. result[3] = 0.0;
  5633. result[4] = 0.0;
  5634. result[5] = column1Row1;
  5635. result[6] = 0.0;
  5636. result[7] = 0.0;
  5637. result[8] = column2Row0;
  5638. result[9] = column2Row1;
  5639. result[10] = column2Row2;
  5640. result[11] = column2Row3;
  5641. result[12] = 0.0;
  5642. result[13] = 0.0;
  5643. result[14] = column3Row2;
  5644. result[15] = 0.0;
  5645. return result;
  5646. };
  5647. /**
  5648. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  5649. *
  5650. * @param {Number} left The number of meters to the left of the camera that will be in view.
  5651. * @param {Number} right The number of meters to the right of the camera that will be in view.
  5652. * @param {Number} bottom The number of meters below of the camera that will be in view.
  5653. * @param {Number} top The number of meters above of the camera that will be in view.
  5654. * @param {Number} near The distance to the near plane in meters.
  5655. * @param {Matrix4} result The object in which the result will be stored.
  5656. * @returns {Matrix4} The modified result parameter.
  5657. */
  5658. Matrix4.computeInfinitePerspectiveOffCenter = function (
  5659. left,
  5660. right,
  5661. bottom,
  5662. top,
  5663. near,
  5664. result
  5665. ) {
  5666. //>>includeStart('debug', pragmas.debug);
  5667. RuntimeError.Check.typeOf.number("left", left);
  5668. RuntimeError.Check.typeOf.number("right", right);
  5669. RuntimeError.Check.typeOf.number("bottom", bottom);
  5670. RuntimeError.Check.typeOf.number("top", top);
  5671. RuntimeError.Check.typeOf.number("near", near);
  5672. RuntimeError.Check.typeOf.object("result", result);
  5673. //>>includeEnd('debug');
  5674. const column0Row0 = (2.0 * near) / (right - left);
  5675. const column1Row1 = (2.0 * near) / (top - bottom);
  5676. const column2Row0 = (right + left) / (right - left);
  5677. const column2Row1 = (top + bottom) / (top - bottom);
  5678. const column2Row2 = -1.0;
  5679. const column2Row3 = -1.0;
  5680. const column3Row2 = -2.0 * near;
  5681. result[0] = column0Row0;
  5682. result[1] = 0.0;
  5683. result[2] = 0.0;
  5684. result[3] = 0.0;
  5685. result[4] = 0.0;
  5686. result[5] = column1Row1;
  5687. result[6] = 0.0;
  5688. result[7] = 0.0;
  5689. result[8] = column2Row0;
  5690. result[9] = column2Row1;
  5691. result[10] = column2Row2;
  5692. result[11] = column2Row3;
  5693. result[12] = 0.0;
  5694. result[13] = 0.0;
  5695. result[14] = column3Row2;
  5696. result[15] = 0.0;
  5697. return result;
  5698. };
  5699. /**
  5700. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  5701. *
  5702. * @param {Object} [viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  5703. * @param {Number} [nearDepthRange=0.0] The near plane distance in window coordinates.
  5704. * @param {Number} [farDepthRange=1.0] The far plane distance in window coordinates.
  5705. * @param {Matrix4} [result] The object in which the result will be stored.
  5706. * @returns {Matrix4} The modified result parameter.
  5707. *
  5708. * @example
  5709. * // Create viewport transformation using an explicit viewport and depth range.
  5710. * const m = Cesium.Matrix4.computeViewportTransformation({
  5711. * x : 0.0,
  5712. * y : 0.0,
  5713. * width : 1024.0,
  5714. * height : 768.0
  5715. * }, 0.0, 1.0, new Cesium.Matrix4());
  5716. */
  5717. Matrix4.computeViewportTransformation = function (
  5718. viewport,
  5719. nearDepthRange,
  5720. farDepthRange,
  5721. result
  5722. ) {
  5723. if (!defaultValue.defined(result)) {
  5724. result = new Matrix4();
  5725. }
  5726. viewport = defaultValue.defaultValue(viewport, defaultValue.defaultValue.EMPTY_OBJECT);
  5727. const x = defaultValue.defaultValue(viewport.x, 0.0);
  5728. const y = defaultValue.defaultValue(viewport.y, 0.0);
  5729. const width = defaultValue.defaultValue(viewport.width, 0.0);
  5730. const height = defaultValue.defaultValue(viewport.height, 0.0);
  5731. nearDepthRange = defaultValue.defaultValue(nearDepthRange, 0.0);
  5732. farDepthRange = defaultValue.defaultValue(farDepthRange, 1.0);
  5733. const halfWidth = width * 0.5;
  5734. const halfHeight = height * 0.5;
  5735. const halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  5736. const column0Row0 = halfWidth;
  5737. const column1Row1 = halfHeight;
  5738. const column2Row2 = halfDepth;
  5739. const column3Row0 = x + halfWidth;
  5740. const column3Row1 = y + halfHeight;
  5741. const column3Row2 = nearDepthRange + halfDepth;
  5742. const column3Row3 = 1.0;
  5743. result[0] = column0Row0;
  5744. result[1] = 0.0;
  5745. result[2] = 0.0;
  5746. result[3] = 0.0;
  5747. result[4] = 0.0;
  5748. result[5] = column1Row1;
  5749. result[6] = 0.0;
  5750. result[7] = 0.0;
  5751. result[8] = 0.0;
  5752. result[9] = 0.0;
  5753. result[10] = column2Row2;
  5754. result[11] = 0.0;
  5755. result[12] = column3Row0;
  5756. result[13] = column3Row1;
  5757. result[14] = column3Row2;
  5758. result[15] = column3Row3;
  5759. return result;
  5760. };
  5761. /**
  5762. * Computes a Matrix4 instance that transforms from world space to view space.
  5763. *
  5764. * @param {Cartesian3} position The position of the camera.
  5765. * @param {Cartesian3} direction The forward direction.
  5766. * @param {Cartesian3} up The up direction.
  5767. * @param {Cartesian3} right The right direction.
  5768. * @param {Matrix4} result The object in which the result will be stored.
  5769. * @returns {Matrix4} The modified result parameter.
  5770. */
  5771. Matrix4.computeView = function (position, direction, up, right, result) {
  5772. //>>includeStart('debug', pragmas.debug);
  5773. RuntimeError.Check.typeOf.object("position", position);
  5774. RuntimeError.Check.typeOf.object("direction", direction);
  5775. RuntimeError.Check.typeOf.object("up", up);
  5776. RuntimeError.Check.typeOf.object("right", right);
  5777. RuntimeError.Check.typeOf.object("result", result);
  5778. //>>includeEnd('debug');
  5779. result[0] = right.x;
  5780. result[1] = up.x;
  5781. result[2] = -direction.x;
  5782. result[3] = 0.0;
  5783. result[4] = right.y;
  5784. result[5] = up.y;
  5785. result[6] = -direction.y;
  5786. result[7] = 0.0;
  5787. result[8] = right.z;
  5788. result[9] = up.z;
  5789. result[10] = -direction.z;
  5790. result[11] = 0.0;
  5791. result[12] = -Cartesian3.dot(right, position);
  5792. result[13] = -Cartesian3.dot(up, position);
  5793. result[14] = Cartesian3.dot(direction, position);
  5794. result[15] = 1.0;
  5795. return result;
  5796. };
  5797. /**
  5798. * Computes an Array from the provided Matrix4 instance.
  5799. * The array will be in column-major order.
  5800. *
  5801. * @param {Matrix4} matrix The matrix to use..
  5802. * @param {Number[]} [result] The Array onto which to store the result.
  5803. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  5804. *
  5805. * @example
  5806. * //create an array from an instance of Matrix4
  5807. * // m = [10.0, 14.0, 18.0, 22.0]
  5808. * // [11.0, 15.0, 19.0, 23.0]
  5809. * // [12.0, 16.0, 20.0, 24.0]
  5810. * // [13.0, 17.0, 21.0, 25.0]
  5811. * const a = Cesium.Matrix4.toArray(m);
  5812. *
  5813. * // m remains the same
  5814. * //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]
  5815. */
  5816. Matrix4.toArray = function (matrix, result) {
  5817. //>>includeStart('debug', pragmas.debug);
  5818. RuntimeError.Check.typeOf.object("matrix", matrix);
  5819. //>>includeEnd('debug');
  5820. if (!defaultValue.defined(result)) {
  5821. return [
  5822. matrix[0],
  5823. matrix[1],
  5824. matrix[2],
  5825. matrix[3],
  5826. matrix[4],
  5827. matrix[5],
  5828. matrix[6],
  5829. matrix[7],
  5830. matrix[8],
  5831. matrix[9],
  5832. matrix[10],
  5833. matrix[11],
  5834. matrix[12],
  5835. matrix[13],
  5836. matrix[14],
  5837. matrix[15],
  5838. ];
  5839. }
  5840. result[0] = matrix[0];
  5841. result[1] = matrix[1];
  5842. result[2] = matrix[2];
  5843. result[3] = matrix[3];
  5844. result[4] = matrix[4];
  5845. result[5] = matrix[5];
  5846. result[6] = matrix[6];
  5847. result[7] = matrix[7];
  5848. result[8] = matrix[8];
  5849. result[9] = matrix[9];
  5850. result[10] = matrix[10];
  5851. result[11] = matrix[11];
  5852. result[12] = matrix[12];
  5853. result[13] = matrix[13];
  5854. result[14] = matrix[14];
  5855. result[15] = matrix[15];
  5856. return result;
  5857. };
  5858. /**
  5859. * Computes the array index of the element at the provided row and column.
  5860. *
  5861. * @param {Number} row The zero-based index of the row.
  5862. * @param {Number} column The zero-based index of the column.
  5863. * @returns {Number} The index of the element at the provided row and column.
  5864. *
  5865. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  5866. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  5867. *
  5868. * @example
  5869. * const myMatrix = new Cesium.Matrix4();
  5870. * const column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  5871. * const column1Row0 = myMatrix[column1Row0Index];
  5872. * myMatrix[column1Row0Index] = 10.0;
  5873. */
  5874. Matrix4.getElementIndex = function (column, row) {
  5875. //>>includeStart('debug', pragmas.debug);
  5876. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  5877. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 3);
  5878. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  5879. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 3);
  5880. //>>includeEnd('debug');
  5881. return column * 4 + row;
  5882. };
  5883. /**
  5884. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  5885. *
  5886. * @param {Matrix4} matrix The matrix to use.
  5887. * @param {Number} index The zero-based index of the column to retrieve.
  5888. * @param {Cartesian4} result The object onto which to store the result.
  5889. * @returns {Cartesian4} The modified result parameter.
  5890. *
  5891. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5892. *
  5893. * @example
  5894. * //returns a Cartesian4 instance with values from the specified column
  5895. * // m = [10.0, 11.0, 12.0, 13.0]
  5896. * // [14.0, 15.0, 16.0, 17.0]
  5897. * // [18.0, 19.0, 20.0, 21.0]
  5898. * // [22.0, 23.0, 24.0, 25.0]
  5899. *
  5900. * //Example 1: Creates an instance of Cartesian
  5901. * const a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  5902. *
  5903. * @example
  5904. * //Example 2: Sets values for Cartesian instance
  5905. * const a = new Cesium.Cartesian4();
  5906. * Cesium.Matrix4.getColumn(m, 2, a);
  5907. *
  5908. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  5909. */
  5910. Matrix4.getColumn = function (matrix, index, result) {
  5911. //>>includeStart('debug', pragmas.debug);
  5912. RuntimeError.Check.typeOf.object("matrix", matrix);
  5913. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5914. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5915. RuntimeError.Check.typeOf.object("result", result);
  5916. //>>includeEnd('debug');
  5917. const startIndex = index * 4;
  5918. const x = matrix[startIndex];
  5919. const y = matrix[startIndex + 1];
  5920. const z = matrix[startIndex + 2];
  5921. const w = matrix[startIndex + 3];
  5922. result.x = x;
  5923. result.y = y;
  5924. result.z = z;
  5925. result.w = w;
  5926. return result;
  5927. };
  5928. /**
  5929. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  5930. *
  5931. * @param {Matrix4} matrix The matrix to use.
  5932. * @param {Number} index The zero-based index of the column to set.
  5933. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  5934. * @param {Matrix4} result The object onto which to store the result.
  5935. * @returns {Matrix4} The modified result parameter.
  5936. *
  5937. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5938. *
  5939. * @example
  5940. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  5941. * // m = [10.0, 11.0, 12.0, 13.0]
  5942. * // [14.0, 15.0, 16.0, 17.0]
  5943. * // [18.0, 19.0, 20.0, 21.0]
  5944. * // [22.0, 23.0, 24.0, 25.0]
  5945. *
  5946. * const a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  5947. *
  5948. * // m remains the same
  5949. * // a = [10.0, 11.0, 99.0, 13.0]
  5950. * // [14.0, 15.0, 98.0, 17.0]
  5951. * // [18.0, 19.0, 97.0, 21.0]
  5952. * // [22.0, 23.0, 96.0, 25.0]
  5953. */
  5954. Matrix4.setColumn = function (matrix, index, cartesian, result) {
  5955. //>>includeStart('debug', pragmas.debug);
  5956. RuntimeError.Check.typeOf.object("matrix", matrix);
  5957. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  5958. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  5959. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  5960. RuntimeError.Check.typeOf.object("result", result);
  5961. //>>includeEnd('debug');
  5962. result = Matrix4.clone(matrix, result);
  5963. const startIndex = index * 4;
  5964. result[startIndex] = cartesian.x;
  5965. result[startIndex + 1] = cartesian.y;
  5966. result[startIndex + 2] = cartesian.z;
  5967. result[startIndex + 3] = cartesian.w;
  5968. return result;
  5969. };
  5970. /**
  5971. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  5972. *
  5973. * @param {Matrix4} matrix The matrix to use.
  5974. * @param {Number} index The zero-based index of the row to retrieve.
  5975. * @param {Cartesian4} result The object onto which to store the result.
  5976. * @returns {Cartesian4} The modified result parameter.
  5977. *
  5978. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  5979. *
  5980. * @example
  5981. * //returns a Cartesian4 instance with values from the specified column
  5982. * // m = [10.0, 11.0, 12.0, 13.0]
  5983. * // [14.0, 15.0, 16.0, 17.0]
  5984. * // [18.0, 19.0, 20.0, 21.0]
  5985. * // [22.0, 23.0, 24.0, 25.0]
  5986. *
  5987. * //Example 1: Returns an instance of Cartesian
  5988. * const a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  5989. *
  5990. * @example
  5991. * //Example 2: Sets values for a Cartesian instance
  5992. * const a = new Cesium.Cartesian4();
  5993. * Cesium.Matrix4.getRow(m, 2, a);
  5994. *
  5995. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  5996. */
  5997. Matrix4.getRow = function (matrix, index, result) {
  5998. //>>includeStart('debug', pragmas.debug);
  5999. RuntimeError.Check.typeOf.object("matrix", matrix);
  6000. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  6001. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  6002. RuntimeError.Check.typeOf.object("result", result);
  6003. //>>includeEnd('debug');
  6004. const x = matrix[index];
  6005. const y = matrix[index + 4];
  6006. const z = matrix[index + 8];
  6007. const w = matrix[index + 12];
  6008. result.x = x;
  6009. result.y = y;
  6010. result.z = z;
  6011. result.w = w;
  6012. return result;
  6013. };
  6014. /**
  6015. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  6016. *
  6017. * @param {Matrix4} matrix The matrix to use.
  6018. * @param {Number} index The zero-based index of the row to set.
  6019. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  6020. * @param {Matrix4} result The object onto which to store the result.
  6021. * @returns {Matrix4} The modified result parameter.
  6022. *
  6023. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6024. *
  6025. * @example
  6026. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  6027. * // m = [10.0, 11.0, 12.0, 13.0]
  6028. * // [14.0, 15.0, 16.0, 17.0]
  6029. * // [18.0, 19.0, 20.0, 21.0]
  6030. * // [22.0, 23.0, 24.0, 25.0]
  6031. *
  6032. * const a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  6033. *
  6034. * // m remains the same
  6035. * // a = [10.0, 11.0, 12.0, 13.0]
  6036. * // [14.0, 15.0, 16.0, 17.0]
  6037. * // [99.0, 98.0, 97.0, 96.0]
  6038. * // [22.0, 23.0, 24.0, 25.0]
  6039. */
  6040. Matrix4.setRow = function (matrix, index, cartesian, result) {
  6041. //>>includeStart('debug', pragmas.debug);
  6042. RuntimeError.Check.typeOf.object("matrix", matrix);
  6043. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  6044. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  6045. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6046. RuntimeError.Check.typeOf.object("result", result);
  6047. //>>includeEnd('debug');
  6048. result = Matrix4.clone(matrix, result);
  6049. result[index] = cartesian.x;
  6050. result[index + 4] = cartesian.y;
  6051. result[index + 8] = cartesian.z;
  6052. result[index + 12] = cartesian.w;
  6053. return result;
  6054. };
  6055. /**
  6056. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  6057. * matrix with the provided translation. This assumes the matrix is an affine transformation.
  6058. *
  6059. * @param {Matrix4} matrix The matrix to use.
  6060. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  6061. * @param {Matrix4} result The object onto which to store the result.
  6062. * @returns {Matrix4} The modified result parameter.
  6063. */
  6064. Matrix4.setTranslation = function (matrix, translation, result) {
  6065. //>>includeStart('debug', pragmas.debug);
  6066. RuntimeError.Check.typeOf.object("matrix", matrix);
  6067. RuntimeError.Check.typeOf.object("translation", translation);
  6068. RuntimeError.Check.typeOf.object("result", result);
  6069. //>>includeEnd('debug');
  6070. result[0] = matrix[0];
  6071. result[1] = matrix[1];
  6072. result[2] = matrix[2];
  6073. result[3] = matrix[3];
  6074. result[4] = matrix[4];
  6075. result[5] = matrix[5];
  6076. result[6] = matrix[6];
  6077. result[7] = matrix[7];
  6078. result[8] = matrix[8];
  6079. result[9] = matrix[9];
  6080. result[10] = matrix[10];
  6081. result[11] = matrix[11];
  6082. result[12] = translation.x;
  6083. result[13] = translation.y;
  6084. result[14] = translation.z;
  6085. result[15] = matrix[15];
  6086. return result;
  6087. };
  6088. const scaleScratch1$1 = new Cartesian3();
  6089. /**
  6090. * Computes a new matrix that replaces the scale with the provided scale.
  6091. * This assumes the matrix is an affine transformation.
  6092. *
  6093. * @param {Matrix4} matrix The matrix to use.
  6094. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  6095. * @param {Matrix4} result The object onto which to store the result.
  6096. * @returns {Matrix4} The modified result parameter.
  6097. *
  6098. * @see Matrix4.setUniformScale
  6099. * @see Matrix4.fromScale
  6100. * @see Matrix4.fromUniformScale
  6101. * @see Matrix4.multiplyByScale
  6102. * @see Matrix4.multiplyByUniformScale
  6103. * @see Matrix4.getScale
  6104. */
  6105. Matrix4.setScale = function (matrix, scale, result) {
  6106. //>>includeStart('debug', pragmas.debug);
  6107. RuntimeError.Check.typeOf.object("matrix", matrix);
  6108. RuntimeError.Check.typeOf.object("scale", scale);
  6109. RuntimeError.Check.typeOf.object("result", result);
  6110. //>>includeEnd('debug');
  6111. const existingScale = Matrix4.getScale(matrix, scaleScratch1$1);
  6112. const scaleRatioX = scale.x / existingScale.x;
  6113. const scaleRatioY = scale.y / existingScale.y;
  6114. const scaleRatioZ = scale.z / existingScale.z;
  6115. result[0] = matrix[0] * scaleRatioX;
  6116. result[1] = matrix[1] * scaleRatioX;
  6117. result[2] = matrix[2] * scaleRatioX;
  6118. result[3] = matrix[3];
  6119. result[4] = matrix[4] * scaleRatioY;
  6120. result[5] = matrix[5] * scaleRatioY;
  6121. result[6] = matrix[6] * scaleRatioY;
  6122. result[7] = matrix[7];
  6123. result[8] = matrix[8] * scaleRatioZ;
  6124. result[9] = matrix[9] * scaleRatioZ;
  6125. result[10] = matrix[10] * scaleRatioZ;
  6126. result[11] = matrix[11];
  6127. result[12] = matrix[12];
  6128. result[13] = matrix[13];
  6129. result[14] = matrix[14];
  6130. result[15] = matrix[15];
  6131. return result;
  6132. };
  6133. const scaleScratch2$1 = new Cartesian3();
  6134. /**
  6135. * Computes a new matrix that replaces the scale with the provided uniform scale.
  6136. * This assumes the matrix is an affine transformation.
  6137. *
  6138. * @param {Matrix4} matrix The matrix to use.
  6139. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  6140. * @param {Matrix4} result The object onto which to store the result.
  6141. * @returns {Matrix4} The modified result parameter.
  6142. *
  6143. * @see Matrix4.setScale
  6144. * @see Matrix4.fromScale
  6145. * @see Matrix4.fromUniformScale
  6146. * @see Matrix4.multiplyByScale
  6147. * @see Matrix4.multiplyByUniformScale
  6148. * @see Matrix4.getScale
  6149. */
  6150. Matrix4.setUniformScale = function (matrix, scale, result) {
  6151. //>>includeStart('debug', pragmas.debug);
  6152. RuntimeError.Check.typeOf.object("matrix", matrix);
  6153. RuntimeError.Check.typeOf.number("scale", scale);
  6154. RuntimeError.Check.typeOf.object("result", result);
  6155. //>>includeEnd('debug');
  6156. const existingScale = Matrix4.getScale(matrix, scaleScratch2$1);
  6157. const scaleRatioX = scale / existingScale.x;
  6158. const scaleRatioY = scale / existingScale.y;
  6159. const scaleRatioZ = scale / existingScale.z;
  6160. result[0] = matrix[0] * scaleRatioX;
  6161. result[1] = matrix[1] * scaleRatioX;
  6162. result[2] = matrix[2] * scaleRatioX;
  6163. result[3] = matrix[3];
  6164. result[4] = matrix[4] * scaleRatioY;
  6165. result[5] = matrix[5] * scaleRatioY;
  6166. result[6] = matrix[6] * scaleRatioY;
  6167. result[7] = matrix[7];
  6168. result[8] = matrix[8] * scaleRatioZ;
  6169. result[9] = matrix[9] * scaleRatioZ;
  6170. result[10] = matrix[10] * scaleRatioZ;
  6171. result[11] = matrix[11];
  6172. result[12] = matrix[12];
  6173. result[13] = matrix[13];
  6174. result[14] = matrix[14];
  6175. result[15] = matrix[15];
  6176. return result;
  6177. };
  6178. const scratchColumn$1 = new Cartesian3();
  6179. /**
  6180. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  6181. *
  6182. * @param {Matrix4} matrix The matrix.
  6183. * @param {Cartesian3} result The object onto which to store the result.
  6184. * @returns {Cartesian3} The modified result parameter
  6185. *
  6186. * @see Matrix4.multiplyByScale
  6187. * @see Matrix4.multiplyByUniformScale
  6188. * @see Matrix4.fromScale
  6189. * @see Matrix4.fromUniformScale
  6190. * @see Matrix4.setScale
  6191. * @see Matrix4.setUniformScale
  6192. */
  6193. Matrix4.getScale = function (matrix, result) {
  6194. //>>includeStart('debug', pragmas.debug);
  6195. RuntimeError.Check.typeOf.object("matrix", matrix);
  6196. RuntimeError.Check.typeOf.object("result", result);
  6197. //>>includeEnd('debug');
  6198. result.x = Cartesian3.magnitude(
  6199. Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$1)
  6200. );
  6201. result.y = Cartesian3.magnitude(
  6202. Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn$1)
  6203. );
  6204. result.z = Cartesian3.magnitude(
  6205. Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn$1)
  6206. );
  6207. return result;
  6208. };
  6209. const scaleScratch3$1 = new Cartesian3();
  6210. /**
  6211. * Computes the maximum scale assuming the matrix is an affine transformation.
  6212. * The maximum scale is the maximum length of the column vectors in the upper-left
  6213. * 3x3 matrix.
  6214. *
  6215. * @param {Matrix4} matrix The matrix.
  6216. * @returns {Number} The maximum scale.
  6217. */
  6218. Matrix4.getMaximumScale = function (matrix) {
  6219. Matrix4.getScale(matrix, scaleScratch3$1);
  6220. return Cartesian3.maximumComponent(scaleScratch3$1);
  6221. };
  6222. const scaleScratch4$1 = new Cartesian3();
  6223. /**
  6224. * Sets the rotation assuming the matrix is an affine transformation.
  6225. *
  6226. * @param {Matrix4} matrix The matrix.
  6227. * @param {Matrix4} rotation The rotation matrix.
  6228. * @returns {Matrix4} The modified result parameter.
  6229. *
  6230. * @see Matrix4.fromRotation
  6231. * @see Matrix4.getRotation
  6232. */
  6233. Matrix4.setRotation = function (matrix, rotation, result) {
  6234. //>>includeStart('debug', pragmas.debug);
  6235. RuntimeError.Check.typeOf.object("matrix", matrix);
  6236. RuntimeError.Check.typeOf.object("result", result);
  6237. //>>includeEnd('debug');
  6238. const scale = Matrix4.getScale(matrix, scaleScratch4$1);
  6239. result[0] = rotation[0] * scale.x;
  6240. result[1] = rotation[1] * scale.x;
  6241. result[2] = rotation[2] * scale.x;
  6242. result[3] = matrix[3];
  6243. result[4] = rotation[3] * scale.y;
  6244. result[5] = rotation[4] * scale.y;
  6245. result[6] = rotation[5] * scale.y;
  6246. result[7] = matrix[7];
  6247. result[8] = rotation[6] * scale.z;
  6248. result[9] = rotation[7] * scale.z;
  6249. result[10] = rotation[8] * scale.z;
  6250. result[11] = matrix[11];
  6251. result[12] = matrix[12];
  6252. result[13] = matrix[13];
  6253. result[14] = matrix[14];
  6254. result[15] = matrix[15];
  6255. return result;
  6256. };
  6257. const scaleScratch5$1 = new Cartesian3();
  6258. /**
  6259. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  6260. *
  6261. * @param {Matrix4} matrix The matrix.
  6262. * @param {Matrix4} result The object onto which to store the result.
  6263. * @returns {Matrix4} The modified result parameter.
  6264. *
  6265. * @see Matrix4.setRotation
  6266. * @see Matrix4.fromRotation
  6267. */
  6268. Matrix4.getRotation = function (matrix, result) {
  6269. //>>includeStart('debug', pragmas.debug);
  6270. RuntimeError.Check.typeOf.object("matrix", matrix);
  6271. RuntimeError.Check.typeOf.object("result", result);
  6272. //>>includeEnd('debug');
  6273. const scale = Matrix4.getScale(matrix, scaleScratch5$1);
  6274. result[0] = matrix[0] / scale.x;
  6275. result[1] = matrix[1] / scale.x;
  6276. result[2] = matrix[2] / scale.x;
  6277. result[3] = matrix[4] / scale.y;
  6278. result[4] = matrix[5] / scale.y;
  6279. result[5] = matrix[6] / scale.y;
  6280. result[6] = matrix[8] / scale.z;
  6281. result[7] = matrix[9] / scale.z;
  6282. result[8] = matrix[10] / scale.z;
  6283. return result;
  6284. };
  6285. /**
  6286. * Computes the product of two matrices.
  6287. *
  6288. * @param {Matrix4} left The first matrix.
  6289. * @param {Matrix4} right The second matrix.
  6290. * @param {Matrix4} result The object onto which to store the result.
  6291. * @returns {Matrix4} The modified result parameter.
  6292. */
  6293. Matrix4.multiply = function (left, right, result) {
  6294. //>>includeStart('debug', pragmas.debug);
  6295. RuntimeError.Check.typeOf.object("left", left);
  6296. RuntimeError.Check.typeOf.object("right", right);
  6297. RuntimeError.Check.typeOf.object("result", result);
  6298. //>>includeEnd('debug');
  6299. const left0 = left[0];
  6300. const left1 = left[1];
  6301. const left2 = left[2];
  6302. const left3 = left[3];
  6303. const left4 = left[4];
  6304. const left5 = left[5];
  6305. const left6 = left[6];
  6306. const left7 = left[7];
  6307. const left8 = left[8];
  6308. const left9 = left[9];
  6309. const left10 = left[10];
  6310. const left11 = left[11];
  6311. const left12 = left[12];
  6312. const left13 = left[13];
  6313. const left14 = left[14];
  6314. const left15 = left[15];
  6315. const right0 = right[0];
  6316. const right1 = right[1];
  6317. const right2 = right[2];
  6318. const right3 = right[3];
  6319. const right4 = right[4];
  6320. const right5 = right[5];
  6321. const right6 = right[6];
  6322. const right7 = right[7];
  6323. const right8 = right[8];
  6324. const right9 = right[9];
  6325. const right10 = right[10];
  6326. const right11 = right[11];
  6327. const right12 = right[12];
  6328. const right13 = right[13];
  6329. const right14 = right[14];
  6330. const right15 = right[15];
  6331. const column0Row0 =
  6332. left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  6333. const column0Row1 =
  6334. left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  6335. const column0Row2 =
  6336. left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  6337. const column0Row3 =
  6338. left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  6339. const column1Row0 =
  6340. left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  6341. const column1Row1 =
  6342. left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  6343. const column1Row2 =
  6344. left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  6345. const column1Row3 =
  6346. left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  6347. const column2Row0 =
  6348. left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  6349. const column2Row1 =
  6350. left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  6351. const column2Row2 =
  6352. left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  6353. const column2Row3 =
  6354. left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  6355. const column3Row0 =
  6356. left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  6357. const column3Row1 =
  6358. left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  6359. const column3Row2 =
  6360. left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  6361. const column3Row3 =
  6362. left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  6363. result[0] = column0Row0;
  6364. result[1] = column0Row1;
  6365. result[2] = column0Row2;
  6366. result[3] = column0Row3;
  6367. result[4] = column1Row0;
  6368. result[5] = column1Row1;
  6369. result[6] = column1Row2;
  6370. result[7] = column1Row3;
  6371. result[8] = column2Row0;
  6372. result[9] = column2Row1;
  6373. result[10] = column2Row2;
  6374. result[11] = column2Row3;
  6375. result[12] = column3Row0;
  6376. result[13] = column3Row1;
  6377. result[14] = column3Row2;
  6378. result[15] = column3Row3;
  6379. return result;
  6380. };
  6381. /**
  6382. * Computes the sum of two matrices.
  6383. *
  6384. * @param {Matrix4} left The first matrix.
  6385. * @param {Matrix4} right The second matrix.
  6386. * @param {Matrix4} result The object onto which to store the result.
  6387. * @returns {Matrix4} The modified result parameter.
  6388. */
  6389. Matrix4.add = function (left, right, result) {
  6390. //>>includeStart('debug', pragmas.debug);
  6391. RuntimeError.Check.typeOf.object("left", left);
  6392. RuntimeError.Check.typeOf.object("right", right);
  6393. RuntimeError.Check.typeOf.object("result", result);
  6394. //>>includeEnd('debug');
  6395. result[0] = left[0] + right[0];
  6396. result[1] = left[1] + right[1];
  6397. result[2] = left[2] + right[2];
  6398. result[3] = left[3] + right[3];
  6399. result[4] = left[4] + right[4];
  6400. result[5] = left[5] + right[5];
  6401. result[6] = left[6] + right[6];
  6402. result[7] = left[7] + right[7];
  6403. result[8] = left[8] + right[8];
  6404. result[9] = left[9] + right[9];
  6405. result[10] = left[10] + right[10];
  6406. result[11] = left[11] + right[11];
  6407. result[12] = left[12] + right[12];
  6408. result[13] = left[13] + right[13];
  6409. result[14] = left[14] + right[14];
  6410. result[15] = left[15] + right[15];
  6411. return result;
  6412. };
  6413. /**
  6414. * Computes the difference of two matrices.
  6415. *
  6416. * @param {Matrix4} left The first matrix.
  6417. * @param {Matrix4} right The second matrix.
  6418. * @param {Matrix4} result The object onto which to store the result.
  6419. * @returns {Matrix4} The modified result parameter.
  6420. */
  6421. Matrix4.subtract = function (left, right, result) {
  6422. //>>includeStart('debug', pragmas.debug);
  6423. RuntimeError.Check.typeOf.object("left", left);
  6424. RuntimeError.Check.typeOf.object("right", right);
  6425. RuntimeError.Check.typeOf.object("result", result);
  6426. //>>includeEnd('debug');
  6427. result[0] = left[0] - right[0];
  6428. result[1] = left[1] - right[1];
  6429. result[2] = left[2] - right[2];
  6430. result[3] = left[3] - right[3];
  6431. result[4] = left[4] - right[4];
  6432. result[5] = left[5] - right[5];
  6433. result[6] = left[6] - right[6];
  6434. result[7] = left[7] - right[7];
  6435. result[8] = left[8] - right[8];
  6436. result[9] = left[9] - right[9];
  6437. result[10] = left[10] - right[10];
  6438. result[11] = left[11] - right[11];
  6439. result[12] = left[12] - right[12];
  6440. result[13] = left[13] - right[13];
  6441. result[14] = left[14] - right[14];
  6442. result[15] = left[15] - right[15];
  6443. return result;
  6444. };
  6445. /**
  6446. * Computes the product of two matrices assuming the matrices are affine transformation matrices,
  6447. * where the upper left 3x3 elements are any matrix, and
  6448. * the upper three elements in the fourth column are the translation.
  6449. * The bottom row is assumed to be [0, 0, 0, 1].
  6450. * The matrix is not verified to be in the proper form.
  6451. * This method is faster than computing the product for general 4x4
  6452. * matrices using {@link Matrix4.multiply}.
  6453. *
  6454. * @param {Matrix4} left The first matrix.
  6455. * @param {Matrix4} right The second matrix.
  6456. * @param {Matrix4} result The object onto which to store the result.
  6457. * @returns {Matrix4} The modified result parameter.
  6458. *
  6459. * @example
  6460. * 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);
  6461. * const m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  6462. * const m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  6463. */
  6464. Matrix4.multiplyTransformation = function (left, right, result) {
  6465. //>>includeStart('debug', pragmas.debug);
  6466. RuntimeError.Check.typeOf.object("left", left);
  6467. RuntimeError.Check.typeOf.object("right", right);
  6468. RuntimeError.Check.typeOf.object("result", result);
  6469. //>>includeEnd('debug');
  6470. const left0 = left[0];
  6471. const left1 = left[1];
  6472. const left2 = left[2];
  6473. const left4 = left[4];
  6474. const left5 = left[5];
  6475. const left6 = left[6];
  6476. const left8 = left[8];
  6477. const left9 = left[9];
  6478. const left10 = left[10];
  6479. const left12 = left[12];
  6480. const left13 = left[13];
  6481. const left14 = left[14];
  6482. const right0 = right[0];
  6483. const right1 = right[1];
  6484. const right2 = right[2];
  6485. const right4 = right[4];
  6486. const right5 = right[5];
  6487. const right6 = right[6];
  6488. const right8 = right[8];
  6489. const right9 = right[9];
  6490. const right10 = right[10];
  6491. const right12 = right[12];
  6492. const right13 = right[13];
  6493. const right14 = right[14];
  6494. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6495. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6496. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6497. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6498. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6499. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6500. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6501. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6502. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6503. const column3Row0 =
  6504. left0 * right12 + left4 * right13 + left8 * right14 + left12;
  6505. const column3Row1 =
  6506. left1 * right12 + left5 * right13 + left9 * right14 + left13;
  6507. const column3Row2 =
  6508. left2 * right12 + left6 * right13 + left10 * right14 + left14;
  6509. result[0] = column0Row0;
  6510. result[1] = column0Row1;
  6511. result[2] = column0Row2;
  6512. result[3] = 0.0;
  6513. result[4] = column1Row0;
  6514. result[5] = column1Row1;
  6515. result[6] = column1Row2;
  6516. result[7] = 0.0;
  6517. result[8] = column2Row0;
  6518. result[9] = column2Row1;
  6519. result[10] = column2Row2;
  6520. result[11] = 0.0;
  6521. result[12] = column3Row0;
  6522. result[13] = column3Row1;
  6523. result[14] = column3Row2;
  6524. result[15] = 1.0;
  6525. return result;
  6526. };
  6527. /**
  6528. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6529. * by a 3x3 rotation matrix. This is an optimization
  6530. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  6531. *
  6532. * @param {Matrix4} matrix The matrix on the left-hand side.
  6533. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  6534. * @param {Matrix4} result The object onto which to store the result.
  6535. * @returns {Matrix4} The modified result parameter.
  6536. *
  6537. * @example
  6538. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  6539. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  6540. */
  6541. Matrix4.multiplyByMatrix3 = function (matrix, rotation, result) {
  6542. //>>includeStart('debug', pragmas.debug);
  6543. RuntimeError.Check.typeOf.object("matrix", matrix);
  6544. RuntimeError.Check.typeOf.object("rotation", rotation);
  6545. RuntimeError.Check.typeOf.object("result", result);
  6546. //>>includeEnd('debug');
  6547. const left0 = matrix[0];
  6548. const left1 = matrix[1];
  6549. const left2 = matrix[2];
  6550. const left4 = matrix[4];
  6551. const left5 = matrix[5];
  6552. const left6 = matrix[6];
  6553. const left8 = matrix[8];
  6554. const left9 = matrix[9];
  6555. const left10 = matrix[10];
  6556. const right0 = rotation[0];
  6557. const right1 = rotation[1];
  6558. const right2 = rotation[2];
  6559. const right4 = rotation[3];
  6560. const right5 = rotation[4];
  6561. const right6 = rotation[5];
  6562. const right8 = rotation[6];
  6563. const right9 = rotation[7];
  6564. const right10 = rotation[8];
  6565. const column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6566. const column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6567. const column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6568. const column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6569. const column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6570. const column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6571. const column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6572. const column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6573. const column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6574. result[0] = column0Row0;
  6575. result[1] = column0Row1;
  6576. result[2] = column0Row2;
  6577. result[3] = 0.0;
  6578. result[4] = column1Row0;
  6579. result[5] = column1Row1;
  6580. result[6] = column1Row2;
  6581. result[7] = 0.0;
  6582. result[8] = column2Row0;
  6583. result[9] = column2Row1;
  6584. result[10] = column2Row2;
  6585. result[11] = 0.0;
  6586. result[12] = matrix[12];
  6587. result[13] = matrix[13];
  6588. result[14] = matrix[14];
  6589. result[15] = matrix[15];
  6590. return result;
  6591. };
  6592. /**
  6593. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6594. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  6595. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  6596. *
  6597. * @param {Matrix4} matrix The matrix on the left-hand side.
  6598. * @param {Cartesian3} translation The translation on the right-hand side.
  6599. * @param {Matrix4} result The object onto which to store the result.
  6600. * @returns {Matrix4} The modified result parameter.
  6601. *
  6602. * @example
  6603. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  6604. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  6605. */
  6606. Matrix4.multiplyByTranslation = function (matrix, translation, result) {
  6607. //>>includeStart('debug', pragmas.debug);
  6608. RuntimeError.Check.typeOf.object("matrix", matrix);
  6609. RuntimeError.Check.typeOf.object("translation", translation);
  6610. RuntimeError.Check.typeOf.object("result", result);
  6611. //>>includeEnd('debug');
  6612. const x = translation.x;
  6613. const y = translation.y;
  6614. const z = translation.z;
  6615. const tx = x * matrix[0] + y * matrix[4] + z * matrix[8] + matrix[12];
  6616. const ty = x * matrix[1] + y * matrix[5] + z * matrix[9] + matrix[13];
  6617. const tz = x * matrix[2] + y * matrix[6] + z * matrix[10] + matrix[14];
  6618. result[0] = matrix[0];
  6619. result[1] = matrix[1];
  6620. result[2] = matrix[2];
  6621. result[3] = matrix[3];
  6622. result[4] = matrix[4];
  6623. result[5] = matrix[5];
  6624. result[6] = matrix[6];
  6625. result[7] = matrix[7];
  6626. result[8] = matrix[8];
  6627. result[9] = matrix[9];
  6628. result[10] = matrix[10];
  6629. result[11] = matrix[11];
  6630. result[12] = tx;
  6631. result[13] = ty;
  6632. result[14] = tz;
  6633. result[15] = matrix[15];
  6634. return result;
  6635. };
  6636. /**
  6637. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6638. * by an implicit non-uniform scale matrix. This is an optimization
  6639. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  6640. * <code>m</code> must be an affine matrix.
  6641. * This function performs fewer allocations and arithmetic operations.
  6642. *
  6643. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  6644. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  6645. * @param {Matrix4} result The object onto which to store the result.
  6646. * @returns {Matrix4} The modified result parameter.
  6647. *
  6648. *
  6649. * @example
  6650. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  6651. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  6652. *
  6653. * @see Matrix4.multiplyByUniformScale
  6654. * @see Matrix4.fromScale
  6655. * @see Matrix4.fromUniformScale
  6656. * @see Matrix4.setScale
  6657. * @see Matrix4.setUniformScale
  6658. * @see Matrix4.getScale
  6659. */
  6660. Matrix4.multiplyByScale = function (matrix, scale, result) {
  6661. //>>includeStart('debug', pragmas.debug);
  6662. RuntimeError.Check.typeOf.object("matrix", matrix);
  6663. RuntimeError.Check.typeOf.object("scale", scale);
  6664. RuntimeError.Check.typeOf.object("result", result);
  6665. //>>includeEnd('debug');
  6666. const scaleX = scale.x;
  6667. const scaleY = scale.y;
  6668. const scaleZ = scale.z;
  6669. // Faster than Cartesian3.equals
  6670. if (scaleX === 1.0 && scaleY === 1.0 && scaleZ === 1.0) {
  6671. return Matrix4.clone(matrix, result);
  6672. }
  6673. result[0] = scaleX * matrix[0];
  6674. result[1] = scaleX * matrix[1];
  6675. result[2] = scaleX * matrix[2];
  6676. result[3] = matrix[3];
  6677. result[4] = scaleY * matrix[4];
  6678. result[5] = scaleY * matrix[5];
  6679. result[6] = scaleY * matrix[6];
  6680. result[7] = matrix[7];
  6681. result[8] = scaleZ * matrix[8];
  6682. result[9] = scaleZ * matrix[9];
  6683. result[10] = scaleZ * matrix[10];
  6684. result[11] = matrix[11];
  6685. result[12] = matrix[12];
  6686. result[13] = matrix[13];
  6687. result[14] = matrix[14];
  6688. result[15] = matrix[15];
  6689. return result;
  6690. };
  6691. /**
  6692. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  6693. *
  6694. * @param {Matrix4} matrix The matrix on the left-hand side.
  6695. * @param {Number} scale The uniform scale on the right-hand side.
  6696. * @param {Matrix4} result The object onto which to store the result.
  6697. * @returns {Matrix4} The modified result parameter.
  6698. *
  6699. * @example
  6700. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  6701. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  6702. *
  6703. * @see Matrix4.multiplyByScale
  6704. * @see Matrix4.fromScale
  6705. * @see Matrix4.fromUniformScale
  6706. * @see Matrix4.setScale
  6707. * @see Matrix4.setUniformScale
  6708. * @see Matrix4.getScale
  6709. */
  6710. Matrix4.multiplyByUniformScale = function (matrix, scale, result) {
  6711. //>>includeStart('debug', pragmas.debug);
  6712. RuntimeError.Check.typeOf.object("matrix", matrix);
  6713. RuntimeError.Check.typeOf.number("scale", scale);
  6714. RuntimeError.Check.typeOf.object("result", result);
  6715. //>>includeEnd('debug');
  6716. result[0] = matrix[0] * scale;
  6717. result[1] = matrix[1] * scale;
  6718. result[2] = matrix[2] * scale;
  6719. result[3] = matrix[3];
  6720. result[4] = matrix[4] * scale;
  6721. result[5] = matrix[5] * scale;
  6722. result[6] = matrix[6] * scale;
  6723. result[7] = matrix[7];
  6724. result[8] = matrix[8] * scale;
  6725. result[9] = matrix[9] * scale;
  6726. result[10] = matrix[10] * scale;
  6727. result[11] = matrix[11];
  6728. result[12] = matrix[12];
  6729. result[13] = matrix[13];
  6730. result[14] = matrix[14];
  6731. result[15] = matrix[15];
  6732. return result;
  6733. };
  6734. /**
  6735. * Computes the product of a matrix and a column vector.
  6736. *
  6737. * @param {Matrix4} matrix The matrix.
  6738. * @param {Cartesian4} cartesian The vector.
  6739. * @param {Cartesian4} result The object onto which to store the result.
  6740. * @returns {Cartesian4} The modified result parameter.
  6741. */
  6742. Matrix4.multiplyByVector = function (matrix, cartesian, result) {
  6743. //>>includeStart('debug', pragmas.debug);
  6744. RuntimeError.Check.typeOf.object("matrix", matrix);
  6745. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6746. RuntimeError.Check.typeOf.object("result", result);
  6747. //>>includeEnd('debug');
  6748. const vX = cartesian.x;
  6749. const vY = cartesian.y;
  6750. const vZ = cartesian.z;
  6751. const vW = cartesian.w;
  6752. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  6753. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  6754. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  6755. const w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  6756. result.x = x;
  6757. result.y = y;
  6758. result.z = z;
  6759. result.w = w;
  6760. return result;
  6761. };
  6762. /**
  6763. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  6764. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  6765. *
  6766. * @param {Matrix4} matrix The matrix.
  6767. * @param {Cartesian3} cartesian The point.
  6768. * @param {Cartesian3} result The object onto which to store the result.
  6769. * @returns {Cartesian3} The modified result parameter.
  6770. *
  6771. * @example
  6772. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  6773. * const result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  6774. * // A shortcut for
  6775. * // Cartesian3 p = ...
  6776. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  6777. */
  6778. Matrix4.multiplyByPointAsVector = function (matrix, cartesian, result) {
  6779. //>>includeStart('debug', pragmas.debug);
  6780. RuntimeError.Check.typeOf.object("matrix", matrix);
  6781. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6782. RuntimeError.Check.typeOf.object("result", result);
  6783. //>>includeEnd('debug');
  6784. const vX = cartesian.x;
  6785. const vY = cartesian.y;
  6786. const vZ = cartesian.z;
  6787. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  6788. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  6789. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  6790. result.x = x;
  6791. result.y = y;
  6792. result.z = z;
  6793. return result;
  6794. };
  6795. /**
  6796. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  6797. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  6798. *
  6799. * @param {Matrix4} matrix The matrix.
  6800. * @param {Cartesian3} cartesian The point.
  6801. * @param {Cartesian3} result The object onto which to store the result.
  6802. * @returns {Cartesian3} The modified result parameter.
  6803. *
  6804. * @example
  6805. * const p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  6806. * const result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  6807. */
  6808. Matrix4.multiplyByPoint = function (matrix, cartesian, result) {
  6809. //>>includeStart('debug', pragmas.debug);
  6810. RuntimeError.Check.typeOf.object("matrix", matrix);
  6811. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  6812. RuntimeError.Check.typeOf.object("result", result);
  6813. //>>includeEnd('debug');
  6814. const vX = cartesian.x;
  6815. const vY = cartesian.y;
  6816. const vZ = cartesian.z;
  6817. const x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  6818. const y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  6819. const z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  6820. result.x = x;
  6821. result.y = y;
  6822. result.z = z;
  6823. return result;
  6824. };
  6825. /**
  6826. * Computes the product of a matrix and a scalar.
  6827. *
  6828. * @param {Matrix4} matrix The matrix.
  6829. * @param {Number} scalar The number to multiply by.
  6830. * @param {Matrix4} result The object onto which to store the result.
  6831. * @returns {Matrix4} The modified result parameter.
  6832. *
  6833. * @example
  6834. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  6835. * // m = [10.0, 11.0, 12.0, 13.0]
  6836. * // [14.0, 15.0, 16.0, 17.0]
  6837. * // [18.0, 19.0, 20.0, 21.0]
  6838. * // [22.0, 23.0, 24.0, 25.0]
  6839. *
  6840. * const a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  6841. *
  6842. * // m remains the same
  6843. * // a = [-20.0, -22.0, -24.0, -26.0]
  6844. * // [-28.0, -30.0, -32.0, -34.0]
  6845. * // [-36.0, -38.0, -40.0, -42.0]
  6846. * // [-44.0, -46.0, -48.0, -50.0]
  6847. */
  6848. Matrix4.multiplyByScalar = function (matrix, scalar, result) {
  6849. //>>includeStart('debug', pragmas.debug);
  6850. RuntimeError.Check.typeOf.object("matrix", matrix);
  6851. RuntimeError.Check.typeOf.number("scalar", scalar);
  6852. RuntimeError.Check.typeOf.object("result", result);
  6853. //>>includeEnd('debug');
  6854. result[0] = matrix[0] * scalar;
  6855. result[1] = matrix[1] * scalar;
  6856. result[2] = matrix[2] * scalar;
  6857. result[3] = matrix[3] * scalar;
  6858. result[4] = matrix[4] * scalar;
  6859. result[5] = matrix[5] * scalar;
  6860. result[6] = matrix[6] * scalar;
  6861. result[7] = matrix[7] * scalar;
  6862. result[8] = matrix[8] * scalar;
  6863. result[9] = matrix[9] * scalar;
  6864. result[10] = matrix[10] * scalar;
  6865. result[11] = matrix[11] * scalar;
  6866. result[12] = matrix[12] * scalar;
  6867. result[13] = matrix[13] * scalar;
  6868. result[14] = matrix[14] * scalar;
  6869. result[15] = matrix[15] * scalar;
  6870. return result;
  6871. };
  6872. /**
  6873. * Computes a negated copy of the provided matrix.
  6874. *
  6875. * @param {Matrix4} matrix The matrix to negate.
  6876. * @param {Matrix4} result The object onto which to store the result.
  6877. * @returns {Matrix4} The modified result parameter.
  6878. *
  6879. * @example
  6880. * //create a new Matrix4 instance which is a negation of a Matrix4
  6881. * // m = [10.0, 11.0, 12.0, 13.0]
  6882. * // [14.0, 15.0, 16.0, 17.0]
  6883. * // [18.0, 19.0, 20.0, 21.0]
  6884. * // [22.0, 23.0, 24.0, 25.0]
  6885. *
  6886. * const a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  6887. *
  6888. * // m remains the same
  6889. * // a = [-10.0, -11.0, -12.0, -13.0]
  6890. * // [-14.0, -15.0, -16.0, -17.0]
  6891. * // [-18.0, -19.0, -20.0, -21.0]
  6892. * // [-22.0, -23.0, -24.0, -25.0]
  6893. */
  6894. Matrix4.negate = function (matrix, result) {
  6895. //>>includeStart('debug', pragmas.debug);
  6896. RuntimeError.Check.typeOf.object("matrix", matrix);
  6897. RuntimeError.Check.typeOf.object("result", result);
  6898. //>>includeEnd('debug');
  6899. result[0] = -matrix[0];
  6900. result[1] = -matrix[1];
  6901. result[2] = -matrix[2];
  6902. result[3] = -matrix[3];
  6903. result[4] = -matrix[4];
  6904. result[5] = -matrix[5];
  6905. result[6] = -matrix[6];
  6906. result[7] = -matrix[7];
  6907. result[8] = -matrix[8];
  6908. result[9] = -matrix[9];
  6909. result[10] = -matrix[10];
  6910. result[11] = -matrix[11];
  6911. result[12] = -matrix[12];
  6912. result[13] = -matrix[13];
  6913. result[14] = -matrix[14];
  6914. result[15] = -matrix[15];
  6915. return result;
  6916. };
  6917. /**
  6918. * Computes the transpose of the provided matrix.
  6919. *
  6920. * @param {Matrix4} matrix The matrix to transpose.
  6921. * @param {Matrix4} result The object onto which to store the result.
  6922. * @returns {Matrix4} The modified result parameter.
  6923. *
  6924. * @example
  6925. * //returns transpose of a Matrix4
  6926. * // m = [10.0, 11.0, 12.0, 13.0]
  6927. * // [14.0, 15.0, 16.0, 17.0]
  6928. * // [18.0, 19.0, 20.0, 21.0]
  6929. * // [22.0, 23.0, 24.0, 25.0]
  6930. *
  6931. * const a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  6932. *
  6933. * // m remains the same
  6934. * // a = [10.0, 14.0, 18.0, 22.0]
  6935. * // [11.0, 15.0, 19.0, 23.0]
  6936. * // [12.0, 16.0, 20.0, 24.0]
  6937. * // [13.0, 17.0, 21.0, 25.0]
  6938. */
  6939. Matrix4.transpose = function (matrix, result) {
  6940. //>>includeStart('debug', pragmas.debug);
  6941. RuntimeError.Check.typeOf.object("matrix", matrix);
  6942. RuntimeError.Check.typeOf.object("result", result);
  6943. //>>includeEnd('debug');
  6944. const matrix1 = matrix[1];
  6945. const matrix2 = matrix[2];
  6946. const matrix3 = matrix[3];
  6947. const matrix6 = matrix[6];
  6948. const matrix7 = matrix[7];
  6949. const matrix11 = matrix[11];
  6950. result[0] = matrix[0];
  6951. result[1] = matrix[4];
  6952. result[2] = matrix[8];
  6953. result[3] = matrix[12];
  6954. result[4] = matrix1;
  6955. result[5] = matrix[5];
  6956. result[6] = matrix[9];
  6957. result[7] = matrix[13];
  6958. result[8] = matrix2;
  6959. result[9] = matrix6;
  6960. result[10] = matrix[10];
  6961. result[11] = matrix[14];
  6962. result[12] = matrix3;
  6963. result[13] = matrix7;
  6964. result[14] = matrix11;
  6965. result[15] = matrix[15];
  6966. return result;
  6967. };
  6968. /**
  6969. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  6970. *
  6971. * @param {Matrix4} matrix The matrix with signed elements.
  6972. * @param {Matrix4} result The object onto which to store the result.
  6973. * @returns {Matrix4} The modified result parameter.
  6974. */
  6975. Matrix4.abs = function (matrix, result) {
  6976. //>>includeStart('debug', pragmas.debug);
  6977. RuntimeError.Check.typeOf.object("matrix", matrix);
  6978. RuntimeError.Check.typeOf.object("result", result);
  6979. //>>includeEnd('debug');
  6980. result[0] = Math.abs(matrix[0]);
  6981. result[1] = Math.abs(matrix[1]);
  6982. result[2] = Math.abs(matrix[2]);
  6983. result[3] = Math.abs(matrix[3]);
  6984. result[4] = Math.abs(matrix[4]);
  6985. result[5] = Math.abs(matrix[5]);
  6986. result[6] = Math.abs(matrix[6]);
  6987. result[7] = Math.abs(matrix[7]);
  6988. result[8] = Math.abs(matrix[8]);
  6989. result[9] = Math.abs(matrix[9]);
  6990. result[10] = Math.abs(matrix[10]);
  6991. result[11] = Math.abs(matrix[11]);
  6992. result[12] = Math.abs(matrix[12]);
  6993. result[13] = Math.abs(matrix[13]);
  6994. result[14] = Math.abs(matrix[14]);
  6995. result[15] = Math.abs(matrix[15]);
  6996. return result;
  6997. };
  6998. /**
  6999. * Compares the provided matrices componentwise and returns
  7000. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7001. *
  7002. * @param {Matrix4} [left] The first matrix.
  7003. * @param {Matrix4} [right] The second matrix.
  7004. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  7005. *
  7006. * @example
  7007. * //compares two Matrix4 instances
  7008. *
  7009. * // a = [10.0, 14.0, 18.0, 22.0]
  7010. * // [11.0, 15.0, 19.0, 23.0]
  7011. * // [12.0, 16.0, 20.0, 24.0]
  7012. * // [13.0, 17.0, 21.0, 25.0]
  7013. *
  7014. * // b = [10.0, 14.0, 18.0, 22.0]
  7015. * // [11.0, 15.0, 19.0, 23.0]
  7016. * // [12.0, 16.0, 20.0, 24.0]
  7017. * // [13.0, 17.0, 21.0, 25.0]
  7018. *
  7019. * if(Cesium.Matrix4.equals(a,b)) {
  7020. * console.log("Both matrices are equal");
  7021. * } else {
  7022. * console.log("They are not equal");
  7023. * }
  7024. *
  7025. * //Prints "Both matrices are equal" on the console
  7026. */
  7027. Matrix4.equals = function (left, right) {
  7028. // Given that most matrices will be transformation matrices, the elements
  7029. // are tested in order such that the test is likely to fail as early
  7030. // as possible. I _think_ this is just as friendly to the L1 cache
  7031. // as testing in index order. It is certainty faster in practice.
  7032. return (
  7033. left === right ||
  7034. (defaultValue.defined(left) &&
  7035. defaultValue.defined(right) &&
  7036. // Translation
  7037. left[12] === right[12] &&
  7038. left[13] === right[13] &&
  7039. left[14] === right[14] &&
  7040. // Rotation/scale
  7041. left[0] === right[0] &&
  7042. left[1] === right[1] &&
  7043. left[2] === right[2] &&
  7044. left[4] === right[4] &&
  7045. left[5] === right[5] &&
  7046. left[6] === right[6] &&
  7047. left[8] === right[8] &&
  7048. left[9] === right[9] &&
  7049. left[10] === right[10] &&
  7050. // Bottom row
  7051. left[3] === right[3] &&
  7052. left[7] === right[7] &&
  7053. left[11] === right[11] &&
  7054. left[15] === right[15])
  7055. );
  7056. };
  7057. /**
  7058. * Compares the provided matrices componentwise and returns
  7059. * <code>true</code> if they are within the provided epsilon,
  7060. * <code>false</code> otherwise.
  7061. *
  7062. * @param {Matrix4} [left] The first matrix.
  7063. * @param {Matrix4} [right] The second matrix.
  7064. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7065. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7066. *
  7067. * @example
  7068. * //compares two Matrix4 instances
  7069. *
  7070. * // a = [10.5, 14.5, 18.5, 22.5]
  7071. * // [11.5, 15.5, 19.5, 23.5]
  7072. * // [12.5, 16.5, 20.5, 24.5]
  7073. * // [13.5, 17.5, 21.5, 25.5]
  7074. *
  7075. * // b = [10.0, 14.0, 18.0, 22.0]
  7076. * // [11.0, 15.0, 19.0, 23.0]
  7077. * // [12.0, 16.0, 20.0, 24.0]
  7078. * // [13.0, 17.0, 21.0, 25.0]
  7079. *
  7080. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  7081. * console.log("Difference between both the matrices is less than 0.1");
  7082. * } else {
  7083. * console.log("Difference between both the matrices is not less than 0.1");
  7084. * }
  7085. *
  7086. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  7087. */
  7088. Matrix4.equalsEpsilon = function (left, right, epsilon) {
  7089. epsilon = defaultValue.defaultValue(epsilon, 0);
  7090. return (
  7091. left === right ||
  7092. (defaultValue.defined(left) &&
  7093. defaultValue.defined(right) &&
  7094. Math.abs(left[0] - right[0]) <= epsilon &&
  7095. Math.abs(left[1] - right[1]) <= epsilon &&
  7096. Math.abs(left[2] - right[2]) <= epsilon &&
  7097. Math.abs(left[3] - right[3]) <= epsilon &&
  7098. Math.abs(left[4] - right[4]) <= epsilon &&
  7099. Math.abs(left[5] - right[5]) <= epsilon &&
  7100. Math.abs(left[6] - right[6]) <= epsilon &&
  7101. Math.abs(left[7] - right[7]) <= epsilon &&
  7102. Math.abs(left[8] - right[8]) <= epsilon &&
  7103. Math.abs(left[9] - right[9]) <= epsilon &&
  7104. Math.abs(left[10] - right[10]) <= epsilon &&
  7105. Math.abs(left[11] - right[11]) <= epsilon &&
  7106. Math.abs(left[12] - right[12]) <= epsilon &&
  7107. Math.abs(left[13] - right[13]) <= epsilon &&
  7108. Math.abs(left[14] - right[14]) <= epsilon &&
  7109. Math.abs(left[15] - right[15]) <= epsilon)
  7110. );
  7111. };
  7112. /**
  7113. * Gets the translation portion of the provided matrix, assuming the matrix is an affine transformation matrix.
  7114. *
  7115. * @param {Matrix4} matrix The matrix to use.
  7116. * @param {Cartesian3} result The object onto which to store the result.
  7117. * @returns {Cartesian3} The modified result parameter.
  7118. */
  7119. Matrix4.getTranslation = function (matrix, result) {
  7120. //>>includeStart('debug', pragmas.debug);
  7121. RuntimeError.Check.typeOf.object("matrix", matrix);
  7122. RuntimeError.Check.typeOf.object("result", result);
  7123. //>>includeEnd('debug');
  7124. result.x = matrix[12];
  7125. result.y = matrix[13];
  7126. result.z = matrix[14];
  7127. return result;
  7128. };
  7129. /**
  7130. * Gets the upper left 3x3 matrix of the provided matrix.
  7131. *
  7132. * @param {Matrix4} matrix The matrix to use.
  7133. * @param {Matrix3} result The object onto which to store the result.
  7134. * @returns {Matrix3} The modified result parameter.
  7135. *
  7136. * @example
  7137. * // returns a Matrix3 instance from a Matrix4 instance
  7138. *
  7139. * // m = [10.0, 14.0, 18.0, 22.0]
  7140. * // [11.0, 15.0, 19.0, 23.0]
  7141. * // [12.0, 16.0, 20.0, 24.0]
  7142. * // [13.0, 17.0, 21.0, 25.0]
  7143. *
  7144. * const b = new Cesium.Matrix3();
  7145. * Cesium.Matrix4.getMatrix3(m,b);
  7146. *
  7147. * // b = [10.0, 14.0, 18.0]
  7148. * // [11.0, 15.0, 19.0]
  7149. * // [12.0, 16.0, 20.0]
  7150. */
  7151. Matrix4.getMatrix3 = function (matrix, result) {
  7152. //>>includeStart('debug', pragmas.debug);
  7153. RuntimeError.Check.typeOf.object("matrix", matrix);
  7154. RuntimeError.Check.typeOf.object("result", result);
  7155. //>>includeEnd('debug');
  7156. result[0] = matrix[0];
  7157. result[1] = matrix[1];
  7158. result[2] = matrix[2];
  7159. result[3] = matrix[4];
  7160. result[4] = matrix[5];
  7161. result[5] = matrix[6];
  7162. result[6] = matrix[8];
  7163. result[7] = matrix[9];
  7164. result[8] = matrix[10];
  7165. return result;
  7166. };
  7167. const scratchInverseRotation = new Matrix3();
  7168. const scratchMatrix3Zero = new Matrix3();
  7169. const scratchBottomRow = new Cartesian4();
  7170. const scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  7171. /**
  7172. * Computes the inverse of the provided matrix using Cramers Rule.
  7173. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  7174. * If the matrix is a proper rigid transformation, it is more efficient
  7175. * to invert it with {@link Matrix4.inverseTransformation}.
  7176. *
  7177. * @param {Matrix4} matrix The matrix to invert.
  7178. * @param {Matrix4} result The object onto which to store the result.
  7179. * @returns {Matrix4} The modified result parameter.
  7180. *
  7181. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  7182. */
  7183. Matrix4.inverse = function (matrix, result) {
  7184. //>>includeStart('debug', pragmas.debug);
  7185. RuntimeError.Check.typeOf.object("matrix", matrix);
  7186. RuntimeError.Check.typeOf.object("result", result);
  7187. //>>includeEnd('debug');
  7188. //
  7189. // Ported from:
  7190. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  7191. //
  7192. const src0 = matrix[0];
  7193. const src1 = matrix[4];
  7194. const src2 = matrix[8];
  7195. const src3 = matrix[12];
  7196. const src4 = matrix[1];
  7197. const src5 = matrix[5];
  7198. const src6 = matrix[9];
  7199. const src7 = matrix[13];
  7200. const src8 = matrix[2];
  7201. const src9 = matrix[6];
  7202. const src10 = matrix[10];
  7203. const src11 = matrix[14];
  7204. const src12 = matrix[3];
  7205. const src13 = matrix[7];
  7206. const src14 = matrix[11];
  7207. const src15 = matrix[15];
  7208. // calculate pairs for first 8 elements (cofactors)
  7209. let tmp0 = src10 * src15;
  7210. let tmp1 = src11 * src14;
  7211. let tmp2 = src9 * src15;
  7212. let tmp3 = src11 * src13;
  7213. let tmp4 = src9 * src14;
  7214. let tmp5 = src10 * src13;
  7215. let tmp6 = src8 * src15;
  7216. let tmp7 = src11 * src12;
  7217. let tmp8 = src8 * src14;
  7218. let tmp9 = src10 * src12;
  7219. let tmp10 = src8 * src13;
  7220. let tmp11 = src9 * src12;
  7221. // calculate first 8 elements (cofactors)
  7222. const dst0 =
  7223. tmp0 * src5 +
  7224. tmp3 * src6 +
  7225. tmp4 * src7 -
  7226. (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  7227. const dst1 =
  7228. tmp1 * src4 +
  7229. tmp6 * src6 +
  7230. tmp9 * src7 -
  7231. (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  7232. const dst2 =
  7233. tmp2 * src4 +
  7234. tmp7 * src5 +
  7235. tmp10 * src7 -
  7236. (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  7237. const dst3 =
  7238. tmp5 * src4 +
  7239. tmp8 * src5 +
  7240. tmp11 * src6 -
  7241. (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  7242. const dst4 =
  7243. tmp1 * src1 +
  7244. tmp2 * src2 +
  7245. tmp5 * src3 -
  7246. (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  7247. const dst5 =
  7248. tmp0 * src0 +
  7249. tmp7 * src2 +
  7250. tmp8 * src3 -
  7251. (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  7252. const dst6 =
  7253. tmp3 * src0 +
  7254. tmp6 * src1 +
  7255. tmp11 * src3 -
  7256. (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  7257. const dst7 =
  7258. tmp4 * src0 +
  7259. tmp9 * src1 +
  7260. tmp10 * src2 -
  7261. (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  7262. // calculate pairs for second 8 elements (cofactors)
  7263. tmp0 = src2 * src7;
  7264. tmp1 = src3 * src6;
  7265. tmp2 = src1 * src7;
  7266. tmp3 = src3 * src5;
  7267. tmp4 = src1 * src6;
  7268. tmp5 = src2 * src5;
  7269. tmp6 = src0 * src7;
  7270. tmp7 = src3 * src4;
  7271. tmp8 = src0 * src6;
  7272. tmp9 = src2 * src4;
  7273. tmp10 = src0 * src5;
  7274. tmp11 = src1 * src4;
  7275. // calculate second 8 elements (cofactors)
  7276. const dst8 =
  7277. tmp0 * src13 +
  7278. tmp3 * src14 +
  7279. tmp4 * src15 -
  7280. (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  7281. const dst9 =
  7282. tmp1 * src12 +
  7283. tmp6 * src14 +
  7284. tmp9 * src15 -
  7285. (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  7286. const dst10 =
  7287. tmp2 * src12 +
  7288. tmp7 * src13 +
  7289. tmp10 * src15 -
  7290. (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  7291. const dst11 =
  7292. tmp5 * src12 +
  7293. tmp8 * src13 +
  7294. tmp11 * src14 -
  7295. (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  7296. const dst12 =
  7297. tmp2 * src10 +
  7298. tmp5 * src11 +
  7299. tmp1 * src9 -
  7300. (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  7301. const dst13 =
  7302. tmp8 * src11 +
  7303. tmp0 * src8 +
  7304. tmp7 * src10 -
  7305. (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  7306. const dst14 =
  7307. tmp6 * src9 +
  7308. tmp11 * src11 +
  7309. tmp3 * src8 -
  7310. (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  7311. const dst15 =
  7312. tmp10 * src10 +
  7313. tmp4 * src8 +
  7314. tmp9 * src9 -
  7315. (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  7316. // calculate determinant
  7317. let det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  7318. if (Math.abs(det) < ComponentDatatype.CesiumMath.EPSILON21) {
  7319. // Special case for a zero scale matrix that can occur, for example,
  7320. // when a model's node has a [0, 0, 0] scale.
  7321. if (
  7322. Matrix3.equalsEpsilon(
  7323. Matrix4.getMatrix3(matrix, scratchInverseRotation),
  7324. scratchMatrix3Zero,
  7325. ComponentDatatype.CesiumMath.EPSILON7
  7326. ) &&
  7327. Cartesian4.equals(
  7328. Matrix4.getRow(matrix, 3, scratchBottomRow),
  7329. scratchExpectedBottomRow
  7330. )
  7331. ) {
  7332. result[0] = 0.0;
  7333. result[1] = 0.0;
  7334. result[2] = 0.0;
  7335. result[3] = 0.0;
  7336. result[4] = 0.0;
  7337. result[5] = 0.0;
  7338. result[6] = 0.0;
  7339. result[7] = 0.0;
  7340. result[8] = 0.0;
  7341. result[9] = 0.0;
  7342. result[10] = 0.0;
  7343. result[11] = 0.0;
  7344. result[12] = -matrix[12];
  7345. result[13] = -matrix[13];
  7346. result[14] = -matrix[14];
  7347. result[15] = 1.0;
  7348. return result;
  7349. }
  7350. throw new RuntimeError.RuntimeError(
  7351. "matrix is not invertible because its determinate is zero."
  7352. );
  7353. }
  7354. // calculate matrix inverse
  7355. det = 1.0 / det;
  7356. result[0] = dst0 * det;
  7357. result[1] = dst1 * det;
  7358. result[2] = dst2 * det;
  7359. result[3] = dst3 * det;
  7360. result[4] = dst4 * det;
  7361. result[5] = dst5 * det;
  7362. result[6] = dst6 * det;
  7363. result[7] = dst7 * det;
  7364. result[8] = dst8 * det;
  7365. result[9] = dst9 * det;
  7366. result[10] = dst10 * det;
  7367. result[11] = dst11 * det;
  7368. result[12] = dst12 * det;
  7369. result[13] = dst13 * det;
  7370. result[14] = dst14 * det;
  7371. result[15] = dst15 * det;
  7372. return result;
  7373. };
  7374. /**
  7375. * Computes the inverse of the provided matrix assuming it is a proper rigid matrix,
  7376. * where the upper left 3x3 elements are a rotation matrix,
  7377. * and the upper three elements in the fourth column are the translation.
  7378. * The bottom row is assumed to be [0, 0, 0, 1].
  7379. * The matrix is not verified to be in the proper form.
  7380. * This method is faster than computing the inverse for a general 4x4
  7381. * matrix using {@link Matrix4.inverse}.
  7382. *
  7383. * @param {Matrix4} matrix The matrix to invert.
  7384. * @param {Matrix4} result The object onto which to store the result.
  7385. * @returns {Matrix4} The modified result parameter.
  7386. */
  7387. Matrix4.inverseTransformation = function (matrix, result) {
  7388. //>>includeStart('debug', pragmas.debug);
  7389. RuntimeError.Check.typeOf.object("matrix", matrix);
  7390. RuntimeError.Check.typeOf.object("result", result);
  7391. //>>includeEnd('debug');
  7392. //This function is an optimized version of the below 4 lines.
  7393. //const rT = Matrix3.transpose(Matrix4.getMatrix3(matrix));
  7394. //const rTN = Matrix3.negate(rT);
  7395. //const rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  7396. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  7397. const matrix0 = matrix[0];
  7398. const matrix1 = matrix[1];
  7399. const matrix2 = matrix[2];
  7400. const matrix4 = matrix[4];
  7401. const matrix5 = matrix[5];
  7402. const matrix6 = matrix[6];
  7403. const matrix8 = matrix[8];
  7404. const matrix9 = matrix[9];
  7405. const matrix10 = matrix[10];
  7406. const vX = matrix[12];
  7407. const vY = matrix[13];
  7408. const vZ = matrix[14];
  7409. const x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  7410. const y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  7411. const z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  7412. result[0] = matrix0;
  7413. result[1] = matrix4;
  7414. result[2] = matrix8;
  7415. result[3] = 0.0;
  7416. result[4] = matrix1;
  7417. result[5] = matrix5;
  7418. result[6] = matrix9;
  7419. result[7] = 0.0;
  7420. result[8] = matrix2;
  7421. result[9] = matrix6;
  7422. result[10] = matrix10;
  7423. result[11] = 0.0;
  7424. result[12] = x;
  7425. result[13] = y;
  7426. result[14] = z;
  7427. result[15] = 1.0;
  7428. return result;
  7429. };
  7430. const scratchTransposeMatrix = new Matrix4();
  7431. /**
  7432. * Computes the inverse transpose of a matrix.
  7433. *
  7434. * @param {Matrix4} matrix The matrix to transpose and invert.
  7435. * @param {Matrix4} result The object onto which to store the result.
  7436. * @returns {Matrix4} The modified result parameter.
  7437. */
  7438. Matrix4.inverseTranspose = function (matrix, result) {
  7439. //>>includeStart('debug', pragmas.debug);
  7440. RuntimeError.Check.typeOf.object("matrix", matrix);
  7441. RuntimeError.Check.typeOf.object("result", result);
  7442. //>>includeEnd('debug');
  7443. return Matrix4.inverse(
  7444. Matrix4.transpose(matrix, scratchTransposeMatrix),
  7445. result
  7446. );
  7447. };
  7448. /**
  7449. * An immutable Matrix4 instance initialized to the identity matrix.
  7450. *
  7451. * @type {Matrix4}
  7452. * @constant
  7453. */
  7454. Matrix4.IDENTITY = Object.freeze(
  7455. new Matrix4(
  7456. 1.0,
  7457. 0.0,
  7458. 0.0,
  7459. 0.0,
  7460. 0.0,
  7461. 1.0,
  7462. 0.0,
  7463. 0.0,
  7464. 0.0,
  7465. 0.0,
  7466. 1.0,
  7467. 0.0,
  7468. 0.0,
  7469. 0.0,
  7470. 0.0,
  7471. 1.0
  7472. )
  7473. );
  7474. /**
  7475. * An immutable Matrix4 instance initialized to the zero matrix.
  7476. *
  7477. * @type {Matrix4}
  7478. * @constant
  7479. */
  7480. Matrix4.ZERO = Object.freeze(
  7481. new Matrix4(
  7482. 0.0,
  7483. 0.0,
  7484. 0.0,
  7485. 0.0,
  7486. 0.0,
  7487. 0.0,
  7488. 0.0,
  7489. 0.0,
  7490. 0.0,
  7491. 0.0,
  7492. 0.0,
  7493. 0.0,
  7494. 0.0,
  7495. 0.0,
  7496. 0.0,
  7497. 0.0
  7498. )
  7499. );
  7500. /**
  7501. * The index into Matrix4 for column 0, row 0.
  7502. *
  7503. * @type {Number}
  7504. * @constant
  7505. */
  7506. Matrix4.COLUMN0ROW0 = 0;
  7507. /**
  7508. * The index into Matrix4 for column 0, row 1.
  7509. *
  7510. * @type {Number}
  7511. * @constant
  7512. */
  7513. Matrix4.COLUMN0ROW1 = 1;
  7514. /**
  7515. * The index into Matrix4 for column 0, row 2.
  7516. *
  7517. * @type {Number}
  7518. * @constant
  7519. */
  7520. Matrix4.COLUMN0ROW2 = 2;
  7521. /**
  7522. * The index into Matrix4 for column 0, row 3.
  7523. *
  7524. * @type {Number}
  7525. * @constant
  7526. */
  7527. Matrix4.COLUMN0ROW3 = 3;
  7528. /**
  7529. * The index into Matrix4 for column 1, row 0.
  7530. *
  7531. * @type {Number}
  7532. * @constant
  7533. */
  7534. Matrix4.COLUMN1ROW0 = 4;
  7535. /**
  7536. * The index into Matrix4 for column 1, row 1.
  7537. *
  7538. * @type {Number}
  7539. * @constant
  7540. */
  7541. Matrix4.COLUMN1ROW1 = 5;
  7542. /**
  7543. * The index into Matrix4 for column 1, row 2.
  7544. *
  7545. * @type {Number}
  7546. * @constant
  7547. */
  7548. Matrix4.COLUMN1ROW2 = 6;
  7549. /**
  7550. * The index into Matrix4 for column 1, row 3.
  7551. *
  7552. * @type {Number}
  7553. * @constant
  7554. */
  7555. Matrix4.COLUMN1ROW3 = 7;
  7556. /**
  7557. * The index into Matrix4 for column 2, row 0.
  7558. *
  7559. * @type {Number}
  7560. * @constant
  7561. */
  7562. Matrix4.COLUMN2ROW0 = 8;
  7563. /**
  7564. * The index into Matrix4 for column 2, row 1.
  7565. *
  7566. * @type {Number}
  7567. * @constant
  7568. */
  7569. Matrix4.COLUMN2ROW1 = 9;
  7570. /**
  7571. * The index into Matrix4 for column 2, row 2.
  7572. *
  7573. * @type {Number}
  7574. * @constant
  7575. */
  7576. Matrix4.COLUMN2ROW2 = 10;
  7577. /**
  7578. * The index into Matrix4 for column 2, row 3.
  7579. *
  7580. * @type {Number}
  7581. * @constant
  7582. */
  7583. Matrix4.COLUMN2ROW3 = 11;
  7584. /**
  7585. * The index into Matrix4 for column 3, row 0.
  7586. *
  7587. * @type {Number}
  7588. * @constant
  7589. */
  7590. Matrix4.COLUMN3ROW0 = 12;
  7591. /**
  7592. * The index into Matrix4 for column 3, row 1.
  7593. *
  7594. * @type {Number}
  7595. * @constant
  7596. */
  7597. Matrix4.COLUMN3ROW1 = 13;
  7598. /**
  7599. * The index into Matrix4 for column 3, row 2.
  7600. *
  7601. * @type {Number}
  7602. * @constant
  7603. */
  7604. Matrix4.COLUMN3ROW2 = 14;
  7605. /**
  7606. * The index into Matrix4 for column 3, row 3.
  7607. *
  7608. * @type {Number}
  7609. * @constant
  7610. */
  7611. Matrix4.COLUMN3ROW3 = 15;
  7612. Object.defineProperties(Matrix4.prototype, {
  7613. /**
  7614. * Gets the number of items in the collection.
  7615. * @memberof Matrix4.prototype
  7616. *
  7617. * @type {Number}
  7618. */
  7619. length: {
  7620. get: function () {
  7621. return Matrix4.packedLength;
  7622. },
  7623. },
  7624. });
  7625. /**
  7626. * Duplicates the provided Matrix4 instance.
  7627. *
  7628. * @param {Matrix4} [result] The object onto which to store the result.
  7629. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  7630. */
  7631. Matrix4.prototype.clone = function (result) {
  7632. return Matrix4.clone(this, result);
  7633. };
  7634. /**
  7635. * Compares this matrix to the provided matrix componentwise and returns
  7636. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7637. *
  7638. * @param {Matrix4} [right] The right hand side matrix.
  7639. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  7640. */
  7641. Matrix4.prototype.equals = function (right) {
  7642. return Matrix4.equals(this, right);
  7643. };
  7644. /**
  7645. * @private
  7646. */
  7647. Matrix4.equalsArray = function (matrix, array, offset) {
  7648. return (
  7649. matrix[0] === array[offset] &&
  7650. matrix[1] === array[offset + 1] &&
  7651. matrix[2] === array[offset + 2] &&
  7652. matrix[3] === array[offset + 3] &&
  7653. matrix[4] === array[offset + 4] &&
  7654. matrix[5] === array[offset + 5] &&
  7655. matrix[6] === array[offset + 6] &&
  7656. matrix[7] === array[offset + 7] &&
  7657. matrix[8] === array[offset + 8] &&
  7658. matrix[9] === array[offset + 9] &&
  7659. matrix[10] === array[offset + 10] &&
  7660. matrix[11] === array[offset + 11] &&
  7661. matrix[12] === array[offset + 12] &&
  7662. matrix[13] === array[offset + 13] &&
  7663. matrix[14] === array[offset + 14] &&
  7664. matrix[15] === array[offset + 15]
  7665. );
  7666. };
  7667. /**
  7668. * Compares this matrix to the provided matrix componentwise and returns
  7669. * <code>true</code> if they are within the provided epsilon,
  7670. * <code>false</code> otherwise.
  7671. *
  7672. * @param {Matrix4} [right] The right hand side matrix.
  7673. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7674. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  7675. */
  7676. Matrix4.prototype.equalsEpsilon = function (right, epsilon) {
  7677. return Matrix4.equalsEpsilon(this, right, epsilon);
  7678. };
  7679. /**
  7680. * Computes a string representing this Matrix with each row being
  7681. * on a separate line and in the format '(column0, column1, column2, column3)'.
  7682. *
  7683. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  7684. */
  7685. Matrix4.prototype.toString = function () {
  7686. return (
  7687. `(${this[0]}, ${this[4]}, ${this[8]}, ${this[12]})\n` +
  7688. `(${this[1]}, ${this[5]}, ${this[9]}, ${this[13]})\n` +
  7689. `(${this[2]}, ${this[6]}, ${this[10]}, ${this[14]})\n` +
  7690. `(${this[3]}, ${this[7]}, ${this[11]}, ${this[15]})`
  7691. );
  7692. };
  7693. /**
  7694. * A two dimensional region specified as longitude and latitude coordinates.
  7695. *
  7696. * @alias Rectangle
  7697. * @constructor
  7698. *
  7699. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  7700. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  7701. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  7702. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  7703. *
  7704. * @see Packable
  7705. */
  7706. function Rectangle(west, south, east, north) {
  7707. /**
  7708. * The westernmost longitude in radians in the range [-Pi, Pi].
  7709. *
  7710. * @type {Number}
  7711. * @default 0.0
  7712. */
  7713. this.west = defaultValue.defaultValue(west, 0.0);
  7714. /**
  7715. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  7716. *
  7717. * @type {Number}
  7718. * @default 0.0
  7719. */
  7720. this.south = defaultValue.defaultValue(south, 0.0);
  7721. /**
  7722. * The easternmost longitude in radians in the range [-Pi, Pi].
  7723. *
  7724. * @type {Number}
  7725. * @default 0.0
  7726. */
  7727. this.east = defaultValue.defaultValue(east, 0.0);
  7728. /**
  7729. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  7730. *
  7731. * @type {Number}
  7732. * @default 0.0
  7733. */
  7734. this.north = defaultValue.defaultValue(north, 0.0);
  7735. }
  7736. Object.defineProperties(Rectangle.prototype, {
  7737. /**
  7738. * Gets the width of the rectangle in radians.
  7739. * @memberof Rectangle.prototype
  7740. * @type {Number}
  7741. * @readonly
  7742. */
  7743. width: {
  7744. get: function () {
  7745. return Rectangle.computeWidth(this);
  7746. },
  7747. },
  7748. /**
  7749. * Gets the height of the rectangle in radians.
  7750. * @memberof Rectangle.prototype
  7751. * @type {Number}
  7752. * @readonly
  7753. */
  7754. height: {
  7755. get: function () {
  7756. return Rectangle.computeHeight(this);
  7757. },
  7758. },
  7759. });
  7760. /**
  7761. * The number of elements used to pack the object into an array.
  7762. * @type {Number}
  7763. */
  7764. Rectangle.packedLength = 4;
  7765. /**
  7766. * Stores the provided instance into the provided array.
  7767. *
  7768. * @param {Rectangle} value The value to pack.
  7769. * @param {Number[]} array The array to pack into.
  7770. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  7771. *
  7772. * @returns {Number[]} The array that was packed into
  7773. */
  7774. Rectangle.pack = function (value, array, startingIndex) {
  7775. //>>includeStart('debug', pragmas.debug);
  7776. RuntimeError.Check.typeOf.object("value", value);
  7777. RuntimeError.Check.defined("array", array);
  7778. //>>includeEnd('debug');
  7779. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  7780. array[startingIndex++] = value.west;
  7781. array[startingIndex++] = value.south;
  7782. array[startingIndex++] = value.east;
  7783. array[startingIndex] = value.north;
  7784. return array;
  7785. };
  7786. /**
  7787. * Retrieves an instance from a packed array.
  7788. *
  7789. * @param {Number[]} array The packed array.
  7790. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  7791. * @param {Rectangle} [result] The object into which to store the result.
  7792. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  7793. */
  7794. Rectangle.unpack = function (array, startingIndex, result) {
  7795. //>>includeStart('debug', pragmas.debug);
  7796. RuntimeError.Check.defined("array", array);
  7797. //>>includeEnd('debug');
  7798. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  7799. if (!defaultValue.defined(result)) {
  7800. result = new Rectangle();
  7801. }
  7802. result.west = array[startingIndex++];
  7803. result.south = array[startingIndex++];
  7804. result.east = array[startingIndex++];
  7805. result.north = array[startingIndex];
  7806. return result;
  7807. };
  7808. /**
  7809. * Computes the width of a rectangle in radians.
  7810. * @param {Rectangle} rectangle The rectangle to compute the width of.
  7811. * @returns {Number} The width.
  7812. */
  7813. Rectangle.computeWidth = function (rectangle) {
  7814. //>>includeStart('debug', pragmas.debug);
  7815. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  7816. //>>includeEnd('debug');
  7817. let east = rectangle.east;
  7818. const west = rectangle.west;
  7819. if (east < west) {
  7820. east += ComponentDatatype.CesiumMath.TWO_PI;
  7821. }
  7822. return east - west;
  7823. };
  7824. /**
  7825. * Computes the height of a rectangle in radians.
  7826. * @param {Rectangle} rectangle The rectangle to compute the height of.
  7827. * @returns {Number} The height.
  7828. */
  7829. Rectangle.computeHeight = function (rectangle) {
  7830. //>>includeStart('debug', pragmas.debug);
  7831. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  7832. //>>includeEnd('debug');
  7833. return rectangle.north - rectangle.south;
  7834. };
  7835. /**
  7836. * Creates a rectangle given the boundary longitude and latitude in degrees.
  7837. *
  7838. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  7839. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  7840. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  7841. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  7842. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7843. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7844. *
  7845. * @example
  7846. * const rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  7847. */
  7848. Rectangle.fromDegrees = function (west, south, east, north, result) {
  7849. west = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(west, 0.0));
  7850. south = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(south, 0.0));
  7851. east = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(east, 0.0));
  7852. north = ComponentDatatype.CesiumMath.toRadians(defaultValue.defaultValue(north, 0.0));
  7853. if (!defaultValue.defined(result)) {
  7854. return new Rectangle(west, south, east, north);
  7855. }
  7856. result.west = west;
  7857. result.south = south;
  7858. result.east = east;
  7859. result.north = north;
  7860. return result;
  7861. };
  7862. /**
  7863. * Creates a rectangle given the boundary longitude and latitude in radians.
  7864. *
  7865. * @param {Number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
  7866. * @param {Number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  7867. * @param {Number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
  7868. * @param {Number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  7869. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7870. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7871. *
  7872. * @example
  7873. * const rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
  7874. */
  7875. Rectangle.fromRadians = function (west, south, east, north, result) {
  7876. if (!defaultValue.defined(result)) {
  7877. return new Rectangle(west, south, east, north);
  7878. }
  7879. result.west = defaultValue.defaultValue(west, 0.0);
  7880. result.south = defaultValue.defaultValue(south, 0.0);
  7881. result.east = defaultValue.defaultValue(east, 0.0);
  7882. result.north = defaultValue.defaultValue(north, 0.0);
  7883. return result;
  7884. };
  7885. /**
  7886. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  7887. *
  7888. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  7889. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7890. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7891. */
  7892. Rectangle.fromCartographicArray = function (cartographics, result) {
  7893. //>>includeStart('debug', pragmas.debug);
  7894. RuntimeError.Check.defined("cartographics", cartographics);
  7895. //>>includeEnd('debug');
  7896. let west = Number.MAX_VALUE;
  7897. let east = -Number.MAX_VALUE;
  7898. let westOverIDL = Number.MAX_VALUE;
  7899. let eastOverIDL = -Number.MAX_VALUE;
  7900. let south = Number.MAX_VALUE;
  7901. let north = -Number.MAX_VALUE;
  7902. for (let i = 0, len = cartographics.length; i < len; i++) {
  7903. const position = cartographics[i];
  7904. west = Math.min(west, position.longitude);
  7905. east = Math.max(east, position.longitude);
  7906. south = Math.min(south, position.latitude);
  7907. north = Math.max(north, position.latitude);
  7908. const lonAdjusted =
  7909. position.longitude >= 0
  7910. ? position.longitude
  7911. : position.longitude + ComponentDatatype.CesiumMath.TWO_PI;
  7912. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  7913. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  7914. }
  7915. if (east - west > eastOverIDL - westOverIDL) {
  7916. west = westOverIDL;
  7917. east = eastOverIDL;
  7918. if (east > ComponentDatatype.CesiumMath.PI) {
  7919. east = east - ComponentDatatype.CesiumMath.TWO_PI;
  7920. }
  7921. if (west > ComponentDatatype.CesiumMath.PI) {
  7922. west = west - ComponentDatatype.CesiumMath.TWO_PI;
  7923. }
  7924. }
  7925. if (!defaultValue.defined(result)) {
  7926. return new Rectangle(west, south, east, north);
  7927. }
  7928. result.west = west;
  7929. result.south = south;
  7930. result.east = east;
  7931. result.north = north;
  7932. return result;
  7933. };
  7934. /**
  7935. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  7936. *
  7937. * @param {Cartesian3[]} cartesians The list of Cartesian instances.
  7938. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  7939. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7940. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  7941. */
  7942. Rectangle.fromCartesianArray = function (cartesians, ellipsoid, result) {
  7943. //>>includeStart('debug', pragmas.debug);
  7944. RuntimeError.Check.defined("cartesians", cartesians);
  7945. //>>includeEnd('debug');
  7946. ellipsoid = defaultValue.defaultValue(ellipsoid, Ellipsoid.WGS84);
  7947. let west = Number.MAX_VALUE;
  7948. let east = -Number.MAX_VALUE;
  7949. let westOverIDL = Number.MAX_VALUE;
  7950. let eastOverIDL = -Number.MAX_VALUE;
  7951. let south = Number.MAX_VALUE;
  7952. let north = -Number.MAX_VALUE;
  7953. for (let i = 0, len = cartesians.length; i < len; i++) {
  7954. const position = ellipsoid.cartesianToCartographic(cartesians[i]);
  7955. west = Math.min(west, position.longitude);
  7956. east = Math.max(east, position.longitude);
  7957. south = Math.min(south, position.latitude);
  7958. north = Math.max(north, position.latitude);
  7959. const lonAdjusted =
  7960. position.longitude >= 0
  7961. ? position.longitude
  7962. : position.longitude + ComponentDatatype.CesiumMath.TWO_PI;
  7963. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  7964. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  7965. }
  7966. if (east - west > eastOverIDL - westOverIDL) {
  7967. west = westOverIDL;
  7968. east = eastOverIDL;
  7969. if (east > ComponentDatatype.CesiumMath.PI) {
  7970. east = east - ComponentDatatype.CesiumMath.TWO_PI;
  7971. }
  7972. if (west > ComponentDatatype.CesiumMath.PI) {
  7973. west = west - ComponentDatatype.CesiumMath.TWO_PI;
  7974. }
  7975. }
  7976. if (!defaultValue.defined(result)) {
  7977. return new Rectangle(west, south, east, north);
  7978. }
  7979. result.west = west;
  7980. result.south = south;
  7981. result.east = east;
  7982. result.north = north;
  7983. return result;
  7984. };
  7985. /**
  7986. * Duplicates a Rectangle.
  7987. *
  7988. * @param {Rectangle} rectangle The rectangle to clone.
  7989. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  7990. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  7991. */
  7992. Rectangle.clone = function (rectangle, result) {
  7993. if (!defaultValue.defined(rectangle)) {
  7994. return undefined;
  7995. }
  7996. if (!defaultValue.defined(result)) {
  7997. return new Rectangle(
  7998. rectangle.west,
  7999. rectangle.south,
  8000. rectangle.east,
  8001. rectangle.north
  8002. );
  8003. }
  8004. result.west = rectangle.west;
  8005. result.south = rectangle.south;
  8006. result.east = rectangle.east;
  8007. result.north = rectangle.north;
  8008. return result;
  8009. };
  8010. /**
  8011. * Compares the provided Rectangles componentwise and returns
  8012. * <code>true</code> if they pass an absolute or relative tolerance test,
  8013. * <code>false</code> otherwise.
  8014. *
  8015. * @param {Rectangle} [left] The first Rectangle.
  8016. * @param {Rectangle} [right] The second Rectangle.
  8017. * @param {Number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
  8018. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  8019. */
  8020. Rectangle.equalsEpsilon = function (left, right, absoluteEpsilon) {
  8021. absoluteEpsilon = defaultValue.defaultValue(absoluteEpsilon, 0);
  8022. return (
  8023. left === right ||
  8024. (defaultValue.defined(left) &&
  8025. defaultValue.defined(right) &&
  8026. Math.abs(left.west - right.west) <= absoluteEpsilon &&
  8027. Math.abs(left.south - right.south) <= absoluteEpsilon &&
  8028. Math.abs(left.east - right.east) <= absoluteEpsilon &&
  8029. Math.abs(left.north - right.north) <= absoluteEpsilon)
  8030. );
  8031. };
  8032. /**
  8033. * Duplicates this Rectangle.
  8034. *
  8035. * @param {Rectangle} [result] The object onto which to store the result.
  8036. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8037. */
  8038. Rectangle.prototype.clone = function (result) {
  8039. return Rectangle.clone(this, result);
  8040. };
  8041. /**
  8042. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8043. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8044. *
  8045. * @param {Rectangle} [other] The Rectangle to compare.
  8046. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  8047. */
  8048. Rectangle.prototype.equals = function (other) {
  8049. return Rectangle.equals(this, other);
  8050. };
  8051. /**
  8052. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  8053. * <code>false</code> otherwise.
  8054. *
  8055. * @param {Rectangle} [left] The first Rectangle.
  8056. * @param {Rectangle} [right] The second Rectangle.
  8057. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  8058. */
  8059. Rectangle.equals = function (left, right) {
  8060. return (
  8061. left === right ||
  8062. (defaultValue.defined(left) &&
  8063. defaultValue.defined(right) &&
  8064. left.west === right.west &&
  8065. left.south === right.south &&
  8066. left.east === right.east &&
  8067. left.north === right.north)
  8068. );
  8069. };
  8070. /**
  8071. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8072. * <code>true</code> if they are within the provided epsilon,
  8073. * <code>false</code> otherwise.
  8074. *
  8075. * @param {Rectangle} [other] The Rectangle to compare.
  8076. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  8077. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  8078. */
  8079. Rectangle.prototype.equalsEpsilon = function (other, epsilon) {
  8080. return Rectangle.equalsEpsilon(this, other, epsilon);
  8081. };
  8082. /**
  8083. * Checks a Rectangle's properties and throws if they are not in valid ranges.
  8084. *
  8085. * @param {Rectangle} rectangle The rectangle to validate
  8086. *
  8087. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8088. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8089. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8090. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8091. */
  8092. Rectangle.validate = function (rectangle) {
  8093. //>>includeStart('debug', pragmas.debug);
  8094. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8095. const north = rectangle.north;
  8096. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  8097. "north",
  8098. north,
  8099. -ComponentDatatype.CesiumMath.PI_OVER_TWO
  8100. );
  8101. RuntimeError.Check.typeOf.number.lessThanOrEquals("north", north, ComponentDatatype.CesiumMath.PI_OVER_TWO);
  8102. const south = rectangle.south;
  8103. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  8104. "south",
  8105. south,
  8106. -ComponentDatatype.CesiumMath.PI_OVER_TWO
  8107. );
  8108. RuntimeError.Check.typeOf.number.lessThanOrEquals("south", south, ComponentDatatype.CesiumMath.PI_OVER_TWO);
  8109. const west = rectangle.west;
  8110. RuntimeError.Check.typeOf.number.greaterThanOrEquals("west", west, -Math.PI);
  8111. RuntimeError.Check.typeOf.number.lessThanOrEquals("west", west, Math.PI);
  8112. const east = rectangle.east;
  8113. RuntimeError.Check.typeOf.number.greaterThanOrEquals("east", east, -Math.PI);
  8114. RuntimeError.Check.typeOf.number.lessThanOrEquals("east", east, Math.PI);
  8115. //>>includeEnd('debug');
  8116. };
  8117. /**
  8118. * Computes the southwest corner of a rectangle.
  8119. *
  8120. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8121. * @param {Cartographic} [result] The object onto which to store the result.
  8122. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8123. */
  8124. Rectangle.southwest = function (rectangle, result) {
  8125. //>>includeStart('debug', pragmas.debug);
  8126. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8127. //>>includeEnd('debug');
  8128. if (!defaultValue.defined(result)) {
  8129. return new Cartographic(rectangle.west, rectangle.south);
  8130. }
  8131. result.longitude = rectangle.west;
  8132. result.latitude = rectangle.south;
  8133. result.height = 0.0;
  8134. return result;
  8135. };
  8136. /**
  8137. * Computes the northwest corner of a rectangle.
  8138. *
  8139. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8140. * @param {Cartographic} [result] The object onto which to store the result.
  8141. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8142. */
  8143. Rectangle.northwest = function (rectangle, result) {
  8144. //>>includeStart('debug', pragmas.debug);
  8145. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8146. //>>includeEnd('debug');
  8147. if (!defaultValue.defined(result)) {
  8148. return new Cartographic(rectangle.west, rectangle.north);
  8149. }
  8150. result.longitude = rectangle.west;
  8151. result.latitude = rectangle.north;
  8152. result.height = 0.0;
  8153. return result;
  8154. };
  8155. /**
  8156. * Computes the northeast corner of a rectangle.
  8157. *
  8158. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8159. * @param {Cartographic} [result] The object onto which to store the result.
  8160. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8161. */
  8162. Rectangle.northeast = function (rectangle, result) {
  8163. //>>includeStart('debug', pragmas.debug);
  8164. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8165. //>>includeEnd('debug');
  8166. if (!defaultValue.defined(result)) {
  8167. return new Cartographic(rectangle.east, rectangle.north);
  8168. }
  8169. result.longitude = rectangle.east;
  8170. result.latitude = rectangle.north;
  8171. result.height = 0.0;
  8172. return result;
  8173. };
  8174. /**
  8175. * Computes the southeast corner of a rectangle.
  8176. *
  8177. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8178. * @param {Cartographic} [result] The object onto which to store the result.
  8179. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8180. */
  8181. Rectangle.southeast = function (rectangle, result) {
  8182. //>>includeStart('debug', pragmas.debug);
  8183. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8184. //>>includeEnd('debug');
  8185. if (!defaultValue.defined(result)) {
  8186. return new Cartographic(rectangle.east, rectangle.south);
  8187. }
  8188. result.longitude = rectangle.east;
  8189. result.latitude = rectangle.south;
  8190. result.height = 0.0;
  8191. return result;
  8192. };
  8193. /**
  8194. * Computes the center of a rectangle.
  8195. *
  8196. * @param {Rectangle} rectangle The rectangle for which to find the center
  8197. * @param {Cartographic} [result] The object onto which to store the result.
  8198. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8199. */
  8200. Rectangle.center = function (rectangle, result) {
  8201. //>>includeStart('debug', pragmas.debug);
  8202. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8203. //>>includeEnd('debug');
  8204. let east = rectangle.east;
  8205. const west = rectangle.west;
  8206. if (east < west) {
  8207. east += ComponentDatatype.CesiumMath.TWO_PI;
  8208. }
  8209. const longitude = ComponentDatatype.CesiumMath.negativePiToPi((west + east) * 0.5);
  8210. const latitude = (rectangle.south + rectangle.north) * 0.5;
  8211. if (!defaultValue.defined(result)) {
  8212. return new Cartographic(longitude, latitude);
  8213. }
  8214. result.longitude = longitude;
  8215. result.latitude = latitude;
  8216. result.height = 0.0;
  8217. return result;
  8218. };
  8219. /**
  8220. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  8221. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  8222. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  8223. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  8224. * coordinates, see {@link Rectangle.simpleIntersection}.
  8225. *
  8226. * @param {Rectangle} rectangle On rectangle to find an intersection
  8227. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8228. * @param {Rectangle} [result] The object onto which to store the result.
  8229. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8230. */
  8231. Rectangle.intersection = function (rectangle, otherRectangle, result) {
  8232. //>>includeStart('debug', pragmas.debug);
  8233. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8234. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8235. //>>includeEnd('debug');
  8236. let rectangleEast = rectangle.east;
  8237. let rectangleWest = rectangle.west;
  8238. let otherRectangleEast = otherRectangle.east;
  8239. let otherRectangleWest = otherRectangle.west;
  8240. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8241. rectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8242. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8243. otherRectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8244. }
  8245. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8246. otherRectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8247. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8248. rectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8249. }
  8250. const west = ComponentDatatype.CesiumMath.negativePiToPi(
  8251. Math.max(rectangleWest, otherRectangleWest)
  8252. );
  8253. const east = ComponentDatatype.CesiumMath.negativePiToPi(
  8254. Math.min(rectangleEast, otherRectangleEast)
  8255. );
  8256. if (
  8257. (rectangle.west < rectangle.east ||
  8258. otherRectangle.west < otherRectangle.east) &&
  8259. east <= west
  8260. ) {
  8261. return undefined;
  8262. }
  8263. const south = Math.max(rectangle.south, otherRectangle.south);
  8264. const north = Math.min(rectangle.north, otherRectangle.north);
  8265. if (south >= north) {
  8266. return undefined;
  8267. }
  8268. if (!defaultValue.defined(result)) {
  8269. return new Rectangle(west, south, east, north);
  8270. }
  8271. result.west = west;
  8272. result.south = south;
  8273. result.east = east;
  8274. result.north = north;
  8275. return result;
  8276. };
  8277. /**
  8278. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  8279. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  8280. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  8281. * and longitude (i.e. projected coordinates).
  8282. *
  8283. * @param {Rectangle} rectangle On rectangle to find an intersection
  8284. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8285. * @param {Rectangle} [result] The object onto which to store the result.
  8286. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8287. */
  8288. Rectangle.simpleIntersection = function (rectangle, otherRectangle, result) {
  8289. //>>includeStart('debug', pragmas.debug);
  8290. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8291. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8292. //>>includeEnd('debug');
  8293. const west = Math.max(rectangle.west, otherRectangle.west);
  8294. const south = Math.max(rectangle.south, otherRectangle.south);
  8295. const east = Math.min(rectangle.east, otherRectangle.east);
  8296. const north = Math.min(rectangle.north, otherRectangle.north);
  8297. if (south >= north || west >= east) {
  8298. return undefined;
  8299. }
  8300. if (!defaultValue.defined(result)) {
  8301. return new Rectangle(west, south, east, north);
  8302. }
  8303. result.west = west;
  8304. result.south = south;
  8305. result.east = east;
  8306. result.north = north;
  8307. return result;
  8308. };
  8309. /**
  8310. * Computes a rectangle that is the union of two rectangles.
  8311. *
  8312. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  8313. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  8314. * @param {Rectangle} [result] The object onto which to store the result.
  8315. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8316. */
  8317. Rectangle.union = function (rectangle, otherRectangle, result) {
  8318. //>>includeStart('debug', pragmas.debug);
  8319. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8320. RuntimeError.Check.typeOf.object("otherRectangle", otherRectangle);
  8321. //>>includeEnd('debug');
  8322. if (!defaultValue.defined(result)) {
  8323. result = new Rectangle();
  8324. }
  8325. let rectangleEast = rectangle.east;
  8326. let rectangleWest = rectangle.west;
  8327. let otherRectangleEast = otherRectangle.east;
  8328. let otherRectangleWest = otherRectangle.west;
  8329. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8330. rectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8331. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8332. otherRectangleEast += ComponentDatatype.CesiumMath.TWO_PI;
  8333. }
  8334. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8335. otherRectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8336. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8337. rectangleWest += ComponentDatatype.CesiumMath.TWO_PI;
  8338. }
  8339. const west = ComponentDatatype.CesiumMath.negativePiToPi(
  8340. Math.min(rectangleWest, otherRectangleWest)
  8341. );
  8342. const east = ComponentDatatype.CesiumMath.negativePiToPi(
  8343. Math.max(rectangleEast, otherRectangleEast)
  8344. );
  8345. result.west = west;
  8346. result.south = Math.min(rectangle.south, otherRectangle.south);
  8347. result.east = east;
  8348. result.north = Math.max(rectangle.north, otherRectangle.north);
  8349. return result;
  8350. };
  8351. /**
  8352. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  8353. *
  8354. * @param {Rectangle} rectangle A rectangle to expand.
  8355. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  8356. * @param {Rectangle} [result] The object onto which to store the result.
  8357. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  8358. */
  8359. Rectangle.expand = function (rectangle, cartographic, result) {
  8360. //>>includeStart('debug', pragmas.debug);
  8361. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8362. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  8363. //>>includeEnd('debug');
  8364. if (!defaultValue.defined(result)) {
  8365. result = new Rectangle();
  8366. }
  8367. result.west = Math.min(rectangle.west, cartographic.longitude);
  8368. result.south = Math.min(rectangle.south, cartographic.latitude);
  8369. result.east = Math.max(rectangle.east, cartographic.longitude);
  8370. result.north = Math.max(rectangle.north, cartographic.latitude);
  8371. return result;
  8372. };
  8373. /**
  8374. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  8375. *
  8376. * @param {Rectangle} rectangle The rectangle
  8377. * @param {Cartographic} cartographic The cartographic to test.
  8378. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  8379. */
  8380. Rectangle.contains = function (rectangle, cartographic) {
  8381. //>>includeStart('debug', pragmas.debug);
  8382. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8383. RuntimeError.Check.typeOf.object("cartographic", cartographic);
  8384. //>>includeEnd('debug');
  8385. let longitude = cartographic.longitude;
  8386. const latitude = cartographic.latitude;
  8387. const west = rectangle.west;
  8388. let east = rectangle.east;
  8389. if (east < west) {
  8390. east += ComponentDatatype.CesiumMath.TWO_PI;
  8391. if (longitude < 0.0) {
  8392. longitude += ComponentDatatype.CesiumMath.TWO_PI;
  8393. }
  8394. }
  8395. return (
  8396. (longitude > west ||
  8397. ComponentDatatype.CesiumMath.equalsEpsilon(longitude, west, ComponentDatatype.CesiumMath.EPSILON14)) &&
  8398. (longitude < east ||
  8399. ComponentDatatype.CesiumMath.equalsEpsilon(longitude, east, ComponentDatatype.CesiumMath.EPSILON14)) &&
  8400. latitude >= rectangle.south &&
  8401. latitude <= rectangle.north
  8402. );
  8403. };
  8404. const subsampleLlaScratch = new Cartographic();
  8405. /**
  8406. * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
  8407. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  8408. * for rectangles that cover the poles or cross the equator.
  8409. *
  8410. * @param {Rectangle} rectangle The rectangle to subsample.
  8411. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  8412. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  8413. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  8414. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  8415. */
  8416. Rectangle.subsample = function (rectangle, ellipsoid, surfaceHeight, result) {
  8417. //>>includeStart('debug', pragmas.debug);
  8418. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8419. //>>includeEnd('debug');
  8420. ellipsoid = defaultValue.defaultValue(ellipsoid, Ellipsoid.WGS84);
  8421. surfaceHeight = defaultValue.defaultValue(surfaceHeight, 0.0);
  8422. if (!defaultValue.defined(result)) {
  8423. result = [];
  8424. }
  8425. let length = 0;
  8426. const north = rectangle.north;
  8427. const south = rectangle.south;
  8428. const east = rectangle.east;
  8429. const west = rectangle.west;
  8430. const lla = subsampleLlaScratch;
  8431. lla.height = surfaceHeight;
  8432. lla.longitude = west;
  8433. lla.latitude = north;
  8434. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8435. length++;
  8436. lla.longitude = east;
  8437. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8438. length++;
  8439. lla.latitude = south;
  8440. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8441. length++;
  8442. lla.longitude = west;
  8443. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8444. length++;
  8445. if (north < 0.0) {
  8446. lla.latitude = north;
  8447. } else if (south > 0.0) {
  8448. lla.latitude = south;
  8449. } else {
  8450. lla.latitude = 0.0;
  8451. }
  8452. for (let i = 1; i < 8; ++i) {
  8453. lla.longitude = -Math.PI + i * ComponentDatatype.CesiumMath.PI_OVER_TWO;
  8454. if (Rectangle.contains(rectangle, lla)) {
  8455. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8456. length++;
  8457. }
  8458. }
  8459. if (lla.latitude === 0.0) {
  8460. lla.longitude = west;
  8461. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8462. length++;
  8463. lla.longitude = east;
  8464. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8465. length++;
  8466. }
  8467. result.length = length;
  8468. return result;
  8469. };
  8470. /**
  8471. * Computes a subsection of a rectangle from normalized coordinates in the range [0.0, 1.0].
  8472. *
  8473. * @param {Rectangle} rectangle The rectangle to subsection.
  8474. * @param {Number} westLerp The west interpolation factor in the range [0.0, 1.0]. Must be less than or equal to eastLerp.
  8475. * @param {Number} southLerp The south interpolation factor in the range [0.0, 1.0]. Must be less than or equal to northLerp.
  8476. * @param {Number} eastLerp The east interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to westLerp.
  8477. * @param {Number} northLerp The north interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to southLerp.
  8478. * @param {Rectangle} [result] The object onto which to store the result.
  8479. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8480. */
  8481. Rectangle.subsection = function (
  8482. rectangle,
  8483. westLerp,
  8484. southLerp,
  8485. eastLerp,
  8486. northLerp,
  8487. result
  8488. ) {
  8489. //>>includeStart('debug', pragmas.debug);
  8490. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  8491. RuntimeError.Check.typeOf.number.greaterThanOrEquals("westLerp", westLerp, 0.0);
  8492. RuntimeError.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, 1.0);
  8493. RuntimeError.Check.typeOf.number.greaterThanOrEquals("southLerp", southLerp, 0.0);
  8494. RuntimeError.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, 1.0);
  8495. RuntimeError.Check.typeOf.number.greaterThanOrEquals("eastLerp", eastLerp, 0.0);
  8496. RuntimeError.Check.typeOf.number.lessThanOrEquals("eastLerp", eastLerp, 1.0);
  8497. RuntimeError.Check.typeOf.number.greaterThanOrEquals("northLerp", northLerp, 0.0);
  8498. RuntimeError.Check.typeOf.number.lessThanOrEquals("northLerp", northLerp, 1.0);
  8499. RuntimeError.Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, eastLerp);
  8500. RuntimeError.Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, northLerp);
  8501. //>>includeEnd('debug');
  8502. if (!defaultValue.defined(result)) {
  8503. result = new Rectangle();
  8504. }
  8505. // This function doesn't use CesiumMath.lerp because it has floating point precision problems
  8506. // when the start and end values are the same but the t changes.
  8507. if (rectangle.west <= rectangle.east) {
  8508. const width = rectangle.east - rectangle.west;
  8509. result.west = rectangle.west + westLerp * width;
  8510. result.east = rectangle.west + eastLerp * width;
  8511. } else {
  8512. const width = ComponentDatatype.CesiumMath.TWO_PI + rectangle.east - rectangle.west;
  8513. result.west = ComponentDatatype.CesiumMath.negativePiToPi(rectangle.west + westLerp * width);
  8514. result.east = ComponentDatatype.CesiumMath.negativePiToPi(rectangle.west + eastLerp * width);
  8515. }
  8516. const height = rectangle.north - rectangle.south;
  8517. result.south = rectangle.south + southLerp * height;
  8518. result.north = rectangle.south + northLerp * height;
  8519. // Fix floating point precision problems when t = 1
  8520. if (westLerp === 1.0) {
  8521. result.west = rectangle.east;
  8522. }
  8523. if (eastLerp === 1.0) {
  8524. result.east = rectangle.east;
  8525. }
  8526. if (southLerp === 1.0) {
  8527. result.south = rectangle.north;
  8528. }
  8529. if (northLerp === 1.0) {
  8530. result.north = rectangle.north;
  8531. }
  8532. return result;
  8533. };
  8534. /**
  8535. * The largest possible rectangle.
  8536. *
  8537. * @type {Rectangle}
  8538. * @constant
  8539. */
  8540. Rectangle.MAX_VALUE = Object.freeze(
  8541. new Rectangle(
  8542. -Math.PI,
  8543. -ComponentDatatype.CesiumMath.PI_OVER_TWO,
  8544. Math.PI,
  8545. ComponentDatatype.CesiumMath.PI_OVER_TWO
  8546. )
  8547. );
  8548. /**
  8549. * A 2D Cartesian point.
  8550. * @alias Cartesian2
  8551. * @constructor
  8552. *
  8553. * @param {Number} [x=0.0] The X component.
  8554. * @param {Number} [y=0.0] The Y component.
  8555. *
  8556. * @see Cartesian3
  8557. * @see Cartesian4
  8558. * @see Packable
  8559. */
  8560. function Cartesian2(x, y) {
  8561. /**
  8562. * The X component.
  8563. * @type {Number}
  8564. * @default 0.0
  8565. */
  8566. this.x = defaultValue.defaultValue(x, 0.0);
  8567. /**
  8568. * The Y component.
  8569. * @type {Number}
  8570. * @default 0.0
  8571. */
  8572. this.y = defaultValue.defaultValue(y, 0.0);
  8573. }
  8574. /**
  8575. * Creates a Cartesian2 instance from x and y coordinates.
  8576. *
  8577. * @param {Number} x The x coordinate.
  8578. * @param {Number} y The y coordinate.
  8579. * @param {Cartesian2} [result] The object onto which to store the result.
  8580. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8581. */
  8582. Cartesian2.fromElements = function (x, y, result) {
  8583. if (!defaultValue.defined(result)) {
  8584. return new Cartesian2(x, y);
  8585. }
  8586. result.x = x;
  8587. result.y = y;
  8588. return result;
  8589. };
  8590. /**
  8591. * Duplicates a Cartesian2 instance.
  8592. *
  8593. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  8594. * @param {Cartesian2} [result] The object onto which to store the result.
  8595. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  8596. */
  8597. Cartesian2.clone = function (cartesian, result) {
  8598. if (!defaultValue.defined(cartesian)) {
  8599. return undefined;
  8600. }
  8601. if (!defaultValue.defined(result)) {
  8602. return new Cartesian2(cartesian.x, cartesian.y);
  8603. }
  8604. result.x = cartesian.x;
  8605. result.y = cartesian.y;
  8606. return result;
  8607. };
  8608. /**
  8609. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  8610. * x and y properties of the Cartesian3 and drops z.
  8611. * @function
  8612. *
  8613. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  8614. * @param {Cartesian2} [result] The object onto which to store the result.
  8615. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8616. */
  8617. Cartesian2.fromCartesian3 = Cartesian2.clone;
  8618. /**
  8619. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  8620. * x and y properties of the Cartesian4 and drops z and w.
  8621. * @function
  8622. *
  8623. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  8624. * @param {Cartesian2} [result] The object onto which to store the result.
  8625. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8626. */
  8627. Cartesian2.fromCartesian4 = Cartesian2.clone;
  8628. /**
  8629. * The number of elements used to pack the object into an array.
  8630. * @type {Number}
  8631. */
  8632. Cartesian2.packedLength = 2;
  8633. /**
  8634. * Stores the provided instance into the provided array.
  8635. *
  8636. * @param {Cartesian2} value The value to pack.
  8637. * @param {Number[]} array The array to pack into.
  8638. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  8639. *
  8640. * @returns {Number[]} The array that was packed into
  8641. */
  8642. Cartesian2.pack = function (value, array, startingIndex) {
  8643. //>>includeStart('debug', pragmas.debug);
  8644. RuntimeError.Check.typeOf.object("value", value);
  8645. RuntimeError.Check.defined("array", array);
  8646. //>>includeEnd('debug');
  8647. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  8648. array[startingIndex++] = value.x;
  8649. array[startingIndex] = value.y;
  8650. return array;
  8651. };
  8652. /**
  8653. * Retrieves an instance from a packed array.
  8654. *
  8655. * @param {Number[]} array The packed array.
  8656. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  8657. * @param {Cartesian2} [result] The object into which to store the result.
  8658. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8659. */
  8660. Cartesian2.unpack = function (array, startingIndex, result) {
  8661. //>>includeStart('debug', pragmas.debug);
  8662. RuntimeError.Check.defined("array", array);
  8663. //>>includeEnd('debug');
  8664. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  8665. if (!defaultValue.defined(result)) {
  8666. result = new Cartesian2();
  8667. }
  8668. result.x = array[startingIndex++];
  8669. result.y = array[startingIndex];
  8670. return result;
  8671. };
  8672. /**
  8673. * Flattens an array of Cartesian2s into an array of components.
  8674. *
  8675. * @param {Cartesian2[]} array The array of cartesians to pack.
  8676. * @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.
  8677. * @returns {Number[]} The packed array.
  8678. */
  8679. Cartesian2.packArray = function (array, result) {
  8680. //>>includeStart('debug', pragmas.debug);
  8681. RuntimeError.Check.defined("array", array);
  8682. //>>includeEnd('debug');
  8683. const length = array.length;
  8684. const resultLength = length * 2;
  8685. if (!defaultValue.defined(result)) {
  8686. result = new Array(resultLength);
  8687. } else if (!Array.isArray(result) && result.length !== resultLength) {
  8688. //>>includeStart('debug', pragmas.debug);
  8689. throw new RuntimeError.DeveloperError(
  8690. "If result is a typed array, it must have exactly array.length * 2 elements"
  8691. );
  8692. //>>includeEnd('debug');
  8693. } else if (result.length !== resultLength) {
  8694. result.length = resultLength;
  8695. }
  8696. for (let i = 0; i < length; ++i) {
  8697. Cartesian2.pack(array[i], result, i * 2);
  8698. }
  8699. return result;
  8700. };
  8701. /**
  8702. * Unpacks an array of cartesian components into an array of Cartesian2s.
  8703. *
  8704. * @param {Number[]} array The array of components to unpack.
  8705. * @param {Cartesian2[]} [result] The array onto which to store the result.
  8706. * @returns {Cartesian2[]} The unpacked array.
  8707. */
  8708. Cartesian2.unpackArray = function (array, result) {
  8709. //>>includeStart('debug', pragmas.debug);
  8710. RuntimeError.Check.defined("array", array);
  8711. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 2);
  8712. if (array.length % 2 !== 0) {
  8713. throw new RuntimeError.DeveloperError("array length must be a multiple of 2.");
  8714. }
  8715. //>>includeEnd('debug');
  8716. const length = array.length;
  8717. if (!defaultValue.defined(result)) {
  8718. result = new Array(length / 2);
  8719. } else {
  8720. result.length = length / 2;
  8721. }
  8722. for (let i = 0; i < length; i += 2) {
  8723. const index = i / 2;
  8724. result[index] = Cartesian2.unpack(array, i, result[index]);
  8725. }
  8726. return result;
  8727. };
  8728. /**
  8729. * Creates a Cartesian2 from two consecutive elements in an array.
  8730. * @function
  8731. *
  8732. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  8733. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  8734. * @param {Cartesian2} [result] The object onto which to store the result.
  8735. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  8736. *
  8737. * @example
  8738. * // Create a Cartesian2 with (1.0, 2.0)
  8739. * const v = [1.0, 2.0];
  8740. * const p = Cesium.Cartesian2.fromArray(v);
  8741. *
  8742. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  8743. * const v2 = [0.0, 0.0, 1.0, 2.0];
  8744. * const p2 = Cesium.Cartesian2.fromArray(v2, 2);
  8745. */
  8746. Cartesian2.fromArray = Cartesian2.unpack;
  8747. /**
  8748. * Computes the value of the maximum component for the supplied Cartesian.
  8749. *
  8750. * @param {Cartesian2} cartesian The cartesian to use.
  8751. * @returns {Number} The value of the maximum component.
  8752. */
  8753. Cartesian2.maximumComponent = function (cartesian) {
  8754. //>>includeStart('debug', pragmas.debug);
  8755. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8756. //>>includeEnd('debug');
  8757. return Math.max(cartesian.x, cartesian.y);
  8758. };
  8759. /**
  8760. * Computes the value of the minimum component for the supplied Cartesian.
  8761. *
  8762. * @param {Cartesian2} cartesian The cartesian to use.
  8763. * @returns {Number} The value of the minimum component.
  8764. */
  8765. Cartesian2.minimumComponent = function (cartesian) {
  8766. //>>includeStart('debug', pragmas.debug);
  8767. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8768. //>>includeEnd('debug');
  8769. return Math.min(cartesian.x, cartesian.y);
  8770. };
  8771. /**
  8772. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  8773. *
  8774. * @param {Cartesian2} first A cartesian to compare.
  8775. * @param {Cartesian2} second A cartesian to compare.
  8776. * @param {Cartesian2} result The object into which to store the result.
  8777. * @returns {Cartesian2} A cartesian with the minimum components.
  8778. */
  8779. Cartesian2.minimumByComponent = function (first, second, result) {
  8780. //>>includeStart('debug', pragmas.debug);
  8781. RuntimeError.Check.typeOf.object("first", first);
  8782. RuntimeError.Check.typeOf.object("second", second);
  8783. RuntimeError.Check.typeOf.object("result", result);
  8784. //>>includeEnd('debug');
  8785. result.x = Math.min(first.x, second.x);
  8786. result.y = Math.min(first.y, second.y);
  8787. return result;
  8788. };
  8789. /**
  8790. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  8791. *
  8792. * @param {Cartesian2} first A cartesian to compare.
  8793. * @param {Cartesian2} second A cartesian to compare.
  8794. * @param {Cartesian2} result The object into which to store the result.
  8795. * @returns {Cartesian2} A cartesian with the maximum components.
  8796. */
  8797. Cartesian2.maximumByComponent = function (first, second, result) {
  8798. //>>includeStart('debug', pragmas.debug);
  8799. RuntimeError.Check.typeOf.object("first", first);
  8800. RuntimeError.Check.typeOf.object("second", second);
  8801. RuntimeError.Check.typeOf.object("result", result);
  8802. //>>includeEnd('debug');
  8803. result.x = Math.max(first.x, second.x);
  8804. result.y = Math.max(first.y, second.y);
  8805. return result;
  8806. };
  8807. /**
  8808. * Constrain a value to lie between two values.
  8809. *
  8810. * @param {Cartesian2} value The value to clamp.
  8811. * @param {Cartesian2} min The minimum bound.
  8812. * @param {Cartesian2} max The maximum bound.
  8813. * @param {Cartesian2} result The object into which to store the result.
  8814. * @returns {Cartesian2} The clamped value such that min <= result <= max.
  8815. */
  8816. Cartesian2.clamp = function (value, min, max, result) {
  8817. //>>includeStart('debug', pragmas.debug);
  8818. RuntimeError.Check.typeOf.object("value", value);
  8819. RuntimeError.Check.typeOf.object("min", min);
  8820. RuntimeError.Check.typeOf.object("max", max);
  8821. RuntimeError.Check.typeOf.object("result", result);
  8822. //>>includeEnd('debug');
  8823. const x = ComponentDatatype.CesiumMath.clamp(value.x, min.x, max.x);
  8824. const y = ComponentDatatype.CesiumMath.clamp(value.y, min.y, max.y);
  8825. result.x = x;
  8826. result.y = y;
  8827. return result;
  8828. };
  8829. /**
  8830. * Computes the provided Cartesian's squared magnitude.
  8831. *
  8832. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  8833. * @returns {Number} The squared magnitude.
  8834. */
  8835. Cartesian2.magnitudeSquared = function (cartesian) {
  8836. //>>includeStart('debug', pragmas.debug);
  8837. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8838. //>>includeEnd('debug');
  8839. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  8840. };
  8841. /**
  8842. * Computes the Cartesian's magnitude (length).
  8843. *
  8844. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  8845. * @returns {Number} The magnitude.
  8846. */
  8847. Cartesian2.magnitude = function (cartesian) {
  8848. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  8849. };
  8850. const distanceScratch = new Cartesian2();
  8851. /**
  8852. * Computes the distance between two points.
  8853. *
  8854. * @param {Cartesian2} left The first point to compute the distance from.
  8855. * @param {Cartesian2} right The second point to compute the distance to.
  8856. * @returns {Number} The distance between two points.
  8857. *
  8858. * @example
  8859. * // Returns 1.0
  8860. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  8861. */
  8862. Cartesian2.distance = function (left, right) {
  8863. //>>includeStart('debug', pragmas.debug);
  8864. RuntimeError.Check.typeOf.object("left", left);
  8865. RuntimeError.Check.typeOf.object("right", right);
  8866. //>>includeEnd('debug');
  8867. Cartesian2.subtract(left, right, distanceScratch);
  8868. return Cartesian2.magnitude(distanceScratch);
  8869. };
  8870. /**
  8871. * Computes the squared distance between two points. Comparing squared distances
  8872. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  8873. *
  8874. * @param {Cartesian2} left The first point to compute the distance from.
  8875. * @param {Cartesian2} right The second point to compute the distance to.
  8876. * @returns {Number} The distance between two points.
  8877. *
  8878. * @example
  8879. * // Returns 4.0, not 2.0
  8880. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  8881. */
  8882. Cartesian2.distanceSquared = function (left, right) {
  8883. //>>includeStart('debug', pragmas.debug);
  8884. RuntimeError.Check.typeOf.object("left", left);
  8885. RuntimeError.Check.typeOf.object("right", right);
  8886. //>>includeEnd('debug');
  8887. Cartesian2.subtract(left, right, distanceScratch);
  8888. return Cartesian2.magnitudeSquared(distanceScratch);
  8889. };
  8890. /**
  8891. * Computes the normalized form of the supplied Cartesian.
  8892. *
  8893. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  8894. * @param {Cartesian2} result The object onto which to store the result.
  8895. * @returns {Cartesian2} The modified result parameter.
  8896. */
  8897. Cartesian2.normalize = function (cartesian, result) {
  8898. //>>includeStart('debug', pragmas.debug);
  8899. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  8900. RuntimeError.Check.typeOf.object("result", result);
  8901. //>>includeEnd('debug');
  8902. const magnitude = Cartesian2.magnitude(cartesian);
  8903. result.x = cartesian.x / magnitude;
  8904. result.y = cartesian.y / magnitude;
  8905. //>>includeStart('debug', pragmas.debug);
  8906. if (isNaN(result.x) || isNaN(result.y)) {
  8907. throw new RuntimeError.DeveloperError("normalized result is not a number");
  8908. }
  8909. //>>includeEnd('debug');
  8910. return result;
  8911. };
  8912. /**
  8913. * Computes the dot (scalar) product of two Cartesians.
  8914. *
  8915. * @param {Cartesian2} left The first Cartesian.
  8916. * @param {Cartesian2} right The second Cartesian.
  8917. * @returns {Number} The dot product.
  8918. */
  8919. Cartesian2.dot = function (left, right) {
  8920. //>>includeStart('debug', pragmas.debug);
  8921. RuntimeError.Check.typeOf.object("left", left);
  8922. RuntimeError.Check.typeOf.object("right", right);
  8923. //>>includeEnd('debug');
  8924. return left.x * right.x + left.y * right.y;
  8925. };
  8926. /**
  8927. * Computes the magnitude of the cross product that would result from implicitly setting the Z coordinate of the input vectors to 0
  8928. *
  8929. * @param {Cartesian2} left The first Cartesian.
  8930. * @param {Cartesian2} right The second Cartesian.
  8931. * @returns {Number} The cross product.
  8932. */
  8933. Cartesian2.cross = function (left, right) {
  8934. //>>includeStart('debug', pragmas.debug);
  8935. RuntimeError.Check.typeOf.object("left", left);
  8936. RuntimeError.Check.typeOf.object("right", right);
  8937. //>>includeEnd('debug');
  8938. return left.x * right.y - left.y * right.x;
  8939. };
  8940. /**
  8941. * Computes the componentwise product of two Cartesians.
  8942. *
  8943. * @param {Cartesian2} left The first Cartesian.
  8944. * @param {Cartesian2} right The second Cartesian.
  8945. * @param {Cartesian2} result The object onto which to store the result.
  8946. * @returns {Cartesian2} The modified result parameter.
  8947. */
  8948. Cartesian2.multiplyComponents = function (left, right, result) {
  8949. //>>includeStart('debug', pragmas.debug);
  8950. RuntimeError.Check.typeOf.object("left", left);
  8951. RuntimeError.Check.typeOf.object("right", right);
  8952. RuntimeError.Check.typeOf.object("result", result);
  8953. //>>includeEnd('debug');
  8954. result.x = left.x * right.x;
  8955. result.y = left.y * right.y;
  8956. return result;
  8957. };
  8958. /**
  8959. * Computes the componentwise quotient of two Cartesians.
  8960. *
  8961. * @param {Cartesian2} left The first Cartesian.
  8962. * @param {Cartesian2} right The second Cartesian.
  8963. * @param {Cartesian2} result The object onto which to store the result.
  8964. * @returns {Cartesian2} The modified result parameter.
  8965. */
  8966. Cartesian2.divideComponents = function (left, right, result) {
  8967. //>>includeStart('debug', pragmas.debug);
  8968. RuntimeError.Check.typeOf.object("left", left);
  8969. RuntimeError.Check.typeOf.object("right", right);
  8970. RuntimeError.Check.typeOf.object("result", result);
  8971. //>>includeEnd('debug');
  8972. result.x = left.x / right.x;
  8973. result.y = left.y / right.y;
  8974. return result;
  8975. };
  8976. /**
  8977. * Computes the componentwise sum of two Cartesians.
  8978. *
  8979. * @param {Cartesian2} left The first Cartesian.
  8980. * @param {Cartesian2} right The second Cartesian.
  8981. * @param {Cartesian2} result The object onto which to store the result.
  8982. * @returns {Cartesian2} The modified result parameter.
  8983. */
  8984. Cartesian2.add = function (left, right, result) {
  8985. //>>includeStart('debug', pragmas.debug);
  8986. RuntimeError.Check.typeOf.object("left", left);
  8987. RuntimeError.Check.typeOf.object("right", right);
  8988. RuntimeError.Check.typeOf.object("result", result);
  8989. //>>includeEnd('debug');
  8990. result.x = left.x + right.x;
  8991. result.y = left.y + right.y;
  8992. return result;
  8993. };
  8994. /**
  8995. * Computes the componentwise difference of two Cartesians.
  8996. *
  8997. * @param {Cartesian2} left The first Cartesian.
  8998. * @param {Cartesian2} right The second Cartesian.
  8999. * @param {Cartesian2} result The object onto which to store the result.
  9000. * @returns {Cartesian2} The modified result parameter.
  9001. */
  9002. Cartesian2.subtract = function (left, right, result) {
  9003. //>>includeStart('debug', pragmas.debug);
  9004. RuntimeError.Check.typeOf.object("left", left);
  9005. RuntimeError.Check.typeOf.object("right", right);
  9006. RuntimeError.Check.typeOf.object("result", result);
  9007. //>>includeEnd('debug');
  9008. result.x = left.x - right.x;
  9009. result.y = left.y - right.y;
  9010. return result;
  9011. };
  9012. /**
  9013. * Multiplies the provided Cartesian componentwise by the provided scalar.
  9014. *
  9015. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  9016. * @param {Number} scalar The scalar to multiply with.
  9017. * @param {Cartesian2} result The object onto which to store the result.
  9018. * @returns {Cartesian2} The modified result parameter.
  9019. */
  9020. Cartesian2.multiplyByScalar = function (cartesian, scalar, result) {
  9021. //>>includeStart('debug', pragmas.debug);
  9022. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9023. RuntimeError.Check.typeOf.number("scalar", scalar);
  9024. RuntimeError.Check.typeOf.object("result", result);
  9025. //>>includeEnd('debug');
  9026. result.x = cartesian.x * scalar;
  9027. result.y = cartesian.y * scalar;
  9028. return result;
  9029. };
  9030. /**
  9031. * Divides the provided Cartesian componentwise by the provided scalar.
  9032. *
  9033. * @param {Cartesian2} cartesian The Cartesian to be divided.
  9034. * @param {Number} scalar The scalar to divide by.
  9035. * @param {Cartesian2} result The object onto which to store the result.
  9036. * @returns {Cartesian2} The modified result parameter.
  9037. */
  9038. Cartesian2.divideByScalar = function (cartesian, scalar, result) {
  9039. //>>includeStart('debug', pragmas.debug);
  9040. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9041. RuntimeError.Check.typeOf.number("scalar", scalar);
  9042. RuntimeError.Check.typeOf.object("result", result);
  9043. //>>includeEnd('debug');
  9044. result.x = cartesian.x / scalar;
  9045. result.y = cartesian.y / scalar;
  9046. return result;
  9047. };
  9048. /**
  9049. * Negates the provided Cartesian.
  9050. *
  9051. * @param {Cartesian2} cartesian The Cartesian to be negated.
  9052. * @param {Cartesian2} result The object onto which to store the result.
  9053. * @returns {Cartesian2} The modified result parameter.
  9054. */
  9055. Cartesian2.negate = function (cartesian, result) {
  9056. //>>includeStart('debug', pragmas.debug);
  9057. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9058. RuntimeError.Check.typeOf.object("result", result);
  9059. //>>includeEnd('debug');
  9060. result.x = -cartesian.x;
  9061. result.y = -cartesian.y;
  9062. return result;
  9063. };
  9064. /**
  9065. * Computes the absolute value of the provided Cartesian.
  9066. *
  9067. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  9068. * @param {Cartesian2} result The object onto which to store the result.
  9069. * @returns {Cartesian2} The modified result parameter.
  9070. */
  9071. Cartesian2.abs = function (cartesian, result) {
  9072. //>>includeStart('debug', pragmas.debug);
  9073. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9074. RuntimeError.Check.typeOf.object("result", result);
  9075. //>>includeEnd('debug');
  9076. result.x = Math.abs(cartesian.x);
  9077. result.y = Math.abs(cartesian.y);
  9078. return result;
  9079. };
  9080. const lerpScratch = new Cartesian2();
  9081. /**
  9082. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  9083. *
  9084. * @param {Cartesian2} start The value corresponding to t at 0.0.
  9085. * @param {Cartesian2} end The value corresponding to t at 1.0.
  9086. * @param {Number} t The point along t at which to interpolate.
  9087. * @param {Cartesian2} result The object onto which to store the result.
  9088. * @returns {Cartesian2} The modified result parameter.
  9089. */
  9090. Cartesian2.lerp = function (start, end, t, result) {
  9091. //>>includeStart('debug', pragmas.debug);
  9092. RuntimeError.Check.typeOf.object("start", start);
  9093. RuntimeError.Check.typeOf.object("end", end);
  9094. RuntimeError.Check.typeOf.number("t", t);
  9095. RuntimeError.Check.typeOf.object("result", result);
  9096. //>>includeEnd('debug');
  9097. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  9098. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  9099. return Cartesian2.add(lerpScratch, result, result);
  9100. };
  9101. const angleBetweenScratch = new Cartesian2();
  9102. const angleBetweenScratch2 = new Cartesian2();
  9103. /**
  9104. * Returns the angle, in radians, between the provided Cartesians.
  9105. *
  9106. * @param {Cartesian2} left The first Cartesian.
  9107. * @param {Cartesian2} right The second Cartesian.
  9108. * @returns {Number} The angle between the Cartesians.
  9109. */
  9110. Cartesian2.angleBetween = function (left, right) {
  9111. //>>includeStart('debug', pragmas.debug);
  9112. RuntimeError.Check.typeOf.object("left", left);
  9113. RuntimeError.Check.typeOf.object("right", right);
  9114. //>>includeEnd('debug');
  9115. Cartesian2.normalize(left, angleBetweenScratch);
  9116. Cartesian2.normalize(right, angleBetweenScratch2);
  9117. return ComponentDatatype.CesiumMath.acosClamped(
  9118. Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2)
  9119. );
  9120. };
  9121. const mostOrthogonalAxisScratch = new Cartesian2();
  9122. /**
  9123. * Returns the axis that is most orthogonal to the provided Cartesian.
  9124. *
  9125. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  9126. * @param {Cartesian2} result The object onto which to store the result.
  9127. * @returns {Cartesian2} The most orthogonal axis.
  9128. */
  9129. Cartesian2.mostOrthogonalAxis = function (cartesian, result) {
  9130. //>>includeStart('debug', pragmas.debug);
  9131. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9132. RuntimeError.Check.typeOf.object("result", result);
  9133. //>>includeEnd('debug');
  9134. const f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  9135. Cartesian2.abs(f, f);
  9136. if (f.x <= f.y) {
  9137. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  9138. } else {
  9139. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  9140. }
  9141. return result;
  9142. };
  9143. /**
  9144. * Compares the provided Cartesians componentwise and returns
  9145. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9146. *
  9147. * @param {Cartesian2} [left] The first Cartesian.
  9148. * @param {Cartesian2} [right] The second Cartesian.
  9149. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  9150. */
  9151. Cartesian2.equals = function (left, right) {
  9152. return (
  9153. left === right ||
  9154. (defaultValue.defined(left) &&
  9155. defaultValue.defined(right) &&
  9156. left.x === right.x &&
  9157. left.y === right.y)
  9158. );
  9159. };
  9160. /**
  9161. * @private
  9162. */
  9163. Cartesian2.equalsArray = function (cartesian, array, offset) {
  9164. return cartesian.x === array[offset] && cartesian.y === array[offset + 1];
  9165. };
  9166. /**
  9167. * Compares the provided Cartesians componentwise and returns
  9168. * <code>true</code> if they pass an absolute or relative tolerance test,
  9169. * <code>false</code> otherwise.
  9170. *
  9171. * @param {Cartesian2} [left] The first Cartesian.
  9172. * @param {Cartesian2} [right] The second Cartesian.
  9173. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  9174. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  9175. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  9176. */
  9177. Cartesian2.equalsEpsilon = function (
  9178. left,
  9179. right,
  9180. relativeEpsilon,
  9181. absoluteEpsilon
  9182. ) {
  9183. return (
  9184. left === right ||
  9185. (defaultValue.defined(left) &&
  9186. defaultValue.defined(right) &&
  9187. ComponentDatatype.CesiumMath.equalsEpsilon(
  9188. left.x,
  9189. right.x,
  9190. relativeEpsilon,
  9191. absoluteEpsilon
  9192. ) &&
  9193. ComponentDatatype.CesiumMath.equalsEpsilon(
  9194. left.y,
  9195. right.y,
  9196. relativeEpsilon,
  9197. absoluteEpsilon
  9198. ))
  9199. );
  9200. };
  9201. /**
  9202. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  9203. *
  9204. * @type {Cartesian2}
  9205. * @constant
  9206. */
  9207. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  9208. /**
  9209. * An immutable Cartesian2 instance initialized to (1.0, 1.0).
  9210. *
  9211. * @type {Cartesian2}
  9212. * @constant
  9213. */
  9214. Cartesian2.ONE = Object.freeze(new Cartesian2(1.0, 1.0));
  9215. /**
  9216. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  9217. *
  9218. * @type {Cartesian2}
  9219. * @constant
  9220. */
  9221. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  9222. /**
  9223. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  9224. *
  9225. * @type {Cartesian2}
  9226. * @constant
  9227. */
  9228. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  9229. /**
  9230. * Duplicates this Cartesian2 instance.
  9231. *
  9232. * @param {Cartesian2} [result] The object onto which to store the result.
  9233. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  9234. */
  9235. Cartesian2.prototype.clone = function (result) {
  9236. return Cartesian2.clone(this, result);
  9237. };
  9238. /**
  9239. * Compares this Cartesian against the provided Cartesian componentwise and returns
  9240. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9241. *
  9242. * @param {Cartesian2} [right] The right hand side Cartesian.
  9243. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  9244. */
  9245. Cartesian2.prototype.equals = function (right) {
  9246. return Cartesian2.equals(this, right);
  9247. };
  9248. /**
  9249. * Compares this Cartesian against the provided Cartesian componentwise and returns
  9250. * <code>true</code> if they pass an absolute or relative tolerance test,
  9251. * <code>false</code> otherwise.
  9252. *
  9253. * @param {Cartesian2} [right] The right hand side Cartesian.
  9254. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  9255. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  9256. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  9257. */
  9258. Cartesian2.prototype.equalsEpsilon = function (
  9259. right,
  9260. relativeEpsilon,
  9261. absoluteEpsilon
  9262. ) {
  9263. return Cartesian2.equalsEpsilon(
  9264. this,
  9265. right,
  9266. relativeEpsilon,
  9267. absoluteEpsilon
  9268. );
  9269. };
  9270. /**
  9271. * Creates a string representing this Cartesian in the format '(x, y)'.
  9272. *
  9273. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  9274. */
  9275. Cartesian2.prototype.toString = function () {
  9276. return `(${this.x}, ${this.y})`;
  9277. };
  9278. /**
  9279. * A 2x2 matrix, indexable as a column-major order array.
  9280. * Constructor parameters are in row-major order for code readability.
  9281. * @alias Matrix2
  9282. * @constructor
  9283. * @implements {ArrayLike<number>}
  9284. *
  9285. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  9286. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  9287. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  9288. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  9289. *
  9290. * @see Matrix2.fromArray
  9291. * @see Matrix2.fromColumnMajorArray
  9292. * @see Matrix2.fromRowMajorArray
  9293. * @see Matrix2.fromScale
  9294. * @see Matrix2.fromUniformScale
  9295. * @see Matrix2.fromRotation
  9296. * @see Matrix3
  9297. * @see Matrix4
  9298. */
  9299. function Matrix2(column0Row0, column1Row0, column0Row1, column1Row1) {
  9300. this[0] = defaultValue.defaultValue(column0Row0, 0.0);
  9301. this[1] = defaultValue.defaultValue(column0Row1, 0.0);
  9302. this[2] = defaultValue.defaultValue(column1Row0, 0.0);
  9303. this[3] = defaultValue.defaultValue(column1Row1, 0.0);
  9304. }
  9305. /**
  9306. * The number of elements used to pack the object into an array.
  9307. * @type {Number}
  9308. */
  9309. Matrix2.packedLength = 4;
  9310. /**
  9311. * Stores the provided instance into the provided array.
  9312. *
  9313. * @param {Matrix2} value The value to pack.
  9314. * @param {Number[]} array The array to pack into.
  9315. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9316. *
  9317. * @returns {Number[]} The array that was packed into
  9318. */
  9319. Matrix2.pack = function (value, array, startingIndex) {
  9320. //>>includeStart('debug', pragmas.debug);
  9321. RuntimeError.Check.typeOf.object("value", value);
  9322. RuntimeError.Check.defined("array", array);
  9323. //>>includeEnd('debug');
  9324. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  9325. array[startingIndex++] = value[0];
  9326. array[startingIndex++] = value[1];
  9327. array[startingIndex++] = value[2];
  9328. array[startingIndex++] = value[3];
  9329. return array;
  9330. };
  9331. /**
  9332. * Retrieves an instance from a packed array.
  9333. *
  9334. * @param {Number[]} array The packed array.
  9335. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  9336. * @param {Matrix2} [result] The object into which to store the result.
  9337. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  9338. */
  9339. Matrix2.unpack = function (array, startingIndex, result) {
  9340. //>>includeStart('debug', pragmas.debug);
  9341. RuntimeError.Check.defined("array", array);
  9342. //>>includeEnd('debug');
  9343. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  9344. if (!defaultValue.defined(result)) {
  9345. result = new Matrix2();
  9346. }
  9347. result[0] = array[startingIndex++];
  9348. result[1] = array[startingIndex++];
  9349. result[2] = array[startingIndex++];
  9350. result[3] = array[startingIndex++];
  9351. return result;
  9352. };
  9353. /**
  9354. * Flattens an array of Matrix2s into an array of components. The components
  9355. * are stored in column-major order.
  9356. *
  9357. * @param {Matrix2[]} array The array of matrices to pack.
  9358. * @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.
  9359. * @returns {Number[]} The packed array.
  9360. */
  9361. Matrix2.packArray = function (array, result) {
  9362. //>>includeStart('debug', pragmas.debug);
  9363. RuntimeError.Check.defined("array", array);
  9364. //>>includeEnd('debug');
  9365. const length = array.length;
  9366. const resultLength = length * 4;
  9367. if (!defaultValue.defined(result)) {
  9368. result = new Array(resultLength);
  9369. } else if (!Array.isArray(result) && result.length !== resultLength) {
  9370. //>>includeStart('debug', pragmas.debug);
  9371. throw new RuntimeError.DeveloperError(
  9372. "If result is a typed array, it must have exactly array.length * 4 elements"
  9373. );
  9374. //>>includeEnd('debug');
  9375. } else if (result.length !== resultLength) {
  9376. result.length = resultLength;
  9377. }
  9378. for (let i = 0; i < length; ++i) {
  9379. Matrix2.pack(array[i], result, i * 4);
  9380. }
  9381. return result;
  9382. };
  9383. /**
  9384. * Unpacks an array of column-major matrix components into an array of Matrix2s.
  9385. *
  9386. * @param {Number[]} array The array of components to unpack.
  9387. * @param {Matrix2[]} [result] The array onto which to store the result.
  9388. * @returns {Matrix2[]} The unpacked array.
  9389. */
  9390. Matrix2.unpackArray = function (array, result) {
  9391. //>>includeStart('debug', pragmas.debug);
  9392. RuntimeError.Check.defined("array", array);
  9393. RuntimeError.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  9394. if (array.length % 4 !== 0) {
  9395. throw new RuntimeError.DeveloperError("array length must be a multiple of 4.");
  9396. }
  9397. //>>includeEnd('debug');
  9398. const length = array.length;
  9399. if (!defaultValue.defined(result)) {
  9400. result = new Array(length / 4);
  9401. } else {
  9402. result.length = length / 4;
  9403. }
  9404. for (let i = 0; i < length; i += 4) {
  9405. const index = i / 4;
  9406. result[index] = Matrix2.unpack(array, i, result[index]);
  9407. }
  9408. return result;
  9409. };
  9410. /**
  9411. * Duplicates a Matrix2 instance.
  9412. *
  9413. * @param {Matrix2} matrix The matrix to duplicate.
  9414. * @param {Matrix2} [result] The object onto which to store the result.
  9415. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided. (Returns undefined if matrix is undefined)
  9416. */
  9417. Matrix2.clone = function (matrix, result) {
  9418. if (!defaultValue.defined(matrix)) {
  9419. return undefined;
  9420. }
  9421. if (!defaultValue.defined(result)) {
  9422. return new Matrix2(matrix[0], matrix[2], matrix[1], matrix[3]);
  9423. }
  9424. result[0] = matrix[0];
  9425. result[1] = matrix[1];
  9426. result[2] = matrix[2];
  9427. result[3] = matrix[3];
  9428. return result;
  9429. };
  9430. /**
  9431. * Creates a Matrix2 from 4 consecutive elements in an array.
  9432. *
  9433. * @function
  9434. * @param {Number[]} array The array whose 4 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  9435. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  9436. * @param {Matrix2} [result] The object onto which to store the result.
  9437. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  9438. *
  9439. * @example
  9440. * // Create the Matrix2:
  9441. * // [1.0, 2.0]
  9442. * // [1.0, 2.0]
  9443. *
  9444. * const v = [1.0, 1.0, 2.0, 2.0];
  9445. * const m = Cesium.Matrix2.fromArray(v);
  9446. *
  9447. * // Create same Matrix2 with using an offset into an array
  9448. * const v2 = [0.0, 0.0, 1.0, 1.0, 2.0, 2.0];
  9449. * const m2 = Cesium.Matrix2.fromArray(v2, 2);
  9450. */
  9451. Matrix2.fromArray = Matrix2.unpack;
  9452. /**
  9453. * Creates a Matrix2 instance from a column-major order array.
  9454. *
  9455. * @param {Number[]} values The column-major order array.
  9456. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9457. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9458. */
  9459. Matrix2.fromColumnMajorArray = function (values, result) {
  9460. //>>includeStart('debug', pragmas.debug);
  9461. RuntimeError.Check.defined("values", values);
  9462. //>>includeEnd('debug');
  9463. return Matrix2.clone(values, result);
  9464. };
  9465. /**
  9466. * Creates a Matrix2 instance from a row-major order array.
  9467. * The resulting matrix will be in column-major order.
  9468. *
  9469. * @param {Number[]} values The row-major order array.
  9470. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9471. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9472. */
  9473. Matrix2.fromRowMajorArray = function (values, result) {
  9474. //>>includeStart('debug', pragmas.debug);
  9475. RuntimeError.Check.defined("values", values);
  9476. //>>includeEnd('debug');
  9477. if (!defaultValue.defined(result)) {
  9478. return new Matrix2(values[0], values[1], values[2], values[3]);
  9479. }
  9480. result[0] = values[0];
  9481. result[1] = values[2];
  9482. result[2] = values[1];
  9483. result[3] = values[3];
  9484. return result;
  9485. };
  9486. /**
  9487. * Computes a Matrix2 instance representing a non-uniform scale.
  9488. *
  9489. * @param {Cartesian2} scale The x and y scale factors.
  9490. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9491. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9492. *
  9493. * @example
  9494. * // Creates
  9495. * // [7.0, 0.0]
  9496. * // [0.0, 8.0]
  9497. * const m = Cesium.Matrix2.fromScale(new Cesium.Cartesian2(7.0, 8.0));
  9498. */
  9499. Matrix2.fromScale = function (scale, result) {
  9500. //>>includeStart('debug', pragmas.debug);
  9501. RuntimeError.Check.typeOf.object("scale", scale);
  9502. //>>includeEnd('debug');
  9503. if (!defaultValue.defined(result)) {
  9504. return new Matrix2(scale.x, 0.0, 0.0, scale.y);
  9505. }
  9506. result[0] = scale.x;
  9507. result[1] = 0.0;
  9508. result[2] = 0.0;
  9509. result[3] = scale.y;
  9510. return result;
  9511. };
  9512. /**
  9513. * Computes a Matrix2 instance representing a uniform scale.
  9514. *
  9515. * @param {Number} scale The uniform scale factor.
  9516. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9517. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9518. *
  9519. * @example
  9520. * // Creates
  9521. * // [2.0, 0.0]
  9522. * // [0.0, 2.0]
  9523. * const m = Cesium.Matrix2.fromUniformScale(2.0);
  9524. */
  9525. Matrix2.fromUniformScale = function (scale, result) {
  9526. //>>includeStart('debug', pragmas.debug);
  9527. RuntimeError.Check.typeOf.number("scale", scale);
  9528. //>>includeEnd('debug');
  9529. if (!defaultValue.defined(result)) {
  9530. return new Matrix2(scale, 0.0, 0.0, scale);
  9531. }
  9532. result[0] = scale;
  9533. result[1] = 0.0;
  9534. result[2] = 0.0;
  9535. result[3] = scale;
  9536. return result;
  9537. };
  9538. /**
  9539. * Creates a rotation matrix.
  9540. *
  9541. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  9542. * @param {Matrix2} [result] The object in which the result will be stored, if undefined a new instance will be created.
  9543. * @returns {Matrix2} The modified result parameter, or a new Matrix2 instance if one was not provided.
  9544. *
  9545. * @example
  9546. * // Rotate a point 45 degrees counterclockwise.
  9547. * const p = new Cesium.Cartesian2(5, 6);
  9548. * const m = Cesium.Matrix2.fromRotation(Cesium.Math.toRadians(45.0));
  9549. * const rotated = Cesium.Matrix2.multiplyByVector(m, p, new Cesium.Cartesian2());
  9550. */
  9551. Matrix2.fromRotation = function (angle, result) {
  9552. //>>includeStart('debug', pragmas.debug);
  9553. RuntimeError.Check.typeOf.number("angle", angle);
  9554. //>>includeEnd('debug');
  9555. const cosAngle = Math.cos(angle);
  9556. const sinAngle = Math.sin(angle);
  9557. if (!defaultValue.defined(result)) {
  9558. return new Matrix2(cosAngle, -sinAngle, sinAngle, cosAngle);
  9559. }
  9560. result[0] = cosAngle;
  9561. result[1] = sinAngle;
  9562. result[2] = -sinAngle;
  9563. result[3] = cosAngle;
  9564. return result;
  9565. };
  9566. /**
  9567. * Creates an Array from the provided Matrix2 instance.
  9568. * The array will be in column-major order.
  9569. *
  9570. * @param {Matrix2} matrix The matrix to use..
  9571. * @param {Number[]} [result] The Array onto which to store the result.
  9572. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  9573. */
  9574. Matrix2.toArray = function (matrix, result) {
  9575. //>>includeStart('debug', pragmas.debug);
  9576. RuntimeError.Check.typeOf.object("matrix", matrix);
  9577. //>>includeEnd('debug');
  9578. if (!defaultValue.defined(result)) {
  9579. return [matrix[0], matrix[1], matrix[2], matrix[3]];
  9580. }
  9581. result[0] = matrix[0];
  9582. result[1] = matrix[1];
  9583. result[2] = matrix[2];
  9584. result[3] = matrix[3];
  9585. return result;
  9586. };
  9587. /**
  9588. * Computes the array index of the element at the provided row and column.
  9589. *
  9590. * @param {Number} row The zero-based index of the row.
  9591. * @param {Number} column The zero-based index of the column.
  9592. * @returns {Number} The index of the element at the provided row and column.
  9593. *
  9594. * @exception {DeveloperError} row must be 0 or 1.
  9595. * @exception {DeveloperError} column must be 0 or 1.
  9596. *
  9597. * @example
  9598. * const myMatrix = new Cesium.Matrix2();
  9599. * const column1Row0Index = Cesium.Matrix2.getElementIndex(1, 0);
  9600. * const column1Row0 = myMatrix[column1Row0Index]
  9601. * myMatrix[column1Row0Index] = 10.0;
  9602. */
  9603. Matrix2.getElementIndex = function (column, row) {
  9604. //>>includeStart('debug', pragmas.debug);
  9605. RuntimeError.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  9606. RuntimeError.Check.typeOf.number.lessThanOrEquals("row", row, 1);
  9607. RuntimeError.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  9608. RuntimeError.Check.typeOf.number.lessThanOrEquals("column", column, 1);
  9609. //>>includeEnd('debug');
  9610. return column * 2 + row;
  9611. };
  9612. /**
  9613. * Retrieves a copy of the matrix column at the provided index as a Cartesian2 instance.
  9614. *
  9615. * @param {Matrix2} matrix The matrix to use.
  9616. * @param {Number} index The zero-based index of the column to retrieve.
  9617. * @param {Cartesian2} result The object onto which to store the result.
  9618. * @returns {Cartesian2} The modified result parameter.
  9619. *
  9620. * @exception {DeveloperError} index must be 0 or 1.
  9621. */
  9622. Matrix2.getColumn = function (matrix, index, result) {
  9623. //>>includeStart('debug', pragmas.debug);
  9624. RuntimeError.Check.typeOf.object("matrix", matrix);
  9625. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9626. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9627. RuntimeError.Check.typeOf.object("result", result);
  9628. //>>includeEnd('debug');
  9629. const startIndex = index * 2;
  9630. const x = matrix[startIndex];
  9631. const y = matrix[startIndex + 1];
  9632. result.x = x;
  9633. result.y = y;
  9634. return result;
  9635. };
  9636. /**
  9637. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian2 instance.
  9638. *
  9639. * @param {Matrix2} matrix The matrix to use.
  9640. * @param {Number} index The zero-based index of the column to set.
  9641. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified column.
  9642. * @param {Cartesian2} result The object onto which to store the result.
  9643. * @returns {Matrix2} The modified result parameter.
  9644. *
  9645. * @exception {DeveloperError} index must be 0 or 1.
  9646. */
  9647. Matrix2.setColumn = function (matrix, index, cartesian, result) {
  9648. //>>includeStart('debug', pragmas.debug);
  9649. RuntimeError.Check.typeOf.object("matrix", matrix);
  9650. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9651. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9652. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9653. RuntimeError.Check.typeOf.object("result", result);
  9654. //>>includeEnd('debug');
  9655. result = Matrix2.clone(matrix, result);
  9656. const startIndex = index * 2;
  9657. result[startIndex] = cartesian.x;
  9658. result[startIndex + 1] = cartesian.y;
  9659. return result;
  9660. };
  9661. /**
  9662. * Retrieves a copy of the matrix row at the provided index as a Cartesian2 instance.
  9663. *
  9664. * @param {Matrix2} matrix The matrix to use.
  9665. * @param {Number} index The zero-based index of the row to retrieve.
  9666. * @param {Cartesian2} result The object onto which to store the result.
  9667. * @returns {Cartesian2} The modified result parameter.
  9668. *
  9669. * @exception {DeveloperError} index must be 0 or 1.
  9670. */
  9671. Matrix2.getRow = function (matrix, index, result) {
  9672. //>>includeStart('debug', pragmas.debug);
  9673. RuntimeError.Check.typeOf.object("matrix", matrix);
  9674. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9675. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9676. RuntimeError.Check.typeOf.object("result", result);
  9677. //>>includeEnd('debug');
  9678. const x = matrix[index];
  9679. const y = matrix[index + 2];
  9680. result.x = x;
  9681. result.y = y;
  9682. return result;
  9683. };
  9684. /**
  9685. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian2 instance.
  9686. *
  9687. * @param {Matrix2} matrix The matrix to use.
  9688. * @param {Number} index The zero-based index of the row to set.
  9689. * @param {Cartesian2} cartesian The Cartesian whose values will be assigned to the specified row.
  9690. * @param {Matrix2} result The object onto which to store the result.
  9691. * @returns {Matrix2} The modified result parameter.
  9692. *
  9693. * @exception {DeveloperError} index must be 0 or 1.
  9694. */
  9695. Matrix2.setRow = function (matrix, index, cartesian, result) {
  9696. //>>includeStart('debug', pragmas.debug);
  9697. RuntimeError.Check.typeOf.object("matrix", matrix);
  9698. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  9699. RuntimeError.Check.typeOf.number.lessThanOrEquals("index", index, 1);
  9700. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9701. RuntimeError.Check.typeOf.object("result", result);
  9702. //>>includeEnd('debug');
  9703. result = Matrix2.clone(matrix, result);
  9704. result[index] = cartesian.x;
  9705. result[index + 2] = cartesian.y;
  9706. return result;
  9707. };
  9708. const scaleScratch1 = new Cartesian2();
  9709. /**
  9710. * Computes a new matrix that replaces the scale with the provided scale.
  9711. * This assumes the matrix is an affine transformation.
  9712. *
  9713. * @param {Matrix2} matrix The matrix to use.
  9714. * @param {Cartesian2} scale The scale that replaces the scale of the provided matrix.
  9715. * @param {Matrix2} result The object onto which to store the result.
  9716. * @returns {Matrix2} The modified result parameter.
  9717. *
  9718. * @see Matrix2.setUniformScale
  9719. * @see Matrix2.fromScale
  9720. * @see Matrix2.fromUniformScale
  9721. * @see Matrix2.multiplyByScale
  9722. * @see Matrix2.multiplyByUniformScale
  9723. * @see Matrix2.getScale
  9724. */
  9725. Matrix2.setScale = function (matrix, scale, result) {
  9726. //>>includeStart('debug', pragmas.debug);
  9727. RuntimeError.Check.typeOf.object("matrix", matrix);
  9728. RuntimeError.Check.typeOf.object("scale", scale);
  9729. RuntimeError.Check.typeOf.object("result", result);
  9730. //>>includeEnd('debug');
  9731. const existingScale = Matrix2.getScale(matrix, scaleScratch1);
  9732. const scaleRatioX = scale.x / existingScale.x;
  9733. const scaleRatioY = scale.y / existingScale.y;
  9734. result[0] = matrix[0] * scaleRatioX;
  9735. result[1] = matrix[1] * scaleRatioX;
  9736. result[2] = matrix[2] * scaleRatioY;
  9737. result[3] = matrix[3] * scaleRatioY;
  9738. return result;
  9739. };
  9740. const scaleScratch2 = new Cartesian2();
  9741. /**
  9742. * Computes a new matrix that replaces the scale with the provided uniform scale.
  9743. * This assumes the matrix is an affine transformation.
  9744. *
  9745. * @param {Matrix2} matrix The matrix to use.
  9746. * @param {Number} scale The uniform scale that replaces the scale of the provided matrix.
  9747. * @param {Matrix2} result The object onto which to store the result.
  9748. * @returns {Matrix2} The modified result parameter.
  9749. *
  9750. * @see Matrix2.setScale
  9751. * @see Matrix2.fromScale
  9752. * @see Matrix2.fromUniformScale
  9753. * @see Matrix2.multiplyByScale
  9754. * @see Matrix2.multiplyByUniformScale
  9755. * @see Matrix2.getScale
  9756. */
  9757. Matrix2.setUniformScale = function (matrix, scale, result) {
  9758. //>>includeStart('debug', pragmas.debug);
  9759. RuntimeError.Check.typeOf.object("matrix", matrix);
  9760. RuntimeError.Check.typeOf.number("scale", scale);
  9761. RuntimeError.Check.typeOf.object("result", result);
  9762. //>>includeEnd('debug');
  9763. const existingScale = Matrix2.getScale(matrix, scaleScratch2);
  9764. const scaleRatioX = scale / existingScale.x;
  9765. const scaleRatioY = scale / existingScale.y;
  9766. result[0] = matrix[0] * scaleRatioX;
  9767. result[1] = matrix[1] * scaleRatioX;
  9768. result[2] = matrix[2] * scaleRatioY;
  9769. result[3] = matrix[3] * scaleRatioY;
  9770. return result;
  9771. };
  9772. const scratchColumn = new Cartesian2();
  9773. /**
  9774. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  9775. *
  9776. * @param {Matrix2} matrix The matrix.
  9777. * @param {Cartesian2} result The object onto which to store the result.
  9778. * @returns {Cartesian2} The modified result parameter.
  9779. *
  9780. * @see Matrix2.multiplyByScale
  9781. * @see Matrix2.multiplyByUniformScale
  9782. * @see Matrix2.fromScale
  9783. * @see Matrix2.fromUniformScale
  9784. * @see Matrix2.setScale
  9785. * @see Matrix2.setUniformScale
  9786. */
  9787. Matrix2.getScale = function (matrix, result) {
  9788. //>>includeStart('debug', pragmas.debug);
  9789. RuntimeError.Check.typeOf.object("matrix", matrix);
  9790. RuntimeError.Check.typeOf.object("result", result);
  9791. //>>includeEnd('debug');
  9792. result.x = Cartesian2.magnitude(
  9793. Cartesian2.fromElements(matrix[0], matrix[1], scratchColumn)
  9794. );
  9795. result.y = Cartesian2.magnitude(
  9796. Cartesian2.fromElements(matrix[2], matrix[3], scratchColumn)
  9797. );
  9798. return result;
  9799. };
  9800. const scaleScratch3 = new Cartesian2();
  9801. /**
  9802. * Computes the maximum scale assuming the matrix is an affine transformation.
  9803. * The maximum scale is the maximum length of the column vectors.
  9804. *
  9805. * @param {Matrix2} matrix The matrix.
  9806. * @returns {Number} The maximum scale.
  9807. */
  9808. Matrix2.getMaximumScale = function (matrix) {
  9809. Matrix2.getScale(matrix, scaleScratch3);
  9810. return Cartesian2.maximumComponent(scaleScratch3);
  9811. };
  9812. const scaleScratch4 = new Cartesian2();
  9813. /**
  9814. * Sets the rotation assuming the matrix is an affine transformation.
  9815. *
  9816. * @param {Matrix2} matrix The matrix.
  9817. * @param {Matrix2} rotation The rotation matrix.
  9818. * @returns {Matrix2} The modified result parameter.
  9819. *
  9820. * @see Matrix2.fromRotation
  9821. * @see Matrix2.getRotation
  9822. */
  9823. Matrix2.setRotation = function (matrix, rotation, result) {
  9824. //>>includeStart('debug', pragmas.debug);
  9825. RuntimeError.Check.typeOf.object("matrix", matrix);
  9826. RuntimeError.Check.typeOf.object("result", result);
  9827. //>>includeEnd('debug');
  9828. const scale = Matrix2.getScale(matrix, scaleScratch4);
  9829. result[0] = rotation[0] * scale.x;
  9830. result[1] = rotation[1] * scale.x;
  9831. result[2] = rotation[2] * scale.y;
  9832. result[3] = rotation[3] * scale.y;
  9833. return result;
  9834. };
  9835. const scaleScratch5 = new Cartesian2();
  9836. /**
  9837. * Extracts the rotation matrix assuming the matrix is an affine transformation.
  9838. *
  9839. * @param {Matrix2} matrix The matrix.
  9840. * @param {Matrix2} result The object onto which to store the result.
  9841. * @returns {Matrix2} The modified result parameter.
  9842. *
  9843. * @see Matrix2.setRotation
  9844. * @see Matrix2.fromRotation
  9845. */
  9846. Matrix2.getRotation = function (matrix, result) {
  9847. //>>includeStart('debug', pragmas.debug);
  9848. RuntimeError.Check.typeOf.object("matrix", matrix);
  9849. RuntimeError.Check.typeOf.object("result", result);
  9850. //>>includeEnd('debug');
  9851. const scale = Matrix2.getScale(matrix, scaleScratch5);
  9852. result[0] = matrix[0] / scale.x;
  9853. result[1] = matrix[1] / scale.x;
  9854. result[2] = matrix[2] / scale.y;
  9855. result[3] = matrix[3] / scale.y;
  9856. return result;
  9857. };
  9858. /**
  9859. * Computes the product of two matrices.
  9860. *
  9861. * @param {Matrix2} left The first matrix.
  9862. * @param {Matrix2} right The second matrix.
  9863. * @param {Matrix2} result The object onto which to store the result.
  9864. * @returns {Matrix2} The modified result parameter.
  9865. */
  9866. Matrix2.multiply = function (left, right, result) {
  9867. //>>includeStart('debug', pragmas.debug);
  9868. RuntimeError.Check.typeOf.object("left", left);
  9869. RuntimeError.Check.typeOf.object("right", right);
  9870. RuntimeError.Check.typeOf.object("result", result);
  9871. //>>includeEnd('debug');
  9872. const column0Row0 = left[0] * right[0] + left[2] * right[1];
  9873. const column1Row0 = left[0] * right[2] + left[2] * right[3];
  9874. const column0Row1 = left[1] * right[0] + left[3] * right[1];
  9875. const column1Row1 = left[1] * right[2] + left[3] * right[3];
  9876. result[0] = column0Row0;
  9877. result[1] = column0Row1;
  9878. result[2] = column1Row0;
  9879. result[3] = column1Row1;
  9880. return result;
  9881. };
  9882. /**
  9883. * Computes the sum of two matrices.
  9884. *
  9885. * @param {Matrix2} left The first matrix.
  9886. * @param {Matrix2} right The second matrix.
  9887. * @param {Matrix2} result The object onto which to store the result.
  9888. * @returns {Matrix2} The modified result parameter.
  9889. */
  9890. Matrix2.add = function (left, right, result) {
  9891. //>>includeStart('debug', pragmas.debug);
  9892. RuntimeError.Check.typeOf.object("left", left);
  9893. RuntimeError.Check.typeOf.object("right", right);
  9894. RuntimeError.Check.typeOf.object("result", result);
  9895. //>>includeEnd('debug');
  9896. result[0] = left[0] + right[0];
  9897. result[1] = left[1] + right[1];
  9898. result[2] = left[2] + right[2];
  9899. result[3] = left[3] + right[3];
  9900. return result;
  9901. };
  9902. /**
  9903. * Computes the difference of two matrices.
  9904. *
  9905. * @param {Matrix2} left The first matrix.
  9906. * @param {Matrix2} right The second matrix.
  9907. * @param {Matrix2} result The object onto which to store the result.
  9908. * @returns {Matrix2} The modified result parameter.
  9909. */
  9910. Matrix2.subtract = function (left, right, result) {
  9911. //>>includeStart('debug', pragmas.debug);
  9912. RuntimeError.Check.typeOf.object("left", left);
  9913. RuntimeError.Check.typeOf.object("right", right);
  9914. RuntimeError.Check.typeOf.object("result", result);
  9915. //>>includeEnd('debug');
  9916. result[0] = left[0] - right[0];
  9917. result[1] = left[1] - right[1];
  9918. result[2] = left[2] - right[2];
  9919. result[3] = left[3] - right[3];
  9920. return result;
  9921. };
  9922. /**
  9923. * Computes the product of a matrix and a column vector.
  9924. *
  9925. * @param {Matrix2} matrix The matrix.
  9926. * @param {Cartesian2} cartesian The column.
  9927. * @param {Cartesian2} result The object onto which to store the result.
  9928. * @returns {Cartesian2} The modified result parameter.
  9929. */
  9930. Matrix2.multiplyByVector = function (matrix, cartesian, result) {
  9931. //>>includeStart('debug', pragmas.debug);
  9932. RuntimeError.Check.typeOf.object("matrix", matrix);
  9933. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  9934. RuntimeError.Check.typeOf.object("result", result);
  9935. //>>includeEnd('debug');
  9936. const x = matrix[0] * cartesian.x + matrix[2] * cartesian.y;
  9937. const y = matrix[1] * cartesian.x + matrix[3] * cartesian.y;
  9938. result.x = x;
  9939. result.y = y;
  9940. return result;
  9941. };
  9942. /**
  9943. * Computes the product of a matrix and a scalar.
  9944. *
  9945. * @param {Matrix2} matrix The matrix.
  9946. * @param {Number} scalar The number to multiply by.
  9947. * @param {Matrix2} result The object onto which to store the result.
  9948. * @returns {Matrix2} The modified result parameter.
  9949. */
  9950. Matrix2.multiplyByScalar = function (matrix, scalar, result) {
  9951. //>>includeStart('debug', pragmas.debug);
  9952. RuntimeError.Check.typeOf.object("matrix", matrix);
  9953. RuntimeError.Check.typeOf.number("scalar", scalar);
  9954. RuntimeError.Check.typeOf.object("result", result);
  9955. //>>includeEnd('debug');
  9956. result[0] = matrix[0] * scalar;
  9957. result[1] = matrix[1] * scalar;
  9958. result[2] = matrix[2] * scalar;
  9959. result[3] = matrix[3] * scalar;
  9960. return result;
  9961. };
  9962. /**
  9963. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  9964. *
  9965. * @param {Matrix2} matrix The matrix on the left-hand side.
  9966. * @param {Number} scale The non-uniform scale on the right-hand side.
  9967. * @param {Matrix2} result The object onto which to store the result.
  9968. * @returns {Matrix2} The modified result parameter.
  9969. *
  9970. *
  9971. * @example
  9972. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
  9973. * Cesium.Matrix2.multiplyByScale(m, scale, m);
  9974. *
  9975. * @see Matrix2.multiplyByUniformScale
  9976. * @see Matrix2.fromScale
  9977. * @see Matrix2.fromUniformScale
  9978. * @see Matrix2.setScale
  9979. * @see Matrix2.setUniformScale
  9980. * @see Matrix2.getScale
  9981. */
  9982. Matrix2.multiplyByScale = function (matrix, scale, result) {
  9983. //>>includeStart('debug', pragmas.debug);
  9984. RuntimeError.Check.typeOf.object("matrix", matrix);
  9985. RuntimeError.Check.typeOf.object("scale", scale);
  9986. RuntimeError.Check.typeOf.object("result", result);
  9987. //>>includeEnd('debug');
  9988. result[0] = matrix[0] * scale.x;
  9989. result[1] = matrix[1] * scale.x;
  9990. result[2] = matrix[2] * scale.y;
  9991. result[3] = matrix[3] * scale.y;
  9992. return result;
  9993. };
  9994. /**
  9995. * Computes the product of a matrix times a uniform scale, as if the scale were a scale matrix.
  9996. *
  9997. * @param {Matrix2} matrix The matrix on the left-hand side.
  9998. * @param {Number} scale The uniform scale on the right-hand side.
  9999. * @param {Matrix2} result The object onto which to store the result.
  10000. * @returns {Matrix2} The modified result parameter.
  10001. *
  10002. * @example
  10003. * // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromUniformScale(scale), m);
  10004. * Cesium.Matrix2.multiplyByUniformScale(m, scale, m);
  10005. *
  10006. * @see Matrix2.multiplyByScale
  10007. * @see Matrix2.fromScale
  10008. * @see Matrix2.fromUniformScale
  10009. * @see Matrix2.setScale
  10010. * @see Matrix2.setUniformScale
  10011. * @see Matrix2.getScale
  10012. */
  10013. Matrix2.multiplyByUniformScale = function (matrix, scale, result) {
  10014. //>>includeStart('debug', pragmas.debug);
  10015. RuntimeError.Check.typeOf.object("matrix", matrix);
  10016. RuntimeError.Check.typeOf.number("scale", scale);
  10017. RuntimeError.Check.typeOf.object("result", result);
  10018. //>>includeEnd('debug');
  10019. result[0] = matrix[0] * scale;
  10020. result[1] = matrix[1] * scale;
  10021. result[2] = matrix[2] * scale;
  10022. result[3] = matrix[3] * scale;
  10023. return result;
  10024. };
  10025. /**
  10026. * Creates a negated copy of the provided matrix.
  10027. *
  10028. * @param {Matrix2} matrix The matrix to negate.
  10029. * @param {Matrix2} result The object onto which to store the result.
  10030. * @returns {Matrix2} The modified result parameter.
  10031. */
  10032. Matrix2.negate = function (matrix, result) {
  10033. //>>includeStart('debug', pragmas.debug);
  10034. RuntimeError.Check.typeOf.object("matrix", matrix);
  10035. RuntimeError.Check.typeOf.object("result", result);
  10036. //>>includeEnd('debug');
  10037. result[0] = -matrix[0];
  10038. result[1] = -matrix[1];
  10039. result[2] = -matrix[2];
  10040. result[3] = -matrix[3];
  10041. return result;
  10042. };
  10043. /**
  10044. * Computes the transpose of the provided matrix.
  10045. *
  10046. * @param {Matrix2} matrix The matrix to transpose.
  10047. * @param {Matrix2} result The object onto which to store the result.
  10048. * @returns {Matrix2} The modified result parameter.
  10049. */
  10050. Matrix2.transpose = function (matrix, result) {
  10051. //>>includeStart('debug', pragmas.debug);
  10052. RuntimeError.Check.typeOf.object("matrix", matrix);
  10053. RuntimeError.Check.typeOf.object("result", result);
  10054. //>>includeEnd('debug');
  10055. const column0Row0 = matrix[0];
  10056. const column0Row1 = matrix[2];
  10057. const column1Row0 = matrix[1];
  10058. const column1Row1 = matrix[3];
  10059. result[0] = column0Row0;
  10060. result[1] = column0Row1;
  10061. result[2] = column1Row0;
  10062. result[3] = column1Row1;
  10063. return result;
  10064. };
  10065. /**
  10066. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  10067. *
  10068. * @param {Matrix2} matrix The matrix with signed elements.
  10069. * @param {Matrix2} result The object onto which to store the result.
  10070. * @returns {Matrix2} The modified result parameter.
  10071. */
  10072. Matrix2.abs = function (matrix, result) {
  10073. //>>includeStart('debug', pragmas.debug);
  10074. RuntimeError.Check.typeOf.object("matrix", matrix);
  10075. RuntimeError.Check.typeOf.object("result", result);
  10076. //>>includeEnd('debug');
  10077. result[0] = Math.abs(matrix[0]);
  10078. result[1] = Math.abs(matrix[1]);
  10079. result[2] = Math.abs(matrix[2]);
  10080. result[3] = Math.abs(matrix[3]);
  10081. return result;
  10082. };
  10083. /**
  10084. * Compares the provided matrices componentwise and returns
  10085. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10086. *
  10087. * @param {Matrix2} [left] The first matrix.
  10088. * @param {Matrix2} [right] The second matrix.
  10089. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  10090. */
  10091. Matrix2.equals = function (left, right) {
  10092. return (
  10093. left === right ||
  10094. (defaultValue.defined(left) &&
  10095. defaultValue.defined(right) &&
  10096. left[0] === right[0] &&
  10097. left[1] === right[1] &&
  10098. left[2] === right[2] &&
  10099. left[3] === right[3])
  10100. );
  10101. };
  10102. /**
  10103. * @private
  10104. */
  10105. Matrix2.equalsArray = function (matrix, array, offset) {
  10106. return (
  10107. matrix[0] === array[offset] &&
  10108. matrix[1] === array[offset + 1] &&
  10109. matrix[2] === array[offset + 2] &&
  10110. matrix[3] === array[offset + 3]
  10111. );
  10112. };
  10113. /**
  10114. * Compares the provided matrices componentwise and returns
  10115. * <code>true</code> if they are within the provided epsilon,
  10116. * <code>false</code> otherwise.
  10117. *
  10118. * @param {Matrix2} [left] The first matrix.
  10119. * @param {Matrix2} [right] The second matrix.
  10120. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  10121. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  10122. */
  10123. Matrix2.equalsEpsilon = function (left, right, epsilon) {
  10124. epsilon = defaultValue.defaultValue(epsilon, 0);
  10125. return (
  10126. left === right ||
  10127. (defaultValue.defined(left) &&
  10128. defaultValue.defined(right) &&
  10129. Math.abs(left[0] - right[0]) <= epsilon &&
  10130. Math.abs(left[1] - right[1]) <= epsilon &&
  10131. Math.abs(left[2] - right[2]) <= epsilon &&
  10132. Math.abs(left[3] - right[3]) <= epsilon)
  10133. );
  10134. };
  10135. /**
  10136. * An immutable Matrix2 instance initialized to the identity matrix.
  10137. *
  10138. * @type {Matrix2}
  10139. * @constant
  10140. */
  10141. Matrix2.IDENTITY = Object.freeze(new Matrix2(1.0, 0.0, 0.0, 1.0));
  10142. /**
  10143. * An immutable Matrix2 instance initialized to the zero matrix.
  10144. *
  10145. * @type {Matrix2}
  10146. * @constant
  10147. */
  10148. Matrix2.ZERO = Object.freeze(new Matrix2(0.0, 0.0, 0.0, 0.0));
  10149. /**
  10150. * The index into Matrix2 for column 0, row 0.
  10151. *
  10152. * @type {Number}
  10153. * @constant
  10154. *
  10155. * @example
  10156. * const matrix = new Cesium.Matrix2();
  10157. * matrix[Cesium.Matrix2.COLUMN0ROW0] = 5.0; // set column 0, row 0 to 5.0
  10158. */
  10159. Matrix2.COLUMN0ROW0 = 0;
  10160. /**
  10161. * The index into Matrix2 for column 0, row 1.
  10162. *
  10163. * @type {Number}
  10164. * @constant
  10165. *
  10166. * @example
  10167. * const matrix = new Cesium.Matrix2();
  10168. * matrix[Cesium.Matrix2.COLUMN0ROW1] = 5.0; // set column 0, row 1 to 5.0
  10169. */
  10170. Matrix2.COLUMN0ROW1 = 1;
  10171. /**
  10172. * The index into Matrix2 for column 1, row 0.
  10173. *
  10174. * @type {Number}
  10175. * @constant
  10176. *
  10177. * @example
  10178. * const matrix = new Cesium.Matrix2();
  10179. * matrix[Cesium.Matrix2.COLUMN1ROW0] = 5.0; // set column 1, row 0 to 5.0
  10180. */
  10181. Matrix2.COLUMN1ROW0 = 2;
  10182. /**
  10183. * The index into Matrix2 for column 1, row 1.
  10184. *
  10185. * @type {Number}
  10186. * @constant
  10187. *
  10188. * @example
  10189. * const matrix = new Cesium.Matrix2();
  10190. * matrix[Cesium.Matrix2.COLUMN1ROW1] = 5.0; // set column 1, row 1 to 5.0
  10191. */
  10192. Matrix2.COLUMN1ROW1 = 3;
  10193. Object.defineProperties(Matrix2.prototype, {
  10194. /**
  10195. * Gets the number of items in the collection.
  10196. * @memberof Matrix2.prototype
  10197. *
  10198. * @type {Number}
  10199. */
  10200. length: {
  10201. get: function () {
  10202. return Matrix2.packedLength;
  10203. },
  10204. },
  10205. });
  10206. /**
  10207. * Duplicates the provided Matrix2 instance.
  10208. *
  10209. * @param {Matrix2} [result] The object onto which to store the result.
  10210. * @returns {Matrix2} The modified result parameter or a new Matrix2 instance if one was not provided.
  10211. */
  10212. Matrix2.prototype.clone = function (result) {
  10213. return Matrix2.clone(this, result);
  10214. };
  10215. /**
  10216. * Compares this matrix to the provided matrix componentwise and returns
  10217. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10218. *
  10219. * @param {Matrix2} [right] The right hand side matrix.
  10220. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  10221. */
  10222. Matrix2.prototype.equals = function (right) {
  10223. return Matrix2.equals(this, right);
  10224. };
  10225. /**
  10226. * Compares this matrix to the provided matrix componentwise and returns
  10227. * <code>true</code> if they are within the provided epsilon,
  10228. * <code>false</code> otherwise.
  10229. *
  10230. * @param {Matrix2} [right] The right hand side matrix.
  10231. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  10232. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  10233. */
  10234. Matrix2.prototype.equalsEpsilon = function (right, epsilon) {
  10235. return Matrix2.equalsEpsilon(this, right, epsilon);
  10236. };
  10237. /**
  10238. * Creates a string representing this Matrix with each row being
  10239. * on a separate line and in the format '(column0, column1)'.
  10240. *
  10241. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1)'.
  10242. */
  10243. Matrix2.prototype.toString = function () {
  10244. return `(${this[0]}, ${this[2]})\n` + `(${this[1]}, ${this[3]})`;
  10245. };
  10246. exports.Cartesian2 = Cartesian2;
  10247. exports.Cartesian3 = Cartesian3;
  10248. exports.Cartesian4 = Cartesian4;
  10249. exports.Cartographic = Cartographic;
  10250. exports.Ellipsoid = Ellipsoid;
  10251. exports.Matrix2 = Matrix2;
  10252. exports.Matrix3 = Matrix3;
  10253. exports.Matrix4 = Matrix4;
  10254. exports.Rectangle = Rectangle;
  10255. }));
  10256. //# sourceMappingURL=Matrix2-9e1c22e2.js.map