Transforms-8be64844.js 569 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-54c2dc71', './Check-6c0211bc', './Math-1124a290', './Cartesian2-33d2657c', './RuntimeError-2109023a'], function (exports, when, Check, _Math, Cartesian2, RuntimeError) { 'use strict';
  24. /**
  25. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  26. * them by the {@link Ellipsoid#maximumRadius}. This projection
  27. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  28. * is also known as EPSG:4326.
  29. *
  30. * @alias GeographicProjection
  31. * @constructor
  32. *
  33. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  34. *
  35. * @see WebMercatorProjection
  36. */
  37. function GeographicProjection(ellipsoid) {
  38. this._ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  39. this._semimajorAxis = this._ellipsoid.maximumRadius;
  40. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  41. }
  42. Object.defineProperties(GeographicProjection.prototype, {
  43. /**
  44. * Gets the {@link Ellipsoid}.
  45. *
  46. * @memberof GeographicProjection.prototype
  47. *
  48. * @type {Ellipsoid}
  49. * @readonly
  50. */
  51. ellipsoid: {
  52. get: function () {
  53. return this._ellipsoid;
  54. },
  55. },
  56. });
  57. /**
  58. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  59. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  60. * ellipsoid. Z is the unmodified height.
  61. *
  62. * @param {Cartographic} cartographic The coordinates to project.
  63. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  64. * undefined, a new instance is created and returned.
  65. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  66. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  67. * created and returned.
  68. */
  69. GeographicProjection.prototype.project = function (cartographic, result) {
  70. // Actually this is the special case of equidistant cylindrical called the plate carree
  71. var semimajorAxis = this._semimajorAxis;
  72. var x = cartographic.longitude * semimajorAxis;
  73. var y = cartographic.latitude * semimajorAxis;
  74. var z = cartographic.height;
  75. if (!when.defined(result)) {
  76. return new Cartesian2.Cartesian3(x, y, z);
  77. }
  78. result.x = x;
  79. result.y = y;
  80. result.z = z;
  81. return result;
  82. };
  83. /**
  84. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  85. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  86. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  87. *
  88. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  89. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  90. * undefined, a new instance is created and returned.
  91. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  92. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  93. * created and returned.
  94. */
  95. GeographicProjection.prototype.unproject = function (cartesian, result) {
  96. //>>includeStart('debug', pragmas.debug);
  97. if (!when.defined(cartesian)) {
  98. throw new Check.DeveloperError("cartesian is required");
  99. }
  100. //>>includeEnd('debug');
  101. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  102. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  103. var latitude = cartesian.y * oneOverEarthSemimajorAxis;
  104. var height = cartesian.z;
  105. if (!when.defined(result)) {
  106. return new Cartesian2.Cartographic(longitude, latitude, height);
  107. }
  108. result.longitude = longitude;
  109. result.latitude = latitude;
  110. result.height = height;
  111. return result;
  112. };
  113. /**
  114. * This enumerated type is used in determining where, relative to the frustum, an
  115. * object is located. The object can either be fully contained within the frustum (INSIDE),
  116. * partially inside the frustum and partially outside (INTERSECTING), or somewhere entirely
  117. * outside of the frustum's 6 planes (OUTSIDE).
  118. *
  119. * @enum {Number}
  120. */
  121. var Intersect = {
  122. /**
  123. * Represents that an object is not contained within the frustum.
  124. *
  125. * @type {Number}
  126. * @constant
  127. */
  128. OUTSIDE: -1,
  129. /**
  130. * Represents that an object intersects one of the frustum's planes.
  131. *
  132. * @type {Number}
  133. * @constant
  134. */
  135. INTERSECTING: 0,
  136. /**
  137. * Represents that an object is fully within the frustum.
  138. *
  139. * @type {Number}
  140. * @constant
  141. */
  142. INSIDE: 1,
  143. };
  144. var Intersect$1 = Object.freeze(Intersect);
  145. /**
  146. * Represents the closed interval [start, stop].
  147. * @alias Interval
  148. * @constructor
  149. *
  150. * @param {Number} [start=0.0] The beginning of the interval.
  151. * @param {Number} [stop=0.0] The end of the interval.
  152. */
  153. function Interval(start, stop) {
  154. /**
  155. * The beginning of the interval.
  156. * @type {Number}
  157. * @default 0.0
  158. */
  159. this.start = when.defaultValue(start, 0.0);
  160. /**
  161. * The end of the interval.
  162. * @type {Number}
  163. * @default 0.0
  164. */
  165. this.stop = when.defaultValue(stop, 0.0);
  166. }
  167. /**
  168. * A 3x3 matrix, indexable as a column-major order array.
  169. * Constructor parameters are in row-major order for code readability.
  170. * @alias Matrix3
  171. * @constructor
  172. * @implements {ArrayLike<number>}
  173. *
  174. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  175. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  176. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  177. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  178. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  179. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  180. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  181. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  182. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  183. *
  184. * @see Matrix3.fromColumnMajorArray
  185. * @see Matrix3.fromRowMajorArray
  186. * @see Matrix3.fromQuaternion
  187. * @see Matrix3.fromScale
  188. * @see Matrix3.fromUniformScale
  189. * @see Matrix2
  190. * @see Matrix4
  191. */
  192. function Matrix3(
  193. column0Row0,
  194. column1Row0,
  195. column2Row0,
  196. column0Row1,
  197. column1Row1,
  198. column2Row1,
  199. column0Row2,
  200. column1Row2,
  201. column2Row2
  202. ) {
  203. this[0] = when.defaultValue(column0Row0, 0.0);
  204. this[1] = when.defaultValue(column0Row1, 0.0);
  205. this[2] = when.defaultValue(column0Row2, 0.0);
  206. this[3] = when.defaultValue(column1Row0, 0.0);
  207. this[4] = when.defaultValue(column1Row1, 0.0);
  208. this[5] = when.defaultValue(column1Row2, 0.0);
  209. this[6] = when.defaultValue(column2Row0, 0.0);
  210. this[7] = when.defaultValue(column2Row1, 0.0);
  211. this[8] = when.defaultValue(column2Row2, 0.0);
  212. }
  213. /**
  214. * The number of elements used to pack the object into an array.
  215. * @type {Number}
  216. */
  217. Matrix3.packedLength = 9;
  218. /**
  219. * Stores the provided instance into the provided array.
  220. *
  221. * @param {Matrix3} value The value to pack.
  222. * @param {Number[]} array The array to pack into.
  223. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  224. *
  225. * @returns {Number[]} The array that was packed into
  226. */
  227. Matrix3.pack = function (value, array, startingIndex) {
  228. //>>includeStart('debug', pragmas.debug);
  229. Check.Check.typeOf.object("value", value);
  230. Check.Check.defined("array", array);
  231. //>>includeEnd('debug');
  232. startingIndex = when.defaultValue(startingIndex, 0);
  233. array[startingIndex++] = value[0];
  234. array[startingIndex++] = value[1];
  235. array[startingIndex++] = value[2];
  236. array[startingIndex++] = value[3];
  237. array[startingIndex++] = value[4];
  238. array[startingIndex++] = value[5];
  239. array[startingIndex++] = value[6];
  240. array[startingIndex++] = value[7];
  241. array[startingIndex++] = value[8];
  242. return array;
  243. };
  244. /**
  245. * Retrieves an instance from a packed array.
  246. *
  247. * @param {Number[]} array The packed array.
  248. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  249. * @param {Matrix3} [result] The object into which to store the result.
  250. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  251. */
  252. Matrix3.unpack = function (array, startingIndex, result) {
  253. //>>includeStart('debug', pragmas.debug);
  254. Check.Check.defined("array", array);
  255. //>>includeEnd('debug');
  256. startingIndex = when.defaultValue(startingIndex, 0);
  257. if (!when.defined(result)) {
  258. result = new Matrix3();
  259. }
  260. result[0] = array[startingIndex++];
  261. result[1] = array[startingIndex++];
  262. result[2] = array[startingIndex++];
  263. result[3] = array[startingIndex++];
  264. result[4] = array[startingIndex++];
  265. result[5] = array[startingIndex++];
  266. result[6] = array[startingIndex++];
  267. result[7] = array[startingIndex++];
  268. result[8] = array[startingIndex++];
  269. return result;
  270. };
  271. /**
  272. * Duplicates a Matrix3 instance.
  273. *
  274. * @param {Matrix3} matrix The matrix to duplicate.
  275. * @param {Matrix3} [result] The object onto which to store the result.
  276. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  277. */
  278. Matrix3.clone = function (matrix, result) {
  279. if (!when.defined(matrix)) {
  280. return undefined;
  281. }
  282. if (!when.defined(result)) {
  283. return new Matrix3(
  284. matrix[0],
  285. matrix[3],
  286. matrix[6],
  287. matrix[1],
  288. matrix[4],
  289. matrix[7],
  290. matrix[2],
  291. matrix[5],
  292. matrix[8]
  293. );
  294. }
  295. result[0] = matrix[0];
  296. result[1] = matrix[1];
  297. result[2] = matrix[2];
  298. result[3] = matrix[3];
  299. result[4] = matrix[4];
  300. result[5] = matrix[5];
  301. result[6] = matrix[6];
  302. result[7] = matrix[7];
  303. result[8] = matrix[8];
  304. return result;
  305. };
  306. /**
  307. * Creates a Matrix3 from 9 consecutive elements in an array.
  308. *
  309. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  310. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  311. * @param {Matrix3} [result] The object onto which to store the result.
  312. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  313. *
  314. * @example
  315. * // Create the Matrix3:
  316. * // [1.0, 2.0, 3.0]
  317. * // [1.0, 2.0, 3.0]
  318. * // [1.0, 2.0, 3.0]
  319. *
  320. * var v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  321. * var m = Cesium.Matrix3.fromArray(v);
  322. *
  323. * // Create same Matrix3 with using an offset into an array
  324. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  325. * var m2 = Cesium.Matrix3.fromArray(v2, 2);
  326. */
  327. Matrix3.fromArray = function (array, startingIndex, result) {
  328. //>>includeStart('debug', pragmas.debug);
  329. Check.Check.defined("array", array);
  330. //>>includeEnd('debug');
  331. startingIndex = when.defaultValue(startingIndex, 0);
  332. if (!when.defined(result)) {
  333. result = new Matrix3();
  334. }
  335. result[0] = array[startingIndex];
  336. result[1] = array[startingIndex + 1];
  337. result[2] = array[startingIndex + 2];
  338. result[3] = array[startingIndex + 3];
  339. result[4] = array[startingIndex + 4];
  340. result[5] = array[startingIndex + 5];
  341. result[6] = array[startingIndex + 6];
  342. result[7] = array[startingIndex + 7];
  343. result[8] = array[startingIndex + 8];
  344. return result;
  345. };
  346. /**
  347. * Creates a Matrix3 instance from a column-major order array.
  348. *
  349. * @param {Number[]} values The column-major order array.
  350. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  351. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  352. */
  353. Matrix3.fromColumnMajorArray = function (values, result) {
  354. //>>includeStart('debug', pragmas.debug);
  355. Check.Check.defined("values", values);
  356. //>>includeEnd('debug');
  357. return Matrix3.clone(values, result);
  358. };
  359. /**
  360. * Creates a Matrix3 instance from a row-major order array.
  361. * The resulting matrix will be in column-major order.
  362. *
  363. * @param {Number[]} values The row-major order array.
  364. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  365. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  366. */
  367. Matrix3.fromRowMajorArray = function (values, result) {
  368. //>>includeStart('debug', pragmas.debug);
  369. Check.Check.defined("values", values);
  370. //>>includeEnd('debug');
  371. if (!when.defined(result)) {
  372. return new Matrix3(
  373. values[0],
  374. values[1],
  375. values[2],
  376. values[3],
  377. values[4],
  378. values[5],
  379. values[6],
  380. values[7],
  381. values[8]
  382. );
  383. }
  384. result[0] = values[0];
  385. result[1] = values[3];
  386. result[2] = values[6];
  387. result[3] = values[1];
  388. result[4] = values[4];
  389. result[5] = values[7];
  390. result[6] = values[2];
  391. result[7] = values[5];
  392. result[8] = values[8];
  393. return result;
  394. };
  395. /**
  396. * Computes a 3x3 rotation matrix from the provided quaternion.
  397. *
  398. * @param {Quaternion} quaternion the quaternion to use.
  399. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  400. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  401. */
  402. Matrix3.fromQuaternion = function (quaternion, result) {
  403. //>>includeStart('debug', pragmas.debug);
  404. Check.Check.typeOf.object("quaternion", quaternion);
  405. //>>includeEnd('debug');
  406. var x2 = quaternion.x * quaternion.x;
  407. var xy = quaternion.x * quaternion.y;
  408. var xz = quaternion.x * quaternion.z;
  409. var xw = quaternion.x * quaternion.w;
  410. var y2 = quaternion.y * quaternion.y;
  411. var yz = quaternion.y * quaternion.z;
  412. var yw = quaternion.y * quaternion.w;
  413. var z2 = quaternion.z * quaternion.z;
  414. var zw = quaternion.z * quaternion.w;
  415. var w2 = quaternion.w * quaternion.w;
  416. var m00 = x2 - y2 - z2 + w2;
  417. var m01 = 2.0 * (xy - zw);
  418. var m02 = 2.0 * (xz + yw);
  419. var m10 = 2.0 * (xy + zw);
  420. var m11 = -x2 + y2 - z2 + w2;
  421. var m12 = 2.0 * (yz - xw);
  422. var m20 = 2.0 * (xz - yw);
  423. var m21 = 2.0 * (yz + xw);
  424. var m22 = -x2 - y2 + z2 + w2;
  425. if (!when.defined(result)) {
  426. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  427. }
  428. result[0] = m00;
  429. result[1] = m10;
  430. result[2] = m20;
  431. result[3] = m01;
  432. result[4] = m11;
  433. result[5] = m21;
  434. result[6] = m02;
  435. result[7] = m12;
  436. result[8] = m22;
  437. return result;
  438. };
  439. /**
  440. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  441. *
  442. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  443. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  444. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  445. */
  446. Matrix3.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  447. //>>includeStart('debug', pragmas.debug);
  448. Check.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  449. //>>includeEnd('debug');
  450. var cosTheta = Math.cos(-headingPitchRoll.pitch);
  451. var cosPsi = Math.cos(-headingPitchRoll.heading);
  452. var cosPhi = Math.cos(headingPitchRoll.roll);
  453. var sinTheta = Math.sin(-headingPitchRoll.pitch);
  454. var sinPsi = Math.sin(-headingPitchRoll.heading);
  455. var sinPhi = Math.sin(headingPitchRoll.roll);
  456. var m00 = cosTheta * cosPsi;
  457. var m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  458. var m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  459. var m10 = cosTheta * sinPsi;
  460. var m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  461. var m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  462. var m20 = -sinTheta;
  463. var m21 = sinPhi * cosTheta;
  464. var m22 = cosPhi * cosTheta;
  465. if (!when.defined(result)) {
  466. return new Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
  467. }
  468. result[0] = m00;
  469. result[1] = m10;
  470. result[2] = m20;
  471. result[3] = m01;
  472. result[4] = m11;
  473. result[5] = m21;
  474. result[6] = m02;
  475. result[7] = m12;
  476. result[8] = m22;
  477. return result;
  478. };
  479. /**
  480. * Computes a Matrix3 instance representing a non-uniform scale.
  481. *
  482. * @param {Cartesian3} scale The x, y, and z scale factors.
  483. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  484. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  485. *
  486. * @example
  487. * // Creates
  488. * // [7.0, 0.0, 0.0]
  489. * // [0.0, 8.0, 0.0]
  490. * // [0.0, 0.0, 9.0]
  491. * var m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  492. */
  493. Matrix3.fromScale = function (scale, result) {
  494. //>>includeStart('debug', pragmas.debug);
  495. Check.Check.typeOf.object("scale", scale);
  496. //>>includeEnd('debug');
  497. if (!when.defined(result)) {
  498. return new Matrix3(scale.x, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, scale.z);
  499. }
  500. result[0] = scale.x;
  501. result[1] = 0.0;
  502. result[2] = 0.0;
  503. result[3] = 0.0;
  504. result[4] = scale.y;
  505. result[5] = 0.0;
  506. result[6] = 0.0;
  507. result[7] = 0.0;
  508. result[8] = scale.z;
  509. return result;
  510. };
  511. /**
  512. * Computes a Matrix3 instance representing a uniform scale.
  513. *
  514. * @param {Number} scale The uniform scale factor.
  515. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  516. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  517. *
  518. * @example
  519. * // Creates
  520. * // [2.0, 0.0, 0.0]
  521. * // [0.0, 2.0, 0.0]
  522. * // [0.0, 0.0, 2.0]
  523. * var m = Cesium.Matrix3.fromUniformScale(2.0);
  524. */
  525. Matrix3.fromUniformScale = function (scale, result) {
  526. //>>includeStart('debug', pragmas.debug);
  527. Check.Check.typeOf.number("scale", scale);
  528. //>>includeEnd('debug');
  529. if (!when.defined(result)) {
  530. return new Matrix3(scale, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, scale);
  531. }
  532. result[0] = scale;
  533. result[1] = 0.0;
  534. result[2] = 0.0;
  535. result[3] = 0.0;
  536. result[4] = scale;
  537. result[5] = 0.0;
  538. result[6] = 0.0;
  539. result[7] = 0.0;
  540. result[8] = scale;
  541. return result;
  542. };
  543. /**
  544. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  545. *
  546. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  547. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  548. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  549. *
  550. * @example
  551. * // Creates
  552. * // [0.0, -9.0, 8.0]
  553. * // [9.0, 0.0, -7.0]
  554. * // [-8.0, 7.0, 0.0]
  555. * var m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  556. */
  557. Matrix3.fromCrossProduct = function (vector, result) {
  558. //>>includeStart('debug', pragmas.debug);
  559. Check.Check.typeOf.object("vector", vector);
  560. //>>includeEnd('debug');
  561. if (!when.defined(result)) {
  562. return new Matrix3(
  563. 0.0,
  564. -vector.z,
  565. vector.y,
  566. vector.z,
  567. 0.0,
  568. -vector.x,
  569. -vector.y,
  570. vector.x,
  571. 0.0
  572. );
  573. }
  574. result[0] = 0.0;
  575. result[1] = vector.z;
  576. result[2] = -vector.y;
  577. result[3] = -vector.z;
  578. result[4] = 0.0;
  579. result[5] = vector.x;
  580. result[6] = vector.y;
  581. result[7] = -vector.x;
  582. result[8] = 0.0;
  583. return result;
  584. };
  585. /**
  586. * Creates a rotation matrix around the x-axis.
  587. *
  588. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  589. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  590. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  591. *
  592. * @example
  593. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  594. * var p = new Cesium.Cartesian3(5, 6, 7);
  595. * var m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  596. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  597. */
  598. Matrix3.fromRotationX = function (angle, result) {
  599. //>>includeStart('debug', pragmas.debug);
  600. Check.Check.typeOf.number("angle", angle);
  601. //>>includeEnd('debug');
  602. var cosAngle = Math.cos(angle);
  603. var sinAngle = Math.sin(angle);
  604. if (!when.defined(result)) {
  605. return new Matrix3(
  606. 1.0,
  607. 0.0,
  608. 0.0,
  609. 0.0,
  610. cosAngle,
  611. -sinAngle,
  612. 0.0,
  613. sinAngle,
  614. cosAngle
  615. );
  616. }
  617. result[0] = 1.0;
  618. result[1] = 0.0;
  619. result[2] = 0.0;
  620. result[3] = 0.0;
  621. result[4] = cosAngle;
  622. result[5] = sinAngle;
  623. result[6] = 0.0;
  624. result[7] = -sinAngle;
  625. result[8] = cosAngle;
  626. return result;
  627. };
  628. /**
  629. * Creates a rotation matrix around the y-axis.
  630. *
  631. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  632. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  633. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  634. *
  635. * @example
  636. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  637. * var p = new Cesium.Cartesian3(5, 6, 7);
  638. * var m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  639. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  640. */
  641. Matrix3.fromRotationY = function (angle, result) {
  642. //>>includeStart('debug', pragmas.debug);
  643. Check.Check.typeOf.number("angle", angle);
  644. //>>includeEnd('debug');
  645. var cosAngle = Math.cos(angle);
  646. var sinAngle = Math.sin(angle);
  647. if (!when.defined(result)) {
  648. return new Matrix3(
  649. cosAngle,
  650. 0.0,
  651. sinAngle,
  652. 0.0,
  653. 1.0,
  654. 0.0,
  655. -sinAngle,
  656. 0.0,
  657. cosAngle
  658. );
  659. }
  660. result[0] = cosAngle;
  661. result[1] = 0.0;
  662. result[2] = -sinAngle;
  663. result[3] = 0.0;
  664. result[4] = 1.0;
  665. result[5] = 0.0;
  666. result[6] = sinAngle;
  667. result[7] = 0.0;
  668. result[8] = cosAngle;
  669. return result;
  670. };
  671. /**
  672. * Creates a rotation matrix around the z-axis.
  673. *
  674. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  675. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  676. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  677. *
  678. * @example
  679. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  680. * var p = new Cesium.Cartesian3(5, 6, 7);
  681. * var m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  682. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  683. */
  684. Matrix3.fromRotationZ = function (angle, result) {
  685. //>>includeStart('debug', pragmas.debug);
  686. Check.Check.typeOf.number("angle", angle);
  687. //>>includeEnd('debug');
  688. var cosAngle = Math.cos(angle);
  689. var sinAngle = Math.sin(angle);
  690. if (!when.defined(result)) {
  691. return new Matrix3(
  692. cosAngle,
  693. -sinAngle,
  694. 0.0,
  695. sinAngle,
  696. cosAngle,
  697. 0.0,
  698. 0.0,
  699. 0.0,
  700. 1.0
  701. );
  702. }
  703. result[0] = cosAngle;
  704. result[1] = sinAngle;
  705. result[2] = 0.0;
  706. result[3] = -sinAngle;
  707. result[4] = cosAngle;
  708. result[5] = 0.0;
  709. result[6] = 0.0;
  710. result[7] = 0.0;
  711. result[8] = 1.0;
  712. return result;
  713. };
  714. /**
  715. * Creates an Array from the provided Matrix3 instance.
  716. * The array will be in column-major order.
  717. *
  718. * @param {Matrix3} matrix The matrix to use..
  719. * @param {Number[]} [result] The Array onto which to store the result.
  720. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  721. */
  722. Matrix3.toArray = function (matrix, result) {
  723. //>>includeStart('debug', pragmas.debug);
  724. Check.Check.typeOf.object("matrix", matrix);
  725. //>>includeEnd('debug');
  726. if (!when.defined(result)) {
  727. return [
  728. matrix[0],
  729. matrix[1],
  730. matrix[2],
  731. matrix[3],
  732. matrix[4],
  733. matrix[5],
  734. matrix[6],
  735. matrix[7],
  736. matrix[8],
  737. ];
  738. }
  739. result[0] = matrix[0];
  740. result[1] = matrix[1];
  741. result[2] = matrix[2];
  742. result[3] = matrix[3];
  743. result[4] = matrix[4];
  744. result[5] = matrix[5];
  745. result[6] = matrix[6];
  746. result[7] = matrix[7];
  747. result[8] = matrix[8];
  748. return result;
  749. };
  750. /**
  751. * Computes the array index of the element at the provided row and column.
  752. *
  753. * @param {Number} row The zero-based index of the row.
  754. * @param {Number} column The zero-based index of the column.
  755. * @returns {Number} The index of the element at the provided row and column.
  756. *
  757. * @exception {DeveloperError} row must be 0, 1, or 2.
  758. * @exception {DeveloperError} column must be 0, 1, or 2.
  759. *
  760. * @example
  761. * var myMatrix = new Cesium.Matrix3();
  762. * var column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  763. * var column1Row0 = myMatrix[column1Row0Index]
  764. * myMatrix[column1Row0Index] = 10.0;
  765. */
  766. Matrix3.getElementIndex = function (column, row) {
  767. //>>includeStart('debug', pragmas.debug);
  768. Check.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  769. Check.Check.typeOf.number.lessThanOrEquals("row", row, 2);
  770. Check.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  771. Check.Check.typeOf.number.lessThanOrEquals("column", column, 2);
  772. //>>includeEnd('debug');
  773. return column * 3 + row;
  774. };
  775. /**
  776. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  777. *
  778. * @param {Matrix3} matrix The matrix to use.
  779. * @param {Number} index The zero-based index of the column to retrieve.
  780. * @param {Cartesian3} result The object onto which to store the result.
  781. * @returns {Cartesian3} The modified result parameter.
  782. *
  783. * @exception {DeveloperError} index must be 0, 1, or 2.
  784. */
  785. Matrix3.getColumn = function (matrix, index, result) {
  786. //>>includeStart('debug', pragmas.debug);
  787. Check.Check.typeOf.object("matrix", matrix);
  788. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  789. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  790. Check.Check.typeOf.object("result", result);
  791. //>>includeEnd('debug');
  792. var startIndex = index * 3;
  793. var x = matrix[startIndex];
  794. var y = matrix[startIndex + 1];
  795. var z = matrix[startIndex + 2];
  796. result.x = x;
  797. result.y = y;
  798. result.z = z;
  799. return result;
  800. };
  801. /**
  802. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  803. *
  804. * @param {Matrix3} matrix The matrix to use.
  805. * @param {Number} index The zero-based index of the column to set.
  806. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  807. * @param {Matrix3} result The object onto which to store the result.
  808. * @returns {Matrix3} The modified result parameter.
  809. *
  810. * @exception {DeveloperError} index must be 0, 1, or 2.
  811. */
  812. Matrix3.setColumn = function (matrix, index, cartesian, result) {
  813. //>>includeStart('debug', pragmas.debug);
  814. Check.Check.typeOf.object("matrix", matrix);
  815. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  816. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  817. Check.Check.typeOf.object("cartesian", cartesian);
  818. Check.Check.typeOf.object("result", result);
  819. //>>includeEnd('debug');
  820. result = Matrix3.clone(matrix, result);
  821. var startIndex = index * 3;
  822. result[startIndex] = cartesian.x;
  823. result[startIndex + 1] = cartesian.y;
  824. result[startIndex + 2] = cartesian.z;
  825. return result;
  826. };
  827. /**
  828. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  829. *
  830. * @param {Matrix3} matrix The matrix to use.
  831. * @param {Number} index The zero-based index of the row to retrieve.
  832. * @param {Cartesian3} result The object onto which to store the result.
  833. * @returns {Cartesian3} The modified result parameter.
  834. *
  835. * @exception {DeveloperError} index must be 0, 1, or 2.
  836. */
  837. Matrix3.getRow = function (matrix, index, result) {
  838. //>>includeStart('debug', pragmas.debug);
  839. Check.Check.typeOf.object("matrix", matrix);
  840. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  841. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  842. Check.Check.typeOf.object("result", result);
  843. //>>includeEnd('debug');
  844. var x = matrix[index];
  845. var y = matrix[index + 3];
  846. var z = matrix[index + 6];
  847. result.x = x;
  848. result.y = y;
  849. result.z = z;
  850. return result;
  851. };
  852. /**
  853. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  854. *
  855. * @param {Matrix3} matrix The matrix to use.
  856. * @param {Number} index The zero-based index of the row to set.
  857. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  858. * @param {Matrix3} result The object onto which to store the result.
  859. * @returns {Matrix3} The modified result parameter.
  860. *
  861. * @exception {DeveloperError} index must be 0, 1, or 2.
  862. */
  863. Matrix3.setRow = function (matrix, index, cartesian, result) {
  864. //>>includeStart('debug', pragmas.debug);
  865. Check.Check.typeOf.object("matrix", matrix);
  866. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  867. Check.Check.typeOf.number.lessThanOrEquals("index", index, 2);
  868. Check.Check.typeOf.object("cartesian", cartesian);
  869. Check.Check.typeOf.object("result", result);
  870. //>>includeEnd('debug');
  871. result = Matrix3.clone(matrix, result);
  872. result[index] = cartesian.x;
  873. result[index + 3] = cartesian.y;
  874. result[index + 6] = cartesian.z;
  875. return result;
  876. };
  877. var scratchColumn = new Cartesian2.Cartesian3();
  878. /**
  879. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  880. *
  881. * @param {Matrix3} matrix The matrix.
  882. * @param {Cartesian3} result The object onto which to store the result.
  883. * @returns {Cartesian3} The modified result parameter.
  884. */
  885. Matrix3.getScale = function (matrix, result) {
  886. //>>includeStart('debug', pragmas.debug);
  887. Check.Check.typeOf.object("matrix", matrix);
  888. Check.Check.typeOf.object("result", result);
  889. //>>includeEnd('debug');
  890. result.x = Cartesian2.Cartesian3.magnitude(
  891. Cartesian2.Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn)
  892. );
  893. result.y = Cartesian2.Cartesian3.magnitude(
  894. Cartesian2.Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn)
  895. );
  896. result.z = Cartesian2.Cartesian3.magnitude(
  897. Cartesian2.Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn)
  898. );
  899. return result;
  900. };
  901. var scratchScale = new Cartesian2.Cartesian3();
  902. /**
  903. * Computes the maximum scale assuming the matrix is an affine transformation.
  904. * The maximum scale is the maximum length of the column vectors.
  905. *
  906. * @param {Matrix3} matrix The matrix.
  907. * @returns {Number} The maximum scale.
  908. */
  909. Matrix3.getMaximumScale = function (matrix) {
  910. Matrix3.getScale(matrix, scratchScale);
  911. return Cartesian2.Cartesian3.maximumComponent(scratchScale);
  912. };
  913. /**
  914. * Computes the product of two matrices.
  915. *
  916. * @param {Matrix3} left The first matrix.
  917. * @param {Matrix3} right The second matrix.
  918. * @param {Matrix3} result The object onto which to store the result.
  919. * @returns {Matrix3} The modified result parameter.
  920. */
  921. Matrix3.multiply = function (left, right, result) {
  922. //>>includeStart('debug', pragmas.debug);
  923. Check.Check.typeOf.object("left", left);
  924. Check.Check.typeOf.object("right", right);
  925. Check.Check.typeOf.object("result", result);
  926. //>>includeEnd('debug');
  927. var column0Row0 =
  928. left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  929. var column0Row1 =
  930. left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  931. var column0Row2 =
  932. left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  933. var column1Row0 =
  934. left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  935. var column1Row1 =
  936. left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  937. var column1Row2 =
  938. left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  939. var column2Row0 =
  940. left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  941. var column2Row1 =
  942. left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  943. var column2Row2 =
  944. left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  945. result[0] = column0Row0;
  946. result[1] = column0Row1;
  947. result[2] = column0Row2;
  948. result[3] = column1Row0;
  949. result[4] = column1Row1;
  950. result[5] = column1Row2;
  951. result[6] = column2Row0;
  952. result[7] = column2Row1;
  953. result[8] = column2Row2;
  954. return result;
  955. };
  956. /**
  957. * Computes the sum of two matrices.
  958. *
  959. * @param {Matrix3} left The first matrix.
  960. * @param {Matrix3} right The second matrix.
  961. * @param {Matrix3} result The object onto which to store the result.
  962. * @returns {Matrix3} The modified result parameter.
  963. */
  964. Matrix3.add = function (left, right, result) {
  965. //>>includeStart('debug', pragmas.debug);
  966. Check.Check.typeOf.object("left", left);
  967. Check.Check.typeOf.object("right", right);
  968. Check.Check.typeOf.object("result", result);
  969. //>>includeEnd('debug');
  970. result[0] = left[0] + right[0];
  971. result[1] = left[1] + right[1];
  972. result[2] = left[2] + right[2];
  973. result[3] = left[3] + right[3];
  974. result[4] = left[4] + right[4];
  975. result[5] = left[5] + right[5];
  976. result[6] = left[6] + right[6];
  977. result[7] = left[7] + right[7];
  978. result[8] = left[8] + right[8];
  979. return result;
  980. };
  981. /**
  982. * Computes the difference of two matrices.
  983. *
  984. * @param {Matrix3} left The first matrix.
  985. * @param {Matrix3} right The second matrix.
  986. * @param {Matrix3} result The object onto which to store the result.
  987. * @returns {Matrix3} The modified result parameter.
  988. */
  989. Matrix3.subtract = function (left, right, result) {
  990. //>>includeStart('debug', pragmas.debug);
  991. Check.Check.typeOf.object("left", left);
  992. Check.Check.typeOf.object("right", right);
  993. Check.Check.typeOf.object("result", result);
  994. //>>includeEnd('debug');
  995. result[0] = left[0] - right[0];
  996. result[1] = left[1] - right[1];
  997. result[2] = left[2] - right[2];
  998. result[3] = left[3] - right[3];
  999. result[4] = left[4] - right[4];
  1000. result[5] = left[5] - right[5];
  1001. result[6] = left[6] - right[6];
  1002. result[7] = left[7] - right[7];
  1003. result[8] = left[8] - right[8];
  1004. return result;
  1005. };
  1006. /**
  1007. * Computes the product of a matrix and a column vector.
  1008. *
  1009. * @param {Matrix3} matrix The matrix.
  1010. * @param {Cartesian3} cartesian The column.
  1011. * @param {Cartesian3} result The object onto which to store the result.
  1012. * @returns {Cartesian3} The modified result parameter.
  1013. */
  1014. Matrix3.multiplyByVector = function (matrix, cartesian, result) {
  1015. //>>includeStart('debug', pragmas.debug);
  1016. Check.Check.typeOf.object("matrix", matrix);
  1017. Check.Check.typeOf.object("cartesian", cartesian);
  1018. Check.Check.typeOf.object("result", result);
  1019. //>>includeEnd('debug');
  1020. var vX = cartesian.x;
  1021. var vY = cartesian.y;
  1022. var vZ = cartesian.z;
  1023. var x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  1024. var y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  1025. var z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  1026. result.x = x;
  1027. result.y = y;
  1028. result.z = z;
  1029. return result;
  1030. };
  1031. /**
  1032. * Computes the product of a matrix and a scalar.
  1033. *
  1034. * @param {Matrix3} matrix The matrix.
  1035. * @param {Number} scalar The number to multiply by.
  1036. * @param {Matrix3} result The object onto which to store the result.
  1037. * @returns {Matrix3} The modified result parameter.
  1038. */
  1039. Matrix3.multiplyByScalar = function (matrix, scalar, result) {
  1040. //>>includeStart('debug', pragmas.debug);
  1041. Check.Check.typeOf.object("matrix", matrix);
  1042. Check.Check.typeOf.number("scalar", scalar);
  1043. Check.Check.typeOf.object("result", result);
  1044. //>>includeEnd('debug');
  1045. result[0] = matrix[0] * scalar;
  1046. result[1] = matrix[1] * scalar;
  1047. result[2] = matrix[2] * scalar;
  1048. result[3] = matrix[3] * scalar;
  1049. result[4] = matrix[4] * scalar;
  1050. result[5] = matrix[5] * scalar;
  1051. result[6] = matrix[6] * scalar;
  1052. result[7] = matrix[7] * scalar;
  1053. result[8] = matrix[8] * scalar;
  1054. return result;
  1055. };
  1056. /**
  1057. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  1058. *
  1059. * @param {Matrix3} matrix The matrix on the left-hand side.
  1060. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  1061. * @param {Matrix3} result The object onto which to store the result.
  1062. * @returns {Matrix3} The modified result parameter.
  1063. *
  1064. *
  1065. * @example
  1066. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  1067. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  1068. *
  1069. * @see Matrix3.fromScale
  1070. * @see Matrix3.multiplyByUniformScale
  1071. */
  1072. Matrix3.multiplyByScale = function (matrix, scale, result) {
  1073. //>>includeStart('debug', pragmas.debug);
  1074. Check.Check.typeOf.object("matrix", matrix);
  1075. Check.Check.typeOf.object("scale", scale);
  1076. Check.Check.typeOf.object("result", result);
  1077. //>>includeEnd('debug');
  1078. result[0] = matrix[0] * scale.x;
  1079. result[1] = matrix[1] * scale.x;
  1080. result[2] = matrix[2] * scale.x;
  1081. result[3] = matrix[3] * scale.y;
  1082. result[4] = matrix[4] * scale.y;
  1083. result[5] = matrix[5] * scale.y;
  1084. result[6] = matrix[6] * scale.z;
  1085. result[7] = matrix[7] * scale.z;
  1086. result[8] = matrix[8] * scale.z;
  1087. return result;
  1088. };
  1089. /**
  1090. * Creates a negated copy of the provided matrix.
  1091. *
  1092. * @param {Matrix3} matrix The matrix to negate.
  1093. * @param {Matrix3} result The object onto which to store the result.
  1094. * @returns {Matrix3} The modified result parameter.
  1095. */
  1096. Matrix3.negate = function (matrix, result) {
  1097. //>>includeStart('debug', pragmas.debug);
  1098. Check.Check.typeOf.object("matrix", matrix);
  1099. Check.Check.typeOf.object("result", result);
  1100. //>>includeEnd('debug');
  1101. result[0] = -matrix[0];
  1102. result[1] = -matrix[1];
  1103. result[2] = -matrix[2];
  1104. result[3] = -matrix[3];
  1105. result[4] = -matrix[4];
  1106. result[5] = -matrix[5];
  1107. result[6] = -matrix[6];
  1108. result[7] = -matrix[7];
  1109. result[8] = -matrix[8];
  1110. return result;
  1111. };
  1112. /**
  1113. * Computes the transpose of the provided matrix.
  1114. *
  1115. * @param {Matrix3} matrix The matrix to transpose.
  1116. * @param {Matrix3} result The object onto which to store the result.
  1117. * @returns {Matrix3} The modified result parameter.
  1118. */
  1119. Matrix3.transpose = function (matrix, result) {
  1120. //>>includeStart('debug', pragmas.debug);
  1121. Check.Check.typeOf.object("matrix", matrix);
  1122. Check.Check.typeOf.object("result", result);
  1123. //>>includeEnd('debug');
  1124. var column0Row0 = matrix[0];
  1125. var column0Row1 = matrix[3];
  1126. var column0Row2 = matrix[6];
  1127. var column1Row0 = matrix[1];
  1128. var column1Row1 = matrix[4];
  1129. var column1Row2 = matrix[7];
  1130. var column2Row0 = matrix[2];
  1131. var column2Row1 = matrix[5];
  1132. var column2Row2 = matrix[8];
  1133. result[0] = column0Row0;
  1134. result[1] = column0Row1;
  1135. result[2] = column0Row2;
  1136. result[3] = column1Row0;
  1137. result[4] = column1Row1;
  1138. result[5] = column1Row2;
  1139. result[6] = column2Row0;
  1140. result[7] = column2Row1;
  1141. result[8] = column2Row2;
  1142. return result;
  1143. };
  1144. var UNIT = new Cartesian2.Cartesian3(1, 1, 1);
  1145. /**
  1146. * Extracts the rotation assuming the matrix is an affine transformation.
  1147. *
  1148. * @param {Matrix3} matrix The matrix.
  1149. * @param {Matrix3} result The object onto which to store the result.
  1150. * @returns {Matrix3} The modified result parameter
  1151. */
  1152. Matrix3.getRotation = function (matrix, result) {
  1153. //>>includeStart('debug', pragmas.debug);
  1154. Check.Check.typeOf.object("matrix", matrix);
  1155. Check.Check.typeOf.object("result", result);
  1156. //>>includeEnd('debug');
  1157. var inverseScale = Cartesian2.Cartesian3.divideComponents(
  1158. UNIT,
  1159. Matrix3.getScale(matrix, scratchScale),
  1160. scratchScale
  1161. );
  1162. result = Matrix3.multiplyByScale(matrix, inverseScale, result);
  1163. return result;
  1164. };
  1165. function computeFrobeniusNorm(matrix) {
  1166. var norm = 0.0;
  1167. for (var i = 0; i < 9; ++i) {
  1168. var temp = matrix[i];
  1169. norm += temp * temp;
  1170. }
  1171. return Math.sqrt(norm);
  1172. }
  1173. var rowVal = [1, 0, 0];
  1174. var colVal = [2, 2, 1];
  1175. function offDiagonalFrobeniusNorm(matrix) {
  1176. // Computes the "off-diagonal" Frobenius norm.
  1177. // Assumes matrix is symmetric.
  1178. var norm = 0.0;
  1179. for (var i = 0; i < 3; ++i) {
  1180. var temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  1181. norm += 2.0 * temp * temp;
  1182. }
  1183. return Math.sqrt(norm);
  1184. }
  1185. function shurDecomposition(matrix, result) {
  1186. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1187. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  1188. //
  1189. // The routine takes a matrix, which is assumed to be symmetric, and
  1190. // finds the largest off-diagonal term, and then creates
  1191. // a matrix (result) which can be used to help reduce it
  1192. var tolerance = _Math.CesiumMath.EPSILON15;
  1193. var maxDiagonal = 0.0;
  1194. var rotAxis = 1;
  1195. // find pivot (rotAxis) based on max diagonal of matrix
  1196. for (var i = 0; i < 3; ++i) {
  1197. var temp = Math.abs(matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]);
  1198. if (temp > maxDiagonal) {
  1199. rotAxis = i;
  1200. maxDiagonal = temp;
  1201. }
  1202. }
  1203. var c = 1.0;
  1204. var s = 0.0;
  1205. var p = rowVal[rotAxis];
  1206. var q = colVal[rotAxis];
  1207. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  1208. var qq = matrix[Matrix3.getElementIndex(q, q)];
  1209. var pp = matrix[Matrix3.getElementIndex(p, p)];
  1210. var qp = matrix[Matrix3.getElementIndex(q, p)];
  1211. var tau = (qq - pp) / 2.0 / qp;
  1212. var t;
  1213. if (tau < 0.0) {
  1214. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  1215. } else {
  1216. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  1217. }
  1218. c = 1.0 / Math.sqrt(1.0 + t * t);
  1219. s = t * c;
  1220. }
  1221. result = Matrix3.clone(Matrix3.IDENTITY, result);
  1222. result[Matrix3.getElementIndex(p, p)] = result[
  1223. Matrix3.getElementIndex(q, q)
  1224. ] = c;
  1225. result[Matrix3.getElementIndex(q, p)] = s;
  1226. result[Matrix3.getElementIndex(p, q)] = -s;
  1227. return result;
  1228. }
  1229. var jMatrix = new Matrix3();
  1230. var jMatrixTranspose = new Matrix3();
  1231. /**
  1232. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  1233. * <p>
  1234. * Returns a diagonal matrix and unitary matrix such that:
  1235. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  1236. * </p>
  1237. * <p>
  1238. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  1239. * of the unitary matrix are the corresponding eigenvectors.
  1240. * </p>
  1241. *
  1242. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  1243. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  1244. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  1245. *
  1246. * @example
  1247. * var a = //... symetric matrix
  1248. * var result = {
  1249. * unitary : new Cesium.Matrix3(),
  1250. * diagonal : new Cesium.Matrix3()
  1251. * };
  1252. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  1253. *
  1254. * var unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  1255. * var b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  1256. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  1257. *
  1258. * var lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  1259. * var v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  1260. * var c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  1261. */
  1262. Matrix3.computeEigenDecomposition = function (matrix, result) {
  1263. //>>includeStart('debug', pragmas.debug);
  1264. Check.Check.typeOf.object("matrix", matrix);
  1265. //>>includeEnd('debug');
  1266. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1267. // section 8.4.3 The Classical Jacobi Algorithm
  1268. var tolerance = _Math.CesiumMath.EPSILON20;
  1269. var maxSweeps = 10;
  1270. var count = 0;
  1271. var sweep = 0;
  1272. if (!when.defined(result)) {
  1273. result = {};
  1274. }
  1275. var unitaryMatrix = (result.unitary = Matrix3.clone(
  1276. Matrix3.IDENTITY,
  1277. result.unitary
  1278. ));
  1279. var diagMatrix = (result.diagonal = Matrix3.clone(matrix, result.diagonal));
  1280. var epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  1281. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  1282. shurDecomposition(diagMatrix, jMatrix);
  1283. Matrix3.transpose(jMatrix, jMatrixTranspose);
  1284. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  1285. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  1286. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  1287. if (++count > 2) {
  1288. ++sweep;
  1289. count = 0;
  1290. }
  1291. }
  1292. return result;
  1293. };
  1294. /**
  1295. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  1296. *
  1297. * @param {Matrix3} matrix The matrix with signed elements.
  1298. * @param {Matrix3} result The object onto which to store the result.
  1299. * @returns {Matrix3} The modified result parameter.
  1300. */
  1301. Matrix3.abs = function (matrix, result) {
  1302. //>>includeStart('debug', pragmas.debug);
  1303. Check.Check.typeOf.object("matrix", matrix);
  1304. Check.Check.typeOf.object("result", result);
  1305. //>>includeEnd('debug');
  1306. result[0] = Math.abs(matrix[0]);
  1307. result[1] = Math.abs(matrix[1]);
  1308. result[2] = Math.abs(matrix[2]);
  1309. result[3] = Math.abs(matrix[3]);
  1310. result[4] = Math.abs(matrix[4]);
  1311. result[5] = Math.abs(matrix[5]);
  1312. result[6] = Math.abs(matrix[6]);
  1313. result[7] = Math.abs(matrix[7]);
  1314. result[8] = Math.abs(matrix[8]);
  1315. return result;
  1316. };
  1317. /**
  1318. * Computes the determinant of the provided matrix.
  1319. *
  1320. * @param {Matrix3} matrix The matrix to use.
  1321. * @returns {Number} The value of the determinant of the matrix.
  1322. */
  1323. Matrix3.determinant = function (matrix) {
  1324. //>>includeStart('debug', pragmas.debug);
  1325. Check.Check.typeOf.object("matrix", matrix);
  1326. //>>includeEnd('debug');
  1327. var m11 = matrix[0];
  1328. var m21 = matrix[3];
  1329. var m31 = matrix[6];
  1330. var m12 = matrix[1];
  1331. var m22 = matrix[4];
  1332. var m32 = matrix[7];
  1333. var m13 = matrix[2];
  1334. var m23 = matrix[5];
  1335. var m33 = matrix[8];
  1336. return (
  1337. m11 * (m22 * m33 - m23 * m32) +
  1338. m12 * (m23 * m31 - m21 * m33) +
  1339. m13 * (m21 * m32 - m22 * m31)
  1340. );
  1341. };
  1342. /**
  1343. * Computes the inverse of the provided matrix.
  1344. *
  1345. * @param {Matrix3} matrix The matrix to invert.
  1346. * @param {Matrix3} result The object onto which to store the result.
  1347. * @returns {Matrix3} The modified result parameter.
  1348. *
  1349. * @exception {DeveloperError} matrix is not invertible.
  1350. */
  1351. Matrix3.inverse = function (matrix, result) {
  1352. //>>includeStart('debug', pragmas.debug);
  1353. Check.Check.typeOf.object("matrix", matrix);
  1354. Check.Check.typeOf.object("result", result);
  1355. //>>includeEnd('debug');
  1356. var m11 = matrix[0];
  1357. var m21 = matrix[1];
  1358. var m31 = matrix[2];
  1359. var m12 = matrix[3];
  1360. var m22 = matrix[4];
  1361. var m32 = matrix[5];
  1362. var m13 = matrix[6];
  1363. var m23 = matrix[7];
  1364. var m33 = matrix[8];
  1365. var determinant = Matrix3.determinant(matrix);
  1366. //>>includeStart('debug', pragmas.debug);
  1367. if (Math.abs(determinant) <= _Math.CesiumMath.EPSILON15) {
  1368. throw new Check.DeveloperError("matrix is not invertible");
  1369. }
  1370. //>>includeEnd('debug');
  1371. result[0] = m22 * m33 - m23 * m32;
  1372. result[1] = m23 * m31 - m21 * m33;
  1373. result[2] = m21 * m32 - m22 * m31;
  1374. result[3] = m13 * m32 - m12 * m33;
  1375. result[4] = m11 * m33 - m13 * m31;
  1376. result[5] = m12 * m31 - m11 * m32;
  1377. result[6] = m12 * m23 - m13 * m22;
  1378. result[7] = m13 * m21 - m11 * m23;
  1379. result[8] = m11 * m22 - m12 * m21;
  1380. var scale = 1.0 / determinant;
  1381. return Matrix3.multiplyByScalar(result, scale, result);
  1382. };
  1383. /**
  1384. * Compares the provided matrices componentwise and returns
  1385. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1386. *
  1387. * @param {Matrix3} [left] The first matrix.
  1388. * @param {Matrix3} [right] The second matrix.
  1389. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1390. */
  1391. Matrix3.equals = function (left, right) {
  1392. return (
  1393. left === right ||
  1394. (when.defined(left) &&
  1395. when.defined(right) &&
  1396. left[0] === right[0] &&
  1397. left[1] === right[1] &&
  1398. left[2] === right[2] &&
  1399. left[3] === right[3] &&
  1400. left[4] === right[4] &&
  1401. left[5] === right[5] &&
  1402. left[6] === right[6] &&
  1403. left[7] === right[7] &&
  1404. left[8] === right[8])
  1405. );
  1406. };
  1407. /**
  1408. * Compares the provided matrices componentwise and returns
  1409. * <code>true</code> if they are within the provided epsilon,
  1410. * <code>false</code> otherwise.
  1411. *
  1412. * @param {Matrix3} [left] The first matrix.
  1413. * @param {Matrix3} [right] The second matrix.
  1414. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1415. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1416. */
  1417. Matrix3.equalsEpsilon = function (left, right, epsilon) {
  1418. epsilon = when.defaultValue(epsilon, 0);
  1419. return (
  1420. left === right ||
  1421. (when.defined(left) &&
  1422. when.defined(right) &&
  1423. Math.abs(left[0] - right[0]) <= epsilon &&
  1424. Math.abs(left[1] - right[1]) <= epsilon &&
  1425. Math.abs(left[2] - right[2]) <= epsilon &&
  1426. Math.abs(left[3] - right[3]) <= epsilon &&
  1427. Math.abs(left[4] - right[4]) <= epsilon &&
  1428. Math.abs(left[5] - right[5]) <= epsilon &&
  1429. Math.abs(left[6] - right[6]) <= epsilon &&
  1430. Math.abs(left[7] - right[7]) <= epsilon &&
  1431. Math.abs(left[8] - right[8]) <= epsilon)
  1432. );
  1433. };
  1434. /**
  1435. * An immutable Matrix3 instance initialized to the identity matrix.
  1436. *
  1437. * @type {Matrix3}
  1438. * @constant
  1439. */
  1440. Matrix3.IDENTITY = Object.freeze(
  1441. new Matrix3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
  1442. );
  1443. /**
  1444. * An immutable Matrix3 instance initialized to the zero matrix.
  1445. *
  1446. * @type {Matrix3}
  1447. * @constant
  1448. */
  1449. Matrix3.ZERO = Object.freeze(
  1450. new Matrix3(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  1451. );
  1452. /**
  1453. * The index into Matrix3 for column 0, row 0.
  1454. *
  1455. * @type {Number}
  1456. * @constant
  1457. */
  1458. Matrix3.COLUMN0ROW0 = 0;
  1459. /**
  1460. * The index into Matrix3 for column 0, row 1.
  1461. *
  1462. * @type {Number}
  1463. * @constant
  1464. */
  1465. Matrix3.COLUMN0ROW1 = 1;
  1466. /**
  1467. * The index into Matrix3 for column 0, row 2.
  1468. *
  1469. * @type {Number}
  1470. * @constant
  1471. */
  1472. Matrix3.COLUMN0ROW2 = 2;
  1473. /**
  1474. * The index into Matrix3 for column 1, row 0.
  1475. *
  1476. * @type {Number}
  1477. * @constant
  1478. */
  1479. Matrix3.COLUMN1ROW0 = 3;
  1480. /**
  1481. * The index into Matrix3 for column 1, row 1.
  1482. *
  1483. * @type {Number}
  1484. * @constant
  1485. */
  1486. Matrix3.COLUMN1ROW1 = 4;
  1487. /**
  1488. * The index into Matrix3 for column 1, row 2.
  1489. *
  1490. * @type {Number}
  1491. * @constant
  1492. */
  1493. Matrix3.COLUMN1ROW2 = 5;
  1494. /**
  1495. * The index into Matrix3 for column 2, row 0.
  1496. *
  1497. * @type {Number}
  1498. * @constant
  1499. */
  1500. Matrix3.COLUMN2ROW0 = 6;
  1501. /**
  1502. * The index into Matrix3 for column 2, row 1.
  1503. *
  1504. * @type {Number}
  1505. * @constant
  1506. */
  1507. Matrix3.COLUMN2ROW1 = 7;
  1508. /**
  1509. * The index into Matrix3 for column 2, row 2.
  1510. *
  1511. * @type {Number}
  1512. * @constant
  1513. */
  1514. Matrix3.COLUMN2ROW2 = 8;
  1515. Object.defineProperties(Matrix3.prototype, {
  1516. /**
  1517. * Gets the number of items in the collection.
  1518. * @memberof Matrix3.prototype
  1519. *
  1520. * @type {Number}
  1521. */
  1522. length: {
  1523. get: function () {
  1524. return Matrix3.packedLength;
  1525. },
  1526. },
  1527. });
  1528. /**
  1529. * Duplicates the provided Matrix3 instance.
  1530. *
  1531. * @param {Matrix3} [result] The object onto which to store the result.
  1532. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  1533. */
  1534. Matrix3.prototype.clone = function (result) {
  1535. return Matrix3.clone(this, result);
  1536. };
  1537. /**
  1538. * Compares this matrix to the provided matrix componentwise and returns
  1539. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1540. *
  1541. * @param {Matrix3} [right] The right hand side matrix.
  1542. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1543. */
  1544. Matrix3.prototype.equals = function (right) {
  1545. return Matrix3.equals(this, right);
  1546. };
  1547. /**
  1548. * @private
  1549. */
  1550. Matrix3.equalsArray = function (matrix, array, offset) {
  1551. return (
  1552. matrix[0] === array[offset] &&
  1553. matrix[1] === array[offset + 1] &&
  1554. matrix[2] === array[offset + 2] &&
  1555. matrix[3] === array[offset + 3] &&
  1556. matrix[4] === array[offset + 4] &&
  1557. matrix[5] === array[offset + 5] &&
  1558. matrix[6] === array[offset + 6] &&
  1559. matrix[7] === array[offset + 7] &&
  1560. matrix[8] === array[offset + 8]
  1561. );
  1562. };
  1563. /**
  1564. * Compares this matrix to the provided matrix componentwise and returns
  1565. * <code>true</code> if they are within the provided epsilon,
  1566. * <code>false</code> otherwise.
  1567. *
  1568. * @param {Matrix3} [right] The right hand side matrix.
  1569. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1570. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1571. */
  1572. Matrix3.prototype.equalsEpsilon = function (right, epsilon) {
  1573. return Matrix3.equalsEpsilon(this, right, epsilon);
  1574. };
  1575. /**
  1576. * Creates a string representing this Matrix with each row being
  1577. * on a separate line and in the format '(column0, column1, column2)'.
  1578. *
  1579. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  1580. */
  1581. Matrix3.prototype.toString = function () {
  1582. return (
  1583. "(" +
  1584. this[0] +
  1585. ", " +
  1586. this[3] +
  1587. ", " +
  1588. this[6] +
  1589. ")\n" +
  1590. "(" +
  1591. this[1] +
  1592. ", " +
  1593. this[4] +
  1594. ", " +
  1595. this[7] +
  1596. ")\n" +
  1597. "(" +
  1598. this[2] +
  1599. ", " +
  1600. this[5] +
  1601. ", " +
  1602. this[8] +
  1603. ")"
  1604. );
  1605. };
  1606. /**
  1607. * A 4D Cartesian point.
  1608. * @alias Cartesian4
  1609. * @constructor
  1610. *
  1611. * @param {Number} [x=0.0] The X component.
  1612. * @param {Number} [y=0.0] The Y component.
  1613. * @param {Number} [z=0.0] The Z component.
  1614. * @param {Number} [w=0.0] The W component.
  1615. *
  1616. * @see Cartesian2
  1617. * @see Cartesian3
  1618. * @see Packable
  1619. */
  1620. function Cartesian4(x, y, z, w) {
  1621. /**
  1622. * The X component.
  1623. * @type {Number}
  1624. * @default 0.0
  1625. */
  1626. this.x = when.defaultValue(x, 0.0);
  1627. /**
  1628. * The Y component.
  1629. * @type {Number}
  1630. * @default 0.0
  1631. */
  1632. this.y = when.defaultValue(y, 0.0);
  1633. /**
  1634. * The Z component.
  1635. * @type {Number}
  1636. * @default 0.0
  1637. */
  1638. this.z = when.defaultValue(z, 0.0);
  1639. /**
  1640. * The W component.
  1641. * @type {Number}
  1642. * @default 0.0
  1643. */
  1644. this.w = when.defaultValue(w, 0.0);
  1645. }
  1646. /**
  1647. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  1648. *
  1649. * @param {Number} x The x coordinate.
  1650. * @param {Number} y The y coordinate.
  1651. * @param {Number} z The z coordinate.
  1652. * @param {Number} w The w coordinate.
  1653. * @param {Cartesian4} [result] The object onto which to store the result.
  1654. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  1655. */
  1656. Cartesian4.fromElements = function (x, y, z, w, result) {
  1657. if (!when.defined(result)) {
  1658. return new Cartesian4(x, y, z, w);
  1659. }
  1660. result.x = x;
  1661. result.y = y;
  1662. result.z = z;
  1663. result.w = w;
  1664. return result;
  1665. };
  1666. /**
  1667. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  1668. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  1669. *
  1670. * @param {Color} color The source color.
  1671. * @param {Cartesian4} [result] The object onto which to store the result.
  1672. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  1673. */
  1674. Cartesian4.fromColor = function (color, result) {
  1675. //>>includeStart('debug', pragmas.debug);
  1676. Check.Check.typeOf.object("color", color);
  1677. //>>includeEnd('debug');
  1678. if (!when.defined(result)) {
  1679. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  1680. }
  1681. result.x = color.red;
  1682. result.y = color.green;
  1683. result.z = color.blue;
  1684. result.w = color.alpha;
  1685. return result;
  1686. };
  1687. /**
  1688. * Duplicates a Cartesian4 instance.
  1689. *
  1690. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  1691. * @param {Cartesian4} [result] The object onto which to store the result.
  1692. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  1693. */
  1694. Cartesian4.clone = function (cartesian, result) {
  1695. if (!when.defined(cartesian)) {
  1696. return undefined;
  1697. }
  1698. if (!when.defined(result)) {
  1699. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  1700. }
  1701. result.x = cartesian.x;
  1702. result.y = cartesian.y;
  1703. result.z = cartesian.z;
  1704. result.w = cartesian.w;
  1705. return result;
  1706. };
  1707. /**
  1708. * The number of elements used to pack the object into an array.
  1709. * @type {Number}
  1710. */
  1711. Cartesian4.packedLength = 4;
  1712. /**
  1713. * Stores the provided instance into the provided array.
  1714. *
  1715. * @param {Cartesian4} value The value to pack.
  1716. * @param {Number[]} array The array to pack into.
  1717. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1718. *
  1719. * @returns {Number[]} The array that was packed into
  1720. */
  1721. Cartesian4.pack = function (value, array, startingIndex) {
  1722. //>>includeStart('debug', pragmas.debug);
  1723. Check.Check.typeOf.object("value", value);
  1724. Check.Check.defined("array", array);
  1725. //>>includeEnd('debug');
  1726. startingIndex = when.defaultValue(startingIndex, 0);
  1727. array[startingIndex++] = value.x;
  1728. array[startingIndex++] = value.y;
  1729. array[startingIndex++] = value.z;
  1730. array[startingIndex] = value.w;
  1731. return array;
  1732. };
  1733. /**
  1734. * Retrieves an instance from a packed array.
  1735. *
  1736. * @param {Number[]} array The packed array.
  1737. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1738. * @param {Cartesian4} [result] The object into which to store the result.
  1739. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  1740. */
  1741. Cartesian4.unpack = function (array, startingIndex, result) {
  1742. //>>includeStart('debug', pragmas.debug);
  1743. Check.Check.defined("array", array);
  1744. //>>includeEnd('debug');
  1745. startingIndex = when.defaultValue(startingIndex, 0);
  1746. if (!when.defined(result)) {
  1747. result = new Cartesian4();
  1748. }
  1749. result.x = array[startingIndex++];
  1750. result.y = array[startingIndex++];
  1751. result.z = array[startingIndex++];
  1752. result.w = array[startingIndex];
  1753. return result;
  1754. };
  1755. /**
  1756. * Flattens an array of Cartesian4s into and array of components.
  1757. *
  1758. * @param {Cartesian4[]} array The array of cartesians to pack.
  1759. * @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.
  1760. * @returns {Number[]} The packed array.
  1761. */
  1762. Cartesian4.packArray = function (array, result) {
  1763. //>>includeStart('debug', pragmas.debug);
  1764. Check.Check.defined("array", array);
  1765. //>>includeEnd('debug');
  1766. var length = array.length;
  1767. var resultLength = length * 4;
  1768. if (!when.defined(result)) {
  1769. result = new Array(resultLength);
  1770. } else if (!Array.isArray(result) && result.length !== resultLength) {
  1771. throw new Check.DeveloperError(
  1772. "If result is a typed array, it must have exactly array.length * 4 elements"
  1773. );
  1774. } else if (result.length !== resultLength) {
  1775. result.length = resultLength;
  1776. }
  1777. for (var i = 0; i < length; ++i) {
  1778. Cartesian4.pack(array[i], result, i * 4);
  1779. }
  1780. return result;
  1781. };
  1782. /**
  1783. * Unpacks an array of cartesian components into and array of Cartesian4s.
  1784. *
  1785. * @param {Number[]} array The array of components to unpack.
  1786. * @param {Cartesian4[]} [result] The array onto which to store the result.
  1787. * @returns {Cartesian4[]} The unpacked array.
  1788. */
  1789. Cartesian4.unpackArray = function (array, result) {
  1790. //>>includeStart('debug', pragmas.debug);
  1791. Check.Check.defined("array", array);
  1792. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 4);
  1793. if (array.length % 4 !== 0) {
  1794. throw new Check.DeveloperError("array length must be a multiple of 4.");
  1795. }
  1796. //>>includeEnd('debug');
  1797. var length = array.length;
  1798. if (!when.defined(result)) {
  1799. result = new Array(length / 4);
  1800. } else {
  1801. result.length = length / 4;
  1802. }
  1803. for (var i = 0; i < length; i += 4) {
  1804. var index = i / 4;
  1805. result[index] = Cartesian4.unpack(array, i, result[index]);
  1806. }
  1807. return result;
  1808. };
  1809. /**
  1810. * Creates a Cartesian4 from four consecutive elements in an array.
  1811. * @function
  1812. *
  1813. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  1814. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  1815. * @param {Cartesian4} [result] The object onto which to store the result.
  1816. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  1817. *
  1818. * @example
  1819. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  1820. * var v = [1.0, 2.0, 3.0, 4.0];
  1821. * var p = Cesium.Cartesian4.fromArray(v);
  1822. *
  1823. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  1824. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  1825. * var p2 = Cesium.Cartesian4.fromArray(v2, 2);
  1826. */
  1827. Cartesian4.fromArray = Cartesian4.unpack;
  1828. /**
  1829. * Computes the value of the maximum component for the supplied Cartesian.
  1830. *
  1831. * @param {Cartesian4} cartesian The cartesian to use.
  1832. * @returns {Number} The value of the maximum component.
  1833. */
  1834. Cartesian4.maximumComponent = function (cartesian) {
  1835. //>>includeStart('debug', pragmas.debug);
  1836. Check.Check.typeOf.object("cartesian", cartesian);
  1837. //>>includeEnd('debug');
  1838. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  1839. };
  1840. /**
  1841. * Computes the value of the minimum component for the supplied Cartesian.
  1842. *
  1843. * @param {Cartesian4} cartesian The cartesian to use.
  1844. * @returns {Number} The value of the minimum component.
  1845. */
  1846. Cartesian4.minimumComponent = function (cartesian) {
  1847. //>>includeStart('debug', pragmas.debug);
  1848. Check.Check.typeOf.object("cartesian", cartesian);
  1849. //>>includeEnd('debug');
  1850. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  1851. };
  1852. /**
  1853. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  1854. *
  1855. * @param {Cartesian4} first A cartesian to compare.
  1856. * @param {Cartesian4} second A cartesian to compare.
  1857. * @param {Cartesian4} result The object into which to store the result.
  1858. * @returns {Cartesian4} A cartesian with the minimum components.
  1859. */
  1860. Cartesian4.minimumByComponent = function (first, second, result) {
  1861. //>>includeStart('debug', pragmas.debug);
  1862. Check.Check.typeOf.object("first", first);
  1863. Check.Check.typeOf.object("second", second);
  1864. Check.Check.typeOf.object("result", result);
  1865. //>>includeEnd('debug');
  1866. result.x = Math.min(first.x, second.x);
  1867. result.y = Math.min(first.y, second.y);
  1868. result.z = Math.min(first.z, second.z);
  1869. result.w = Math.min(first.w, second.w);
  1870. return result;
  1871. };
  1872. /**
  1873. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  1874. *
  1875. * @param {Cartesian4} first A cartesian to compare.
  1876. * @param {Cartesian4} second A cartesian to compare.
  1877. * @param {Cartesian4} result The object into which to store the result.
  1878. * @returns {Cartesian4} A cartesian with the maximum components.
  1879. */
  1880. Cartesian4.maximumByComponent = function (first, second, result) {
  1881. //>>includeStart('debug', pragmas.debug);
  1882. Check.Check.typeOf.object("first", first);
  1883. Check.Check.typeOf.object("second", second);
  1884. Check.Check.typeOf.object("result", result);
  1885. //>>includeEnd('debug');
  1886. result.x = Math.max(first.x, second.x);
  1887. result.y = Math.max(first.y, second.y);
  1888. result.z = Math.max(first.z, second.z);
  1889. result.w = Math.max(first.w, second.w);
  1890. return result;
  1891. };
  1892. /**
  1893. * Computes the provided Cartesian's squared magnitude.
  1894. *
  1895. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  1896. * @returns {Number} The squared magnitude.
  1897. */
  1898. Cartesian4.magnitudeSquared = function (cartesian) {
  1899. //>>includeStart('debug', pragmas.debug);
  1900. Check.Check.typeOf.object("cartesian", cartesian);
  1901. //>>includeEnd('debug');
  1902. return (
  1903. cartesian.x * cartesian.x +
  1904. cartesian.y * cartesian.y +
  1905. cartesian.z * cartesian.z +
  1906. cartesian.w * cartesian.w
  1907. );
  1908. };
  1909. /**
  1910. * Computes the Cartesian's magnitude (length).
  1911. *
  1912. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  1913. * @returns {Number} The magnitude.
  1914. */
  1915. Cartesian4.magnitude = function (cartesian) {
  1916. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  1917. };
  1918. var distanceScratch = new Cartesian4();
  1919. /**
  1920. * Computes the 4-space distance between two points.
  1921. *
  1922. * @param {Cartesian4} left The first point to compute the distance from.
  1923. * @param {Cartesian4} right The second point to compute the distance to.
  1924. * @returns {Number} The distance between two points.
  1925. *
  1926. * @example
  1927. * // Returns 1.0
  1928. * var d = Cesium.Cartesian4.distance(
  1929. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  1930. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  1931. */
  1932. Cartesian4.distance = function (left, right) {
  1933. //>>includeStart('debug', pragmas.debug);
  1934. Check.Check.typeOf.object("left", left);
  1935. Check.Check.typeOf.object("right", right);
  1936. //>>includeEnd('debug');
  1937. Cartesian4.subtract(left, right, distanceScratch);
  1938. return Cartesian4.magnitude(distanceScratch);
  1939. };
  1940. /**
  1941. * Computes the squared distance between two points. Comparing squared distances
  1942. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  1943. *
  1944. * @param {Cartesian4} left The first point to compute the distance from.
  1945. * @param {Cartesian4} right The second point to compute the distance to.
  1946. * @returns {Number} The distance between two points.
  1947. *
  1948. * @example
  1949. * // Returns 4.0, not 2.0
  1950. * var d = Cesium.Cartesian4.distance(
  1951. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  1952. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  1953. */
  1954. Cartesian4.distanceSquared = function (left, right) {
  1955. //>>includeStart('debug', pragmas.debug);
  1956. Check.Check.typeOf.object("left", left);
  1957. Check.Check.typeOf.object("right", right);
  1958. //>>includeEnd('debug');
  1959. Cartesian4.subtract(left, right, distanceScratch);
  1960. return Cartesian4.magnitudeSquared(distanceScratch);
  1961. };
  1962. /**
  1963. * Computes the normalized form of the supplied Cartesian.
  1964. *
  1965. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  1966. * @param {Cartesian4} result The object onto which to store the result.
  1967. * @returns {Cartesian4} The modified result parameter.
  1968. */
  1969. Cartesian4.normalize = function (cartesian, result) {
  1970. //>>includeStart('debug', pragmas.debug);
  1971. Check.Check.typeOf.object("cartesian", cartesian);
  1972. Check.Check.typeOf.object("result", result);
  1973. //>>includeEnd('debug');
  1974. var magnitude = Cartesian4.magnitude(cartesian);
  1975. result.x = cartesian.x / magnitude;
  1976. result.y = cartesian.y / magnitude;
  1977. result.z = cartesian.z / magnitude;
  1978. result.w = cartesian.w / magnitude;
  1979. //>>includeStart('debug', pragmas.debug);
  1980. if (
  1981. isNaN(result.x) ||
  1982. isNaN(result.y) ||
  1983. isNaN(result.z) ||
  1984. isNaN(result.w)
  1985. ) {
  1986. throw new Check.DeveloperError("normalized result is not a number");
  1987. }
  1988. //>>includeEnd('debug');
  1989. return result;
  1990. };
  1991. /**
  1992. * Computes the dot (scalar) product of two Cartesians.
  1993. *
  1994. * @param {Cartesian4} left The first Cartesian.
  1995. * @param {Cartesian4} right The second Cartesian.
  1996. * @returns {Number} The dot product.
  1997. */
  1998. Cartesian4.dot = function (left, right) {
  1999. //>>includeStart('debug', pragmas.debug);
  2000. Check.Check.typeOf.object("left", left);
  2001. Check.Check.typeOf.object("right", right);
  2002. //>>includeEnd('debug');
  2003. return (
  2004. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  2005. );
  2006. };
  2007. /**
  2008. * Computes the componentwise product of two Cartesians.
  2009. *
  2010. * @param {Cartesian4} left The first Cartesian.
  2011. * @param {Cartesian4} right The second Cartesian.
  2012. * @param {Cartesian4} result The object onto which to store the result.
  2013. * @returns {Cartesian4} The modified result parameter.
  2014. */
  2015. Cartesian4.multiplyComponents = function (left, right, result) {
  2016. //>>includeStart('debug', pragmas.debug);
  2017. Check.Check.typeOf.object("left", left);
  2018. Check.Check.typeOf.object("right", right);
  2019. Check.Check.typeOf.object("result", result);
  2020. //>>includeEnd('debug');
  2021. result.x = left.x * right.x;
  2022. result.y = left.y * right.y;
  2023. result.z = left.z * right.z;
  2024. result.w = left.w * right.w;
  2025. return result;
  2026. };
  2027. /**
  2028. * Computes the componentwise quotient of two Cartesians.
  2029. *
  2030. * @param {Cartesian4} left The first Cartesian.
  2031. * @param {Cartesian4} right The second Cartesian.
  2032. * @param {Cartesian4} result The object onto which to store the result.
  2033. * @returns {Cartesian4} The modified result parameter.
  2034. */
  2035. Cartesian4.divideComponents = function (left, right, result) {
  2036. //>>includeStart('debug', pragmas.debug);
  2037. Check.Check.typeOf.object("left", left);
  2038. Check.Check.typeOf.object("right", right);
  2039. Check.Check.typeOf.object("result", result);
  2040. //>>includeEnd('debug');
  2041. result.x = left.x / right.x;
  2042. result.y = left.y / right.y;
  2043. result.z = left.z / right.z;
  2044. result.w = left.w / right.w;
  2045. return result;
  2046. };
  2047. /**
  2048. * Computes the componentwise sum of two Cartesians.
  2049. *
  2050. * @param {Cartesian4} left The first Cartesian.
  2051. * @param {Cartesian4} right The second Cartesian.
  2052. * @param {Cartesian4} result The object onto which to store the result.
  2053. * @returns {Cartesian4} The modified result parameter.
  2054. */
  2055. Cartesian4.add = function (left, right, result) {
  2056. //>>includeStart('debug', pragmas.debug);
  2057. Check.Check.typeOf.object("left", left);
  2058. Check.Check.typeOf.object("right", right);
  2059. Check.Check.typeOf.object("result", result);
  2060. //>>includeEnd('debug');
  2061. result.x = left.x + right.x;
  2062. result.y = left.y + right.y;
  2063. result.z = left.z + right.z;
  2064. result.w = left.w + right.w;
  2065. return result;
  2066. };
  2067. /**
  2068. * Computes the componentwise difference of two Cartesians.
  2069. *
  2070. * @param {Cartesian4} left The first Cartesian.
  2071. * @param {Cartesian4} right The second Cartesian.
  2072. * @param {Cartesian4} result The object onto which to store the result.
  2073. * @returns {Cartesian4} The modified result parameter.
  2074. */
  2075. Cartesian4.subtract = function (left, right, result) {
  2076. //>>includeStart('debug', pragmas.debug);
  2077. Check.Check.typeOf.object("left", left);
  2078. Check.Check.typeOf.object("right", right);
  2079. Check.Check.typeOf.object("result", result);
  2080. //>>includeEnd('debug');
  2081. result.x = left.x - right.x;
  2082. result.y = left.y - right.y;
  2083. result.z = left.z - right.z;
  2084. result.w = left.w - right.w;
  2085. return result;
  2086. };
  2087. /**
  2088. * Multiplies the provided Cartesian componentwise by the provided scalar.
  2089. *
  2090. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  2091. * @param {Number} scalar The scalar to multiply with.
  2092. * @param {Cartesian4} result The object onto which to store the result.
  2093. * @returns {Cartesian4} The modified result parameter.
  2094. */
  2095. Cartesian4.multiplyByScalar = function (cartesian, scalar, result) {
  2096. //>>includeStart('debug', pragmas.debug);
  2097. Check.Check.typeOf.object("cartesian", cartesian);
  2098. Check.Check.typeOf.number("scalar", scalar);
  2099. Check.Check.typeOf.object("result", result);
  2100. //>>includeEnd('debug');
  2101. result.x = cartesian.x * scalar;
  2102. result.y = cartesian.y * scalar;
  2103. result.z = cartesian.z * scalar;
  2104. result.w = cartesian.w * scalar;
  2105. return result;
  2106. };
  2107. /**
  2108. * Divides the provided Cartesian componentwise by the provided scalar.
  2109. *
  2110. * @param {Cartesian4} cartesian The Cartesian to be divided.
  2111. * @param {Number} scalar The scalar to divide by.
  2112. * @param {Cartesian4} result The object onto which to store the result.
  2113. * @returns {Cartesian4} The modified result parameter.
  2114. */
  2115. Cartesian4.divideByScalar = function (cartesian, scalar, result) {
  2116. //>>includeStart('debug', pragmas.debug);
  2117. Check.Check.typeOf.object("cartesian", cartesian);
  2118. Check.Check.typeOf.number("scalar", scalar);
  2119. Check.Check.typeOf.object("result", result);
  2120. //>>includeEnd('debug');
  2121. result.x = cartesian.x / scalar;
  2122. result.y = cartesian.y / scalar;
  2123. result.z = cartesian.z / scalar;
  2124. result.w = cartesian.w / scalar;
  2125. return result;
  2126. };
  2127. /**
  2128. * Negates the provided Cartesian.
  2129. *
  2130. * @param {Cartesian4} cartesian The Cartesian to be negated.
  2131. * @param {Cartesian4} result The object onto which to store the result.
  2132. * @returns {Cartesian4} The modified result parameter.
  2133. */
  2134. Cartesian4.negate = function (cartesian, result) {
  2135. //>>includeStart('debug', pragmas.debug);
  2136. Check.Check.typeOf.object("cartesian", cartesian);
  2137. Check.Check.typeOf.object("result", result);
  2138. //>>includeEnd('debug');
  2139. result.x = -cartesian.x;
  2140. result.y = -cartesian.y;
  2141. result.z = -cartesian.z;
  2142. result.w = -cartesian.w;
  2143. return result;
  2144. };
  2145. /**
  2146. * Computes the absolute value of the provided Cartesian.
  2147. *
  2148. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  2149. * @param {Cartesian4} result The object onto which to store the result.
  2150. * @returns {Cartesian4} The modified result parameter.
  2151. */
  2152. Cartesian4.abs = function (cartesian, result) {
  2153. //>>includeStart('debug', pragmas.debug);
  2154. Check.Check.typeOf.object("cartesian", cartesian);
  2155. Check.Check.typeOf.object("result", result);
  2156. //>>includeEnd('debug');
  2157. result.x = Math.abs(cartesian.x);
  2158. result.y = Math.abs(cartesian.y);
  2159. result.z = Math.abs(cartesian.z);
  2160. result.w = Math.abs(cartesian.w);
  2161. return result;
  2162. };
  2163. var lerpScratch = new Cartesian4();
  2164. /**
  2165. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  2166. *
  2167. * @param {Cartesian4} start The value corresponding to t at 0.0.
  2168. * @param {Cartesian4}end The value corresponding to t at 1.0.
  2169. * @param {Number} t The point along t at which to interpolate.
  2170. * @param {Cartesian4} result The object onto which to store the result.
  2171. * @returns {Cartesian4} The modified result parameter.
  2172. */
  2173. Cartesian4.lerp = function (start, end, t, result) {
  2174. //>>includeStart('debug', pragmas.debug);
  2175. Check.Check.typeOf.object("start", start);
  2176. Check.Check.typeOf.object("end", end);
  2177. Check.Check.typeOf.number("t", t);
  2178. Check.Check.typeOf.object("result", result);
  2179. //>>includeEnd('debug');
  2180. Cartesian4.multiplyByScalar(end, t, lerpScratch);
  2181. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  2182. return Cartesian4.add(lerpScratch, result, result);
  2183. };
  2184. var mostOrthogonalAxisScratch = new Cartesian4();
  2185. /**
  2186. * Returns the axis that is most orthogonal to the provided Cartesian.
  2187. *
  2188. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  2189. * @param {Cartesian4} result The object onto which to store the result.
  2190. * @returns {Cartesian4} The most orthogonal axis.
  2191. */
  2192. Cartesian4.mostOrthogonalAxis = function (cartesian, result) {
  2193. //>>includeStart('debug', pragmas.debug);
  2194. Check.Check.typeOf.object("cartesian", cartesian);
  2195. Check.Check.typeOf.object("result", result);
  2196. //>>includeEnd('debug');
  2197. var f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch);
  2198. Cartesian4.abs(f, f);
  2199. if (f.x <= f.y) {
  2200. if (f.x <= f.z) {
  2201. if (f.x <= f.w) {
  2202. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  2203. } else {
  2204. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  2205. }
  2206. } else if (f.z <= f.w) {
  2207. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  2208. } else {
  2209. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  2210. }
  2211. } else if (f.y <= f.z) {
  2212. if (f.y <= f.w) {
  2213. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  2214. } else {
  2215. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  2216. }
  2217. } else if (f.z <= f.w) {
  2218. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  2219. } else {
  2220. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  2221. }
  2222. return result;
  2223. };
  2224. /**
  2225. * Compares the provided Cartesians componentwise and returns
  2226. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2227. *
  2228. * @param {Cartesian4} [left] The first Cartesian.
  2229. * @param {Cartesian4} [right] The second Cartesian.
  2230. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2231. */
  2232. Cartesian4.equals = function (left, right) {
  2233. return (
  2234. left === right ||
  2235. (when.defined(left) &&
  2236. when.defined(right) &&
  2237. left.x === right.x &&
  2238. left.y === right.y &&
  2239. left.z === right.z &&
  2240. left.w === right.w)
  2241. );
  2242. };
  2243. /**
  2244. * @private
  2245. */
  2246. Cartesian4.equalsArray = function (cartesian, array, offset) {
  2247. return (
  2248. cartesian.x === array[offset] &&
  2249. cartesian.y === array[offset + 1] &&
  2250. cartesian.z === array[offset + 2] &&
  2251. cartesian.w === array[offset + 3]
  2252. );
  2253. };
  2254. /**
  2255. * Compares the provided Cartesians componentwise and returns
  2256. * <code>true</code> if they pass an absolute or relative tolerance test,
  2257. * <code>false</code> otherwise.
  2258. *
  2259. * @param {Cartesian4} [left] The first Cartesian.
  2260. * @param {Cartesian4} [right] The second Cartesian.
  2261. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  2262. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  2263. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2264. */
  2265. Cartesian4.equalsEpsilon = function (
  2266. left,
  2267. right,
  2268. relativeEpsilon,
  2269. absoluteEpsilon
  2270. ) {
  2271. return (
  2272. left === right ||
  2273. (when.defined(left) &&
  2274. when.defined(right) &&
  2275. _Math.CesiumMath.equalsEpsilon(
  2276. left.x,
  2277. right.x,
  2278. relativeEpsilon,
  2279. absoluteEpsilon
  2280. ) &&
  2281. _Math.CesiumMath.equalsEpsilon(
  2282. left.y,
  2283. right.y,
  2284. relativeEpsilon,
  2285. absoluteEpsilon
  2286. ) &&
  2287. _Math.CesiumMath.equalsEpsilon(
  2288. left.z,
  2289. right.z,
  2290. relativeEpsilon,
  2291. absoluteEpsilon
  2292. ) &&
  2293. _Math.CesiumMath.equalsEpsilon(
  2294. left.w,
  2295. right.w,
  2296. relativeEpsilon,
  2297. absoluteEpsilon
  2298. ))
  2299. );
  2300. };
  2301. /**
  2302. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  2303. *
  2304. * @type {Cartesian4}
  2305. * @constant
  2306. */
  2307. Cartesian4.ZERO = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  2308. /**
  2309. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  2310. *
  2311. * @type {Cartesian4}
  2312. * @constant
  2313. */
  2314. Cartesian4.UNIT_X = Object.freeze(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  2315. /**
  2316. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  2317. *
  2318. * @type {Cartesian4}
  2319. * @constant
  2320. */
  2321. Cartesian4.UNIT_Y = Object.freeze(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  2322. /**
  2323. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  2324. *
  2325. * @type {Cartesian4}
  2326. * @constant
  2327. */
  2328. Cartesian4.UNIT_Z = Object.freeze(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  2329. /**
  2330. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  2331. *
  2332. * @type {Cartesian4}
  2333. * @constant
  2334. */
  2335. Cartesian4.UNIT_W = Object.freeze(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  2336. /**
  2337. * Duplicates this Cartesian4 instance.
  2338. *
  2339. * @param {Cartesian4} [result] The object onto which to store the result.
  2340. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  2341. */
  2342. Cartesian4.prototype.clone = function (result) {
  2343. return Cartesian4.clone(this, result);
  2344. };
  2345. /**
  2346. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2347. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2348. *
  2349. * @param {Cartesian4} [right] The right hand side Cartesian.
  2350. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2351. */
  2352. Cartesian4.prototype.equals = function (right) {
  2353. return Cartesian4.equals(this, right);
  2354. };
  2355. /**
  2356. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2357. * <code>true</code> if they pass an absolute or relative tolerance test,
  2358. * <code>false</code> otherwise.
  2359. *
  2360. * @param {Cartesian4} [right] The right hand side Cartesian.
  2361. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  2362. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  2363. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  2364. */
  2365. Cartesian4.prototype.equalsEpsilon = function (
  2366. right,
  2367. relativeEpsilon,
  2368. absoluteEpsilon
  2369. ) {
  2370. return Cartesian4.equalsEpsilon(
  2371. this,
  2372. right,
  2373. relativeEpsilon,
  2374. absoluteEpsilon
  2375. );
  2376. };
  2377. /**
  2378. * Creates a string representing this Cartesian in the format '(x, y, z, w)'.
  2379. *
  2380. * @returns {String} A string representing the provided Cartesian in the format '(x, y, z, w)'.
  2381. */
  2382. Cartesian4.prototype.toString = function () {
  2383. return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.w + ")";
  2384. };
  2385. var scratchFloatArray = new Float32Array(1);
  2386. var SHIFT_LEFT_8 = 256.0;
  2387. var SHIFT_LEFT_16 = 65536.0;
  2388. var SHIFT_LEFT_24 = 16777216.0;
  2389. var SHIFT_RIGHT_8 = 1.0 / SHIFT_LEFT_8;
  2390. var SHIFT_RIGHT_16 = 1.0 / SHIFT_LEFT_16;
  2391. var SHIFT_RIGHT_24 = 1.0 / SHIFT_LEFT_24;
  2392. var BIAS = 38.0;
  2393. /**
  2394. * Packs an arbitrary floating point value to 4 values representable using uint8.
  2395. *
  2396. * @param {Number} value A floating point number
  2397. * @param {Cartesian4} [result] The Cartesian4 that will contain the packed float.
  2398. * @returns {Cartesian4} A Cartesian4 representing the float packed to values in x, y, z, and w.
  2399. */
  2400. Cartesian4.packFloat = function (value, result) {
  2401. //>>includeStart('debug', pragmas.debug);
  2402. Check.Check.typeOf.number("value", value);
  2403. //>>includeEnd('debug');
  2404. if (!when.defined(result)) {
  2405. result = new Cartesian4();
  2406. }
  2407. // Force the value to 32 bit precision
  2408. scratchFloatArray[0] = value;
  2409. value = scratchFloatArray[0];
  2410. if (value === 0.0) {
  2411. return Cartesian4.clone(Cartesian4.ZERO, result);
  2412. }
  2413. var sign = value < 0.0 ? 1.0 : 0.0;
  2414. var exponent;
  2415. if (!isFinite(value)) {
  2416. value = 0.1;
  2417. exponent = BIAS;
  2418. } else {
  2419. value = Math.abs(value);
  2420. exponent = Math.floor(_Math.CesiumMath.logBase(value, 10)) + 1.0;
  2421. value = value / Math.pow(10.0, exponent);
  2422. }
  2423. var temp = value * SHIFT_LEFT_8;
  2424. result.x = Math.floor(temp);
  2425. temp = (temp - result.x) * SHIFT_LEFT_8;
  2426. result.y = Math.floor(temp);
  2427. temp = (temp - result.y) * SHIFT_LEFT_8;
  2428. result.z = Math.floor(temp);
  2429. result.w = (exponent + BIAS) * 2.0 + sign;
  2430. return result;
  2431. };
  2432. /**
  2433. * Unpacks a float packed using Cartesian4.packFloat.
  2434. *
  2435. * @param {Cartesian4} packedFloat A Cartesian4 containing a float packed to 4 values representable using uint8.
  2436. * @returns {Number} The unpacked float.
  2437. * @private
  2438. */
  2439. Cartesian4.unpackFloat = function (packedFloat) {
  2440. //>>includeStart('debug', pragmas.debug);
  2441. Check.Check.typeOf.object("packedFloat", packedFloat);
  2442. //>>includeEnd('debug');
  2443. var temp = packedFloat.w / 2.0;
  2444. var exponent = Math.floor(temp);
  2445. var sign = (temp - exponent) * 2.0;
  2446. exponent = exponent - BIAS;
  2447. sign = sign * 2.0 - 1.0;
  2448. sign = -sign;
  2449. if (exponent >= BIAS) {
  2450. return sign < 0.0 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
  2451. }
  2452. var unpacked = sign * packedFloat.x * SHIFT_RIGHT_8;
  2453. unpacked += sign * packedFloat.y * SHIFT_RIGHT_16;
  2454. unpacked += sign * packedFloat.z * SHIFT_RIGHT_24;
  2455. return unpacked * Math.pow(10.0, exponent);
  2456. };
  2457. /**
  2458. * A 4x4 matrix, indexable as a column-major order array.
  2459. * Constructor parameters are in row-major order for code readability.
  2460. * @alias Matrix4
  2461. * @constructor
  2462. * @implements {ArrayLike<number>}
  2463. *
  2464. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  2465. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  2466. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  2467. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  2468. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  2469. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  2470. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  2471. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  2472. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  2473. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  2474. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  2475. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  2476. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  2477. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  2478. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  2479. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  2480. *
  2481. * @see Matrix4.fromColumnMajorArray
  2482. * @see Matrix4.fromRowMajorArray
  2483. * @see Matrix4.fromRotationTranslation
  2484. * @see Matrix4.fromTranslationRotationScale
  2485. * @see Matrix4.fromTranslationQuaternionRotationScale
  2486. * @see Matrix4.fromTranslation
  2487. * @see Matrix4.fromScale
  2488. * @see Matrix4.fromUniformScale
  2489. * @see Matrix4.fromCamera
  2490. * @see Matrix4.computePerspectiveFieldOfView
  2491. * @see Matrix4.computeOrthographicOffCenter
  2492. * @see Matrix4.computePerspectiveOffCenter
  2493. * @see Matrix4.computeInfinitePerspectiveOffCenter
  2494. * @see Matrix4.computeViewportTransformation
  2495. * @see Matrix4.computeView
  2496. * @see Matrix2
  2497. * @see Matrix3
  2498. * @see Packable
  2499. */
  2500. function Matrix4(
  2501. column0Row0,
  2502. column1Row0,
  2503. column2Row0,
  2504. column3Row0,
  2505. column0Row1,
  2506. column1Row1,
  2507. column2Row1,
  2508. column3Row1,
  2509. column0Row2,
  2510. column1Row2,
  2511. column2Row2,
  2512. column3Row2,
  2513. column0Row3,
  2514. column1Row3,
  2515. column2Row3,
  2516. column3Row3
  2517. ) {
  2518. this[0] = when.defaultValue(column0Row0, 0.0);
  2519. this[1] = when.defaultValue(column0Row1, 0.0);
  2520. this[2] = when.defaultValue(column0Row2, 0.0);
  2521. this[3] = when.defaultValue(column0Row3, 0.0);
  2522. this[4] = when.defaultValue(column1Row0, 0.0);
  2523. this[5] = when.defaultValue(column1Row1, 0.0);
  2524. this[6] = when.defaultValue(column1Row2, 0.0);
  2525. this[7] = when.defaultValue(column1Row3, 0.0);
  2526. this[8] = when.defaultValue(column2Row0, 0.0);
  2527. this[9] = when.defaultValue(column2Row1, 0.0);
  2528. this[10] = when.defaultValue(column2Row2, 0.0);
  2529. this[11] = when.defaultValue(column2Row3, 0.0);
  2530. this[12] = when.defaultValue(column3Row0, 0.0);
  2531. this[13] = when.defaultValue(column3Row1, 0.0);
  2532. this[14] = when.defaultValue(column3Row2, 0.0);
  2533. this[15] = when.defaultValue(column3Row3, 0.0);
  2534. }
  2535. /**
  2536. * The number of elements used to pack the object into an array.
  2537. * @type {Number}
  2538. */
  2539. Matrix4.packedLength = 16;
  2540. /**
  2541. * Stores the provided instance into the provided array.
  2542. *
  2543. * @param {Matrix4} value The value to pack.
  2544. * @param {Number[]} array The array to pack into.
  2545. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2546. *
  2547. * @returns {Number[]} The array that was packed into
  2548. */
  2549. Matrix4.pack = function (value, array, startingIndex) {
  2550. //>>includeStart('debug', pragmas.debug);
  2551. Check.Check.typeOf.object("value", value);
  2552. Check.Check.defined("array", array);
  2553. //>>includeEnd('debug');
  2554. startingIndex = when.defaultValue(startingIndex, 0);
  2555. array[startingIndex++] = value[0];
  2556. array[startingIndex++] = value[1];
  2557. array[startingIndex++] = value[2];
  2558. array[startingIndex++] = value[3];
  2559. array[startingIndex++] = value[4];
  2560. array[startingIndex++] = value[5];
  2561. array[startingIndex++] = value[6];
  2562. array[startingIndex++] = value[7];
  2563. array[startingIndex++] = value[8];
  2564. array[startingIndex++] = value[9];
  2565. array[startingIndex++] = value[10];
  2566. array[startingIndex++] = value[11];
  2567. array[startingIndex++] = value[12];
  2568. array[startingIndex++] = value[13];
  2569. array[startingIndex++] = value[14];
  2570. array[startingIndex] = value[15];
  2571. return array;
  2572. };
  2573. /**
  2574. * Retrieves an instance from a packed array.
  2575. *
  2576. * @param {Number[]} array The packed array.
  2577. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2578. * @param {Matrix4} [result] The object into which to store the result.
  2579. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  2580. */
  2581. Matrix4.unpack = function (array, startingIndex, result) {
  2582. //>>includeStart('debug', pragmas.debug);
  2583. Check.Check.defined("array", array);
  2584. //>>includeEnd('debug');
  2585. startingIndex = when.defaultValue(startingIndex, 0);
  2586. if (!when.defined(result)) {
  2587. result = new Matrix4();
  2588. }
  2589. result[0] = array[startingIndex++];
  2590. result[1] = array[startingIndex++];
  2591. result[2] = array[startingIndex++];
  2592. result[3] = array[startingIndex++];
  2593. result[4] = array[startingIndex++];
  2594. result[5] = array[startingIndex++];
  2595. result[6] = array[startingIndex++];
  2596. result[7] = array[startingIndex++];
  2597. result[8] = array[startingIndex++];
  2598. result[9] = array[startingIndex++];
  2599. result[10] = array[startingIndex++];
  2600. result[11] = array[startingIndex++];
  2601. result[12] = array[startingIndex++];
  2602. result[13] = array[startingIndex++];
  2603. result[14] = array[startingIndex++];
  2604. result[15] = array[startingIndex];
  2605. return result;
  2606. };
  2607. /**
  2608. * Duplicates a Matrix4 instance.
  2609. *
  2610. * @param {Matrix4} matrix The matrix to duplicate.
  2611. * @param {Matrix4} [result] The object onto which to store the result.
  2612. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  2613. */
  2614. Matrix4.clone = function (matrix, result) {
  2615. if (!when.defined(matrix)) {
  2616. return undefined;
  2617. }
  2618. if (!when.defined(result)) {
  2619. return new Matrix4(
  2620. matrix[0],
  2621. matrix[4],
  2622. matrix[8],
  2623. matrix[12],
  2624. matrix[1],
  2625. matrix[5],
  2626. matrix[9],
  2627. matrix[13],
  2628. matrix[2],
  2629. matrix[6],
  2630. matrix[10],
  2631. matrix[14],
  2632. matrix[3],
  2633. matrix[7],
  2634. matrix[11],
  2635. matrix[15]
  2636. );
  2637. }
  2638. result[0] = matrix[0];
  2639. result[1] = matrix[1];
  2640. result[2] = matrix[2];
  2641. result[3] = matrix[3];
  2642. result[4] = matrix[4];
  2643. result[5] = matrix[5];
  2644. result[6] = matrix[6];
  2645. result[7] = matrix[7];
  2646. result[8] = matrix[8];
  2647. result[9] = matrix[9];
  2648. result[10] = matrix[10];
  2649. result[11] = matrix[11];
  2650. result[12] = matrix[12];
  2651. result[13] = matrix[13];
  2652. result[14] = matrix[14];
  2653. result[15] = matrix[15];
  2654. return result;
  2655. };
  2656. /**
  2657. * Creates a Matrix4 from 16 consecutive elements in an array.
  2658. * @function
  2659. *
  2660. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  2661. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  2662. * @param {Matrix4} [result] The object onto which to store the result.
  2663. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  2664. *
  2665. * @example
  2666. * // Create the Matrix4:
  2667. * // [1.0, 2.0, 3.0, 4.0]
  2668. * // [1.0, 2.0, 3.0, 4.0]
  2669. * // [1.0, 2.0, 3.0, 4.0]
  2670. * // [1.0, 2.0, 3.0, 4.0]
  2671. *
  2672. * var 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];
  2673. * var m = Cesium.Matrix4.fromArray(v);
  2674. *
  2675. * // Create same Matrix4 with using an offset into an array
  2676. * var 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];
  2677. * var m2 = Cesium.Matrix4.fromArray(v2, 2);
  2678. */
  2679. Matrix4.fromArray = Matrix4.unpack;
  2680. /**
  2681. * Computes a Matrix4 instance from a column-major order array.
  2682. *
  2683. * @param {Number[]} values The column-major order array.
  2684. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2685. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2686. */
  2687. Matrix4.fromColumnMajorArray = function (values, result) {
  2688. //>>includeStart('debug', pragmas.debug);
  2689. Check.Check.defined("values", values);
  2690. //>>includeEnd('debug');
  2691. return Matrix4.clone(values, result);
  2692. };
  2693. /**
  2694. * Computes a Matrix4 instance from a row-major order array.
  2695. * The resulting matrix will be in column-major order.
  2696. *
  2697. * @param {Number[]} values The row-major order array.
  2698. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2699. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2700. */
  2701. Matrix4.fromRowMajorArray = function (values, result) {
  2702. //>>includeStart('debug', pragmas.debug);
  2703. Check.Check.defined("values", values);
  2704. //>>includeEnd('debug');
  2705. if (!when.defined(result)) {
  2706. return new Matrix4(
  2707. values[0],
  2708. values[1],
  2709. values[2],
  2710. values[3],
  2711. values[4],
  2712. values[5],
  2713. values[6],
  2714. values[7],
  2715. values[8],
  2716. values[9],
  2717. values[10],
  2718. values[11],
  2719. values[12],
  2720. values[13],
  2721. values[14],
  2722. values[15]
  2723. );
  2724. }
  2725. result[0] = values[0];
  2726. result[1] = values[4];
  2727. result[2] = values[8];
  2728. result[3] = values[12];
  2729. result[4] = values[1];
  2730. result[5] = values[5];
  2731. result[6] = values[9];
  2732. result[7] = values[13];
  2733. result[8] = values[2];
  2734. result[9] = values[6];
  2735. result[10] = values[10];
  2736. result[11] = values[14];
  2737. result[12] = values[3];
  2738. result[13] = values[7];
  2739. result[14] = values[11];
  2740. result[15] = values[15];
  2741. return result;
  2742. };
  2743. /**
  2744. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  2745. * and a Cartesian3 representing the translation.
  2746. *
  2747. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  2748. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  2749. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2750. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2751. */
  2752. Matrix4.fromRotationTranslation = function (rotation, translation, result) {
  2753. //>>includeStart('debug', pragmas.debug);
  2754. Check.Check.typeOf.object("rotation", rotation);
  2755. //>>includeEnd('debug');
  2756. translation = when.defaultValue(translation, Cartesian2.Cartesian3.ZERO);
  2757. if (!when.defined(result)) {
  2758. return new Matrix4(
  2759. rotation[0],
  2760. rotation[3],
  2761. rotation[6],
  2762. translation.x,
  2763. rotation[1],
  2764. rotation[4],
  2765. rotation[7],
  2766. translation.y,
  2767. rotation[2],
  2768. rotation[5],
  2769. rotation[8],
  2770. translation.z,
  2771. 0.0,
  2772. 0.0,
  2773. 0.0,
  2774. 1.0
  2775. );
  2776. }
  2777. result[0] = rotation[0];
  2778. result[1] = rotation[1];
  2779. result[2] = rotation[2];
  2780. result[3] = 0.0;
  2781. result[4] = rotation[3];
  2782. result[5] = rotation[4];
  2783. result[6] = rotation[5];
  2784. result[7] = 0.0;
  2785. result[8] = rotation[6];
  2786. result[9] = rotation[7];
  2787. result[10] = rotation[8];
  2788. result[11] = 0.0;
  2789. result[12] = translation.x;
  2790. result[13] = translation.y;
  2791. result[14] = translation.z;
  2792. result[15] = 1.0;
  2793. return result;
  2794. };
  2795. /**
  2796. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  2797. * representation with the rotation represented as a quaternion.
  2798. *
  2799. * @param {Cartesian3} translation The translation transformation.
  2800. * @param {Quaternion} rotation The rotation transformation.
  2801. * @param {Cartesian3} scale The non-uniform scale transformation.
  2802. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2803. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2804. *
  2805. * @example
  2806. * var result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  2807. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  2808. * Cesium.Quaternion.IDENTITY, // rotation
  2809. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  2810. * result);
  2811. */
  2812. Matrix4.fromTranslationQuaternionRotationScale = function (
  2813. translation,
  2814. rotation,
  2815. scale,
  2816. result
  2817. ) {
  2818. //>>includeStart('debug', pragmas.debug);
  2819. Check.Check.typeOf.object("translation", translation);
  2820. Check.Check.typeOf.object("rotation", rotation);
  2821. Check.Check.typeOf.object("scale", scale);
  2822. //>>includeEnd('debug');
  2823. if (!when.defined(result)) {
  2824. result = new Matrix4();
  2825. }
  2826. var scaleX = scale.x;
  2827. var scaleY = scale.y;
  2828. var scaleZ = scale.z;
  2829. var x2 = rotation.x * rotation.x;
  2830. var xy = rotation.x * rotation.y;
  2831. var xz = rotation.x * rotation.z;
  2832. var xw = rotation.x * rotation.w;
  2833. var y2 = rotation.y * rotation.y;
  2834. var yz = rotation.y * rotation.z;
  2835. var yw = rotation.y * rotation.w;
  2836. var z2 = rotation.z * rotation.z;
  2837. var zw = rotation.z * rotation.w;
  2838. var w2 = rotation.w * rotation.w;
  2839. var m00 = x2 - y2 - z2 + w2;
  2840. var m01 = 2.0 * (xy - zw);
  2841. var m02 = 2.0 * (xz + yw);
  2842. var m10 = 2.0 * (xy + zw);
  2843. var m11 = -x2 + y2 - z2 + w2;
  2844. var m12 = 2.0 * (yz - xw);
  2845. var m20 = 2.0 * (xz - yw);
  2846. var m21 = 2.0 * (yz + xw);
  2847. var m22 = -x2 - y2 + z2 + w2;
  2848. result[0] = m00 * scaleX;
  2849. result[1] = m10 * scaleX;
  2850. result[2] = m20 * scaleX;
  2851. result[3] = 0.0;
  2852. result[4] = m01 * scaleY;
  2853. result[5] = m11 * scaleY;
  2854. result[6] = m21 * scaleY;
  2855. result[7] = 0.0;
  2856. result[8] = m02 * scaleZ;
  2857. result[9] = m12 * scaleZ;
  2858. result[10] = m22 * scaleZ;
  2859. result[11] = 0.0;
  2860. result[12] = translation.x;
  2861. result[13] = translation.y;
  2862. result[14] = translation.z;
  2863. result[15] = 1.0;
  2864. return result;
  2865. };
  2866. /**
  2867. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  2868. *
  2869. * @param {TranslationRotationScale} translationRotationScale The instance.
  2870. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2871. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2872. */
  2873. Matrix4.fromTranslationRotationScale = function (
  2874. translationRotationScale,
  2875. result
  2876. ) {
  2877. //>>includeStart('debug', pragmas.debug);
  2878. Check.Check.typeOf.object("translationRotationScale", translationRotationScale);
  2879. //>>includeEnd('debug');
  2880. return Matrix4.fromTranslationQuaternionRotationScale(
  2881. translationRotationScale.translation,
  2882. translationRotationScale.rotation,
  2883. translationRotationScale.scale,
  2884. result
  2885. );
  2886. };
  2887. /**
  2888. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  2889. *
  2890. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  2891. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2892. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2893. *
  2894. * @see Matrix4.multiplyByTranslation
  2895. */
  2896. Matrix4.fromTranslation = function (translation, result) {
  2897. //>>includeStart('debug', pragmas.debug);
  2898. Check.Check.typeOf.object("translation", translation);
  2899. //>>includeEnd('debug');
  2900. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  2901. };
  2902. /**
  2903. * Computes a Matrix4 instance representing a non-uniform scale.
  2904. *
  2905. * @param {Cartesian3} scale The x, y, and z scale factors.
  2906. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2907. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2908. *
  2909. * @example
  2910. * // Creates
  2911. * // [7.0, 0.0, 0.0, 0.0]
  2912. * // [0.0, 8.0, 0.0, 0.0]
  2913. * // [0.0, 0.0, 9.0, 0.0]
  2914. * // [0.0, 0.0, 0.0, 1.0]
  2915. * var m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  2916. */
  2917. Matrix4.fromScale = function (scale, result) {
  2918. //>>includeStart('debug', pragmas.debug);
  2919. Check.Check.typeOf.object("scale", scale);
  2920. //>>includeEnd('debug');
  2921. if (!when.defined(result)) {
  2922. return new Matrix4(
  2923. scale.x,
  2924. 0.0,
  2925. 0.0,
  2926. 0.0,
  2927. 0.0,
  2928. scale.y,
  2929. 0.0,
  2930. 0.0,
  2931. 0.0,
  2932. 0.0,
  2933. scale.z,
  2934. 0.0,
  2935. 0.0,
  2936. 0.0,
  2937. 0.0,
  2938. 1.0
  2939. );
  2940. }
  2941. result[0] = scale.x;
  2942. result[1] = 0.0;
  2943. result[2] = 0.0;
  2944. result[3] = 0.0;
  2945. result[4] = 0.0;
  2946. result[5] = scale.y;
  2947. result[6] = 0.0;
  2948. result[7] = 0.0;
  2949. result[8] = 0.0;
  2950. result[9] = 0.0;
  2951. result[10] = scale.z;
  2952. result[11] = 0.0;
  2953. result[12] = 0.0;
  2954. result[13] = 0.0;
  2955. result[14] = 0.0;
  2956. result[15] = 1.0;
  2957. return result;
  2958. };
  2959. /**
  2960. * Computes a Matrix4 instance representing a uniform scale.
  2961. *
  2962. * @param {Number} scale The uniform scale factor.
  2963. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  2964. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  2965. *
  2966. * @example
  2967. * // Creates
  2968. * // [2.0, 0.0, 0.0, 0.0]
  2969. * // [0.0, 2.0, 0.0, 0.0]
  2970. * // [0.0, 0.0, 2.0, 0.0]
  2971. * // [0.0, 0.0, 0.0, 1.0]
  2972. * var m = Cesium.Matrix4.fromUniformScale(2.0);
  2973. */
  2974. Matrix4.fromUniformScale = function (scale, result) {
  2975. //>>includeStart('debug', pragmas.debug);
  2976. Check.Check.typeOf.number("scale", scale);
  2977. //>>includeEnd('debug');
  2978. if (!when.defined(result)) {
  2979. return new Matrix4(
  2980. scale,
  2981. 0.0,
  2982. 0.0,
  2983. 0.0,
  2984. 0.0,
  2985. scale,
  2986. 0.0,
  2987. 0.0,
  2988. 0.0,
  2989. 0.0,
  2990. scale,
  2991. 0.0,
  2992. 0.0,
  2993. 0.0,
  2994. 0.0,
  2995. 1.0
  2996. );
  2997. }
  2998. result[0] = scale;
  2999. result[1] = 0.0;
  3000. result[2] = 0.0;
  3001. result[3] = 0.0;
  3002. result[4] = 0.0;
  3003. result[5] = scale;
  3004. result[6] = 0.0;
  3005. result[7] = 0.0;
  3006. result[8] = 0.0;
  3007. result[9] = 0.0;
  3008. result[10] = scale;
  3009. result[11] = 0.0;
  3010. result[12] = 0.0;
  3011. result[13] = 0.0;
  3012. result[14] = 0.0;
  3013. result[15] = 1.0;
  3014. return result;
  3015. };
  3016. var fromCameraF = new Cartesian2.Cartesian3();
  3017. var fromCameraR = new Cartesian2.Cartesian3();
  3018. var fromCameraU = new Cartesian2.Cartesian3();
  3019. /**
  3020. * Computes a Matrix4 instance from a Camera.
  3021. *
  3022. * @param {Camera} camera The camera to use.
  3023. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3024. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  3025. */
  3026. Matrix4.fromCamera = function (camera, result) {
  3027. //>>includeStart('debug', pragmas.debug);
  3028. Check.Check.typeOf.object("camera", camera);
  3029. //>>includeEnd('debug');
  3030. var position = camera.position;
  3031. var direction = camera.direction;
  3032. var up = camera.up;
  3033. //>>includeStart('debug', pragmas.debug);
  3034. Check.Check.typeOf.object("camera.position", position);
  3035. Check.Check.typeOf.object("camera.direction", direction);
  3036. Check.Check.typeOf.object("camera.up", up);
  3037. //>>includeEnd('debug');
  3038. Cartesian2.Cartesian3.normalize(direction, fromCameraF);
  3039. Cartesian2.Cartesian3.normalize(
  3040. Cartesian2.Cartesian3.cross(fromCameraF, up, fromCameraR),
  3041. fromCameraR
  3042. );
  3043. Cartesian2.Cartesian3.normalize(
  3044. Cartesian2.Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU),
  3045. fromCameraU
  3046. );
  3047. var sX = fromCameraR.x;
  3048. var sY = fromCameraR.y;
  3049. var sZ = fromCameraR.z;
  3050. var fX = fromCameraF.x;
  3051. var fY = fromCameraF.y;
  3052. var fZ = fromCameraF.z;
  3053. var uX = fromCameraU.x;
  3054. var uY = fromCameraU.y;
  3055. var uZ = fromCameraU.z;
  3056. var positionX = position.x;
  3057. var positionY = position.y;
  3058. var positionZ = position.z;
  3059. var t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
  3060. var t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
  3061. var t2 = fX * positionX + fY * positionY + fZ * positionZ;
  3062. // The code below this comment is an optimized
  3063. // version of the commented lines.
  3064. // Rather that create two matrices and then multiply,
  3065. // we just bake in the multiplcation as part of creation.
  3066. // var rotation = new Matrix4(
  3067. // sX, sY, sZ, 0.0,
  3068. // uX, uY, uZ, 0.0,
  3069. // -fX, -fY, -fZ, 0.0,
  3070. // 0.0, 0.0, 0.0, 1.0);
  3071. // var translation = new Matrix4(
  3072. // 1.0, 0.0, 0.0, -position.x,
  3073. // 0.0, 1.0, 0.0, -position.y,
  3074. // 0.0, 0.0, 1.0, -position.z,
  3075. // 0.0, 0.0, 0.0, 1.0);
  3076. // return rotation.multiply(translation);
  3077. if (!when.defined(result)) {
  3078. return new Matrix4(
  3079. sX,
  3080. sY,
  3081. sZ,
  3082. t0,
  3083. uX,
  3084. uY,
  3085. uZ,
  3086. t1,
  3087. -fX,
  3088. -fY,
  3089. -fZ,
  3090. t2,
  3091. 0.0,
  3092. 0.0,
  3093. 0.0,
  3094. 1.0
  3095. );
  3096. }
  3097. result[0] = sX;
  3098. result[1] = uX;
  3099. result[2] = -fX;
  3100. result[3] = 0.0;
  3101. result[4] = sY;
  3102. result[5] = uY;
  3103. result[6] = -fY;
  3104. result[7] = 0.0;
  3105. result[8] = sZ;
  3106. result[9] = uZ;
  3107. result[10] = -fZ;
  3108. result[11] = 0.0;
  3109. result[12] = t0;
  3110. result[13] = t1;
  3111. result[14] = t2;
  3112. result[15] = 1.0;
  3113. return result;
  3114. };
  3115. /**
  3116. * Computes a Matrix4 instance representing a perspective transformation matrix.
  3117. *
  3118. * @param {Number} fovY The field of view along the Y axis in radians.
  3119. * @param {Number} aspectRatio The aspect ratio.
  3120. * @param {Number} near The distance to the near plane in meters.
  3121. * @param {Number} far The distance to the far plane in meters.
  3122. * @param {Matrix4} result The object in which the result will be stored.
  3123. * @returns {Matrix4} The modified result parameter.
  3124. *
  3125. * @exception {DeveloperError} fovY must be in (0, PI].
  3126. * @exception {DeveloperError} aspectRatio must be greater than zero.
  3127. * @exception {DeveloperError} near must be greater than zero.
  3128. * @exception {DeveloperError} far must be greater than zero.
  3129. */
  3130. Matrix4.computePerspectiveFieldOfView = function (
  3131. fovY,
  3132. aspectRatio,
  3133. near,
  3134. far,
  3135. result
  3136. ) {
  3137. //>>includeStart('debug', pragmas.debug);
  3138. Check.Check.typeOf.number.greaterThan("fovY", fovY, 0.0);
  3139. Check.Check.typeOf.number.lessThan("fovY", fovY, Math.PI);
  3140. Check.Check.typeOf.number.greaterThan("near", near, 0.0);
  3141. Check.Check.typeOf.number.greaterThan("far", far, 0.0);
  3142. Check.Check.typeOf.object("result", result);
  3143. //>>includeEnd('debug');
  3144. var bottom = Math.tan(fovY * 0.5);
  3145. var column1Row1 = 1.0 / bottom;
  3146. var column0Row0 = column1Row1 / aspectRatio;
  3147. var column2Row2 = (far + near) / (near - far);
  3148. var column3Row2 = (2.0 * far * near) / (near - far);
  3149. result[0] = column0Row0;
  3150. result[1] = 0.0;
  3151. result[2] = 0.0;
  3152. result[3] = 0.0;
  3153. result[4] = 0.0;
  3154. result[5] = column1Row1;
  3155. result[6] = 0.0;
  3156. result[7] = 0.0;
  3157. result[8] = 0.0;
  3158. result[9] = 0.0;
  3159. result[10] = column2Row2;
  3160. result[11] = -1.0;
  3161. result[12] = 0.0;
  3162. result[13] = 0.0;
  3163. result[14] = column3Row2;
  3164. result[15] = 0.0;
  3165. return result;
  3166. };
  3167. /**
  3168. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  3169. *
  3170. * @param {Number} left The number of meters to the left of the camera that will be in view.
  3171. * @param {Number} right The number of meters to the right of the camera that will be in view.
  3172. * @param {Number} bottom The number of meters below of the camera that will be in view.
  3173. * @param {Number} top The number of meters above of the camera that will be in view.
  3174. * @param {Number} near The distance to the near plane in meters.
  3175. * @param {Number} far The distance to the far plane in meters.
  3176. * @param {Matrix4} result The object in which the result will be stored.
  3177. * @returns {Matrix4} The modified result parameter.
  3178. */
  3179. Matrix4.computeOrthographicOffCenter = function (
  3180. left,
  3181. right,
  3182. bottom,
  3183. top,
  3184. near,
  3185. far,
  3186. result
  3187. ) {
  3188. //>>includeStart('debug', pragmas.debug);
  3189. Check.Check.typeOf.number("left", left);
  3190. Check.Check.typeOf.number("right", right);
  3191. Check.Check.typeOf.number("bottom", bottom);
  3192. Check.Check.typeOf.number("top", top);
  3193. Check.Check.typeOf.number("near", near);
  3194. Check.Check.typeOf.number("far", far);
  3195. Check.Check.typeOf.object("result", result);
  3196. //>>includeEnd('debug');
  3197. var a = 1.0 / (right - left);
  3198. var b = 1.0 / (top - bottom);
  3199. var c = 1.0 / (far - near);
  3200. var tx = -(right + left) * a;
  3201. var ty = -(top + bottom) * b;
  3202. var tz = -(far + near) * c;
  3203. a *= 2.0;
  3204. b *= 2.0;
  3205. c *= -2.0;
  3206. result[0] = a;
  3207. result[1] = 0.0;
  3208. result[2] = 0.0;
  3209. result[3] = 0.0;
  3210. result[4] = 0.0;
  3211. result[5] = b;
  3212. result[6] = 0.0;
  3213. result[7] = 0.0;
  3214. result[8] = 0.0;
  3215. result[9] = 0.0;
  3216. result[10] = c;
  3217. result[11] = 0.0;
  3218. result[12] = tx;
  3219. result[13] = ty;
  3220. result[14] = tz;
  3221. result[15] = 1.0;
  3222. return result;
  3223. };
  3224. /**
  3225. * Computes a Matrix4 instance representing an off center perspective transformation.
  3226. *
  3227. * @param {Number} left The number of meters to the left of the camera that will be in view.
  3228. * @param {Number} right The number of meters to the right of the camera that will be in view.
  3229. * @param {Number} bottom The number of meters below of the camera that will be in view.
  3230. * @param {Number} top The number of meters above of the camera that will be in view.
  3231. * @param {Number} near The distance to the near plane in meters.
  3232. * @param {Number} far The distance to the far plane in meters.
  3233. * @param {Matrix4} result The object in which the result will be stored.
  3234. * @returns {Matrix4} The modified result parameter.
  3235. */
  3236. Matrix4.computePerspectiveOffCenter = function (
  3237. left,
  3238. right,
  3239. bottom,
  3240. top,
  3241. near,
  3242. far,
  3243. result
  3244. ) {
  3245. //>>includeStart('debug', pragmas.debug);
  3246. Check.Check.typeOf.number("left", left);
  3247. Check.Check.typeOf.number("right", right);
  3248. Check.Check.typeOf.number("bottom", bottom);
  3249. Check.Check.typeOf.number("top", top);
  3250. Check.Check.typeOf.number("near", near);
  3251. Check.Check.typeOf.number("far", far);
  3252. Check.Check.typeOf.object("result", result);
  3253. //>>includeEnd('debug');
  3254. var column0Row0 = (2.0 * near) / (right - left);
  3255. var column1Row1 = (2.0 * near) / (top - bottom);
  3256. var column2Row0 = (right + left) / (right - left);
  3257. var column2Row1 = (top + bottom) / (top - bottom);
  3258. var column2Row2 = -(far + near) / (far - near);
  3259. var column2Row3 = -1.0;
  3260. var column3Row2 = (-2.0 * far * near) / (far - near);
  3261. result[0] = column0Row0;
  3262. result[1] = 0.0;
  3263. result[2] = 0.0;
  3264. result[3] = 0.0;
  3265. result[4] = 0.0;
  3266. result[5] = column1Row1;
  3267. result[6] = 0.0;
  3268. result[7] = 0.0;
  3269. result[8] = column2Row0;
  3270. result[9] = column2Row1;
  3271. result[10] = column2Row2;
  3272. result[11] = column2Row3;
  3273. result[12] = 0.0;
  3274. result[13] = 0.0;
  3275. result[14] = column3Row2;
  3276. result[15] = 0.0;
  3277. return result;
  3278. };
  3279. /**
  3280. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  3281. *
  3282. * @param {Number} left The number of meters to the left of the camera that will be in view.
  3283. * @param {Number} right The number of meters to the right of the camera that will be in view.
  3284. * @param {Number} bottom The number of meters below of the camera that will be in view.
  3285. * @param {Number} top The number of meters above of the camera that will be in view.
  3286. * @param {Number} near The distance to the near plane in meters.
  3287. * @param {Matrix4} result The object in which the result will be stored.
  3288. * @returns {Matrix4} The modified result parameter.
  3289. */
  3290. Matrix4.computeInfinitePerspectiveOffCenter = function (
  3291. left,
  3292. right,
  3293. bottom,
  3294. top,
  3295. near,
  3296. result
  3297. ) {
  3298. //>>includeStart('debug', pragmas.debug);
  3299. Check.Check.typeOf.number("left", left);
  3300. Check.Check.typeOf.number("right", right);
  3301. Check.Check.typeOf.number("bottom", bottom);
  3302. Check.Check.typeOf.number("top", top);
  3303. Check.Check.typeOf.number("near", near);
  3304. Check.Check.typeOf.object("result", result);
  3305. //>>includeEnd('debug');
  3306. var column0Row0 = (2.0 * near) / (right - left);
  3307. var column1Row1 = (2.0 * near) / (top - bottom);
  3308. var column2Row0 = (right + left) / (right - left);
  3309. var column2Row1 = (top + bottom) / (top - bottom);
  3310. var column2Row2 = -1.0;
  3311. var column2Row3 = -1.0;
  3312. var column3Row2 = -2.0 * near;
  3313. result[0] = column0Row0;
  3314. result[1] = 0.0;
  3315. result[2] = 0.0;
  3316. result[3] = 0.0;
  3317. result[4] = 0.0;
  3318. result[5] = column1Row1;
  3319. result[6] = 0.0;
  3320. result[7] = 0.0;
  3321. result[8] = column2Row0;
  3322. result[9] = column2Row1;
  3323. result[10] = column2Row2;
  3324. result[11] = column2Row3;
  3325. result[12] = 0.0;
  3326. result[13] = 0.0;
  3327. result[14] = column3Row2;
  3328. result[15] = 0.0;
  3329. return result;
  3330. };
  3331. /**
  3332. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  3333. *
  3334. * @param {Object} [viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  3335. * @param {Number} [nearDepthRange=0.0] The near plane distance in window coordinates.
  3336. * @param {Number} [farDepthRange=1.0] The far plane distance in window coordinates.
  3337. * @param {Matrix4} [result] The object in which the result will be stored.
  3338. * @returns {Matrix4} The modified result parameter.
  3339. *
  3340. * @example
  3341. * // Create viewport transformation using an explicit viewport and depth range.
  3342. * var m = Cesium.Matrix4.computeViewportTransformation({
  3343. * x : 0.0,
  3344. * y : 0.0,
  3345. * width : 1024.0,
  3346. * height : 768.0
  3347. * }, 0.0, 1.0, new Cesium.Matrix4());
  3348. */
  3349. Matrix4.computeViewportTransformation = function (
  3350. viewport,
  3351. nearDepthRange,
  3352. farDepthRange,
  3353. result
  3354. ) {
  3355. if (!when.defined(result)) {
  3356. result = new Matrix4();
  3357. }
  3358. viewport = when.defaultValue(viewport, when.defaultValue.EMPTY_OBJECT);
  3359. var x = when.defaultValue(viewport.x, 0.0);
  3360. var y = when.defaultValue(viewport.y, 0.0);
  3361. var width = when.defaultValue(viewport.width, 0.0);
  3362. var height = when.defaultValue(viewport.height, 0.0);
  3363. nearDepthRange = when.defaultValue(nearDepthRange, 0.0);
  3364. farDepthRange = when.defaultValue(farDepthRange, 1.0);
  3365. var halfWidth = width * 0.5;
  3366. var halfHeight = height * 0.5;
  3367. var halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  3368. var column0Row0 = halfWidth;
  3369. var column1Row1 = halfHeight;
  3370. var column2Row2 = halfDepth;
  3371. var column3Row0 = x + halfWidth;
  3372. var column3Row1 = y + halfHeight;
  3373. var column3Row2 = nearDepthRange + halfDepth;
  3374. var column3Row3 = 1.0;
  3375. result[0] = column0Row0;
  3376. result[1] = 0.0;
  3377. result[2] = 0.0;
  3378. result[3] = 0.0;
  3379. result[4] = 0.0;
  3380. result[5] = column1Row1;
  3381. result[6] = 0.0;
  3382. result[7] = 0.0;
  3383. result[8] = 0.0;
  3384. result[9] = 0.0;
  3385. result[10] = column2Row2;
  3386. result[11] = 0.0;
  3387. result[12] = column3Row0;
  3388. result[13] = column3Row1;
  3389. result[14] = column3Row2;
  3390. result[15] = column3Row3;
  3391. return result;
  3392. };
  3393. /**
  3394. * Computes a Matrix4 instance that transforms from world space to view space.
  3395. *
  3396. * @param {Cartesian3} position The position of the camera.
  3397. * @param {Cartesian3} direction The forward direction.
  3398. * @param {Cartesian3} up The up direction.
  3399. * @param {Cartesian3} right The right direction.
  3400. * @param {Matrix4} result The object in which the result will be stored.
  3401. * @returns {Matrix4} The modified result parameter.
  3402. */
  3403. Matrix4.computeView = function (position, direction, up, right, result) {
  3404. //>>includeStart('debug', pragmas.debug);
  3405. Check.Check.typeOf.object("position", position);
  3406. Check.Check.typeOf.object("direction", direction);
  3407. Check.Check.typeOf.object("up", up);
  3408. Check.Check.typeOf.object("right", right);
  3409. Check.Check.typeOf.object("result", result);
  3410. //>>includeEnd('debug');
  3411. result[0] = right.x;
  3412. result[1] = up.x;
  3413. result[2] = -direction.x;
  3414. result[3] = 0.0;
  3415. result[4] = right.y;
  3416. result[5] = up.y;
  3417. result[6] = -direction.y;
  3418. result[7] = 0.0;
  3419. result[8] = right.z;
  3420. result[9] = up.z;
  3421. result[10] = -direction.z;
  3422. result[11] = 0.0;
  3423. result[12] = -Cartesian2.Cartesian3.dot(right, position);
  3424. result[13] = -Cartesian2.Cartesian3.dot(up, position);
  3425. result[14] = Cartesian2.Cartesian3.dot(direction, position);
  3426. result[15] = 1.0;
  3427. return result;
  3428. };
  3429. /**
  3430. * Computes an Array from the provided Matrix4 instance.
  3431. * The array will be in column-major order.
  3432. *
  3433. * @param {Matrix4} matrix The matrix to use..
  3434. * @param {Number[]} [result] The Array onto which to store the result.
  3435. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  3436. *
  3437. * @example
  3438. * //create an array from an instance of Matrix4
  3439. * // m = [10.0, 14.0, 18.0, 22.0]
  3440. * // [11.0, 15.0, 19.0, 23.0]
  3441. * // [12.0, 16.0, 20.0, 24.0]
  3442. * // [13.0, 17.0, 21.0, 25.0]
  3443. * var a = Cesium.Matrix4.toArray(m);
  3444. *
  3445. * // m remains the same
  3446. * //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]
  3447. */
  3448. Matrix4.toArray = function (matrix, result) {
  3449. //>>includeStart('debug', pragmas.debug);
  3450. Check.Check.typeOf.object("matrix", matrix);
  3451. //>>includeEnd('debug');
  3452. if (!when.defined(result)) {
  3453. return [
  3454. matrix[0],
  3455. matrix[1],
  3456. matrix[2],
  3457. matrix[3],
  3458. matrix[4],
  3459. matrix[5],
  3460. matrix[6],
  3461. matrix[7],
  3462. matrix[8],
  3463. matrix[9],
  3464. matrix[10],
  3465. matrix[11],
  3466. matrix[12],
  3467. matrix[13],
  3468. matrix[14],
  3469. matrix[15],
  3470. ];
  3471. }
  3472. result[0] = matrix[0];
  3473. result[1] = matrix[1];
  3474. result[2] = matrix[2];
  3475. result[3] = matrix[3];
  3476. result[4] = matrix[4];
  3477. result[5] = matrix[5];
  3478. result[6] = matrix[6];
  3479. result[7] = matrix[7];
  3480. result[8] = matrix[8];
  3481. result[9] = matrix[9];
  3482. result[10] = matrix[10];
  3483. result[11] = matrix[11];
  3484. result[12] = matrix[12];
  3485. result[13] = matrix[13];
  3486. result[14] = matrix[14];
  3487. result[15] = matrix[15];
  3488. return result;
  3489. };
  3490. /**
  3491. * Computes the array index of the element at the provided row and column.
  3492. *
  3493. * @param {Number} row The zero-based index of the row.
  3494. * @param {Number} column The zero-based index of the column.
  3495. * @returns {Number} The index of the element at the provided row and column.
  3496. *
  3497. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  3498. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  3499. *
  3500. * @example
  3501. * var myMatrix = new Cesium.Matrix4();
  3502. * var column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  3503. * var column1Row0 = myMatrix[column1Row0Index];
  3504. * myMatrix[column1Row0Index] = 10.0;
  3505. */
  3506. Matrix4.getElementIndex = function (column, row) {
  3507. //>>includeStart('debug', pragmas.debug);
  3508. Check.Check.typeOf.number.greaterThanOrEquals("row", row, 0);
  3509. Check.Check.typeOf.number.lessThanOrEquals("row", row, 3);
  3510. Check.Check.typeOf.number.greaterThanOrEquals("column", column, 0);
  3511. Check.Check.typeOf.number.lessThanOrEquals("column", column, 3);
  3512. //>>includeEnd('debug');
  3513. return column * 4 + row;
  3514. };
  3515. /**
  3516. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  3517. *
  3518. * @param {Matrix4} matrix The matrix to use.
  3519. * @param {Number} index The zero-based index of the column to retrieve.
  3520. * @param {Cartesian4} result The object onto which to store the result.
  3521. * @returns {Cartesian4} The modified result parameter.
  3522. *
  3523. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  3524. *
  3525. * @example
  3526. * //returns a Cartesian4 instance with values from the specified column
  3527. * // m = [10.0, 11.0, 12.0, 13.0]
  3528. * // [14.0, 15.0, 16.0, 17.0]
  3529. * // [18.0, 19.0, 20.0, 21.0]
  3530. * // [22.0, 23.0, 24.0, 25.0]
  3531. *
  3532. * //Example 1: Creates an instance of Cartesian
  3533. * var a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  3534. *
  3535. * @example
  3536. * //Example 2: Sets values for Cartesian instance
  3537. * var a = new Cesium.Cartesian4();
  3538. * Cesium.Matrix4.getColumn(m, 2, a);
  3539. *
  3540. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  3541. */
  3542. Matrix4.getColumn = function (matrix, index, result) {
  3543. //>>includeStart('debug', pragmas.debug);
  3544. Check.Check.typeOf.object("matrix", matrix);
  3545. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  3546. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  3547. Check.Check.typeOf.object("result", result);
  3548. //>>includeEnd('debug');
  3549. var startIndex = index * 4;
  3550. var x = matrix[startIndex];
  3551. var y = matrix[startIndex + 1];
  3552. var z = matrix[startIndex + 2];
  3553. var w = matrix[startIndex + 3];
  3554. result.x = x;
  3555. result.y = y;
  3556. result.z = z;
  3557. result.w = w;
  3558. return result;
  3559. };
  3560. /**
  3561. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  3562. *
  3563. * @param {Matrix4} matrix The matrix to use.
  3564. * @param {Number} index The zero-based index of the column to set.
  3565. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  3566. * @param {Matrix4} result The object onto which to store the result.
  3567. * @returns {Matrix4} The modified result parameter.
  3568. *
  3569. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  3570. *
  3571. * @example
  3572. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  3573. * // m = [10.0, 11.0, 12.0, 13.0]
  3574. * // [14.0, 15.0, 16.0, 17.0]
  3575. * // [18.0, 19.0, 20.0, 21.0]
  3576. * // [22.0, 23.0, 24.0, 25.0]
  3577. *
  3578. * var a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  3579. *
  3580. * // m remains the same
  3581. * // a = [10.0, 11.0, 99.0, 13.0]
  3582. * // [14.0, 15.0, 98.0, 17.0]
  3583. * // [18.0, 19.0, 97.0, 21.0]
  3584. * // [22.0, 23.0, 96.0, 25.0]
  3585. */
  3586. Matrix4.setColumn = function (matrix, index, cartesian, result) {
  3587. //>>includeStart('debug', pragmas.debug);
  3588. Check.Check.typeOf.object("matrix", matrix);
  3589. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  3590. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  3591. Check.Check.typeOf.object("cartesian", cartesian);
  3592. Check.Check.typeOf.object("result", result);
  3593. //>>includeEnd('debug');
  3594. result = Matrix4.clone(matrix, result);
  3595. var startIndex = index * 4;
  3596. result[startIndex] = cartesian.x;
  3597. result[startIndex + 1] = cartesian.y;
  3598. result[startIndex + 2] = cartesian.z;
  3599. result[startIndex + 3] = cartesian.w;
  3600. return result;
  3601. };
  3602. /**
  3603. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  3604. * matrix with the provided translation. This assumes the matrix is an affine transformation
  3605. *
  3606. * @param {Matrix4} matrix The matrix to use.
  3607. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  3608. * @param {Matrix4} result The object onto which to store the result.
  3609. * @returns {Matrix4} The modified result parameter.
  3610. */
  3611. Matrix4.setTranslation = function (matrix, translation, result) {
  3612. //>>includeStart('debug', pragmas.debug);
  3613. Check.Check.typeOf.object("matrix", matrix);
  3614. Check.Check.typeOf.object("translation", translation);
  3615. Check.Check.typeOf.object("result", result);
  3616. //>>includeEnd('debug');
  3617. result[0] = matrix[0];
  3618. result[1] = matrix[1];
  3619. result[2] = matrix[2];
  3620. result[3] = matrix[3];
  3621. result[4] = matrix[4];
  3622. result[5] = matrix[5];
  3623. result[6] = matrix[6];
  3624. result[7] = matrix[7];
  3625. result[8] = matrix[8];
  3626. result[9] = matrix[9];
  3627. result[10] = matrix[10];
  3628. result[11] = matrix[11];
  3629. result[12] = translation.x;
  3630. result[13] = translation.y;
  3631. result[14] = translation.z;
  3632. result[15] = matrix[15];
  3633. return result;
  3634. };
  3635. var scaleScratch = new Cartesian2.Cartesian3();
  3636. /**
  3637. * Computes a new matrix that replaces the scale with the provided scale. This assumes the matrix is an affine transformation
  3638. *
  3639. * @param {Matrix4} matrix The matrix to use.
  3640. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  3641. * @param {Matrix4} result The object onto which to store the result.
  3642. * @returns {Matrix4} The modified result parameter.
  3643. */
  3644. Matrix4.setScale = function (matrix, scale, result) {
  3645. //>>includeStart('debug', pragmas.debug);
  3646. Check.Check.typeOf.object("matrix", matrix);
  3647. Check.Check.typeOf.object("scale", scale);
  3648. Check.Check.typeOf.object("result", result);
  3649. //>>includeEnd('debug');
  3650. var existingScale = Matrix4.getScale(matrix, scaleScratch);
  3651. var newScale = Cartesian2.Cartesian3.divideComponents(
  3652. scale,
  3653. existingScale,
  3654. scaleScratch
  3655. );
  3656. return Matrix4.multiplyByScale(matrix, newScale, result);
  3657. };
  3658. /**
  3659. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  3660. *
  3661. * @param {Matrix4} matrix The matrix to use.
  3662. * @param {Number} index The zero-based index of the row to retrieve.
  3663. * @param {Cartesian4} result The object onto which to store the result.
  3664. * @returns {Cartesian4} The modified result parameter.
  3665. *
  3666. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  3667. *
  3668. * @example
  3669. * //returns a Cartesian4 instance with values from the specified column
  3670. * // m = [10.0, 11.0, 12.0, 13.0]
  3671. * // [14.0, 15.0, 16.0, 17.0]
  3672. * // [18.0, 19.0, 20.0, 21.0]
  3673. * // [22.0, 23.0, 24.0, 25.0]
  3674. *
  3675. * //Example 1: Returns an instance of Cartesian
  3676. * var a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  3677. *
  3678. * @example
  3679. * //Example 2: Sets values for a Cartesian instance
  3680. * var a = new Cesium.Cartesian4();
  3681. * Cesium.Matrix4.getRow(m, 2, a);
  3682. *
  3683. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  3684. */
  3685. Matrix4.getRow = function (matrix, index, result) {
  3686. //>>includeStart('debug', pragmas.debug);
  3687. Check.Check.typeOf.object("matrix", matrix);
  3688. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  3689. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  3690. Check.Check.typeOf.object("result", result);
  3691. //>>includeEnd('debug');
  3692. var x = matrix[index];
  3693. var y = matrix[index + 4];
  3694. var z = matrix[index + 8];
  3695. var w = matrix[index + 12];
  3696. result.x = x;
  3697. result.y = y;
  3698. result.z = z;
  3699. result.w = w;
  3700. return result;
  3701. };
  3702. /**
  3703. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  3704. *
  3705. * @param {Matrix4} matrix The matrix to use.
  3706. * @param {Number} index The zero-based index of the row to set.
  3707. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  3708. * @param {Matrix4} result The object onto which to store the result.
  3709. * @returns {Matrix4} The modified result parameter.
  3710. *
  3711. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  3712. *
  3713. * @example
  3714. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  3715. * // m = [10.0, 11.0, 12.0, 13.0]
  3716. * // [14.0, 15.0, 16.0, 17.0]
  3717. * // [18.0, 19.0, 20.0, 21.0]
  3718. * // [22.0, 23.0, 24.0, 25.0]
  3719. *
  3720. * var a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  3721. *
  3722. * // m remains the same
  3723. * // a = [10.0, 11.0, 12.0, 13.0]
  3724. * // [14.0, 15.0, 16.0, 17.0]
  3725. * // [99.0, 98.0, 97.0, 96.0]
  3726. * // [22.0, 23.0, 24.0, 25.0]
  3727. */
  3728. Matrix4.setRow = function (matrix, index, cartesian, result) {
  3729. //>>includeStart('debug', pragmas.debug);
  3730. Check.Check.typeOf.object("matrix", matrix);
  3731. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  3732. Check.Check.typeOf.number.lessThanOrEquals("index", index, 3);
  3733. Check.Check.typeOf.object("cartesian", cartesian);
  3734. Check.Check.typeOf.object("result", result);
  3735. //>>includeEnd('debug');
  3736. result = Matrix4.clone(matrix, result);
  3737. result[index] = cartesian.x;
  3738. result[index + 4] = cartesian.y;
  3739. result[index + 8] = cartesian.z;
  3740. result[index + 12] = cartesian.w;
  3741. return result;
  3742. };
  3743. var scratchColumn$1 = new Cartesian2.Cartesian3();
  3744. /**
  3745. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  3746. *
  3747. * @param {Matrix4} matrix The matrix.
  3748. * @param {Cartesian3} result The object onto which to store the result.
  3749. * @returns {Cartesian3} The modified result parameter
  3750. */
  3751. Matrix4.getScale = function (matrix, result) {
  3752. //>>includeStart('debug', pragmas.debug);
  3753. Check.Check.typeOf.object("matrix", matrix);
  3754. Check.Check.typeOf.object("result", result);
  3755. //>>includeEnd('debug');
  3756. result.x = Cartesian2.Cartesian3.magnitude(
  3757. Cartesian2.Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$1)
  3758. );
  3759. result.y = Cartesian2.Cartesian3.magnitude(
  3760. Cartesian2.Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn$1)
  3761. );
  3762. result.z = Cartesian2.Cartesian3.magnitude(
  3763. Cartesian2.Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn$1)
  3764. );
  3765. return result;
  3766. };
  3767. var scratchScale$1 = new Cartesian2.Cartesian3();
  3768. /**
  3769. * Computes the maximum scale assuming the matrix is an affine transformation.
  3770. * The maximum scale is the maximum length of the column vectors in the upper-left
  3771. * 3x3 matrix.
  3772. *
  3773. * @param {Matrix4} matrix The matrix.
  3774. * @returns {Number} The maximum scale.
  3775. */
  3776. Matrix4.getMaximumScale = function (matrix) {
  3777. Matrix4.getScale(matrix, scratchScale$1);
  3778. return Cartesian2.Cartesian3.maximumComponent(scratchScale$1);
  3779. };
  3780. /**
  3781. * Computes the product of two matrices.
  3782. *
  3783. * @param {Matrix4} left The first matrix.
  3784. * @param {Matrix4} right The second matrix.
  3785. * @param {Matrix4} result The object onto which to store the result.
  3786. * @returns {Matrix4} The modified result parameter.
  3787. */
  3788. Matrix4.multiply = function (left, right, result) {
  3789. //>>includeStart('debug', pragmas.debug);
  3790. Check.Check.typeOf.object("left", left);
  3791. Check.Check.typeOf.object("right", right);
  3792. Check.Check.typeOf.object("result", result);
  3793. //>>includeEnd('debug');
  3794. var left0 = left[0];
  3795. var left1 = left[1];
  3796. var left2 = left[2];
  3797. var left3 = left[3];
  3798. var left4 = left[4];
  3799. var left5 = left[5];
  3800. var left6 = left[6];
  3801. var left7 = left[7];
  3802. var left8 = left[8];
  3803. var left9 = left[9];
  3804. var left10 = left[10];
  3805. var left11 = left[11];
  3806. var left12 = left[12];
  3807. var left13 = left[13];
  3808. var left14 = left[14];
  3809. var left15 = left[15];
  3810. var right0 = right[0];
  3811. var right1 = right[1];
  3812. var right2 = right[2];
  3813. var right3 = right[3];
  3814. var right4 = right[4];
  3815. var right5 = right[5];
  3816. var right6 = right[6];
  3817. var right7 = right[7];
  3818. var right8 = right[8];
  3819. var right9 = right[9];
  3820. var right10 = right[10];
  3821. var right11 = right[11];
  3822. var right12 = right[12];
  3823. var right13 = right[13];
  3824. var right14 = right[14];
  3825. var right15 = right[15];
  3826. var column0Row0 =
  3827. left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  3828. var column0Row1 =
  3829. left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  3830. var column0Row2 =
  3831. left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  3832. var column0Row3 =
  3833. left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  3834. var column1Row0 =
  3835. left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  3836. var column1Row1 =
  3837. left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  3838. var column1Row2 =
  3839. left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  3840. var column1Row3 =
  3841. left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  3842. var column2Row0 =
  3843. left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  3844. var column2Row1 =
  3845. left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  3846. var column2Row2 =
  3847. left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  3848. var column2Row3 =
  3849. left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  3850. var column3Row0 =
  3851. left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  3852. var column3Row1 =
  3853. left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  3854. var column3Row2 =
  3855. left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  3856. var column3Row3 =
  3857. left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  3858. result[0] = column0Row0;
  3859. result[1] = column0Row1;
  3860. result[2] = column0Row2;
  3861. result[3] = column0Row3;
  3862. result[4] = column1Row0;
  3863. result[5] = column1Row1;
  3864. result[6] = column1Row2;
  3865. result[7] = column1Row3;
  3866. result[8] = column2Row0;
  3867. result[9] = column2Row1;
  3868. result[10] = column2Row2;
  3869. result[11] = column2Row3;
  3870. result[12] = column3Row0;
  3871. result[13] = column3Row1;
  3872. result[14] = column3Row2;
  3873. result[15] = column3Row3;
  3874. return result;
  3875. };
  3876. /**
  3877. * Computes the sum of two matrices.
  3878. *
  3879. * @param {Matrix4} left The first matrix.
  3880. * @param {Matrix4} right The second matrix.
  3881. * @param {Matrix4} result The object onto which to store the result.
  3882. * @returns {Matrix4} The modified result parameter.
  3883. */
  3884. Matrix4.add = function (left, right, result) {
  3885. //>>includeStart('debug', pragmas.debug);
  3886. Check.Check.typeOf.object("left", left);
  3887. Check.Check.typeOf.object("right", right);
  3888. Check.Check.typeOf.object("result", result);
  3889. //>>includeEnd('debug');
  3890. result[0] = left[0] + right[0];
  3891. result[1] = left[1] + right[1];
  3892. result[2] = left[2] + right[2];
  3893. result[3] = left[3] + right[3];
  3894. result[4] = left[4] + right[4];
  3895. result[5] = left[5] + right[5];
  3896. result[6] = left[6] + right[6];
  3897. result[7] = left[7] + right[7];
  3898. result[8] = left[8] + right[8];
  3899. result[9] = left[9] + right[9];
  3900. result[10] = left[10] + right[10];
  3901. result[11] = left[11] + right[11];
  3902. result[12] = left[12] + right[12];
  3903. result[13] = left[13] + right[13];
  3904. result[14] = left[14] + right[14];
  3905. result[15] = left[15] + right[15];
  3906. return result;
  3907. };
  3908. /**
  3909. * Computes the difference of two matrices.
  3910. *
  3911. * @param {Matrix4} left The first matrix.
  3912. * @param {Matrix4} right The second matrix.
  3913. * @param {Matrix4} result The object onto which to store the result.
  3914. * @returns {Matrix4} The modified result parameter.
  3915. */
  3916. Matrix4.subtract = function (left, right, result) {
  3917. //>>includeStart('debug', pragmas.debug);
  3918. Check.Check.typeOf.object("left", left);
  3919. Check.Check.typeOf.object("right", right);
  3920. Check.Check.typeOf.object("result", result);
  3921. //>>includeEnd('debug');
  3922. result[0] = left[0] - right[0];
  3923. result[1] = left[1] - right[1];
  3924. result[2] = left[2] - right[2];
  3925. result[3] = left[3] - right[3];
  3926. result[4] = left[4] - right[4];
  3927. result[5] = left[5] - right[5];
  3928. result[6] = left[6] - right[6];
  3929. result[7] = left[7] - right[7];
  3930. result[8] = left[8] - right[8];
  3931. result[9] = left[9] - right[9];
  3932. result[10] = left[10] - right[10];
  3933. result[11] = left[11] - right[11];
  3934. result[12] = left[12] - right[12];
  3935. result[13] = left[13] - right[13];
  3936. result[14] = left[14] - right[14];
  3937. result[15] = left[15] - right[15];
  3938. return result;
  3939. };
  3940. /**
  3941. * Computes the product of two matrices assuming the matrices are
  3942. * affine transformation matrices, where the upper left 3x3 elements
  3943. * are a rotation matrix, and the upper three elements in the fourth
  3944. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  3945. * The matrix is not verified to be in the proper form.
  3946. * This method is faster than computing the product for general 4x4
  3947. * matrices using {@link Matrix4.multiply}.
  3948. *
  3949. * @param {Matrix4} left The first matrix.
  3950. * @param {Matrix4} right The second matrix.
  3951. * @param {Matrix4} result The object onto which to store the result.
  3952. * @returns {Matrix4} The modified result parameter.
  3953. *
  3954. * @example
  3955. * var 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);
  3956. * var m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  3957. * var m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  3958. */
  3959. Matrix4.multiplyTransformation = function (left, right, result) {
  3960. //>>includeStart('debug', pragmas.debug);
  3961. Check.Check.typeOf.object("left", left);
  3962. Check.Check.typeOf.object("right", right);
  3963. Check.Check.typeOf.object("result", result);
  3964. //>>includeEnd('debug');
  3965. var left0 = left[0];
  3966. var left1 = left[1];
  3967. var left2 = left[2];
  3968. var left4 = left[4];
  3969. var left5 = left[5];
  3970. var left6 = left[6];
  3971. var left8 = left[8];
  3972. var left9 = left[9];
  3973. var left10 = left[10];
  3974. var left12 = left[12];
  3975. var left13 = left[13];
  3976. var left14 = left[14];
  3977. var right0 = right[0];
  3978. var right1 = right[1];
  3979. var right2 = right[2];
  3980. var right4 = right[4];
  3981. var right5 = right[5];
  3982. var right6 = right[6];
  3983. var right8 = right[8];
  3984. var right9 = right[9];
  3985. var right10 = right[10];
  3986. var right12 = right[12];
  3987. var right13 = right[13];
  3988. var right14 = right[14];
  3989. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  3990. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  3991. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  3992. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  3993. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  3994. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  3995. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  3996. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  3997. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  3998. var column3Row0 =
  3999. left0 * right12 + left4 * right13 + left8 * right14 + left12;
  4000. var column3Row1 =
  4001. left1 * right12 + left5 * right13 + left9 * right14 + left13;
  4002. var column3Row2 =
  4003. left2 * right12 + left6 * right13 + left10 * right14 + left14;
  4004. result[0] = column0Row0;
  4005. result[1] = column0Row1;
  4006. result[2] = column0Row2;
  4007. result[3] = 0.0;
  4008. result[4] = column1Row0;
  4009. result[5] = column1Row1;
  4010. result[6] = column1Row2;
  4011. result[7] = 0.0;
  4012. result[8] = column2Row0;
  4013. result[9] = column2Row1;
  4014. result[10] = column2Row2;
  4015. result[11] = 0.0;
  4016. result[12] = column3Row0;
  4017. result[13] = column3Row1;
  4018. result[14] = column3Row2;
  4019. result[15] = 1.0;
  4020. return result;
  4021. };
  4022. /**
  4023. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  4024. * by a 3x3 rotation matrix. This is an optimization
  4025. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  4026. *
  4027. * @param {Matrix4} matrix The matrix on the left-hand side.
  4028. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  4029. * @param {Matrix4} result The object onto which to store the result.
  4030. * @returns {Matrix4} The modified result parameter.
  4031. *
  4032. * @example
  4033. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  4034. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  4035. */
  4036. Matrix4.multiplyByMatrix3 = function (matrix, rotation, result) {
  4037. //>>includeStart('debug', pragmas.debug);
  4038. Check.Check.typeOf.object("matrix", matrix);
  4039. Check.Check.typeOf.object("rotation", rotation);
  4040. Check.Check.typeOf.object("result", result);
  4041. //>>includeEnd('debug');
  4042. var left0 = matrix[0];
  4043. var left1 = matrix[1];
  4044. var left2 = matrix[2];
  4045. var left4 = matrix[4];
  4046. var left5 = matrix[5];
  4047. var left6 = matrix[6];
  4048. var left8 = matrix[8];
  4049. var left9 = matrix[9];
  4050. var left10 = matrix[10];
  4051. var right0 = rotation[0];
  4052. var right1 = rotation[1];
  4053. var right2 = rotation[2];
  4054. var right4 = rotation[3];
  4055. var right5 = rotation[4];
  4056. var right6 = rotation[5];
  4057. var right8 = rotation[6];
  4058. var right9 = rotation[7];
  4059. var right10 = rotation[8];
  4060. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  4061. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  4062. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  4063. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  4064. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  4065. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  4066. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  4067. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  4068. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  4069. result[0] = column0Row0;
  4070. result[1] = column0Row1;
  4071. result[2] = column0Row2;
  4072. result[3] = 0.0;
  4073. result[4] = column1Row0;
  4074. result[5] = column1Row1;
  4075. result[6] = column1Row2;
  4076. result[7] = 0.0;
  4077. result[8] = column2Row0;
  4078. result[9] = column2Row1;
  4079. result[10] = column2Row2;
  4080. result[11] = 0.0;
  4081. result[12] = matrix[12];
  4082. result[13] = matrix[13];
  4083. result[14] = matrix[14];
  4084. result[15] = matrix[15];
  4085. return result;
  4086. };
  4087. /**
  4088. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  4089. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  4090. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  4091. *
  4092. * @param {Matrix4} matrix The matrix on the left-hand side.
  4093. * @param {Cartesian3} translation The translation on the right-hand side.
  4094. * @param {Matrix4} result The object onto which to store the result.
  4095. * @returns {Matrix4} The modified result parameter.
  4096. *
  4097. * @example
  4098. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  4099. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  4100. */
  4101. Matrix4.multiplyByTranslation = function (matrix, translation, result) {
  4102. //>>includeStart('debug', pragmas.debug);
  4103. Check.Check.typeOf.object("matrix", matrix);
  4104. Check.Check.typeOf.object("translation", translation);
  4105. Check.Check.typeOf.object("result", result);
  4106. //>>includeEnd('debug');
  4107. var x = translation.x;
  4108. var y = translation.y;
  4109. var z = translation.z;
  4110. var tx = x * matrix[0] + y * matrix[4] + z * matrix[8] + matrix[12];
  4111. var ty = x * matrix[1] + y * matrix[5] + z * matrix[9] + matrix[13];
  4112. var tz = x * matrix[2] + y * matrix[6] + z * matrix[10] + matrix[14];
  4113. result[0] = matrix[0];
  4114. result[1] = matrix[1];
  4115. result[2] = matrix[2];
  4116. result[3] = matrix[3];
  4117. result[4] = matrix[4];
  4118. result[5] = matrix[5];
  4119. result[6] = matrix[6];
  4120. result[7] = matrix[7];
  4121. result[8] = matrix[8];
  4122. result[9] = matrix[9];
  4123. result[10] = matrix[10];
  4124. result[11] = matrix[11];
  4125. result[12] = tx;
  4126. result[13] = ty;
  4127. result[14] = tz;
  4128. result[15] = matrix[15];
  4129. return result;
  4130. };
  4131. var uniformScaleScratch = new Cartesian2.Cartesian3();
  4132. /**
  4133. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  4134. * by an implicit uniform scale matrix. This is an optimization
  4135. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  4136. * <code>m</code> must be an affine matrix.
  4137. * This function performs fewer allocations and arithmetic operations.
  4138. *
  4139. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  4140. * @param {Number} scale The uniform scale on the right-hand side.
  4141. * @param {Matrix4} result The object onto which to store the result.
  4142. * @returns {Matrix4} The modified result parameter.
  4143. *
  4144. *
  4145. * @example
  4146. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  4147. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  4148. *
  4149. * @see Matrix4.fromUniformScale
  4150. * @see Matrix4.multiplyByScale
  4151. */
  4152. Matrix4.multiplyByUniformScale = function (matrix, scale, result) {
  4153. //>>includeStart('debug', pragmas.debug);
  4154. Check.Check.typeOf.object("matrix", matrix);
  4155. Check.Check.typeOf.number("scale", scale);
  4156. Check.Check.typeOf.object("result", result);
  4157. //>>includeEnd('debug');
  4158. uniformScaleScratch.x = scale;
  4159. uniformScaleScratch.y = scale;
  4160. uniformScaleScratch.z = scale;
  4161. return Matrix4.multiplyByScale(matrix, uniformScaleScratch, result);
  4162. };
  4163. /**
  4164. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  4165. * by an implicit non-uniform scale matrix. This is an optimization
  4166. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  4167. * <code>m</code> must be an affine matrix.
  4168. * This function performs fewer allocations and arithmetic operations.
  4169. *
  4170. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  4171. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  4172. * @param {Matrix4} result The object onto which to store the result.
  4173. * @returns {Matrix4} The modified result parameter.
  4174. *
  4175. *
  4176. * @example
  4177. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  4178. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  4179. *
  4180. * @see Matrix4.fromScale
  4181. * @see Matrix4.multiplyByUniformScale
  4182. */
  4183. Matrix4.multiplyByScale = function (matrix, scale, result) {
  4184. //>>includeStart('debug', pragmas.debug);
  4185. Check.Check.typeOf.object("matrix", matrix);
  4186. Check.Check.typeOf.object("scale", scale);
  4187. Check.Check.typeOf.object("result", result);
  4188. //>>includeEnd('debug');
  4189. var scaleX = scale.x;
  4190. var scaleY = scale.y;
  4191. var scaleZ = scale.z;
  4192. // Faster than Cartesian3.equals
  4193. if (scaleX === 1.0 && scaleY === 1.0 && scaleZ === 1.0) {
  4194. return Matrix4.clone(matrix, result);
  4195. }
  4196. result[0] = scaleX * matrix[0];
  4197. result[1] = scaleX * matrix[1];
  4198. result[2] = scaleX * matrix[2];
  4199. result[3] = 0.0;
  4200. result[4] = scaleY * matrix[4];
  4201. result[5] = scaleY * matrix[5];
  4202. result[6] = scaleY * matrix[6];
  4203. result[7] = 0.0;
  4204. result[8] = scaleZ * matrix[8];
  4205. result[9] = scaleZ * matrix[9];
  4206. result[10] = scaleZ * matrix[10];
  4207. result[11] = 0.0;
  4208. result[12] = matrix[12];
  4209. result[13] = matrix[13];
  4210. result[14] = matrix[14];
  4211. result[15] = 1.0;
  4212. return result;
  4213. };
  4214. /**
  4215. * Computes the product of a matrix and a column vector.
  4216. *
  4217. * @param {Matrix4} matrix The matrix.
  4218. * @param {Cartesian4} cartesian The vector.
  4219. * @param {Cartesian4} result The object onto which to store the result.
  4220. * @returns {Cartesian4} The modified result parameter.
  4221. */
  4222. Matrix4.multiplyByVector = function (matrix, cartesian, result) {
  4223. //>>includeStart('debug', pragmas.debug);
  4224. Check.Check.typeOf.object("matrix", matrix);
  4225. Check.Check.typeOf.object("cartesian", cartesian);
  4226. Check.Check.typeOf.object("result", result);
  4227. //>>includeEnd('debug');
  4228. var vX = cartesian.x;
  4229. var vY = cartesian.y;
  4230. var vZ = cartesian.z;
  4231. var vW = cartesian.w;
  4232. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  4233. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  4234. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  4235. var w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  4236. result.x = x;
  4237. result.y = y;
  4238. result.z = z;
  4239. result.w = w;
  4240. return result;
  4241. };
  4242. /**
  4243. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  4244. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  4245. *
  4246. * @param {Matrix4} matrix The matrix.
  4247. * @param {Cartesian3} cartesian The point.
  4248. * @param {Cartesian3} result The object onto which to store the result.
  4249. * @returns {Cartesian3} The modified result parameter.
  4250. *
  4251. * @example
  4252. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  4253. * var result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  4254. * // A shortcut for
  4255. * // Cartesian3 p = ...
  4256. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  4257. */
  4258. Matrix4.multiplyByPointAsVector = function (matrix, cartesian, result) {
  4259. //>>includeStart('debug', pragmas.debug);
  4260. Check.Check.typeOf.object("matrix", matrix);
  4261. Check.Check.typeOf.object("cartesian", cartesian);
  4262. Check.Check.typeOf.object("result", result);
  4263. //>>includeEnd('debug');
  4264. var vX = cartesian.x;
  4265. var vY = cartesian.y;
  4266. var vZ = cartesian.z;
  4267. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  4268. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  4269. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  4270. result.x = x;
  4271. result.y = y;
  4272. result.z = z;
  4273. return result;
  4274. };
  4275. /**
  4276. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  4277. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  4278. *
  4279. * @param {Matrix4} matrix The matrix.
  4280. * @param {Cartesian3} cartesian The point.
  4281. * @param {Cartesian3} result The object onto which to store the result.
  4282. * @returns {Cartesian3} The modified result parameter.
  4283. *
  4284. * @example
  4285. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  4286. * var result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  4287. */
  4288. Matrix4.multiplyByPoint = function (matrix, cartesian, result) {
  4289. //>>includeStart('debug', pragmas.debug);
  4290. Check.Check.typeOf.object("matrix", matrix);
  4291. Check.Check.typeOf.object("cartesian", cartesian);
  4292. Check.Check.typeOf.object("result", result);
  4293. //>>includeEnd('debug');
  4294. var vX = cartesian.x;
  4295. var vY = cartesian.y;
  4296. var vZ = cartesian.z;
  4297. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  4298. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  4299. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  4300. result.x = x;
  4301. result.y = y;
  4302. result.z = z;
  4303. return result;
  4304. };
  4305. /**
  4306. * Computes the product of a matrix and a scalar.
  4307. *
  4308. * @param {Matrix4} matrix The matrix.
  4309. * @param {Number} scalar The number to multiply by.
  4310. * @param {Matrix4} result The object onto which to store the result.
  4311. * @returns {Matrix4} The modified result parameter.
  4312. *
  4313. * @example
  4314. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  4315. * // m = [10.0, 11.0, 12.0, 13.0]
  4316. * // [14.0, 15.0, 16.0, 17.0]
  4317. * // [18.0, 19.0, 20.0, 21.0]
  4318. * // [22.0, 23.0, 24.0, 25.0]
  4319. *
  4320. * var a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  4321. *
  4322. * // m remains the same
  4323. * // a = [-20.0, -22.0, -24.0, -26.0]
  4324. * // [-28.0, -30.0, -32.0, -34.0]
  4325. * // [-36.0, -38.0, -40.0, -42.0]
  4326. * // [-44.0, -46.0, -48.0, -50.0]
  4327. */
  4328. Matrix4.multiplyByScalar = function (matrix, scalar, result) {
  4329. //>>includeStart('debug', pragmas.debug);
  4330. Check.Check.typeOf.object("matrix", matrix);
  4331. Check.Check.typeOf.number("scalar", scalar);
  4332. Check.Check.typeOf.object("result", result);
  4333. //>>includeEnd('debug');
  4334. result[0] = matrix[0] * scalar;
  4335. result[1] = matrix[1] * scalar;
  4336. result[2] = matrix[2] * scalar;
  4337. result[3] = matrix[3] * scalar;
  4338. result[4] = matrix[4] * scalar;
  4339. result[5] = matrix[5] * scalar;
  4340. result[6] = matrix[6] * scalar;
  4341. result[7] = matrix[7] * scalar;
  4342. result[8] = matrix[8] * scalar;
  4343. result[9] = matrix[9] * scalar;
  4344. result[10] = matrix[10] * scalar;
  4345. result[11] = matrix[11] * scalar;
  4346. result[12] = matrix[12] * scalar;
  4347. result[13] = matrix[13] * scalar;
  4348. result[14] = matrix[14] * scalar;
  4349. result[15] = matrix[15] * scalar;
  4350. return result;
  4351. };
  4352. /**
  4353. * Computes a negated copy of the provided matrix.
  4354. *
  4355. * @param {Matrix4} matrix The matrix to negate.
  4356. * @param {Matrix4} result The object onto which to store the result.
  4357. * @returns {Matrix4} The modified result parameter.
  4358. *
  4359. * @example
  4360. * //create a new Matrix4 instance which is a negation of a Matrix4
  4361. * // m = [10.0, 11.0, 12.0, 13.0]
  4362. * // [14.0, 15.0, 16.0, 17.0]
  4363. * // [18.0, 19.0, 20.0, 21.0]
  4364. * // [22.0, 23.0, 24.0, 25.0]
  4365. *
  4366. * var a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  4367. *
  4368. * // m remains the same
  4369. * // a = [-10.0, -11.0, -12.0, -13.0]
  4370. * // [-14.0, -15.0, -16.0, -17.0]
  4371. * // [-18.0, -19.0, -20.0, -21.0]
  4372. * // [-22.0, -23.0, -24.0, -25.0]
  4373. */
  4374. Matrix4.negate = function (matrix, result) {
  4375. //>>includeStart('debug', pragmas.debug);
  4376. Check.Check.typeOf.object("matrix", matrix);
  4377. Check.Check.typeOf.object("result", result);
  4378. //>>includeEnd('debug');
  4379. result[0] = -matrix[0];
  4380. result[1] = -matrix[1];
  4381. result[2] = -matrix[2];
  4382. result[3] = -matrix[3];
  4383. result[4] = -matrix[4];
  4384. result[5] = -matrix[5];
  4385. result[6] = -matrix[6];
  4386. result[7] = -matrix[7];
  4387. result[8] = -matrix[8];
  4388. result[9] = -matrix[9];
  4389. result[10] = -matrix[10];
  4390. result[11] = -matrix[11];
  4391. result[12] = -matrix[12];
  4392. result[13] = -matrix[13];
  4393. result[14] = -matrix[14];
  4394. result[15] = -matrix[15];
  4395. return result;
  4396. };
  4397. /**
  4398. * Computes the transpose of the provided matrix.
  4399. *
  4400. * @param {Matrix4} matrix The matrix to transpose.
  4401. * @param {Matrix4} result The object onto which to store the result.
  4402. * @returns {Matrix4} The modified result parameter.
  4403. *
  4404. * @example
  4405. * //returns transpose of a Matrix4
  4406. * // m = [10.0, 11.0, 12.0, 13.0]
  4407. * // [14.0, 15.0, 16.0, 17.0]
  4408. * // [18.0, 19.0, 20.0, 21.0]
  4409. * // [22.0, 23.0, 24.0, 25.0]
  4410. *
  4411. * var a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  4412. *
  4413. * // m remains the same
  4414. * // a = [10.0, 14.0, 18.0, 22.0]
  4415. * // [11.0, 15.0, 19.0, 23.0]
  4416. * // [12.0, 16.0, 20.0, 24.0]
  4417. * // [13.0, 17.0, 21.0, 25.0]
  4418. */
  4419. Matrix4.transpose = function (matrix, result) {
  4420. //>>includeStart('debug', pragmas.debug);
  4421. Check.Check.typeOf.object("matrix", matrix);
  4422. Check.Check.typeOf.object("result", result);
  4423. //>>includeEnd('debug');
  4424. var matrix1 = matrix[1];
  4425. var matrix2 = matrix[2];
  4426. var matrix3 = matrix[3];
  4427. var matrix6 = matrix[6];
  4428. var matrix7 = matrix[7];
  4429. var matrix11 = matrix[11];
  4430. result[0] = matrix[0];
  4431. result[1] = matrix[4];
  4432. result[2] = matrix[8];
  4433. result[3] = matrix[12];
  4434. result[4] = matrix1;
  4435. result[5] = matrix[5];
  4436. result[6] = matrix[9];
  4437. result[7] = matrix[13];
  4438. result[8] = matrix2;
  4439. result[9] = matrix6;
  4440. result[10] = matrix[10];
  4441. result[11] = matrix[14];
  4442. result[12] = matrix3;
  4443. result[13] = matrix7;
  4444. result[14] = matrix11;
  4445. result[15] = matrix[15];
  4446. return result;
  4447. };
  4448. /**
  4449. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  4450. *
  4451. * @param {Matrix4} matrix The matrix with signed elements.
  4452. * @param {Matrix4} result The object onto which to store the result.
  4453. * @returns {Matrix4} The modified result parameter.
  4454. */
  4455. Matrix4.abs = function (matrix, result) {
  4456. //>>includeStart('debug', pragmas.debug);
  4457. Check.Check.typeOf.object("matrix", matrix);
  4458. Check.Check.typeOf.object("result", result);
  4459. //>>includeEnd('debug');
  4460. result[0] = Math.abs(matrix[0]);
  4461. result[1] = Math.abs(matrix[1]);
  4462. result[2] = Math.abs(matrix[2]);
  4463. result[3] = Math.abs(matrix[3]);
  4464. result[4] = Math.abs(matrix[4]);
  4465. result[5] = Math.abs(matrix[5]);
  4466. result[6] = Math.abs(matrix[6]);
  4467. result[7] = Math.abs(matrix[7]);
  4468. result[8] = Math.abs(matrix[8]);
  4469. result[9] = Math.abs(matrix[9]);
  4470. result[10] = Math.abs(matrix[10]);
  4471. result[11] = Math.abs(matrix[11]);
  4472. result[12] = Math.abs(matrix[12]);
  4473. result[13] = Math.abs(matrix[13]);
  4474. result[14] = Math.abs(matrix[14]);
  4475. result[15] = Math.abs(matrix[15]);
  4476. return result;
  4477. };
  4478. /**
  4479. * Compares the provided matrices componentwise and returns
  4480. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4481. *
  4482. * @param {Matrix4} [left] The first matrix.
  4483. * @param {Matrix4} [right] The second matrix.
  4484. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  4485. *
  4486. * @example
  4487. * //compares two Matrix4 instances
  4488. *
  4489. * // a = [10.0, 14.0, 18.0, 22.0]
  4490. * // [11.0, 15.0, 19.0, 23.0]
  4491. * // [12.0, 16.0, 20.0, 24.0]
  4492. * // [13.0, 17.0, 21.0, 25.0]
  4493. *
  4494. * // b = [10.0, 14.0, 18.0, 22.0]
  4495. * // [11.0, 15.0, 19.0, 23.0]
  4496. * // [12.0, 16.0, 20.0, 24.0]
  4497. * // [13.0, 17.0, 21.0, 25.0]
  4498. *
  4499. * if(Cesium.Matrix4.equals(a,b)) {
  4500. * console.log("Both matrices are equal");
  4501. * } else {
  4502. * console.log("They are not equal");
  4503. * }
  4504. *
  4505. * //Prints "Both matrices are equal" on the console
  4506. */
  4507. Matrix4.equals = function (left, right) {
  4508. // Given that most matrices will be transformation matrices, the elements
  4509. // are tested in order such that the test is likely to fail as early
  4510. // as possible. I _think_ this is just as friendly to the L1 cache
  4511. // as testing in index order. It is certainty faster in practice.
  4512. return (
  4513. left === right ||
  4514. (when.defined(left) &&
  4515. when.defined(right) &&
  4516. // Translation
  4517. left[12] === right[12] &&
  4518. left[13] === right[13] &&
  4519. left[14] === right[14] &&
  4520. // Rotation/scale
  4521. left[0] === right[0] &&
  4522. left[1] === right[1] &&
  4523. left[2] === right[2] &&
  4524. left[4] === right[4] &&
  4525. left[5] === right[5] &&
  4526. left[6] === right[6] &&
  4527. left[8] === right[8] &&
  4528. left[9] === right[9] &&
  4529. left[10] === right[10] &&
  4530. // Bottom row
  4531. left[3] === right[3] &&
  4532. left[7] === right[7] &&
  4533. left[11] === right[11] &&
  4534. left[15] === right[15])
  4535. );
  4536. };
  4537. /**
  4538. * Compares the provided matrices componentwise and returns
  4539. * <code>true</code> if they are within the provided epsilon,
  4540. * <code>false</code> otherwise.
  4541. *
  4542. * @param {Matrix4} [left] The first matrix.
  4543. * @param {Matrix4} [right] The second matrix.
  4544. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  4545. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  4546. *
  4547. * @example
  4548. * //compares two Matrix4 instances
  4549. *
  4550. * // a = [10.5, 14.5, 18.5, 22.5]
  4551. * // [11.5, 15.5, 19.5, 23.5]
  4552. * // [12.5, 16.5, 20.5, 24.5]
  4553. * // [13.5, 17.5, 21.5, 25.5]
  4554. *
  4555. * // b = [10.0, 14.0, 18.0, 22.0]
  4556. * // [11.0, 15.0, 19.0, 23.0]
  4557. * // [12.0, 16.0, 20.0, 24.0]
  4558. * // [13.0, 17.0, 21.0, 25.0]
  4559. *
  4560. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  4561. * console.log("Difference between both the matrices is less than 0.1");
  4562. * } else {
  4563. * console.log("Difference between both the matrices is not less than 0.1");
  4564. * }
  4565. *
  4566. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  4567. */
  4568. Matrix4.equalsEpsilon = function (left, right, epsilon) {
  4569. epsilon = when.defaultValue(epsilon, 0);
  4570. return (
  4571. left === right ||
  4572. (when.defined(left) &&
  4573. when.defined(right) &&
  4574. Math.abs(left[0] - right[0]) <= epsilon &&
  4575. Math.abs(left[1] - right[1]) <= epsilon &&
  4576. Math.abs(left[2] - right[2]) <= epsilon &&
  4577. Math.abs(left[3] - right[3]) <= epsilon &&
  4578. Math.abs(left[4] - right[4]) <= epsilon &&
  4579. Math.abs(left[5] - right[5]) <= epsilon &&
  4580. Math.abs(left[6] - right[6]) <= epsilon &&
  4581. Math.abs(left[7] - right[7]) <= epsilon &&
  4582. Math.abs(left[8] - right[8]) <= epsilon &&
  4583. Math.abs(left[9] - right[9]) <= epsilon &&
  4584. Math.abs(left[10] - right[10]) <= epsilon &&
  4585. Math.abs(left[11] - right[11]) <= epsilon &&
  4586. Math.abs(left[12] - right[12]) <= epsilon &&
  4587. Math.abs(left[13] - right[13]) <= epsilon &&
  4588. Math.abs(left[14] - right[14]) <= epsilon &&
  4589. Math.abs(left[15] - right[15]) <= epsilon)
  4590. );
  4591. };
  4592. /**
  4593. * Gets the translation portion of the provided matrix, assuming the matrix is a affine transformation matrix.
  4594. *
  4595. * @param {Matrix4} matrix The matrix to use.
  4596. * @param {Cartesian3} result The object onto which to store the result.
  4597. * @returns {Cartesian3} The modified result parameter.
  4598. */
  4599. Matrix4.getTranslation = function (matrix, result) {
  4600. //>>includeStart('debug', pragmas.debug);
  4601. Check.Check.typeOf.object("matrix", matrix);
  4602. Check.Check.typeOf.object("result", result);
  4603. //>>includeEnd('debug');
  4604. result.x = matrix[12];
  4605. result.y = matrix[13];
  4606. result.z = matrix[14];
  4607. return result;
  4608. };
  4609. /**
  4610. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is an affine transformation matrix.
  4611. *
  4612. * @param {Matrix4} matrix The matrix to use.
  4613. * @param {Matrix3} result The object onto which to store the result.
  4614. * @returns {Matrix3} The modified result parameter.
  4615. *
  4616. * @example
  4617. * // returns a Matrix3 instance from a Matrix4 instance
  4618. *
  4619. * // m = [10.0, 14.0, 18.0, 22.0]
  4620. * // [11.0, 15.0, 19.0, 23.0]
  4621. * // [12.0, 16.0, 20.0, 24.0]
  4622. * // [13.0, 17.0, 21.0, 25.0]
  4623. *
  4624. * var b = new Cesium.Matrix3();
  4625. * Cesium.Matrix4.getMatrix3(m,b);
  4626. *
  4627. * // b = [10.0, 14.0, 18.0]
  4628. * // [11.0, 15.0, 19.0]
  4629. * // [12.0, 16.0, 20.0]
  4630. */
  4631. Matrix4.getMatrix3 = function (matrix, result) {
  4632. //>>includeStart('debug', pragmas.debug);
  4633. Check.Check.typeOf.object("matrix", matrix);
  4634. Check.Check.typeOf.object("result", result);
  4635. //>>includeEnd('debug');
  4636. result[0] = matrix[0];
  4637. result[1] = matrix[1];
  4638. result[2] = matrix[2];
  4639. result[3] = matrix[4];
  4640. result[4] = matrix[5];
  4641. result[5] = matrix[6];
  4642. result[6] = matrix[8];
  4643. result[7] = matrix[9];
  4644. result[8] = matrix[10];
  4645. return result;
  4646. };
  4647. var scratchInverseRotation = new Matrix3();
  4648. var scratchMatrix3Zero = new Matrix3();
  4649. var scratchBottomRow = new Cartesian4();
  4650. var scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  4651. /**
  4652. * Computes the inverse of the provided matrix using Cramers Rule.
  4653. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  4654. * If the matrix is an affine transformation matrix, it is more efficient
  4655. * to invert it with {@link Matrix4.inverseTransformation}.
  4656. *
  4657. * @param {Matrix4} matrix The matrix to invert.
  4658. * @param {Matrix4} result The object onto which to store the result.
  4659. * @returns {Matrix4} The modified result parameter.
  4660. *
  4661. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  4662. */
  4663. Matrix4.inverse = function (matrix, result) {
  4664. //>>includeStart('debug', pragmas.debug);
  4665. Check.Check.typeOf.object("matrix", matrix);
  4666. Check.Check.typeOf.object("result", result);
  4667. //>>includeEnd('debug');
  4668. //
  4669. // Ported from:
  4670. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  4671. //
  4672. var src0 = matrix[0];
  4673. var src1 = matrix[4];
  4674. var src2 = matrix[8];
  4675. var src3 = matrix[12];
  4676. var src4 = matrix[1];
  4677. var src5 = matrix[5];
  4678. var src6 = matrix[9];
  4679. var src7 = matrix[13];
  4680. var src8 = matrix[2];
  4681. var src9 = matrix[6];
  4682. var src10 = matrix[10];
  4683. var src11 = matrix[14];
  4684. var src12 = matrix[3];
  4685. var src13 = matrix[7];
  4686. var src14 = matrix[11];
  4687. var src15 = matrix[15];
  4688. // calculate pairs for first 8 elements (cofactors)
  4689. var tmp0 = src10 * src15;
  4690. var tmp1 = src11 * src14;
  4691. var tmp2 = src9 * src15;
  4692. var tmp3 = src11 * src13;
  4693. var tmp4 = src9 * src14;
  4694. var tmp5 = src10 * src13;
  4695. var tmp6 = src8 * src15;
  4696. var tmp7 = src11 * src12;
  4697. var tmp8 = src8 * src14;
  4698. var tmp9 = src10 * src12;
  4699. var tmp10 = src8 * src13;
  4700. var tmp11 = src9 * src12;
  4701. // calculate first 8 elements (cofactors)
  4702. var dst0 =
  4703. tmp0 * src5 +
  4704. tmp3 * src6 +
  4705. tmp4 * src7 -
  4706. (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  4707. var dst1 =
  4708. tmp1 * src4 +
  4709. tmp6 * src6 +
  4710. tmp9 * src7 -
  4711. (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  4712. var dst2 =
  4713. tmp2 * src4 +
  4714. tmp7 * src5 +
  4715. tmp10 * src7 -
  4716. (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  4717. var dst3 =
  4718. tmp5 * src4 +
  4719. tmp8 * src5 +
  4720. tmp11 * src6 -
  4721. (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  4722. var dst4 =
  4723. tmp1 * src1 +
  4724. tmp2 * src2 +
  4725. tmp5 * src3 -
  4726. (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  4727. var dst5 =
  4728. tmp0 * src0 +
  4729. tmp7 * src2 +
  4730. tmp8 * src3 -
  4731. (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  4732. var dst6 =
  4733. tmp3 * src0 +
  4734. tmp6 * src1 +
  4735. tmp11 * src3 -
  4736. (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  4737. var dst7 =
  4738. tmp4 * src0 +
  4739. tmp9 * src1 +
  4740. tmp10 * src2 -
  4741. (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  4742. // calculate pairs for second 8 elements (cofactors)
  4743. tmp0 = src2 * src7;
  4744. tmp1 = src3 * src6;
  4745. tmp2 = src1 * src7;
  4746. tmp3 = src3 * src5;
  4747. tmp4 = src1 * src6;
  4748. tmp5 = src2 * src5;
  4749. tmp6 = src0 * src7;
  4750. tmp7 = src3 * src4;
  4751. tmp8 = src0 * src6;
  4752. tmp9 = src2 * src4;
  4753. tmp10 = src0 * src5;
  4754. tmp11 = src1 * src4;
  4755. // calculate second 8 elements (cofactors)
  4756. var dst8 =
  4757. tmp0 * src13 +
  4758. tmp3 * src14 +
  4759. tmp4 * src15 -
  4760. (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  4761. var dst9 =
  4762. tmp1 * src12 +
  4763. tmp6 * src14 +
  4764. tmp9 * src15 -
  4765. (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  4766. var dst10 =
  4767. tmp2 * src12 +
  4768. tmp7 * src13 +
  4769. tmp10 * src15 -
  4770. (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  4771. var dst11 =
  4772. tmp5 * src12 +
  4773. tmp8 * src13 +
  4774. tmp11 * src14 -
  4775. (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  4776. var dst12 =
  4777. tmp2 * src10 +
  4778. tmp5 * src11 +
  4779. tmp1 * src9 -
  4780. (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  4781. var dst13 =
  4782. tmp8 * src11 +
  4783. tmp0 * src8 +
  4784. tmp7 * src10 -
  4785. (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  4786. var dst14 =
  4787. tmp6 * src9 +
  4788. tmp11 * src11 +
  4789. tmp3 * src8 -
  4790. (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  4791. var dst15 =
  4792. tmp10 * src10 +
  4793. tmp4 * src8 +
  4794. tmp9 * src9 -
  4795. (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  4796. // calculate determinant
  4797. var det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  4798. if (Math.abs(det) < _Math.CesiumMath.EPSILON21) {
  4799. // Special case for a zero scale matrix that can occur, for example,
  4800. // when a model's node has a [0, 0, 0] scale.
  4801. if (
  4802. Matrix3.equalsEpsilon(
  4803. Matrix4.getMatrix3(matrix, scratchInverseRotation),
  4804. scratchMatrix3Zero,
  4805. _Math.CesiumMath.EPSILON7
  4806. ) &&
  4807. Cartesian4.equals(
  4808. Matrix4.getRow(matrix, 3, scratchBottomRow),
  4809. scratchExpectedBottomRow
  4810. )
  4811. ) {
  4812. result[0] = 0.0;
  4813. result[1] = 0.0;
  4814. result[2] = 0.0;
  4815. result[3] = 0.0;
  4816. result[4] = 0.0;
  4817. result[5] = 0.0;
  4818. result[6] = 0.0;
  4819. result[7] = 0.0;
  4820. result[8] = 0.0;
  4821. result[9] = 0.0;
  4822. result[10] = 0.0;
  4823. result[11] = 0.0;
  4824. result[12] = -matrix[12];
  4825. result[13] = -matrix[13];
  4826. result[14] = -matrix[14];
  4827. result[15] = 1.0;
  4828. return result;
  4829. }
  4830. throw new RuntimeError.RuntimeError(
  4831. "matrix is not invertible because its determinate is zero."
  4832. );
  4833. }
  4834. // calculate matrix inverse
  4835. det = 1.0 / det;
  4836. result[0] = dst0 * det;
  4837. result[1] = dst1 * det;
  4838. result[2] = dst2 * det;
  4839. result[3] = dst3 * det;
  4840. result[4] = dst4 * det;
  4841. result[5] = dst5 * det;
  4842. result[6] = dst6 * det;
  4843. result[7] = dst7 * det;
  4844. result[8] = dst8 * det;
  4845. result[9] = dst9 * det;
  4846. result[10] = dst10 * det;
  4847. result[11] = dst11 * det;
  4848. result[12] = dst12 * det;
  4849. result[13] = dst13 * det;
  4850. result[14] = dst14 * det;
  4851. result[15] = dst15 * det;
  4852. return result;
  4853. };
  4854. /**
  4855. * Computes the inverse of the provided matrix assuming it is
  4856. * an affine transformation matrix, where the upper left 3x3 elements
  4857. * are a rotation matrix, and the upper three elements in the fourth
  4858. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  4859. * The matrix is not verified to be in the proper form.
  4860. * This method is faster than computing the inverse for a general 4x4
  4861. * matrix using {@link Matrix4.inverse}.
  4862. *
  4863. * @param {Matrix4} matrix The matrix to invert.
  4864. * @param {Matrix4} result The object onto which to store the result.
  4865. * @returns {Matrix4} The modified result parameter.
  4866. */
  4867. Matrix4.inverseTransformation = function (matrix, result) {
  4868. //>>includeStart('debug', pragmas.debug);
  4869. Check.Check.typeOf.object("matrix", matrix);
  4870. Check.Check.typeOf.object("result", result);
  4871. //>>includeEnd('debug');
  4872. //This function is an optimized version of the below 4 lines.
  4873. //var rT = Matrix3.transpose(Matrix4.getMatrix3(matrix));
  4874. //var rTN = Matrix3.negate(rT);
  4875. //var rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  4876. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  4877. var matrix0 = matrix[0];
  4878. var matrix1 = matrix[1];
  4879. var matrix2 = matrix[2];
  4880. var matrix4 = matrix[4];
  4881. var matrix5 = matrix[5];
  4882. var matrix6 = matrix[6];
  4883. var matrix8 = matrix[8];
  4884. var matrix9 = matrix[9];
  4885. var matrix10 = matrix[10];
  4886. var vX = matrix[12];
  4887. var vY = matrix[13];
  4888. var vZ = matrix[14];
  4889. var x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  4890. var y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  4891. var z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  4892. result[0] = matrix0;
  4893. result[1] = matrix4;
  4894. result[2] = matrix8;
  4895. result[3] = 0.0;
  4896. result[4] = matrix1;
  4897. result[5] = matrix5;
  4898. result[6] = matrix9;
  4899. result[7] = 0.0;
  4900. result[8] = matrix2;
  4901. result[9] = matrix6;
  4902. result[10] = matrix10;
  4903. result[11] = 0.0;
  4904. result[12] = x;
  4905. result[13] = y;
  4906. result[14] = z;
  4907. result[15] = 1.0;
  4908. return result;
  4909. };
  4910. /**
  4911. * An immutable Matrix4 instance initialized to the identity matrix.
  4912. *
  4913. * @type {Matrix4}
  4914. * @constant
  4915. */
  4916. Matrix4.IDENTITY = Object.freeze(
  4917. new Matrix4(
  4918. 1.0,
  4919. 0.0,
  4920. 0.0,
  4921. 0.0,
  4922. 0.0,
  4923. 1.0,
  4924. 0.0,
  4925. 0.0,
  4926. 0.0,
  4927. 0.0,
  4928. 1.0,
  4929. 0.0,
  4930. 0.0,
  4931. 0.0,
  4932. 0.0,
  4933. 1.0
  4934. )
  4935. );
  4936. /**
  4937. * An immutable Matrix4 instance initialized to the zero matrix.
  4938. *
  4939. * @type {Matrix4}
  4940. * @constant
  4941. */
  4942. Matrix4.ZERO = Object.freeze(
  4943. new Matrix4(
  4944. 0.0,
  4945. 0.0,
  4946. 0.0,
  4947. 0.0,
  4948. 0.0,
  4949. 0.0,
  4950. 0.0,
  4951. 0.0,
  4952. 0.0,
  4953. 0.0,
  4954. 0.0,
  4955. 0.0,
  4956. 0.0,
  4957. 0.0,
  4958. 0.0,
  4959. 0.0
  4960. )
  4961. );
  4962. /**
  4963. * The index into Matrix4 for column 0, row 0.
  4964. *
  4965. * @type {Number}
  4966. * @constant
  4967. */
  4968. Matrix4.COLUMN0ROW0 = 0;
  4969. /**
  4970. * The index into Matrix4 for column 0, row 1.
  4971. *
  4972. * @type {Number}
  4973. * @constant
  4974. */
  4975. Matrix4.COLUMN0ROW1 = 1;
  4976. /**
  4977. * The index into Matrix4 for column 0, row 2.
  4978. *
  4979. * @type {Number}
  4980. * @constant
  4981. */
  4982. Matrix4.COLUMN0ROW2 = 2;
  4983. /**
  4984. * The index into Matrix4 for column 0, row 3.
  4985. *
  4986. * @type {Number}
  4987. * @constant
  4988. */
  4989. Matrix4.COLUMN0ROW3 = 3;
  4990. /**
  4991. * The index into Matrix4 for column 1, row 0.
  4992. *
  4993. * @type {Number}
  4994. * @constant
  4995. */
  4996. Matrix4.COLUMN1ROW0 = 4;
  4997. /**
  4998. * The index into Matrix4 for column 1, row 1.
  4999. *
  5000. * @type {Number}
  5001. * @constant
  5002. */
  5003. Matrix4.COLUMN1ROW1 = 5;
  5004. /**
  5005. * The index into Matrix4 for column 1, row 2.
  5006. *
  5007. * @type {Number}
  5008. * @constant
  5009. */
  5010. Matrix4.COLUMN1ROW2 = 6;
  5011. /**
  5012. * The index into Matrix4 for column 1, row 3.
  5013. *
  5014. * @type {Number}
  5015. * @constant
  5016. */
  5017. Matrix4.COLUMN1ROW3 = 7;
  5018. /**
  5019. * The index into Matrix4 for column 2, row 0.
  5020. *
  5021. * @type {Number}
  5022. * @constant
  5023. */
  5024. Matrix4.COLUMN2ROW0 = 8;
  5025. /**
  5026. * The index into Matrix4 for column 2, row 1.
  5027. *
  5028. * @type {Number}
  5029. * @constant
  5030. */
  5031. Matrix4.COLUMN2ROW1 = 9;
  5032. /**
  5033. * The index into Matrix4 for column 2, row 2.
  5034. *
  5035. * @type {Number}
  5036. * @constant
  5037. */
  5038. Matrix4.COLUMN2ROW2 = 10;
  5039. /**
  5040. * The index into Matrix4 for column 2, row 3.
  5041. *
  5042. * @type {Number}
  5043. * @constant
  5044. */
  5045. Matrix4.COLUMN2ROW3 = 11;
  5046. /**
  5047. * The index into Matrix4 for column 3, row 0.
  5048. *
  5049. * @type {Number}
  5050. * @constant
  5051. */
  5052. Matrix4.COLUMN3ROW0 = 12;
  5053. /**
  5054. * The index into Matrix4 for column 3, row 1.
  5055. *
  5056. * @type {Number}
  5057. * @constant
  5058. */
  5059. Matrix4.COLUMN3ROW1 = 13;
  5060. /**
  5061. * The index into Matrix4 for column 3, row 2.
  5062. *
  5063. * @type {Number}
  5064. * @constant
  5065. */
  5066. Matrix4.COLUMN3ROW2 = 14;
  5067. /**
  5068. * The index into Matrix4 for column 3, row 3.
  5069. *
  5070. * @type {Number}
  5071. * @constant
  5072. */
  5073. Matrix4.COLUMN3ROW3 = 15;
  5074. Object.defineProperties(Matrix4.prototype, {
  5075. /**
  5076. * Gets the number of items in the collection.
  5077. * @memberof Matrix4.prototype
  5078. *
  5079. * @type {Number}
  5080. */
  5081. length: {
  5082. get: function () {
  5083. return Matrix4.packedLength;
  5084. },
  5085. },
  5086. });
  5087. /**
  5088. * Duplicates the provided Matrix4 instance.
  5089. *
  5090. * @param {Matrix4} [result] The object onto which to store the result.
  5091. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  5092. */
  5093. Matrix4.prototype.clone = function (result) {
  5094. return Matrix4.clone(this, result);
  5095. };
  5096. /**
  5097. * Compares this matrix to the provided matrix componentwise and returns
  5098. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5099. *
  5100. * @param {Matrix4} [right] The right hand side matrix.
  5101. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  5102. */
  5103. Matrix4.prototype.equals = function (right) {
  5104. return Matrix4.equals(this, right);
  5105. };
  5106. /**
  5107. * @private
  5108. */
  5109. Matrix4.equalsArray = function (matrix, array, offset) {
  5110. return (
  5111. matrix[0] === array[offset] &&
  5112. matrix[1] === array[offset + 1] &&
  5113. matrix[2] === array[offset + 2] &&
  5114. matrix[3] === array[offset + 3] &&
  5115. matrix[4] === array[offset + 4] &&
  5116. matrix[5] === array[offset + 5] &&
  5117. matrix[6] === array[offset + 6] &&
  5118. matrix[7] === array[offset + 7] &&
  5119. matrix[8] === array[offset + 8] &&
  5120. matrix[9] === array[offset + 9] &&
  5121. matrix[10] === array[offset + 10] &&
  5122. matrix[11] === array[offset + 11] &&
  5123. matrix[12] === array[offset + 12] &&
  5124. matrix[13] === array[offset + 13] &&
  5125. matrix[14] === array[offset + 14] &&
  5126. matrix[15] === array[offset + 15]
  5127. );
  5128. };
  5129. /**
  5130. * Compares this matrix to the provided matrix componentwise and returns
  5131. * <code>true</code> if they are within the provided epsilon,
  5132. * <code>false</code> otherwise.
  5133. *
  5134. * @param {Matrix4} [right] The right hand side matrix.
  5135. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  5136. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  5137. */
  5138. Matrix4.prototype.equalsEpsilon = function (right, epsilon) {
  5139. return Matrix4.equalsEpsilon(this, right, epsilon);
  5140. };
  5141. /**
  5142. * Computes a string representing this Matrix with each row being
  5143. * on a separate line and in the format '(column0, column1, column2, column3)'.
  5144. *
  5145. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  5146. */
  5147. Matrix4.prototype.toString = function () {
  5148. return (
  5149. "(" +
  5150. this[0] +
  5151. ", " +
  5152. this[4] +
  5153. ", " +
  5154. this[8] +
  5155. ", " +
  5156. this[12] +
  5157. ")\n" +
  5158. "(" +
  5159. this[1] +
  5160. ", " +
  5161. this[5] +
  5162. ", " +
  5163. this[9] +
  5164. ", " +
  5165. this[13] +
  5166. ")\n" +
  5167. "(" +
  5168. this[2] +
  5169. ", " +
  5170. this[6] +
  5171. ", " +
  5172. this[10] +
  5173. ", " +
  5174. this[14] +
  5175. ")\n" +
  5176. "(" +
  5177. this[3] +
  5178. ", " +
  5179. this[7] +
  5180. ", " +
  5181. this[11] +
  5182. ", " +
  5183. this[15] +
  5184. ")"
  5185. );
  5186. };
  5187. /**
  5188. * A bounding sphere with a center and a radius.
  5189. * @alias BoundingSphere
  5190. * @constructor
  5191. *
  5192. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  5193. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  5194. *
  5195. * @see AxisAlignedBoundingBox
  5196. * @see BoundingRectangle
  5197. * @see Packable
  5198. */
  5199. function BoundingSphere(center, radius) {
  5200. /**
  5201. * The center point of the sphere.
  5202. * @type {Cartesian3}
  5203. * @default {@link Cartesian3.ZERO}
  5204. */
  5205. this.center = Cartesian2.Cartesian3.clone(when.defaultValue(center, Cartesian2.Cartesian3.ZERO));
  5206. /**
  5207. * The radius of the sphere.
  5208. * @type {Number}
  5209. * @default 0.0
  5210. */
  5211. this.radius = when.defaultValue(radius, 0.0);
  5212. }
  5213. var fromPointsXMin = new Cartesian2.Cartesian3();
  5214. var fromPointsYMin = new Cartesian2.Cartesian3();
  5215. var fromPointsZMin = new Cartesian2.Cartesian3();
  5216. var fromPointsXMax = new Cartesian2.Cartesian3();
  5217. var fromPointsYMax = new Cartesian2.Cartesian3();
  5218. var fromPointsZMax = new Cartesian2.Cartesian3();
  5219. var fromPointsCurrentPos = new Cartesian2.Cartesian3();
  5220. var fromPointsScratch = new Cartesian2.Cartesian3();
  5221. var fromPointsRitterCenter = new Cartesian2.Cartesian3();
  5222. var fromPointsMinBoxPt = new Cartesian2.Cartesian3();
  5223. var fromPointsMaxBoxPt = new Cartesian2.Cartesian3();
  5224. var fromPointsNaiveCenterScratch = new Cartesian2.Cartesian3();
  5225. var volumeConstant = (4.0 / 3.0) * _Math.CesiumMath.PI;
  5226. /**
  5227. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  5228. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  5229. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  5230. *
  5231. * @param {Cartesian3[]} [positions] An array of points that the bounding sphere will enclose. Each point must have <code>x</code>, <code>y</code>, and <code>z</code> properties.
  5232. * @param {BoundingSphere} [result] The object onto which to store the result.
  5233. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  5234. *
  5235. * @see {@link http://help.agi.com/AGIComponents/html/BlogBoundingSphere.htm|Bounding Sphere computation article}
  5236. */
  5237. BoundingSphere.fromPoints = function (positions, result) {
  5238. if (!when.defined(result)) {
  5239. result = new BoundingSphere();
  5240. }
  5241. if (!when.defined(positions) || positions.length === 0) {
  5242. result.center = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5243. result.radius = 0.0;
  5244. return result;
  5245. }
  5246. var currentPos = Cartesian2.Cartesian3.clone(positions[0], fromPointsCurrentPos);
  5247. var xMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsXMin);
  5248. var yMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsYMin);
  5249. var zMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsZMin);
  5250. var xMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsXMax);
  5251. var yMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsYMax);
  5252. var zMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsZMax);
  5253. var numPositions = positions.length;
  5254. var i;
  5255. for (i = 1; i < numPositions; i++) {
  5256. Cartesian2.Cartesian3.clone(positions[i], currentPos);
  5257. var x = currentPos.x;
  5258. var y = currentPos.y;
  5259. var z = currentPos.z;
  5260. // Store points containing the the smallest and largest components
  5261. if (x < xMin.x) {
  5262. Cartesian2.Cartesian3.clone(currentPos, xMin);
  5263. }
  5264. if (x > xMax.x) {
  5265. Cartesian2.Cartesian3.clone(currentPos, xMax);
  5266. }
  5267. if (y < yMin.y) {
  5268. Cartesian2.Cartesian3.clone(currentPos, yMin);
  5269. }
  5270. if (y > yMax.y) {
  5271. Cartesian2.Cartesian3.clone(currentPos, yMax);
  5272. }
  5273. if (z < zMin.z) {
  5274. Cartesian2.Cartesian3.clone(currentPos, zMin);
  5275. }
  5276. if (z > zMax.z) {
  5277. Cartesian2.Cartesian3.clone(currentPos, zMax);
  5278. }
  5279. }
  5280. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  5281. var xSpan = Cartesian2.Cartesian3.magnitudeSquared(
  5282. Cartesian2.Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  5283. );
  5284. var ySpan = Cartesian2.Cartesian3.magnitudeSquared(
  5285. Cartesian2.Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  5286. );
  5287. var zSpan = Cartesian2.Cartesian3.magnitudeSquared(
  5288. Cartesian2.Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  5289. );
  5290. // Set the diameter endpoints to the largest span.
  5291. var diameter1 = xMin;
  5292. var diameter2 = xMax;
  5293. var maxSpan = xSpan;
  5294. if (ySpan > maxSpan) {
  5295. maxSpan = ySpan;
  5296. diameter1 = yMin;
  5297. diameter2 = yMax;
  5298. }
  5299. if (zSpan > maxSpan) {
  5300. maxSpan = zSpan;
  5301. diameter1 = zMin;
  5302. diameter2 = zMax;
  5303. }
  5304. // Calculate the center of the initial sphere found by Ritter's algorithm
  5305. var ritterCenter = fromPointsRitterCenter;
  5306. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  5307. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  5308. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  5309. // Calculate the radius of the initial sphere found by Ritter's algorithm
  5310. var radiusSquared = Cartesian2.Cartesian3.magnitudeSquared(
  5311. Cartesian2.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  5312. );
  5313. var ritterRadius = Math.sqrt(radiusSquared);
  5314. // Find the center of the sphere found using the Naive method.
  5315. var minBoxPt = fromPointsMinBoxPt;
  5316. minBoxPt.x = xMin.x;
  5317. minBoxPt.y = yMin.y;
  5318. minBoxPt.z = zMin.z;
  5319. var maxBoxPt = fromPointsMaxBoxPt;
  5320. maxBoxPt.x = xMax.x;
  5321. maxBoxPt.y = yMax.y;
  5322. maxBoxPt.z = zMax.z;
  5323. var naiveCenter = Cartesian2.Cartesian3.midpoint(
  5324. minBoxPt,
  5325. maxBoxPt,
  5326. fromPointsNaiveCenterScratch
  5327. );
  5328. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  5329. var naiveRadius = 0;
  5330. for (i = 0; i < numPositions; i++) {
  5331. Cartesian2.Cartesian3.clone(positions[i], currentPos);
  5332. // Find the furthest point from the naive center to calculate the naive radius.
  5333. var r = Cartesian2.Cartesian3.magnitude(
  5334. Cartesian2.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  5335. );
  5336. if (r > naiveRadius) {
  5337. naiveRadius = r;
  5338. }
  5339. // Make adjustments to the Ritter Sphere to include all points.
  5340. var oldCenterToPointSquared = Cartesian2.Cartesian3.magnitudeSquared(
  5341. Cartesian2.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  5342. );
  5343. if (oldCenterToPointSquared > radiusSquared) {
  5344. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  5345. // Calculate new radius to include the point that lies outside
  5346. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  5347. radiusSquared = ritterRadius * ritterRadius;
  5348. // Calculate center of new Ritter sphere
  5349. var oldToNew = oldCenterToPoint - ritterRadius;
  5350. ritterCenter.x =
  5351. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  5352. oldCenterToPoint;
  5353. ritterCenter.y =
  5354. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  5355. oldCenterToPoint;
  5356. ritterCenter.z =
  5357. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  5358. oldCenterToPoint;
  5359. }
  5360. }
  5361. if (ritterRadius < naiveRadius) {
  5362. Cartesian2.Cartesian3.clone(ritterCenter, result.center);
  5363. result.radius = ritterRadius;
  5364. } else {
  5365. Cartesian2.Cartesian3.clone(naiveCenter, result.center);
  5366. result.radius = naiveRadius;
  5367. }
  5368. return result;
  5369. };
  5370. var defaultProjection = new GeographicProjection();
  5371. var fromRectangle2DLowerLeft = new Cartesian2.Cartesian3();
  5372. var fromRectangle2DUpperRight = new Cartesian2.Cartesian3();
  5373. var fromRectangle2DSouthwest = new Cartesian2.Cartographic();
  5374. var fromRectangle2DNortheast = new Cartesian2.Cartographic();
  5375. /**
  5376. * Computes a bounding sphere from a rectangle projected in 2D.
  5377. *
  5378. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  5379. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  5380. * @param {BoundingSphere} [result] The object onto which to store the result.
  5381. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5382. */
  5383. BoundingSphere.fromRectangle2D = function (rectangle, projection, result) {
  5384. return BoundingSphere.fromRectangleWithHeights2D(
  5385. rectangle,
  5386. projection,
  5387. 0.0,
  5388. 0.0,
  5389. result
  5390. );
  5391. };
  5392. /**
  5393. * Computes a bounding sphere from a rectangle projected in 2D. The bounding sphere accounts for the
  5394. * object's minimum and maximum heights over the rectangle.
  5395. *
  5396. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  5397. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  5398. * @param {Number} [minimumHeight=0.0] The minimum height over the rectangle.
  5399. * @param {Number} [maximumHeight=0.0] The maximum height over the rectangle.
  5400. * @param {BoundingSphere} [result] The object onto which to store the result.
  5401. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5402. */
  5403. BoundingSphere.fromRectangleWithHeights2D = function (
  5404. rectangle,
  5405. projection,
  5406. minimumHeight,
  5407. maximumHeight,
  5408. result
  5409. ) {
  5410. if (!when.defined(result)) {
  5411. result = new BoundingSphere();
  5412. }
  5413. if (!when.defined(rectangle)) {
  5414. result.center = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5415. result.radius = 0.0;
  5416. return result;
  5417. }
  5418. projection = when.defaultValue(projection, defaultProjection);
  5419. Cartesian2.Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  5420. fromRectangle2DSouthwest.height = minimumHeight;
  5421. Cartesian2.Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  5422. fromRectangle2DNortheast.height = maximumHeight;
  5423. var lowerLeft = projection.project(
  5424. fromRectangle2DSouthwest,
  5425. fromRectangle2DLowerLeft
  5426. );
  5427. var upperRight = projection.project(
  5428. fromRectangle2DNortheast,
  5429. fromRectangle2DUpperRight
  5430. );
  5431. var width = upperRight.x - lowerLeft.x;
  5432. var height = upperRight.y - lowerLeft.y;
  5433. var elevation = upperRight.z - lowerLeft.z;
  5434. result.radius =
  5435. Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  5436. var center = result.center;
  5437. center.x = lowerLeft.x + width * 0.5;
  5438. center.y = lowerLeft.y + height * 0.5;
  5439. center.z = lowerLeft.z + elevation * 0.5;
  5440. return result;
  5441. };
  5442. var fromRectangle3DScratch = [];
  5443. /**
  5444. * Computes a bounding sphere from a rectangle in 3D. The bounding sphere is created using a subsample of points
  5445. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  5446. *
  5447. * @param {Rectangle} [rectangle] The valid rectangle used to create a bounding sphere.
  5448. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  5449. * @param {Number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  5450. * @param {BoundingSphere} [result] The object onto which to store the result.
  5451. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5452. */
  5453. BoundingSphere.fromRectangle3D = function (
  5454. rectangle,
  5455. ellipsoid,
  5456. surfaceHeight,
  5457. result
  5458. ) {
  5459. ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  5460. surfaceHeight = when.defaultValue(surfaceHeight, 0.0);
  5461. if (!when.defined(result)) {
  5462. result = new BoundingSphere();
  5463. }
  5464. if (!when.defined(rectangle)) {
  5465. result.center = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5466. result.radius = 0.0;
  5467. return result;
  5468. }
  5469. var positions = Cartesian2.Rectangle.subsample(
  5470. rectangle,
  5471. ellipsoid,
  5472. surfaceHeight,
  5473. fromRectangle3DScratch
  5474. );
  5475. return BoundingSphere.fromPoints(positions, result);
  5476. };
  5477. /**
  5478. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  5479. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  5480. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  5481. * ensure a tight fit.
  5482. *
  5483. * @param {Number[]} [positions] An array of points that the bounding sphere will enclose. Each point
  5484. * is formed from three elements in the array in the order X, Y, Z.
  5485. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  5486. * origin of the coordinate system. This is useful when the positions are to be used for
  5487. * relative-to-center (RTC) rendering.
  5488. * @param {Number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  5489. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  5490. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  5491. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  5492. * the stride is 5, however, two array elements are skipped and the next position begins at array
  5493. * index 5.
  5494. * @param {BoundingSphere} [result] The object onto which to store the result.
  5495. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  5496. *
  5497. * @example
  5498. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  5499. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  5500. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  5501. * var center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  5502. * var points = [1.0, 2.0, 3.0, 0.1, 0.2,
  5503. * 4.0, 5.0, 6.0, 0.1, 0.2,
  5504. * 7.0, 8.0, 9.0, 0.1, 0.2];
  5505. * var sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  5506. *
  5507. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  5508. */
  5509. BoundingSphere.fromVertices = function (positions, center, stride, result) {
  5510. if (!when.defined(result)) {
  5511. result = new BoundingSphere();
  5512. }
  5513. if (!when.defined(positions) || positions.length === 0) {
  5514. result.center = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5515. result.radius = 0.0;
  5516. return result;
  5517. }
  5518. center = when.defaultValue(center, Cartesian2.Cartesian3.ZERO);
  5519. stride = when.defaultValue(stride, 3);
  5520. //>>includeStart('debug', pragmas.debug);
  5521. Check.Check.typeOf.number.greaterThanOrEquals("stride", stride, 3);
  5522. //>>includeEnd('debug');
  5523. var currentPos = fromPointsCurrentPos;
  5524. currentPos.x = positions[0] + center.x;
  5525. currentPos.y = positions[1] + center.y;
  5526. currentPos.z = positions[2] + center.z;
  5527. var xMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsXMin);
  5528. var yMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsYMin);
  5529. var zMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsZMin);
  5530. var xMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsXMax);
  5531. var yMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsYMax);
  5532. var zMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsZMax);
  5533. var numElements = positions.length;
  5534. var i;
  5535. for (i = 0; i < numElements; i += stride) {
  5536. var x = positions[i] + center.x;
  5537. var y = positions[i + 1] + center.y;
  5538. var z = positions[i + 2] + center.z;
  5539. currentPos.x = x;
  5540. currentPos.y = y;
  5541. currentPos.z = z;
  5542. // Store points containing the the smallest and largest components
  5543. if (x < xMin.x) {
  5544. Cartesian2.Cartesian3.clone(currentPos, xMin);
  5545. }
  5546. if (x > xMax.x) {
  5547. Cartesian2.Cartesian3.clone(currentPos, xMax);
  5548. }
  5549. if (y < yMin.y) {
  5550. Cartesian2.Cartesian3.clone(currentPos, yMin);
  5551. }
  5552. if (y > yMax.y) {
  5553. Cartesian2.Cartesian3.clone(currentPos, yMax);
  5554. }
  5555. if (z < zMin.z) {
  5556. Cartesian2.Cartesian3.clone(currentPos, zMin);
  5557. }
  5558. if (z > zMax.z) {
  5559. Cartesian2.Cartesian3.clone(currentPos, zMax);
  5560. }
  5561. }
  5562. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  5563. var xSpan = Cartesian2.Cartesian3.magnitudeSquared(
  5564. Cartesian2.Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  5565. );
  5566. var ySpan = Cartesian2.Cartesian3.magnitudeSquared(
  5567. Cartesian2.Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  5568. );
  5569. var zSpan = Cartesian2.Cartesian3.magnitudeSquared(
  5570. Cartesian2.Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  5571. );
  5572. // Set the diameter endpoints to the largest span.
  5573. var diameter1 = xMin;
  5574. var diameter2 = xMax;
  5575. var maxSpan = xSpan;
  5576. if (ySpan > maxSpan) {
  5577. maxSpan = ySpan;
  5578. diameter1 = yMin;
  5579. diameter2 = yMax;
  5580. }
  5581. if (zSpan > maxSpan) {
  5582. maxSpan = zSpan;
  5583. diameter1 = zMin;
  5584. diameter2 = zMax;
  5585. }
  5586. // Calculate the center of the initial sphere found by Ritter's algorithm
  5587. var ritterCenter = fromPointsRitterCenter;
  5588. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  5589. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  5590. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  5591. // Calculate the radius of the initial sphere found by Ritter's algorithm
  5592. var radiusSquared = Cartesian2.Cartesian3.magnitudeSquared(
  5593. Cartesian2.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  5594. );
  5595. var ritterRadius = Math.sqrt(radiusSquared);
  5596. // Find the center of the sphere found using the Naive method.
  5597. var minBoxPt = fromPointsMinBoxPt;
  5598. minBoxPt.x = xMin.x;
  5599. minBoxPt.y = yMin.y;
  5600. minBoxPt.z = zMin.z;
  5601. var maxBoxPt = fromPointsMaxBoxPt;
  5602. maxBoxPt.x = xMax.x;
  5603. maxBoxPt.y = yMax.y;
  5604. maxBoxPt.z = zMax.z;
  5605. var naiveCenter = Cartesian2.Cartesian3.midpoint(
  5606. minBoxPt,
  5607. maxBoxPt,
  5608. fromPointsNaiveCenterScratch
  5609. );
  5610. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  5611. var naiveRadius = 0;
  5612. for (i = 0; i < numElements; i += stride) {
  5613. currentPos.x = positions[i] + center.x;
  5614. currentPos.y = positions[i + 1] + center.y;
  5615. currentPos.z = positions[i + 2] + center.z;
  5616. // Find the furthest point from the naive center to calculate the naive radius.
  5617. var r = Cartesian2.Cartesian3.magnitude(
  5618. Cartesian2.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  5619. );
  5620. if (r > naiveRadius) {
  5621. naiveRadius = r;
  5622. }
  5623. // Make adjustments to the Ritter Sphere to include all points.
  5624. var oldCenterToPointSquared = Cartesian2.Cartesian3.magnitudeSquared(
  5625. Cartesian2.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  5626. );
  5627. if (oldCenterToPointSquared > radiusSquared) {
  5628. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  5629. // Calculate new radius to include the point that lies outside
  5630. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  5631. radiusSquared = ritterRadius * ritterRadius;
  5632. // Calculate center of new Ritter sphere
  5633. var oldToNew = oldCenterToPoint - ritterRadius;
  5634. ritterCenter.x =
  5635. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  5636. oldCenterToPoint;
  5637. ritterCenter.y =
  5638. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  5639. oldCenterToPoint;
  5640. ritterCenter.z =
  5641. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  5642. oldCenterToPoint;
  5643. }
  5644. }
  5645. if (ritterRadius < naiveRadius) {
  5646. Cartesian2.Cartesian3.clone(ritterCenter, result.center);
  5647. result.radius = ritterRadius;
  5648. } else {
  5649. Cartesian2.Cartesian3.clone(naiveCenter, result.center);
  5650. result.radius = naiveRadius;
  5651. }
  5652. return result;
  5653. };
  5654. /**
  5655. * Computes a tight-fitting bounding sphere enclosing a list of EncodedCartesian3s, where the points are
  5656. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  5657. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  5658. * ensure a tight fit.
  5659. *
  5660. * @param {Number[]} [positionsHigh] An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  5661. * is formed from three elements in the array in the order X, Y, Z.
  5662. * @param {Number[]} [positionsLow] An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  5663. * is formed from three elements in the array in the order X, Y, Z.
  5664. * @param {BoundingSphere} [result] The object onto which to store the result.
  5665. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  5666. *
  5667. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  5668. */
  5669. BoundingSphere.fromEncodedCartesianVertices = function (
  5670. positionsHigh,
  5671. positionsLow,
  5672. result
  5673. ) {
  5674. if (!when.defined(result)) {
  5675. result = new BoundingSphere();
  5676. }
  5677. if (
  5678. !when.defined(positionsHigh) ||
  5679. !when.defined(positionsLow) ||
  5680. positionsHigh.length !== positionsLow.length ||
  5681. positionsHigh.length === 0
  5682. ) {
  5683. result.center = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5684. result.radius = 0.0;
  5685. return result;
  5686. }
  5687. var currentPos = fromPointsCurrentPos;
  5688. currentPos.x = positionsHigh[0] + positionsLow[0];
  5689. currentPos.y = positionsHigh[1] + positionsLow[1];
  5690. currentPos.z = positionsHigh[2] + positionsLow[2];
  5691. var xMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsXMin);
  5692. var yMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsYMin);
  5693. var zMin = Cartesian2.Cartesian3.clone(currentPos, fromPointsZMin);
  5694. var xMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsXMax);
  5695. var yMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsYMax);
  5696. var zMax = Cartesian2.Cartesian3.clone(currentPos, fromPointsZMax);
  5697. var numElements = positionsHigh.length;
  5698. var i;
  5699. for (i = 0; i < numElements; i += 3) {
  5700. var x = positionsHigh[i] + positionsLow[i];
  5701. var y = positionsHigh[i + 1] + positionsLow[i + 1];
  5702. var z = positionsHigh[i + 2] + positionsLow[i + 2];
  5703. currentPos.x = x;
  5704. currentPos.y = y;
  5705. currentPos.z = z;
  5706. // Store points containing the the smallest and largest components
  5707. if (x < xMin.x) {
  5708. Cartesian2.Cartesian3.clone(currentPos, xMin);
  5709. }
  5710. if (x > xMax.x) {
  5711. Cartesian2.Cartesian3.clone(currentPos, xMax);
  5712. }
  5713. if (y < yMin.y) {
  5714. Cartesian2.Cartesian3.clone(currentPos, yMin);
  5715. }
  5716. if (y > yMax.y) {
  5717. Cartesian2.Cartesian3.clone(currentPos, yMax);
  5718. }
  5719. if (z < zMin.z) {
  5720. Cartesian2.Cartesian3.clone(currentPos, zMin);
  5721. }
  5722. if (z > zMax.z) {
  5723. Cartesian2.Cartesian3.clone(currentPos, zMax);
  5724. }
  5725. }
  5726. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  5727. var xSpan = Cartesian2.Cartesian3.magnitudeSquared(
  5728. Cartesian2.Cartesian3.subtract(xMax, xMin, fromPointsScratch)
  5729. );
  5730. var ySpan = Cartesian2.Cartesian3.magnitudeSquared(
  5731. Cartesian2.Cartesian3.subtract(yMax, yMin, fromPointsScratch)
  5732. );
  5733. var zSpan = Cartesian2.Cartesian3.magnitudeSquared(
  5734. Cartesian2.Cartesian3.subtract(zMax, zMin, fromPointsScratch)
  5735. );
  5736. // Set the diameter endpoints to the largest span.
  5737. var diameter1 = xMin;
  5738. var diameter2 = xMax;
  5739. var maxSpan = xSpan;
  5740. if (ySpan > maxSpan) {
  5741. maxSpan = ySpan;
  5742. diameter1 = yMin;
  5743. diameter2 = yMax;
  5744. }
  5745. if (zSpan > maxSpan) {
  5746. maxSpan = zSpan;
  5747. diameter1 = zMin;
  5748. diameter2 = zMax;
  5749. }
  5750. // Calculate the center of the initial sphere found by Ritter's algorithm
  5751. var ritterCenter = fromPointsRitterCenter;
  5752. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  5753. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  5754. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  5755. // Calculate the radius of the initial sphere found by Ritter's algorithm
  5756. var radiusSquared = Cartesian2.Cartesian3.magnitudeSquared(
  5757. Cartesian2.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch)
  5758. );
  5759. var ritterRadius = Math.sqrt(radiusSquared);
  5760. // Find the center of the sphere found using the Naive method.
  5761. var minBoxPt = fromPointsMinBoxPt;
  5762. minBoxPt.x = xMin.x;
  5763. minBoxPt.y = yMin.y;
  5764. minBoxPt.z = zMin.z;
  5765. var maxBoxPt = fromPointsMaxBoxPt;
  5766. maxBoxPt.x = xMax.x;
  5767. maxBoxPt.y = yMax.y;
  5768. maxBoxPt.z = zMax.z;
  5769. var naiveCenter = Cartesian2.Cartesian3.midpoint(
  5770. minBoxPt,
  5771. maxBoxPt,
  5772. fromPointsNaiveCenterScratch
  5773. );
  5774. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  5775. var naiveRadius = 0;
  5776. for (i = 0; i < numElements; i += 3) {
  5777. currentPos.x = positionsHigh[i] + positionsLow[i];
  5778. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  5779. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  5780. // Find the furthest point from the naive center to calculate the naive radius.
  5781. var r = Cartesian2.Cartesian3.magnitude(
  5782. Cartesian2.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch)
  5783. );
  5784. if (r > naiveRadius) {
  5785. naiveRadius = r;
  5786. }
  5787. // Make adjustments to the Ritter Sphere to include all points.
  5788. var oldCenterToPointSquared = Cartesian2.Cartesian3.magnitudeSquared(
  5789. Cartesian2.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch)
  5790. );
  5791. if (oldCenterToPointSquared > radiusSquared) {
  5792. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  5793. // Calculate new radius to include the point that lies outside
  5794. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  5795. radiusSquared = ritterRadius * ritterRadius;
  5796. // Calculate center of new Ritter sphere
  5797. var oldToNew = oldCenterToPoint - ritterRadius;
  5798. ritterCenter.x =
  5799. (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) /
  5800. oldCenterToPoint;
  5801. ritterCenter.y =
  5802. (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) /
  5803. oldCenterToPoint;
  5804. ritterCenter.z =
  5805. (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) /
  5806. oldCenterToPoint;
  5807. }
  5808. }
  5809. if (ritterRadius < naiveRadius) {
  5810. Cartesian2.Cartesian3.clone(ritterCenter, result.center);
  5811. result.radius = ritterRadius;
  5812. } else {
  5813. Cartesian2.Cartesian3.clone(naiveCenter, result.center);
  5814. result.radius = naiveRadius;
  5815. }
  5816. return result;
  5817. };
  5818. /**
  5819. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  5820. * tighly and fully encompases the box.
  5821. *
  5822. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  5823. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  5824. * @param {BoundingSphere} [result] The object onto which to store the result.
  5825. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5826. *
  5827. * @example
  5828. * // Create a bounding sphere around the unit cube
  5829. * var sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  5830. */
  5831. BoundingSphere.fromCornerPoints = function (corner, oppositeCorner, result) {
  5832. //>>includeStart('debug', pragmas.debug);
  5833. Check.Check.typeOf.object("corner", corner);
  5834. Check.Check.typeOf.object("oppositeCorner", oppositeCorner);
  5835. //>>includeEnd('debug');
  5836. if (!when.defined(result)) {
  5837. result = new BoundingSphere();
  5838. }
  5839. var center = Cartesian2.Cartesian3.midpoint(corner, oppositeCorner, result.center);
  5840. result.radius = Cartesian2.Cartesian3.distance(center, oppositeCorner);
  5841. return result;
  5842. };
  5843. /**
  5844. * Creates a bounding sphere encompassing an ellipsoid.
  5845. *
  5846. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  5847. * @param {BoundingSphere} [result] The object onto which to store the result.
  5848. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5849. *
  5850. * @example
  5851. * var boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  5852. */
  5853. BoundingSphere.fromEllipsoid = function (ellipsoid, result) {
  5854. //>>includeStart('debug', pragmas.debug);
  5855. Check.Check.typeOf.object("ellipsoid", ellipsoid);
  5856. //>>includeEnd('debug');
  5857. if (!when.defined(result)) {
  5858. result = new BoundingSphere();
  5859. }
  5860. Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5861. result.radius = ellipsoid.maximumRadius;
  5862. return result;
  5863. };
  5864. var fromBoundingSpheresScratch = new Cartesian2.Cartesian3();
  5865. /**
  5866. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  5867. *
  5868. * @param {BoundingSphere[]} [boundingSpheres] The array of bounding spheres.
  5869. * @param {BoundingSphere} [result] The object onto which to store the result.
  5870. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5871. */
  5872. BoundingSphere.fromBoundingSpheres = function (boundingSpheres, result) {
  5873. if (!when.defined(result)) {
  5874. result = new BoundingSphere();
  5875. }
  5876. if (!when.defined(boundingSpheres) || boundingSpheres.length === 0) {
  5877. result.center = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, result.center);
  5878. result.radius = 0.0;
  5879. return result;
  5880. }
  5881. var length = boundingSpheres.length;
  5882. if (length === 1) {
  5883. return BoundingSphere.clone(boundingSpheres[0], result);
  5884. }
  5885. if (length === 2) {
  5886. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  5887. }
  5888. var positions = [];
  5889. var i;
  5890. for (i = 0; i < length; i++) {
  5891. positions.push(boundingSpheres[i].center);
  5892. }
  5893. result = BoundingSphere.fromPoints(positions, result);
  5894. var center = result.center;
  5895. var radius = result.radius;
  5896. for (i = 0; i < length; i++) {
  5897. var tmp = boundingSpheres[i];
  5898. radius = Math.max(
  5899. radius,
  5900. Cartesian2.Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) +
  5901. tmp.radius
  5902. );
  5903. }
  5904. result.radius = radius;
  5905. return result;
  5906. };
  5907. var fromOrientedBoundingBoxScratchU = new Cartesian2.Cartesian3();
  5908. var fromOrientedBoundingBoxScratchV = new Cartesian2.Cartesian3();
  5909. var fromOrientedBoundingBoxScratchW = new Cartesian2.Cartesian3();
  5910. /**
  5911. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  5912. *
  5913. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  5914. * @param {BoundingSphere} [result] The object onto which to store the result.
  5915. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5916. */
  5917. BoundingSphere.fromOrientedBoundingBox = function (
  5918. orientedBoundingBox,
  5919. result
  5920. ) {
  5921. //>>includeStart('debug', pragmas.debug);
  5922. Check.Check.defined("orientedBoundingBox", orientedBoundingBox);
  5923. //>>includeEnd('debug');
  5924. if (!when.defined(result)) {
  5925. result = new BoundingSphere();
  5926. }
  5927. var halfAxes = orientedBoundingBox.halfAxes;
  5928. var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  5929. var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  5930. var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  5931. Cartesian2.Cartesian3.add(u, v, u);
  5932. Cartesian2.Cartesian3.add(u, w, u);
  5933. result.center = Cartesian2.Cartesian3.clone(orientedBoundingBox.center, result.center);
  5934. result.radius = Cartesian2.Cartesian3.magnitude(u);
  5935. return result;
  5936. };
  5937. /**
  5938. * Duplicates a BoundingSphere instance.
  5939. *
  5940. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  5941. * @param {BoundingSphere} [result] The object onto which to store the result.
  5942. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  5943. */
  5944. BoundingSphere.clone = function (sphere, result) {
  5945. if (!when.defined(sphere)) {
  5946. return undefined;
  5947. }
  5948. if (!when.defined(result)) {
  5949. return new BoundingSphere(sphere.center, sphere.radius);
  5950. }
  5951. result.center = Cartesian2.Cartesian3.clone(sphere.center, result.center);
  5952. result.radius = sphere.radius;
  5953. return result;
  5954. };
  5955. /**
  5956. * The number of elements used to pack the object into an array.
  5957. * @type {Number}
  5958. */
  5959. BoundingSphere.packedLength = 4;
  5960. /**
  5961. * Stores the provided instance into the provided array.
  5962. *
  5963. * @param {BoundingSphere} value The value to pack.
  5964. * @param {Number[]} array The array to pack into.
  5965. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  5966. *
  5967. * @returns {Number[]} The array that was packed into
  5968. */
  5969. BoundingSphere.pack = function (value, array, startingIndex) {
  5970. //>>includeStart('debug', pragmas.debug);
  5971. Check.Check.typeOf.object("value", value);
  5972. Check.Check.defined("array", array);
  5973. //>>includeEnd('debug');
  5974. startingIndex = when.defaultValue(startingIndex, 0);
  5975. var center = value.center;
  5976. array[startingIndex++] = center.x;
  5977. array[startingIndex++] = center.y;
  5978. array[startingIndex++] = center.z;
  5979. array[startingIndex] = value.radius;
  5980. return array;
  5981. };
  5982. /**
  5983. * Retrieves an instance from a packed array.
  5984. *
  5985. * @param {Number[]} array The packed array.
  5986. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  5987. * @param {BoundingSphere} [result] The object into which to store the result.
  5988. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  5989. */
  5990. BoundingSphere.unpack = function (array, startingIndex, result) {
  5991. //>>includeStart('debug', pragmas.debug);
  5992. Check.Check.defined("array", array);
  5993. //>>includeEnd('debug');
  5994. startingIndex = when.defaultValue(startingIndex, 0);
  5995. if (!when.defined(result)) {
  5996. result = new BoundingSphere();
  5997. }
  5998. var center = result.center;
  5999. center.x = array[startingIndex++];
  6000. center.y = array[startingIndex++];
  6001. center.z = array[startingIndex++];
  6002. result.radius = array[startingIndex];
  6003. return result;
  6004. };
  6005. var unionScratch = new Cartesian2.Cartesian3();
  6006. var unionScratchCenter = new Cartesian2.Cartesian3();
  6007. /**
  6008. * Computes a bounding sphere that contains both the left and right bounding spheres.
  6009. *
  6010. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  6011. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  6012. * @param {BoundingSphere} [result] The object onto which to store the result.
  6013. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  6014. */
  6015. BoundingSphere.union = function (left, right, result) {
  6016. //>>includeStart('debug', pragmas.debug);
  6017. Check.Check.typeOf.object("left", left);
  6018. Check.Check.typeOf.object("right", right);
  6019. //>>includeEnd('debug');
  6020. if (!when.defined(result)) {
  6021. result = new BoundingSphere();
  6022. }
  6023. var leftCenter = left.center;
  6024. var leftRadius = left.radius;
  6025. var rightCenter = right.center;
  6026. var rightRadius = right.radius;
  6027. var toRightCenter = Cartesian2.Cartesian3.subtract(
  6028. rightCenter,
  6029. leftCenter,
  6030. unionScratch
  6031. );
  6032. var centerSeparation = Cartesian2.Cartesian3.magnitude(toRightCenter);
  6033. if (leftRadius >= centerSeparation + rightRadius) {
  6034. // Left sphere wins.
  6035. left.clone(result);
  6036. return result;
  6037. }
  6038. if (rightRadius >= centerSeparation + leftRadius) {
  6039. // Right sphere wins.
  6040. right.clone(result);
  6041. return result;
  6042. }
  6043. // There are two tangent points, one on far side of each sphere.
  6044. var halfDistanceBetweenTangentPoints =
  6045. (leftRadius + centerSeparation + rightRadius) * 0.5;
  6046. // Compute the center point halfway between the two tangent points.
  6047. var center = Cartesian2.Cartesian3.multiplyByScalar(
  6048. toRightCenter,
  6049. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation,
  6050. unionScratchCenter
  6051. );
  6052. Cartesian2.Cartesian3.add(center, leftCenter, center);
  6053. Cartesian2.Cartesian3.clone(center, result.center);
  6054. result.radius = halfDistanceBetweenTangentPoints;
  6055. return result;
  6056. };
  6057. var expandScratch = new Cartesian2.Cartesian3();
  6058. /**
  6059. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  6060. *
  6061. * @param {BoundingSphere} sphere A sphere to expand.
  6062. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  6063. * @param {BoundingSphere} [result] The object onto which to store the result.
  6064. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  6065. */
  6066. BoundingSphere.expand = function (sphere, point, result) {
  6067. //>>includeStart('debug', pragmas.debug);
  6068. Check.Check.typeOf.object("sphere", sphere);
  6069. Check.Check.typeOf.object("point", point);
  6070. //>>includeEnd('debug');
  6071. result = BoundingSphere.clone(sphere, result);
  6072. var radius = Cartesian2.Cartesian3.magnitude(
  6073. Cartesian2.Cartesian3.subtract(point, result.center, expandScratch)
  6074. );
  6075. if (radius > result.radius) {
  6076. result.radius = radius;
  6077. }
  6078. return result;
  6079. };
  6080. /**
  6081. * Determines which side of a plane a sphere is located.
  6082. *
  6083. * @param {BoundingSphere} sphere The bounding sphere to test.
  6084. * @param {Plane} plane The plane to test against.
  6085. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  6086. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  6087. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  6088. * intersects the plane.
  6089. */
  6090. BoundingSphere.intersectPlane = function (sphere, plane) {
  6091. //>>includeStart('debug', pragmas.debug);
  6092. Check.Check.typeOf.object("sphere", sphere);
  6093. Check.Check.typeOf.object("plane", plane);
  6094. //>>includeEnd('debug');
  6095. var center = sphere.center;
  6096. var radius = sphere.radius;
  6097. var normal = plane.normal;
  6098. var distanceToPlane = Cartesian2.Cartesian3.dot(normal, center) + plane.distance;
  6099. if (distanceToPlane < -radius) {
  6100. // The center point is negative side of the plane normal
  6101. return Intersect$1.OUTSIDE;
  6102. } else if (distanceToPlane < radius) {
  6103. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  6104. return Intersect$1.INTERSECTING;
  6105. }
  6106. return Intersect$1.INSIDE;
  6107. };
  6108. /**
  6109. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  6110. *
  6111. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  6112. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  6113. * @param {BoundingSphere} [result] The object onto which to store the result.
  6114. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  6115. */
  6116. BoundingSphere.transform = function (sphere, transform, result) {
  6117. //>>includeStart('debug', pragmas.debug);
  6118. Check.Check.typeOf.object("sphere", sphere);
  6119. Check.Check.typeOf.object("transform", transform);
  6120. //>>includeEnd('debug');
  6121. if (!when.defined(result)) {
  6122. result = new BoundingSphere();
  6123. }
  6124. result.center = Matrix4.multiplyByPoint(
  6125. transform,
  6126. sphere.center,
  6127. result.center
  6128. );
  6129. result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
  6130. return result;
  6131. };
  6132. var distanceSquaredToScratch = new Cartesian2.Cartesian3();
  6133. /**
  6134. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  6135. *
  6136. * @param {BoundingSphere} sphere The sphere.
  6137. * @param {Cartesian3} cartesian The point
  6138. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  6139. *
  6140. * @example
  6141. * // Sort bounding spheres from back to front
  6142. * spheres.sort(function(a, b) {
  6143. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  6144. * });
  6145. */
  6146. BoundingSphere.distanceSquaredTo = function (sphere, cartesian) {
  6147. //>>includeStart('debug', pragmas.debug);
  6148. Check.Check.typeOf.object("sphere", sphere);
  6149. Check.Check.typeOf.object("cartesian", cartesian);
  6150. //>>includeEnd('debug');
  6151. var diff = Cartesian2.Cartesian3.subtract(
  6152. sphere.center,
  6153. cartesian,
  6154. distanceSquaredToScratch
  6155. );
  6156. return Cartesian2.Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
  6157. };
  6158. /**
  6159. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  6160. * The transformation matrix is not verified to have a uniform scale of 1.
  6161. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  6162. *
  6163. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  6164. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  6165. * @param {BoundingSphere} [result] The object onto which to store the result.
  6166. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  6167. *
  6168. * @example
  6169. * var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  6170. * var boundingSphere = new Cesium.BoundingSphere();
  6171. * var newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  6172. */
  6173. BoundingSphere.transformWithoutScale = function (sphere, transform, result) {
  6174. //>>includeStart('debug', pragmas.debug);
  6175. Check.Check.typeOf.object("sphere", sphere);
  6176. Check.Check.typeOf.object("transform", transform);
  6177. //>>includeEnd('debug');
  6178. if (!when.defined(result)) {
  6179. result = new BoundingSphere();
  6180. }
  6181. result.center = Matrix4.multiplyByPoint(
  6182. transform,
  6183. sphere.center,
  6184. result.center
  6185. );
  6186. result.radius = sphere.radius;
  6187. return result;
  6188. };
  6189. var scratchCartesian3 = new Cartesian2.Cartesian3();
  6190. /**
  6191. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  6192. * plus/minus the radius of the bounding sphere.
  6193. * <br>
  6194. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  6195. * closest and farthest planes from position that intersect the bounding sphere.
  6196. *
  6197. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  6198. * @param {Cartesian3} position The position to calculate the distance from.
  6199. * @param {Cartesian3} direction The direction from position.
  6200. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  6201. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  6202. */
  6203. BoundingSphere.computePlaneDistances = function (
  6204. sphere,
  6205. position,
  6206. direction,
  6207. result
  6208. ) {
  6209. //>>includeStart('debug', pragmas.debug);
  6210. Check.Check.typeOf.object("sphere", sphere);
  6211. Check.Check.typeOf.object("position", position);
  6212. Check.Check.typeOf.object("direction", direction);
  6213. //>>includeEnd('debug');
  6214. if (!when.defined(result)) {
  6215. result = new Interval();
  6216. }
  6217. var toCenter = Cartesian2.Cartesian3.subtract(
  6218. sphere.center,
  6219. position,
  6220. scratchCartesian3
  6221. );
  6222. var mag = Cartesian2.Cartesian3.dot(direction, toCenter);
  6223. result.start = mag - sphere.radius;
  6224. result.stop = mag + sphere.radius;
  6225. return result;
  6226. };
  6227. var projectTo2DNormalScratch = new Cartesian2.Cartesian3();
  6228. var projectTo2DEastScratch = new Cartesian2.Cartesian3();
  6229. var projectTo2DNorthScratch = new Cartesian2.Cartesian3();
  6230. var projectTo2DWestScratch = new Cartesian2.Cartesian3();
  6231. var projectTo2DSouthScratch = new Cartesian2.Cartesian3();
  6232. var projectTo2DCartographicScratch = new Cartesian2.Cartographic();
  6233. var projectTo2DPositionsScratch = new Array(8);
  6234. for (var n = 0; n < 8; ++n) {
  6235. projectTo2DPositionsScratch[n] = new Cartesian2.Cartesian3();
  6236. }
  6237. var projectTo2DProjection = new GeographicProjection();
  6238. /**
  6239. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  6240. *
  6241. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  6242. * @param {Object} [projection=GeographicProjection] The projection to 2D.
  6243. * @param {BoundingSphere} [result] The object onto which to store the result.
  6244. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  6245. */
  6246. BoundingSphere.projectTo2D = function (sphere, projection, result) {
  6247. //>>includeStart('debug', pragmas.debug);
  6248. Check.Check.typeOf.object("sphere", sphere);
  6249. //>>includeEnd('debug');
  6250. projection = when.defaultValue(projection, projectTo2DProjection);
  6251. var ellipsoid = projection.ellipsoid;
  6252. var center = sphere.center;
  6253. var radius = sphere.radius;
  6254. var normal;
  6255. if (Cartesian2.Cartesian3.equals(center, Cartesian2.Cartesian3.ZERO)) {
  6256. // Bounding sphere is at the center. The geodetic surface normal is not
  6257. // defined here so pick the x-axis as a fallback.
  6258. normal = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.UNIT_X, projectTo2DNormalScratch);
  6259. } else {
  6260. normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  6261. }
  6262. var east = Cartesian2.Cartesian3.cross(
  6263. Cartesian2.Cartesian3.UNIT_Z,
  6264. normal,
  6265. projectTo2DEastScratch
  6266. );
  6267. Cartesian2.Cartesian3.normalize(east, east);
  6268. var north = Cartesian2.Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  6269. Cartesian2.Cartesian3.normalize(north, north);
  6270. Cartesian2.Cartesian3.multiplyByScalar(normal, radius, normal);
  6271. Cartesian2.Cartesian3.multiplyByScalar(north, radius, north);
  6272. Cartesian2.Cartesian3.multiplyByScalar(east, radius, east);
  6273. var south = Cartesian2.Cartesian3.negate(north, projectTo2DSouthScratch);
  6274. var west = Cartesian2.Cartesian3.negate(east, projectTo2DWestScratch);
  6275. var positions = projectTo2DPositionsScratch;
  6276. // top NE corner
  6277. var corner = positions[0];
  6278. Cartesian2.Cartesian3.add(normal, north, corner);
  6279. Cartesian2.Cartesian3.add(corner, east, corner);
  6280. // top NW corner
  6281. corner = positions[1];
  6282. Cartesian2.Cartesian3.add(normal, north, corner);
  6283. Cartesian2.Cartesian3.add(corner, west, corner);
  6284. // top SW corner
  6285. corner = positions[2];
  6286. Cartesian2.Cartesian3.add(normal, south, corner);
  6287. Cartesian2.Cartesian3.add(corner, west, corner);
  6288. // top SE corner
  6289. corner = positions[3];
  6290. Cartesian2.Cartesian3.add(normal, south, corner);
  6291. Cartesian2.Cartesian3.add(corner, east, corner);
  6292. Cartesian2.Cartesian3.negate(normal, normal);
  6293. // bottom NE corner
  6294. corner = positions[4];
  6295. Cartesian2.Cartesian3.add(normal, north, corner);
  6296. Cartesian2.Cartesian3.add(corner, east, corner);
  6297. // bottom NW corner
  6298. corner = positions[5];
  6299. Cartesian2.Cartesian3.add(normal, north, corner);
  6300. Cartesian2.Cartesian3.add(corner, west, corner);
  6301. // bottom SW corner
  6302. corner = positions[6];
  6303. Cartesian2.Cartesian3.add(normal, south, corner);
  6304. Cartesian2.Cartesian3.add(corner, west, corner);
  6305. // bottom SE corner
  6306. corner = positions[7];
  6307. Cartesian2.Cartesian3.add(normal, south, corner);
  6308. Cartesian2.Cartesian3.add(corner, east, corner);
  6309. var length = positions.length;
  6310. for (var i = 0; i < length; ++i) {
  6311. var position = positions[i];
  6312. Cartesian2.Cartesian3.add(center, position, position);
  6313. var cartographic = ellipsoid.cartesianToCartographic(
  6314. position,
  6315. projectTo2DCartographicScratch
  6316. );
  6317. projection.project(cartographic, position);
  6318. }
  6319. result = BoundingSphere.fromPoints(positions, result);
  6320. // swizzle center components
  6321. center = result.center;
  6322. var x = center.x;
  6323. var y = center.y;
  6324. var z = center.z;
  6325. center.x = z;
  6326. center.y = x;
  6327. center.z = y;
  6328. return result;
  6329. };
  6330. /**
  6331. * Determines whether or not a sphere is hidden from view by the occluder.
  6332. *
  6333. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  6334. * @param {Occluder} occluder The occluder.
  6335. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  6336. */
  6337. BoundingSphere.isOccluded = function (sphere, occluder) {
  6338. //>>includeStart('debug', pragmas.debug);
  6339. Check.Check.typeOf.object("sphere", sphere);
  6340. Check.Check.typeOf.object("occluder", occluder);
  6341. //>>includeEnd('debug');
  6342. return !occluder.isBoundingSphereVisible(sphere);
  6343. };
  6344. /**
  6345. * Compares the provided BoundingSphere componentwise and returns
  6346. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6347. *
  6348. * @param {BoundingSphere} [left] The first BoundingSphere.
  6349. * @param {BoundingSphere} [right] The second BoundingSphere.
  6350. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  6351. */
  6352. BoundingSphere.equals = function (left, right) {
  6353. return (
  6354. left === right ||
  6355. (when.defined(left) &&
  6356. when.defined(right) &&
  6357. Cartesian2.Cartesian3.equals(left.center, right.center) &&
  6358. left.radius === right.radius)
  6359. );
  6360. };
  6361. /**
  6362. * Determines which side of a plane the sphere is located.
  6363. *
  6364. * @param {Plane} plane The plane to test against.
  6365. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  6366. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  6367. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  6368. * intersects the plane.
  6369. */
  6370. BoundingSphere.prototype.intersectPlane = function (plane) {
  6371. return BoundingSphere.intersectPlane(this, plane);
  6372. };
  6373. /**
  6374. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  6375. *
  6376. * @param {Cartesian3} cartesian The point
  6377. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  6378. *
  6379. * @example
  6380. * // Sort bounding spheres from back to front
  6381. * spheres.sort(function(a, b) {
  6382. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  6383. * });
  6384. */
  6385. BoundingSphere.prototype.distanceSquaredTo = function (cartesian) {
  6386. return BoundingSphere.distanceSquaredTo(this, cartesian);
  6387. };
  6388. /**
  6389. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  6390. * plus/minus the radius of the bounding sphere.
  6391. * <br>
  6392. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  6393. * closest and farthest planes from position that intersect the bounding sphere.
  6394. *
  6395. * @param {Cartesian3} position The position to calculate the distance from.
  6396. * @param {Cartesian3} direction The direction from position.
  6397. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  6398. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  6399. */
  6400. BoundingSphere.prototype.computePlaneDistances = function (
  6401. position,
  6402. direction,
  6403. result
  6404. ) {
  6405. return BoundingSphere.computePlaneDistances(
  6406. this,
  6407. position,
  6408. direction,
  6409. result
  6410. );
  6411. };
  6412. /**
  6413. * Determines whether or not a sphere is hidden from view by the occluder.
  6414. *
  6415. * @param {Occluder} occluder The occluder.
  6416. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  6417. */
  6418. BoundingSphere.prototype.isOccluded = function (occluder) {
  6419. return BoundingSphere.isOccluded(this, occluder);
  6420. };
  6421. /**
  6422. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  6423. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6424. *
  6425. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  6426. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  6427. */
  6428. BoundingSphere.prototype.equals = function (right) {
  6429. return BoundingSphere.equals(this, right);
  6430. };
  6431. /**
  6432. * Duplicates this BoundingSphere instance.
  6433. *
  6434. * @param {BoundingSphere} [result] The object onto which to store the result.
  6435. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  6436. */
  6437. BoundingSphere.prototype.clone = function (result) {
  6438. return BoundingSphere.clone(this, result);
  6439. };
  6440. /**
  6441. * Computes the radius of the BoundingSphere.
  6442. * @returns {Number} The radius of the BoundingSphere.
  6443. */
  6444. BoundingSphere.prototype.volume = function () {
  6445. var radius = this.radius;
  6446. return volumeConstant * radius * radius * radius;
  6447. };
  6448. var _supportsFullscreen;
  6449. var _names = {
  6450. requestFullscreen: undefined,
  6451. exitFullscreen: undefined,
  6452. fullscreenEnabled: undefined,
  6453. fullscreenElement: undefined,
  6454. fullscreenchange: undefined,
  6455. fullscreenerror: undefined,
  6456. };
  6457. /**
  6458. * Browser-independent functions for working with the standard fullscreen API.
  6459. *
  6460. * @namespace Fullscreen
  6461. *
  6462. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  6463. */
  6464. var Fullscreen = {};
  6465. Object.defineProperties(Fullscreen, {
  6466. /**
  6467. * The element that is currently fullscreen, if any. To simply check if the
  6468. * browser is in fullscreen mode or not, use {@link Fullscreen#fullscreen}.
  6469. * @memberof Fullscreen
  6470. * @type {Object}
  6471. * @readonly
  6472. */
  6473. element: {
  6474. get: function () {
  6475. if (!Fullscreen.supportsFullscreen()) {
  6476. return undefined;
  6477. }
  6478. return document[_names.fullscreenElement];
  6479. },
  6480. },
  6481. /**
  6482. * The name of the event on the document that is fired when fullscreen is
  6483. * entered or exited. This event name is intended for use with addEventListener.
  6484. * In your event handler, to determine if the browser is in fullscreen mode or not,
  6485. * use {@link Fullscreen#fullscreen}.
  6486. * @memberof Fullscreen
  6487. * @type {String}
  6488. * @readonly
  6489. */
  6490. changeEventName: {
  6491. get: function () {
  6492. if (!Fullscreen.supportsFullscreen()) {
  6493. return undefined;
  6494. }
  6495. return _names.fullscreenchange;
  6496. },
  6497. },
  6498. /**
  6499. * The name of the event that is fired when a fullscreen error
  6500. * occurs. This event name is intended for use with addEventListener.
  6501. * @memberof Fullscreen
  6502. * @type {String}
  6503. * @readonly
  6504. */
  6505. errorEventName: {
  6506. get: function () {
  6507. if (!Fullscreen.supportsFullscreen()) {
  6508. return undefined;
  6509. }
  6510. return _names.fullscreenerror;
  6511. },
  6512. },
  6513. /**
  6514. * Determine whether the browser will allow an element to be made fullscreen, or not.
  6515. * For example, by default, iframes cannot go fullscreen unless the containing page
  6516. * adds an "allowfullscreen" attribute (or prefixed equivalent).
  6517. * @memberof Fullscreen
  6518. * @type {Boolean}
  6519. * @readonly
  6520. */
  6521. enabled: {
  6522. get: function () {
  6523. if (!Fullscreen.supportsFullscreen()) {
  6524. return undefined;
  6525. }
  6526. return document[_names.fullscreenEnabled];
  6527. },
  6528. },
  6529. /**
  6530. * Determines if the browser is currently in fullscreen mode.
  6531. * @memberof Fullscreen
  6532. * @type {Boolean}
  6533. * @readonly
  6534. */
  6535. fullscreen: {
  6536. get: function () {
  6537. if (!Fullscreen.supportsFullscreen()) {
  6538. return undefined;
  6539. }
  6540. return Fullscreen.element !== null;
  6541. },
  6542. },
  6543. });
  6544. /**
  6545. * Detects whether the browser supports the standard fullscreen API.
  6546. *
  6547. * @returns {Boolean} <code>true</code> if the browser supports the standard fullscreen API,
  6548. * <code>false</code> otherwise.
  6549. */
  6550. Fullscreen.supportsFullscreen = function () {
  6551. if (when.defined(_supportsFullscreen)) {
  6552. return _supportsFullscreen;
  6553. }
  6554. _supportsFullscreen = false;
  6555. var body = document.body;
  6556. if (typeof body.requestFullscreen === "function") {
  6557. // go with the unprefixed, standard set of names
  6558. _names.requestFullscreen = "requestFullscreen";
  6559. _names.exitFullscreen = "exitFullscreen";
  6560. _names.fullscreenEnabled = "fullscreenEnabled";
  6561. _names.fullscreenElement = "fullscreenElement";
  6562. _names.fullscreenchange = "fullscreenchange";
  6563. _names.fullscreenerror = "fullscreenerror";
  6564. _supportsFullscreen = true;
  6565. return _supportsFullscreen;
  6566. }
  6567. //check for the correct combination of prefix plus the various names that browsers use
  6568. var prefixes = ["webkit", "moz", "o", "ms", "khtml"];
  6569. var name;
  6570. for (var i = 0, len = prefixes.length; i < len; ++i) {
  6571. var prefix = prefixes[i];
  6572. // casing of Fullscreen differs across browsers
  6573. name = prefix + "RequestFullscreen";
  6574. if (typeof body[name] === "function") {
  6575. _names.requestFullscreen = name;
  6576. _supportsFullscreen = true;
  6577. } else {
  6578. name = prefix + "RequestFullScreen";
  6579. if (typeof body[name] === "function") {
  6580. _names.requestFullscreen = name;
  6581. _supportsFullscreen = true;
  6582. }
  6583. }
  6584. // disagreement about whether it's "exit" as per spec, or "cancel"
  6585. name = prefix + "ExitFullscreen";
  6586. if (typeof document[name] === "function") {
  6587. _names.exitFullscreen = name;
  6588. } else {
  6589. name = prefix + "CancelFullScreen";
  6590. if (typeof document[name] === "function") {
  6591. _names.exitFullscreen = name;
  6592. }
  6593. }
  6594. // casing of Fullscreen differs across browsers
  6595. name = prefix + "FullscreenEnabled";
  6596. if (document[name] !== undefined) {
  6597. _names.fullscreenEnabled = name;
  6598. } else {
  6599. name = prefix + "FullScreenEnabled";
  6600. if (document[name] !== undefined) {
  6601. _names.fullscreenEnabled = name;
  6602. }
  6603. }
  6604. // casing of Fullscreen differs across browsers
  6605. name = prefix + "FullscreenElement";
  6606. if (document[name] !== undefined) {
  6607. _names.fullscreenElement = name;
  6608. } else {
  6609. name = prefix + "FullScreenElement";
  6610. if (document[name] !== undefined) {
  6611. _names.fullscreenElement = name;
  6612. }
  6613. }
  6614. // thankfully, event names are all lowercase per spec
  6615. name = prefix + "fullscreenchange";
  6616. // event names do not have 'on' in the front, but the property on the document does
  6617. if (document["on" + name] !== undefined) {
  6618. //except on IE
  6619. if (prefix === "ms") {
  6620. name = "MSFullscreenChange";
  6621. }
  6622. _names.fullscreenchange = name;
  6623. }
  6624. name = prefix + "fullscreenerror";
  6625. if (document["on" + name] !== undefined) {
  6626. //except on IE
  6627. if (prefix === "ms") {
  6628. name = "MSFullscreenError";
  6629. }
  6630. _names.fullscreenerror = name;
  6631. }
  6632. }
  6633. return _supportsFullscreen;
  6634. };
  6635. /**
  6636. * Asynchronously requests the browser to enter fullscreen mode on the given element.
  6637. * If fullscreen mode is not supported by the browser, does nothing.
  6638. *
  6639. * @param {Object} element The HTML element which will be placed into fullscreen mode.
  6640. * @param {Object} [vrDevice] The HMDVRDevice device.
  6641. *
  6642. * @example
  6643. * // Put the entire page into fullscreen.
  6644. * Cesium.Fullscreen.requestFullscreen(document.body)
  6645. *
  6646. * // Place only the Cesium canvas into fullscreen.
  6647. * Cesium.Fullscreen.requestFullscreen(scene.canvas)
  6648. */
  6649. Fullscreen.requestFullscreen = function (element, vrDevice) {
  6650. if (!Fullscreen.supportsFullscreen()) {
  6651. return;
  6652. }
  6653. element[_names.requestFullscreen]({ vrDisplay: vrDevice });
  6654. };
  6655. /**
  6656. * Asynchronously exits fullscreen mode. If the browser is not currently
  6657. * in fullscreen, or if fullscreen mode is not supported by the browser, does nothing.
  6658. */
  6659. Fullscreen.exitFullscreen = function () {
  6660. if (!Fullscreen.supportsFullscreen()) {
  6661. return;
  6662. }
  6663. document[_names.exitFullscreen]();
  6664. };
  6665. //For unit tests
  6666. Fullscreen._names = _names;
  6667. var theNavigator;
  6668. if (typeof navigator !== "undefined") {
  6669. theNavigator = navigator;
  6670. } else {
  6671. theNavigator = {};
  6672. }
  6673. function extractVersion(versionString) {
  6674. var parts = versionString.split(".");
  6675. for (var i = 0, len = parts.length; i < len; ++i) {
  6676. parts[i] = parseInt(parts[i], 10);
  6677. }
  6678. return parts;
  6679. }
  6680. var isChromeResult;
  6681. var chromeVersionResult;
  6682. function isChrome() {
  6683. if (!when.defined(isChromeResult)) {
  6684. isChromeResult = false;
  6685. // Edge contains Chrome in the user agent too
  6686. if (!isEdge()) {
  6687. var fields = / Chrome\/([\.0-9]+)/.exec(theNavigator.userAgent);
  6688. if (fields !== null) {
  6689. isChromeResult = true;
  6690. chromeVersionResult = extractVersion(fields[1]);
  6691. }
  6692. }
  6693. }
  6694. return isChromeResult;
  6695. }
  6696. function chromeVersion() {
  6697. return isChrome() && chromeVersionResult;
  6698. }
  6699. var isSafariResult;
  6700. var safariVersionResult;
  6701. function isSafari() {
  6702. if (!when.defined(isSafariResult)) {
  6703. isSafariResult = false;
  6704. // Chrome and Edge contain Safari in the user agent too
  6705. if (
  6706. !isChrome() &&
  6707. !isEdge() &&
  6708. / Safari\/[\.0-9]+/.test(theNavigator.userAgent)
  6709. ) {
  6710. var fields = / Version\/([\.0-9]+)/.exec(theNavigator.userAgent);
  6711. if (fields !== null) {
  6712. isSafariResult = true;
  6713. safariVersionResult = extractVersion(fields[1]);
  6714. }
  6715. }
  6716. }
  6717. return isSafariResult;
  6718. }
  6719. function safariVersion() {
  6720. return isSafari() && safariVersionResult;
  6721. }
  6722. var isWebkitResult;
  6723. var webkitVersionResult;
  6724. function isWebkit() {
  6725. if (!when.defined(isWebkitResult)) {
  6726. isWebkitResult = false;
  6727. var fields = / AppleWebKit\/([\.0-9]+)(\+?)/.exec(theNavigator.userAgent);
  6728. if (fields !== null) {
  6729. isWebkitResult = true;
  6730. webkitVersionResult = extractVersion(fields[1]);
  6731. webkitVersionResult.isNightly = !!fields[2];
  6732. }
  6733. }
  6734. return isWebkitResult;
  6735. }
  6736. function webkitVersion() {
  6737. return isWebkit() && webkitVersionResult;
  6738. }
  6739. var isInternetExplorerResult;
  6740. var internetExplorerVersionResult;
  6741. function isInternetExplorer() {
  6742. if (!when.defined(isInternetExplorerResult)) {
  6743. isInternetExplorerResult = false;
  6744. var fields;
  6745. if (theNavigator.appName === "Microsoft Internet Explorer") {
  6746. fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  6747. if (fields !== null) {
  6748. isInternetExplorerResult = true;
  6749. internetExplorerVersionResult = extractVersion(fields[1]);
  6750. }
  6751. } else if (theNavigator.appName === "Netscape") {
  6752. fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(
  6753. theNavigator.userAgent
  6754. );
  6755. if (fields !== null) {
  6756. isInternetExplorerResult = true;
  6757. internetExplorerVersionResult = extractVersion(fields[1]);
  6758. }
  6759. }
  6760. }
  6761. return isInternetExplorerResult;
  6762. }
  6763. function internetExplorerVersion() {
  6764. return isInternetExplorer() && internetExplorerVersionResult;
  6765. }
  6766. var isEdgeResult;
  6767. var edgeVersionResult;
  6768. function isEdge() {
  6769. if (!when.defined(isEdgeResult)) {
  6770. isEdgeResult = false;
  6771. var fields = / Edge\/([\.0-9]+)/.exec(theNavigator.userAgent);
  6772. if (fields !== null) {
  6773. isEdgeResult = true;
  6774. edgeVersionResult = extractVersion(fields[1]);
  6775. }
  6776. }
  6777. return isEdgeResult;
  6778. }
  6779. function edgeVersion() {
  6780. return isEdge() && edgeVersionResult;
  6781. }
  6782. var isFirefoxResult;
  6783. var firefoxVersionResult;
  6784. function isFirefox() {
  6785. if (!when.defined(isFirefoxResult)) {
  6786. isFirefoxResult = false;
  6787. var fields = /Firefox\/([\.0-9]+)/.exec(theNavigator.userAgent);
  6788. if (fields !== null) {
  6789. isFirefoxResult = true;
  6790. firefoxVersionResult = extractVersion(fields[1]);
  6791. }
  6792. }
  6793. return isFirefoxResult;
  6794. }
  6795. var isWindowsResult;
  6796. function isWindows() {
  6797. if (!when.defined(isWindowsResult)) {
  6798. isWindowsResult = /Windows/i.test(theNavigator.appVersion);
  6799. }
  6800. return isWindowsResult;
  6801. }
  6802. function firefoxVersion() {
  6803. return isFirefox() && firefoxVersionResult;
  6804. }
  6805. var hasPointerEvents;
  6806. function supportsPointerEvents() {
  6807. if (!when.defined(hasPointerEvents)) {
  6808. //While navigator.pointerEnabled is deprecated in the W3C specification
  6809. //we still need to use it if it exists in order to support browsers
  6810. //that rely on it, such as the Windows WebBrowser control which defines
  6811. //PointerEvent but sets navigator.pointerEnabled to false.
  6812. //Firefox disabled because of https://github.com/CesiumGS/cesium/issues/6372
  6813. hasPointerEvents =
  6814. !isFirefox() &&
  6815. typeof PointerEvent !== "undefined" &&
  6816. (!when.defined(theNavigator.pointerEnabled) || theNavigator.pointerEnabled);
  6817. }
  6818. return hasPointerEvents;
  6819. }
  6820. var imageRenderingValueResult;
  6821. var supportsImageRenderingPixelatedResult;
  6822. function supportsImageRenderingPixelated() {
  6823. if (!when.defined(supportsImageRenderingPixelatedResult)) {
  6824. var canvas = document.createElement("canvas");
  6825. canvas.setAttribute(
  6826. "style",
  6827. "image-rendering: -moz-crisp-edges;" + "image-rendering: pixelated;"
  6828. );
  6829. //canvas.style.imageRendering will be undefined, null or an empty string on unsupported browsers.
  6830. var tmp = canvas.style.imageRendering;
  6831. supportsImageRenderingPixelatedResult = when.defined(tmp) && tmp !== "";
  6832. if (supportsImageRenderingPixelatedResult) {
  6833. imageRenderingValueResult = tmp;
  6834. }
  6835. }
  6836. return supportsImageRenderingPixelatedResult;
  6837. }
  6838. function imageRenderingValue() {
  6839. return supportsImageRenderingPixelated()
  6840. ? imageRenderingValueResult
  6841. : undefined;
  6842. }
  6843. function supportsWebP() {
  6844. //>>includeStart('debug', pragmas.debug);
  6845. if (!supportsWebP.initialized) {
  6846. throw new Check.DeveloperError(
  6847. "You must call FeatureDetection.supportsWebP.initialize and wait for the promise to resolve before calling FeatureDetection.supportsWebP"
  6848. );
  6849. }
  6850. //>>includeEnd('debug');
  6851. return supportsWebP._result;
  6852. }
  6853. supportsWebP._promise = undefined;
  6854. supportsWebP._result = undefined;
  6855. supportsWebP.initialize = function () {
  6856. // From https://developers.google.com/speed/webp/faq#how_can_i_detect_browser_support_for_webp
  6857. if (when.defined(supportsWebP._promise)) {
  6858. return supportsWebP._promise;
  6859. }
  6860. var supportsWebPDeferred = when.when.defer();
  6861. supportsWebP._promise = supportsWebPDeferred.promise;
  6862. if (isEdge()) {
  6863. // Edge's WebP support with WebGL is incomplete.
  6864. // See bug report: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/19221241/
  6865. supportsWebP._result = false;
  6866. supportsWebPDeferred.resolve(supportsWebP._result);
  6867. return supportsWebPDeferred.promise;
  6868. }
  6869. var image = new Image();
  6870. image.onload = function () {
  6871. supportsWebP._result = image.width > 0 && image.height > 0;
  6872. supportsWebPDeferred.resolve(supportsWebP._result);
  6873. };
  6874. image.onerror = function () {
  6875. supportsWebP._result = false;
  6876. supportsWebPDeferred.resolve(supportsWebP._result);
  6877. };
  6878. image.src =
  6879. "data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA";
  6880. return supportsWebPDeferred.promise;
  6881. };
  6882. Object.defineProperties(supportsWebP, {
  6883. initialized: {
  6884. get: function () {
  6885. return when.defined(supportsWebP._result);
  6886. },
  6887. },
  6888. });
  6889. var typedArrayTypes = [];
  6890. if (typeof ArrayBuffer !== "undefined") {
  6891. typedArrayTypes.push(
  6892. Int8Array,
  6893. Uint8Array,
  6894. Int16Array,
  6895. Uint16Array,
  6896. Int32Array,
  6897. Uint32Array,
  6898. Float32Array,
  6899. Float64Array
  6900. );
  6901. if (typeof Uint8ClampedArray !== "undefined") {
  6902. typedArrayTypes.push(Uint8ClampedArray);
  6903. }
  6904. if (typeof Uint8ClampedArray !== "undefined") {
  6905. typedArrayTypes.push(Uint8ClampedArray);
  6906. }
  6907. }
  6908. /**
  6909. * A set of functions to detect whether the current browser supports
  6910. * various features.
  6911. *
  6912. * @namespace FeatureDetection
  6913. */
  6914. var FeatureDetection = {
  6915. isChrome: isChrome,
  6916. chromeVersion: chromeVersion,
  6917. isSafari: isSafari,
  6918. safariVersion: safariVersion,
  6919. isWebkit: isWebkit,
  6920. webkitVersion: webkitVersion,
  6921. isInternetExplorer: isInternetExplorer,
  6922. internetExplorerVersion: internetExplorerVersion,
  6923. isEdge: isEdge,
  6924. edgeVersion: edgeVersion,
  6925. isFirefox: isFirefox,
  6926. firefoxVersion: firefoxVersion,
  6927. isWindows: isWindows,
  6928. hardwareConcurrency: when.defaultValue(theNavigator.hardwareConcurrency, 3),
  6929. supportsPointerEvents: supportsPointerEvents,
  6930. supportsImageRenderingPixelated: supportsImageRenderingPixelated,
  6931. supportsWebP: supportsWebP,
  6932. imageRenderingValue: imageRenderingValue,
  6933. typedArrayTypes: typedArrayTypes,
  6934. };
  6935. /**
  6936. * Detects whether the current browser supports the full screen standard.
  6937. *
  6938. * @returns {Boolean} true if the browser supports the full screen standard, false if not.
  6939. *
  6940. * @see Fullscreen
  6941. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  6942. */
  6943. FeatureDetection.supportsFullscreen = function () {
  6944. return Fullscreen.supportsFullscreen();
  6945. };
  6946. /**
  6947. * Detects whether the current browser supports typed arrays.
  6948. *
  6949. * @returns {Boolean} true if the browser supports typed arrays, false if not.
  6950. *
  6951. * @see {@link http://www.khronos.org/registry/typedarray/specs/latest/|Typed Array Specification}
  6952. */
  6953. FeatureDetection.supportsTypedArrays = function () {
  6954. return typeof ArrayBuffer !== "undefined";
  6955. };
  6956. /**
  6957. * Detects whether the current browser supports Web Workers.
  6958. *
  6959. * @returns {Boolean} true if the browsers supports Web Workers, false if not.
  6960. *
  6961. * @see {@link http://www.w3.org/TR/workers/}
  6962. */
  6963. FeatureDetection.supportsWebWorkers = function () {
  6964. return typeof Worker !== "undefined";
  6965. };
  6966. /**
  6967. * Detects whether the current browser supports Web Assembly.
  6968. *
  6969. * @returns {Boolean} true if the browsers supports Web Assembly, false if not.
  6970. *
  6971. * @see {@link https://developer.mozilla.org/en-US/docs/WebAssembly}
  6972. */
  6973. FeatureDetection.supportsWebAssembly = function () {
  6974. return typeof WebAssembly !== "undefined" && !FeatureDetection.isEdge();
  6975. };
  6976. /**
  6977. * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
  6978. * @alias Quaternion
  6979. * @constructor
  6980. *
  6981. * @param {Number} [x=0.0] The X component.
  6982. * @param {Number} [y=0.0] The Y component.
  6983. * @param {Number} [z=0.0] The Z component.
  6984. * @param {Number} [w=0.0] The W component.
  6985. *
  6986. * @see PackableForInterpolation
  6987. */
  6988. function Quaternion(x, y, z, w) {
  6989. /**
  6990. * The X component.
  6991. * @type {Number}
  6992. * @default 0.0
  6993. */
  6994. this.x = when.defaultValue(x, 0.0);
  6995. /**
  6996. * The Y component.
  6997. * @type {Number}
  6998. * @default 0.0
  6999. */
  7000. this.y = when.defaultValue(y, 0.0);
  7001. /**
  7002. * The Z component.
  7003. * @type {Number}
  7004. * @default 0.0
  7005. */
  7006. this.z = when.defaultValue(z, 0.0);
  7007. /**
  7008. * The W component.
  7009. * @type {Number}
  7010. * @default 0.0
  7011. */
  7012. this.w = when.defaultValue(w, 0.0);
  7013. }
  7014. var fromAxisAngleScratch = new Cartesian2.Cartesian3();
  7015. /**
  7016. * Computes a quaternion representing a rotation around an axis.
  7017. *
  7018. * @param {Cartesian3} axis The axis of rotation.
  7019. * @param {Number} angle The angle in radians to rotate around the axis.
  7020. * @param {Quaternion} [result] The object onto which to store the result.
  7021. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  7022. */
  7023. Quaternion.fromAxisAngle = function (axis, angle, result) {
  7024. //>>includeStart('debug', pragmas.debug);
  7025. Check.Check.typeOf.object("axis", axis);
  7026. Check.Check.typeOf.number("angle", angle);
  7027. //>>includeEnd('debug');
  7028. var halfAngle = angle / 2.0;
  7029. var s = Math.sin(halfAngle);
  7030. fromAxisAngleScratch = Cartesian2.Cartesian3.normalize(axis, fromAxisAngleScratch);
  7031. var x = fromAxisAngleScratch.x * s;
  7032. var y = fromAxisAngleScratch.y * s;
  7033. var z = fromAxisAngleScratch.z * s;
  7034. var w = Math.cos(halfAngle);
  7035. if (!when.defined(result)) {
  7036. return new Quaternion(x, y, z, w);
  7037. }
  7038. result.x = x;
  7039. result.y = y;
  7040. result.z = z;
  7041. result.w = w;
  7042. return result;
  7043. };
  7044. var fromRotationMatrixNext = [1, 2, 0];
  7045. var fromRotationMatrixQuat = new Array(3);
  7046. /**
  7047. * Computes a Quaternion from the provided Matrix3 instance.
  7048. *
  7049. * @param {Matrix3} matrix The rotation matrix.
  7050. * @param {Quaternion} [result] The object onto which to store the result.
  7051. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  7052. *
  7053. * @see Matrix3.fromQuaternion
  7054. */
  7055. Quaternion.fromRotationMatrix = function (matrix, result) {
  7056. //>>includeStart('debug', pragmas.debug);
  7057. Check.Check.typeOf.object("matrix", matrix);
  7058. //>>includeEnd('debug');
  7059. var root;
  7060. var x;
  7061. var y;
  7062. var z;
  7063. var w;
  7064. var m00 = matrix[Matrix3.COLUMN0ROW0];
  7065. var m11 = matrix[Matrix3.COLUMN1ROW1];
  7066. var m22 = matrix[Matrix3.COLUMN2ROW2];
  7067. var trace = m00 + m11 + m22;
  7068. if (trace > 0.0) {
  7069. // |w| > 1/2, may as well choose w > 1/2
  7070. root = Math.sqrt(trace + 1.0); // 2w
  7071. w = 0.5 * root;
  7072. root = 0.5 / root; // 1/(4w)
  7073. x = (matrix[Matrix3.COLUMN1ROW2] - matrix[Matrix3.COLUMN2ROW1]) * root;
  7074. y = (matrix[Matrix3.COLUMN2ROW0] - matrix[Matrix3.COLUMN0ROW2]) * root;
  7075. z = (matrix[Matrix3.COLUMN0ROW1] - matrix[Matrix3.COLUMN1ROW0]) * root;
  7076. } else {
  7077. // |w| <= 1/2
  7078. var next = fromRotationMatrixNext;
  7079. var i = 0;
  7080. if (m11 > m00) {
  7081. i = 1;
  7082. }
  7083. if (m22 > m00 && m22 > m11) {
  7084. i = 2;
  7085. }
  7086. var j = next[i];
  7087. var k = next[j];
  7088. root = Math.sqrt(
  7089. matrix[Matrix3.getElementIndex(i, i)] -
  7090. matrix[Matrix3.getElementIndex(j, j)] -
  7091. matrix[Matrix3.getElementIndex(k, k)] +
  7092. 1.0
  7093. );
  7094. var quat = fromRotationMatrixQuat;
  7095. quat[i] = 0.5 * root;
  7096. root = 0.5 / root;
  7097. w =
  7098. (matrix[Matrix3.getElementIndex(k, j)] -
  7099. matrix[Matrix3.getElementIndex(j, k)]) *
  7100. root;
  7101. quat[j] =
  7102. (matrix[Matrix3.getElementIndex(j, i)] +
  7103. matrix[Matrix3.getElementIndex(i, j)]) *
  7104. root;
  7105. quat[k] =
  7106. (matrix[Matrix3.getElementIndex(k, i)] +
  7107. matrix[Matrix3.getElementIndex(i, k)]) *
  7108. root;
  7109. x = -quat[0];
  7110. y = -quat[1];
  7111. z = -quat[2];
  7112. }
  7113. if (!when.defined(result)) {
  7114. return new Quaternion(x, y, z, w);
  7115. }
  7116. result.x = x;
  7117. result.y = y;
  7118. result.z = z;
  7119. result.w = w;
  7120. return result;
  7121. };
  7122. var scratchHPRQuaternion = new Quaternion();
  7123. var scratchHeadingQuaternion = new Quaternion();
  7124. var scratchPitchQuaternion = new Quaternion();
  7125. var scratchRollQuaternion = new Quaternion();
  7126. /**
  7127. * Computes a rotation from the given heading, pitch and roll angles. Heading is the rotation about the
  7128. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  7129. * the positive x axis.
  7130. *
  7131. * @param {HeadingPitchRoll} headingPitchRoll The rotation expressed as a heading, pitch and roll.
  7132. * @param {Quaternion} [result] The object onto which to store the result.
  7133. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  7134. */
  7135. Quaternion.fromHeadingPitchRoll = function (headingPitchRoll, result) {
  7136. //>>includeStart('debug', pragmas.debug);
  7137. Check.Check.typeOf.object("headingPitchRoll", headingPitchRoll);
  7138. //>>includeEnd('debug');
  7139. scratchRollQuaternion = Quaternion.fromAxisAngle(
  7140. Cartesian2.Cartesian3.UNIT_X,
  7141. headingPitchRoll.roll,
  7142. scratchHPRQuaternion
  7143. );
  7144. scratchPitchQuaternion = Quaternion.fromAxisAngle(
  7145. Cartesian2.Cartesian3.UNIT_Y,
  7146. -headingPitchRoll.pitch,
  7147. result
  7148. );
  7149. result = Quaternion.multiply(
  7150. scratchPitchQuaternion,
  7151. scratchRollQuaternion,
  7152. scratchPitchQuaternion
  7153. );
  7154. scratchHeadingQuaternion = Quaternion.fromAxisAngle(
  7155. Cartesian2.Cartesian3.UNIT_Z,
  7156. -headingPitchRoll.heading,
  7157. scratchHPRQuaternion
  7158. );
  7159. return Quaternion.multiply(scratchHeadingQuaternion, result, result);
  7160. };
  7161. var sampledQuaternionAxis = new Cartesian2.Cartesian3();
  7162. var sampledQuaternionRotation = new Cartesian2.Cartesian3();
  7163. var sampledQuaternionTempQuaternion = new Quaternion();
  7164. var sampledQuaternionQuaternion0 = new Quaternion();
  7165. var sampledQuaternionQuaternion0Conjugate = new Quaternion();
  7166. /**
  7167. * The number of elements used to pack the object into an array.
  7168. * @type {Number}
  7169. */
  7170. Quaternion.packedLength = 4;
  7171. /**
  7172. * Stores the provided instance into the provided array.
  7173. *
  7174. * @param {Quaternion} value The value to pack.
  7175. * @param {Number[]} array The array to pack into.
  7176. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  7177. *
  7178. * @returns {Number[]} The array that was packed into
  7179. */
  7180. Quaternion.pack = function (value, array, startingIndex) {
  7181. //>>includeStart('debug', pragmas.debug);
  7182. Check.Check.typeOf.object("value", value);
  7183. Check.Check.defined("array", array);
  7184. //>>includeEnd('debug');
  7185. startingIndex = when.defaultValue(startingIndex, 0);
  7186. array[startingIndex++] = value.x;
  7187. array[startingIndex++] = value.y;
  7188. array[startingIndex++] = value.z;
  7189. array[startingIndex] = value.w;
  7190. return array;
  7191. };
  7192. /**
  7193. * Retrieves an instance from a packed array.
  7194. *
  7195. * @param {Number[]} array The packed array.
  7196. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  7197. * @param {Quaternion} [result] The object into which to store the result.
  7198. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  7199. */
  7200. Quaternion.unpack = function (array, startingIndex, result) {
  7201. //>>includeStart('debug', pragmas.debug);
  7202. Check.Check.defined("array", array);
  7203. //>>includeEnd('debug');
  7204. startingIndex = when.defaultValue(startingIndex, 0);
  7205. if (!when.defined(result)) {
  7206. result = new Quaternion();
  7207. }
  7208. result.x = array[startingIndex];
  7209. result.y = array[startingIndex + 1];
  7210. result.z = array[startingIndex + 2];
  7211. result.w = array[startingIndex + 3];
  7212. return result;
  7213. };
  7214. /**
  7215. * The number of elements used to store the object into an array in its interpolatable form.
  7216. * @type {Number}
  7217. */
  7218. Quaternion.packedInterpolationLength = 3;
  7219. /**
  7220. * Converts a packed array into a form suitable for interpolation.
  7221. *
  7222. * @param {Number[]} packedArray The packed array.
  7223. * @param {Number} [startingIndex=0] The index of the first element to be converted.
  7224. * @param {Number} [lastIndex=packedArray.length] The index of the last element to be converted.
  7225. * @param {Number[]} [result] The object into which to store the result.
  7226. */
  7227. Quaternion.convertPackedArrayForInterpolation = function (
  7228. packedArray,
  7229. startingIndex,
  7230. lastIndex,
  7231. result
  7232. ) {
  7233. Quaternion.unpack(
  7234. packedArray,
  7235. lastIndex * 4,
  7236. sampledQuaternionQuaternion0Conjugate
  7237. );
  7238. Quaternion.conjugate(
  7239. sampledQuaternionQuaternion0Conjugate,
  7240. sampledQuaternionQuaternion0Conjugate
  7241. );
  7242. for (var i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  7243. var offset = i * 3;
  7244. Quaternion.unpack(
  7245. packedArray,
  7246. (startingIndex + i) * 4,
  7247. sampledQuaternionTempQuaternion
  7248. );
  7249. Quaternion.multiply(
  7250. sampledQuaternionTempQuaternion,
  7251. sampledQuaternionQuaternion0Conjugate,
  7252. sampledQuaternionTempQuaternion
  7253. );
  7254. if (sampledQuaternionTempQuaternion.w < 0) {
  7255. Quaternion.negate(
  7256. sampledQuaternionTempQuaternion,
  7257. sampledQuaternionTempQuaternion
  7258. );
  7259. }
  7260. Quaternion.computeAxis(
  7261. sampledQuaternionTempQuaternion,
  7262. sampledQuaternionAxis
  7263. );
  7264. var angle = Quaternion.computeAngle(sampledQuaternionTempQuaternion);
  7265. if (!when.defined(result)) {
  7266. result = [];
  7267. }
  7268. result[offset] = sampledQuaternionAxis.x * angle;
  7269. result[offset + 1] = sampledQuaternionAxis.y * angle;
  7270. result[offset + 2] = sampledQuaternionAxis.z * angle;
  7271. }
  7272. };
  7273. /**
  7274. * Retrieves an instance from a packed array converted with {@link convertPackedArrayForInterpolation}.
  7275. *
  7276. * @param {Number[]} array The array previously packed for interpolation.
  7277. * @param {Number[]} sourceArray The original packed array.
  7278. * @param {Number} [firstIndex=0] The firstIndex used to convert the array.
  7279. * @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  7280. * @param {Quaternion} [result] The object into which to store the result.
  7281. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  7282. */
  7283. Quaternion.unpackInterpolationResult = function (
  7284. array,
  7285. sourceArray,
  7286. firstIndex,
  7287. lastIndex,
  7288. result
  7289. ) {
  7290. if (!when.defined(result)) {
  7291. result = new Quaternion();
  7292. }
  7293. Cartesian2.Cartesian3.fromArray(array, 0, sampledQuaternionRotation);
  7294. var magnitude = Cartesian2.Cartesian3.magnitude(sampledQuaternionRotation);
  7295. Quaternion.unpack(sourceArray, lastIndex * 4, sampledQuaternionQuaternion0);
  7296. if (magnitude === 0) {
  7297. Quaternion.clone(Quaternion.IDENTITY, sampledQuaternionTempQuaternion);
  7298. } else {
  7299. Quaternion.fromAxisAngle(
  7300. sampledQuaternionRotation,
  7301. magnitude,
  7302. sampledQuaternionTempQuaternion
  7303. );
  7304. }
  7305. return Quaternion.multiply(
  7306. sampledQuaternionTempQuaternion,
  7307. sampledQuaternionQuaternion0,
  7308. result
  7309. );
  7310. };
  7311. /**
  7312. * Duplicates a Quaternion instance.
  7313. *
  7314. * @param {Quaternion} quaternion The quaternion to duplicate.
  7315. * @param {Quaternion} [result] The object onto which to store the result.
  7316. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided. (Returns undefined if quaternion is undefined)
  7317. */
  7318. Quaternion.clone = function (quaternion, result) {
  7319. if (!when.defined(quaternion)) {
  7320. return undefined;
  7321. }
  7322. if (!when.defined(result)) {
  7323. return new Quaternion(
  7324. quaternion.x,
  7325. quaternion.y,
  7326. quaternion.z,
  7327. quaternion.w
  7328. );
  7329. }
  7330. result.x = quaternion.x;
  7331. result.y = quaternion.y;
  7332. result.z = quaternion.z;
  7333. result.w = quaternion.w;
  7334. return result;
  7335. };
  7336. /**
  7337. * Computes the conjugate of the provided quaternion.
  7338. *
  7339. * @param {Quaternion} quaternion The quaternion to conjugate.
  7340. * @param {Quaternion} result The object onto which to store the result.
  7341. * @returns {Quaternion} The modified result parameter.
  7342. */
  7343. Quaternion.conjugate = function (quaternion, result) {
  7344. //>>includeStart('debug', pragmas.debug);
  7345. Check.Check.typeOf.object("quaternion", quaternion);
  7346. Check.Check.typeOf.object("result", result);
  7347. //>>includeEnd('debug');
  7348. result.x = -quaternion.x;
  7349. result.y = -quaternion.y;
  7350. result.z = -quaternion.z;
  7351. result.w = quaternion.w;
  7352. return result;
  7353. };
  7354. /**
  7355. * Computes magnitude squared for the provided quaternion.
  7356. *
  7357. * @param {Quaternion} quaternion The quaternion to conjugate.
  7358. * @returns {Number} The magnitude squared.
  7359. */
  7360. Quaternion.magnitudeSquared = function (quaternion) {
  7361. //>>includeStart('debug', pragmas.debug);
  7362. Check.Check.typeOf.object("quaternion", quaternion);
  7363. //>>includeEnd('debug');
  7364. return (
  7365. quaternion.x * quaternion.x +
  7366. quaternion.y * quaternion.y +
  7367. quaternion.z * quaternion.z +
  7368. quaternion.w * quaternion.w
  7369. );
  7370. };
  7371. /**
  7372. * Computes magnitude for the provided quaternion.
  7373. *
  7374. * @param {Quaternion} quaternion The quaternion to conjugate.
  7375. * @returns {Number} The magnitude.
  7376. */
  7377. Quaternion.magnitude = function (quaternion) {
  7378. return Math.sqrt(Quaternion.magnitudeSquared(quaternion));
  7379. };
  7380. /**
  7381. * Computes the normalized form of the provided quaternion.
  7382. *
  7383. * @param {Quaternion} quaternion The quaternion to normalize.
  7384. * @param {Quaternion} result The object onto which to store the result.
  7385. * @returns {Quaternion} The modified result parameter.
  7386. */
  7387. Quaternion.normalize = function (quaternion, result) {
  7388. //>>includeStart('debug', pragmas.debug);
  7389. Check.Check.typeOf.object("result", result);
  7390. //>>includeEnd('debug');
  7391. var inverseMagnitude = 1.0 / Quaternion.magnitude(quaternion);
  7392. var x = quaternion.x * inverseMagnitude;
  7393. var y = quaternion.y * inverseMagnitude;
  7394. var z = quaternion.z * inverseMagnitude;
  7395. var w = quaternion.w * inverseMagnitude;
  7396. result.x = x;
  7397. result.y = y;
  7398. result.z = z;
  7399. result.w = w;
  7400. return result;
  7401. };
  7402. /**
  7403. * Computes the inverse of the provided quaternion.
  7404. *
  7405. * @param {Quaternion} quaternion The quaternion to normalize.
  7406. * @param {Quaternion} result The object onto which to store the result.
  7407. * @returns {Quaternion} The modified result parameter.
  7408. */
  7409. Quaternion.inverse = function (quaternion, result) {
  7410. //>>includeStart('debug', pragmas.debug);
  7411. Check.Check.typeOf.object("result", result);
  7412. //>>includeEnd('debug');
  7413. var magnitudeSquared = Quaternion.magnitudeSquared(quaternion);
  7414. result = Quaternion.conjugate(quaternion, result);
  7415. return Quaternion.multiplyByScalar(result, 1.0 / magnitudeSquared, result);
  7416. };
  7417. /**
  7418. * Computes the componentwise sum of two quaternions.
  7419. *
  7420. * @param {Quaternion} left The first quaternion.
  7421. * @param {Quaternion} right The second quaternion.
  7422. * @param {Quaternion} result The object onto which to store the result.
  7423. * @returns {Quaternion} The modified result parameter.
  7424. */
  7425. Quaternion.add = function (left, right, result) {
  7426. //>>includeStart('debug', pragmas.debug);
  7427. Check.Check.typeOf.object("left", left);
  7428. Check.Check.typeOf.object("right", right);
  7429. Check.Check.typeOf.object("result", result);
  7430. //>>includeEnd('debug');
  7431. result.x = left.x + right.x;
  7432. result.y = left.y + right.y;
  7433. result.z = left.z + right.z;
  7434. result.w = left.w + right.w;
  7435. return result;
  7436. };
  7437. /**
  7438. * Computes the componentwise difference of two quaternions.
  7439. *
  7440. * @param {Quaternion} left The first quaternion.
  7441. * @param {Quaternion} right The second quaternion.
  7442. * @param {Quaternion} result The object onto which to store the result.
  7443. * @returns {Quaternion} The modified result parameter.
  7444. */
  7445. Quaternion.subtract = function (left, right, result) {
  7446. //>>includeStart('debug', pragmas.debug);
  7447. Check.Check.typeOf.object("left", left);
  7448. Check.Check.typeOf.object("right", right);
  7449. Check.Check.typeOf.object("result", result);
  7450. //>>includeEnd('debug');
  7451. result.x = left.x - right.x;
  7452. result.y = left.y - right.y;
  7453. result.z = left.z - right.z;
  7454. result.w = left.w - right.w;
  7455. return result;
  7456. };
  7457. /**
  7458. * Negates the provided quaternion.
  7459. *
  7460. * @param {Quaternion} quaternion The quaternion to be negated.
  7461. * @param {Quaternion} result The object onto which to store the result.
  7462. * @returns {Quaternion} The modified result parameter.
  7463. */
  7464. Quaternion.negate = function (quaternion, result) {
  7465. //>>includeStart('debug', pragmas.debug);
  7466. Check.Check.typeOf.object("quaternion", quaternion);
  7467. Check.Check.typeOf.object("result", result);
  7468. //>>includeEnd('debug');
  7469. result.x = -quaternion.x;
  7470. result.y = -quaternion.y;
  7471. result.z = -quaternion.z;
  7472. result.w = -quaternion.w;
  7473. return result;
  7474. };
  7475. /**
  7476. * Computes the dot (scalar) product of two quaternions.
  7477. *
  7478. * @param {Quaternion} left The first quaternion.
  7479. * @param {Quaternion} right The second quaternion.
  7480. * @returns {Number} The dot product.
  7481. */
  7482. Quaternion.dot = function (left, right) {
  7483. //>>includeStart('debug', pragmas.debug);
  7484. Check.Check.typeOf.object("left", left);
  7485. Check.Check.typeOf.object("right", right);
  7486. //>>includeEnd('debug');
  7487. return (
  7488. left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w
  7489. );
  7490. };
  7491. /**
  7492. * Computes the product of two quaternions.
  7493. *
  7494. * @param {Quaternion} left The first quaternion.
  7495. * @param {Quaternion} right The second quaternion.
  7496. * @param {Quaternion} result The object onto which to store the result.
  7497. * @returns {Quaternion} The modified result parameter.
  7498. */
  7499. Quaternion.multiply = function (left, right, result) {
  7500. //>>includeStart('debug', pragmas.debug);
  7501. Check.Check.typeOf.object("left", left);
  7502. Check.Check.typeOf.object("right", right);
  7503. Check.Check.typeOf.object("result", result);
  7504. //>>includeEnd('debug');
  7505. var leftX = left.x;
  7506. var leftY = left.y;
  7507. var leftZ = left.z;
  7508. var leftW = left.w;
  7509. var rightX = right.x;
  7510. var rightY = right.y;
  7511. var rightZ = right.z;
  7512. var rightW = right.w;
  7513. var x = leftW * rightX + leftX * rightW + leftY * rightZ - leftZ * rightY;
  7514. var y = leftW * rightY - leftX * rightZ + leftY * rightW + leftZ * rightX;
  7515. var z = leftW * rightZ + leftX * rightY - leftY * rightX + leftZ * rightW;
  7516. var w = leftW * rightW - leftX * rightX - leftY * rightY - leftZ * rightZ;
  7517. result.x = x;
  7518. result.y = y;
  7519. result.z = z;
  7520. result.w = w;
  7521. return result;
  7522. };
  7523. /**
  7524. * Multiplies the provided quaternion componentwise by the provided scalar.
  7525. *
  7526. * @param {Quaternion} quaternion The quaternion to be scaled.
  7527. * @param {Number} scalar The scalar to multiply with.
  7528. * @param {Quaternion} result The object onto which to store the result.
  7529. * @returns {Quaternion} The modified result parameter.
  7530. */
  7531. Quaternion.multiplyByScalar = function (quaternion, scalar, result) {
  7532. //>>includeStart('debug', pragmas.debug);
  7533. Check.Check.typeOf.object("quaternion", quaternion);
  7534. Check.Check.typeOf.number("scalar", scalar);
  7535. Check.Check.typeOf.object("result", result);
  7536. //>>includeEnd('debug');
  7537. result.x = quaternion.x * scalar;
  7538. result.y = quaternion.y * scalar;
  7539. result.z = quaternion.z * scalar;
  7540. result.w = quaternion.w * scalar;
  7541. return result;
  7542. };
  7543. /**
  7544. * Divides the provided quaternion componentwise by the provided scalar.
  7545. *
  7546. * @param {Quaternion} quaternion The quaternion to be divided.
  7547. * @param {Number} scalar The scalar to divide by.
  7548. * @param {Quaternion} result The object onto which to store the result.
  7549. * @returns {Quaternion} The modified result parameter.
  7550. */
  7551. Quaternion.divideByScalar = function (quaternion, scalar, result) {
  7552. //>>includeStart('debug', pragmas.debug);
  7553. Check.Check.typeOf.object("quaternion", quaternion);
  7554. Check.Check.typeOf.number("scalar", scalar);
  7555. Check.Check.typeOf.object("result", result);
  7556. //>>includeEnd('debug');
  7557. result.x = quaternion.x / scalar;
  7558. result.y = quaternion.y / scalar;
  7559. result.z = quaternion.z / scalar;
  7560. result.w = quaternion.w / scalar;
  7561. return result;
  7562. };
  7563. /**
  7564. * Computes the axis of rotation of the provided quaternion.
  7565. *
  7566. * @param {Quaternion} quaternion The quaternion to use.
  7567. * @param {Cartesian3} result The object onto which to store the result.
  7568. * @returns {Cartesian3} The modified result parameter.
  7569. */
  7570. Quaternion.computeAxis = function (quaternion, result) {
  7571. //>>includeStart('debug', pragmas.debug);
  7572. Check.Check.typeOf.object("quaternion", quaternion);
  7573. Check.Check.typeOf.object("result", result);
  7574. //>>includeEnd('debug');
  7575. var w = quaternion.w;
  7576. if (Math.abs(w - 1.0) < _Math.CesiumMath.EPSILON6) {
  7577. result.x = result.y = result.z = 0;
  7578. return result;
  7579. }
  7580. var scalar = 1.0 / Math.sqrt(1.0 - w * w);
  7581. result.x = quaternion.x * scalar;
  7582. result.y = quaternion.y * scalar;
  7583. result.z = quaternion.z * scalar;
  7584. return result;
  7585. };
  7586. /**
  7587. * Computes the angle of rotation of the provided quaternion.
  7588. *
  7589. * @param {Quaternion} quaternion The quaternion to use.
  7590. * @returns {Number} The angle of rotation.
  7591. */
  7592. Quaternion.computeAngle = function (quaternion) {
  7593. //>>includeStart('debug', pragmas.debug);
  7594. Check.Check.typeOf.object("quaternion", quaternion);
  7595. //>>includeEnd('debug');
  7596. if (Math.abs(quaternion.w - 1.0) < _Math.CesiumMath.EPSILON6) {
  7597. return 0.0;
  7598. }
  7599. return 2.0 * Math.acos(quaternion.w);
  7600. };
  7601. var lerpScratch$1 = new Quaternion();
  7602. /**
  7603. * Computes the linear interpolation or extrapolation at t using the provided quaternions.
  7604. *
  7605. * @param {Quaternion} start The value corresponding to t at 0.0.
  7606. * @param {Quaternion} end The value corresponding to t at 1.0.
  7607. * @param {Number} t The point along t at which to interpolate.
  7608. * @param {Quaternion} result The object onto which to store the result.
  7609. * @returns {Quaternion} The modified result parameter.
  7610. */
  7611. Quaternion.lerp = function (start, end, t, result) {
  7612. //>>includeStart('debug', pragmas.debug);
  7613. Check.Check.typeOf.object("start", start);
  7614. Check.Check.typeOf.object("end", end);
  7615. Check.Check.typeOf.number("t", t);
  7616. Check.Check.typeOf.object("result", result);
  7617. //>>includeEnd('debug');
  7618. lerpScratch$1 = Quaternion.multiplyByScalar(end, t, lerpScratch$1);
  7619. result = Quaternion.multiplyByScalar(start, 1.0 - t, result);
  7620. return Quaternion.add(lerpScratch$1, result, result);
  7621. };
  7622. var slerpEndNegated = new Quaternion();
  7623. var slerpScaledP = new Quaternion();
  7624. var slerpScaledR = new Quaternion();
  7625. /**
  7626. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  7627. *
  7628. * @param {Quaternion} start The value corresponding to t at 0.0.
  7629. * @param {Quaternion} end The value corresponding to t at 1.0.
  7630. * @param {Number} t The point along t at which to interpolate.
  7631. * @param {Quaternion} result The object onto which to store the result.
  7632. * @returns {Quaternion} The modified result parameter.
  7633. *
  7634. * @see Quaternion#fastSlerp
  7635. */
  7636. Quaternion.slerp = function (start, end, t, result) {
  7637. //>>includeStart('debug', pragmas.debug);
  7638. Check.Check.typeOf.object("start", start);
  7639. Check.Check.typeOf.object("end", end);
  7640. Check.Check.typeOf.number("t", t);
  7641. Check.Check.typeOf.object("result", result);
  7642. //>>includeEnd('debug');
  7643. var dot = Quaternion.dot(start, end);
  7644. // The angle between start must be acute. Since q and -q represent
  7645. // the same rotation, negate q to get the acute angle.
  7646. var r = end;
  7647. if (dot < 0.0) {
  7648. dot = -dot;
  7649. r = slerpEndNegated = Quaternion.negate(end, slerpEndNegated);
  7650. }
  7651. // dot > 0, as the dot product approaches 1, the angle between the
  7652. // quaternions vanishes. use linear interpolation.
  7653. if (1.0 - dot < _Math.CesiumMath.EPSILON6) {
  7654. return Quaternion.lerp(start, r, t, result);
  7655. }
  7656. var theta = Math.acos(dot);
  7657. slerpScaledP = Quaternion.multiplyByScalar(
  7658. start,
  7659. Math.sin((1 - t) * theta),
  7660. slerpScaledP
  7661. );
  7662. slerpScaledR = Quaternion.multiplyByScalar(
  7663. r,
  7664. Math.sin(t * theta),
  7665. slerpScaledR
  7666. );
  7667. result = Quaternion.add(slerpScaledP, slerpScaledR, result);
  7668. return Quaternion.multiplyByScalar(result, 1.0 / Math.sin(theta), result);
  7669. };
  7670. /**
  7671. * The logarithmic quaternion function.
  7672. *
  7673. * @param {Quaternion} quaternion The unit quaternion.
  7674. * @param {Cartesian3} result The object onto which to store the result.
  7675. * @returns {Cartesian3} The modified result parameter.
  7676. */
  7677. Quaternion.log = function (quaternion, result) {
  7678. //>>includeStart('debug', pragmas.debug);
  7679. Check.Check.typeOf.object("quaternion", quaternion);
  7680. Check.Check.typeOf.object("result", result);
  7681. //>>includeEnd('debug');
  7682. var theta = _Math.CesiumMath.acosClamped(quaternion.w);
  7683. var thetaOverSinTheta = 0.0;
  7684. if (theta !== 0.0) {
  7685. thetaOverSinTheta = theta / Math.sin(theta);
  7686. }
  7687. return Cartesian2.Cartesian3.multiplyByScalar(quaternion, thetaOverSinTheta, result);
  7688. };
  7689. /**
  7690. * The exponential quaternion function.
  7691. *
  7692. * @param {Cartesian3} cartesian The cartesian.
  7693. * @param {Quaternion} result The object onto which to store the result.
  7694. * @returns {Quaternion} The modified result parameter.
  7695. */
  7696. Quaternion.exp = function (cartesian, result) {
  7697. //>>includeStart('debug', pragmas.debug);
  7698. Check.Check.typeOf.object("cartesian", cartesian);
  7699. Check.Check.typeOf.object("result", result);
  7700. //>>includeEnd('debug');
  7701. var theta = Cartesian2.Cartesian3.magnitude(cartesian);
  7702. var sinThetaOverTheta = 0.0;
  7703. if (theta !== 0.0) {
  7704. sinThetaOverTheta = Math.sin(theta) / theta;
  7705. }
  7706. result.x = cartesian.x * sinThetaOverTheta;
  7707. result.y = cartesian.y * sinThetaOverTheta;
  7708. result.z = cartesian.z * sinThetaOverTheta;
  7709. result.w = Math.cos(theta);
  7710. return result;
  7711. };
  7712. var squadScratchCartesian0 = new Cartesian2.Cartesian3();
  7713. var squadScratchCartesian1 = new Cartesian2.Cartesian3();
  7714. var squadScratchQuaternion0 = new Quaternion();
  7715. var squadScratchQuaternion1 = new Quaternion();
  7716. /**
  7717. * Computes an inner quadrangle point.
  7718. * <p>This will compute quaternions that ensure a squad curve is C<sup>1</sup>.</p>
  7719. *
  7720. * @param {Quaternion} q0 The first quaternion.
  7721. * @param {Quaternion} q1 The second quaternion.
  7722. * @param {Quaternion} q2 The third quaternion.
  7723. * @param {Quaternion} result The object onto which to store the result.
  7724. * @returns {Quaternion} The modified result parameter.
  7725. *
  7726. * @see Quaternion#squad
  7727. */
  7728. Quaternion.computeInnerQuadrangle = function (q0, q1, q2, result) {
  7729. //>>includeStart('debug', pragmas.debug);
  7730. Check.Check.typeOf.object("q0", q0);
  7731. Check.Check.typeOf.object("q1", q1);
  7732. Check.Check.typeOf.object("q2", q2);
  7733. Check.Check.typeOf.object("result", result);
  7734. //>>includeEnd('debug');
  7735. var qInv = Quaternion.conjugate(q1, squadScratchQuaternion0);
  7736. Quaternion.multiply(qInv, q2, squadScratchQuaternion1);
  7737. var cart0 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian0);
  7738. Quaternion.multiply(qInv, q0, squadScratchQuaternion1);
  7739. var cart1 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian1);
  7740. Cartesian2.Cartesian3.add(cart0, cart1, cart0);
  7741. Cartesian2.Cartesian3.multiplyByScalar(cart0, 0.25, cart0);
  7742. Cartesian2.Cartesian3.negate(cart0, cart0);
  7743. Quaternion.exp(cart0, squadScratchQuaternion0);
  7744. return Quaternion.multiply(q1, squadScratchQuaternion0, result);
  7745. };
  7746. /**
  7747. * Computes the spherical quadrangle interpolation between quaternions.
  7748. *
  7749. * @param {Quaternion} q0 The first quaternion.
  7750. * @param {Quaternion} q1 The second quaternion.
  7751. * @param {Quaternion} s0 The first inner quadrangle.
  7752. * @param {Quaternion} s1 The second inner quadrangle.
  7753. * @param {Number} t The time in [0,1] used to interpolate.
  7754. * @param {Quaternion} result The object onto which to store the result.
  7755. * @returns {Quaternion} The modified result parameter.
  7756. *
  7757. *
  7758. * @example
  7759. * // 1. compute the squad interpolation between two quaternions on a curve
  7760. * var s0 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i - 1], quaternions[i], quaternions[i + 1], new Cesium.Quaternion());
  7761. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i], quaternions[i + 1], quaternions[i + 2], new Cesium.Quaternion());
  7762. * var q = Cesium.Quaternion.squad(quaternions[i], quaternions[i + 1], s0, s1, t, new Cesium.Quaternion());
  7763. *
  7764. * // 2. compute the squad interpolation as above but where the first quaternion is a end point.
  7765. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[0], quaternions[1], quaternions[2], new Cesium.Quaternion());
  7766. * var q = Cesium.Quaternion.squad(quaternions[0], quaternions[1], quaternions[0], s1, t, new Cesium.Quaternion());
  7767. *
  7768. * @see Quaternion#computeInnerQuadrangle
  7769. */
  7770. Quaternion.squad = function (q0, q1, s0, s1, t, result) {
  7771. //>>includeStart('debug', pragmas.debug);
  7772. Check.Check.typeOf.object("q0", q0);
  7773. Check.Check.typeOf.object("q1", q1);
  7774. Check.Check.typeOf.object("s0", s0);
  7775. Check.Check.typeOf.object("s1", s1);
  7776. Check.Check.typeOf.number("t", t);
  7777. Check.Check.typeOf.object("result", result);
  7778. //>>includeEnd('debug');
  7779. var slerp0 = Quaternion.slerp(q0, q1, t, squadScratchQuaternion0);
  7780. var slerp1 = Quaternion.slerp(s0, s1, t, squadScratchQuaternion1);
  7781. return Quaternion.slerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  7782. };
  7783. var fastSlerpScratchQuaternion = new Quaternion();
  7784. var opmu = 1.90110745351730037;
  7785. var u = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  7786. var v = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  7787. var bT = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  7788. var bD = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  7789. for (var i = 0; i < 7; ++i) {
  7790. var s = i + 1.0;
  7791. var t = 2.0 * s + 1.0;
  7792. u[i] = 1.0 / (s * t);
  7793. v[i] = s / t;
  7794. }
  7795. u[7] = opmu / (8.0 * 17.0);
  7796. v[7] = (opmu * 8.0) / 17.0;
  7797. /**
  7798. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  7799. * This implementation is faster than {@link Quaternion#slerp}, but is only accurate up to 10<sup>-6</sup>.
  7800. *
  7801. * @param {Quaternion} start The value corresponding to t at 0.0.
  7802. * @param {Quaternion} end The value corresponding to t at 1.0.
  7803. * @param {Number} t The point along t at which to interpolate.
  7804. * @param {Quaternion} result The object onto which to store the result.
  7805. * @returns {Quaternion} The modified result parameter.
  7806. *
  7807. * @see Quaternion#slerp
  7808. */
  7809. Quaternion.fastSlerp = function (start, end, t, result) {
  7810. //>>includeStart('debug', pragmas.debug);
  7811. Check.Check.typeOf.object("start", start);
  7812. Check.Check.typeOf.object("end", end);
  7813. Check.Check.typeOf.number("t", t);
  7814. Check.Check.typeOf.object("result", result);
  7815. //>>includeEnd('debug');
  7816. var x = Quaternion.dot(start, end);
  7817. var sign;
  7818. if (x >= 0) {
  7819. sign = 1.0;
  7820. } else {
  7821. sign = -1.0;
  7822. x = -x;
  7823. }
  7824. var xm1 = x - 1.0;
  7825. var d = 1.0 - t;
  7826. var sqrT = t * t;
  7827. var sqrD = d * d;
  7828. for (var i = 7; i >= 0; --i) {
  7829. bT[i] = (u[i] * sqrT - v[i]) * xm1;
  7830. bD[i] = (u[i] * sqrD - v[i]) * xm1;
  7831. }
  7832. var cT =
  7833. sign *
  7834. t *
  7835. (1.0 +
  7836. bT[0] *
  7837. (1.0 +
  7838. bT[1] *
  7839. (1.0 +
  7840. bT[2] *
  7841. (1.0 +
  7842. bT[3] *
  7843. (1.0 +
  7844. bT[4] *
  7845. (1.0 + bT[5] * (1.0 + bT[6] * (1.0 + bT[7]))))))));
  7846. var cD =
  7847. d *
  7848. (1.0 +
  7849. bD[0] *
  7850. (1.0 +
  7851. bD[1] *
  7852. (1.0 +
  7853. bD[2] *
  7854. (1.0 +
  7855. bD[3] *
  7856. (1.0 +
  7857. bD[4] *
  7858. (1.0 + bD[5] * (1.0 + bD[6] * (1.0 + bD[7]))))))));
  7859. var temp = Quaternion.multiplyByScalar(start, cD, fastSlerpScratchQuaternion);
  7860. Quaternion.multiplyByScalar(end, cT, result);
  7861. return Quaternion.add(temp, result, result);
  7862. };
  7863. /**
  7864. * Computes the spherical quadrangle interpolation between quaternions.
  7865. * An implementation that is faster than {@link Quaternion#squad}, but less accurate.
  7866. *
  7867. * @param {Quaternion} q0 The first quaternion.
  7868. * @param {Quaternion} q1 The second quaternion.
  7869. * @param {Quaternion} s0 The first inner quadrangle.
  7870. * @param {Quaternion} s1 The second inner quadrangle.
  7871. * @param {Number} t The time in [0,1] used to interpolate.
  7872. * @param {Quaternion} result The object onto which to store the result.
  7873. * @returns {Quaternion} The modified result parameter or a new instance if none was provided.
  7874. *
  7875. * @see Quaternion#squad
  7876. */
  7877. Quaternion.fastSquad = function (q0, q1, s0, s1, t, result) {
  7878. //>>includeStart('debug', pragmas.debug);
  7879. Check.Check.typeOf.object("q0", q0);
  7880. Check.Check.typeOf.object("q1", q1);
  7881. Check.Check.typeOf.object("s0", s0);
  7882. Check.Check.typeOf.object("s1", s1);
  7883. Check.Check.typeOf.number("t", t);
  7884. Check.Check.typeOf.object("result", result);
  7885. //>>includeEnd('debug');
  7886. var slerp0 = Quaternion.fastSlerp(q0, q1, t, squadScratchQuaternion0);
  7887. var slerp1 = Quaternion.fastSlerp(s0, s1, t, squadScratchQuaternion1);
  7888. return Quaternion.fastSlerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  7889. };
  7890. /**
  7891. * Compares the provided quaternions componentwise and returns
  7892. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7893. *
  7894. * @param {Quaternion} [left] The first quaternion.
  7895. * @param {Quaternion} [right] The second quaternion.
  7896. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  7897. */
  7898. Quaternion.equals = function (left, right) {
  7899. return (
  7900. left === right ||
  7901. (when.defined(left) &&
  7902. when.defined(right) &&
  7903. left.x === right.x &&
  7904. left.y === right.y &&
  7905. left.z === right.z &&
  7906. left.w === right.w)
  7907. );
  7908. };
  7909. /**
  7910. * Compares the provided quaternions componentwise and returns
  7911. * <code>true</code> if they are within the provided epsilon,
  7912. * <code>false</code> otherwise.
  7913. *
  7914. * @param {Quaternion} [left] The first quaternion.
  7915. * @param {Quaternion} [right] The second quaternion.
  7916. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7917. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7918. */
  7919. Quaternion.equalsEpsilon = function (left, right, epsilon) {
  7920. epsilon = when.defaultValue(epsilon, 0);
  7921. return (
  7922. left === right ||
  7923. (when.defined(left) &&
  7924. when.defined(right) &&
  7925. Math.abs(left.x - right.x) <= epsilon &&
  7926. Math.abs(left.y - right.y) <= epsilon &&
  7927. Math.abs(left.z - right.z) <= epsilon &&
  7928. Math.abs(left.w - right.w) <= epsilon)
  7929. );
  7930. };
  7931. /**
  7932. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 0.0).
  7933. *
  7934. * @type {Quaternion}
  7935. * @constant
  7936. */
  7937. Quaternion.ZERO = Object.freeze(new Quaternion(0.0, 0.0, 0.0, 0.0));
  7938. /**
  7939. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 1.0).
  7940. *
  7941. * @type {Quaternion}
  7942. * @constant
  7943. */
  7944. Quaternion.IDENTITY = Object.freeze(new Quaternion(0.0, 0.0, 0.0, 1.0));
  7945. /**
  7946. * Duplicates this Quaternion instance.
  7947. *
  7948. * @param {Quaternion} [result] The object onto which to store the result.
  7949. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  7950. */
  7951. Quaternion.prototype.clone = function (result) {
  7952. return Quaternion.clone(this, result);
  7953. };
  7954. /**
  7955. * Compares this and the provided quaternion componentwise and returns
  7956. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7957. *
  7958. * @param {Quaternion} [right] The right hand side quaternion.
  7959. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  7960. */
  7961. Quaternion.prototype.equals = function (right) {
  7962. return Quaternion.equals(this, right);
  7963. };
  7964. /**
  7965. * Compares this and the provided quaternion componentwise and returns
  7966. * <code>true</code> if they are within the provided epsilon,
  7967. * <code>false</code> otherwise.
  7968. *
  7969. * @param {Quaternion} [right] The right hand side quaternion.
  7970. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  7971. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7972. */
  7973. Quaternion.prototype.equalsEpsilon = function (right, epsilon) {
  7974. return Quaternion.equalsEpsilon(this, right, epsilon);
  7975. };
  7976. /**
  7977. * Returns a string representing this quaternion in the format (x, y, z, w).
  7978. *
  7979. * @returns {String} A string representing this Quaternion.
  7980. */
  7981. Quaternion.prototype.toString = function () {
  7982. return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.w + ")";
  7983. };
  7984. /**
  7985. * Finds an item in a sorted array.
  7986. *
  7987. * @function
  7988. * @param {Array} array The sorted array to search.
  7989. * @param {*} itemToFind The item to find in the array.
  7990. * @param {binarySearchComparator} comparator The function to use to compare the item to
  7991. * elements in the array.
  7992. * @returns {Number} The index of <code>itemToFind</code> in the array, if it exists. If <code>itemToFind</code>
  7993. * does not exist, the return value is a negative number which is the bitwise complement (~)
  7994. * of the index before which the itemToFind should be inserted in order to maintain the
  7995. * sorted order of the array.
  7996. *
  7997. * @example
  7998. * // Create a comparator function to search through an array of numbers.
  7999. * function comparator(a, b) {
  8000. * return a - b;
  8001. * };
  8002. * var numbers = [0, 2, 4, 6, 8];
  8003. * var index = Cesium.binarySearch(numbers, 6, comparator); // 3
  8004. */
  8005. function binarySearch(array, itemToFind, comparator) {
  8006. //>>includeStart('debug', pragmas.debug);
  8007. Check.Check.defined("array", array);
  8008. Check.Check.defined("itemToFind", itemToFind);
  8009. Check.Check.defined("comparator", comparator);
  8010. //>>includeEnd('debug');
  8011. var low = 0;
  8012. var high = array.length - 1;
  8013. var i;
  8014. var comparison;
  8015. while (low <= high) {
  8016. i = ~~((low + high) / 2);
  8017. comparison = comparator(array[i], itemToFind);
  8018. if (comparison < 0) {
  8019. low = i + 1;
  8020. continue;
  8021. }
  8022. if (comparison > 0) {
  8023. high = i - 1;
  8024. continue;
  8025. }
  8026. return i;
  8027. }
  8028. return ~(high + 1);
  8029. }
  8030. /**
  8031. * A set of Earth Orientation Parameters (EOP) sampled at a time.
  8032. *
  8033. * @alias EarthOrientationParametersSample
  8034. * @constructor
  8035. *
  8036. * @param {Number} xPoleWander The pole wander about the X axis, in radians.
  8037. * @param {Number} yPoleWander The pole wander about the Y axis, in radians.
  8038. * @param {Number} xPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  8039. * @param {Number} yPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  8040. * @param {Number} ut1MinusUtc The difference in time standards, UT1 - UTC, in seconds.
  8041. *
  8042. * @private
  8043. */
  8044. function EarthOrientationParametersSample(
  8045. xPoleWander,
  8046. yPoleWander,
  8047. xPoleOffset,
  8048. yPoleOffset,
  8049. ut1MinusUtc
  8050. ) {
  8051. /**
  8052. * The pole wander about the X axis, in radians.
  8053. * @type {Number}
  8054. */
  8055. this.xPoleWander = xPoleWander;
  8056. /**
  8057. * The pole wander about the Y axis, in radians.
  8058. * @type {Number}
  8059. */
  8060. this.yPoleWander = yPoleWander;
  8061. /**
  8062. * The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  8063. * @type {Number}
  8064. */
  8065. this.xPoleOffset = xPoleOffset;
  8066. /**
  8067. * The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  8068. * @type {Number}
  8069. */
  8070. this.yPoleOffset = yPoleOffset;
  8071. /**
  8072. * The difference in time standards, UT1 - UTC, in seconds.
  8073. * @type {Number}
  8074. */
  8075. this.ut1MinusUtc = ut1MinusUtc;
  8076. }
  8077. /**
  8078. @license
  8079. sprintf.js from the php.js project - https://github.com/kvz/phpjs
  8080. Directly from https://github.com/kvz/phpjs/blob/master/functions/strings/sprintf.js
  8081. php.js is copyright 2012 Kevin van Zonneveld.
  8082. Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
  8083. (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
  8084. (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jack, Jonas
  8085. Raoni Soares Silva (http://www.jsfromhell.com), Philip Peterson, Legaev
  8086. Andrey, Ates Goral (http://magnetiq.com), Alex, Ratheous, Martijn Wieringa,
  8087. Rafa? Kukawski (http://blog.kukawski.pl), lmeyrick
  8088. (https://sourceforge.net/projects/bcmath-js/), Nate, Philippe Baumann,
  8089. Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/), Carlos R.
  8090. L. Rodrigues (http://www.jsfromhell.com), Ash Searle
  8091. (http://hexmen.com/blog/), Jani Hartikainen, travc, Ole Vrijenhoek,
  8092. Erkekjetter, Michael Grier, Rafa? Kukawski (http://kukawski.pl), Johnny
  8093. Mast (http://www.phpvrouwen.nl), T.Wild, d3x,
  8094. http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
  8095. Rafa? Kukawski (http://blog.kukawski.pl/), stag019, pilus, WebDevHobo
  8096. (http://webdevhobo.blogspot.com/), marrtins, GeekFG
  8097. (http://geekfg.blogspot.com), Andrea Giammarchi
  8098. (http://webreflection.blogspot.com), Arpad Ray (mailto:arpad@php.net),
  8099. gorthaur, Paul Smith, Tim de Koning (http://www.kingsquare.nl), Joris, Oleg
  8100. Eremeev, Steve Hilder, majak, gettimeofday, KELAN, Josh Fraser
  8101. (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
  8102. Marc Palau, Martin
  8103. (http://www.erlenwiese.de/), Breaking Par Consulting Inc
  8104. (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
  8105. Chris, Mirek Slugen, saulius, Alfonso Jimenez
  8106. (http://www.alfonsojimenez.com), Diplom@t (http://difane.com/), felix,
  8107. Mailfaker (http://www.weedem.fr/), Tyler Akins (http://rumkin.com), Caio
  8108. Ariede (http://caioariede.com), Robin, Kankrelune
  8109. (http://www.webfaktory.info/), Karol Kowalski, Imgen Tata
  8110. (http://www.myipdf.com/), mdsjack (http://www.mdsjack.bo.it), Dreamer,
  8111. Felix Geisendoerfer (http://www.debuggable.com/felix), Lars Fischer, AJ,
  8112. David, Aman Gupta, Michael White, Public Domain
  8113. (http://www.json.org/json2.js), Steven Levithan
  8114. (http://blog.stevenlevithan.com), Sakimori, Pellentesque Malesuada,
  8115. Thunder.m, Dj (http://phpjs.org/functions/htmlentities:425#comment_134018),
  8116. Steve Clay, David James, Francois, class_exists, nobbler, T. Wild, Itsacon
  8117. (http://www.itsacon.net/), date, Ole Vrijenhoek (http://www.nervous.nl/),
  8118. Fox, Raphael (Ao RUDLER), Marco, noname, Mateusz "loonquawl" Zalega, Frank
  8119. Forte, Arno, ger, mktime, john (http://www.jd-tech.net), Nick Kolosov
  8120. (http://sammy.ru), marc andreu, Scott Cariss, Douglas Crockford
  8121. (http://javascript.crockford.com), madipta, Slawomir Kaniecki,
  8122. ReverseSyntax, Nathan, Alex Wilson, kenneth, Bayron Guevara, Adam Wallner
  8123. (http://web2.bitbaro.hu/), paulo kuong, jmweb, Lincoln Ramsay, djmix,
  8124. Pyerre, Jon Hohle, Thiago Mata (http://thiagomata.blog.com), lmeyrick
  8125. (https://sourceforge.net/projects/bcmath-js/this.), Linuxworld, duncan,
  8126. Gilbert, Sanjoy Roy, Shingo, sankai, Oskar Larsson H?gfeldt
  8127. (http://oskar-lh.name/), Denny Wardhana, 0m3r, Everlasto, Subhasis Deb,
  8128. josh, jd, Pier Paolo Ramon (http://www.mastersoup.com/), P, merabi, Soren
  8129. Hansen, Eugene Bulkin (http://doubleaw.com/), Der Simon
  8130. (http://innerdom.sourceforge.net/), echo is bad, Ozh, XoraX
  8131. (http://www.xorax.info), EdorFaus, JB, J A R, Marc Jansen, Francesco, LH,
  8132. Stoyan Kyosev (http://www.svest.org/), nord_ua, omid
  8133. (http://phpjs.org/functions/380:380#comment_137122), Brad Touesnard, MeEtc
  8134. (http://yass.meetcweb.com), Peter-Paul Koch
  8135. (http://www.quirksmode.org/js/beat.html), Olivier Louvignes
  8136. (http://mg-crea.com/), T0bsn, Tim Wiel, Bryan Elliott, Jalal Berrami,
  8137. Martin, JT, David Randall, Thomas Beaucourt (http://www.webapp.fr), taith,
  8138. vlado houba, Pierre-Luc Paour, Kristof Coomans (SCK-CEN Belgian Nucleair
  8139. Research Centre), Martin Pool, Kirk Strobeck, Rick Waldron, Brant Messenger
  8140. (http://www.brantmessenger.com/), Devan Penner-Woelk, Saulo Vallory, Wagner
  8141. B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
  8142. (http://carrot.org/), Christoph, Daniel Esteban, strftime, Mick@el, rezna,
  8143. Simon Willison (http://simonwillison.net), Anton Ongson, Gabriel Paderni,
  8144. Marco van Oort, penutbutterjelly, Philipp Lenssen, Bjorn Roesbeke
  8145. (http://www.bjornroesbeke.be/), Bug?, Eric Nagel, Tomasz Wesolowski,
  8146. Evertjan Garretsen, Bobby Drake, Blues (http://tech.bluesmoon.info/), Luke
  8147. Godfrey, Pul, uestla, Alan C, Ulrich, Rafal Kukawski, Yves Sucaet,
  8148. sowberry, Norman "zEh" Fuchs, hitwork, Zahlii, johnrembo, Nick Callen,
  8149. Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Brian Tafoya
  8150. (http://www.premasolutions.com/), Philippe Jausions
  8151. (http://pear.php.net/user/jausions), Aidan Lister
  8152. (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
  8153. strcmp, Taras Bogach, jpfle, Alexander Ermolaev
  8154. (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
  8155. dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
  8156. (http://www.pedrotainha.com), James, Arnout Kazemier
  8157. (http://www.3rd-Eden.com), Chris McMacken, gabriel paderni, Yannoo,
  8158. FGFEmperor, baris ozdil, Tod Gentille, Greg Frazier, jakes, 3D-GRAF, Allan
  8159. Jensen (http://www.winternet.no), Howard Yeend, Benjamin Lupton, davook,
  8160. daniel airton wermann (http://wermann.com.br), Atli T¨®r, Maximusya, Ryan
  8161. W Tenney (http://ryan.10e.us), Alexander M Beedie, fearphage
  8162. (http://http/my.opera.com/fearphage/), Nathan Sepulveda, Victor, Matteo,
  8163. Billy, stensi, Cord, Manish, T.J. Leahy, Riddler
  8164. (http://www.frontierwebdev.com/), Rafa? Kukawski, FremyCompany, Matt
  8165. Bradley, Tim de Koning, Luis Salazar (http://www.freaky-media.com/), Diogo
  8166. Resende, Rival, Andrej Pavlovic, Garagoth, Le Torbi
  8167. (http://www.letorbi.de/), Dino, Josep Sanz (http://www.ws3.es/), rem,
  8168. Russell Walker (http://www.nbill.co.uk/), Jamie Beck
  8169. (http://www.terabit.ca/), setcookie, Michael, YUI Library:
  8170. http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
  8171. http://hacks.bluesmoon.info/strftime/strftime.js, Ben
  8172. (http://benblume.co.uk/), DtTvB
  8173. (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Andreas, William,
  8174. meo, incidence, Cagri Ekin, Amirouche, Amir Habibi
  8175. (http://www.residence-mixte.com/), Luke Smith (http://lucassmith.name),
  8176. Kheang Hok Chin (http://www.distantia.ca/), Jay Klehr, Lorenzo Pisani,
  8177. Tony, Yen-Wei Liu, Greenseed, mk.keck, Leslie Hoare, dude, booeyOH, Ben
  8178. Bryan
  8179. Licensed under the MIT (MIT-LICENSE.txt) license.
  8180. Permission is hereby granted, free of charge, to any person obtaining a
  8181. copy of this software and associated documentation files (the
  8182. "Software"), to deal in the Software without restriction, including
  8183. without limitation the rights to use, copy, modify, merge, publish,
  8184. distribute, sublicense, and/or sell copies of the Software, and to
  8185. permit persons to whom the Software is furnished to do so, subject to
  8186. the following conditions:
  8187. The above copyright notice and this permission notice shall be included
  8188. in all copies or substantial portions of the Software.
  8189. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8190. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  8191. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8192. IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
  8193. OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  8194. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  8195. OTHER DEALINGS IN THE SOFTWARE.
  8196. */
  8197. function sprintf () {
  8198. // http://kevin.vanzonneveld.net
  8199. // + original by: Ash Searle (http://hexmen.com/blog/)
  8200. // + namespaced by: Michael White (http://getsprink.com)
  8201. // + tweaked by: Jack
  8202. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  8203. // + input by: Paulo Freitas
  8204. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  8205. // + input by: Brett Zamir (http://brett-zamir.me)
  8206. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  8207. // + improved by: Dj
  8208. // + improved by: Allidylls
  8209. // * example 1: sprintf("%01.2f", 123.1);
  8210. // * returns 1: 123.10
  8211. // * example 2: sprintf("[%10s]", 'monkey');
  8212. // * returns 2: '[ monkey]'
  8213. // * example 3: sprintf("[%'#10s]", 'monkey');
  8214. // * returns 3: '[####monkey]'
  8215. // * example 4: sprintf("%d", 123456789012345);
  8216. // * returns 4: '123456789012345'
  8217. var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuideEfFgG])/g;
  8218. var a = arguments,
  8219. i = 0,
  8220. format = a[i++];
  8221. // pad()
  8222. var pad = function (str, len, chr, leftJustify) {
  8223. if (!chr) {
  8224. chr = ' ';
  8225. }
  8226. var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
  8227. return leftJustify ? str + padding : padding + str;
  8228. };
  8229. // justify()
  8230. var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
  8231. var diff = minWidth - value.length;
  8232. if (diff > 0) {
  8233. if (leftJustify || !zeroPad) {
  8234. value = pad(value, minWidth, customPadChar, leftJustify);
  8235. } else {
  8236. value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
  8237. }
  8238. }
  8239. return value;
  8240. };
  8241. // formatBaseX()
  8242. var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
  8243. // Note: casts negative numbers to positive ones
  8244. var number = value >>> 0;
  8245. prefix = prefix && number && {
  8246. '2': '0b',
  8247. '8': '0',
  8248. '16': '0x'
  8249. }[base] || '';
  8250. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  8251. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  8252. };
  8253. // formatString()
  8254. var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
  8255. if (precision != null) {
  8256. value = value.slice(0, precision);
  8257. }
  8258. return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
  8259. };
  8260. // doFormat()
  8261. var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
  8262. var number;
  8263. var prefix;
  8264. var method;
  8265. var textTransform;
  8266. var value;
  8267. if (substring == '%%') {
  8268. return '%';
  8269. }
  8270. // parse flags
  8271. var leftJustify = false,
  8272. positivePrefix = '',
  8273. zeroPad = false,
  8274. prefixBaseX = false,
  8275. customPadChar = ' ';
  8276. var flagsl = flags.length;
  8277. for (var j = 0; flags && j < flagsl; j++) {
  8278. switch (flags.charAt(j)) {
  8279. case ' ':
  8280. positivePrefix = ' ';
  8281. break;
  8282. case '+':
  8283. positivePrefix = '+';
  8284. break;
  8285. case '-':
  8286. leftJustify = true;
  8287. break;
  8288. case "'":
  8289. customPadChar = flags.charAt(j + 1);
  8290. break;
  8291. case '0':
  8292. zeroPad = true;
  8293. break;
  8294. case '#':
  8295. prefixBaseX = true;
  8296. break;
  8297. }
  8298. }
  8299. // parameters may be null, undefined, empty-string or real valued
  8300. // we want to ignore null, undefined and empty-string values
  8301. if (!minWidth) {
  8302. minWidth = 0;
  8303. } else if (minWidth == '*') {
  8304. minWidth = +a[i++];
  8305. } else if (minWidth.charAt(0) == '*') {
  8306. minWidth = +a[minWidth.slice(1, -1)];
  8307. } else {
  8308. minWidth = +minWidth;
  8309. }
  8310. // Note: undocumented perl feature:
  8311. if (minWidth < 0) {
  8312. minWidth = -minWidth;
  8313. leftJustify = true;
  8314. }
  8315. if (!isFinite(minWidth)) {
  8316. throw new Error('sprintf: (minimum-)width must be finite');
  8317. }
  8318. if (!precision) {
  8319. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
  8320. } else if (precision == '*') {
  8321. precision = +a[i++];
  8322. } else if (precision.charAt(0) == '*') {
  8323. precision = +a[precision.slice(1, -1)];
  8324. } else {
  8325. precision = +precision;
  8326. }
  8327. // grab value using valueIndex if required?
  8328. value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  8329. switch (type) {
  8330. case 's':
  8331. return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
  8332. case 'c':
  8333. return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
  8334. case 'b':
  8335. return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  8336. case 'o':
  8337. return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  8338. case 'x':
  8339. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  8340. case 'X':
  8341. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
  8342. case 'u':
  8343. return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  8344. case 'i':
  8345. case 'd':
  8346. number = +value || 0;
  8347. number = Math.round(number - number % 1); // Plain Math.round doesn't just truncate
  8348. prefix = number < 0 ? '-' : positivePrefix;
  8349. value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  8350. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  8351. case 'e':
  8352. case 'E':
  8353. case 'f': // Should handle locales (as per setlocale)
  8354. case 'F':
  8355. case 'g':
  8356. case 'G':
  8357. number = +value;
  8358. prefix = number < 0 ? '-' : positivePrefix;
  8359. method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  8360. textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  8361. value = prefix + Math.abs(number)[method](precision);
  8362. return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
  8363. default:
  8364. return substring;
  8365. }
  8366. };
  8367. return format.replace(regex, doFormat);
  8368. }
  8369. /**
  8370. * Represents a Gregorian date in a more precise format than the JavaScript Date object.
  8371. * In addition to submillisecond precision, this object can also represent leap seconds.
  8372. * @alias GregorianDate
  8373. * @constructor
  8374. *
  8375. * @param {Number} [year] The year as a whole number.
  8376. * @param {Number} [month] The month as a whole number with range [1, 12].
  8377. * @param {Number} [day] The day of the month as a whole number starting at 1.
  8378. * @param {Number} [hour] The hour as a whole number with range [0, 23].
  8379. * @param {Number} [minute] The minute of the hour as a whole number with range [0, 59].
  8380. * @param {Number} [second] The second of the minute as a whole number with range [0, 60], with 60 representing a leap second.
  8381. * @param {Number} [millisecond] The millisecond of the second as a floating point number with range [0.0, 1000.0).
  8382. * @param {Boolean} [isLeapSecond] Whether this time is during a leap second.
  8383. *
  8384. * @see JulianDate#toGregorianDate
  8385. */
  8386. function GregorianDate(
  8387. year,
  8388. month,
  8389. day,
  8390. hour,
  8391. minute,
  8392. second,
  8393. millisecond,
  8394. isLeapSecond
  8395. ) {
  8396. /**
  8397. * Gets or sets the year as a whole number.
  8398. * @type {Number}
  8399. */
  8400. this.year = year;
  8401. /**
  8402. * Gets or sets the month as a whole number with range [1, 12].
  8403. * @type {Number}
  8404. */
  8405. this.month = month;
  8406. /**
  8407. * Gets or sets the day of the month as a whole number starting at 1.
  8408. * @type {Number}
  8409. */
  8410. this.day = day;
  8411. /**
  8412. * Gets or sets the hour as a whole number with range [0, 23].
  8413. * @type {Number}
  8414. */
  8415. this.hour = hour;
  8416. /**
  8417. * Gets or sets the minute of the hour as a whole number with range [0, 59].
  8418. * @type {Number}
  8419. */
  8420. this.minute = minute;
  8421. /**
  8422. * Gets or sets the second of the minute as a whole number with range [0, 60], with 60 representing a leap second.
  8423. * @type {Number}
  8424. */
  8425. this.second = second;
  8426. /**
  8427. * Gets or sets the millisecond of the second as a floating point number with range [0.0, 1000.0).
  8428. * @type {Number}
  8429. */
  8430. this.millisecond = millisecond;
  8431. /**
  8432. * Gets or sets whether this time is during a leap second.
  8433. * @type {Boolean}
  8434. */
  8435. this.isLeapSecond = isLeapSecond;
  8436. }
  8437. /**
  8438. * Determines if a given date is a leap year.
  8439. *
  8440. * @function isLeapYear
  8441. *
  8442. * @param {Number} year The year to be tested.
  8443. * @returns {Boolean} True if <code>year</code> is a leap year.
  8444. *
  8445. * @example
  8446. * var leapYear = Cesium.isLeapYear(2000); // true
  8447. */
  8448. function isLeapYear(year) {
  8449. //>>includeStart('debug', pragmas.debug);
  8450. if (year === null || isNaN(year)) {
  8451. throw new Check.DeveloperError("year is required and must be a number.");
  8452. }
  8453. //>>includeEnd('debug');
  8454. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  8455. }
  8456. /**
  8457. * Describes a single leap second, which is constructed from a {@link JulianDate} and a
  8458. * numerical offset representing the number of seconds TAI is ahead of the UTC time standard.
  8459. * @alias LeapSecond
  8460. * @constructor
  8461. *
  8462. * @param {JulianDate} [date] A Julian date representing the time of the leap second.
  8463. * @param {Number} [offset] The cumulative number of seconds that TAI is ahead of UTC at the provided date.
  8464. */
  8465. function LeapSecond(date, offset) {
  8466. /**
  8467. * Gets or sets the date at which this leap second occurs.
  8468. * @type {JulianDate}
  8469. */
  8470. this.julianDate = date;
  8471. /**
  8472. * Gets or sets the cumulative number of seconds between the UTC and TAI time standards at the time
  8473. * of this leap second.
  8474. * @type {Number}
  8475. */
  8476. this.offset = offset;
  8477. }
  8478. /**
  8479. * Constants for time conversions like those done by {@link JulianDate}.
  8480. *
  8481. * @namespace TimeConstants
  8482. *
  8483. * @see JulianDate
  8484. *
  8485. * @private
  8486. */
  8487. var TimeConstants = {
  8488. /**
  8489. * The number of seconds in one millisecond: <code>0.001</code>
  8490. * @type {Number}
  8491. * @constant
  8492. */
  8493. SECONDS_PER_MILLISECOND: 0.001,
  8494. /**
  8495. * The number of seconds in one minute: <code>60</code>.
  8496. * @type {Number}
  8497. * @constant
  8498. */
  8499. SECONDS_PER_MINUTE: 60.0,
  8500. /**
  8501. * The number of minutes in one hour: <code>60</code>.
  8502. * @type {Number}
  8503. * @constant
  8504. */
  8505. MINUTES_PER_HOUR: 60.0,
  8506. /**
  8507. * The number of hours in one day: <code>24</code>.
  8508. * @type {Number}
  8509. * @constant
  8510. */
  8511. HOURS_PER_DAY: 24.0,
  8512. /**
  8513. * The number of seconds in one hour: <code>3600</code>.
  8514. * @type {Number}
  8515. * @constant
  8516. */
  8517. SECONDS_PER_HOUR: 3600.0,
  8518. /**
  8519. * The number of minutes in one day: <code>1440</code>.
  8520. * @type {Number}
  8521. * @constant
  8522. */
  8523. MINUTES_PER_DAY: 1440.0,
  8524. /**
  8525. * The number of seconds in one day, ignoring leap seconds: <code>86400</code>.
  8526. * @type {Number}
  8527. * @constant
  8528. */
  8529. SECONDS_PER_DAY: 86400.0,
  8530. /**
  8531. * The number of days in one Julian century: <code>36525</code>.
  8532. * @type {Number}
  8533. * @constant
  8534. */
  8535. DAYS_PER_JULIAN_CENTURY: 36525.0,
  8536. /**
  8537. * One trillionth of a second.
  8538. * @type {Number}
  8539. * @constant
  8540. */
  8541. PICOSECOND: 0.000000001,
  8542. /**
  8543. * The number of days to subtract from a Julian date to determine the
  8544. * modified Julian date, which gives the number of days since midnight
  8545. * on November 17, 1858.
  8546. * @type {Number}
  8547. * @constant
  8548. */
  8549. MODIFIED_JULIAN_DATE_DIFFERENCE: 2400000.5,
  8550. };
  8551. var TimeConstants$1 = Object.freeze(TimeConstants);
  8552. /**
  8553. * Provides the type of time standards which JulianDate can take as input.
  8554. *
  8555. * @enum {Number}
  8556. *
  8557. * @see JulianDate
  8558. */
  8559. var TimeStandard = {
  8560. /**
  8561. * Represents the coordinated Universal Time (UTC) time standard.
  8562. *
  8563. * UTC is related to TAI according to the relationship
  8564. * <code>UTC = TAI - deltaT</code> where <code>deltaT</code> is the number of leap
  8565. * seconds which have been introduced as of the time in TAI.
  8566. *
  8567. * @type {Number}
  8568. * @constant
  8569. */
  8570. UTC: 0,
  8571. /**
  8572. * Represents the International Atomic Time (TAI) time standard.
  8573. * TAI is the principal time standard to which the other time standards are related.
  8574. *
  8575. * @type {Number}
  8576. * @constant
  8577. */
  8578. TAI: 1,
  8579. };
  8580. var TimeStandard$1 = Object.freeze(TimeStandard);
  8581. var gregorianDateScratch = new GregorianDate();
  8582. var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  8583. var daysInLeapFeburary = 29;
  8584. function compareLeapSecondDates(leapSecond, dateToFind) {
  8585. return JulianDate.compare(leapSecond.julianDate, dateToFind.julianDate);
  8586. }
  8587. // we don't really need a leap second instance, anything with a julianDate property will do
  8588. var binarySearchScratchLeapSecond = new LeapSecond();
  8589. function convertUtcToTai(julianDate) {
  8590. //Even though julianDate is in UTC, we'll treat it as TAI and
  8591. //search the leap second table for it.
  8592. binarySearchScratchLeapSecond.julianDate = julianDate;
  8593. var leapSeconds = JulianDate.leapSeconds;
  8594. var index = binarySearch(
  8595. leapSeconds,
  8596. binarySearchScratchLeapSecond,
  8597. compareLeapSecondDates
  8598. );
  8599. if (index < 0) {
  8600. index = ~index;
  8601. }
  8602. if (index >= leapSeconds.length) {
  8603. index = leapSeconds.length - 1;
  8604. }
  8605. var offset = leapSeconds[index].offset;
  8606. if (index > 0) {
  8607. //Now we have the index of the closest leap second that comes on or after our UTC time.
  8608. //However, if the difference between the UTC date being converted and the TAI
  8609. //defined leap second is greater than the offset, we are off by one and need to use
  8610. //the previous leap second.
  8611. var difference = JulianDate.secondsDifference(
  8612. leapSeconds[index].julianDate,
  8613. julianDate
  8614. );
  8615. if (difference > offset) {
  8616. index--;
  8617. offset = leapSeconds[index].offset;
  8618. }
  8619. }
  8620. JulianDate.addSeconds(julianDate, offset, julianDate);
  8621. }
  8622. function convertTaiToUtc(julianDate, result) {
  8623. binarySearchScratchLeapSecond.julianDate = julianDate;
  8624. var leapSeconds = JulianDate.leapSeconds;
  8625. var index = binarySearch(
  8626. leapSeconds,
  8627. binarySearchScratchLeapSecond,
  8628. compareLeapSecondDates
  8629. );
  8630. if (index < 0) {
  8631. index = ~index;
  8632. }
  8633. //All times before our first leap second get the first offset.
  8634. if (index === 0) {
  8635. return JulianDate.addSeconds(julianDate, -leapSeconds[0].offset, result);
  8636. }
  8637. //All times after our leap second get the last offset.
  8638. if (index >= leapSeconds.length) {
  8639. return JulianDate.addSeconds(
  8640. julianDate,
  8641. -leapSeconds[index - 1].offset,
  8642. result
  8643. );
  8644. }
  8645. //Compute the difference between the found leap second and the time we are converting.
  8646. var difference = JulianDate.secondsDifference(
  8647. leapSeconds[index].julianDate,
  8648. julianDate
  8649. );
  8650. if (difference === 0) {
  8651. //The date is in our leap second table.
  8652. return JulianDate.addSeconds(
  8653. julianDate,
  8654. -leapSeconds[index].offset,
  8655. result
  8656. );
  8657. }
  8658. if (difference <= 1.0) {
  8659. //The requested date is during the moment of a leap second, then we cannot convert to UTC
  8660. return undefined;
  8661. }
  8662. //The time is in between two leap seconds, index is the leap second after the date
  8663. //we're converting, so we subtract one to get the correct LeapSecond instance.
  8664. return JulianDate.addSeconds(
  8665. julianDate,
  8666. -leapSeconds[--index].offset,
  8667. result
  8668. );
  8669. }
  8670. function setComponents(wholeDays, secondsOfDay, julianDate) {
  8671. var extraDays = (secondsOfDay / TimeConstants$1.SECONDS_PER_DAY) | 0;
  8672. wholeDays += extraDays;
  8673. secondsOfDay -= TimeConstants$1.SECONDS_PER_DAY * extraDays;
  8674. if (secondsOfDay < 0) {
  8675. wholeDays--;
  8676. secondsOfDay += TimeConstants$1.SECONDS_PER_DAY;
  8677. }
  8678. julianDate.dayNumber = wholeDays;
  8679. julianDate.secondsOfDay = secondsOfDay;
  8680. return julianDate;
  8681. }
  8682. function computeJulianDateComponents(
  8683. year,
  8684. month,
  8685. day,
  8686. hour,
  8687. minute,
  8688. second,
  8689. millisecond
  8690. ) {
  8691. // Algorithm from page 604 of the Explanatory Supplement to the
  8692. // Astronomical Almanac (Seidelmann 1992).
  8693. var a = ((month - 14) / 12) | 0;
  8694. var b = year + 4800 + a;
  8695. var dayNumber =
  8696. (((1461 * b) / 4) | 0) +
  8697. (((367 * (month - 2 - 12 * a)) / 12) | 0) -
  8698. (((3 * (((b + 100) / 100) | 0)) / 4) | 0) +
  8699. day -
  8700. 32075;
  8701. // JulianDates are noon-based
  8702. hour = hour - 12;
  8703. if (hour < 0) {
  8704. hour += 24;
  8705. }
  8706. var secondsOfDay =
  8707. second +
  8708. (hour * TimeConstants$1.SECONDS_PER_HOUR +
  8709. minute * TimeConstants$1.SECONDS_PER_MINUTE +
  8710. millisecond * TimeConstants$1.SECONDS_PER_MILLISECOND);
  8711. if (secondsOfDay >= 43200.0) {
  8712. dayNumber -= 1;
  8713. }
  8714. return [dayNumber, secondsOfDay];
  8715. }
  8716. //Regular expressions used for ISO8601 date parsing.
  8717. //YYYY
  8718. var matchCalendarYear = /^(\d{4})$/;
  8719. //YYYY-MM (YYYYMM is invalid)
  8720. var matchCalendarMonth = /^(\d{4})-(\d{2})$/;
  8721. //YYYY-DDD or YYYYDDD
  8722. var matchOrdinalDate = /^(\d{4})-?(\d{3})$/;
  8723. //YYYY-Www or YYYYWww or YYYY-Www-D or YYYYWwwD
  8724. var matchWeekDate = /^(\d{4})-?W(\d{2})-?(\d{1})?$/;
  8725. //YYYY-MM-DD or YYYYMMDD
  8726. var matchCalendarDate = /^(\d{4})-?(\d{2})-?(\d{2})$/;
  8727. // Match utc offset
  8728. var utcOffset = /([Z+\-])?(\d{2})?:?(\d{2})?$/;
  8729. // Match hours HH or HH.xxxxx
  8730. var matchHours = /^(\d{2})(\.\d+)?/.source + utcOffset.source;
  8731. // Match hours/minutes HH:MM HHMM.xxxxx
  8732. var matchHoursMinutes = /^(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  8733. // Match hours/minutes HH:MM:SS HHMMSS.xxxxx
  8734. var matchHoursMinutesSeconds =
  8735. /^(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  8736. var iso8601ErrorMessage = "Invalid ISO 8601 date.";
  8737. /**
  8738. * Represents an astronomical Julian date, which is the number of days since noon on January 1, -4712 (4713 BC).
  8739. * For increased precision, this class stores the whole number part of the date and the seconds
  8740. * part of the date in separate components. In order to be safe for arithmetic and represent
  8741. * leap seconds, the date is always stored in the International Atomic Time standard
  8742. * {@link TimeStandard.TAI}.
  8743. * @alias JulianDate
  8744. * @constructor
  8745. *
  8746. * @param {Number} [julianDayNumber=0.0] The Julian Day Number representing the number of whole days. Fractional days will also be handled correctly.
  8747. * @param {Number} [secondsOfDay=0.0] The number of seconds into the current Julian Day Number. Fractional seconds, negative seconds and seconds greater than a day will be handled correctly.
  8748. * @param {TimeStandard} [timeStandard=TimeStandard.UTC] The time standard in which the first two parameters are defined.
  8749. */
  8750. function JulianDate(julianDayNumber, secondsOfDay, timeStandard) {
  8751. /**
  8752. * Gets or sets the number of whole days.
  8753. * @type {Number}
  8754. */
  8755. this.dayNumber = undefined;
  8756. /**
  8757. * Gets or sets the number of seconds into the current day.
  8758. * @type {Number}
  8759. */
  8760. this.secondsOfDay = undefined;
  8761. julianDayNumber = when.defaultValue(julianDayNumber, 0.0);
  8762. secondsOfDay = when.defaultValue(secondsOfDay, 0.0);
  8763. timeStandard = when.defaultValue(timeStandard, TimeStandard$1.UTC);
  8764. //If julianDayNumber is fractional, make it an integer and add the number of seconds the fraction represented.
  8765. var wholeDays = julianDayNumber | 0;
  8766. secondsOfDay =
  8767. secondsOfDay +
  8768. (julianDayNumber - wholeDays) * TimeConstants$1.SECONDS_PER_DAY;
  8769. setComponents(wholeDays, secondsOfDay, this);
  8770. if (timeStandard === TimeStandard$1.UTC) {
  8771. convertUtcToTai(this);
  8772. }
  8773. }
  8774. /**
  8775. * Creates a new instance from a GregorianDate.
  8776. *
  8777. * @param {GregorianDate} date A GregorianDate.
  8778. * @param {JulianDate} [result] An existing instance to use for the result.
  8779. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  8780. *
  8781. * @exception {DeveloperError} date must be a valid GregorianDate.
  8782. */
  8783. JulianDate.fromGregorianDate = function (date, result) {
  8784. //>>includeStart('debug', pragmas.debug);
  8785. if (!(date instanceof GregorianDate)) {
  8786. throw new Check.DeveloperError("date must be a valid GregorianDate.");
  8787. }
  8788. //>>includeEnd('debug');
  8789. var components = computeJulianDateComponents(
  8790. date.year,
  8791. date.month,
  8792. date.day,
  8793. date.hour,
  8794. date.minute,
  8795. date.second,
  8796. date.millisecond
  8797. );
  8798. if (!when.defined(result)) {
  8799. return new JulianDate(components[0], components[1], TimeStandard$1.UTC);
  8800. }
  8801. setComponents(components[0], components[1], result);
  8802. convertUtcToTai(result);
  8803. return result;
  8804. };
  8805. /**
  8806. * Creates a new instance from a JavaScript Date.
  8807. *
  8808. * @param {Date} date A JavaScript Date.
  8809. * @param {JulianDate} [result] An existing instance to use for the result.
  8810. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  8811. *
  8812. * @exception {DeveloperError} date must be a valid JavaScript Date.
  8813. */
  8814. JulianDate.fromDate = function (date, result) {
  8815. //>>includeStart('debug', pragmas.debug);
  8816. if (!(date instanceof Date) || isNaN(date.getTime())) {
  8817. throw new Check.DeveloperError("date must be a valid JavaScript Date.");
  8818. }
  8819. //>>includeEnd('debug');
  8820. var components = computeJulianDateComponents(
  8821. date.getUTCFullYear(),
  8822. date.getUTCMonth() + 1,
  8823. date.getUTCDate(),
  8824. date.getUTCHours(),
  8825. date.getUTCMinutes(),
  8826. date.getUTCSeconds(),
  8827. date.getUTCMilliseconds()
  8828. );
  8829. if (!when.defined(result)) {
  8830. return new JulianDate(components[0], components[1], TimeStandard$1.UTC);
  8831. }
  8832. setComponents(components[0], components[1], result);
  8833. convertUtcToTai(result);
  8834. return result;
  8835. };
  8836. /**
  8837. * Creates a new instance from a from an {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} date.
  8838. * This method is superior to <code>Date.parse</code> because it will handle all valid formats defined by the ISO 8601
  8839. * specification, including leap seconds and sub-millisecond times, which discarded by most JavaScript implementations.
  8840. *
  8841. * @param {String} iso8601String An ISO 8601 date.
  8842. * @param {JulianDate} [result] An existing instance to use for the result.
  8843. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  8844. *
  8845. * @exception {DeveloperError} Invalid ISO 8601 date.
  8846. */
  8847. JulianDate.fromIso8601 = function (iso8601String, result) {
  8848. //>>includeStart('debug', pragmas.debug);
  8849. if (typeof iso8601String !== "string") {
  8850. throw new Check.DeveloperError(iso8601ErrorMessage);
  8851. }
  8852. //>>includeEnd('debug');
  8853. //Comma and decimal point both indicate a fractional number according to ISO 8601,
  8854. //start out by blanket replacing , with . which is the only valid such symbol in JS.
  8855. iso8601String = iso8601String.replace(",", ".");
  8856. //Split the string into its date and time components, denoted by a mandatory T
  8857. var tokens = iso8601String.split("T");
  8858. var year;
  8859. var month = 1;
  8860. var day = 1;
  8861. var hour = 0;
  8862. var minute = 0;
  8863. var second = 0;
  8864. var millisecond = 0;
  8865. //Lacking a time is okay, but a missing date is illegal.
  8866. var date = tokens[0];
  8867. var time = tokens[1];
  8868. var tmp;
  8869. var inLeapYear;
  8870. //>>includeStart('debug', pragmas.debug);
  8871. if (!when.defined(date)) {
  8872. throw new Check.DeveloperError(iso8601ErrorMessage);
  8873. }
  8874. var dashCount;
  8875. //>>includeEnd('debug');
  8876. //First match the date against possible regular expressions.
  8877. tokens = date.match(matchCalendarDate);
  8878. if (tokens !== null) {
  8879. //>>includeStart('debug', pragmas.debug);
  8880. dashCount = date.split("-").length - 1;
  8881. if (dashCount > 0 && dashCount !== 2) {
  8882. throw new Check.DeveloperError(iso8601ErrorMessage);
  8883. }
  8884. //>>includeEnd('debug');
  8885. year = +tokens[1];
  8886. month = +tokens[2];
  8887. day = +tokens[3];
  8888. } else {
  8889. tokens = date.match(matchCalendarMonth);
  8890. if (tokens !== null) {
  8891. year = +tokens[1];
  8892. month = +tokens[2];
  8893. } else {
  8894. tokens = date.match(matchCalendarYear);
  8895. if (tokens !== null) {
  8896. year = +tokens[1];
  8897. } else {
  8898. //Not a year/month/day so it must be an ordinal date.
  8899. var dayOfYear;
  8900. tokens = date.match(matchOrdinalDate);
  8901. if (tokens !== null) {
  8902. year = +tokens[1];
  8903. dayOfYear = +tokens[2];
  8904. inLeapYear = isLeapYear(year);
  8905. //This validation is only applicable for this format.
  8906. //>>includeStart('debug', pragmas.debug);
  8907. if (
  8908. dayOfYear < 1 ||
  8909. (inLeapYear && dayOfYear > 366) ||
  8910. (!inLeapYear && dayOfYear > 365)
  8911. ) {
  8912. throw new Check.DeveloperError(iso8601ErrorMessage);
  8913. }
  8914. //>>includeEnd('debug')
  8915. } else {
  8916. tokens = date.match(matchWeekDate);
  8917. if (tokens !== null) {
  8918. //ISO week date to ordinal date from
  8919. //http://en.wikipedia.org/w/index.php?title=ISO_week_date&oldid=474176775
  8920. year = +tokens[1];
  8921. var weekNumber = +tokens[2];
  8922. var dayOfWeek = +tokens[3] || 0;
  8923. //>>includeStart('debug', pragmas.debug);
  8924. dashCount = date.split("-").length - 1;
  8925. if (
  8926. dashCount > 0 &&
  8927. ((!when.defined(tokens[3]) && dashCount !== 1) ||
  8928. (when.defined(tokens[3]) && dashCount !== 2))
  8929. ) {
  8930. throw new Check.DeveloperError(iso8601ErrorMessage);
  8931. }
  8932. //>>includeEnd('debug')
  8933. var january4 = new Date(Date.UTC(year, 0, 4));
  8934. dayOfYear = weekNumber * 7 + dayOfWeek - january4.getUTCDay() - 3;
  8935. } else {
  8936. //None of our regular expressions succeeded in parsing the date properly.
  8937. //>>includeStart('debug', pragmas.debug);
  8938. throw new Check.DeveloperError(iso8601ErrorMessage);
  8939. //>>includeEnd('debug')
  8940. }
  8941. }
  8942. //Split an ordinal date into month/day.
  8943. tmp = new Date(Date.UTC(year, 0, 1));
  8944. tmp.setUTCDate(dayOfYear);
  8945. month = tmp.getUTCMonth() + 1;
  8946. day = tmp.getUTCDate();
  8947. }
  8948. }
  8949. }
  8950. //Now that we have all of the date components, validate them to make sure nothing is out of range.
  8951. inLeapYear = isLeapYear(year);
  8952. //>>includeStart('debug', pragmas.debug);
  8953. if (
  8954. month < 1 ||
  8955. month > 12 ||
  8956. day < 1 ||
  8957. ((month !== 2 || !inLeapYear) && day > daysInMonth[month - 1]) ||
  8958. (inLeapYear && month === 2 && day > daysInLeapFeburary)
  8959. ) {
  8960. throw new Check.DeveloperError(iso8601ErrorMessage);
  8961. }
  8962. //>>includeEnd('debug')
  8963. //Now move onto the time string, which is much simpler.
  8964. //If no time is specified, it is considered the beginning of the day, UTC to match Javascript's implementation.
  8965. var offsetIndex;
  8966. if (when.defined(time)) {
  8967. tokens = time.match(matchHoursMinutesSeconds);
  8968. if (tokens !== null) {
  8969. //>>includeStart('debug', pragmas.debug);
  8970. dashCount = time.split(":").length - 1;
  8971. if (dashCount > 0 && dashCount !== 2 && dashCount !== 3) {
  8972. throw new Check.DeveloperError(iso8601ErrorMessage);
  8973. }
  8974. //>>includeEnd('debug')
  8975. hour = +tokens[1];
  8976. minute = +tokens[2];
  8977. second = +tokens[3];
  8978. millisecond = +(tokens[4] || 0) * 1000.0;
  8979. offsetIndex = 5;
  8980. } else {
  8981. tokens = time.match(matchHoursMinutes);
  8982. if (tokens !== null) {
  8983. //>>includeStart('debug', pragmas.debug);
  8984. dashCount = time.split(":").length - 1;
  8985. if (dashCount > 2) {
  8986. throw new Check.DeveloperError(iso8601ErrorMessage);
  8987. }
  8988. //>>includeEnd('debug')
  8989. hour = +tokens[1];
  8990. minute = +tokens[2];
  8991. second = +(tokens[3] || 0) * 60.0;
  8992. offsetIndex = 4;
  8993. } else {
  8994. tokens = time.match(matchHours);
  8995. if (tokens !== null) {
  8996. hour = +tokens[1];
  8997. minute = +(tokens[2] || 0) * 60.0;
  8998. offsetIndex = 3;
  8999. } else {
  9000. //>>includeStart('debug', pragmas.debug);
  9001. throw new Check.DeveloperError(iso8601ErrorMessage);
  9002. //>>includeEnd('debug')
  9003. }
  9004. }
  9005. }
  9006. //Validate that all values are in proper range. Minutes and hours have special cases at 60 and 24.
  9007. //>>includeStart('debug', pragmas.debug);
  9008. if (
  9009. minute >= 60 ||
  9010. second >= 61 ||
  9011. hour > 24 ||
  9012. (hour === 24 && (minute > 0 || second > 0 || millisecond > 0))
  9013. ) {
  9014. throw new Check.DeveloperError(iso8601ErrorMessage);
  9015. }
  9016. //>>includeEnd('debug');
  9017. //Check the UTC offset value, if no value exists, use local time
  9018. //a Z indicates UTC, + or - are offsets.
  9019. var offset = tokens[offsetIndex];
  9020. var offsetHours = +tokens[offsetIndex + 1];
  9021. var offsetMinutes = +(tokens[offsetIndex + 2] || 0);
  9022. switch (offset) {
  9023. case "+":
  9024. hour = hour - offsetHours;
  9025. minute = minute - offsetMinutes;
  9026. break;
  9027. case "-":
  9028. hour = hour + offsetHours;
  9029. minute = minute + offsetMinutes;
  9030. break;
  9031. case "Z":
  9032. break;
  9033. default:
  9034. minute =
  9035. minute +
  9036. new Date(
  9037. Date.UTC(year, month - 1, day, hour, minute)
  9038. ).getTimezoneOffset();
  9039. break;
  9040. }
  9041. }
  9042. //ISO8601 denotes a leap second by any time having a seconds component of 60 seconds.
  9043. //If that's the case, we need to temporarily subtract a second in order to build a UTC date.
  9044. //Then we add it back in after converting to TAI.
  9045. var isLeapSecond = second === 60;
  9046. if (isLeapSecond) {
  9047. second--;
  9048. }
  9049. //Even if we successfully parsed the string into its components, after applying UTC offset or
  9050. //special cases like 24:00:00 denoting midnight, we need to normalize the data appropriately.
  9051. //milliseconds can never be greater than 1000, and seconds can't be above 60, so we start with minutes
  9052. while (minute >= 60) {
  9053. minute -= 60;
  9054. hour++;
  9055. }
  9056. while (hour >= 24) {
  9057. hour -= 24;
  9058. day++;
  9059. }
  9060. tmp = inLeapYear && month === 2 ? daysInLeapFeburary : daysInMonth[month - 1];
  9061. while (day > tmp) {
  9062. day -= tmp;
  9063. month++;
  9064. if (month > 12) {
  9065. month -= 12;
  9066. year++;
  9067. }
  9068. tmp =
  9069. inLeapYear && month === 2 ? daysInLeapFeburary : daysInMonth[month - 1];
  9070. }
  9071. //If UTC offset is at the beginning/end of the day, minutes can be negative.
  9072. while (minute < 0) {
  9073. minute += 60;
  9074. hour--;
  9075. }
  9076. while (hour < 0) {
  9077. hour += 24;
  9078. day--;
  9079. }
  9080. while (day < 1) {
  9081. month--;
  9082. if (month < 1) {
  9083. month += 12;
  9084. year--;
  9085. }
  9086. tmp =
  9087. inLeapYear && month === 2 ? daysInLeapFeburary : daysInMonth[month - 1];
  9088. day += tmp;
  9089. }
  9090. //Now create the JulianDate components from the Gregorian date and actually create our instance.
  9091. var components = computeJulianDateComponents(
  9092. year,
  9093. month,
  9094. day,
  9095. hour,
  9096. minute,
  9097. second,
  9098. millisecond
  9099. );
  9100. if (!when.defined(result)) {
  9101. result = new JulianDate(components[0], components[1], TimeStandard$1.UTC);
  9102. } else {
  9103. setComponents(components[0], components[1], result);
  9104. convertUtcToTai(result);
  9105. }
  9106. //If we were on a leap second, add it back.
  9107. if (isLeapSecond) {
  9108. JulianDate.addSeconds(result, 1, result);
  9109. }
  9110. return result;
  9111. };
  9112. /**
  9113. * Creates a new instance that represents the current system time.
  9114. * This is equivalent to calling <code>JulianDate.fromDate(new Date());</code>.
  9115. *
  9116. * @param {JulianDate} [result] An existing instance to use for the result.
  9117. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  9118. */
  9119. JulianDate.now = function (result) {
  9120. return JulianDate.fromDate(new Date(), result);
  9121. };
  9122. var toGregorianDateScratch = new JulianDate(0, 0, TimeStandard$1.TAI);
  9123. /**
  9124. * Creates a {@link GregorianDate} from the provided instance.
  9125. *
  9126. * @param {JulianDate} julianDate The date to be converted.
  9127. * @param {GregorianDate} [result] An existing instance to use for the result.
  9128. * @returns {GregorianDate} The modified result parameter or a new instance if none was provided.
  9129. */
  9130. JulianDate.toGregorianDate = function (julianDate, result) {
  9131. //>>includeStart('debug', pragmas.debug);
  9132. if (!when.defined(julianDate)) {
  9133. throw new Check.DeveloperError("julianDate is required.");
  9134. }
  9135. //>>includeEnd('debug');
  9136. var isLeapSecond = false;
  9137. var thisUtc = convertTaiToUtc(julianDate, toGregorianDateScratch);
  9138. if (!when.defined(thisUtc)) {
  9139. //Conversion to UTC will fail if we are during a leap second.
  9140. //If that's the case, subtract a second and convert again.
  9141. //JavaScript doesn't support leap seconds, so this results in second 59 being repeated twice.
  9142. JulianDate.addSeconds(julianDate, -1, toGregorianDateScratch);
  9143. thisUtc = convertTaiToUtc(toGregorianDateScratch, toGregorianDateScratch);
  9144. isLeapSecond = true;
  9145. }
  9146. var julianDayNumber = thisUtc.dayNumber;
  9147. var secondsOfDay = thisUtc.secondsOfDay;
  9148. if (secondsOfDay >= 43200.0) {
  9149. julianDayNumber += 1;
  9150. }
  9151. // Algorithm from page 604 of the Explanatory Supplement to the
  9152. // Astronomical Almanac (Seidelmann 1992).
  9153. var L = (julianDayNumber + 68569) | 0;
  9154. var N = ((4 * L) / 146097) | 0;
  9155. L = (L - (((146097 * N + 3) / 4) | 0)) | 0;
  9156. var I = ((4000 * (L + 1)) / 1461001) | 0;
  9157. L = (L - (((1461 * I) / 4) | 0) + 31) | 0;
  9158. var J = ((80 * L) / 2447) | 0;
  9159. var day = (L - (((2447 * J) / 80) | 0)) | 0;
  9160. L = (J / 11) | 0;
  9161. var month = (J + 2 - 12 * L) | 0;
  9162. var year = (100 * (N - 49) + I + L) | 0;
  9163. var hour = (secondsOfDay / TimeConstants$1.SECONDS_PER_HOUR) | 0;
  9164. var remainingSeconds = secondsOfDay - hour * TimeConstants$1.SECONDS_PER_HOUR;
  9165. var minute = (remainingSeconds / TimeConstants$1.SECONDS_PER_MINUTE) | 0;
  9166. remainingSeconds =
  9167. remainingSeconds - minute * TimeConstants$1.SECONDS_PER_MINUTE;
  9168. var second = remainingSeconds | 0;
  9169. var millisecond =
  9170. (remainingSeconds - second) / TimeConstants$1.SECONDS_PER_MILLISECOND;
  9171. // JulianDates are noon-based
  9172. hour += 12;
  9173. if (hour > 23) {
  9174. hour -= 24;
  9175. }
  9176. //If we were on a leap second, add it back.
  9177. if (isLeapSecond) {
  9178. second += 1;
  9179. }
  9180. if (!when.defined(result)) {
  9181. return new GregorianDate(
  9182. year,
  9183. month,
  9184. day,
  9185. hour,
  9186. minute,
  9187. second,
  9188. millisecond,
  9189. isLeapSecond
  9190. );
  9191. }
  9192. result.year = year;
  9193. result.month = month;
  9194. result.day = day;
  9195. result.hour = hour;
  9196. result.minute = minute;
  9197. result.second = second;
  9198. result.millisecond = millisecond;
  9199. result.isLeapSecond = isLeapSecond;
  9200. return result;
  9201. };
  9202. /**
  9203. * Creates a JavaScript Date from the provided instance.
  9204. * Since JavaScript dates are only accurate to the nearest millisecond and
  9205. * cannot represent a leap second, consider using {@link JulianDate.toGregorianDate} instead.
  9206. * If the provided JulianDate is during a leap second, the previous second is used.
  9207. *
  9208. * @param {JulianDate} julianDate The date to be converted.
  9209. * @returns {Date} A new instance representing the provided date.
  9210. */
  9211. JulianDate.toDate = function (julianDate) {
  9212. //>>includeStart('debug', pragmas.debug);
  9213. if (!when.defined(julianDate)) {
  9214. throw new Check.DeveloperError("julianDate is required.");
  9215. }
  9216. //>>includeEnd('debug');
  9217. var gDate = JulianDate.toGregorianDate(julianDate, gregorianDateScratch);
  9218. var second = gDate.second;
  9219. if (gDate.isLeapSecond) {
  9220. second -= 1;
  9221. }
  9222. return new Date(
  9223. Date.UTC(
  9224. gDate.year,
  9225. gDate.month - 1,
  9226. gDate.day,
  9227. gDate.hour,
  9228. gDate.minute,
  9229. second,
  9230. gDate.millisecond
  9231. )
  9232. );
  9233. };
  9234. /**
  9235. * Creates an ISO8601 representation of the provided date.
  9236. *
  9237. * @param {JulianDate} julianDate The date to be converted.
  9238. * @param {Number} [precision] The number of fractional digits used to represent the seconds component. By default, the most precise representation is used.
  9239. * @returns {String} The ISO8601 representation of the provided date.
  9240. */
  9241. JulianDate.toIso8601 = function (julianDate, precision) {
  9242. //>>includeStart('debug', pragmas.debug);
  9243. if (!when.defined(julianDate)) {
  9244. throw new Check.DeveloperError("julianDate is required.");
  9245. }
  9246. //>>includeEnd('debug');
  9247. var gDate = JulianDate.toGregorianDate(julianDate, gregorianDateScratch);
  9248. var year = gDate.year;
  9249. var month = gDate.month;
  9250. var day = gDate.day;
  9251. var hour = gDate.hour;
  9252. var minute = gDate.minute;
  9253. var second = gDate.second;
  9254. var millisecond = gDate.millisecond;
  9255. // special case - Iso8601.MAXIMUM_VALUE produces a string which we can't parse unless we adjust.
  9256. // 10000-01-01T00:00:00 is the same instant as 9999-12-31T24:00:00
  9257. if (
  9258. year === 10000 &&
  9259. month === 1 &&
  9260. day === 1 &&
  9261. hour === 0 &&
  9262. minute === 0 &&
  9263. second === 0 &&
  9264. millisecond === 0
  9265. ) {
  9266. year = 9999;
  9267. month = 12;
  9268. day = 31;
  9269. hour = 24;
  9270. }
  9271. var millisecondStr;
  9272. if (!when.defined(precision) && millisecond !== 0) {
  9273. //Forces milliseconds into a number with at least 3 digits to whatever the default toString() precision is.
  9274. millisecondStr = (millisecond * 0.01).toString().replace(".", "");
  9275. return sprintf(
  9276. "%04d-%02d-%02dT%02d:%02d:%02d.%sZ",
  9277. year,
  9278. month,
  9279. day,
  9280. hour,
  9281. minute,
  9282. second,
  9283. millisecondStr
  9284. );
  9285. }
  9286. //Precision is either 0 or milliseconds is 0 with undefined precision, in either case, leave off milliseconds entirely
  9287. if (!when.defined(precision) || precision === 0) {
  9288. return sprintf(
  9289. "%04d-%02d-%02dT%02d:%02d:%02dZ",
  9290. year,
  9291. month,
  9292. day,
  9293. hour,
  9294. minute,
  9295. second
  9296. );
  9297. }
  9298. //Forces milliseconds into a number with at least 3 digits to whatever the specified precision is.
  9299. millisecondStr = (millisecond * 0.01)
  9300. .toFixed(precision)
  9301. .replace(".", "")
  9302. .slice(0, precision);
  9303. return sprintf(
  9304. "%04d-%02d-%02dT%02d:%02d:%02d.%sZ",
  9305. year,
  9306. month,
  9307. day,
  9308. hour,
  9309. minute,
  9310. second,
  9311. millisecondStr
  9312. );
  9313. };
  9314. /**
  9315. * Duplicates a JulianDate instance.
  9316. *
  9317. * @param {JulianDate} julianDate The date to duplicate.
  9318. * @param {JulianDate} [result] An existing instance to use for the result.
  9319. * @returns {JulianDate} The modified result parameter or a new instance if none was provided. Returns undefined if julianDate is undefined.
  9320. */
  9321. JulianDate.clone = function (julianDate, result) {
  9322. if (!when.defined(julianDate)) {
  9323. return undefined;
  9324. }
  9325. if (!when.defined(result)) {
  9326. return new JulianDate(
  9327. julianDate.dayNumber,
  9328. julianDate.secondsOfDay,
  9329. TimeStandard$1.TAI
  9330. );
  9331. }
  9332. result.dayNumber = julianDate.dayNumber;
  9333. result.secondsOfDay = julianDate.secondsOfDay;
  9334. return result;
  9335. };
  9336. /**
  9337. * Compares two instances.
  9338. *
  9339. * @param {JulianDate} left The first instance.
  9340. * @param {JulianDate} right The second instance.
  9341. * @returns {Number} A negative value if left is less than right, a positive value if left is greater than right, or zero if left and right are equal.
  9342. */
  9343. JulianDate.compare = function (left, right) {
  9344. //>>includeStart('debug', pragmas.debug);
  9345. if (!when.defined(left)) {
  9346. throw new Check.DeveloperError("left is required.");
  9347. }
  9348. if (!when.defined(right)) {
  9349. throw new Check.DeveloperError("right is required.");
  9350. }
  9351. //>>includeEnd('debug');
  9352. var julianDayNumberDifference = left.dayNumber - right.dayNumber;
  9353. if (julianDayNumberDifference !== 0) {
  9354. return julianDayNumberDifference;
  9355. }
  9356. return left.secondsOfDay - right.secondsOfDay;
  9357. };
  9358. /**
  9359. * Compares two instances and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  9360. *
  9361. * @param {JulianDate} [left] The first instance.
  9362. * @param {JulianDate} [right] The second instance.
  9363. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  9364. */
  9365. JulianDate.equals = function (left, right) {
  9366. return (
  9367. left === right ||
  9368. (when.defined(left) &&
  9369. when.defined(right) &&
  9370. left.dayNumber === right.dayNumber &&
  9371. left.secondsOfDay === right.secondsOfDay)
  9372. );
  9373. };
  9374. /**
  9375. * Compares two instances and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  9376. * each other. That is, in order for the dates to be considered equal (and for
  9377. * this function to return <code>true</code>), the absolute value of the difference between them, in
  9378. * seconds, must be less than <code>epsilon</code>.
  9379. *
  9380. * @param {JulianDate} [left] The first instance.
  9381. * @param {JulianDate} [right] The second instance.
  9382. * @param {Number} [epsilon=0] The maximum number of seconds that should separate the two instances.
  9383. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  9384. */
  9385. JulianDate.equalsEpsilon = function (left, right, epsilon) {
  9386. epsilon = when.defaultValue(epsilon, 0);
  9387. return (
  9388. left === right ||
  9389. (when.defined(left) &&
  9390. when.defined(right) &&
  9391. Math.abs(JulianDate.secondsDifference(left, right)) <= epsilon)
  9392. );
  9393. };
  9394. /**
  9395. * Computes the total number of whole and fractional days represented by the provided instance.
  9396. *
  9397. * @param {JulianDate} julianDate The date.
  9398. * @returns {Number} The Julian date as single floating point number.
  9399. */
  9400. JulianDate.totalDays = function (julianDate) {
  9401. //>>includeStart('debug', pragmas.debug);
  9402. if (!when.defined(julianDate)) {
  9403. throw new Check.DeveloperError("julianDate is required.");
  9404. }
  9405. //>>includeEnd('debug');
  9406. return (
  9407. julianDate.dayNumber +
  9408. julianDate.secondsOfDay / TimeConstants$1.SECONDS_PER_DAY
  9409. );
  9410. };
  9411. /**
  9412. * Computes the difference in seconds between the provided instance.
  9413. *
  9414. * @param {JulianDate} left The first instance.
  9415. * @param {JulianDate} right The second instance.
  9416. * @returns {Number} The difference, in seconds, when subtracting <code>right</code> from <code>left</code>.
  9417. */
  9418. JulianDate.secondsDifference = function (left, right) {
  9419. //>>includeStart('debug', pragmas.debug);
  9420. if (!when.defined(left)) {
  9421. throw new Check.DeveloperError("left is required.");
  9422. }
  9423. if (!when.defined(right)) {
  9424. throw new Check.DeveloperError("right is required.");
  9425. }
  9426. //>>includeEnd('debug');
  9427. var dayDifference =
  9428. (left.dayNumber - right.dayNumber) * TimeConstants$1.SECONDS_PER_DAY;
  9429. return dayDifference + (left.secondsOfDay - right.secondsOfDay);
  9430. };
  9431. /**
  9432. * Computes the difference in days between the provided instance.
  9433. *
  9434. * @param {JulianDate} left The first instance.
  9435. * @param {JulianDate} right The second instance.
  9436. * @returns {Number} The difference, in days, when subtracting <code>right</code> from <code>left</code>.
  9437. */
  9438. JulianDate.daysDifference = function (left, right) {
  9439. //>>includeStart('debug', pragmas.debug);
  9440. if (!when.defined(left)) {
  9441. throw new Check.DeveloperError("left is required.");
  9442. }
  9443. if (!when.defined(right)) {
  9444. throw new Check.DeveloperError("right is required.");
  9445. }
  9446. //>>includeEnd('debug');
  9447. var dayDifference = left.dayNumber - right.dayNumber;
  9448. var secondDifference =
  9449. (left.secondsOfDay - right.secondsOfDay) / TimeConstants$1.SECONDS_PER_DAY;
  9450. return dayDifference + secondDifference;
  9451. };
  9452. /**
  9453. * Computes the number of seconds the provided instance is ahead of UTC.
  9454. *
  9455. * @param {JulianDate} julianDate The date.
  9456. * @returns {Number} The number of seconds the provided instance is ahead of UTC
  9457. */
  9458. JulianDate.computeTaiMinusUtc = function (julianDate) {
  9459. binarySearchScratchLeapSecond.julianDate = julianDate;
  9460. var leapSeconds = JulianDate.leapSeconds;
  9461. var index = binarySearch(
  9462. leapSeconds,
  9463. binarySearchScratchLeapSecond,
  9464. compareLeapSecondDates
  9465. );
  9466. if (index < 0) {
  9467. index = ~index;
  9468. --index;
  9469. if (index < 0) {
  9470. index = 0;
  9471. }
  9472. }
  9473. return leapSeconds[index].offset;
  9474. };
  9475. /**
  9476. * Adds the provided number of seconds to the provided date instance.
  9477. *
  9478. * @param {JulianDate} julianDate The date.
  9479. * @param {Number} seconds The number of seconds to add or subtract.
  9480. * @param {JulianDate} result An existing instance to use for the result.
  9481. * @returns {JulianDate} The modified result parameter.
  9482. */
  9483. JulianDate.addSeconds = function (julianDate, seconds, result) {
  9484. //>>includeStart('debug', pragmas.debug);
  9485. if (!when.defined(julianDate)) {
  9486. throw new Check.DeveloperError("julianDate is required.");
  9487. }
  9488. if (!when.defined(seconds)) {
  9489. throw new Check.DeveloperError("seconds is required.");
  9490. }
  9491. if (!when.defined(result)) {
  9492. throw new Check.DeveloperError("result is required.");
  9493. }
  9494. //>>includeEnd('debug');
  9495. return setComponents(
  9496. julianDate.dayNumber,
  9497. julianDate.secondsOfDay + seconds,
  9498. result
  9499. );
  9500. };
  9501. /**
  9502. * Adds the provided number of minutes to the provided date instance.
  9503. *
  9504. * @param {JulianDate} julianDate The date.
  9505. * @param {Number} minutes The number of minutes to add or subtract.
  9506. * @param {JulianDate} result An existing instance to use for the result.
  9507. * @returns {JulianDate} The modified result parameter.
  9508. */
  9509. JulianDate.addMinutes = function (julianDate, minutes, result) {
  9510. //>>includeStart('debug', pragmas.debug);
  9511. if (!when.defined(julianDate)) {
  9512. throw new Check.DeveloperError("julianDate is required.");
  9513. }
  9514. if (!when.defined(minutes)) {
  9515. throw new Check.DeveloperError("minutes is required.");
  9516. }
  9517. if (!when.defined(result)) {
  9518. throw new Check.DeveloperError("result is required.");
  9519. }
  9520. //>>includeEnd('debug');
  9521. var newSecondsOfDay =
  9522. julianDate.secondsOfDay + minutes * TimeConstants$1.SECONDS_PER_MINUTE;
  9523. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  9524. };
  9525. /**
  9526. * Adds the provided number of hours to the provided date instance.
  9527. *
  9528. * @param {JulianDate} julianDate The date.
  9529. * @param {Number} hours The number of hours to add or subtract.
  9530. * @param {JulianDate} result An existing instance to use for the result.
  9531. * @returns {JulianDate} The modified result parameter.
  9532. */
  9533. JulianDate.addHours = function (julianDate, hours, result) {
  9534. //>>includeStart('debug', pragmas.debug);
  9535. if (!when.defined(julianDate)) {
  9536. throw new Check.DeveloperError("julianDate is required.");
  9537. }
  9538. if (!when.defined(hours)) {
  9539. throw new Check.DeveloperError("hours is required.");
  9540. }
  9541. if (!when.defined(result)) {
  9542. throw new Check.DeveloperError("result is required.");
  9543. }
  9544. //>>includeEnd('debug');
  9545. var newSecondsOfDay =
  9546. julianDate.secondsOfDay + hours * TimeConstants$1.SECONDS_PER_HOUR;
  9547. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  9548. };
  9549. /**
  9550. * Adds the provided number of days to the provided date instance.
  9551. *
  9552. * @param {JulianDate} julianDate The date.
  9553. * @param {Number} days The number of days to add or subtract.
  9554. * @param {JulianDate} result An existing instance to use for the result.
  9555. * @returns {JulianDate} The modified result parameter.
  9556. */
  9557. JulianDate.addDays = function (julianDate, days, result) {
  9558. //>>includeStart('debug', pragmas.debug);
  9559. if (!when.defined(julianDate)) {
  9560. throw new Check.DeveloperError("julianDate is required.");
  9561. }
  9562. if (!when.defined(days)) {
  9563. throw new Check.DeveloperError("days is required.");
  9564. }
  9565. if (!when.defined(result)) {
  9566. throw new Check.DeveloperError("result is required.");
  9567. }
  9568. //>>includeEnd('debug');
  9569. var newJulianDayNumber = julianDate.dayNumber + days;
  9570. return setComponents(newJulianDayNumber, julianDate.secondsOfDay, result);
  9571. };
  9572. /**
  9573. * Compares the provided instances and returns <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  9574. *
  9575. * @param {JulianDate} left The first instance.
  9576. * @param {JulianDate} right The second instance.
  9577. * @returns {Boolean} <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  9578. */
  9579. JulianDate.lessThan = function (left, right) {
  9580. return JulianDate.compare(left, right) < 0;
  9581. };
  9582. /**
  9583. * Compares the provided instances and returns <code>true</code> if <code>left</code> is earlier than or equal to <code>right</code>, <code>false</code> otherwise.
  9584. *
  9585. * @param {JulianDate} left The first instance.
  9586. * @param {JulianDate} right The second instance.
  9587. * @returns {Boolean} <code>true</code> if <code>left</code> is earlier than or equal to <code>right</code>, <code>false</code> otherwise.
  9588. */
  9589. JulianDate.lessThanOrEquals = function (left, right) {
  9590. return JulianDate.compare(left, right) <= 0;
  9591. };
  9592. /**
  9593. * Compares the provided instances and returns <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  9594. *
  9595. * @param {JulianDate} left The first instance.
  9596. * @param {JulianDate} right The second instance.
  9597. * @returns {Boolean} <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  9598. */
  9599. JulianDate.greaterThan = function (left, right) {
  9600. return JulianDate.compare(left, right) > 0;
  9601. };
  9602. /**
  9603. * Compares the provided instances and returns <code>true</code> if <code>left</code> is later than or equal to <code>right</code>, <code>false</code> otherwise.
  9604. *
  9605. * @param {JulianDate} left The first instance.
  9606. * @param {JulianDate} right The second instance.
  9607. * @returns {Boolean} <code>true</code> if <code>left</code> is later than or equal to <code>right</code>, <code>false</code> otherwise.
  9608. */
  9609. JulianDate.greaterThanOrEquals = function (left, right) {
  9610. return JulianDate.compare(left, right) >= 0;
  9611. };
  9612. /**
  9613. * Duplicates this instance.
  9614. *
  9615. * @param {JulianDate} [result] An existing instance to use for the result.
  9616. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  9617. */
  9618. JulianDate.prototype.clone = function (result) {
  9619. return JulianDate.clone(this, result);
  9620. };
  9621. /**
  9622. * Compares this and the provided instance and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  9623. *
  9624. * @param {JulianDate} [right] The second instance.
  9625. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  9626. */
  9627. JulianDate.prototype.equals = function (right) {
  9628. return JulianDate.equals(this, right);
  9629. };
  9630. /**
  9631. * Compares this and the provided instance and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  9632. * each other. That is, in order for the dates to be considered equal (and for
  9633. * this function to return <code>true</code>), the absolute value of the difference between them, in
  9634. * seconds, must be less than <code>epsilon</code>.
  9635. *
  9636. * @param {JulianDate} [right] The second instance.
  9637. * @param {Number} [epsilon=0] The maximum number of seconds that should separate the two instances.
  9638. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  9639. */
  9640. JulianDate.prototype.equalsEpsilon = function (right, epsilon) {
  9641. return JulianDate.equalsEpsilon(this, right, epsilon);
  9642. };
  9643. /**
  9644. * Creates a string representing this date in ISO8601 format.
  9645. *
  9646. * @returns {String} A string representing this date in ISO8601 format.
  9647. */
  9648. JulianDate.prototype.toString = function () {
  9649. return JulianDate.toIso8601(this);
  9650. };
  9651. /**
  9652. * Gets or sets the list of leap seconds used throughout Cesium.
  9653. * @memberof JulianDate
  9654. * @type {LeapSecond[]}
  9655. */
  9656. JulianDate.leapSeconds = [
  9657. new LeapSecond(new JulianDate(2441317, 43210.0, TimeStandard$1.TAI), 10), // January 1, 1972 00:00:00 UTC
  9658. new LeapSecond(new JulianDate(2441499, 43211.0, TimeStandard$1.TAI), 11), // July 1, 1972 00:00:00 UTC
  9659. new LeapSecond(new JulianDate(2441683, 43212.0, TimeStandard$1.TAI), 12), // January 1, 1973 00:00:00 UTC
  9660. new LeapSecond(new JulianDate(2442048, 43213.0, TimeStandard$1.TAI), 13), // January 1, 1974 00:00:00 UTC
  9661. new LeapSecond(new JulianDate(2442413, 43214.0, TimeStandard$1.TAI), 14), // January 1, 1975 00:00:00 UTC
  9662. new LeapSecond(new JulianDate(2442778, 43215.0, TimeStandard$1.TAI), 15), // January 1, 1976 00:00:00 UTC
  9663. new LeapSecond(new JulianDate(2443144, 43216.0, TimeStandard$1.TAI), 16), // January 1, 1977 00:00:00 UTC
  9664. new LeapSecond(new JulianDate(2443509, 43217.0, TimeStandard$1.TAI), 17), // January 1, 1978 00:00:00 UTC
  9665. new LeapSecond(new JulianDate(2443874, 43218.0, TimeStandard$1.TAI), 18), // January 1, 1979 00:00:00 UTC
  9666. new LeapSecond(new JulianDate(2444239, 43219.0, TimeStandard$1.TAI), 19), // January 1, 1980 00:00:00 UTC
  9667. new LeapSecond(new JulianDate(2444786, 43220.0, TimeStandard$1.TAI), 20), // July 1, 1981 00:00:00 UTC
  9668. new LeapSecond(new JulianDate(2445151, 43221.0, TimeStandard$1.TAI), 21), // July 1, 1982 00:00:00 UTC
  9669. new LeapSecond(new JulianDate(2445516, 43222.0, TimeStandard$1.TAI), 22), // July 1, 1983 00:00:00 UTC
  9670. new LeapSecond(new JulianDate(2446247, 43223.0, TimeStandard$1.TAI), 23), // July 1, 1985 00:00:00 UTC
  9671. new LeapSecond(new JulianDate(2447161, 43224.0, TimeStandard$1.TAI), 24), // January 1, 1988 00:00:00 UTC
  9672. new LeapSecond(new JulianDate(2447892, 43225.0, TimeStandard$1.TAI), 25), // January 1, 1990 00:00:00 UTC
  9673. new LeapSecond(new JulianDate(2448257, 43226.0, TimeStandard$1.TAI), 26), // January 1, 1991 00:00:00 UTC
  9674. new LeapSecond(new JulianDate(2448804, 43227.0, TimeStandard$1.TAI), 27), // July 1, 1992 00:00:00 UTC
  9675. new LeapSecond(new JulianDate(2449169, 43228.0, TimeStandard$1.TAI), 28), // July 1, 1993 00:00:00 UTC
  9676. new LeapSecond(new JulianDate(2449534, 43229.0, TimeStandard$1.TAI), 29), // July 1, 1994 00:00:00 UTC
  9677. new LeapSecond(new JulianDate(2450083, 43230.0, TimeStandard$1.TAI), 30), // January 1, 1996 00:00:00 UTC
  9678. new LeapSecond(new JulianDate(2450630, 43231.0, TimeStandard$1.TAI), 31), // July 1, 1997 00:00:00 UTC
  9679. new LeapSecond(new JulianDate(2451179, 43232.0, TimeStandard$1.TAI), 32), // January 1, 1999 00:00:00 UTC
  9680. new LeapSecond(new JulianDate(2453736, 43233.0, TimeStandard$1.TAI), 33), // January 1, 2006 00:00:00 UTC
  9681. new LeapSecond(new JulianDate(2454832, 43234.0, TimeStandard$1.TAI), 34), // January 1, 2009 00:00:00 UTC
  9682. new LeapSecond(new JulianDate(2456109, 43235.0, TimeStandard$1.TAI), 35), // July 1, 2012 00:00:00 UTC
  9683. new LeapSecond(new JulianDate(2457204, 43236.0, TimeStandard$1.TAI), 36), // July 1, 2015 00:00:00 UTC
  9684. new LeapSecond(new JulianDate(2457754, 43237.0, TimeStandard$1.TAI), 37), // January 1, 2017 00:00:00 UTC
  9685. ];
  9686. /**
  9687. * @license
  9688. *
  9689. * Grauw URI utilities
  9690. *
  9691. * See: http://hg.grauw.nl/grauw-lib/file/tip/src/uri.js
  9692. *
  9693. * @author Laurens Holst (http://www.grauw.nl/)
  9694. *
  9695. * Copyright 2012 Laurens Holst
  9696. *
  9697. * Licensed under the Apache License, Version 2.0 (the "License");
  9698. * you may not use this file except in compliance with the License.
  9699. * You may obtain a copy of the License at
  9700. *
  9701. * http://www.apache.org/licenses/LICENSE-2.0
  9702. *
  9703. * Unless required by applicable law or agreed to in writing, software
  9704. * distributed under the License is distributed on an "AS IS" BASIS,
  9705. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9706. * See the License for the specific language governing permissions and
  9707. * limitations under the License.
  9708. *
  9709. */
  9710. /**
  9711. * Constructs a URI object.
  9712. * @constructor
  9713. * @class Implementation of URI parsing and base URI resolving algorithm in RFC 3986.
  9714. * @param {string|URI} uri A string or URI object to create the object from.
  9715. */
  9716. function URI(uri) {
  9717. if (uri instanceof URI) { // copy constructor
  9718. this.scheme = uri.scheme;
  9719. this.authority = uri.authority;
  9720. this.path = uri.path;
  9721. this.query = uri.query;
  9722. this.fragment = uri.fragment;
  9723. } else if (uri) { // uri is URI string or cast to string
  9724. var c = parseRegex.exec(uri);
  9725. this.scheme = c[1];
  9726. this.authority = c[2];
  9727. this.path = c[3];
  9728. this.query = c[4];
  9729. this.fragment = c[5];
  9730. }
  9731. }
  9732. // Initial values on the prototype
  9733. URI.prototype.scheme = null;
  9734. URI.prototype.authority = null;
  9735. URI.prototype.path = '';
  9736. URI.prototype.query = null;
  9737. URI.prototype.fragment = null;
  9738. // Regular expression from RFC 3986 appendix B
  9739. var parseRegex = new RegExp('^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?$');
  9740. /**
  9741. * Returns the scheme part of the URI.
  9742. * In "http://example.com:80/a/b?x#y" this is "http".
  9743. */
  9744. URI.prototype.getScheme = function() {
  9745. return this.scheme;
  9746. };
  9747. /**
  9748. * Returns the authority part of the URI.
  9749. * In "http://example.com:80/a/b?x#y" this is "example.com:80".
  9750. */
  9751. URI.prototype.getAuthority = function() {
  9752. return this.authority;
  9753. };
  9754. /**
  9755. * Returns the path part of the URI.
  9756. * In "http://example.com:80/a/b?x#y" this is "/a/b".
  9757. * In "mailto:mike@example.com" this is "mike@example.com".
  9758. */
  9759. URI.prototype.getPath = function() {
  9760. return this.path;
  9761. };
  9762. /**
  9763. * Returns the query part of the URI.
  9764. * In "http://example.com:80/a/b?x#y" this is "x".
  9765. */
  9766. URI.prototype.getQuery = function() {
  9767. return this.query;
  9768. };
  9769. /**
  9770. * Returns the fragment part of the URI.
  9771. * In "http://example.com:80/a/b?x#y" this is "y".
  9772. */
  9773. URI.prototype.getFragment = function() {
  9774. return this.fragment;
  9775. };
  9776. /**
  9777. * Tests whether the URI is an absolute URI.
  9778. * See RFC 3986 section 4.3.
  9779. */
  9780. URI.prototype.isAbsolute = function() {
  9781. return !!this.scheme && !this.fragment;
  9782. };
  9783. ///**
  9784. //* Extensive validation of the URI against the ABNF in RFC 3986
  9785. //*/
  9786. //URI.prototype.validate
  9787. /**
  9788. * Tests whether the URI is a same-document reference.
  9789. * See RFC 3986 section 4.4.
  9790. *
  9791. * To perform more thorough comparison, you can normalise the URI objects.
  9792. */
  9793. URI.prototype.isSameDocumentAs = function(uri) {
  9794. return uri.scheme == this.scheme &&
  9795. uri.authority == this.authority &&
  9796. uri.path == this.path &&
  9797. uri.query == this.query;
  9798. };
  9799. /**
  9800. * Simple String Comparison of two URIs.
  9801. * See RFC 3986 section 6.2.1.
  9802. *
  9803. * To perform more thorough comparison, you can normalise the URI objects.
  9804. */
  9805. URI.prototype.equals = function(uri) {
  9806. return this.isSameDocumentAs(uri) && uri.fragment == this.fragment;
  9807. };
  9808. /**
  9809. * Normalizes the URI using syntax-based normalization.
  9810. * This includes case normalization, percent-encoding normalization and path segment normalization.
  9811. * XXX: Percent-encoding normalization does not escape characters that need to be escaped.
  9812. * (Although that would not be a valid URI in the first place. See validate().)
  9813. * See RFC 3986 section 6.2.2.
  9814. */
  9815. URI.prototype.normalize = function() {
  9816. this.removeDotSegments();
  9817. if (this.scheme)
  9818. this.scheme = this.scheme.toLowerCase();
  9819. if (this.authority)
  9820. this.authority = this.authority.replace(authorityRegex, replaceAuthority).
  9821. replace(caseRegex, replaceCase);
  9822. if (this.path)
  9823. this.path = this.path.replace(caseRegex, replaceCase);
  9824. if (this.query)
  9825. this.query = this.query.replace(caseRegex, replaceCase);
  9826. if (this.fragment)
  9827. this.fragment = this.fragment.replace(caseRegex, replaceCase);
  9828. };
  9829. var caseRegex = /%[0-9a-z]{2}/gi;
  9830. var percentRegex = /[a-zA-Z0-9\-\._~]/;
  9831. var authorityRegex = /(.*@)?([^@:]*)(:.*)?/;
  9832. function replaceCase(str) {
  9833. var dec = unescape(str);
  9834. return percentRegex.test(dec) ? dec : str.toUpperCase();
  9835. }
  9836. function replaceAuthority(str, p1, p2, p3) {
  9837. return (p1 || '') + p2.toLowerCase() + (p3 || '');
  9838. }
  9839. /**
  9840. * Resolve a relative URI (this) against a base URI.
  9841. * The base URI must be an absolute URI.
  9842. * See RFC 3986 section 5.2
  9843. */
  9844. URI.prototype.resolve = function(baseURI) {
  9845. var uri = new URI();
  9846. if (this.scheme) {
  9847. uri.scheme = this.scheme;
  9848. uri.authority = this.authority;
  9849. uri.path = this.path;
  9850. uri.query = this.query;
  9851. } else {
  9852. uri.scheme = baseURI.scheme;
  9853. if (this.authority) {
  9854. uri.authority = this.authority;
  9855. uri.path = this.path;
  9856. uri.query = this.query;
  9857. } else {
  9858. uri.authority = baseURI.authority;
  9859. if (this.path == '') {
  9860. uri.path = baseURI.path;
  9861. uri.query = this.query || baseURI.query;
  9862. } else {
  9863. if (this.path.charAt(0) == '/') {
  9864. uri.path = this.path;
  9865. uri.removeDotSegments();
  9866. } else {
  9867. if (baseURI.authority && baseURI.path == '') {
  9868. uri.path = '/' + this.path;
  9869. } else {
  9870. uri.path = baseURI.path.substring(0, baseURI.path.lastIndexOf('/') + 1) + this.path;
  9871. }
  9872. uri.removeDotSegments();
  9873. }
  9874. uri.query = this.query;
  9875. }
  9876. }
  9877. }
  9878. uri.fragment = this.fragment;
  9879. return uri;
  9880. };
  9881. /**
  9882. * Remove dot segments from path.
  9883. * See RFC 3986 section 5.2.4
  9884. * @private
  9885. */
  9886. URI.prototype.removeDotSegments = function() {
  9887. var input = this.path.split('/'),
  9888. output = [],
  9889. segment,
  9890. absPath = input[0] == '';
  9891. if (absPath)
  9892. input.shift();
  9893. var sFirst = input[0] == '' ? input.shift() : null;
  9894. while (input.length) {
  9895. segment = input.shift();
  9896. if (segment == '..') {
  9897. output.pop();
  9898. } else if (segment != '.') {
  9899. output.push(segment);
  9900. }
  9901. }
  9902. if (segment == '.' || segment == '..')
  9903. output.push('');
  9904. if (absPath)
  9905. output.unshift('');
  9906. this.path = output.join('/');
  9907. };
  9908. // We don't like this function because it builds up a cache that is never cleared.
  9909. // /**
  9910. // * Resolves a relative URI against an absolute base URI.
  9911. // * Convenience method.
  9912. // * @param {String} uri the relative URI to resolve
  9913. // * @param {String} baseURI the base URI (must be absolute) to resolve against
  9914. // */
  9915. // URI.resolve = function(sURI, sBaseURI) {
  9916. // var uri = cache[sURI] || (cache[sURI] = new URI(sURI));
  9917. // var baseURI = cache[sBaseURI] || (cache[sBaseURI] = new URI(sBaseURI));
  9918. // return uri.resolve(baseURI).toString();
  9919. // };
  9920. // var cache = {};
  9921. /**
  9922. * Serialises the URI to a string.
  9923. */
  9924. URI.prototype.toString = function() {
  9925. var result = '';
  9926. if (this.scheme)
  9927. result += this.scheme + ':';
  9928. if (this.authority)
  9929. result += '//' + this.authority;
  9930. result += this.path;
  9931. if (this.query)
  9932. result += '?' + this.query;
  9933. if (this.fragment)
  9934. result += '#' + this.fragment;
  9935. return result;
  9936. };
  9937. /**
  9938. * @private
  9939. */
  9940. function appendForwardSlash(url) {
  9941. if (url.length === 0 || url[url.length - 1] !== "/") {
  9942. url = url + "/";
  9943. }
  9944. return url;
  9945. }
  9946. /**
  9947. * Clones an object, returning a new object containing the same properties.
  9948. *
  9949. * @function
  9950. *
  9951. * @param {Object} object The object to clone.
  9952. * @param {Boolean} [deep=false] If true, all properties will be deep cloned recursively.
  9953. * @returns {Object} The cloned object.
  9954. */
  9955. function clone(object, deep) {
  9956. if (object === null || typeof object !== "object") {
  9957. return object;
  9958. }
  9959. deep = when.defaultValue(deep, false);
  9960. var result = new object.constructor();
  9961. for (var propertyName in object) {
  9962. if (object.hasOwnProperty(propertyName)) {
  9963. var value = object[propertyName];
  9964. if (deep) {
  9965. value = clone(value, deep);
  9966. }
  9967. result[propertyName] = value;
  9968. }
  9969. }
  9970. return result;
  9971. }
  9972. /**
  9973. * Merges two objects, copying their properties onto a new combined object. When two objects have the same
  9974. * property, the value of the property on the first object is used. If either object is undefined,
  9975. * it will be treated as an empty object.
  9976. *
  9977. * @example
  9978. * var object1 = {
  9979. * propOne : 1,
  9980. * propTwo : {
  9981. * value1 : 10
  9982. * }
  9983. * }
  9984. * var object2 = {
  9985. * propTwo : 2
  9986. * }
  9987. * var final = Cesium.combine(object1, object2);
  9988. *
  9989. * // final === {
  9990. * // propOne : 1,
  9991. * // propTwo : {
  9992. * // value1 : 10
  9993. * // }
  9994. * // }
  9995. *
  9996. * @param {Object} [object1] The first object to merge.
  9997. * @param {Object} [object2] The second object to merge.
  9998. * @param {Boolean} [deep=false] Perform a recursive merge.
  9999. * @returns {Object} The combined object containing all properties from both objects.
  10000. *
  10001. * @function
  10002. */
  10003. function combine(object1, object2, deep) {
  10004. deep = when.defaultValue(deep, false);
  10005. var result = {};
  10006. var object1Defined = when.defined(object1);
  10007. var object2Defined = when.defined(object2);
  10008. var property;
  10009. var object1Value;
  10010. var object2Value;
  10011. if (object1Defined) {
  10012. for (property in object1) {
  10013. if (object1.hasOwnProperty(property)) {
  10014. object1Value = object1[property];
  10015. if (
  10016. object2Defined &&
  10017. deep &&
  10018. typeof object1Value === "object" &&
  10019. object2.hasOwnProperty(property)
  10020. ) {
  10021. object2Value = object2[property];
  10022. if (typeof object2Value === "object") {
  10023. result[property] = combine(object1Value, object2Value, deep);
  10024. } else {
  10025. result[property] = object1Value;
  10026. }
  10027. } else {
  10028. result[property] = object1Value;
  10029. }
  10030. }
  10031. }
  10032. }
  10033. if (object2Defined) {
  10034. for (property in object2) {
  10035. if (
  10036. object2.hasOwnProperty(property) &&
  10037. !result.hasOwnProperty(property)
  10038. ) {
  10039. object2Value = object2[property];
  10040. result[property] = object2Value;
  10041. }
  10042. }
  10043. }
  10044. return result;
  10045. }
  10046. /**
  10047. * Given a relative Uri and a base Uri, returns the absolute Uri of the relative Uri.
  10048. * @function
  10049. *
  10050. * @param {String} relative The relative Uri.
  10051. * @param {String} [base] The base Uri.
  10052. * @returns {String} The absolute Uri of the given relative Uri.
  10053. *
  10054. * @example
  10055. * //absolute Uri will be "https://test.com/awesome.png";
  10056. * var absoluteUri = Cesium.getAbsoluteUri('awesome.png', 'https://test.com');
  10057. */
  10058. function getAbsoluteUri(relative, base) {
  10059. var documentObject;
  10060. if (typeof document !== "undefined") {
  10061. documentObject = document;
  10062. }
  10063. return getAbsoluteUri._implementation(relative, base, documentObject);
  10064. }
  10065. getAbsoluteUri._implementation = function (relative, base, documentObject) {
  10066. //>>includeStart('debug', pragmas.debug);
  10067. if (!when.defined(relative)) {
  10068. throw new Check.DeveloperError("relative uri is required.");
  10069. }
  10070. //>>includeEnd('debug');
  10071. if (!when.defined(base)) {
  10072. if (typeof documentObject === "undefined") {
  10073. return relative;
  10074. }
  10075. base = when.defaultValue(documentObject.baseURI, documentObject.location.href);
  10076. }
  10077. var baseUri = new URI(base);
  10078. var relativeUri = new URI(relative);
  10079. return relativeUri.resolve(baseUri).toString();
  10080. };
  10081. /**
  10082. * Given a URI, returns the base path of the URI.
  10083. * @function
  10084. *
  10085. * @param {String} uri The Uri.
  10086. * @param {Boolean} [includeQuery = false] Whether or not to include the query string and fragment form the uri
  10087. * @returns {String} The base path of the Uri.
  10088. *
  10089. * @example
  10090. * // basePath will be "/Gallery/";
  10091. * var basePath = Cesium.getBaseUri('/Gallery/simple.czml?value=true&example=false');
  10092. *
  10093. * // basePath will be "/Gallery/?value=true&example=false";
  10094. * var basePath = Cesium.getBaseUri('/Gallery/simple.czml?value=true&example=false', true);
  10095. */
  10096. function getBaseUri(uri, includeQuery) {
  10097. //>>includeStart('debug', pragmas.debug);
  10098. if (!when.defined(uri)) {
  10099. throw new Check.DeveloperError("uri is required.");
  10100. }
  10101. //>>includeEnd('debug');
  10102. var basePath = "";
  10103. var i = uri.lastIndexOf("/");
  10104. if (i !== -1) {
  10105. basePath = uri.substring(0, i + 1);
  10106. }
  10107. if (!includeQuery) {
  10108. return basePath;
  10109. }
  10110. uri = new URI(uri);
  10111. if (when.defined(uri.query)) {
  10112. basePath += "?" + uri.query;
  10113. }
  10114. if (when.defined(uri.fragment)) {
  10115. basePath += "#" + uri.fragment;
  10116. }
  10117. return basePath;
  10118. }
  10119. /**
  10120. * Given a URI, returns the extension of the URI.
  10121. * @function getExtensionFromUri
  10122. *
  10123. * @param {String} uri The Uri.
  10124. * @returns {String} The extension of the Uri.
  10125. *
  10126. * @example
  10127. * //extension will be "czml";
  10128. * var extension = Cesium.getExtensionFromUri('/Gallery/simple.czml?value=true&example=false');
  10129. */
  10130. function getExtensionFromUri(uri) {
  10131. //>>includeStart('debug', pragmas.debug);
  10132. if (!when.defined(uri)) {
  10133. throw new Check.DeveloperError("uri is required.");
  10134. }
  10135. //>>includeEnd('debug');
  10136. var uriObject = new URI(uri);
  10137. uriObject.normalize();
  10138. var path = uriObject.path;
  10139. var index = path.lastIndexOf("/");
  10140. if (index !== -1) {
  10141. path = path.substr(index + 1);
  10142. }
  10143. index = path.lastIndexOf(".");
  10144. if (index === -1) {
  10145. path = "";
  10146. } else {
  10147. path = path.substr(index + 1);
  10148. }
  10149. return path;
  10150. }
  10151. var blobUriRegex = /^blob:/i;
  10152. /**
  10153. * Determines if the specified uri is a blob uri.
  10154. *
  10155. * @function isBlobUri
  10156. *
  10157. * @param {String} uri The uri to test.
  10158. * @returns {Boolean} true when the uri is a blob uri; otherwise, false.
  10159. *
  10160. * @private
  10161. */
  10162. function isBlobUri(uri) {
  10163. //>>includeStart('debug', pragmas.debug);
  10164. Check.Check.typeOf.string("uri", uri);
  10165. //>>includeEnd('debug');
  10166. return blobUriRegex.test(uri);
  10167. }
  10168. var a;
  10169. /**
  10170. * Given a URL, determine whether that URL is considered cross-origin to the current page.
  10171. *
  10172. * @private
  10173. */
  10174. function isCrossOriginUrl(url) {
  10175. if (!when.defined(a)) {
  10176. a = document.createElement("a");
  10177. }
  10178. // copy window location into the anchor to get consistent results
  10179. // when the port is default for the protocol (e.g. 80 for HTTP)
  10180. a.href = window.location.href;
  10181. // host includes both hostname and port if the port is not standard
  10182. var host = a.host;
  10183. var protocol = a.protocol;
  10184. a.href = url;
  10185. // IE only absolutizes href on get, not set
  10186. // eslint-disable-next-line no-self-assign
  10187. a.href = a.href;
  10188. return protocol !== a.protocol || host !== a.host;
  10189. }
  10190. var dataUriRegex = /^data:/i;
  10191. /**
  10192. * Determines if the specified uri is a data uri.
  10193. *
  10194. * @function isDataUri
  10195. *
  10196. * @param {String} uri The uri to test.
  10197. * @returns {Boolean} true when the uri is a data uri; otherwise, false.
  10198. *
  10199. * @private
  10200. */
  10201. function isDataUri(uri) {
  10202. //>>includeStart('debug', pragmas.debug);
  10203. Check.Check.typeOf.string("uri", uri);
  10204. //>>includeEnd('debug');
  10205. return dataUriRegex.test(uri);
  10206. }
  10207. /**
  10208. * @private
  10209. */
  10210. function loadAndExecuteScript(url) {
  10211. var deferred = when.when.defer();
  10212. var script = document.createElement("script");
  10213. script.async = true;
  10214. script.src = url;
  10215. var head = document.getElementsByTagName("head")[0];
  10216. script.onload = function () {
  10217. script.onload = undefined;
  10218. head.removeChild(script);
  10219. deferred.resolve();
  10220. };
  10221. script.onerror = function (e) {
  10222. deferred.reject(e);
  10223. };
  10224. head.appendChild(script);
  10225. return deferred.promise;
  10226. }
  10227. /**
  10228. * Converts an object representing a set of name/value pairs into a query string,
  10229. * with names and values encoded properly for use in a URL. Values that are arrays
  10230. * will produce multiple values with the same name.
  10231. * @function objectToQuery
  10232. *
  10233. * @param {Object} obj The object containing data to encode.
  10234. * @returns {String} An encoded query string.
  10235. *
  10236. *
  10237. * @example
  10238. * var str = Cesium.objectToQuery({
  10239. * key1 : 'some value',
  10240. * key2 : 'a/b',
  10241. * key3 : ['x', 'y']
  10242. * });
  10243. *
  10244. * @see queryToObject
  10245. * // str will be:
  10246. * // 'key1=some%20value&key2=a%2Fb&key3=x&key3=y'
  10247. */
  10248. function objectToQuery(obj) {
  10249. //>>includeStart('debug', pragmas.debug);
  10250. if (!when.defined(obj)) {
  10251. throw new Check.DeveloperError("obj is required.");
  10252. }
  10253. //>>includeEnd('debug');
  10254. var result = "";
  10255. for (var propName in obj) {
  10256. if (obj.hasOwnProperty(propName)) {
  10257. var value = obj[propName];
  10258. var part = encodeURIComponent(propName) + "=";
  10259. if (Array.isArray(value)) {
  10260. for (var i = 0, len = value.length; i < len; ++i) {
  10261. result += part + encodeURIComponent(value[i]) + "&";
  10262. }
  10263. } else {
  10264. result += part + encodeURIComponent(value) + "&";
  10265. }
  10266. }
  10267. }
  10268. // trim last &
  10269. result = result.slice(0, -1);
  10270. // This function used to replace %20 with + which is more compact and readable.
  10271. // However, some servers didn't properly handle + as a space.
  10272. // https://github.com/CesiumGS/cesium/issues/2192
  10273. return result;
  10274. }
  10275. /**
  10276. * Parses a query string into an object, where the keys and values of the object are the
  10277. * name/value pairs from the query string, decoded. If a name appears multiple times,
  10278. * the value in the object will be an array of values.
  10279. * @function queryToObject
  10280. *
  10281. * @param {String} queryString The query string.
  10282. * @returns {Object} An object containing the parameters parsed from the query string.
  10283. *
  10284. *
  10285. * @example
  10286. * var obj = Cesium.queryToObject('key1=some%20value&key2=a%2Fb&key3=x&key3=y');
  10287. * // obj will be:
  10288. * // {
  10289. * // key1 : 'some value',
  10290. * // key2 : 'a/b',
  10291. * // key3 : ['x', 'y']
  10292. * // }
  10293. *
  10294. * @see objectToQuery
  10295. */
  10296. function queryToObject(queryString) {
  10297. //>>includeStart('debug', pragmas.debug);
  10298. if (!when.defined(queryString)) {
  10299. throw new Check.DeveloperError("queryString is required.");
  10300. }
  10301. //>>includeEnd('debug');
  10302. var result = {};
  10303. if (queryString === "") {
  10304. return result;
  10305. }
  10306. var parts = queryString.replace(/\+/g, "%20").split(/[&;]/);
  10307. for (var i = 0, len = parts.length; i < len; ++i) {
  10308. var subparts = parts[i].split("=");
  10309. var name = decodeURIComponent(subparts[0]);
  10310. var value = subparts[1];
  10311. if (when.defined(value)) {
  10312. value = decodeURIComponent(value);
  10313. } else {
  10314. value = "";
  10315. }
  10316. var resultValue = result[name];
  10317. if (typeof resultValue === "string") {
  10318. // expand the single value to an array
  10319. result[name] = [resultValue, value];
  10320. } else if (Array.isArray(resultValue)) {
  10321. resultValue.push(value);
  10322. } else {
  10323. result[name] = value;
  10324. }
  10325. }
  10326. return result;
  10327. }
  10328. /**
  10329. * State of the request.
  10330. *
  10331. * @enum {Number}
  10332. */
  10333. var RequestState = {
  10334. /**
  10335. * Initial unissued state.
  10336. *
  10337. * @type Number
  10338. * @constant
  10339. */
  10340. UNISSUED: 0,
  10341. /**
  10342. * Issued but not yet active. Will become active when open slots are available.
  10343. *
  10344. * @type Number
  10345. * @constant
  10346. */
  10347. ISSUED: 1,
  10348. /**
  10349. * Actual http request has been sent.
  10350. *
  10351. * @type Number
  10352. * @constant
  10353. */
  10354. ACTIVE: 2,
  10355. /**
  10356. * Request completed successfully.
  10357. *
  10358. * @type Number
  10359. * @constant
  10360. */
  10361. RECEIVED: 3,
  10362. /**
  10363. * Request was cancelled, either explicitly or automatically because of low priority.
  10364. *
  10365. * @type Number
  10366. * @constant
  10367. */
  10368. CANCELLED: 4,
  10369. /**
  10370. * Request failed.
  10371. *
  10372. * @type Number
  10373. * @constant
  10374. */
  10375. FAILED: 5,
  10376. };
  10377. var RequestState$1 = Object.freeze(RequestState);
  10378. /**
  10379. * An enum identifying the type of request. Used for finer grained logging and priority sorting.
  10380. *
  10381. * @enum {Number}
  10382. */
  10383. var RequestType = {
  10384. /**
  10385. * Terrain request.
  10386. *
  10387. * @type Number
  10388. * @constant
  10389. */
  10390. TERRAIN: 0,
  10391. /**
  10392. * Imagery request.
  10393. *
  10394. * @type Number
  10395. * @constant
  10396. */
  10397. IMAGERY: 1,
  10398. /**
  10399. * 3D Tiles request.
  10400. *
  10401. * @type Number
  10402. * @constant
  10403. */
  10404. TILES3D: 2,
  10405. /**
  10406. * Other request.
  10407. *
  10408. * @type Number
  10409. * @constant
  10410. */
  10411. OTHER: 3,
  10412. };
  10413. var RequestType$1 = Object.freeze(RequestType);
  10414. /**
  10415. * Stores information for making a request. In general this does not need to be constructed directly.
  10416. *
  10417. * @alias Request
  10418. * @constructor
  10419. * @param {Object} [options] An object with the following properties:
  10420. * @param {String} [options.url] The url to request.
  10421. * @param {Request.RequestCallback} [options.requestFunction] The function that makes the actual data request.
  10422. * @param {Request.CancelCallback} [options.cancelFunction] The function that is called when the request is cancelled.
  10423. * @param {Request.PriorityCallback} [options.priorityFunction] The function that is called to update the request's priority, which occurs once per frame.
  10424. * @param {Number} [options.priority=0.0] The initial priority of the request.
  10425. * @param {Boolean} [options.throttle=false] Whether to throttle and prioritize the request. If false, the request will be sent immediately. If true, the request will be throttled and sent based on priority.
  10426. * @param {Boolean} [options.throttleByServer=false] Whether to throttle the request by server.
  10427. * @param {RequestType} [options.type=RequestType.OTHER] The type of request.
  10428. */
  10429. function Request(options) {
  10430. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  10431. var throttleByServer = when.defaultValue(options.throttleByServer, false);
  10432. var throttle = when.defaultValue(options.throttle, false);
  10433. /**
  10434. * The URL to request.
  10435. *
  10436. * @type {String}
  10437. */
  10438. this.url = options.url;
  10439. /**
  10440. * The function that makes the actual data request.
  10441. *
  10442. * @type {Request.RequestCallback}
  10443. */
  10444. this.requestFunction = options.requestFunction;
  10445. /**
  10446. * The function that is called when the request is cancelled.
  10447. *
  10448. * @type {Request.CancelCallback}
  10449. */
  10450. this.cancelFunction = options.cancelFunction;
  10451. /**
  10452. * The function that is called to update the request's priority, which occurs once per frame.
  10453. *
  10454. * @type {Request.PriorityCallback}
  10455. */
  10456. this.priorityFunction = options.priorityFunction;
  10457. /**
  10458. * Priority is a unit-less value where lower values represent higher priority.
  10459. * For world-based objects, this is usually the distance from the camera.
  10460. * A request that does not have a priority function defaults to a priority of 0.
  10461. *
  10462. * If priorityFunction is defined, this value is updated every frame with the result of that call.
  10463. *
  10464. * @type {Number}
  10465. * @default 0.0
  10466. */
  10467. this.priority = when.defaultValue(options.priority, 0.0);
  10468. /**
  10469. * Whether to throttle and prioritize the request. If false, the request will be sent immediately. If true, the
  10470. * request will be throttled and sent based on priority.
  10471. *
  10472. * @type {Boolean}
  10473. * @readonly
  10474. *
  10475. * @default false
  10476. */
  10477. this.throttle = throttle;
  10478. /**
  10479. * Whether to throttle the request by server. Browsers typically support about 6-8 parallel connections
  10480. * for HTTP/1 servers, and an unlimited amount of connections for HTTP/2 servers. Setting this value
  10481. * to <code>true</code> is preferable for requests going through HTTP/1 servers.
  10482. *
  10483. * @type {Boolean}
  10484. * @readonly
  10485. *
  10486. * @default false
  10487. */
  10488. this.throttleByServer = throttleByServer;
  10489. /**
  10490. * Type of request.
  10491. *
  10492. * @type {RequestType}
  10493. * @readonly
  10494. *
  10495. * @default RequestType.OTHER
  10496. */
  10497. this.type = when.defaultValue(options.type, RequestType$1.OTHER);
  10498. /**
  10499. * A key used to identify the server that a request is going to. It is derived from the url's authority and scheme.
  10500. *
  10501. * @type {String}
  10502. *
  10503. * @private
  10504. */
  10505. this.serverKey = undefined;
  10506. /**
  10507. * The current state of the request.
  10508. *
  10509. * @type {RequestState}
  10510. * @readonly
  10511. */
  10512. this.state = RequestState$1.UNISSUED;
  10513. /**
  10514. * The requests's deferred promise.
  10515. *
  10516. * @type {Object}
  10517. *
  10518. * @private
  10519. */
  10520. this.deferred = undefined;
  10521. /**
  10522. * Whether the request was explicitly cancelled.
  10523. *
  10524. * @type {Boolean}
  10525. *
  10526. * @private
  10527. */
  10528. this.cancelled = false;
  10529. }
  10530. /**
  10531. * Mark the request as cancelled.
  10532. *
  10533. * @private
  10534. */
  10535. Request.prototype.cancel = function () {
  10536. this.cancelled = true;
  10537. };
  10538. /**
  10539. * Duplicates a Request instance.
  10540. *
  10541. * @param {Request} [result] The object onto which to store the result.
  10542. *
  10543. * @returns {Request} The modified result parameter or a new Resource instance if one was not provided.
  10544. */
  10545. Request.prototype.clone = function (result) {
  10546. if (!when.defined(result)) {
  10547. return new Request(this);
  10548. }
  10549. result.url = this.url;
  10550. result.requestFunction = this.requestFunction;
  10551. result.cancelFunction = this.cancelFunction;
  10552. result.priorityFunction = this.priorityFunction;
  10553. result.priority = this.priority;
  10554. result.throttle = this.throttle;
  10555. result.throttleByServer = this.throttleByServer;
  10556. result.type = this.type;
  10557. result.serverKey = this.serverKey;
  10558. // These get defaulted because the cloned request hasn't been issued
  10559. result.state = this.RequestState.UNISSUED;
  10560. result.deferred = undefined;
  10561. result.cancelled = false;
  10562. return result;
  10563. };
  10564. /**
  10565. * Parses the result of XMLHttpRequest's getAllResponseHeaders() method into
  10566. * a dictionary.
  10567. *
  10568. * @function parseResponseHeaders
  10569. *
  10570. * @param {String} headerString The header string returned by getAllResponseHeaders(). The format is
  10571. * described here: http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders()-method
  10572. * @returns {Object} A dictionary of key/value pairs, where each key is the name of a header and the corresponding value
  10573. * is that header's value.
  10574. *
  10575. * @private
  10576. */
  10577. function parseResponseHeaders(headerString) {
  10578. var headers = {};
  10579. if (!headerString) {
  10580. return headers;
  10581. }
  10582. var headerPairs = headerString.split("\u000d\u000a");
  10583. for (var i = 0; i < headerPairs.length; ++i) {
  10584. var headerPair = headerPairs[i];
  10585. // Can't use split() here because it does the wrong thing
  10586. // if the header value has the string ": " in it.
  10587. var index = headerPair.indexOf("\u003a\u0020");
  10588. if (index > 0) {
  10589. var key = headerPair.substring(0, index);
  10590. var val = headerPair.substring(index + 2);
  10591. headers[key] = val;
  10592. }
  10593. }
  10594. return headers;
  10595. }
  10596. /**
  10597. * An event that is raised when a request encounters an error.
  10598. *
  10599. * @constructor
  10600. * @alias RequestErrorEvent
  10601. *
  10602. * @param {Number} [statusCode] The HTTP error status code, such as 404.
  10603. * @param {Object} [response] The response included along with the error.
  10604. * @param {String|Object} [responseHeaders] The response headers, represented either as an object literal or as a
  10605. * string in the format returned by XMLHttpRequest's getAllResponseHeaders() function.
  10606. */
  10607. function RequestErrorEvent(statusCode, response, responseHeaders) {
  10608. /**
  10609. * The HTTP error status code, such as 404. If the error does not have a particular
  10610. * HTTP code, this property will be undefined.
  10611. *
  10612. * @type {Number}
  10613. */
  10614. this.statusCode = statusCode;
  10615. /**
  10616. * The response included along with the error. If the error does not include a response,
  10617. * this property will be undefined.
  10618. *
  10619. * @type {Object}
  10620. */
  10621. this.response = response;
  10622. /**
  10623. * The headers included in the response, represented as an object literal of key/value pairs.
  10624. * If the error does not include any headers, this property will be undefined.
  10625. *
  10626. * @type {Object}
  10627. */
  10628. this.responseHeaders = responseHeaders;
  10629. if (typeof this.responseHeaders === "string") {
  10630. this.responseHeaders = parseResponseHeaders(this.responseHeaders);
  10631. }
  10632. }
  10633. /**
  10634. * Creates a string representing this RequestErrorEvent.
  10635. * @memberof RequestErrorEvent
  10636. *
  10637. * @returns {String} A string representing the provided RequestErrorEvent.
  10638. */
  10639. RequestErrorEvent.prototype.toString = function () {
  10640. var str = "Request has failed.";
  10641. if (when.defined(this.statusCode)) {
  10642. str += " Status Code: " + this.statusCode;
  10643. }
  10644. return str;
  10645. };
  10646. /**
  10647. * A generic utility class for managing subscribers for a particular event.
  10648. * This class is usually instantiated inside of a container class and
  10649. * exposed as a property for others to subscribe to.
  10650. *
  10651. * @alias Event
  10652. * @constructor
  10653. * @example
  10654. * MyObject.prototype.myListener = function(arg1, arg2) {
  10655. * this.myArg1Copy = arg1;
  10656. * this.myArg2Copy = arg2;
  10657. * }
  10658. *
  10659. * var myObjectInstance = new MyObject();
  10660. * var evt = new Cesium.Event();
  10661. * evt.addEventListener(MyObject.prototype.myListener, myObjectInstance);
  10662. * evt.raiseEvent('1', '2');
  10663. * evt.removeEventListener(MyObject.prototype.myListener);
  10664. */
  10665. function Event() {
  10666. this._listeners = [];
  10667. this._scopes = [];
  10668. this._toRemove = [];
  10669. this._insideRaiseEvent = false;
  10670. }
  10671. Object.defineProperties(Event.prototype, {
  10672. /**
  10673. * The number of listeners currently subscribed to the event.
  10674. * @memberof Event.prototype
  10675. * @type {Number}
  10676. * @readonly
  10677. */
  10678. numberOfListeners: {
  10679. get: function () {
  10680. return this._listeners.length - this._toRemove.length;
  10681. },
  10682. },
  10683. });
  10684. /**
  10685. * Registers a callback function to be executed whenever the event is raised.
  10686. * An optional scope can be provided to serve as the <code>this</code> pointer
  10687. * in which the function will execute.
  10688. *
  10689. * @param {Function} listener The function to be executed when the event is raised.
  10690. * @param {Object} [scope] An optional object scope to serve as the <code>this</code>
  10691. * pointer in which the listener function will execute.
  10692. * @returns {Event.RemoveCallback} A function that will remove this event listener when invoked.
  10693. *
  10694. * @see Event#raiseEvent
  10695. * @see Event#removeEventListener
  10696. */
  10697. Event.prototype.addEventListener = function (listener, scope) {
  10698. //>>includeStart('debug', pragmas.debug);
  10699. Check.Check.typeOf.func("listener", listener);
  10700. //>>includeEnd('debug');
  10701. this._listeners.push(listener);
  10702. this._scopes.push(scope);
  10703. var event = this;
  10704. return function () {
  10705. event.removeEventListener(listener, scope);
  10706. };
  10707. };
  10708. /**
  10709. * Unregisters a previously registered callback.
  10710. *
  10711. * @param {Function} listener The function to be unregistered.
  10712. * @param {Object} [scope] The scope that was originally passed to addEventListener.
  10713. * @returns {Boolean} <code>true</code> if the listener was removed; <code>false</code> if the listener and scope are not registered with the event.
  10714. *
  10715. * @see Event#addEventListener
  10716. * @see Event#raiseEvent
  10717. */
  10718. Event.prototype.removeEventListener = function (listener, scope) {
  10719. //>>includeStart('debug', pragmas.debug);
  10720. Check.Check.typeOf.func("listener", listener);
  10721. //>>includeEnd('debug');
  10722. var listeners = this._listeners;
  10723. var scopes = this._scopes;
  10724. var index = -1;
  10725. for (var i = 0; i < listeners.length; i++) {
  10726. if (listeners[i] === listener && scopes[i] === scope) {
  10727. index = i;
  10728. break;
  10729. }
  10730. }
  10731. if (index !== -1) {
  10732. if (this._insideRaiseEvent) {
  10733. //In order to allow removing an event subscription from within
  10734. //a callback, we don't actually remove the items here. Instead
  10735. //remember the index they are at and undefined their value.
  10736. this._toRemove.push(index);
  10737. listeners[index] = undefined;
  10738. scopes[index] = undefined;
  10739. } else {
  10740. listeners.splice(index, 1);
  10741. scopes.splice(index, 1);
  10742. }
  10743. return true;
  10744. }
  10745. return false;
  10746. };
  10747. function compareNumber(a, b) {
  10748. return b - a;
  10749. }
  10750. /**
  10751. * Raises the event by calling each registered listener with all supplied arguments.
  10752. *
  10753. * @param {...Object} arguments This method takes any number of parameters and passes them through to the listener functions.
  10754. *
  10755. * @see Event#addEventListener
  10756. * @see Event#removeEventListener
  10757. */
  10758. Event.prototype.raiseEvent = function () {
  10759. this._insideRaiseEvent = true;
  10760. var i;
  10761. var listeners = this._listeners;
  10762. var scopes = this._scopes;
  10763. var length = listeners.length;
  10764. for (i = 0; i < length; i++) {
  10765. var listener = listeners[i];
  10766. if (when.defined(listener)) {
  10767. listeners[i].apply(scopes[i], arguments);
  10768. }
  10769. }
  10770. //Actually remove items removed in removeEventListener.
  10771. var toRemove = this._toRemove;
  10772. length = toRemove.length;
  10773. if (length > 0) {
  10774. toRemove.sort(compareNumber);
  10775. for (i = 0; i < length; i++) {
  10776. var index = toRemove[i];
  10777. listeners.splice(index, 1);
  10778. scopes.splice(index, 1);
  10779. }
  10780. toRemove.length = 0;
  10781. }
  10782. this._insideRaiseEvent = false;
  10783. };
  10784. /**
  10785. * Array implementation of a heap.
  10786. *
  10787. * @alias Heap
  10788. * @constructor
  10789. * @private
  10790. *
  10791. * @param {Object} options Object with the following properties:
  10792. * @param {Heap.ComparatorCallback} options.comparator The comparator to use for the heap. If comparator(a, b) is less than 0, sort a to a lower index than b, otherwise sort to a higher index.
  10793. */
  10794. function Heap(options) {
  10795. //>>includeStart('debug', pragmas.debug);
  10796. Check.Check.typeOf.object("options", options);
  10797. Check.Check.defined("options.comparator", options.comparator);
  10798. //>>includeEnd('debug');
  10799. this._comparator = options.comparator;
  10800. this._array = [];
  10801. this._length = 0;
  10802. this._maximumLength = undefined;
  10803. }
  10804. Object.defineProperties(Heap.prototype, {
  10805. /**
  10806. * Gets the length of the heap.
  10807. *
  10808. * @memberof Heap.prototype
  10809. *
  10810. * @type {Number}
  10811. * @readonly
  10812. */
  10813. length: {
  10814. get: function () {
  10815. return this._length;
  10816. },
  10817. },
  10818. /**
  10819. * Gets the internal array.
  10820. *
  10821. * @memberof Heap.prototype
  10822. *
  10823. * @type {Array}
  10824. * @readonly
  10825. */
  10826. internalArray: {
  10827. get: function () {
  10828. return this._array;
  10829. },
  10830. },
  10831. /**
  10832. * Gets and sets the maximum length of the heap.
  10833. *
  10834. * @memberof Heap.prototype
  10835. *
  10836. * @type {Number}
  10837. */
  10838. maximumLength: {
  10839. get: function () {
  10840. return this._maximumLength;
  10841. },
  10842. set: function (value) {
  10843. //>>includeStart('debug', pragmas.debug);
  10844. Check.Check.typeOf.number.greaterThanOrEquals("maximumLength", value, 0);
  10845. //>>includeEnd('debug');
  10846. var originalLength = this._length;
  10847. if (value < originalLength) {
  10848. var array = this._array;
  10849. // Remove trailing references
  10850. for (var i = value; i < originalLength; ++i) {
  10851. array[i] = undefined;
  10852. }
  10853. this._length = value;
  10854. array.length = value;
  10855. }
  10856. this._maximumLength = value;
  10857. },
  10858. },
  10859. /**
  10860. * The comparator to use for the heap. If comparator(a, b) is less than 0, sort a to a lower index than b, otherwise sort to a higher index.
  10861. *
  10862. * @memberof Heap.prototype
  10863. *
  10864. * @type {Heap.ComparatorCallback}
  10865. */
  10866. comparator: {
  10867. get: function () {
  10868. return this._comparator;
  10869. },
  10870. },
  10871. });
  10872. function swap(array, a, b) {
  10873. var temp = array[a];
  10874. array[a] = array[b];
  10875. array[b] = temp;
  10876. }
  10877. /**
  10878. * Resizes the internal array of the heap.
  10879. *
  10880. * @param {Number} [length] The length to resize internal array to. Defaults to the current length of the heap.
  10881. */
  10882. Heap.prototype.reserve = function (length) {
  10883. length = when.defaultValue(length, this._length);
  10884. this._array.length = length;
  10885. };
  10886. /**
  10887. * Update the heap so that index and all descendants satisfy the heap property.
  10888. *
  10889. * @param {Number} [index=0] The starting index to heapify from.
  10890. */
  10891. Heap.prototype.heapify = function (index) {
  10892. index = when.defaultValue(index, 0);
  10893. var length = this._length;
  10894. var comparator = this._comparator;
  10895. var array = this._array;
  10896. var candidate = -1;
  10897. var inserting = true;
  10898. while (inserting) {
  10899. var right = 2 * (index + 1);
  10900. var left = right - 1;
  10901. if (left < length && comparator(array[left], array[index]) < 0) {
  10902. candidate = left;
  10903. } else {
  10904. candidate = index;
  10905. }
  10906. if (right < length && comparator(array[right], array[candidate]) < 0) {
  10907. candidate = right;
  10908. }
  10909. if (candidate !== index) {
  10910. swap(array, candidate, index);
  10911. index = candidate;
  10912. } else {
  10913. inserting = false;
  10914. }
  10915. }
  10916. };
  10917. /**
  10918. * Resort the heap.
  10919. */
  10920. Heap.prototype.resort = function () {
  10921. var length = this._length;
  10922. for (var i = Math.ceil(length / 2); i >= 0; --i) {
  10923. this.heapify(i);
  10924. }
  10925. };
  10926. /**
  10927. * Insert an element into the heap. If the length would grow greater than maximumLength
  10928. * of the heap, extra elements are removed.
  10929. *
  10930. * @param {*} element The element to insert
  10931. *
  10932. * @return {*} The element that was removed from the heap if the heap is at full capacity.
  10933. */
  10934. Heap.prototype.insert = function (element) {
  10935. //>>includeStart('debug', pragmas.debug);
  10936. Check.Check.defined("element", element);
  10937. //>>includeEnd('debug');
  10938. var array = this._array;
  10939. var comparator = this._comparator;
  10940. var maximumLength = this._maximumLength;
  10941. var index = this._length++;
  10942. if (index < array.length) {
  10943. array[index] = element;
  10944. } else {
  10945. array.push(element);
  10946. }
  10947. while (index !== 0) {
  10948. var parent = Math.floor((index - 1) / 2);
  10949. if (comparator(array[index], array[parent]) < 0) {
  10950. swap(array, index, parent);
  10951. index = parent;
  10952. } else {
  10953. break;
  10954. }
  10955. }
  10956. var removedElement;
  10957. if (when.defined(maximumLength) && this._length > maximumLength) {
  10958. removedElement = array[maximumLength];
  10959. this._length = maximumLength;
  10960. }
  10961. return removedElement;
  10962. };
  10963. /**
  10964. * Remove the element specified by index from the heap and return it.
  10965. *
  10966. * @param {Number} [index=0] The index to remove.
  10967. * @returns {*} The specified element of the heap.
  10968. */
  10969. Heap.prototype.pop = function (index) {
  10970. index = when.defaultValue(index, 0);
  10971. if (this._length === 0) {
  10972. return undefined;
  10973. }
  10974. //>>includeStart('debug', pragmas.debug);
  10975. Check.Check.typeOf.number.lessThan("index", index, this._length);
  10976. //>>includeEnd('debug');
  10977. var array = this._array;
  10978. var root = array[index];
  10979. swap(array, index, --this._length);
  10980. this.heapify(index);
  10981. array[this._length] = undefined; // Remove trailing reference
  10982. return root;
  10983. };
  10984. function sortRequests(a, b) {
  10985. return a.priority - b.priority;
  10986. }
  10987. var statistics = {
  10988. numberOfAttemptedRequests: 0,
  10989. numberOfActiveRequests: 0,
  10990. numberOfCancelledRequests: 0,
  10991. numberOfCancelledActiveRequests: 0,
  10992. numberOfFailedRequests: 0,
  10993. numberOfActiveRequestsEver: 0,
  10994. lastNumberOfActiveRequests: 0,
  10995. };
  10996. var priorityHeapLength = 20;
  10997. var requestHeap = new Heap({
  10998. comparator: sortRequests,
  10999. });
  11000. requestHeap.maximumLength = priorityHeapLength;
  11001. requestHeap.reserve(priorityHeapLength);
  11002. var activeRequests = [];
  11003. var numberOfActiveRequestsByServer = {};
  11004. var pageUri =
  11005. typeof document !== "undefined" ? new URI(document.location.href) : new URI();
  11006. var requestCompletedEvent = new Event();
  11007. /**
  11008. * The request scheduler is used to track and constrain the number of active requests in order to prioritize incoming requests. The ability
  11009. * to retain control over the number of requests in CesiumJS is important because due to events such as changes in the camera position,
  11010. * a lot of new requests may be generated and a lot of in-flight requests may become redundant. The request scheduler manually constrains the
  11011. * number of requests so that newer requests wait in a shorter queue and don't have to compete for bandwidth with requests that have expired.
  11012. *
  11013. * @namespace RequestScheduler
  11014. *
  11015. */
  11016. function RequestScheduler() {}
  11017. /**
  11018. * The maximum number of simultaneous active requests. Un-throttled requests do not observe this limit.
  11019. * @type {Number}
  11020. * @default 50
  11021. */
  11022. RequestScheduler.maximumRequests = 50;
  11023. /**
  11024. * The maximum number of simultaneous active requests per server. Un-throttled requests or servers specifically
  11025. * listed in {@link requestsByServer} do not observe this limit.
  11026. * @type {Number}
  11027. * @default 6
  11028. */
  11029. RequestScheduler.maximumRequestsPerServer = 6;
  11030. /**
  11031. * A per server key list of overrides to use for throttling instead of <code>maximumRequestsPerServer</code>
  11032. * @type {Object}
  11033. *
  11034. * @example
  11035. * RequestScheduler.requestsByServer = {
  11036. * 'api.cesium.com:443': 18,
  11037. * 'assets.cesium.com:443': 18
  11038. * };
  11039. */
  11040. RequestScheduler.requestsByServer = {
  11041. "api.cesium.com:443": 18,
  11042. "assets.cesium.com:443": 18,
  11043. };
  11044. /**
  11045. * Specifies if the request scheduler should throttle incoming requests, or let the browser queue requests under its control.
  11046. * @type {Boolean}
  11047. * @default true
  11048. */
  11049. RequestScheduler.throttleRequests = true;
  11050. /**
  11051. * When true, log statistics to the console every frame
  11052. * @type {Boolean}
  11053. * @default false
  11054. * @private
  11055. */
  11056. RequestScheduler.debugShowStatistics = false;
  11057. /**
  11058. * An event that's raised when a request is completed. Event handlers are passed
  11059. * the error object if the request fails.
  11060. *
  11061. * @type {Event}
  11062. * @default Event()
  11063. * @private
  11064. */
  11065. RequestScheduler.requestCompletedEvent = requestCompletedEvent;
  11066. Object.defineProperties(RequestScheduler, {
  11067. /**
  11068. * Returns the statistics used by the request scheduler.
  11069. *
  11070. * @memberof RequestScheduler
  11071. *
  11072. * @type Object
  11073. * @readonly
  11074. * @private
  11075. */
  11076. statistics: {
  11077. get: function () {
  11078. return statistics;
  11079. },
  11080. },
  11081. /**
  11082. * The maximum size of the priority heap. This limits the number of requests that are sorted by priority. Only applies to requests that are not yet active.
  11083. *
  11084. * @memberof RequestScheduler
  11085. *
  11086. * @type {Number}
  11087. * @default 20
  11088. * @private
  11089. */
  11090. priorityHeapLength: {
  11091. get: function () {
  11092. return priorityHeapLength;
  11093. },
  11094. set: function (value) {
  11095. // If the new length shrinks the heap, need to cancel some of the requests.
  11096. // Since this value is not intended to be tweaked regularly it is fine to just cancel the high priority requests.
  11097. if (value < priorityHeapLength) {
  11098. while (requestHeap.length > value) {
  11099. var request = requestHeap.pop();
  11100. cancelRequest(request);
  11101. }
  11102. }
  11103. priorityHeapLength = value;
  11104. requestHeap.maximumLength = value;
  11105. requestHeap.reserve(value);
  11106. },
  11107. },
  11108. });
  11109. function updatePriority(request) {
  11110. if (when.defined(request.priorityFunction)) {
  11111. request.priority = request.priorityFunction();
  11112. }
  11113. }
  11114. function serverHasOpenSlots(serverKey) {
  11115. var maxRequests = when.defaultValue(
  11116. RequestScheduler.requestsByServer[serverKey],
  11117. RequestScheduler.maximumRequestsPerServer
  11118. );
  11119. return numberOfActiveRequestsByServer[serverKey] < maxRequests;
  11120. }
  11121. function issueRequest(request) {
  11122. if (request.state === RequestState$1.UNISSUED) {
  11123. request.state = RequestState$1.ISSUED;
  11124. request.deferred = when.when.defer();
  11125. }
  11126. return request.deferred.promise;
  11127. }
  11128. function getRequestReceivedFunction(request) {
  11129. return function (results) {
  11130. if (request.state === RequestState$1.CANCELLED) {
  11131. // If the data request comes back but the request is cancelled, ignore it.
  11132. return;
  11133. }
  11134. --statistics.numberOfActiveRequests;
  11135. --numberOfActiveRequestsByServer[request.serverKey];
  11136. requestCompletedEvent.raiseEvent();
  11137. request.state = RequestState$1.RECEIVED;
  11138. request.deferred.resolve(results);
  11139. // explicitly set to undefined to ensure GC of request response data. See #8843
  11140. request.deferred = undefined;
  11141. };
  11142. }
  11143. function getRequestFailedFunction(request) {
  11144. return function (error) {
  11145. if (request.state === RequestState$1.CANCELLED) {
  11146. // If the data request comes back but the request is cancelled, ignore it.
  11147. return;
  11148. }
  11149. ++statistics.numberOfFailedRequests;
  11150. --statistics.numberOfActiveRequests;
  11151. --numberOfActiveRequestsByServer[request.serverKey];
  11152. requestCompletedEvent.raiseEvent(error);
  11153. request.state = RequestState$1.FAILED;
  11154. request.deferred.reject(error);
  11155. };
  11156. }
  11157. function startRequest(request) {
  11158. var promise = issueRequest(request);
  11159. request.state = RequestState$1.ACTIVE;
  11160. activeRequests.push(request);
  11161. ++statistics.numberOfActiveRequests;
  11162. ++statistics.numberOfActiveRequestsEver;
  11163. ++numberOfActiveRequestsByServer[request.serverKey];
  11164. request
  11165. .requestFunction()
  11166. .then(getRequestReceivedFunction(request))
  11167. .otherwise(getRequestFailedFunction(request));
  11168. return promise;
  11169. }
  11170. function cancelRequest(request) {
  11171. var active = request.state === RequestState$1.ACTIVE;
  11172. request.state = RequestState$1.CANCELLED;
  11173. ++statistics.numberOfCancelledRequests;
  11174. // check that deferred has not been cleared since cancelRequest can be called
  11175. // on a finished request, e.g. by clearForSpecs during tests
  11176. if (when.defined(request.deferred)) {
  11177. request.deferred.reject();
  11178. request.deferred = undefined;
  11179. }
  11180. if (active) {
  11181. --statistics.numberOfActiveRequests;
  11182. --numberOfActiveRequestsByServer[request.serverKey];
  11183. ++statistics.numberOfCancelledActiveRequests;
  11184. }
  11185. if (when.defined(request.cancelFunction)) {
  11186. request.cancelFunction();
  11187. }
  11188. }
  11189. /**
  11190. * Sort requests by priority and start requests.
  11191. * @private
  11192. */
  11193. RequestScheduler.update = function () {
  11194. var i;
  11195. var request;
  11196. // Loop over all active requests. Cancelled, failed, or received requests are removed from the array to make room for new requests.
  11197. var removeCount = 0;
  11198. var activeLength = activeRequests.length;
  11199. for (i = 0; i < activeLength; ++i) {
  11200. request = activeRequests[i];
  11201. if (request.cancelled) {
  11202. // Request was explicitly cancelled
  11203. cancelRequest(request);
  11204. }
  11205. if (request.state !== RequestState$1.ACTIVE) {
  11206. // Request is no longer active, remove from array
  11207. ++removeCount;
  11208. continue;
  11209. }
  11210. if (removeCount > 0) {
  11211. // Shift back to fill in vacated slots from completed requests
  11212. activeRequests[i - removeCount] = request;
  11213. }
  11214. }
  11215. activeRequests.length -= removeCount;
  11216. // Update priority of issued requests and resort the heap
  11217. var issuedRequests = requestHeap.internalArray;
  11218. var issuedLength = requestHeap.length;
  11219. for (i = 0; i < issuedLength; ++i) {
  11220. updatePriority(issuedRequests[i]);
  11221. }
  11222. requestHeap.resort();
  11223. // Get the number of open slots and fill with the highest priority requests.
  11224. // Un-throttled requests are automatically added to activeRequests, so activeRequests.length may exceed maximumRequests
  11225. var openSlots = Math.max(
  11226. RequestScheduler.maximumRequests - activeRequests.length,
  11227. 0
  11228. );
  11229. var filledSlots = 0;
  11230. while (filledSlots < openSlots && requestHeap.length > 0) {
  11231. // Loop until all open slots are filled or the heap becomes empty
  11232. request = requestHeap.pop();
  11233. if (request.cancelled) {
  11234. // Request was explicitly cancelled
  11235. cancelRequest(request);
  11236. continue;
  11237. }
  11238. if (request.throttleByServer && !serverHasOpenSlots(request.serverKey)) {
  11239. // Open slots are available, but the request is throttled by its server. Cancel and try again later.
  11240. cancelRequest(request);
  11241. continue;
  11242. }
  11243. startRequest(request);
  11244. ++filledSlots;
  11245. }
  11246. updateStatistics();
  11247. };
  11248. /**
  11249. * Get the server key from a given url.
  11250. *
  11251. * @param {String} url The url.
  11252. * @returns {String} The server key.
  11253. * @private
  11254. */
  11255. RequestScheduler.getServerKey = function (url) {
  11256. //>>includeStart('debug', pragmas.debug);
  11257. Check.Check.typeOf.string("url", url);
  11258. //>>includeEnd('debug');
  11259. var uri = new URI(url).resolve(pageUri);
  11260. uri.normalize();
  11261. var serverKey = uri.authority;
  11262. if (!/:/.test(serverKey)) {
  11263. // If the authority does not contain a port number, add port 443 for https or port 80 for http
  11264. serverKey = serverKey + ":" + (uri.scheme === "https" ? "443" : "80");
  11265. }
  11266. var length = numberOfActiveRequestsByServer[serverKey];
  11267. if (!when.defined(length)) {
  11268. numberOfActiveRequestsByServer[serverKey] = 0;
  11269. }
  11270. return serverKey;
  11271. };
  11272. /**
  11273. * Issue a request. If request.throttle is false, the request is sent immediately. Otherwise the request will be
  11274. * queued and sorted by priority before being sent.
  11275. *
  11276. * @param {Request} request The request object.
  11277. *
  11278. * @returns {Promise|undefined} A Promise for the requested data, or undefined if this request does not have high enough priority to be issued.
  11279. *
  11280. * @private
  11281. */
  11282. RequestScheduler.request = function (request) {
  11283. //>>includeStart('debug', pragmas.debug);
  11284. Check.Check.typeOf.object("request", request);
  11285. Check.Check.typeOf.string("request.url", request.url);
  11286. Check.Check.typeOf.func("request.requestFunction", request.requestFunction);
  11287. //>>includeEnd('debug');
  11288. if (isDataUri(request.url) || isBlobUri(request.url)) {
  11289. requestCompletedEvent.raiseEvent();
  11290. request.state = RequestState$1.RECEIVED;
  11291. return request.requestFunction();
  11292. }
  11293. ++statistics.numberOfAttemptedRequests;
  11294. if (!when.defined(request.serverKey)) {
  11295. request.serverKey = RequestScheduler.getServerKey(request.url);
  11296. }
  11297. if (
  11298. RequestScheduler.throttleRequests &&
  11299. request.throttleByServer &&
  11300. !serverHasOpenSlots(request.serverKey)
  11301. ) {
  11302. // Server is saturated. Try again later.
  11303. return undefined;
  11304. }
  11305. if (!RequestScheduler.throttleRequests || !request.throttle) {
  11306. return startRequest(request);
  11307. }
  11308. if (activeRequests.length >= RequestScheduler.maximumRequests) {
  11309. // Active requests are saturated. Try again later.
  11310. return undefined;
  11311. }
  11312. // Insert into the priority heap and see if a request was bumped off. If this request is the lowest
  11313. // priority it will be returned.
  11314. updatePriority(request);
  11315. var removedRequest = requestHeap.insert(request);
  11316. if (when.defined(removedRequest)) {
  11317. if (removedRequest === request) {
  11318. // Request does not have high enough priority to be issued
  11319. return undefined;
  11320. }
  11321. // A previously issued request has been bumped off the priority heap, so cancel it
  11322. cancelRequest(removedRequest);
  11323. }
  11324. return issueRequest(request);
  11325. };
  11326. function updateStatistics() {
  11327. if (!RequestScheduler.debugShowStatistics) {
  11328. return;
  11329. }
  11330. if (
  11331. statistics.numberOfActiveRequests === 0 &&
  11332. statistics.lastNumberOfActiveRequests > 0
  11333. ) {
  11334. if (statistics.numberOfAttemptedRequests > 0) {
  11335. console.log(
  11336. "Number of attempted requests: " + statistics.numberOfAttemptedRequests
  11337. );
  11338. statistics.numberOfAttemptedRequests = 0;
  11339. }
  11340. if (statistics.numberOfCancelledRequests > 0) {
  11341. console.log(
  11342. "Number of cancelled requests: " + statistics.numberOfCancelledRequests
  11343. );
  11344. statistics.numberOfCancelledRequests = 0;
  11345. }
  11346. if (statistics.numberOfCancelledActiveRequests > 0) {
  11347. console.log(
  11348. "Number of cancelled active requests: " +
  11349. statistics.numberOfCancelledActiveRequests
  11350. );
  11351. statistics.numberOfCancelledActiveRequests = 0;
  11352. }
  11353. if (statistics.numberOfFailedRequests > 0) {
  11354. console.log(
  11355. "Number of failed requests: " + statistics.numberOfFailedRequests
  11356. );
  11357. statistics.numberOfFailedRequests = 0;
  11358. }
  11359. }
  11360. statistics.lastNumberOfActiveRequests = statistics.numberOfActiveRequests;
  11361. }
  11362. /**
  11363. * For testing only. Clears any requests that may not have completed from previous tests.
  11364. *
  11365. * @private
  11366. */
  11367. RequestScheduler.clearForSpecs = function () {
  11368. while (requestHeap.length > 0) {
  11369. var request = requestHeap.pop();
  11370. cancelRequest(request);
  11371. }
  11372. var length = activeRequests.length;
  11373. for (var i = 0; i < length; ++i) {
  11374. cancelRequest(activeRequests[i]);
  11375. }
  11376. activeRequests.length = 0;
  11377. numberOfActiveRequestsByServer = {};
  11378. // Clear stats
  11379. statistics.numberOfAttemptedRequests = 0;
  11380. statistics.numberOfActiveRequests = 0;
  11381. statistics.numberOfCancelledRequests = 0;
  11382. statistics.numberOfCancelledActiveRequests = 0;
  11383. statistics.numberOfFailedRequests = 0;
  11384. statistics.numberOfActiveRequestsEver = 0;
  11385. statistics.lastNumberOfActiveRequests = 0;
  11386. };
  11387. /**
  11388. * For testing only.
  11389. *
  11390. * @private
  11391. */
  11392. RequestScheduler.numberOfActiveRequestsByServer = function (serverKey) {
  11393. return numberOfActiveRequestsByServer[serverKey];
  11394. };
  11395. /**
  11396. * For testing only.
  11397. *
  11398. * @private
  11399. */
  11400. RequestScheduler.requestHeap = requestHeap;
  11401. /**
  11402. * A singleton that contains all of the servers that are trusted. Credentials will be sent with
  11403. * any requests to these servers.
  11404. *
  11405. * @namespace TrustedServers
  11406. *
  11407. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  11408. */
  11409. var TrustedServers = {};
  11410. var _servers = {};
  11411. /**
  11412. * Adds a trusted server to the registry
  11413. *
  11414. * @param {String} host The host to be added.
  11415. * @param {Number} port The port used to access the host.
  11416. *
  11417. * @example
  11418. * // Add a trusted server
  11419. * TrustedServers.add('my.server.com', 80);
  11420. */
  11421. TrustedServers.add = function (host, port) {
  11422. //>>includeStart('debug', pragmas.debug);
  11423. if (!when.defined(host)) {
  11424. throw new Check.DeveloperError("host is required.");
  11425. }
  11426. if (!when.defined(port) || port <= 0) {
  11427. throw new Check.DeveloperError("port is required to be greater than 0.");
  11428. }
  11429. //>>includeEnd('debug');
  11430. var authority = host.toLowerCase() + ":" + port;
  11431. if (!when.defined(_servers[authority])) {
  11432. _servers[authority] = true;
  11433. }
  11434. };
  11435. /**
  11436. * Removes a trusted server from the registry
  11437. *
  11438. * @param {String} host The host to be removed.
  11439. * @param {Number} port The port used to access the host.
  11440. *
  11441. * @example
  11442. * // Remove a trusted server
  11443. * TrustedServers.remove('my.server.com', 80);
  11444. */
  11445. TrustedServers.remove = function (host, port) {
  11446. //>>includeStart('debug', pragmas.debug);
  11447. if (!when.defined(host)) {
  11448. throw new Check.DeveloperError("host is required.");
  11449. }
  11450. if (!when.defined(port) || port <= 0) {
  11451. throw new Check.DeveloperError("port is required to be greater than 0.");
  11452. }
  11453. //>>includeEnd('debug');
  11454. var authority = host.toLowerCase() + ":" + port;
  11455. if (when.defined(_servers[authority])) {
  11456. delete _servers[authority];
  11457. }
  11458. };
  11459. function getAuthority(url) {
  11460. var uri = new URI(url);
  11461. uri.normalize();
  11462. // Removes username:password@ so we just have host[:port]
  11463. var authority = uri.getAuthority();
  11464. if (!when.defined(authority)) {
  11465. return undefined; // Relative URL
  11466. }
  11467. if (authority.indexOf("@") !== -1) {
  11468. var parts = authority.split("@");
  11469. authority = parts[1];
  11470. }
  11471. // If the port is missing add one based on the scheme
  11472. if (authority.indexOf(":") === -1) {
  11473. var scheme = uri.getScheme();
  11474. if (!when.defined(scheme)) {
  11475. scheme = window.location.protocol;
  11476. scheme = scheme.substring(0, scheme.length - 1);
  11477. }
  11478. if (scheme === "http") {
  11479. authority += ":80";
  11480. } else if (scheme === "https") {
  11481. authority += ":443";
  11482. } else {
  11483. return undefined;
  11484. }
  11485. }
  11486. return authority;
  11487. }
  11488. /**
  11489. * Tests whether a server is trusted or not. The server must have been added with the port if it is included in the url.
  11490. *
  11491. * @param {String} url The url to be tested against the trusted list
  11492. *
  11493. * @returns {boolean} Returns true if url is trusted, false otherwise.
  11494. *
  11495. * @example
  11496. * // Add server
  11497. * TrustedServers.add('my.server.com', 81);
  11498. *
  11499. * // Check if server is trusted
  11500. * if (TrustedServers.contains('https://my.server.com:81/path/to/file.png')) {
  11501. * // my.server.com:81 is trusted
  11502. * }
  11503. * if (TrustedServers.contains('https://my.server.com/path/to/file.png')) {
  11504. * // my.server.com isn't trusted
  11505. * }
  11506. */
  11507. TrustedServers.contains = function (url) {
  11508. //>>includeStart('debug', pragmas.debug);
  11509. if (!when.defined(url)) {
  11510. throw new Check.DeveloperError("url is required.");
  11511. }
  11512. //>>includeEnd('debug');
  11513. var authority = getAuthority(url);
  11514. if (when.defined(authority) && when.defined(_servers[authority])) {
  11515. return true;
  11516. }
  11517. return false;
  11518. };
  11519. /**
  11520. * Clears the registry
  11521. *
  11522. * @example
  11523. * // Remove a trusted server
  11524. * TrustedServers.clear();
  11525. */
  11526. TrustedServers.clear = function () {
  11527. _servers = {};
  11528. };
  11529. var xhrBlobSupported = (function () {
  11530. try {
  11531. var xhr = new XMLHttpRequest();
  11532. xhr.open("GET", "#", true);
  11533. xhr.responseType = "blob";
  11534. return xhr.responseType === "blob";
  11535. } catch (e) {
  11536. return false;
  11537. }
  11538. })();
  11539. /**
  11540. * Parses a query string and returns the object equivalent.
  11541. *
  11542. * @param {Uri} uri The Uri with a query object.
  11543. * @param {Resource} resource The Resource that will be assigned queryParameters.
  11544. * @param {Boolean} merge If true, we'll merge with the resource's existing queryParameters. Otherwise they will be replaced.
  11545. * @param {Boolean} preserveQueryParameters If true duplicate parameters will be concatenated into an array. If false, keys in uri will take precedence.
  11546. *
  11547. * @private
  11548. */
  11549. function parseQuery(uri, resource, merge, preserveQueryParameters) {
  11550. var queryString = uri.query;
  11551. if (!when.defined(queryString) || queryString.length === 0) {
  11552. return {};
  11553. }
  11554. var query;
  11555. // Special case we run into where the querystring is just a string, not key/value pairs
  11556. if (queryString.indexOf("=") === -1) {
  11557. var result = {};
  11558. result[queryString] = undefined;
  11559. query = result;
  11560. } else {
  11561. query = queryToObject(queryString);
  11562. }
  11563. if (merge) {
  11564. resource._queryParameters = combineQueryParameters(
  11565. query,
  11566. resource._queryParameters,
  11567. preserveQueryParameters
  11568. );
  11569. } else {
  11570. resource._queryParameters = query;
  11571. }
  11572. uri.query = undefined;
  11573. }
  11574. /**
  11575. * Converts a query object into a string.
  11576. *
  11577. * @param {Uri} uri The Uri object that will have the query object set.
  11578. * @param {Resource} resource The resource that has queryParameters
  11579. *
  11580. * @private
  11581. */
  11582. function stringifyQuery(uri, resource) {
  11583. var queryObject = resource._queryParameters;
  11584. var keys = Object.keys(queryObject);
  11585. // We have 1 key with an undefined value, so this is just a string, not key/value pairs
  11586. if (keys.length === 1 && !when.defined(queryObject[keys[0]])) {
  11587. uri.query = keys[0];
  11588. } else {
  11589. uri.query = objectToQuery(queryObject);
  11590. }
  11591. }
  11592. /**
  11593. * Clones a value if it is defined, otherwise returns the default value
  11594. *
  11595. * @param {*} [val] The value to clone.
  11596. * @param {*} [defaultVal] The default value.
  11597. *
  11598. * @returns {*} A clone of val or the defaultVal.
  11599. *
  11600. * @private
  11601. */
  11602. function defaultClone(val, defaultVal) {
  11603. if (!when.defined(val)) {
  11604. return defaultVal;
  11605. }
  11606. return when.defined(val.clone) ? val.clone() : clone(val);
  11607. }
  11608. /**
  11609. * Checks to make sure the Resource isn't already being requested.
  11610. *
  11611. * @param {Request} request The request to check.
  11612. *
  11613. * @private
  11614. */
  11615. function checkAndResetRequest(request) {
  11616. if (
  11617. request.state === RequestState$1.ISSUED ||
  11618. request.state === RequestState$1.ACTIVE
  11619. ) {
  11620. throw new RuntimeError.RuntimeError("The Resource is already being fetched.");
  11621. }
  11622. request.state = RequestState$1.UNISSUED;
  11623. request.deferred = undefined;
  11624. }
  11625. /**
  11626. * This combines a map of query parameters.
  11627. *
  11628. * @param {Object} q1 The first map of query parameters. Values in this map will take precedence if preserveQueryParameters is false.
  11629. * @param {Object} q2 The second map of query parameters.
  11630. * @param {Boolean} preserveQueryParameters If true duplicate parameters will be concatenated into an array. If false, keys in q1 will take precedence.
  11631. *
  11632. * @returns {Object} The combined map of query parameters.
  11633. *
  11634. * @example
  11635. * var q1 = {
  11636. * a: 1,
  11637. * b: 2
  11638. * };
  11639. * var q2 = {
  11640. * a: 3,
  11641. * c: 4
  11642. * };
  11643. * var q3 = {
  11644. * b: [5, 6],
  11645. * d: 7
  11646. * }
  11647. *
  11648. * // Returns
  11649. * // {
  11650. * // a: [1, 3],
  11651. * // b: 2,
  11652. * // c: 4
  11653. * // };
  11654. * combineQueryParameters(q1, q2, true);
  11655. *
  11656. * // Returns
  11657. * // {
  11658. * // a: 1,
  11659. * // b: 2,
  11660. * // c: 4
  11661. * // };
  11662. * combineQueryParameters(q1, q2, false);
  11663. *
  11664. * // Returns
  11665. * // {
  11666. * // a: 1,
  11667. * // b: [2, 5, 6],
  11668. * // d: 7
  11669. * // };
  11670. * combineQueryParameters(q1, q3, true);
  11671. *
  11672. * // Returns
  11673. * // {
  11674. * // a: 1,
  11675. * // b: 2,
  11676. * // d: 7
  11677. * // };
  11678. * combineQueryParameters(q1, q3, false);
  11679. *
  11680. * @private
  11681. */
  11682. function combineQueryParameters(q1, q2, preserveQueryParameters) {
  11683. if (!preserveQueryParameters) {
  11684. return combine(q1, q2);
  11685. }
  11686. var result = clone(q1, true);
  11687. for (var param in q2) {
  11688. if (q2.hasOwnProperty(param)) {
  11689. var value = result[param];
  11690. var q2Value = q2[param];
  11691. if (when.defined(value)) {
  11692. if (!Array.isArray(value)) {
  11693. value = result[param] = [value];
  11694. }
  11695. result[param] = value.concat(q2Value);
  11696. } else {
  11697. result[param] = Array.isArray(q2Value) ? q2Value.slice() : q2Value;
  11698. }
  11699. }
  11700. }
  11701. return result;
  11702. }
  11703. /**
  11704. * A resource that includes the location and any other parameters we need to retrieve it or create derived resources. It also provides the ability to retry requests.
  11705. *
  11706. * @alias Resource
  11707. * @constructor
  11708. *
  11709. * @param {String|Object} options A url or an object with the following properties
  11710. * @param {String} options.url The url of the resource.
  11711. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  11712. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  11713. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  11714. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  11715. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  11716. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  11717. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  11718. *
  11719. * @example
  11720. * function refreshTokenRetryCallback(resource, error) {
  11721. * if (error.statusCode === 403) {
  11722. * // 403 status code means a new token should be generated
  11723. * return getNewAccessToken()
  11724. * .then(function(token) {
  11725. * resource.queryParameters.access_token = token;
  11726. * return true;
  11727. * })
  11728. * .otherwise(function() {
  11729. * return false;
  11730. * });
  11731. * }
  11732. *
  11733. * return false;
  11734. * }
  11735. *
  11736. * var resource = new Resource({
  11737. * url: 'http://server.com/path/to/resource.json',
  11738. * proxy: new DefaultProxy('/proxy/'),
  11739. * headers: {
  11740. * 'X-My-Header': 'valueOfHeader'
  11741. * },
  11742. * queryParameters: {
  11743. * 'access_token': '123-435-456-000'
  11744. * },
  11745. * retryCallback: refreshTokenRetryCallback,
  11746. * retryAttempts: 1
  11747. * });
  11748. */
  11749. function Resource(options) {
  11750. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  11751. if (typeof options === "string") {
  11752. options = {
  11753. url: options,
  11754. };
  11755. }
  11756. //>>includeStart('debug', pragmas.debug);
  11757. Check.Check.typeOf.string("options.url", options.url);
  11758. //>>includeEnd('debug');
  11759. this._url = undefined;
  11760. this._templateValues = defaultClone(options.templateValues, {});
  11761. this._queryParameters = defaultClone(options.queryParameters, {});
  11762. /**
  11763. * Additional HTTP headers that will be sent with the request.
  11764. *
  11765. * @type {Object}
  11766. */
  11767. this.headers = defaultClone(options.headers, {});
  11768. /**
  11769. * A Request object that will be used. Intended for internal use only.
  11770. *
  11771. * @type {Request}
  11772. */
  11773. this.request = when.defaultValue(options.request, new Request());
  11774. /**
  11775. * A proxy to be used when loading the resource.
  11776. *
  11777. * @type {Proxy}
  11778. */
  11779. this.proxy = options.proxy;
  11780. /**
  11781. * Function to call when a request for this resource fails. If it returns true or a Promise that resolves to true, the request will be retried.
  11782. *
  11783. * @type {Function}
  11784. */
  11785. this.retryCallback = options.retryCallback;
  11786. /**
  11787. * The number of times the retryCallback should be called before giving up.
  11788. *
  11789. * @type {Number}
  11790. */
  11791. this.retryAttempts = when.defaultValue(options.retryAttempts, 0);
  11792. this._retryCount = 0;
  11793. var uri = new URI(options.url);
  11794. parseQuery(uri, this, true, true);
  11795. // Remove the fragment as it's not sent with a request
  11796. uri.fragment = undefined;
  11797. this._url = uri.toString();
  11798. }
  11799. /**
  11800. * A helper function to create a resource depending on whether we have a String or a Resource
  11801. *
  11802. * @param {Resource|String} resource A Resource or a String to use when creating a new Resource.
  11803. *
  11804. * @returns {Resource} If resource is a String, a Resource constructed with the url and options. Otherwise the resource parameter is returned.
  11805. *
  11806. * @private
  11807. */
  11808. Resource.createIfNeeded = function (resource) {
  11809. if (resource instanceof Resource) {
  11810. // Keep existing request object. This function is used internally to duplicate a Resource, so that it can't
  11811. // be modified outside of a class that holds it (eg. an imagery or terrain provider). Since the Request objects
  11812. // are managed outside of the providers, by the tile loading code, we want to keep the request property the same so if it is changed
  11813. // in the underlying tiling code the requests for this resource will use it.
  11814. return resource.getDerivedResource({
  11815. request: resource.request,
  11816. });
  11817. }
  11818. if (typeof resource !== "string") {
  11819. return resource;
  11820. }
  11821. return new Resource({
  11822. url: resource,
  11823. });
  11824. };
  11825. var supportsImageBitmapOptionsPromise;
  11826. /**
  11827. * A helper function to check whether createImageBitmap supports passing ImageBitmapOptions.
  11828. *
  11829. * @returns {Promise<Boolean>} A promise that resolves to true if this browser supports creating an ImageBitmap with options.
  11830. *
  11831. * @private
  11832. */
  11833. Resource.supportsImageBitmapOptions = function () {
  11834. // Until the HTML folks figure out what to do about this, we need to actually try loading an image to
  11835. // know if this browser supports passing options to the createImageBitmap function.
  11836. // https://github.com/whatwg/html/pull/4248
  11837. if (when.defined(supportsImageBitmapOptionsPromise)) {
  11838. return supportsImageBitmapOptionsPromise;
  11839. }
  11840. if (typeof createImageBitmap !== "function") {
  11841. supportsImageBitmapOptionsPromise = when.when.resolve(false);
  11842. return supportsImageBitmapOptionsPromise;
  11843. }
  11844. var imageDataUri =
  11845. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==";
  11846. supportsImageBitmapOptionsPromise = Resource.fetchBlob({
  11847. url: imageDataUri,
  11848. })
  11849. .then(function (blob) {
  11850. return createImageBitmap(blob, {
  11851. imageOrientation: "flipY",
  11852. premultiplyAlpha: "none",
  11853. });
  11854. })
  11855. .then(function (imageBitmap) {
  11856. return true;
  11857. })
  11858. .otherwise(function () {
  11859. return false;
  11860. });
  11861. return supportsImageBitmapOptionsPromise;
  11862. };
  11863. Object.defineProperties(Resource, {
  11864. /**
  11865. * Returns true if blobs are supported.
  11866. *
  11867. * @memberof Resource
  11868. * @type {Boolean}
  11869. *
  11870. * @readonly
  11871. */
  11872. isBlobSupported: {
  11873. get: function () {
  11874. return xhrBlobSupported;
  11875. },
  11876. },
  11877. });
  11878. Object.defineProperties(Resource.prototype, {
  11879. /**
  11880. * Query parameters appended to the url.
  11881. *
  11882. * @memberof Resource.prototype
  11883. * @type {Object}
  11884. *
  11885. * @readonly
  11886. */
  11887. queryParameters: {
  11888. get: function () {
  11889. return this._queryParameters;
  11890. },
  11891. },
  11892. /**
  11893. * The key/value pairs used to replace template parameters in the url.
  11894. *
  11895. * @memberof Resource.prototype
  11896. * @type {Object}
  11897. *
  11898. * @readonly
  11899. */
  11900. templateValues: {
  11901. get: function () {
  11902. return this._templateValues;
  11903. },
  11904. },
  11905. /**
  11906. * The url to the resource with template values replaced, query string appended and encoded by proxy if one was set.
  11907. *
  11908. * @memberof Resource.prototype
  11909. * @type {String}
  11910. */
  11911. url: {
  11912. get: function () {
  11913. return this.getUrlComponent(true, true);
  11914. },
  11915. set: function (value) {
  11916. var uri = new URI(value);
  11917. parseQuery(uri, this, false);
  11918. // Remove the fragment as it's not sent with a request
  11919. uri.fragment = undefined;
  11920. this._url = uri.toString();
  11921. },
  11922. },
  11923. /**
  11924. * The file extension of the resource.
  11925. *
  11926. * @memberof Resource.prototype
  11927. * @type {String}
  11928. *
  11929. * @readonly
  11930. */
  11931. extension: {
  11932. get: function () {
  11933. return getExtensionFromUri(this._url);
  11934. },
  11935. },
  11936. /**
  11937. * True if the Resource refers to a data URI.
  11938. *
  11939. * @memberof Resource.prototype
  11940. * @type {Boolean}
  11941. */
  11942. isDataUri: {
  11943. get: function () {
  11944. return isDataUri(this._url);
  11945. },
  11946. },
  11947. /**
  11948. * True if the Resource refers to a blob URI.
  11949. *
  11950. * @memberof Resource.prototype
  11951. * @type {Boolean}
  11952. */
  11953. isBlobUri: {
  11954. get: function () {
  11955. return isBlobUri(this._url);
  11956. },
  11957. },
  11958. /**
  11959. * True if the Resource refers to a cross origin URL.
  11960. *
  11961. * @memberof Resource.prototype
  11962. * @type {Boolean}
  11963. */
  11964. isCrossOriginUrl: {
  11965. get: function () {
  11966. return isCrossOriginUrl(this._url);
  11967. },
  11968. },
  11969. /**
  11970. * True if the Resource has request headers. This is equivalent to checking if the headers property has any keys.
  11971. *
  11972. * @memberof Resource.prototype
  11973. * @type {Boolean}
  11974. */
  11975. hasHeaders: {
  11976. get: function () {
  11977. return Object.keys(this.headers).length > 0;
  11978. },
  11979. },
  11980. });
  11981. /**
  11982. * Override Object#toString so that implicit string conversion gives the
  11983. * complete URL represented by this Resource.
  11984. *
  11985. * @returns {String} The URL represented by this Resource
  11986. */
  11987. Resource.prototype.toString = function () {
  11988. return this.getUrlComponent(true, true);
  11989. };
  11990. /**
  11991. * Returns the url, optional with the query string and processed by a proxy.
  11992. *
  11993. * @param {Boolean} [query=false] If true, the query string is included.
  11994. * @param {Boolean} [proxy=false] If true, the url is processed by the proxy object, if defined.
  11995. *
  11996. * @returns {String} The url with all the requested components.
  11997. */
  11998. Resource.prototype.getUrlComponent = function (query, proxy) {
  11999. if (this.isDataUri) {
  12000. return this._url;
  12001. }
  12002. var uri = new URI(this._url);
  12003. if (query) {
  12004. stringifyQuery(uri, this);
  12005. }
  12006. // objectToQuery escapes the placeholders. Undo that.
  12007. var url = uri.toString().replace(/%7B/g, "{").replace(/%7D/g, "}");
  12008. var templateValues = this._templateValues;
  12009. url = url.replace(/{(.*?)}/g, function (match, key) {
  12010. var replacement = templateValues[key];
  12011. if (when.defined(replacement)) {
  12012. // use the replacement value from templateValues if there is one...
  12013. return encodeURIComponent(replacement);
  12014. }
  12015. // otherwise leave it unchanged
  12016. return match;
  12017. });
  12018. if (proxy && when.defined(this.proxy)) {
  12019. url = this.proxy.getURL(url);
  12020. }
  12021. return url;
  12022. };
  12023. /**
  12024. * Combines the specified object and the existing query parameters. This allows you to add many parameters at once,
  12025. * as opposed to adding them one at a time to the queryParameters property. If a value is already set, it will be replaced with the new value.
  12026. *
  12027. * @param {Object} params The query parameters
  12028. * @param {Boolean} [useAsDefault=false] If true the params will be used as the default values, so they will only be set if they are undefined.
  12029. */
  12030. Resource.prototype.setQueryParameters = function (params, useAsDefault) {
  12031. if (useAsDefault) {
  12032. this._queryParameters = combineQueryParameters(
  12033. this._queryParameters,
  12034. params,
  12035. false
  12036. );
  12037. } else {
  12038. this._queryParameters = combineQueryParameters(
  12039. params,
  12040. this._queryParameters,
  12041. false
  12042. );
  12043. }
  12044. };
  12045. /**
  12046. * Combines the specified object and the existing query parameters. This allows you to add many parameters at once,
  12047. * as opposed to adding them one at a time to the queryParameters property.
  12048. *
  12049. * @param {Object} params The query parameters
  12050. */
  12051. Resource.prototype.appendQueryParameters = function (params) {
  12052. this._queryParameters = combineQueryParameters(
  12053. params,
  12054. this._queryParameters,
  12055. true
  12056. );
  12057. };
  12058. /**
  12059. * Combines the specified object and the existing template values. This allows you to add many values at once,
  12060. * as opposed to adding them one at a time to the templateValues property. If a value is already set, it will become an array and the new value will be appended.
  12061. *
  12062. * @param {Object} template The template values
  12063. * @param {Boolean} [useAsDefault=false] If true the values will be used as the default values, so they will only be set if they are undefined.
  12064. */
  12065. Resource.prototype.setTemplateValues = function (template, useAsDefault) {
  12066. if (useAsDefault) {
  12067. this._templateValues = combine(this._templateValues, template);
  12068. } else {
  12069. this._templateValues = combine(template, this._templateValues);
  12070. }
  12071. };
  12072. /**
  12073. * Returns a resource relative to the current instance. All properties remain the same as the current instance unless overridden in options.
  12074. *
  12075. * @param {Object} options An object with the following properties
  12076. * @param {String} [options.url] The url that will be resolved relative to the url of the current instance.
  12077. * @param {Object} [options.queryParameters] An object containing query parameters that will be combined with those of the current instance.
  12078. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}). These will be combined with those of the current instance.
  12079. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12080. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12081. * @param {Resource.RetryCallback} [options.retryCallback] The function to call when loading the resource fails.
  12082. * @param {Number} [options.retryAttempts] The number of times the retryCallback should be called before giving up.
  12083. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12084. * @param {Boolean} [options.preserveQueryParameters=false] If true, this will keep all query parameters from the current resource and derived resource. If false, derived parameters will replace those of the current resource.
  12085. *
  12086. * @returns {Resource} The resource derived from the current one.
  12087. */
  12088. Resource.prototype.getDerivedResource = function (options) {
  12089. var resource = this.clone();
  12090. resource._retryCount = 0;
  12091. if (when.defined(options.url)) {
  12092. var uri = new URI(options.url);
  12093. var preserveQueryParameters = when.defaultValue(
  12094. options.preserveQueryParameters,
  12095. false
  12096. );
  12097. parseQuery(uri, resource, true, preserveQueryParameters);
  12098. // Remove the fragment as it's not sent with a request
  12099. uri.fragment = undefined;
  12100. resource._url = uri.resolve(new URI(getAbsoluteUri(this._url))).toString();
  12101. }
  12102. if (when.defined(options.queryParameters)) {
  12103. resource._queryParameters = combine(
  12104. options.queryParameters,
  12105. resource._queryParameters
  12106. );
  12107. }
  12108. if (when.defined(options.templateValues)) {
  12109. resource._templateValues = combine(
  12110. options.templateValues,
  12111. resource.templateValues
  12112. );
  12113. }
  12114. if (when.defined(options.headers)) {
  12115. resource.headers = combine(options.headers, resource.headers);
  12116. }
  12117. if (when.defined(options.proxy)) {
  12118. resource.proxy = options.proxy;
  12119. }
  12120. if (when.defined(options.request)) {
  12121. resource.request = options.request;
  12122. }
  12123. if (when.defined(options.retryCallback)) {
  12124. resource.retryCallback = options.retryCallback;
  12125. }
  12126. if (when.defined(options.retryAttempts)) {
  12127. resource.retryAttempts = options.retryAttempts;
  12128. }
  12129. return resource;
  12130. };
  12131. /**
  12132. * Called when a resource fails to load. This will call the retryCallback function if defined until retryAttempts is reached.
  12133. *
  12134. * @param {Error} [error] The error that was encountered.
  12135. *
  12136. * @returns {Promise<Boolean>} A promise to a boolean, that if true will cause the resource request to be retried.
  12137. *
  12138. * @private
  12139. */
  12140. Resource.prototype.retryOnError = function (error) {
  12141. var retryCallback = this.retryCallback;
  12142. if (
  12143. typeof retryCallback !== "function" ||
  12144. this._retryCount >= this.retryAttempts
  12145. ) {
  12146. return when.when(false);
  12147. }
  12148. var that = this;
  12149. return when.when(retryCallback(this, error)).then(function (result) {
  12150. ++that._retryCount;
  12151. return result;
  12152. });
  12153. };
  12154. /**
  12155. * Duplicates a Resource instance.
  12156. *
  12157. * @param {Resource} [result] The object onto which to store the result.
  12158. *
  12159. * @returns {Resource} The modified result parameter or a new Resource instance if one was not provided.
  12160. */
  12161. Resource.prototype.clone = function (result) {
  12162. if (!when.defined(result)) {
  12163. result = new Resource({
  12164. url: this._url,
  12165. });
  12166. }
  12167. result._url = this._url;
  12168. result._queryParameters = clone(this._queryParameters);
  12169. result._templateValues = clone(this._templateValues);
  12170. result.headers = clone(this.headers);
  12171. result.proxy = this.proxy;
  12172. result.retryCallback = this.retryCallback;
  12173. result.retryAttempts = this.retryAttempts;
  12174. result._retryCount = 0;
  12175. result.request = this.request.clone();
  12176. return result;
  12177. };
  12178. /**
  12179. * Returns the base path of the Resource.
  12180. *
  12181. * @param {Boolean} [includeQuery = false] Whether or not to include the query string and fragment form the uri
  12182. *
  12183. * @returns {String} The base URI of the resource
  12184. */
  12185. Resource.prototype.getBaseUri = function (includeQuery) {
  12186. return getBaseUri(this.getUrlComponent(includeQuery), includeQuery);
  12187. };
  12188. /**
  12189. * Appends a forward slash to the URL.
  12190. */
  12191. Resource.prototype.appendForwardSlash = function () {
  12192. this._url = appendForwardSlash(this._url);
  12193. };
  12194. /**
  12195. * Asynchronously loads the resource as raw binary data. Returns a promise that will resolve to
  12196. * an ArrayBuffer once loaded, or reject if the resource failed to load. The data is loaded
  12197. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12198. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12199. *
  12200. * @returns {Promise.<ArrayBuffer>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12201. *
  12202. * @example
  12203. * // load a single URL asynchronously
  12204. * resource.fetchArrayBuffer().then(function(arrayBuffer) {
  12205. * // use the data
  12206. * }).otherwise(function(error) {
  12207. * // an error occurred
  12208. * });
  12209. *
  12210. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12211. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12212. */
  12213. Resource.prototype.fetchArrayBuffer = function () {
  12214. return this.fetch({
  12215. responseType: "arraybuffer",
  12216. });
  12217. };
  12218. /**
  12219. * Creates a Resource and calls fetchArrayBuffer() on it.
  12220. *
  12221. * @param {String|Object} options A url or an object with the following properties
  12222. * @param {String} options.url The url of the resource.
  12223. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12224. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12225. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12226. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12227. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12228. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12229. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12230. * @returns {Promise.<ArrayBuffer>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12231. */
  12232. Resource.fetchArrayBuffer = function (options) {
  12233. var resource = new Resource(options);
  12234. return resource.fetchArrayBuffer();
  12235. };
  12236. /**
  12237. * Asynchronously loads the given resource as a blob. Returns a promise that will resolve to
  12238. * a Blob once loaded, or reject if the resource failed to load. The data is loaded
  12239. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12240. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12241. *
  12242. * @returns {Promise.<Blob>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12243. *
  12244. * @example
  12245. * // load a single URL asynchronously
  12246. * resource.fetchBlob().then(function(blob) {
  12247. * // use the data
  12248. * }).otherwise(function(error) {
  12249. * // an error occurred
  12250. * });
  12251. *
  12252. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12253. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12254. */
  12255. Resource.prototype.fetchBlob = function () {
  12256. return this.fetch({
  12257. responseType: "blob",
  12258. });
  12259. };
  12260. /**
  12261. * Creates a Resource and calls fetchBlob() on it.
  12262. *
  12263. * @param {String|Object} options A url or an object with the following properties
  12264. * @param {String} options.url The url of the resource.
  12265. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12266. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12267. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12268. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12269. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12270. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12271. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12272. * @returns {Promise.<Blob>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12273. */
  12274. Resource.fetchBlob = function (options) {
  12275. var resource = new Resource(options);
  12276. return resource.fetchBlob();
  12277. };
  12278. /**
  12279. * Asynchronously loads the given image resource. Returns a promise that will resolve to
  12280. * an {@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap|ImageBitmap} if <code>preferImageBitmap</code> is true and the browser supports <code>createImageBitmap</code> or otherwise an
  12281. * {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement|Image} once loaded, or reject if the image failed to load.
  12282. *
  12283. * @param {Object} [options] An object with the following properties.
  12284. * @param {Boolean} [options.preferBlob=false] If true, we will load the image via a blob.
  12285. * @param {Boolean} [options.preferImageBitmap=false] If true, image will be decoded during fetch and an <code>ImageBitmap</code> is returned.
  12286. * @param {Boolean} [options.flipY=false] If true, image will be vertically flipped during decode. Only applies if the browser supports <code>createImageBitmap</code>.
  12287. * @returns {Promise.<ImageBitmap>|Promise.<HTMLImageElement>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12288. *
  12289. *
  12290. * @example
  12291. * // load a single image asynchronously
  12292. * resource.fetchImage().then(function(image) {
  12293. * // use the loaded image
  12294. * }).otherwise(function(error) {
  12295. * // an error occurred
  12296. * });
  12297. *
  12298. * // load several images in parallel
  12299. * when.all([resource1.fetchImage(), resource2.fetchImage()]).then(function(images) {
  12300. * // images is an array containing all the loaded images
  12301. * });
  12302. *
  12303. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12304. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12305. */
  12306. Resource.prototype.fetchImage = function (options) {
  12307. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  12308. var preferImageBitmap = when.defaultValue(options.preferImageBitmap, false);
  12309. var preferBlob = when.defaultValue(options.preferBlob, false);
  12310. var flipY = when.defaultValue(options.flipY, false);
  12311. checkAndResetRequest(this.request);
  12312. // We try to load the image normally if
  12313. // 1. Blobs aren't supported
  12314. // 2. It's a data URI
  12315. // 3. It's a blob URI
  12316. // 4. It doesn't have request headers and we preferBlob is false
  12317. if (
  12318. !xhrBlobSupported ||
  12319. this.isDataUri ||
  12320. this.isBlobUri ||
  12321. (!this.hasHeaders && !preferBlob)
  12322. ) {
  12323. return fetchImage({
  12324. resource: this,
  12325. flipY: flipY,
  12326. preferImageBitmap: preferImageBitmap,
  12327. });
  12328. }
  12329. var blobPromise = this.fetchBlob();
  12330. if (!when.defined(blobPromise)) {
  12331. return;
  12332. }
  12333. var supportsImageBitmap;
  12334. var useImageBitmap;
  12335. var generatedBlobResource;
  12336. var generatedBlob;
  12337. return Resource.supportsImageBitmapOptions()
  12338. .then(function (result) {
  12339. supportsImageBitmap = result;
  12340. useImageBitmap = supportsImageBitmap && preferImageBitmap;
  12341. return blobPromise;
  12342. })
  12343. .then(function (blob) {
  12344. if (!when.defined(blob)) {
  12345. return;
  12346. }
  12347. generatedBlob = blob;
  12348. if (useImageBitmap) {
  12349. return Resource.createImageBitmapFromBlob(blob, {
  12350. flipY: flipY,
  12351. premultiplyAlpha: false,
  12352. });
  12353. }
  12354. var blobUrl = window.URL.createObjectURL(blob);
  12355. generatedBlobResource = new Resource({
  12356. url: blobUrl,
  12357. });
  12358. return fetchImage({
  12359. resource: generatedBlobResource,
  12360. flipY: flipY,
  12361. preferImageBitmap: false,
  12362. });
  12363. })
  12364. .then(function (image) {
  12365. if (!when.defined(image)) {
  12366. return;
  12367. }
  12368. // The blob object may be needed for use by a TileDiscardPolicy,
  12369. // so attach it to the image.
  12370. image.blob = generatedBlob;
  12371. if (useImageBitmap) {
  12372. return image;
  12373. }
  12374. window.URL.revokeObjectURL(generatedBlobResource.url);
  12375. return image;
  12376. })
  12377. .otherwise(function (error) {
  12378. if (when.defined(generatedBlobResource)) {
  12379. window.URL.revokeObjectURL(generatedBlobResource.url);
  12380. }
  12381. // If the blob load succeeded but the image decode failed, attach the blob
  12382. // to the error object for use by a TileDiscardPolicy.
  12383. // In particular, BingMapsImageryProvider uses this to detect the
  12384. // zero-length response that is returned when a tile is not available.
  12385. error.blob = generatedBlob;
  12386. return when.when.reject(error);
  12387. });
  12388. };
  12389. /**
  12390. * Fetches an image and returns a promise to it.
  12391. *
  12392. * @param {Object} [options] An object with the following properties.
  12393. * @param {Resource} [options.resource] Resource object that points to an image to fetch.
  12394. * @param {Boolean} [options.preferImageBitmap] If true, image will be decoded during fetch and an <code>ImageBitmap</code> is returned.
  12395. * @param {Boolean} [options.flipY] If true, image will be vertically flipped during decode. Only applies if the browser supports <code>createImageBitmap</code>.
  12396. *
  12397. * @private
  12398. */
  12399. function fetchImage(options) {
  12400. var resource = options.resource;
  12401. var flipY = options.flipY;
  12402. var preferImageBitmap = options.preferImageBitmap;
  12403. var request = resource.request;
  12404. request.url = resource.url;
  12405. request.requestFunction = function () {
  12406. var crossOrigin = false;
  12407. // data URIs can't have crossorigin set.
  12408. if (!resource.isDataUri && !resource.isBlobUri) {
  12409. crossOrigin = resource.isCrossOriginUrl;
  12410. }
  12411. var deferred = when.when.defer();
  12412. Resource._Implementations.createImage(
  12413. request,
  12414. crossOrigin,
  12415. deferred,
  12416. flipY,
  12417. preferImageBitmap
  12418. );
  12419. return deferred.promise;
  12420. };
  12421. var promise = RequestScheduler.request(request);
  12422. if (!when.defined(promise)) {
  12423. return;
  12424. }
  12425. return promise.otherwise(function (e) {
  12426. // Don't retry cancelled or otherwise aborted requests
  12427. if (request.state !== RequestState$1.FAILED) {
  12428. return when.when.reject(e);
  12429. }
  12430. return resource.retryOnError(e).then(function (retry) {
  12431. if (retry) {
  12432. // Reset request so it can try again
  12433. request.state = RequestState$1.UNISSUED;
  12434. request.deferred = undefined;
  12435. return fetchImage({
  12436. resource: resource,
  12437. flipY: flipY,
  12438. preferImageBitmap: preferImageBitmap,
  12439. });
  12440. }
  12441. return when.when.reject(e);
  12442. });
  12443. });
  12444. }
  12445. /**
  12446. * Creates a Resource and calls fetchImage() on it.
  12447. *
  12448. * @param {String|Object} options A url or an object with the following properties
  12449. * @param {String} options.url The url of the resource.
  12450. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12451. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12452. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12453. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12454. * @param {Boolean} [options.flipY=false] Whether to vertically flip the image during fetch and decode. Only applies when requesting an image and the browser supports <code>createImageBitmap</code>.
  12455. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12456. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12457. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12458. * @param {Boolean} [options.preferBlob=false] If true, we will load the image via a blob.
  12459. * @param {Boolean} [options.preferImageBitmap=false] If true, image will be decoded during fetch and an <code>ImageBitmap</code> is returned.
  12460. * @returns {Promise.<ImageBitmap>|Promise.<HTMLImageElement>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12461. */
  12462. Resource.fetchImage = function (options) {
  12463. var resource = new Resource(options);
  12464. return resource.fetchImage({
  12465. flipY: options.flipY,
  12466. preferBlob: options.preferBlob,
  12467. preferImageBitmap: options.preferImageBitmap,
  12468. });
  12469. };
  12470. /**
  12471. * Asynchronously loads the given resource as text. Returns a promise that will resolve to
  12472. * a String once loaded, or reject if the resource failed to load. The data is loaded
  12473. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12474. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12475. *
  12476. * @returns {Promise.<String>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12477. *
  12478. * @example
  12479. * // load text from a URL, setting a custom header
  12480. * var resource = new Resource({
  12481. * url: 'http://someUrl.com/someJson.txt',
  12482. * headers: {
  12483. * 'X-Custom-Header' : 'some value'
  12484. * }
  12485. * });
  12486. * resource.fetchText().then(function(text) {
  12487. * // Do something with the text
  12488. * }).otherwise(function(error) {
  12489. * // an error occurred
  12490. * });
  12491. *
  12492. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  12493. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12494. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12495. */
  12496. Resource.prototype.fetchText = function () {
  12497. return this.fetch({
  12498. responseType: "text",
  12499. });
  12500. };
  12501. /**
  12502. * Creates a Resource and calls fetchText() on it.
  12503. *
  12504. * @param {String|Object} options A url or an object with the following properties
  12505. * @param {String} options.url The url of the resource.
  12506. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12507. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12508. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12509. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12510. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12511. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12512. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12513. * @returns {Promise.<String>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12514. */
  12515. Resource.fetchText = function (options) {
  12516. var resource = new Resource(options);
  12517. return resource.fetchText();
  12518. };
  12519. // note: &#42;&#47;&#42; below is */* but that ends the comment block early
  12520. /**
  12521. * Asynchronously loads the given resource as JSON. Returns a promise that will resolve to
  12522. * a JSON object once loaded, or reject if the resource failed to load. The data is loaded
  12523. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12524. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled. This function
  12525. * adds 'Accept: application/json,&#42;&#47;&#42;;q=0.01' to the request headers, if not
  12526. * already specified.
  12527. *
  12528. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12529. *
  12530. *
  12531. * @example
  12532. * resource.fetchJson().then(function(jsonData) {
  12533. * // Do something with the JSON object
  12534. * }).otherwise(function(error) {
  12535. * // an error occurred
  12536. * });
  12537. *
  12538. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12539. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12540. */
  12541. Resource.prototype.fetchJson = function () {
  12542. var promise = this.fetch({
  12543. responseType: "text",
  12544. headers: {
  12545. Accept: "application/json,*/*;q=0.01",
  12546. },
  12547. });
  12548. if (!when.defined(promise)) {
  12549. return undefined;
  12550. }
  12551. return promise.then(function (value) {
  12552. if (!when.defined(value)) {
  12553. return;
  12554. }
  12555. return JSON.parse(value);
  12556. });
  12557. };
  12558. /**
  12559. * Creates a Resource and calls fetchJson() on it.
  12560. *
  12561. * @param {String|Object} options A url or an object with the following properties
  12562. * @param {String} options.url The url of the resource.
  12563. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12564. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12565. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12566. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12567. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12568. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12569. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12570. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12571. */
  12572. Resource.fetchJson = function (options) {
  12573. var resource = new Resource(options);
  12574. return resource.fetchJson();
  12575. };
  12576. /**
  12577. * Asynchronously loads the given resource as XML. Returns a promise that will resolve to
  12578. * an XML Document once loaded, or reject if the resource failed to load. The data is loaded
  12579. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12580. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12581. *
  12582. * @returns {Promise.<XMLDocument>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12583. *
  12584. *
  12585. * @example
  12586. * // load XML from a URL, setting a custom header
  12587. * Cesium.loadXML('http://someUrl.com/someXML.xml', {
  12588. * 'X-Custom-Header' : 'some value'
  12589. * }).then(function(document) {
  12590. * // Do something with the document
  12591. * }).otherwise(function(error) {
  12592. * // an error occurred
  12593. * });
  12594. *
  12595. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  12596. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12597. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12598. */
  12599. Resource.prototype.fetchXML = function () {
  12600. return this.fetch({
  12601. responseType: "document",
  12602. overrideMimeType: "text/xml",
  12603. });
  12604. };
  12605. /**
  12606. * Creates a Resource and calls fetchXML() on it.
  12607. *
  12608. * @param {String|Object} options A url or an object with the following properties
  12609. * @param {String} options.url The url of the resource.
  12610. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12611. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12612. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12613. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12614. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12615. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12616. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12617. * @returns {Promise.<XMLDocument>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12618. */
  12619. Resource.fetchXML = function (options) {
  12620. var resource = new Resource(options);
  12621. return resource.fetchXML();
  12622. };
  12623. /**
  12624. * Requests a resource using JSONP.
  12625. *
  12626. * @param {String} [callbackParameterName='callback'] The callback parameter name that the server expects.
  12627. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12628. *
  12629. *
  12630. * @example
  12631. * // load a data asynchronously
  12632. * resource.fetchJsonp().then(function(data) {
  12633. * // use the loaded data
  12634. * }).otherwise(function(error) {
  12635. * // an error occurred
  12636. * });
  12637. *
  12638. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12639. */
  12640. Resource.prototype.fetchJsonp = function (callbackParameterName) {
  12641. callbackParameterName = when.defaultValue(callbackParameterName, "callback");
  12642. checkAndResetRequest(this.request);
  12643. //generate a unique function name
  12644. var functionName;
  12645. do {
  12646. functionName = "loadJsonp" + Math.random().toString().substring(2, 8);
  12647. } while (when.defined(window[functionName]));
  12648. return fetchJsonp(this, callbackParameterName, functionName);
  12649. };
  12650. function fetchJsonp(resource, callbackParameterName, functionName) {
  12651. var callbackQuery = {};
  12652. callbackQuery[callbackParameterName] = functionName;
  12653. resource.setQueryParameters(callbackQuery);
  12654. var request = resource.request;
  12655. request.url = resource.url;
  12656. request.requestFunction = function () {
  12657. var deferred = when.when.defer();
  12658. //assign a function with that name in the global scope
  12659. window[functionName] = function (data) {
  12660. deferred.resolve(data);
  12661. try {
  12662. delete window[functionName];
  12663. } catch (e) {
  12664. window[functionName] = undefined;
  12665. }
  12666. };
  12667. Resource._Implementations.loadAndExecuteScript(
  12668. resource.url,
  12669. functionName,
  12670. deferred
  12671. );
  12672. return deferred.promise;
  12673. };
  12674. var promise = RequestScheduler.request(request);
  12675. if (!when.defined(promise)) {
  12676. return;
  12677. }
  12678. return promise.otherwise(function (e) {
  12679. if (request.state !== RequestState$1.FAILED) {
  12680. return when.when.reject(e);
  12681. }
  12682. return resource.retryOnError(e).then(function (retry) {
  12683. if (retry) {
  12684. // Reset request so it can try again
  12685. request.state = RequestState$1.UNISSUED;
  12686. request.deferred = undefined;
  12687. return fetchJsonp(resource, callbackParameterName, functionName);
  12688. }
  12689. return when.when.reject(e);
  12690. });
  12691. });
  12692. }
  12693. /**
  12694. * Creates a Resource from a URL and calls fetchJsonp() on it.
  12695. *
  12696. * @param {String|Object} options A url or an object with the following properties
  12697. * @param {String} options.url The url of the resource.
  12698. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12699. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12700. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12701. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12702. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12703. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12704. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12705. * @param {String} [options.callbackParameterName='callback'] The callback parameter name that the server expects.
  12706. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12707. */
  12708. Resource.fetchJsonp = function (options) {
  12709. var resource = new Resource(options);
  12710. return resource.fetchJsonp(options.callbackParameterName);
  12711. };
  12712. /**
  12713. * @private
  12714. */
  12715. Resource.prototype._makeRequest = function (options) {
  12716. var resource = this;
  12717. checkAndResetRequest(resource.request);
  12718. var request = resource.request;
  12719. request.url = resource.url;
  12720. request.requestFunction = function () {
  12721. var responseType = options.responseType;
  12722. var headers = combine(options.headers, resource.headers);
  12723. var overrideMimeType = options.overrideMimeType;
  12724. var method = options.method;
  12725. var data = options.data;
  12726. var deferred = when.when.defer();
  12727. var xhr = Resource._Implementations.loadWithXhr(
  12728. resource.url,
  12729. responseType,
  12730. method,
  12731. data,
  12732. headers,
  12733. deferred,
  12734. overrideMimeType
  12735. );
  12736. if (when.defined(xhr) && when.defined(xhr.abort)) {
  12737. request.cancelFunction = function () {
  12738. xhr.abort();
  12739. };
  12740. }
  12741. return deferred.promise;
  12742. };
  12743. var promise = RequestScheduler.request(request);
  12744. if (!when.defined(promise)) {
  12745. return;
  12746. }
  12747. return promise
  12748. .then(function (data) {
  12749. // explicitly set to undefined to ensure GC of request response data. See #8843
  12750. request.cancelFunction = undefined;
  12751. return data;
  12752. })
  12753. .otherwise(function (e) {
  12754. request.cancelFunction = undefined;
  12755. if (request.state !== RequestState$1.FAILED) {
  12756. return when.when.reject(e);
  12757. }
  12758. return resource.retryOnError(e).then(function (retry) {
  12759. if (retry) {
  12760. // Reset request so it can try again
  12761. request.state = RequestState$1.UNISSUED;
  12762. request.deferred = undefined;
  12763. return resource.fetch(options);
  12764. }
  12765. return when.when.reject(e);
  12766. });
  12767. });
  12768. };
  12769. var dataUriRegex$1 = /^data:(.*?)(;base64)?,(.*)$/;
  12770. function decodeDataUriText(isBase64, data) {
  12771. var result = decodeURIComponent(data);
  12772. if (isBase64) {
  12773. return atob(result);
  12774. }
  12775. return result;
  12776. }
  12777. function decodeDataUriArrayBuffer(isBase64, data) {
  12778. var byteString = decodeDataUriText(isBase64, data);
  12779. var buffer = new ArrayBuffer(byteString.length);
  12780. var view = new Uint8Array(buffer);
  12781. for (var i = 0; i < byteString.length; i++) {
  12782. view[i] = byteString.charCodeAt(i);
  12783. }
  12784. return buffer;
  12785. }
  12786. function decodeDataUri(dataUriRegexResult, responseType) {
  12787. responseType = when.defaultValue(responseType, "");
  12788. var mimeType = dataUriRegexResult[1];
  12789. var isBase64 = !!dataUriRegexResult[2];
  12790. var data = dataUriRegexResult[3];
  12791. switch (responseType) {
  12792. case "":
  12793. case "text":
  12794. return decodeDataUriText(isBase64, data);
  12795. case "arraybuffer":
  12796. return decodeDataUriArrayBuffer(isBase64, data);
  12797. case "blob":
  12798. var buffer = decodeDataUriArrayBuffer(isBase64, data);
  12799. return new Blob([buffer], {
  12800. type: mimeType,
  12801. });
  12802. case "document":
  12803. var parser = new DOMParser();
  12804. return parser.parseFromString(
  12805. decodeDataUriText(isBase64, data),
  12806. mimeType
  12807. );
  12808. case "json":
  12809. return JSON.parse(decodeDataUriText(isBase64, data));
  12810. default:
  12811. //>>includeStart('debug', pragmas.debug);
  12812. throw new Check.DeveloperError("Unhandled responseType: " + responseType);
  12813. //>>includeEnd('debug');
  12814. }
  12815. }
  12816. /**
  12817. * Asynchronously loads the given resource. Returns a promise that will resolve to
  12818. * the result once loaded, or reject if the resource failed to load. The data is loaded
  12819. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12820. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled. It's recommended that you use
  12821. * the more specific functions eg. fetchJson, fetchBlob, etc.
  12822. *
  12823. * @param {Object} [options] Object with the following properties:
  12824. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12825. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  12826. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12827. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12828. *
  12829. *
  12830. * @example
  12831. * resource.fetch()
  12832. * .then(function(body) {
  12833. * // use the data
  12834. * }).otherwise(function(error) {
  12835. * // an error occurred
  12836. * });
  12837. *
  12838. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12839. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12840. */
  12841. Resource.prototype.fetch = function (options) {
  12842. options = defaultClone(options, {});
  12843. options.method = "GET";
  12844. return this._makeRequest(options);
  12845. };
  12846. /**
  12847. * Creates a Resource from a URL and calls fetch() on it.
  12848. *
  12849. * @param {String|Object} options A url or an object with the following properties
  12850. * @param {String} options.url The url of the resource.
  12851. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12852. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12853. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12854. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12855. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12856. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12857. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12858. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12859. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12860. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12861. */
  12862. Resource.fetch = function (options) {
  12863. var resource = new Resource(options);
  12864. return resource.fetch({
  12865. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  12866. responseType: options.responseType,
  12867. overrideMimeType: options.overrideMimeType,
  12868. });
  12869. };
  12870. /**
  12871. * Asynchronously deletes the given resource. Returns a promise that will resolve to
  12872. * the result once loaded, or reject if the resource failed to load. The data is loaded
  12873. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12874. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12875. *
  12876. * @param {Object} [options] Object with the following properties:
  12877. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12878. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  12879. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12880. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12881. *
  12882. *
  12883. * @example
  12884. * resource.delete()
  12885. * .then(function(body) {
  12886. * // use the data
  12887. * }).otherwise(function(error) {
  12888. * // an error occurred
  12889. * });
  12890. *
  12891. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12892. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12893. */
  12894. Resource.prototype.delete = function (options) {
  12895. options = defaultClone(options, {});
  12896. options.method = "DELETE";
  12897. return this._makeRequest(options);
  12898. };
  12899. /**
  12900. * Creates a Resource from a URL and calls delete() on it.
  12901. *
  12902. * @param {String|Object} options A url or an object with the following properties
  12903. * @param {String} options.url The url of the resource.
  12904. * @param {Object} [options.data] Data that is posted with the resource.
  12905. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12906. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12907. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12908. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12909. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12910. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12911. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12912. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12913. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12914. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12915. */
  12916. Resource.delete = function (options) {
  12917. var resource = new Resource(options);
  12918. return resource.delete({
  12919. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  12920. responseType: options.responseType,
  12921. overrideMimeType: options.overrideMimeType,
  12922. data: options.data,
  12923. });
  12924. };
  12925. /**
  12926. * Asynchronously gets headers the given resource. Returns a promise that will resolve to
  12927. * the result once loaded, or reject if the resource failed to load. The data is loaded
  12928. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12929. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12930. *
  12931. * @param {Object} [options] Object with the following properties:
  12932. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12933. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  12934. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12935. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12936. *
  12937. *
  12938. * @example
  12939. * resource.head()
  12940. * .then(function(headers) {
  12941. * // use the data
  12942. * }).otherwise(function(error) {
  12943. * // an error occurred
  12944. * });
  12945. *
  12946. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  12947. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  12948. */
  12949. Resource.prototype.head = function (options) {
  12950. options = defaultClone(options, {});
  12951. options.method = "HEAD";
  12952. return this._makeRequest(options);
  12953. };
  12954. /**
  12955. * Creates a Resource from a URL and calls head() on it.
  12956. *
  12957. * @param {String|Object} options A url or an object with the following properties
  12958. * @param {String} options.url The url of the resource.
  12959. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  12960. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  12961. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  12962. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  12963. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  12964. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  12965. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  12966. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12967. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12968. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12969. */
  12970. Resource.head = function (options) {
  12971. var resource = new Resource(options);
  12972. return resource.head({
  12973. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  12974. responseType: options.responseType,
  12975. overrideMimeType: options.overrideMimeType,
  12976. });
  12977. };
  12978. /**
  12979. * Asynchronously gets options the given resource. Returns a promise that will resolve to
  12980. * the result once loaded, or reject if the resource failed to load. The data is loaded
  12981. * using XMLHttpRequest, which means that in order to make requests to another origin,
  12982. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  12983. *
  12984. * @param {Object} [options] Object with the following properties:
  12985. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  12986. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  12987. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  12988. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  12989. *
  12990. *
  12991. * @example
  12992. * resource.options()
  12993. * .then(function(headers) {
  12994. * // use the data
  12995. * }).otherwise(function(error) {
  12996. * // an error occurred
  12997. * });
  12998. *
  12999. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  13000. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  13001. */
  13002. Resource.prototype.options = function (options) {
  13003. options = defaultClone(options, {});
  13004. options.method = "OPTIONS";
  13005. return this._makeRequest(options);
  13006. };
  13007. /**
  13008. * Creates a Resource from a URL and calls options() on it.
  13009. *
  13010. * @param {String|Object} options A url or an object with the following properties
  13011. * @param {String} options.url The url of the resource.
  13012. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  13013. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  13014. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  13015. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  13016. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  13017. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  13018. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  13019. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13020. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13021. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13022. */
  13023. Resource.options = function (options) {
  13024. var resource = new Resource(options);
  13025. return resource.options({
  13026. // Make copy of just the needed fields because headers can be passed to both the constructor and to fetch
  13027. responseType: options.responseType,
  13028. overrideMimeType: options.overrideMimeType,
  13029. });
  13030. };
  13031. /**
  13032. * Asynchronously posts data to the given resource. Returns a promise that will resolve to
  13033. * the result once loaded, or reject if the resource failed to load. The data is loaded
  13034. * using XMLHttpRequest, which means that in order to make requests to another origin,
  13035. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  13036. *
  13037. * @param {Object} data Data that is posted with the resource.
  13038. * @param {Object} [options] Object with the following properties:
  13039. * @param {Object} [options.data] Data that is posted with the resource.
  13040. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13041. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  13042. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13043. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13044. *
  13045. *
  13046. * @example
  13047. * resource.post(data)
  13048. * .then(function(result) {
  13049. * // use the result
  13050. * }).otherwise(function(error) {
  13051. * // an error occurred
  13052. * });
  13053. *
  13054. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  13055. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  13056. */
  13057. Resource.prototype.post = function (data, options) {
  13058. Check.Check.defined("data", data);
  13059. options = defaultClone(options, {});
  13060. options.method = "POST";
  13061. options.data = data;
  13062. return this._makeRequest(options);
  13063. };
  13064. /**
  13065. * Creates a Resource from a URL and calls post() on it.
  13066. *
  13067. * @param {Object} options A url or an object with the following properties
  13068. * @param {String} options.url The url of the resource.
  13069. * @param {Object} options.data Data that is posted with the resource.
  13070. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  13071. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  13072. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  13073. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  13074. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  13075. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  13076. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  13077. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13078. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13079. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13080. */
  13081. Resource.post = function (options) {
  13082. var resource = new Resource(options);
  13083. return resource.post(options.data, {
  13084. // Make copy of just the needed fields because headers can be passed to both the constructor and to post
  13085. responseType: options.responseType,
  13086. overrideMimeType: options.overrideMimeType,
  13087. });
  13088. };
  13089. /**
  13090. * Asynchronously puts data to the given resource. Returns a promise that will resolve to
  13091. * the result once loaded, or reject if the resource failed to load. The data is loaded
  13092. * using XMLHttpRequest, which means that in order to make requests to another origin,
  13093. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  13094. *
  13095. * @param {Object} data Data that is posted with the resource.
  13096. * @param {Object} [options] Object with the following properties:
  13097. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13098. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  13099. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13100. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13101. *
  13102. *
  13103. * @example
  13104. * resource.put(data)
  13105. * .then(function(result) {
  13106. * // use the result
  13107. * }).otherwise(function(error) {
  13108. * // an error occurred
  13109. * });
  13110. *
  13111. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  13112. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  13113. */
  13114. Resource.prototype.put = function (data, options) {
  13115. Check.Check.defined("data", data);
  13116. options = defaultClone(options, {});
  13117. options.method = "PUT";
  13118. options.data = data;
  13119. return this._makeRequest(options);
  13120. };
  13121. /**
  13122. * Creates a Resource from a URL and calls put() on it.
  13123. *
  13124. * @param {Object} options A url or an object with the following properties
  13125. * @param {String} options.url The url of the resource.
  13126. * @param {Object} options.data Data that is posted with the resource.
  13127. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  13128. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  13129. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  13130. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  13131. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  13132. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  13133. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  13134. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13135. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13136. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13137. */
  13138. Resource.put = function (options) {
  13139. var resource = new Resource(options);
  13140. return resource.put(options.data, {
  13141. // Make copy of just the needed fields because headers can be passed to both the constructor and to post
  13142. responseType: options.responseType,
  13143. overrideMimeType: options.overrideMimeType,
  13144. });
  13145. };
  13146. /**
  13147. * Asynchronously patches data to the given resource. Returns a promise that will resolve to
  13148. * the result once loaded, or reject if the resource failed to load. The data is loaded
  13149. * using XMLHttpRequest, which means that in order to make requests to another origin,
  13150. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  13151. *
  13152. * @param {Object} data Data that is posted with the resource.
  13153. * @param {Object} [options] Object with the following properties:
  13154. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13155. * @param {Object} [options.headers] Additional HTTP headers to send with the request, if any.
  13156. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13157. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13158. *
  13159. *
  13160. * @example
  13161. * resource.patch(data)
  13162. * .then(function(result) {
  13163. * // use the result
  13164. * }).otherwise(function(error) {
  13165. * // an error occurred
  13166. * });
  13167. *
  13168. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  13169. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  13170. */
  13171. Resource.prototype.patch = function (data, options) {
  13172. Check.Check.defined("data", data);
  13173. options = defaultClone(options, {});
  13174. options.method = "PATCH";
  13175. options.data = data;
  13176. return this._makeRequest(options);
  13177. };
  13178. /**
  13179. * Creates a Resource from a URL and calls patch() on it.
  13180. *
  13181. * @param {Object} options A url or an object with the following properties
  13182. * @param {String} options.url The url of the resource.
  13183. * @param {Object} options.data Data that is posted with the resource.
  13184. * @param {Object} [options.queryParameters] An object containing query parameters that will be sent when retrieving the resource.
  13185. * @param {Object} [options.templateValues] Key/Value pairs that are used to replace template values (eg. {x}).
  13186. * @param {Object} [options.headers={}] Additional HTTP headers that will be sent.
  13187. * @param {Proxy} [options.proxy] A proxy to be used when loading the resource.
  13188. * @param {Resource.RetryCallback} [options.retryCallback] The Function to call when a request for this resource fails. If it returns true, the request will be retried.
  13189. * @param {Number} [options.retryAttempts=0] The number of times the retryCallback should be called before giving up.
  13190. * @param {Request} [options.request] A Request object that will be used. Intended for internal use only.
  13191. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  13192. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  13193. * @returns {Promise.<*>|undefined} a promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  13194. */
  13195. Resource.patch = function (options) {
  13196. var resource = new Resource(options);
  13197. return resource.patch(options.data, {
  13198. // Make copy of just the needed fields because headers can be passed to both the constructor and to post
  13199. responseType: options.responseType,
  13200. overrideMimeType: options.overrideMimeType,
  13201. });
  13202. };
  13203. /**
  13204. * Contains implementations of functions that can be replaced for testing
  13205. *
  13206. * @private
  13207. */
  13208. Resource._Implementations = {};
  13209. function loadImageElement(url, crossOrigin, deferred) {
  13210. var image = new Image();
  13211. image.onload = function () {
  13212. deferred.resolve(image);
  13213. };
  13214. image.onerror = function (e) {
  13215. deferred.reject(e);
  13216. };
  13217. if (crossOrigin) {
  13218. if (TrustedServers.contains(url)) {
  13219. image.crossOrigin = "use-credentials";
  13220. } else {
  13221. image.crossOrigin = "";
  13222. }
  13223. }
  13224. image.src = url;
  13225. }
  13226. Resource._Implementations.createImage = function (
  13227. request,
  13228. crossOrigin,
  13229. deferred,
  13230. flipY,
  13231. preferImageBitmap
  13232. ) {
  13233. var url = request.url;
  13234. // Passing an Image to createImageBitmap will force it to run on the main thread
  13235. // since DOM elements don't exist on workers. We convert it to a blob so it's non-blocking.
  13236. // See:
  13237. // https://bugzilla.mozilla.org/show_bug.cgi?id=1044102#c38
  13238. // https://bugs.chromium.org/p/chromium/issues/detail?id=580202#c10
  13239. Resource.supportsImageBitmapOptions()
  13240. .then(function (supportsImageBitmap) {
  13241. // We can only use ImageBitmap if we can flip on decode.
  13242. // See: https://github.com/CesiumGS/cesium/pull/7579#issuecomment-466146898
  13243. if (!(supportsImageBitmap && preferImageBitmap)) {
  13244. loadImageElement(url, crossOrigin, deferred);
  13245. return;
  13246. }
  13247. var responseType = "blob";
  13248. var method = "GET";
  13249. var xhrDeferred = when.when.defer();
  13250. var xhr = Resource._Implementations.loadWithXhr(
  13251. url,
  13252. responseType,
  13253. method,
  13254. undefined,
  13255. undefined,
  13256. xhrDeferred,
  13257. undefined,
  13258. undefined,
  13259. undefined
  13260. );
  13261. if (when.defined(xhr) && when.defined(xhr.abort)) {
  13262. request.cancelFunction = function () {
  13263. xhr.abort();
  13264. };
  13265. }
  13266. return xhrDeferred.promise
  13267. .then(function (blob) {
  13268. if (!when.defined(blob)) {
  13269. deferred.reject(
  13270. new RuntimeError.RuntimeError(
  13271. "Successfully retrieved " +
  13272. url +
  13273. " but it contained no content."
  13274. )
  13275. );
  13276. return;
  13277. }
  13278. return Resource.createImageBitmapFromBlob(blob, {
  13279. flipY: flipY,
  13280. premultiplyAlpha: false,
  13281. });
  13282. })
  13283. .then(deferred.resolve);
  13284. })
  13285. .otherwise(deferred.reject);
  13286. };
  13287. /**
  13288. * Wrapper for createImageBitmap
  13289. *
  13290. * @private
  13291. */
  13292. Resource.createImageBitmapFromBlob = function (blob, options) {
  13293. Check.Check.defined("options", options);
  13294. Check.Check.typeOf.bool("options.flipY", options.flipY);
  13295. Check.Check.typeOf.bool("options.premultiplyAlpha", options.premultiplyAlpha);
  13296. return createImageBitmap(blob, {
  13297. imageOrientation: options.flipY ? "flipY" : "none",
  13298. premultiplyAlpha: options.premultiplyAlpha ? "premultiply" : "none",
  13299. });
  13300. };
  13301. function decodeResponse(loadWithHttpResponse, responseType) {
  13302. switch (responseType) {
  13303. case "text":
  13304. return loadWithHttpResponse.toString("utf8");
  13305. case "json":
  13306. return JSON.parse(loadWithHttpResponse.toString("utf8"));
  13307. default:
  13308. return new Uint8Array(loadWithHttpResponse).buffer;
  13309. }
  13310. }
  13311. function loadWithHttpRequest(
  13312. url,
  13313. responseType,
  13314. method,
  13315. data,
  13316. headers,
  13317. deferred,
  13318. overrideMimeType
  13319. ) {
  13320. // Note: only the 'json' and 'text' responseTypes transforms the loaded buffer
  13321. /* eslint-disable no-undef */
  13322. var URL = require("url").parse(url);
  13323. var http = URL.protocol === "https:" ? require("https") : require("http");
  13324. var zlib = require("zlib");
  13325. /* eslint-enable no-undef */
  13326. var options = {
  13327. protocol: URL.protocol,
  13328. hostname: URL.hostname,
  13329. port: URL.port,
  13330. path: URL.path,
  13331. query: URL.query,
  13332. method: method,
  13333. headers: headers,
  13334. };
  13335. http
  13336. .request(options)
  13337. .on("response", function (res) {
  13338. if (res.statusCode < 200 || res.statusCode >= 300) {
  13339. deferred.reject(
  13340. new RequestErrorEvent(res.statusCode, res, res.headers)
  13341. );
  13342. return;
  13343. }
  13344. var chunkArray = [];
  13345. res.on("data", function (chunk) {
  13346. chunkArray.push(chunk);
  13347. });
  13348. res.on("end", function () {
  13349. // eslint-disable-next-line no-undef
  13350. var result = Buffer.concat(chunkArray);
  13351. if (res.headers["content-encoding"] === "gzip") {
  13352. zlib.gunzip(result, function (error, resultUnzipped) {
  13353. if (error) {
  13354. deferred.reject(
  13355. new RuntimeError.RuntimeError("Error decompressing response.")
  13356. );
  13357. } else {
  13358. deferred.resolve(decodeResponse(resultUnzipped, responseType));
  13359. }
  13360. });
  13361. } else {
  13362. deferred.resolve(decodeResponse(result, responseType));
  13363. }
  13364. });
  13365. })
  13366. .on("error", function (e) {
  13367. deferred.reject(new RequestErrorEvent());
  13368. })
  13369. .end();
  13370. }
  13371. var noXMLHttpRequest = typeof XMLHttpRequest === "undefined";
  13372. Resource._Implementations.loadWithXhr = function (
  13373. url,
  13374. responseType,
  13375. method,
  13376. data,
  13377. headers,
  13378. deferred,
  13379. overrideMimeType
  13380. ) {
  13381. var dataUriRegexResult = dataUriRegex$1.exec(url);
  13382. if (dataUriRegexResult !== null) {
  13383. deferred.resolve(decodeDataUri(dataUriRegexResult, responseType));
  13384. return;
  13385. }
  13386. if (noXMLHttpRequest) {
  13387. loadWithHttpRequest(
  13388. url,
  13389. responseType,
  13390. method,
  13391. data,
  13392. headers,
  13393. deferred);
  13394. return;
  13395. }
  13396. var xhr = new XMLHttpRequest();
  13397. if (TrustedServers.contains(url)) {
  13398. xhr.withCredentials = true;
  13399. }
  13400. xhr.open(method, url, true);
  13401. if (when.defined(overrideMimeType) && when.defined(xhr.overrideMimeType)) {
  13402. xhr.overrideMimeType(overrideMimeType);
  13403. }
  13404. if (when.defined(headers)) {
  13405. for (var key in headers) {
  13406. if (headers.hasOwnProperty(key)) {
  13407. xhr.setRequestHeader(key, headers[key]);
  13408. }
  13409. }
  13410. }
  13411. if (when.defined(responseType)) {
  13412. xhr.responseType = responseType;
  13413. }
  13414. // While non-standard, file protocol always returns a status of 0 on success
  13415. var localFile = false;
  13416. if (typeof url === "string") {
  13417. localFile =
  13418. url.indexOf("file://") === 0 ||
  13419. (typeof window !== "undefined" && window.location.origin === "file://");
  13420. }
  13421. xhr.onload = function () {
  13422. if (
  13423. (xhr.status < 200 || xhr.status >= 300) &&
  13424. !(localFile && xhr.status === 0)
  13425. ) {
  13426. deferred.reject(
  13427. new RequestErrorEvent(
  13428. xhr.status,
  13429. xhr.response,
  13430. xhr.getAllResponseHeaders()
  13431. )
  13432. );
  13433. return;
  13434. }
  13435. var response = xhr.response;
  13436. var browserResponseType = xhr.responseType;
  13437. if (method === "HEAD" || method === "OPTIONS") {
  13438. var responseHeaderString = xhr.getAllResponseHeaders();
  13439. var splitHeaders = responseHeaderString.trim().split(/[\r\n]+/);
  13440. var responseHeaders = {};
  13441. splitHeaders.forEach(function (line) {
  13442. var parts = line.split(": ");
  13443. var header = parts.shift();
  13444. responseHeaders[header] = parts.join(": ");
  13445. });
  13446. deferred.resolve(responseHeaders);
  13447. return;
  13448. }
  13449. //All modern browsers will go into either the first or second if block or last else block.
  13450. //Other code paths support older browsers that either do not support the supplied responseType
  13451. //or do not support the xhr.response property.
  13452. if (xhr.status === 204) {
  13453. // accept no content
  13454. deferred.resolve();
  13455. } else if (
  13456. when.defined(response) &&
  13457. (!when.defined(responseType) || browserResponseType === responseType)
  13458. ) {
  13459. deferred.resolve(response);
  13460. } else if (responseType === "json" && typeof response === "string") {
  13461. try {
  13462. deferred.resolve(JSON.parse(response));
  13463. } catch (e) {
  13464. deferred.reject(e);
  13465. }
  13466. } else if (
  13467. (browserResponseType === "" || browserResponseType === "document") &&
  13468. when.defined(xhr.responseXML) &&
  13469. xhr.responseXML.hasChildNodes()
  13470. ) {
  13471. deferred.resolve(xhr.responseXML);
  13472. } else if (
  13473. (browserResponseType === "" || browserResponseType === "text") &&
  13474. when.defined(xhr.responseText)
  13475. ) {
  13476. deferred.resolve(xhr.responseText);
  13477. } else {
  13478. deferred.reject(
  13479. new RuntimeError.RuntimeError("Invalid XMLHttpRequest response type.")
  13480. );
  13481. }
  13482. };
  13483. xhr.onerror = function (e) {
  13484. deferred.reject(new RequestErrorEvent());
  13485. };
  13486. xhr.send(data);
  13487. return xhr;
  13488. };
  13489. Resource._Implementations.loadAndExecuteScript = function (
  13490. url,
  13491. functionName,
  13492. deferred
  13493. ) {
  13494. return loadAndExecuteScript(url).otherwise(deferred.reject);
  13495. };
  13496. /**
  13497. * The default implementations
  13498. *
  13499. * @private
  13500. */
  13501. Resource._DefaultImplementations = {};
  13502. Resource._DefaultImplementations.createImage =
  13503. Resource._Implementations.createImage;
  13504. Resource._DefaultImplementations.loadWithXhr =
  13505. Resource._Implementations.loadWithXhr;
  13506. Resource._DefaultImplementations.loadAndExecuteScript =
  13507. Resource._Implementations.loadAndExecuteScript;
  13508. /**
  13509. * A resource instance initialized to the current browser location
  13510. *
  13511. * @type {Resource}
  13512. * @constant
  13513. */
  13514. Resource.DEFAULT = Object.freeze(
  13515. new Resource({
  13516. url:
  13517. typeof document === "undefined"
  13518. ? ""
  13519. : document.location.href.split("?")[0],
  13520. })
  13521. );
  13522. /**
  13523. * Specifies Earth polar motion coordinates and the difference between UT1 and UTC.
  13524. * These Earth Orientation Parameters (EOP) are primarily used in the transformation from
  13525. * the International Celestial Reference Frame (ICRF) to the International Terrestrial
  13526. * Reference Frame (ITRF).
  13527. *
  13528. * @alias EarthOrientationParameters
  13529. * @constructor
  13530. *
  13531. * @param {Object} [options] Object with the following properties:
  13532. * @param {Resource|String} [options.url] The URL from which to obtain EOP data. If neither this
  13533. * parameter nor options.data is specified, all EOP values are assumed
  13534. * to be 0.0. If options.data is specified, this parameter is
  13535. * ignored.
  13536. * @param {Object} [options.data] The actual EOP data. If neither this
  13537. * parameter nor options.data is specified, all EOP values are assumed
  13538. * to be 0.0.
  13539. * @param {Boolean} [options.addNewLeapSeconds=true] True if leap seconds that
  13540. * are specified in the EOP data but not in {@link JulianDate.leapSeconds}
  13541. * should be added to {@link JulianDate.leapSeconds}. False if
  13542. * new leap seconds should be handled correctly in the context
  13543. * of the EOP data but otherwise ignored.
  13544. *
  13545. * @example
  13546. * // An example EOP data file, EOP.json:
  13547. * {
  13548. * "columnNames" : ["dateIso8601","modifiedJulianDateUtc","xPoleWanderRadians","yPoleWanderRadians","ut1MinusUtcSeconds","lengthOfDayCorrectionSeconds","xCelestialPoleOffsetRadians","yCelestialPoleOffsetRadians","taiMinusUtcSeconds"],
  13549. * "samples" : [
  13550. * "2011-07-01T00:00:00Z",55743.0,2.117957047295119e-7,2.111518721609984e-6,-0.2908948,-2.956e-4,3.393695767766752e-11,3.3452143996557983e-10,34.0,
  13551. * "2011-07-02T00:00:00Z",55744.0,2.193297093339541e-7,2.115460256837405e-6,-0.29065,-1.824e-4,-8.241832578862112e-11,5.623838700870617e-10,34.0,
  13552. * "2011-07-03T00:00:00Z",55745.0,2.262286080161428e-7,2.1191157519929706e-6,-0.2905572,1.9e-6,-3.490658503988659e-10,6.981317007977318e-10,34.0
  13553. * ]
  13554. * }
  13555. *
  13556. * @example
  13557. * // Loading the EOP data
  13558. * var eop = new Cesium.EarthOrientationParameters({ url : 'Data/EOP.json' });
  13559. * Cesium.Transforms.earthOrientationParameters = eop;
  13560. *
  13561. * @private
  13562. */
  13563. function EarthOrientationParameters(options) {
  13564. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  13565. this._dates = undefined;
  13566. this._samples = undefined;
  13567. this._dateColumn = -1;
  13568. this._xPoleWanderRadiansColumn = -1;
  13569. this._yPoleWanderRadiansColumn = -1;
  13570. this._ut1MinusUtcSecondsColumn = -1;
  13571. this._xCelestialPoleOffsetRadiansColumn = -1;
  13572. this._yCelestialPoleOffsetRadiansColumn = -1;
  13573. this._taiMinusUtcSecondsColumn = -1;
  13574. this._columnCount = 0;
  13575. this._lastIndex = -1;
  13576. this._downloadPromise = undefined;
  13577. this._dataError = undefined;
  13578. this._addNewLeapSeconds = when.defaultValue(options.addNewLeapSeconds, true);
  13579. if (when.defined(options.data)) {
  13580. // Use supplied EOP data.
  13581. onDataReady(this, options.data);
  13582. } else if (when.defined(options.url)) {
  13583. var resource = Resource.createIfNeeded(options.url);
  13584. // Download EOP data.
  13585. var that = this;
  13586. this._downloadPromise = resource
  13587. .fetchJson()
  13588. .then(function (eopData) {
  13589. onDataReady(that, eopData);
  13590. })
  13591. .otherwise(function () {
  13592. that._dataError =
  13593. "An error occurred while retrieving the EOP data from the URL " +
  13594. resource.url +
  13595. ".";
  13596. });
  13597. } else {
  13598. // Use all zeros for EOP data.
  13599. onDataReady(this, {
  13600. columnNames: [
  13601. "dateIso8601",
  13602. "modifiedJulianDateUtc",
  13603. "xPoleWanderRadians",
  13604. "yPoleWanderRadians",
  13605. "ut1MinusUtcSeconds",
  13606. "lengthOfDayCorrectionSeconds",
  13607. "xCelestialPoleOffsetRadians",
  13608. "yCelestialPoleOffsetRadians",
  13609. "taiMinusUtcSeconds",
  13610. ],
  13611. samples: [],
  13612. });
  13613. }
  13614. }
  13615. /**
  13616. * A default {@link EarthOrientationParameters} instance that returns zero for all EOP values.
  13617. */
  13618. EarthOrientationParameters.NONE = Object.freeze({
  13619. getPromiseToLoad: function () {
  13620. return when.when.resolve();
  13621. },
  13622. compute: function (date, result) {
  13623. if (!when.defined(result)) {
  13624. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  13625. } else {
  13626. result.xPoleWander = 0.0;
  13627. result.yPoleWander = 0.0;
  13628. result.xPoleOffset = 0.0;
  13629. result.yPoleOffset = 0.0;
  13630. result.ut1MinusUtc = 0.0;
  13631. }
  13632. return result;
  13633. },
  13634. });
  13635. /**
  13636. * Gets a promise that, when resolved, indicates that the EOP data has been loaded and is
  13637. * ready to use.
  13638. *
  13639. * @returns {Promise<void>} The promise.
  13640. */
  13641. EarthOrientationParameters.prototype.getPromiseToLoad = function () {
  13642. return when.when(this._downloadPromise);
  13643. };
  13644. /**
  13645. * Computes the Earth Orientation Parameters (EOP) for a given date by interpolating.
  13646. * If the EOP data has not yet been download, this method returns undefined.
  13647. *
  13648. * @param {JulianDate} date The date for each to evaluate the EOP.
  13649. * @param {EarthOrientationParametersSample} [result] The instance to which to copy the result.
  13650. * If this parameter is undefined, a new instance is created and returned.
  13651. * @returns {EarthOrientationParametersSample} The EOP evaluated at the given date, or
  13652. * undefined if the data necessary to evaluate EOP at the date has not yet been
  13653. * downloaded.
  13654. *
  13655. * @exception {RuntimeError} The loaded EOP data has an error and cannot be used.
  13656. *
  13657. * @see EarthOrientationParameters#getPromiseToLoad
  13658. */
  13659. EarthOrientationParameters.prototype.compute = function (date, result) {
  13660. // We cannot compute until the samples are available.
  13661. if (!when.defined(this._samples)) {
  13662. if (when.defined(this._dataError)) {
  13663. throw new RuntimeError.RuntimeError(this._dataError);
  13664. }
  13665. return undefined;
  13666. }
  13667. if (!when.defined(result)) {
  13668. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  13669. }
  13670. if (this._samples.length === 0) {
  13671. result.xPoleWander = 0.0;
  13672. result.yPoleWander = 0.0;
  13673. result.xPoleOffset = 0.0;
  13674. result.yPoleOffset = 0.0;
  13675. result.ut1MinusUtc = 0.0;
  13676. return result;
  13677. }
  13678. var dates = this._dates;
  13679. var lastIndex = this._lastIndex;
  13680. var before = 0;
  13681. var after = 0;
  13682. if (when.defined(lastIndex)) {
  13683. var previousIndexDate = dates[lastIndex];
  13684. var nextIndexDate = dates[lastIndex + 1];
  13685. var isAfterPrevious = JulianDate.lessThanOrEquals(previousIndexDate, date);
  13686. var isAfterLastSample = !when.defined(nextIndexDate);
  13687. var isBeforeNext =
  13688. isAfterLastSample || JulianDate.greaterThanOrEquals(nextIndexDate, date);
  13689. if (isAfterPrevious && isBeforeNext) {
  13690. before = lastIndex;
  13691. if (!isAfterLastSample && nextIndexDate.equals(date)) {
  13692. ++before;
  13693. }
  13694. after = before + 1;
  13695. interpolate(this, dates, this._samples, date, before, after, result);
  13696. return result;
  13697. }
  13698. }
  13699. var index = binarySearch(dates, date, JulianDate.compare, this._dateColumn);
  13700. if (index >= 0) {
  13701. // If the next entry is the same date, use the later entry. This way, if two entries
  13702. // describe the same moment, one before a leap second and the other after, then we will use
  13703. // the post-leap second data.
  13704. if (index < dates.length - 1 && dates[index + 1].equals(date)) {
  13705. ++index;
  13706. }
  13707. before = index;
  13708. after = index;
  13709. } else {
  13710. after = ~index;
  13711. before = after - 1;
  13712. // Use the first entry if the date requested is before the beginning of the data.
  13713. if (before < 0) {
  13714. before = 0;
  13715. }
  13716. }
  13717. this._lastIndex = before;
  13718. interpolate(this, dates, this._samples, date, before, after, result);
  13719. return result;
  13720. };
  13721. function compareLeapSecondDates$1(leapSecond, dateToFind) {
  13722. return JulianDate.compare(leapSecond.julianDate, dateToFind);
  13723. }
  13724. function onDataReady(eop, eopData) {
  13725. if (!when.defined(eopData.columnNames)) {
  13726. eop._dataError =
  13727. "Error in loaded EOP data: The columnNames property is required.";
  13728. return;
  13729. }
  13730. if (!when.defined(eopData.samples)) {
  13731. eop._dataError =
  13732. "Error in loaded EOP data: The samples property is required.";
  13733. return;
  13734. }
  13735. var dateColumn = eopData.columnNames.indexOf("modifiedJulianDateUtc");
  13736. var xPoleWanderRadiansColumn = eopData.columnNames.indexOf(
  13737. "xPoleWanderRadians"
  13738. );
  13739. var yPoleWanderRadiansColumn = eopData.columnNames.indexOf(
  13740. "yPoleWanderRadians"
  13741. );
  13742. var ut1MinusUtcSecondsColumn = eopData.columnNames.indexOf(
  13743. "ut1MinusUtcSeconds"
  13744. );
  13745. var xCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf(
  13746. "xCelestialPoleOffsetRadians"
  13747. );
  13748. var yCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf(
  13749. "yCelestialPoleOffsetRadians"
  13750. );
  13751. var taiMinusUtcSecondsColumn = eopData.columnNames.indexOf(
  13752. "taiMinusUtcSeconds"
  13753. );
  13754. if (
  13755. dateColumn < 0 ||
  13756. xPoleWanderRadiansColumn < 0 ||
  13757. yPoleWanderRadiansColumn < 0 ||
  13758. ut1MinusUtcSecondsColumn < 0 ||
  13759. xCelestialPoleOffsetRadiansColumn < 0 ||
  13760. yCelestialPoleOffsetRadiansColumn < 0 ||
  13761. taiMinusUtcSecondsColumn < 0
  13762. ) {
  13763. eop._dataError =
  13764. "Error in loaded EOP data: The columnNames property must include modifiedJulianDateUtc, xPoleWanderRadians, yPoleWanderRadians, ut1MinusUtcSeconds, xCelestialPoleOffsetRadians, yCelestialPoleOffsetRadians, and taiMinusUtcSeconds columns";
  13765. return;
  13766. }
  13767. var samples = (eop._samples = eopData.samples);
  13768. var dates = (eop._dates = []);
  13769. eop._dateColumn = dateColumn;
  13770. eop._xPoleWanderRadiansColumn = xPoleWanderRadiansColumn;
  13771. eop._yPoleWanderRadiansColumn = yPoleWanderRadiansColumn;
  13772. eop._ut1MinusUtcSecondsColumn = ut1MinusUtcSecondsColumn;
  13773. eop._xCelestialPoleOffsetRadiansColumn = xCelestialPoleOffsetRadiansColumn;
  13774. eop._yCelestialPoleOffsetRadiansColumn = yCelestialPoleOffsetRadiansColumn;
  13775. eop._taiMinusUtcSecondsColumn = taiMinusUtcSecondsColumn;
  13776. eop._columnCount = eopData.columnNames.length;
  13777. eop._lastIndex = undefined;
  13778. var lastTaiMinusUtc;
  13779. var addNewLeapSeconds = eop._addNewLeapSeconds;
  13780. // Convert the ISO8601 dates to JulianDates.
  13781. for (var i = 0, len = samples.length; i < len; i += eop._columnCount) {
  13782. var mjd = samples[i + dateColumn];
  13783. var taiMinusUtc = samples[i + taiMinusUtcSecondsColumn];
  13784. var day = mjd + TimeConstants$1.MODIFIED_JULIAN_DATE_DIFFERENCE;
  13785. var date = new JulianDate(day, taiMinusUtc, TimeStandard$1.TAI);
  13786. dates.push(date);
  13787. if (addNewLeapSeconds) {
  13788. if (taiMinusUtc !== lastTaiMinusUtc && when.defined(lastTaiMinusUtc)) {
  13789. // We crossed a leap second boundary, so add the leap second
  13790. // if it does not already exist.
  13791. var leapSeconds = JulianDate.leapSeconds;
  13792. var leapSecondIndex = binarySearch(
  13793. leapSeconds,
  13794. date,
  13795. compareLeapSecondDates$1
  13796. );
  13797. if (leapSecondIndex < 0) {
  13798. var leapSecond = new LeapSecond(date, taiMinusUtc);
  13799. leapSeconds.splice(~leapSecondIndex, 0, leapSecond);
  13800. }
  13801. }
  13802. lastTaiMinusUtc = taiMinusUtc;
  13803. }
  13804. }
  13805. }
  13806. function fillResultFromIndex(eop, samples, index, columnCount, result) {
  13807. var start = index * columnCount;
  13808. result.xPoleWander = samples[start + eop._xPoleWanderRadiansColumn];
  13809. result.yPoleWander = samples[start + eop._yPoleWanderRadiansColumn];
  13810. result.xPoleOffset = samples[start + eop._xCelestialPoleOffsetRadiansColumn];
  13811. result.yPoleOffset = samples[start + eop._yCelestialPoleOffsetRadiansColumn];
  13812. result.ut1MinusUtc = samples[start + eop._ut1MinusUtcSecondsColumn];
  13813. }
  13814. function linearInterp(dx, y1, y2) {
  13815. return y1 + dx * (y2 - y1);
  13816. }
  13817. function interpolate(eop, dates, samples, date, before, after, result) {
  13818. var columnCount = eop._columnCount;
  13819. // First check the bounds on the EOP data
  13820. // If we are after the bounds of the data, return zeros.
  13821. // The 'before' index should never be less than zero.
  13822. if (after > dates.length - 1) {
  13823. result.xPoleWander = 0;
  13824. result.yPoleWander = 0;
  13825. result.xPoleOffset = 0;
  13826. result.yPoleOffset = 0;
  13827. result.ut1MinusUtc = 0;
  13828. return result;
  13829. }
  13830. var beforeDate = dates[before];
  13831. var afterDate = dates[after];
  13832. if (beforeDate.equals(afterDate) || date.equals(beforeDate)) {
  13833. fillResultFromIndex(eop, samples, before, columnCount, result);
  13834. return result;
  13835. } else if (date.equals(afterDate)) {
  13836. fillResultFromIndex(eop, samples, after, columnCount, result);
  13837. return result;
  13838. }
  13839. var factor =
  13840. JulianDate.secondsDifference(date, beforeDate) /
  13841. JulianDate.secondsDifference(afterDate, beforeDate);
  13842. var startBefore = before * columnCount;
  13843. var startAfter = after * columnCount;
  13844. // Handle UT1 leap second edge case
  13845. var beforeUt1MinusUtc = samples[startBefore + eop._ut1MinusUtcSecondsColumn];
  13846. var afterUt1MinusUtc = samples[startAfter + eop._ut1MinusUtcSecondsColumn];
  13847. var offsetDifference = afterUt1MinusUtc - beforeUt1MinusUtc;
  13848. if (offsetDifference > 0.5 || offsetDifference < -0.5) {
  13849. // The absolute difference between the values is more than 0.5, so we may have
  13850. // crossed a leap second. Check if this is the case and, if so, adjust the
  13851. // afterValue to account for the leap second. This way, our interpolation will
  13852. // produce reasonable results.
  13853. var beforeTaiMinusUtc =
  13854. samples[startBefore + eop._taiMinusUtcSecondsColumn];
  13855. var afterTaiMinusUtc = samples[startAfter + eop._taiMinusUtcSecondsColumn];
  13856. if (beforeTaiMinusUtc !== afterTaiMinusUtc) {
  13857. if (afterDate.equals(date)) {
  13858. // If we are at the end of the leap second interval, take the second value
  13859. // Otherwise, the interpolation below will yield the wrong side of the
  13860. // discontinuity
  13861. // At the end of the leap second, we need to start accounting for the jump
  13862. beforeUt1MinusUtc = afterUt1MinusUtc;
  13863. } else {
  13864. // Otherwise, remove the leap second so that the interpolation is correct
  13865. afterUt1MinusUtc -= afterTaiMinusUtc - beforeTaiMinusUtc;
  13866. }
  13867. }
  13868. }
  13869. result.xPoleWander = linearInterp(
  13870. factor,
  13871. samples[startBefore + eop._xPoleWanderRadiansColumn],
  13872. samples[startAfter + eop._xPoleWanderRadiansColumn]
  13873. );
  13874. result.yPoleWander = linearInterp(
  13875. factor,
  13876. samples[startBefore + eop._yPoleWanderRadiansColumn],
  13877. samples[startAfter + eop._yPoleWanderRadiansColumn]
  13878. );
  13879. result.xPoleOffset = linearInterp(
  13880. factor,
  13881. samples[startBefore + eop._xCelestialPoleOffsetRadiansColumn],
  13882. samples[startAfter + eop._xCelestialPoleOffsetRadiansColumn]
  13883. );
  13884. result.yPoleOffset = linearInterp(
  13885. factor,
  13886. samples[startBefore + eop._yCelestialPoleOffsetRadiansColumn],
  13887. samples[startAfter + eop._yCelestialPoleOffsetRadiansColumn]
  13888. );
  13889. result.ut1MinusUtc = linearInterp(
  13890. factor,
  13891. beforeUt1MinusUtc,
  13892. afterUt1MinusUtc
  13893. );
  13894. return result;
  13895. }
  13896. /**
  13897. * A rotation expressed as a heading, pitch, and roll. Heading is the rotation about the
  13898. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  13899. * the positive x axis.
  13900. * @alias HeadingPitchRoll
  13901. * @constructor
  13902. *
  13903. * @param {Number} [heading=0.0] The heading component in radians.
  13904. * @param {Number} [pitch=0.0] The pitch component in radians.
  13905. * @param {Number} [roll=0.0] The roll component in radians.
  13906. */
  13907. function HeadingPitchRoll(heading, pitch, roll) {
  13908. /**
  13909. * Gets or sets the heading.
  13910. * @type {Number}
  13911. * @default 0.0
  13912. */
  13913. this.heading = when.defaultValue(heading, 0.0);
  13914. /**
  13915. * Gets or sets the pitch.
  13916. * @type {Number}
  13917. * @default 0.0
  13918. */
  13919. this.pitch = when.defaultValue(pitch, 0.0);
  13920. /**
  13921. * Gets or sets the roll.
  13922. * @type {Number}
  13923. * @default 0.0
  13924. */
  13925. this.roll = when.defaultValue(roll, 0.0);
  13926. }
  13927. /**
  13928. * Computes the heading, pitch and roll from a quaternion (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  13929. *
  13930. * @param {Quaternion} quaternion The quaternion from which to retrieve heading, pitch, and roll, all expressed in radians.
  13931. * @param {HeadingPitchRoll} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  13932. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  13933. */
  13934. HeadingPitchRoll.fromQuaternion = function (quaternion, result) {
  13935. //>>includeStart('debug', pragmas.debug);
  13936. if (!when.defined(quaternion)) {
  13937. throw new Check.DeveloperError("quaternion is required");
  13938. }
  13939. //>>includeEnd('debug');
  13940. if (!when.defined(result)) {
  13941. result = new HeadingPitchRoll();
  13942. }
  13943. var test = 2 * (quaternion.w * quaternion.y - quaternion.z * quaternion.x);
  13944. var denominatorRoll =
  13945. 1 - 2 * (quaternion.x * quaternion.x + quaternion.y * quaternion.y);
  13946. var numeratorRoll =
  13947. 2 * (quaternion.w * quaternion.x + quaternion.y * quaternion.z);
  13948. var denominatorHeading =
  13949. 1 - 2 * (quaternion.y * quaternion.y + quaternion.z * quaternion.z);
  13950. var numeratorHeading =
  13951. 2 * (quaternion.w * quaternion.z + quaternion.x * quaternion.y);
  13952. result.heading = -Math.atan2(numeratorHeading, denominatorHeading);
  13953. result.roll = Math.atan2(numeratorRoll, denominatorRoll);
  13954. result.pitch = -_Math.CesiumMath.asinClamped(test);
  13955. return result;
  13956. };
  13957. /**
  13958. * Returns a new HeadingPitchRoll instance from angles given in degrees.
  13959. *
  13960. * @param {Number} heading the heading in degrees
  13961. * @param {Number} pitch the pitch in degrees
  13962. * @param {Number} roll the heading in degrees
  13963. * @param {HeadingPitchRoll} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  13964. * @returns {HeadingPitchRoll} A new HeadingPitchRoll instance
  13965. */
  13966. HeadingPitchRoll.fromDegrees = function (heading, pitch, roll, result) {
  13967. //>>includeStart('debug', pragmas.debug);
  13968. if (!when.defined(heading)) {
  13969. throw new Check.DeveloperError("heading is required");
  13970. }
  13971. if (!when.defined(pitch)) {
  13972. throw new Check.DeveloperError("pitch is required");
  13973. }
  13974. if (!when.defined(roll)) {
  13975. throw new Check.DeveloperError("roll is required");
  13976. }
  13977. //>>includeEnd('debug');
  13978. if (!when.defined(result)) {
  13979. result = new HeadingPitchRoll();
  13980. }
  13981. result.heading = heading * _Math.CesiumMath.RADIANS_PER_DEGREE;
  13982. result.pitch = pitch * _Math.CesiumMath.RADIANS_PER_DEGREE;
  13983. result.roll = roll * _Math.CesiumMath.RADIANS_PER_DEGREE;
  13984. return result;
  13985. };
  13986. /**
  13987. * Duplicates a HeadingPitchRoll instance.
  13988. *
  13989. * @param {HeadingPitchRoll} headingPitchRoll The HeadingPitchRoll to duplicate.
  13990. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  13991. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided. (Returns undefined if headingPitchRoll is undefined)
  13992. */
  13993. HeadingPitchRoll.clone = function (headingPitchRoll, result) {
  13994. if (!when.defined(headingPitchRoll)) {
  13995. return undefined;
  13996. }
  13997. if (!when.defined(result)) {
  13998. return new HeadingPitchRoll(
  13999. headingPitchRoll.heading,
  14000. headingPitchRoll.pitch,
  14001. headingPitchRoll.roll
  14002. );
  14003. }
  14004. result.heading = headingPitchRoll.heading;
  14005. result.pitch = headingPitchRoll.pitch;
  14006. result.roll = headingPitchRoll.roll;
  14007. return result;
  14008. };
  14009. /**
  14010. * Compares the provided HeadingPitchRolls componentwise and returns
  14011. * <code>true</code> if they are equal, <code>false</code> otherwise.
  14012. *
  14013. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  14014. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  14015. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  14016. */
  14017. HeadingPitchRoll.equals = function (left, right) {
  14018. return (
  14019. left === right ||
  14020. (when.defined(left) &&
  14021. when.defined(right) &&
  14022. left.heading === right.heading &&
  14023. left.pitch === right.pitch &&
  14024. left.roll === right.roll)
  14025. );
  14026. };
  14027. /**
  14028. * Compares the provided HeadingPitchRolls componentwise and returns
  14029. * <code>true</code> if they pass an absolute or relative tolerance test,
  14030. * <code>false</code> otherwise.
  14031. *
  14032. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  14033. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  14034. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  14035. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  14036. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  14037. */
  14038. HeadingPitchRoll.equalsEpsilon = function (
  14039. left,
  14040. right,
  14041. relativeEpsilon,
  14042. absoluteEpsilon
  14043. ) {
  14044. return (
  14045. left === right ||
  14046. (when.defined(left) &&
  14047. when.defined(right) &&
  14048. _Math.CesiumMath.equalsEpsilon(
  14049. left.heading,
  14050. right.heading,
  14051. relativeEpsilon,
  14052. absoluteEpsilon
  14053. ) &&
  14054. _Math.CesiumMath.equalsEpsilon(
  14055. left.pitch,
  14056. right.pitch,
  14057. relativeEpsilon,
  14058. absoluteEpsilon
  14059. ) &&
  14060. _Math.CesiumMath.equalsEpsilon(
  14061. left.roll,
  14062. right.roll,
  14063. relativeEpsilon,
  14064. absoluteEpsilon
  14065. ))
  14066. );
  14067. };
  14068. /**
  14069. * Duplicates this HeadingPitchRoll instance.
  14070. *
  14071. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  14072. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  14073. */
  14074. HeadingPitchRoll.prototype.clone = function (result) {
  14075. return HeadingPitchRoll.clone(this, result);
  14076. };
  14077. /**
  14078. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  14079. * <code>true</code> if they are equal, <code>false</code> otherwise.
  14080. *
  14081. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  14082. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  14083. */
  14084. HeadingPitchRoll.prototype.equals = function (right) {
  14085. return HeadingPitchRoll.equals(this, right);
  14086. };
  14087. /**
  14088. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  14089. * <code>true</code> if they pass an absolute or relative tolerance test,
  14090. * <code>false</code> otherwise.
  14091. *
  14092. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  14093. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  14094. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  14095. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  14096. */
  14097. HeadingPitchRoll.prototype.equalsEpsilon = function (
  14098. right,
  14099. relativeEpsilon,
  14100. absoluteEpsilon
  14101. ) {
  14102. return HeadingPitchRoll.equalsEpsilon(
  14103. this,
  14104. right,
  14105. relativeEpsilon,
  14106. absoluteEpsilon
  14107. );
  14108. };
  14109. /**
  14110. * Creates a string representing this HeadingPitchRoll in the format '(heading, pitch, roll)' in radians.
  14111. *
  14112. * @returns {String} A string representing the provided HeadingPitchRoll in the format '(heading, pitch, roll)'.
  14113. */
  14114. HeadingPitchRoll.prototype.toString = function () {
  14115. return "(" + this.heading + ", " + this.pitch + ", " + this.roll + ")";
  14116. };
  14117. /*global CESIUM_BASE_URL*/
  14118. var cesiumScriptRegex = /((?:.*\/)|^)Cesium\.js(?:\?|\#|$)/;
  14119. function getBaseUrlFromCesiumScript() {
  14120. var scripts = document.getElementsByTagName("script");
  14121. for (var i = 0, len = scripts.length; i < len; ++i) {
  14122. var src = scripts[i].getAttribute("src");
  14123. var result = cesiumScriptRegex.exec(src);
  14124. if (result !== null) {
  14125. return result[1];
  14126. }
  14127. }
  14128. return undefined;
  14129. }
  14130. var a$1;
  14131. function tryMakeAbsolute(url) {
  14132. if (typeof document === "undefined") {
  14133. //Node.js and Web Workers. In both cases, the URL will already be absolute.
  14134. return url;
  14135. }
  14136. if (!when.defined(a$1)) {
  14137. a$1 = document.createElement("a");
  14138. }
  14139. a$1.href = url;
  14140. // IE only absolutizes href on get, not set
  14141. // eslint-disable-next-line no-self-assign
  14142. a$1.href = a$1.href;
  14143. return a$1.href;
  14144. }
  14145. var baseResource;
  14146. function getCesiumBaseUrl() {
  14147. if (when.defined(baseResource)) {
  14148. return baseResource;
  14149. }
  14150. var baseUrlString;
  14151. if (typeof CESIUM_BASE_URL !== "undefined") {
  14152. baseUrlString = CESIUM_BASE_URL;
  14153. } else if (
  14154. typeof define === "object" &&
  14155. when.defined(define.amd) &&
  14156. !define.amd.toUrlUndefined &&
  14157. when.defined(require.toUrl)
  14158. ) {
  14159. baseUrlString = getAbsoluteUri(
  14160. "..",
  14161. buildModuleUrl("Core/buildModuleUrl.js")
  14162. );
  14163. } else {
  14164. baseUrlString = getBaseUrlFromCesiumScript();
  14165. }
  14166. //>>includeStart('debug', pragmas.debug);
  14167. if (!when.defined(baseUrlString)) {
  14168. throw new Check.DeveloperError(
  14169. "Unable to determine Cesium base URL automatically, try defining a global variable called CESIUM_BASE_URL."
  14170. );
  14171. }
  14172. //>>includeEnd('debug');
  14173. baseResource = new Resource({
  14174. url: tryMakeAbsolute(baseUrlString),
  14175. });
  14176. baseResource.appendForwardSlash();
  14177. return baseResource;
  14178. }
  14179. function buildModuleUrlFromRequireToUrl(moduleID) {
  14180. //moduleID will be non-relative, so require it relative to this module, in Core.
  14181. return tryMakeAbsolute(require.toUrl("../" + moduleID));
  14182. }
  14183. function buildModuleUrlFromBaseUrl(moduleID) {
  14184. var resource = getCesiumBaseUrl().getDerivedResource({
  14185. url: moduleID,
  14186. });
  14187. return resource.url;
  14188. }
  14189. var implementation;
  14190. /**
  14191. * Given a relative URL under the Cesium base URL, returns an absolute URL.
  14192. * @function
  14193. *
  14194. * @param {String} relativeUrl The relative path.
  14195. * @returns {String} The absolutely URL representation of the provided path.
  14196. *
  14197. * @example
  14198. * var viewer = new Cesium.Viewer("cesiumContainer", {
  14199. * imageryProvider: new Cesium.TileMapServiceImageryProvider({
  14200. * url: Cesium.buildModuleUrl("Assets/Textures/NaturalEarthII"),
  14201. * }),
  14202. * baseLayerPicker: false,
  14203. * });
  14204. */
  14205. function buildModuleUrl(relativeUrl) {
  14206. if (!when.defined(implementation)) {
  14207. //select implementation
  14208. if (
  14209. typeof define === "object" &&
  14210. when.defined(define.amd) &&
  14211. !define.amd.toUrlUndefined &&
  14212. when.defined(require.toUrl)
  14213. ) {
  14214. implementation = buildModuleUrlFromRequireToUrl;
  14215. } else {
  14216. implementation = buildModuleUrlFromBaseUrl;
  14217. }
  14218. }
  14219. var url = implementation(relativeUrl);
  14220. return url;
  14221. }
  14222. // exposed for testing
  14223. buildModuleUrl._cesiumScriptRegex = cesiumScriptRegex;
  14224. buildModuleUrl._buildModuleUrlFromBaseUrl = buildModuleUrlFromBaseUrl;
  14225. buildModuleUrl._clearBaseResource = function () {
  14226. baseResource = undefined;
  14227. };
  14228. /**
  14229. * Sets the base URL for resolving modules.
  14230. * @param {String} value The new base URL.
  14231. */
  14232. buildModuleUrl.setBaseUrl = function (value) {
  14233. baseResource = Resource.DEFAULT.getDerivedResource({
  14234. url: value,
  14235. });
  14236. };
  14237. /**
  14238. * Gets the base URL for resolving modules.
  14239. */
  14240. buildModuleUrl.getCesiumBaseUrl = getCesiumBaseUrl;
  14241. /**
  14242. * An IAU 2006 XYS value sampled at a particular time.
  14243. *
  14244. * @alias Iau2006XysSample
  14245. * @constructor
  14246. *
  14247. * @param {Number} x The X value.
  14248. * @param {Number} y The Y value.
  14249. * @param {Number} s The S value.
  14250. *
  14251. * @private
  14252. */
  14253. function Iau2006XysSample(x, y, s) {
  14254. /**
  14255. * The X value.
  14256. * @type {Number}
  14257. */
  14258. this.x = x;
  14259. /**
  14260. * The Y value.
  14261. * @type {Number}
  14262. */
  14263. this.y = y;
  14264. /**
  14265. * The S value.
  14266. * @type {Number}
  14267. */
  14268. this.s = s;
  14269. }
  14270. /**
  14271. * A set of IAU2006 XYS data that is used to evaluate the transformation between the International
  14272. * Celestial Reference Frame (ICRF) and the International Terrestrial Reference Frame (ITRF).
  14273. *
  14274. * @alias Iau2006XysData
  14275. * @constructor
  14276. *
  14277. * @param {Object} [options] Object with the following properties:
  14278. * @param {Resource|String} [options.xysFileUrlTemplate='Assets/IAU2006_XYS/IAU2006_XYS_{0}.json'] A template URL for obtaining the XYS data. In the template,
  14279. * `{0}` will be replaced with the file index.
  14280. * @param {Number} [options.interpolationOrder=9] The order of interpolation to perform on the XYS data.
  14281. * @param {Number} [options.sampleZeroJulianEphemerisDate=2442396.5] The Julian ephemeris date (JED) of the
  14282. * first XYS sample.
  14283. * @param {Number} [options.stepSizeDays=1.0] The step size, in days, between successive XYS samples.
  14284. * @param {Number} [options.samplesPerXysFile=1000] The number of samples in each XYS file.
  14285. * @param {Number} [options.totalSamples=27426] The total number of samples in all XYS files.
  14286. *
  14287. * @private
  14288. */
  14289. function Iau2006XysData(options) {
  14290. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  14291. this._xysFileUrlTemplate = Resource.createIfNeeded(
  14292. options.xysFileUrlTemplate
  14293. );
  14294. this._interpolationOrder = when.defaultValue(options.interpolationOrder, 9);
  14295. this._sampleZeroJulianEphemerisDate = when.defaultValue(
  14296. options.sampleZeroJulianEphemerisDate,
  14297. 2442396.5
  14298. );
  14299. this._sampleZeroDateTT = new JulianDate(
  14300. this._sampleZeroJulianEphemerisDate,
  14301. 0.0,
  14302. TimeStandard$1.TAI
  14303. );
  14304. this._stepSizeDays = when.defaultValue(options.stepSizeDays, 1.0);
  14305. this._samplesPerXysFile = when.defaultValue(options.samplesPerXysFile, 1000);
  14306. this._totalSamples = when.defaultValue(options.totalSamples, 27426);
  14307. this._samples = new Array(this._totalSamples * 3);
  14308. this._chunkDownloadsInProgress = [];
  14309. var order = this._interpolationOrder;
  14310. // Compute denominators and X values for interpolation.
  14311. var denom = (this._denominators = new Array(order + 1));
  14312. var xTable = (this._xTable = new Array(order + 1));
  14313. var stepN = Math.pow(this._stepSizeDays, order);
  14314. for (var i = 0; i <= order; ++i) {
  14315. denom[i] = stepN;
  14316. xTable[i] = i * this._stepSizeDays;
  14317. for (var j = 0; j <= order; ++j) {
  14318. if (j !== i) {
  14319. denom[i] *= i - j;
  14320. }
  14321. }
  14322. denom[i] = 1.0 / denom[i];
  14323. }
  14324. // Allocate scratch arrays for interpolation.
  14325. this._work = new Array(order + 1);
  14326. this._coef = new Array(order + 1);
  14327. }
  14328. var julianDateScratch = new JulianDate(0, 0.0, TimeStandard$1.TAI);
  14329. function getDaysSinceEpoch(xys, dayTT, secondTT) {
  14330. var dateTT = julianDateScratch;
  14331. dateTT.dayNumber = dayTT;
  14332. dateTT.secondsOfDay = secondTT;
  14333. return JulianDate.daysDifference(dateTT, xys._sampleZeroDateTT);
  14334. }
  14335. /**
  14336. * Preloads XYS data for a specified date range.
  14337. *
  14338. * @param {Number} startDayTT The Julian day number of the beginning of the interval to preload, expressed in
  14339. * the Terrestrial Time (TT) time standard.
  14340. * @param {Number} startSecondTT The seconds past noon of the beginning of the interval to preload, expressed in
  14341. * the Terrestrial Time (TT) time standard.
  14342. * @param {Number} stopDayTT The Julian day number of the end of the interval to preload, expressed in
  14343. * the Terrestrial Time (TT) time standard.
  14344. * @param {Number} stopSecondTT The seconds past noon of the end of the interval to preload, expressed in
  14345. * the Terrestrial Time (TT) time standard.
  14346. * @returns {Promise<void>} A promise that, when resolved, indicates that the requested interval has been
  14347. * preloaded.
  14348. */
  14349. Iau2006XysData.prototype.preload = function (
  14350. startDayTT,
  14351. startSecondTT,
  14352. stopDayTT,
  14353. stopSecondTT
  14354. ) {
  14355. var startDaysSinceEpoch = getDaysSinceEpoch(this, startDayTT, startSecondTT);
  14356. var stopDaysSinceEpoch = getDaysSinceEpoch(this, stopDayTT, stopSecondTT);
  14357. var startIndex =
  14358. (startDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  14359. 0;
  14360. if (startIndex < 0) {
  14361. startIndex = 0;
  14362. }
  14363. var stopIndex =
  14364. (stopDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  14365. (0 + this._interpolationOrder);
  14366. if (stopIndex >= this._totalSamples) {
  14367. stopIndex = this._totalSamples - 1;
  14368. }
  14369. var startChunk = (startIndex / this._samplesPerXysFile) | 0;
  14370. var stopChunk = (stopIndex / this._samplesPerXysFile) | 0;
  14371. var promises = [];
  14372. for (var i = startChunk; i <= stopChunk; ++i) {
  14373. promises.push(requestXysChunk(this, i));
  14374. }
  14375. return when.when.all(promises);
  14376. };
  14377. /**
  14378. * Computes the XYS values for a given date by interpolating. If the required data is not yet downloaded,
  14379. * this method will return undefined.
  14380. *
  14381. * @param {Number} dayTT The Julian day number for which to compute the XYS value, expressed in
  14382. * the Terrestrial Time (TT) time standard.
  14383. * @param {Number} secondTT The seconds past noon of the date for which to compute the XYS value, expressed in
  14384. * the Terrestrial Time (TT) time standard.
  14385. * @param {Iau2006XysSample} [result] The instance to which to copy the interpolated result. If this parameter
  14386. * is undefined, a new instance is allocated and returned.
  14387. * @returns {Iau2006XysSample} The interpolated XYS values, or undefined if the required data for this
  14388. * computation has not yet been downloaded.
  14389. *
  14390. * @see Iau2006XysData#preload
  14391. */
  14392. Iau2006XysData.prototype.computeXysRadians = function (
  14393. dayTT,
  14394. secondTT,
  14395. result
  14396. ) {
  14397. var daysSinceEpoch = getDaysSinceEpoch(this, dayTT, secondTT);
  14398. if (daysSinceEpoch < 0.0) {
  14399. // Can't evaluate prior to the epoch of the data.
  14400. return undefined;
  14401. }
  14402. var centerIndex = (daysSinceEpoch / this._stepSizeDays) | 0;
  14403. if (centerIndex >= this._totalSamples) {
  14404. // Can't evaluate after the last sample in the data.
  14405. return undefined;
  14406. }
  14407. var degree = this._interpolationOrder;
  14408. var firstIndex = centerIndex - ((degree / 2) | 0);
  14409. if (firstIndex < 0) {
  14410. firstIndex = 0;
  14411. }
  14412. var lastIndex = firstIndex + degree;
  14413. if (lastIndex >= this._totalSamples) {
  14414. lastIndex = this._totalSamples - 1;
  14415. firstIndex = lastIndex - degree;
  14416. if (firstIndex < 0) {
  14417. firstIndex = 0;
  14418. }
  14419. }
  14420. // Are all the samples we need present?
  14421. // We can assume so if the first and last are present
  14422. var isDataMissing = false;
  14423. var samples = this._samples;
  14424. if (!when.defined(samples[firstIndex * 3])) {
  14425. requestXysChunk(this, (firstIndex / this._samplesPerXysFile) | 0);
  14426. isDataMissing = true;
  14427. }
  14428. if (!when.defined(samples[lastIndex * 3])) {
  14429. requestXysChunk(this, (lastIndex / this._samplesPerXysFile) | 0);
  14430. isDataMissing = true;
  14431. }
  14432. if (isDataMissing) {
  14433. return undefined;
  14434. }
  14435. if (!when.defined(result)) {
  14436. result = new Iau2006XysSample(0.0, 0.0, 0.0);
  14437. } else {
  14438. result.x = 0.0;
  14439. result.y = 0.0;
  14440. result.s = 0.0;
  14441. }
  14442. var x = daysSinceEpoch - firstIndex * this._stepSizeDays;
  14443. var work = this._work;
  14444. var denom = this._denominators;
  14445. var coef = this._coef;
  14446. var xTable = this._xTable;
  14447. var i, j;
  14448. for (i = 0; i <= degree; ++i) {
  14449. work[i] = x - xTable[i];
  14450. }
  14451. for (i = 0; i <= degree; ++i) {
  14452. coef[i] = 1.0;
  14453. for (j = 0; j <= degree; ++j) {
  14454. if (j !== i) {
  14455. coef[i] *= work[j];
  14456. }
  14457. }
  14458. coef[i] *= denom[i];
  14459. var sampleIndex = (firstIndex + i) * 3;
  14460. result.x += coef[i] * samples[sampleIndex++];
  14461. result.y += coef[i] * samples[sampleIndex++];
  14462. result.s += coef[i] * samples[sampleIndex];
  14463. }
  14464. return result;
  14465. };
  14466. function requestXysChunk(xysData, chunkIndex) {
  14467. if (xysData._chunkDownloadsInProgress[chunkIndex]) {
  14468. // Chunk has already been requested.
  14469. return xysData._chunkDownloadsInProgress[chunkIndex];
  14470. }
  14471. var deferred = when.when.defer();
  14472. xysData._chunkDownloadsInProgress[chunkIndex] = deferred;
  14473. var chunkUrl;
  14474. var xysFileUrlTemplate = xysData._xysFileUrlTemplate;
  14475. if (when.defined(xysFileUrlTemplate)) {
  14476. chunkUrl = xysFileUrlTemplate.getDerivedResource({
  14477. templateValues: {
  14478. "0": chunkIndex,
  14479. },
  14480. });
  14481. } else {
  14482. chunkUrl = new Resource({
  14483. url: buildModuleUrl(
  14484. "Assets/IAU2006_XYS/IAU2006_XYS_" + chunkIndex + ".json"
  14485. ),
  14486. });
  14487. }
  14488. when.when(chunkUrl.fetchJson(), function (chunk) {
  14489. xysData._chunkDownloadsInProgress[chunkIndex] = false;
  14490. var samples = xysData._samples;
  14491. var newSamples = chunk.samples;
  14492. var startIndex = chunkIndex * xysData._samplesPerXysFile * 3;
  14493. for (var i = 0, len = newSamples.length; i < len; ++i) {
  14494. samples[startIndex + i] = newSamples[i];
  14495. }
  14496. deferred.resolve();
  14497. });
  14498. return deferred.promise;
  14499. }
  14500. /**
  14501. * Contains functions for transforming positions to various reference frames.
  14502. *
  14503. * @namespace Transforms
  14504. */
  14505. var Transforms = {};
  14506. var vectorProductLocalFrame = {
  14507. up: {
  14508. south: "east",
  14509. north: "west",
  14510. west: "south",
  14511. east: "north",
  14512. },
  14513. down: {
  14514. south: "west",
  14515. north: "east",
  14516. west: "north",
  14517. east: "south",
  14518. },
  14519. south: {
  14520. up: "west",
  14521. down: "east",
  14522. west: "down",
  14523. east: "up",
  14524. },
  14525. north: {
  14526. up: "east",
  14527. down: "west",
  14528. west: "up",
  14529. east: "down",
  14530. },
  14531. west: {
  14532. up: "north",
  14533. down: "south",
  14534. north: "down",
  14535. south: "up",
  14536. },
  14537. east: {
  14538. up: "south",
  14539. down: "north",
  14540. north: "up",
  14541. south: "down",
  14542. },
  14543. };
  14544. var degeneratePositionLocalFrame = {
  14545. north: [-1, 0, 0],
  14546. east: [0, 1, 0],
  14547. up: [0, 0, 1],
  14548. south: [1, 0, 0],
  14549. west: [0, -1, 0],
  14550. down: [0, 0, -1],
  14551. };
  14552. var localFrameToFixedFrameCache = {};
  14553. var scratchCalculateCartesian = {
  14554. east: new Cartesian2.Cartesian3(),
  14555. north: new Cartesian2.Cartesian3(),
  14556. up: new Cartesian2.Cartesian3(),
  14557. west: new Cartesian2.Cartesian3(),
  14558. south: new Cartesian2.Cartesian3(),
  14559. down: new Cartesian2.Cartesian3(),
  14560. };
  14561. var scratchFirstCartesian = new Cartesian2.Cartesian3();
  14562. var scratchSecondCartesian = new Cartesian2.Cartesian3();
  14563. var scratchThirdCartesian = new Cartesian2.Cartesian3();
  14564. /**
  14565. * Generates a function that computes a 4x4 transformation matrix from a reference frame
  14566. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  14567. * @param {String} firstAxis name of the first axis of the local reference frame. Must be
  14568. * 'east', 'north', 'up', 'west', 'south' or 'down'.
  14569. * @param {String} secondAxis name of the second axis of the local reference frame. Must be
  14570. * 'east', 'north', 'up', 'west', 'south' or 'down'.
  14571. * @return {Transforms.LocalFrameToFixedFrame} The function that will computes a
  14572. * 4x4 transformation matrix from a reference frame, with first axis and second axis compliant with the parameters,
  14573. */
  14574. Transforms.localFrameToFixedFrameGenerator = function (firstAxis, secondAxis) {
  14575. if (
  14576. !vectorProductLocalFrame.hasOwnProperty(firstAxis) ||
  14577. !vectorProductLocalFrame[firstAxis].hasOwnProperty(secondAxis)
  14578. ) {
  14579. throw new Check.DeveloperError(
  14580. "firstAxis and secondAxis must be east, north, up, west, south or down."
  14581. );
  14582. }
  14583. var thirdAxis = vectorProductLocalFrame[firstAxis][secondAxis];
  14584. /**
  14585. * Computes a 4x4 transformation matrix from a reference frame
  14586. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  14587. * @callback Transforms.LocalFrameToFixedFrame
  14588. * @param {Cartesian3} origin The center point of the local reference frame.
  14589. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14590. * @param {Matrix4} [result] The object onto which to store the result.
  14591. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  14592. */
  14593. var resultat;
  14594. var hashAxis = firstAxis + secondAxis;
  14595. if (when.defined(localFrameToFixedFrameCache[hashAxis])) {
  14596. resultat = localFrameToFixedFrameCache[hashAxis];
  14597. } else {
  14598. resultat = function (origin, ellipsoid, result) {
  14599. //>>includeStart('debug', pragmas.debug);
  14600. if (!when.defined(origin)) {
  14601. throw new Check.DeveloperError("origin is required.");
  14602. }
  14603. //>>includeEnd('debug');
  14604. if (!when.defined(result)) {
  14605. result = new Matrix4();
  14606. }
  14607. if (
  14608. Cartesian2.Cartesian3.equalsEpsilon(origin, Cartesian2.Cartesian3.ZERO, _Math.CesiumMath.EPSILON14)
  14609. ) {
  14610. // If x, y, and z are zero, use the degenerate local frame, which is a special case
  14611. Cartesian2.Cartesian3.unpack(
  14612. degeneratePositionLocalFrame[firstAxis],
  14613. 0,
  14614. scratchFirstCartesian
  14615. );
  14616. Cartesian2.Cartesian3.unpack(
  14617. degeneratePositionLocalFrame[secondAxis],
  14618. 0,
  14619. scratchSecondCartesian
  14620. );
  14621. Cartesian2.Cartesian3.unpack(
  14622. degeneratePositionLocalFrame[thirdAxis],
  14623. 0,
  14624. scratchThirdCartesian
  14625. );
  14626. } else if (
  14627. _Math.CesiumMath.equalsEpsilon(origin.x, 0.0, _Math.CesiumMath.EPSILON14) &&
  14628. _Math.CesiumMath.equalsEpsilon(origin.y, 0.0, _Math.CesiumMath.EPSILON14)
  14629. ) {
  14630. // If x and y are zero, assume origin is at a pole, which is a special case.
  14631. var sign = _Math.CesiumMath.sign(origin.z);
  14632. Cartesian2.Cartesian3.unpack(
  14633. degeneratePositionLocalFrame[firstAxis],
  14634. 0,
  14635. scratchFirstCartesian
  14636. );
  14637. if (firstAxis !== "east" && firstAxis !== "west") {
  14638. Cartesian2.Cartesian3.multiplyByScalar(
  14639. scratchFirstCartesian,
  14640. sign,
  14641. scratchFirstCartesian
  14642. );
  14643. }
  14644. Cartesian2.Cartesian3.unpack(
  14645. degeneratePositionLocalFrame[secondAxis],
  14646. 0,
  14647. scratchSecondCartesian
  14648. );
  14649. if (secondAxis !== "east" && secondAxis !== "west") {
  14650. Cartesian2.Cartesian3.multiplyByScalar(
  14651. scratchSecondCartesian,
  14652. sign,
  14653. scratchSecondCartesian
  14654. );
  14655. }
  14656. Cartesian2.Cartesian3.unpack(
  14657. degeneratePositionLocalFrame[thirdAxis],
  14658. 0,
  14659. scratchThirdCartesian
  14660. );
  14661. if (thirdAxis !== "east" && thirdAxis !== "west") {
  14662. Cartesian2.Cartesian3.multiplyByScalar(
  14663. scratchThirdCartesian,
  14664. sign,
  14665. scratchThirdCartesian
  14666. );
  14667. }
  14668. } else {
  14669. ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  14670. ellipsoid.geodeticSurfaceNormal(origin, scratchCalculateCartesian.up);
  14671. var up = scratchCalculateCartesian.up;
  14672. var east = scratchCalculateCartesian.east;
  14673. east.x = -origin.y;
  14674. east.y = origin.x;
  14675. east.z = 0.0;
  14676. Cartesian2.Cartesian3.normalize(east, scratchCalculateCartesian.east);
  14677. Cartesian2.Cartesian3.cross(up, east, scratchCalculateCartesian.north);
  14678. Cartesian2.Cartesian3.multiplyByScalar(
  14679. scratchCalculateCartesian.up,
  14680. -1,
  14681. scratchCalculateCartesian.down
  14682. );
  14683. Cartesian2.Cartesian3.multiplyByScalar(
  14684. scratchCalculateCartesian.east,
  14685. -1,
  14686. scratchCalculateCartesian.west
  14687. );
  14688. Cartesian2.Cartesian3.multiplyByScalar(
  14689. scratchCalculateCartesian.north,
  14690. -1,
  14691. scratchCalculateCartesian.south
  14692. );
  14693. scratchFirstCartesian = scratchCalculateCartesian[firstAxis];
  14694. scratchSecondCartesian = scratchCalculateCartesian[secondAxis];
  14695. scratchThirdCartesian = scratchCalculateCartesian[thirdAxis];
  14696. }
  14697. result[0] = scratchFirstCartesian.x;
  14698. result[1] = scratchFirstCartesian.y;
  14699. result[2] = scratchFirstCartesian.z;
  14700. result[3] = 0.0;
  14701. result[4] = scratchSecondCartesian.x;
  14702. result[5] = scratchSecondCartesian.y;
  14703. result[6] = scratchSecondCartesian.z;
  14704. result[7] = 0.0;
  14705. result[8] = scratchThirdCartesian.x;
  14706. result[9] = scratchThirdCartesian.y;
  14707. result[10] = scratchThirdCartesian.z;
  14708. result[11] = 0.0;
  14709. result[12] = origin.x;
  14710. result[13] = origin.y;
  14711. result[14] = origin.z;
  14712. result[15] = 1.0;
  14713. return result;
  14714. };
  14715. localFrameToFixedFrameCache[hashAxis] = resultat;
  14716. }
  14717. return resultat;
  14718. };
  14719. /**
  14720. * Computes a 4x4 transformation matrix from a reference frame with an east-north-up axes
  14721. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  14722. * The local axes are defined as:
  14723. * <ul>
  14724. * <li>The <code>x</code> axis points in the local east direction.</li>
  14725. * <li>The <code>y</code> axis points in the local north direction.</li>
  14726. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  14727. * </ul>
  14728. *
  14729. * @function
  14730. * @param {Cartesian3} origin The center point of the local reference frame.
  14731. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14732. * @param {Matrix4} [result] The object onto which to store the result.
  14733. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  14734. *
  14735. * @example
  14736. * // Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame.
  14737. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  14738. * var transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);
  14739. */
  14740. Transforms.eastNorthUpToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  14741. "east",
  14742. "north"
  14743. );
  14744. /**
  14745. * Computes a 4x4 transformation matrix from a reference frame with an north-east-down axes
  14746. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  14747. * The local axes are defined as:
  14748. * <ul>
  14749. * <li>The <code>x</code> axis points in the local north direction.</li>
  14750. * <li>The <code>y</code> axis points in the local east direction.</li>
  14751. * <li>The <code>z</code> axis points in the opposite direction of the ellipsoid surface normal which passes through the position.</li>
  14752. * </ul>
  14753. *
  14754. * @function
  14755. * @param {Cartesian3} origin The center point of the local reference frame.
  14756. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14757. * @param {Matrix4} [result] The object onto which to store the result.
  14758. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  14759. *
  14760. * @example
  14761. * // Get the transform from local north-east-down at cartographic (0.0, 0.0) to Earth's fixed frame.
  14762. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  14763. * var transform = Cesium.Transforms.northEastDownToFixedFrame(center);
  14764. */
  14765. Transforms.northEastDownToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  14766. "north",
  14767. "east"
  14768. );
  14769. /**
  14770. * Computes a 4x4 transformation matrix from a reference frame with an north-up-east axes
  14771. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  14772. * The local axes are defined as:
  14773. * <ul>
  14774. * <li>The <code>x</code> axis points in the local north direction.</li>
  14775. * <li>The <code>y</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  14776. * <li>The <code>z</code> axis points in the local east direction.</li>
  14777. * </ul>
  14778. *
  14779. * @function
  14780. * @param {Cartesian3} origin The center point of the local reference frame.
  14781. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14782. * @param {Matrix4} [result] The object onto which to store the result.
  14783. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  14784. *
  14785. * @example
  14786. * // Get the transform from local north-up-east at cartographic (0.0, 0.0) to Earth's fixed frame.
  14787. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  14788. * var transform = Cesium.Transforms.northUpEastToFixedFrame(center);
  14789. */
  14790. Transforms.northUpEastToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  14791. "north",
  14792. "up"
  14793. );
  14794. /**
  14795. * Computes a 4x4 transformation matrix from a reference frame with an north-west-up axes
  14796. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  14797. * The local axes are defined as:
  14798. * <ul>
  14799. * <li>The <code>x</code> axis points in the local north direction.</li>
  14800. * <li>The <code>y</code> axis points in the local west direction.</li>
  14801. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  14802. * </ul>
  14803. *
  14804. * @function
  14805. * @param {Cartesian3} origin The center point of the local reference frame.
  14806. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14807. * @param {Matrix4} [result] The object onto which to store the result.
  14808. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  14809. *
  14810. * @example
  14811. * // Get the transform from local north-West-Up at cartographic (0.0, 0.0) to Earth's fixed frame.
  14812. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  14813. * var transform = Cesium.Transforms.northWestUpToFixedFrame(center);
  14814. */
  14815. Transforms.northWestUpToFixedFrame = Transforms.localFrameToFixedFrameGenerator(
  14816. "north",
  14817. "west"
  14818. );
  14819. var scratchHPRQuaternion$1 = new Quaternion();
  14820. var scratchScale$2 = new Cartesian2.Cartesian3(1.0, 1.0, 1.0);
  14821. var scratchHPRMatrix4 = new Matrix4();
  14822. /**
  14823. * Computes a 4x4 transformation matrix from a reference frame with axes computed from the heading-pitch-roll angles
  14824. * centered at the provided origin to the provided ellipsoid's fixed reference frame. Heading is the rotation from the local north
  14825. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  14826. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  14827. *
  14828. * @param {Cartesian3} origin The center point of the local reference frame.
  14829. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  14830. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14831. * @param {Transforms.LocalFrameToFixedFrame} [fixedFrameTransform=Transforms.eastNorthUpToFixedFrame] A 4x4 transformation
  14832. * matrix from a reference frame to the provided ellipsoid's fixed reference frame
  14833. * @param {Matrix4} [result] The object onto which to store the result.
  14834. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  14835. *
  14836. * @example
  14837. * // Get the transform from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  14838. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  14839. * var heading = -Cesium.Math.PI_OVER_TWO;
  14840. * var pitch = Cesium.Math.PI_OVER_FOUR;
  14841. * var roll = 0.0;
  14842. * var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  14843. * var transform = Cesium.Transforms.headingPitchRollToFixedFrame(center, hpr);
  14844. */
  14845. Transforms.headingPitchRollToFixedFrame = function (
  14846. origin,
  14847. headingPitchRoll,
  14848. ellipsoid,
  14849. fixedFrameTransform,
  14850. result
  14851. ) {
  14852. //>>includeStart('debug', pragmas.debug);
  14853. Check.Check.typeOf.object("HeadingPitchRoll", headingPitchRoll);
  14854. //>>includeEnd('debug');
  14855. fixedFrameTransform = when.defaultValue(
  14856. fixedFrameTransform,
  14857. Transforms.eastNorthUpToFixedFrame
  14858. );
  14859. var hprQuaternion = Quaternion.fromHeadingPitchRoll(
  14860. headingPitchRoll,
  14861. scratchHPRQuaternion$1
  14862. );
  14863. var hprMatrix = Matrix4.fromTranslationQuaternionRotationScale(
  14864. Cartesian2.Cartesian3.ZERO,
  14865. hprQuaternion,
  14866. scratchScale$2,
  14867. scratchHPRMatrix4
  14868. );
  14869. result = fixedFrameTransform(origin, ellipsoid, result);
  14870. return Matrix4.multiply(result, hprMatrix, result);
  14871. };
  14872. var scratchENUMatrix4 = new Matrix4();
  14873. var scratchHPRMatrix3 = new Matrix3();
  14874. /**
  14875. * Computes a quaternion from a reference frame with axes computed from the heading-pitch-roll angles
  14876. * centered at the provided origin. Heading is the rotation from the local north
  14877. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  14878. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  14879. *
  14880. * @param {Cartesian3} origin The center point of the local reference frame.
  14881. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  14882. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14883. * @param {Transforms.LocalFrameToFixedFrame} [fixedFrameTransform=Transforms.eastNorthUpToFixedFrame] A 4x4 transformation
  14884. * matrix from a reference frame to the provided ellipsoid's fixed reference frame
  14885. * @param {Quaternion} [result] The object onto which to store the result.
  14886. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  14887. *
  14888. * @example
  14889. * // Get the quaternion from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  14890. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  14891. * var heading = -Cesium.Math.PI_OVER_TWO;
  14892. * var pitch = Cesium.Math.PI_OVER_FOUR;
  14893. * var roll = 0.0;
  14894. * var hpr = new HeadingPitchRoll(heading, pitch, roll);
  14895. * var quaternion = Cesium.Transforms.headingPitchRollQuaternion(center, hpr);
  14896. */
  14897. Transforms.headingPitchRollQuaternion = function (
  14898. origin,
  14899. headingPitchRoll,
  14900. ellipsoid,
  14901. fixedFrameTransform,
  14902. result
  14903. ) {
  14904. //>>includeStart('debug', pragmas.debug);
  14905. Check.Check.typeOf.object("HeadingPitchRoll", headingPitchRoll);
  14906. //>>includeEnd('debug');
  14907. var transform = Transforms.headingPitchRollToFixedFrame(
  14908. origin,
  14909. headingPitchRoll,
  14910. ellipsoid,
  14911. fixedFrameTransform,
  14912. scratchENUMatrix4
  14913. );
  14914. var rotation = Matrix4.getMatrix3(transform, scratchHPRMatrix3);
  14915. return Quaternion.fromRotationMatrix(rotation, result);
  14916. };
  14917. var noScale = new Cartesian2.Cartesian3(1.0, 1.0, 1.0);
  14918. var hprCenterScratch = new Cartesian2.Cartesian3();
  14919. var ffScratch = new Matrix4();
  14920. var hprTransformScratch = new Matrix4();
  14921. var hprRotationScratch = new Matrix3();
  14922. var hprQuaternionScratch = new Quaternion();
  14923. /**
  14924. * Computes heading-pitch-roll angles from a transform in a particular reference frame. Heading is the rotation from the local north
  14925. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  14926. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  14927. *
  14928. * @param {Matrix4} transform The transform
  14929. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  14930. * @param {Transforms.LocalFrameToFixedFrame} [fixedFrameTransform=Transforms.eastNorthUpToFixedFrame] A 4x4 transformation
  14931. * matrix from a reference frame to the provided ellipsoid's fixed reference frame
  14932. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  14933. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if none was provided.
  14934. */
  14935. Transforms.fixedFrameToHeadingPitchRoll = function (
  14936. transform,
  14937. ellipsoid,
  14938. fixedFrameTransform,
  14939. result
  14940. ) {
  14941. //>>includeStart('debug', pragmas.debug);
  14942. Check.Check.defined("transform", transform);
  14943. //>>includeEnd('debug');
  14944. ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  14945. fixedFrameTransform = when.defaultValue(
  14946. fixedFrameTransform,
  14947. Transforms.eastNorthUpToFixedFrame
  14948. );
  14949. if (!when.defined(result)) {
  14950. result = new HeadingPitchRoll();
  14951. }
  14952. var center = Matrix4.getTranslation(transform, hprCenterScratch);
  14953. if (Cartesian2.Cartesian3.equals(center, Cartesian2.Cartesian3.ZERO)) {
  14954. result.heading = 0;
  14955. result.pitch = 0;
  14956. result.roll = 0;
  14957. return result;
  14958. }
  14959. var toFixedFrame = Matrix4.inverseTransformation(
  14960. fixedFrameTransform(center, ellipsoid, ffScratch),
  14961. ffScratch
  14962. );
  14963. var transformCopy = Matrix4.setScale(transform, noScale, hprTransformScratch);
  14964. transformCopy = Matrix4.setTranslation(
  14965. transformCopy,
  14966. Cartesian2.Cartesian3.ZERO,
  14967. transformCopy
  14968. );
  14969. toFixedFrame = Matrix4.multiply(toFixedFrame, transformCopy, toFixedFrame);
  14970. var quaternionRotation = Quaternion.fromRotationMatrix(
  14971. Matrix4.getMatrix3(toFixedFrame, hprRotationScratch),
  14972. hprQuaternionScratch
  14973. );
  14974. quaternionRotation = Quaternion.normalize(
  14975. quaternionRotation,
  14976. quaternionRotation
  14977. );
  14978. return HeadingPitchRoll.fromQuaternion(quaternionRotation, result);
  14979. };
  14980. var gmstConstant0 = 6 * 3600 + 41 * 60 + 50.54841;
  14981. var gmstConstant1 = 8640184.812866;
  14982. var gmstConstant2 = 0.093104;
  14983. var gmstConstant3 = -6.2e-6;
  14984. var rateCoef = 1.1772758384668e-19;
  14985. var wgs84WRPrecessing = 7.2921158553e-5;
  14986. var twoPiOverSecondsInDay = _Math.CesiumMath.TWO_PI / 86400.0;
  14987. var dateInUtc = new JulianDate();
  14988. /**
  14989. * Computes a rotation matrix to transform a point or vector from True Equator Mean Equinox (TEME) axes to the
  14990. * pseudo-fixed axes at a given time. This method treats the UT1 time standard as equivalent to UTC.
  14991. *
  14992. * @param {JulianDate} date The time at which to compute the rotation matrix.
  14993. * @param {Matrix3} [result] The object onto which to store the result.
  14994. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if none was provided.
  14995. *
  14996. * @example
  14997. * //Set the view to the inertial frame.
  14998. * scene.postUpdate.addEventListener(function(scene, time) {
  14999. * var now = Cesium.JulianDate.now();
  15000. * var offset = Cesium.Matrix4.multiplyByPoint(camera.transform, camera.position, new Cesium.Cartesian3());
  15001. * var transform = Cesium.Matrix4.fromRotationTranslation(Cesium.Transforms.computeTemeToPseudoFixedMatrix(now));
  15002. * var inverseTransform = Cesium.Matrix4.inverseTransformation(transform, new Cesium.Matrix4());
  15003. * Cesium.Matrix4.multiplyByPoint(inverseTransform, offset, offset);
  15004. * camera.lookAtTransform(transform, offset);
  15005. * });
  15006. */
  15007. Transforms.computeTemeToPseudoFixedMatrix = function (date, result) {
  15008. //>>includeStart('debug', pragmas.debug);
  15009. if (!when.defined(date)) {
  15010. throw new Check.DeveloperError("date is required.");
  15011. }
  15012. //>>includeEnd('debug');
  15013. // GMST is actually computed using UT1. We're using UTC as an approximation of UT1.
  15014. // We do not want to use the function like convertTaiToUtc in JulianDate because
  15015. // we explicitly do not want to fail when inside the leap second.
  15016. dateInUtc = JulianDate.addSeconds(
  15017. date,
  15018. -JulianDate.computeTaiMinusUtc(date),
  15019. dateInUtc
  15020. );
  15021. var utcDayNumber = dateInUtc.dayNumber;
  15022. var utcSecondsIntoDay = dateInUtc.secondsOfDay;
  15023. var t;
  15024. var diffDays = utcDayNumber - 2451545;
  15025. if (utcSecondsIntoDay >= 43200.0) {
  15026. t = (diffDays + 0.5) / TimeConstants$1.DAYS_PER_JULIAN_CENTURY;
  15027. } else {
  15028. t = (diffDays - 0.5) / TimeConstants$1.DAYS_PER_JULIAN_CENTURY;
  15029. }
  15030. var gmst0 =
  15031. gmstConstant0 +
  15032. t * (gmstConstant1 + t * (gmstConstant2 + t * gmstConstant3));
  15033. var angle = (gmst0 * twoPiOverSecondsInDay) % _Math.CesiumMath.TWO_PI;
  15034. var ratio = wgs84WRPrecessing + rateCoef * (utcDayNumber - 2451545.5);
  15035. var secondsSinceMidnight =
  15036. (utcSecondsIntoDay + TimeConstants$1.SECONDS_PER_DAY * 0.5) %
  15037. TimeConstants$1.SECONDS_PER_DAY;
  15038. var gha = angle + ratio * secondsSinceMidnight;
  15039. var cosGha = Math.cos(gha);
  15040. var sinGha = Math.sin(gha);
  15041. if (!when.defined(result)) {
  15042. return new Matrix3(
  15043. cosGha,
  15044. sinGha,
  15045. 0.0,
  15046. -sinGha,
  15047. cosGha,
  15048. 0.0,
  15049. 0.0,
  15050. 0.0,
  15051. 1.0
  15052. );
  15053. }
  15054. result[0] = cosGha;
  15055. result[1] = -sinGha;
  15056. result[2] = 0.0;
  15057. result[3] = sinGha;
  15058. result[4] = cosGha;
  15059. result[5] = 0.0;
  15060. result[6] = 0.0;
  15061. result[7] = 0.0;
  15062. result[8] = 1.0;
  15063. return result;
  15064. };
  15065. /**
  15066. * The source of IAU 2006 XYS data, used for computing the transformation between the
  15067. * Fixed and ICRF axes.
  15068. * @type {Iau2006XysData}
  15069. *
  15070. * @see Transforms.computeIcrfToFixedMatrix
  15071. * @see Transforms.computeFixedToIcrfMatrix
  15072. *
  15073. * @private
  15074. */
  15075. Transforms.iau2006XysData = new Iau2006XysData();
  15076. /**
  15077. * The source of Earth Orientation Parameters (EOP) data, used for computing the transformation
  15078. * between the Fixed and ICRF axes. By default, zero values are used for all EOP values,
  15079. * yielding a reasonable but not completely accurate representation of the ICRF axes.
  15080. * @type {EarthOrientationParameters}
  15081. *
  15082. * @see Transforms.computeIcrfToFixedMatrix
  15083. * @see Transforms.computeFixedToIcrfMatrix
  15084. *
  15085. * @private
  15086. */
  15087. Transforms.earthOrientationParameters = EarthOrientationParameters.NONE;
  15088. var ttMinusTai = 32.184;
  15089. var j2000ttDays = 2451545.0;
  15090. /**
  15091. * Preloads the data necessary to transform between the ICRF and Fixed axes, in either
  15092. * direction, over a given interval. This function returns a promise that, when resolved,
  15093. * indicates that the preload has completed.
  15094. *
  15095. * @param {TimeInterval} timeInterval The interval to preload.
  15096. * @returns {Promise<void>} A promise that, when resolved, indicates that the preload has completed
  15097. * and evaluation of the transformation between the fixed and ICRF axes will
  15098. * no longer return undefined for a time inside the interval.
  15099. *
  15100. *
  15101. * @example
  15102. * var interval = new Cesium.TimeInterval(...);
  15103. * when(Cesium.Transforms.preloadIcrfFixed(interval), function() {
  15104. * // the data is now loaded
  15105. * });
  15106. *
  15107. * @see Transforms.computeIcrfToFixedMatrix
  15108. * @see Transforms.computeFixedToIcrfMatrix
  15109. * @see when
  15110. */
  15111. Transforms.preloadIcrfFixed = function (timeInterval) {
  15112. var startDayTT = timeInterval.start.dayNumber;
  15113. var startSecondTT = timeInterval.start.secondsOfDay + ttMinusTai;
  15114. var stopDayTT = timeInterval.stop.dayNumber;
  15115. var stopSecondTT = timeInterval.stop.secondsOfDay + ttMinusTai;
  15116. var xysPromise = Transforms.iau2006XysData.preload(
  15117. startDayTT,
  15118. startSecondTT,
  15119. stopDayTT,
  15120. stopSecondTT
  15121. );
  15122. var eopPromise = Transforms.earthOrientationParameters.getPromiseToLoad();
  15123. return when.when.all([xysPromise, eopPromise]);
  15124. };
  15125. /**
  15126. * Computes a rotation matrix to transform a point or vector from the International Celestial
  15127. * Reference Frame (GCRF/ICRF) inertial frame axes to the Earth-Fixed frame axes (ITRF)
  15128. * at a given time. This function may return undefined if the data necessary to
  15129. * do the transformation is not yet loaded.
  15130. *
  15131. * @param {JulianDate} date The time at which to compute the rotation matrix.
  15132. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  15133. * not specified, a new instance is created and returned.
  15134. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  15135. * transformation is not yet loaded.
  15136. *
  15137. *
  15138. * @example
  15139. * scene.postUpdate.addEventListener(function(scene, time) {
  15140. * // View in ICRF.
  15141. * var icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time);
  15142. * if (Cesium.defined(icrfToFixed)) {
  15143. * var offset = Cesium.Cartesian3.clone(camera.position);
  15144. * var transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);
  15145. * camera.lookAtTransform(transform, offset);
  15146. * }
  15147. * });
  15148. *
  15149. * @see Transforms.preloadIcrfFixed
  15150. */
  15151. Transforms.computeIcrfToFixedMatrix = function (date, result) {
  15152. //>>includeStart('debug', pragmas.debug);
  15153. if (!when.defined(date)) {
  15154. throw new Check.DeveloperError("date is required.");
  15155. }
  15156. //>>includeEnd('debug');
  15157. if (!when.defined(result)) {
  15158. result = new Matrix3();
  15159. }
  15160. var fixedToIcrfMtx = Transforms.computeFixedToIcrfMatrix(date, result);
  15161. if (!when.defined(fixedToIcrfMtx)) {
  15162. return undefined;
  15163. }
  15164. return Matrix3.transpose(fixedToIcrfMtx, result);
  15165. };
  15166. var xysScratch = new Iau2006XysSample(0.0, 0.0, 0.0);
  15167. var eopScratch = new EarthOrientationParametersSample(
  15168. 0.0,
  15169. 0.0,
  15170. 0.0,
  15171. 0.0,
  15172. 0.0,
  15173. 0.0
  15174. );
  15175. var rotation1Scratch = new Matrix3();
  15176. var rotation2Scratch = new Matrix3();
  15177. /**
  15178. * Computes a rotation matrix to transform a point or vector from the Earth-Fixed frame axes (ITRF)
  15179. * to the International Celestial Reference Frame (GCRF/ICRF) inertial frame axes
  15180. * at a given time. This function may return undefined if the data necessary to
  15181. * do the transformation is not yet loaded.
  15182. *
  15183. * @param {JulianDate} date The time at which to compute the rotation matrix.
  15184. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  15185. * not specified, a new instance is created and returned.
  15186. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  15187. * transformation is not yet loaded.
  15188. *
  15189. *
  15190. * @example
  15191. * // Transform a point from the ICRF axes to the Fixed axes.
  15192. * var now = Cesium.JulianDate.now();
  15193. * var pointInFixed = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  15194. * var fixedToIcrf = Cesium.Transforms.computeIcrfToFixedMatrix(now);
  15195. * var pointInInertial = new Cesium.Cartesian3();
  15196. * if (Cesium.defined(fixedToIcrf)) {
  15197. * pointInInertial = Cesium.Matrix3.multiplyByVector(fixedToIcrf, pointInFixed, pointInInertial);
  15198. * }
  15199. *
  15200. * @see Transforms.preloadIcrfFixed
  15201. */
  15202. Transforms.computeFixedToIcrfMatrix = function (date, result) {
  15203. //>>includeStart('debug', pragmas.debug);
  15204. if (!when.defined(date)) {
  15205. throw new Check.DeveloperError("date is required.");
  15206. }
  15207. //>>includeEnd('debug');
  15208. if (!when.defined(result)) {
  15209. result = new Matrix3();
  15210. }
  15211. // Compute pole wander
  15212. var eop = Transforms.earthOrientationParameters.compute(date, eopScratch);
  15213. if (!when.defined(eop)) {
  15214. return undefined;
  15215. }
  15216. // There is no external conversion to Terrestrial Time (TT).
  15217. // So use International Atomic Time (TAI) and convert using offsets.
  15218. // Here we are assuming that dayTT and secondTT are positive
  15219. var dayTT = date.dayNumber;
  15220. // It's possible here that secondTT could roll over 86400
  15221. // This does not seem to affect the precision (unit tests check for this)
  15222. var secondTT = date.secondsOfDay + ttMinusTai;
  15223. var xys = Transforms.iau2006XysData.computeXysRadians(
  15224. dayTT,
  15225. secondTT,
  15226. xysScratch
  15227. );
  15228. if (!when.defined(xys)) {
  15229. return undefined;
  15230. }
  15231. var x = xys.x + eop.xPoleOffset;
  15232. var y = xys.y + eop.yPoleOffset;
  15233. // Compute XYS rotation
  15234. var a = 1.0 / (1.0 + Math.sqrt(1.0 - x * x - y * y));
  15235. var rotation1 = rotation1Scratch;
  15236. rotation1[0] = 1.0 - a * x * x;
  15237. rotation1[3] = -a * x * y;
  15238. rotation1[6] = x;
  15239. rotation1[1] = -a * x * y;
  15240. rotation1[4] = 1 - a * y * y;
  15241. rotation1[7] = y;
  15242. rotation1[2] = -x;
  15243. rotation1[5] = -y;
  15244. rotation1[8] = 1 - a * (x * x + y * y);
  15245. var rotation2 = Matrix3.fromRotationZ(-xys.s, rotation2Scratch);
  15246. var matrixQ = Matrix3.multiply(rotation1, rotation2, rotation1Scratch);
  15247. // Similar to TT conversions above
  15248. // It's possible here that secondTT could roll over 86400
  15249. // This does not seem to affect the precision (unit tests check for this)
  15250. var dateUt1day = date.dayNumber;
  15251. var dateUt1sec =
  15252. date.secondsOfDay - JulianDate.computeTaiMinusUtc(date) + eop.ut1MinusUtc;
  15253. // Compute Earth rotation angle
  15254. // The IERS standard for era is
  15255. // era = 0.7790572732640 + 1.00273781191135448 * Tu
  15256. // where
  15257. // Tu = JulianDateInUt1 - 2451545.0
  15258. // However, you get much more precision if you make the following simplification
  15259. // era = a + (1 + b) * (JulianDayNumber + FractionOfDay - 2451545)
  15260. // era = a + (JulianDayNumber - 2451545) + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  15261. // era = a + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  15262. // since (JulianDayNumber - 2451545) represents an integer number of revolutions which will be discarded anyway.
  15263. var daysSinceJ2000 = dateUt1day - 2451545;
  15264. var fractionOfDay = dateUt1sec / TimeConstants$1.SECONDS_PER_DAY;
  15265. var era =
  15266. 0.779057273264 +
  15267. fractionOfDay +
  15268. 0.00273781191135448 * (daysSinceJ2000 + fractionOfDay);
  15269. era = (era % 1.0) * _Math.CesiumMath.TWO_PI;
  15270. var earthRotation = Matrix3.fromRotationZ(era, rotation2Scratch);
  15271. // pseudoFixed to ICRF
  15272. var pfToIcrf = Matrix3.multiply(matrixQ, earthRotation, rotation1Scratch);
  15273. // Compute pole wander matrix
  15274. var cosxp = Math.cos(eop.xPoleWander);
  15275. var cosyp = Math.cos(eop.yPoleWander);
  15276. var sinxp = Math.sin(eop.xPoleWander);
  15277. var sinyp = Math.sin(eop.yPoleWander);
  15278. var ttt = dayTT - j2000ttDays + secondTT / TimeConstants$1.SECONDS_PER_DAY;
  15279. ttt /= 36525.0;
  15280. // approximate sp value in rad
  15281. var sp = (-47.0e-6 * ttt * _Math.CesiumMath.RADIANS_PER_DEGREE) / 3600.0;
  15282. var cossp = Math.cos(sp);
  15283. var sinsp = Math.sin(sp);
  15284. var fToPfMtx = rotation2Scratch;
  15285. fToPfMtx[0] = cosxp * cossp;
  15286. fToPfMtx[1] = cosxp * sinsp;
  15287. fToPfMtx[2] = sinxp;
  15288. fToPfMtx[3] = -cosyp * sinsp + sinyp * sinxp * cossp;
  15289. fToPfMtx[4] = cosyp * cossp + sinyp * sinxp * sinsp;
  15290. fToPfMtx[5] = -sinyp * cosxp;
  15291. fToPfMtx[6] = -sinyp * sinsp - cosyp * sinxp * cossp;
  15292. fToPfMtx[7] = sinyp * cossp - cosyp * sinxp * sinsp;
  15293. fToPfMtx[8] = cosyp * cosxp;
  15294. return Matrix3.multiply(pfToIcrf, fToPfMtx, result);
  15295. };
  15296. var pointToWindowCoordinatesTemp = new Cartesian4();
  15297. /**
  15298. * Transform a point from model coordinates to window coordinates.
  15299. *
  15300. * @param {Matrix4} modelViewProjectionMatrix The 4x4 model-view-projection matrix.
  15301. * @param {Matrix4} viewportTransformation The 4x4 viewport transformation.
  15302. * @param {Cartesian3} point The point to transform.
  15303. * @param {Cartesian2} [result] The object onto which to store the result.
  15304. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  15305. */
  15306. Transforms.pointToWindowCoordinates = function (
  15307. modelViewProjectionMatrix,
  15308. viewportTransformation,
  15309. point,
  15310. result
  15311. ) {
  15312. result = Transforms.pointToGLWindowCoordinates(
  15313. modelViewProjectionMatrix,
  15314. viewportTransformation,
  15315. point,
  15316. result
  15317. );
  15318. result.y = 2.0 * viewportTransformation[5] - result.y;
  15319. return result;
  15320. };
  15321. /**
  15322. * @private
  15323. */
  15324. Transforms.pointToGLWindowCoordinates = function (
  15325. modelViewProjectionMatrix,
  15326. viewportTransformation,
  15327. point,
  15328. result
  15329. ) {
  15330. //>>includeStart('debug', pragmas.debug);
  15331. if (!when.defined(modelViewProjectionMatrix)) {
  15332. throw new Check.DeveloperError("modelViewProjectionMatrix is required.");
  15333. }
  15334. if (!when.defined(viewportTransformation)) {
  15335. throw new Check.DeveloperError("viewportTransformation is required.");
  15336. }
  15337. if (!when.defined(point)) {
  15338. throw new Check.DeveloperError("point is required.");
  15339. }
  15340. //>>includeEnd('debug');
  15341. if (!when.defined(result)) {
  15342. result = new Cartesian2.Cartesian2();
  15343. }
  15344. var tmp = pointToWindowCoordinatesTemp;
  15345. Matrix4.multiplyByVector(
  15346. modelViewProjectionMatrix,
  15347. Cartesian4.fromElements(point.x, point.y, point.z, 1, tmp),
  15348. tmp
  15349. );
  15350. Cartesian4.multiplyByScalar(tmp, 1.0 / tmp.w, tmp);
  15351. Matrix4.multiplyByVector(viewportTransformation, tmp, tmp);
  15352. return Cartesian2.Cartesian2.fromCartesian4(tmp, result);
  15353. };
  15354. var normalScratch = new Cartesian2.Cartesian3();
  15355. var rightScratch = new Cartesian2.Cartesian3();
  15356. var upScratch = new Cartesian2.Cartesian3();
  15357. /**
  15358. * Transform a position and velocity to a rotation matrix.
  15359. *
  15360. * @param {Cartesian3} position The position to transform.
  15361. * @param {Cartesian3} velocity The velocity vector to transform.
  15362. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  15363. * @param {Matrix3} [result] The object onto which to store the result.
  15364. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if none was provided.
  15365. */
  15366. Transforms.rotationMatrixFromPositionVelocity = function (
  15367. position,
  15368. velocity,
  15369. ellipsoid,
  15370. result
  15371. ) {
  15372. //>>includeStart('debug', pragmas.debug);
  15373. if (!when.defined(position)) {
  15374. throw new Check.DeveloperError("position is required.");
  15375. }
  15376. if (!when.defined(velocity)) {
  15377. throw new Check.DeveloperError("velocity is required.");
  15378. }
  15379. //>>includeEnd('debug');
  15380. var normal = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84).geodeticSurfaceNormal(
  15381. position,
  15382. normalScratch
  15383. );
  15384. var right = Cartesian2.Cartesian3.cross(velocity, normal, rightScratch);
  15385. if (Cartesian2.Cartesian3.equalsEpsilon(right, Cartesian2.Cartesian3.ZERO, _Math.CesiumMath.EPSILON6)) {
  15386. right = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.UNIT_X, right);
  15387. }
  15388. var up = Cartesian2.Cartesian3.cross(right, velocity, upScratch);
  15389. Cartesian2.Cartesian3.normalize(up, up);
  15390. Cartesian2.Cartesian3.cross(velocity, up, right);
  15391. Cartesian2.Cartesian3.negate(right, right);
  15392. Cartesian2.Cartesian3.normalize(right, right);
  15393. if (!when.defined(result)) {
  15394. result = new Matrix3();
  15395. }
  15396. result[0] = velocity.x;
  15397. result[1] = velocity.y;
  15398. result[2] = velocity.z;
  15399. result[3] = right.x;
  15400. result[4] = right.y;
  15401. result[5] = right.z;
  15402. result[6] = up.x;
  15403. result[7] = up.y;
  15404. result[8] = up.z;
  15405. return result;
  15406. };
  15407. var swizzleMatrix = new Matrix4(
  15408. 0.0,
  15409. 0.0,
  15410. 1.0,
  15411. 0.0,
  15412. 1.0,
  15413. 0.0,
  15414. 0.0,
  15415. 0.0,
  15416. 0.0,
  15417. 1.0,
  15418. 0.0,
  15419. 0.0,
  15420. 0.0,
  15421. 0.0,
  15422. 0.0,
  15423. 1.0
  15424. );
  15425. var scratchCartographic = new Cartesian2.Cartographic();
  15426. var scratchCartesian3Projection = new Cartesian2.Cartesian3();
  15427. var scratchCenter = new Cartesian2.Cartesian3();
  15428. var scratchRotation = new Matrix3();
  15429. var scratchFromENU = new Matrix4();
  15430. var scratchToENU = new Matrix4();
  15431. /**
  15432. * @private
  15433. */
  15434. Transforms.basisTo2D = function (projection, matrix, result) {
  15435. //>>includeStart('debug', pragmas.debug);
  15436. if (!when.defined(projection)) {
  15437. throw new Check.DeveloperError("projection is required.");
  15438. }
  15439. if (!when.defined(matrix)) {
  15440. throw new Check.DeveloperError("matrix is required.");
  15441. }
  15442. if (!when.defined(result)) {
  15443. throw new Check.DeveloperError("result is required.");
  15444. }
  15445. //>>includeEnd('debug');
  15446. var rtcCenter = Matrix4.getTranslation(matrix, scratchCenter);
  15447. var ellipsoid = projection.ellipsoid;
  15448. // Get the 2D Center
  15449. var cartographic = ellipsoid.cartesianToCartographic(
  15450. rtcCenter,
  15451. scratchCartographic
  15452. );
  15453. var projectedPosition = projection.project(
  15454. cartographic,
  15455. scratchCartesian3Projection
  15456. );
  15457. Cartesian2.Cartesian3.fromElements(
  15458. projectedPosition.z,
  15459. projectedPosition.x,
  15460. projectedPosition.y,
  15461. projectedPosition
  15462. );
  15463. // Assuming the instance are positioned in WGS84, invert the WGS84 transform to get the local transform and then convert to 2D
  15464. var fromENU = Transforms.eastNorthUpToFixedFrame(
  15465. rtcCenter,
  15466. ellipsoid,
  15467. scratchFromENU
  15468. );
  15469. var toENU = Matrix4.inverseTransformation(fromENU, scratchToENU);
  15470. var rotation = Matrix4.getMatrix3(matrix, scratchRotation);
  15471. var local = Matrix4.multiplyByMatrix3(toENU, rotation, result);
  15472. Matrix4.multiply(swizzleMatrix, local, result); // Swap x, y, z for 2D
  15473. Matrix4.setTranslation(result, projectedPosition, result); // Use the projected center
  15474. return result;
  15475. };
  15476. /**
  15477. * @private
  15478. */
  15479. Transforms.wgs84To2DModelMatrix = function (projection, center, result) {
  15480. //>>includeStart('debug', pragmas.debug);
  15481. if (!when.defined(projection)) {
  15482. throw new Check.DeveloperError("projection is required.");
  15483. }
  15484. if (!when.defined(center)) {
  15485. throw new Check.DeveloperError("center is required.");
  15486. }
  15487. if (!when.defined(result)) {
  15488. throw new Check.DeveloperError("result is required.");
  15489. }
  15490. //>>includeEnd('debug');
  15491. var ellipsoid = projection.ellipsoid;
  15492. var fromENU = Transforms.eastNorthUpToFixedFrame(
  15493. center,
  15494. ellipsoid,
  15495. scratchFromENU
  15496. );
  15497. var toENU = Matrix4.inverseTransformation(fromENU, scratchToENU);
  15498. var cartographic = ellipsoid.cartesianToCartographic(
  15499. center,
  15500. scratchCartographic
  15501. );
  15502. var projectedPosition = projection.project(
  15503. cartographic,
  15504. scratchCartesian3Projection
  15505. );
  15506. Cartesian2.Cartesian3.fromElements(
  15507. projectedPosition.z,
  15508. projectedPosition.x,
  15509. projectedPosition.y,
  15510. projectedPosition
  15511. );
  15512. var translation = Matrix4.fromTranslation(projectedPosition, scratchFromENU);
  15513. Matrix4.multiply(swizzleMatrix, toENU, result);
  15514. Matrix4.multiply(translation, result, result);
  15515. return result;
  15516. };
  15517. exports.BoundingSphere = BoundingSphere;
  15518. exports.Cartesian4 = Cartesian4;
  15519. exports.FeatureDetection = FeatureDetection;
  15520. exports.GeographicProjection = GeographicProjection;
  15521. exports.Intersect = Intersect$1;
  15522. exports.Interval = Interval;
  15523. exports.Matrix3 = Matrix3;
  15524. exports.Matrix4 = Matrix4;
  15525. exports.Quaternion = Quaternion;
  15526. exports.Resource = Resource;
  15527. exports.Transforms = Transforms;
  15528. exports.buildModuleUrl = buildModuleUrl;
  15529. });
  15530. //# sourceMappingURL=Transforms-8be64844.js.map