upsampleQuantizedTerrainMesh.js 939 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2016 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/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. (function () {
  24. /*global define*/
  25. define('Core/defined',[],function() {
  26. 'use strict';
  27. /**
  28. * @exports defined
  29. *
  30. * @param {Object} value The object.
  31. * @returns {Boolean} Returns true if the object is defined, returns false otherwise.
  32. *
  33. * @example
  34. * if (Cesium.defined(positions)) {
  35. * doSomething();
  36. * } else {
  37. * doSomethingElse();
  38. * }
  39. */
  40. function defined(value) {
  41. return value !== undefined && value !== null;
  42. }
  43. return defined;
  44. });
  45. /*global define*/
  46. define('Core/freezeObject',[
  47. './defined'
  48. ], function(
  49. defined) {
  50. 'use strict';
  51. /**
  52. * Freezes an object, using Object.freeze if available, otherwise returns
  53. * the object unchanged. This function should be used in setup code to prevent
  54. * errors from completely halting JavaScript execution in legacy browsers.
  55. *
  56. * @private
  57. *
  58. * @exports freezeObject
  59. */
  60. var freezeObject = Object.freeze;
  61. if (!defined(freezeObject)) {
  62. freezeObject = function(o) {
  63. return o;
  64. };
  65. }
  66. return freezeObject;
  67. });
  68. /*global define*/
  69. define('Core/defaultValue',[
  70. './freezeObject'
  71. ], function(
  72. freezeObject) {
  73. 'use strict';
  74. /**
  75. * Returns the first parameter if not undefined, otherwise the second parameter.
  76. * Useful for setting a default value for a parameter.
  77. *
  78. * @exports defaultValue
  79. *
  80. * @param {*} a
  81. * @param {*} b
  82. * @returns {*} Returns the first parameter if not undefined, otherwise the second parameter.
  83. *
  84. * @example
  85. * param = Cesium.defaultValue(param, 'default');
  86. */
  87. function defaultValue(a, b) {
  88. if (a !== undefined) {
  89. return a;
  90. }
  91. return b;
  92. }
  93. /**
  94. * A frozen empty object that can be used as the default value for options passed as
  95. * an object literal.
  96. */
  97. defaultValue.EMPTY_OBJECT = freezeObject({});
  98. return defaultValue;
  99. });
  100. /*global define*/
  101. define('Core/DeveloperError',[
  102. './defined'
  103. ], function(
  104. defined) {
  105. 'use strict';
  106. /**
  107. * Constructs an exception object that is thrown due to a developer error, e.g., invalid argument,
  108. * argument out of range, etc. This exception should only be thrown during development;
  109. * it usually indicates a bug in the calling code. This exception should never be
  110. * caught; instead the calling code should strive not to generate it.
  111. * <br /><br />
  112. * On the other hand, a {@link RuntimeError} indicates an exception that may
  113. * be thrown at runtime, e.g., out of memory, that the calling code should be prepared
  114. * to catch.
  115. *
  116. * @alias DeveloperError
  117. * @constructor
  118. * @extends Error
  119. *
  120. * @param {String} [message] The error message for this exception.
  121. *
  122. * @see RuntimeError
  123. */
  124. function DeveloperError(message) {
  125. /**
  126. * 'DeveloperError' indicating that this exception was thrown due to a developer error.
  127. * @type {String}
  128. * @readonly
  129. */
  130. this.name = 'DeveloperError';
  131. /**
  132. * The explanation for why this exception was thrown.
  133. * @type {String}
  134. * @readonly
  135. */
  136. this.message = message;
  137. //Browsers such as IE don't have a stack property until you actually throw the error.
  138. var stack;
  139. try {
  140. throw new Error();
  141. } catch (e) {
  142. stack = e.stack;
  143. }
  144. /**
  145. * The stack trace of this exception, if available.
  146. * @type {String}
  147. * @readonly
  148. */
  149. this.stack = stack;
  150. }
  151. if (defined(Object.create)) {
  152. DeveloperError.prototype = Object.create(Error.prototype);
  153. DeveloperError.prototype.constructor = DeveloperError;
  154. }
  155. DeveloperError.prototype.toString = function() {
  156. var str = this.name + ': ' + this.message;
  157. if (defined(this.stack)) {
  158. str += '\n' + this.stack.toString();
  159. }
  160. return str;
  161. };
  162. /**
  163. * @private
  164. */
  165. DeveloperError.throwInstantiationError = function() {
  166. throw new DeveloperError('This function defines an interface and should not be called directly.');
  167. };
  168. return DeveloperError;
  169. });
  170. /*
  171. I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
  172. so it's better encapsulated. Now you can have multiple random number generators
  173. and they won't stomp all over eachother's state.
  174. If you want to use this as a substitute for Math.random(), use the random()
  175. method like so:
  176. var m = new MersenneTwister();
  177. var randomNumber = m.random();
  178. You can also call the other genrand_{foo}() methods on the instance.
  179. If you want to use a specific seed in order to get a repeatable random
  180. sequence, pass an integer into the constructor:
  181. var m = new MersenneTwister(123);
  182. and that will always produce the same random sequence.
  183. Sean McCullough (banksean@gmail.com)
  184. */
  185. /*
  186. A C-program for MT19937, with initialization improved 2002/1/26.
  187. Coded by Takuji Nishimura and Makoto Matsumoto.
  188. Before using, initialize the state by using init_genrand(seed)
  189. or init_by_array(init_key, key_length).
  190. */
  191. /**
  192. @license
  193. mersenne-twister.js - https://gist.github.com/banksean/300494
  194. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  195. All rights reserved.
  196. Redistribution and use in source and binary forms, with or without
  197. modification, are permitted provided that the following conditions
  198. are met:
  199. 1. Redistributions of source code must retain the above copyright
  200. notice, this list of conditions and the following disclaimer.
  201. 2. Redistributions in binary form must reproduce the above copyright
  202. notice, this list of conditions and the following disclaimer in the
  203. documentation and/or other materials provided with the distribution.
  204. 3. The names of its contributors may not be used to endorse or promote
  205. products derived from this software without specific prior written
  206. permission.
  207. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  208. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  209. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  210. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  211. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  212. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  213. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  214. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  215. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  216. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  217. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  218. */
  219. /*
  220. Any feedback is very welcome.
  221. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  222. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  223. */
  224. define('ThirdParty/mersenne-twister',[],function() {
  225. var MersenneTwister = function(seed) {
  226. if (seed == undefined) {
  227. seed = new Date().getTime();
  228. }
  229. /* Period parameters */
  230. this.N = 624;
  231. this.M = 397;
  232. this.MATRIX_A = 0x9908b0df; /* constant vector a */
  233. this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
  234. this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
  235. this.mt = new Array(this.N); /* the array for the state vector */
  236. this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
  237. this.init_genrand(seed);
  238. }
  239. /* initializes mt[N] with a seed */
  240. MersenneTwister.prototype.init_genrand = function(s) {
  241. this.mt[0] = s >>> 0;
  242. for (this.mti=1; this.mti<this.N; this.mti++) {
  243. var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30);
  244. this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
  245. + this.mti;
  246. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  247. /* In the previous versions, MSBs of the seed affect */
  248. /* only MSBs of the array mt[]. */
  249. /* 2002/01/09 modified by Makoto Matsumoto */
  250. this.mt[this.mti] >>>= 0;
  251. /* for >32 bit machines */
  252. }
  253. }
  254. /* initialize by an array with array-length */
  255. /* init_key is the array for initializing keys */
  256. /* key_length is its length */
  257. /* slight change for C++, 2004/2/26 */
  258. //MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
  259. // var i, j, k;
  260. // this.init_genrand(19650218);
  261. // i=1; j=0;
  262. // k = (this.N>key_length ? this.N : key_length);
  263. // for (; k; k--) {
  264. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30)
  265. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))
  266. // + init_key[j] + j; /* non linear */
  267. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  268. // i++; j++;
  269. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  270. // if (j>=key_length) j=0;
  271. // }
  272. // for (k=this.N-1; k; k--) {
  273. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
  274. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))
  275. // - i; /* non linear */
  276. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  277. // i++;
  278. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  279. // }
  280. //
  281. // this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  282. //}
  283. /* generates a random number on [0,0xffffffff]-interval */
  284. MersenneTwister.prototype.genrand_int32 = function() {
  285. var y;
  286. var mag01 = new Array(0x0, this.MATRIX_A);
  287. /* mag01[x] = x * MATRIX_A for x=0,1 */
  288. if (this.mti >= this.N) { /* generate N words at one time */
  289. var kk;
  290. if (this.mti == this.N+1) /* if init_genrand() has not been called, */
  291. this.init_genrand(5489); /* a default initial seed is used */
  292. for (kk=0;kk<this.N-this.M;kk++) {
  293. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  294. this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
  295. }
  296. for (;kk<this.N-1;kk++) {
  297. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  298. this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  299. }
  300. y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK);
  301. this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
  302. this.mti = 0;
  303. }
  304. y = this.mt[this.mti++];
  305. /* Tempering */
  306. y ^= (y >>> 11);
  307. y ^= (y << 7) & 0x9d2c5680;
  308. y ^= (y << 15) & 0xefc60000;
  309. y ^= (y >>> 18);
  310. return y >>> 0;
  311. }
  312. /* generates a random number on [0,0x7fffffff]-interval */
  313. //MersenneTwister.prototype.genrand_int31 = function() {
  314. // return (this.genrand_int32()>>>1);
  315. //}
  316. /* generates a random number on [0,1]-real-interval */
  317. //MersenneTwister.prototype.genrand_real1 = function() {
  318. // return this.genrand_int32()*(1.0/4294967295.0);
  319. // /* divided by 2^32-1 */
  320. //}
  321. /* generates a random number on [0,1)-real-interval */
  322. MersenneTwister.prototype.random = function() {
  323. return this.genrand_int32()*(1.0/4294967296.0);
  324. /* divided by 2^32 */
  325. }
  326. /* generates a random number on (0,1)-real-interval */
  327. //MersenneTwister.prototype.genrand_real3 = function() {
  328. // return (this.genrand_int32() + 0.5)*(1.0/4294967296.0);
  329. // /* divided by 2^32 */
  330. //}
  331. /* generates a random number on [0,1) with 53-bit resolution*/
  332. //MersenneTwister.prototype.genrand_res53 = function() {
  333. // var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6;
  334. // return(a*67108864.0+b)*(1.0/9007199254740992.0);
  335. //}
  336. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  337. return MersenneTwister;
  338. });
  339. /*global define*/
  340. define('Core/Math',[
  341. '../ThirdParty/mersenne-twister',
  342. './defaultValue',
  343. './defined',
  344. './DeveloperError'
  345. ], function(
  346. MersenneTwister,
  347. defaultValue,
  348. defined,
  349. DeveloperError) {
  350. 'use strict';
  351. /**
  352. * Math functions.
  353. *
  354. * @exports CesiumMath
  355. */
  356. var CesiumMath = {};
  357. /**
  358. * 0.1
  359. * @type {Number}
  360. * @constant
  361. */
  362. CesiumMath.EPSILON1 = 0.1;
  363. /**
  364. * 0.01
  365. * @type {Number}
  366. * @constant
  367. */
  368. CesiumMath.EPSILON2 = 0.01;
  369. /**
  370. * 0.001
  371. * @type {Number}
  372. * @constant
  373. */
  374. CesiumMath.EPSILON3 = 0.001;
  375. /**
  376. * 0.0001
  377. * @type {Number}
  378. * @constant
  379. */
  380. CesiumMath.EPSILON4 = 0.0001;
  381. /**
  382. * 0.00001
  383. * @type {Number}
  384. * @constant
  385. */
  386. CesiumMath.EPSILON5 = 0.00001;
  387. /**
  388. * 0.000001
  389. * @type {Number}
  390. * @constant
  391. */
  392. CesiumMath.EPSILON6 = 0.000001;
  393. /**
  394. * 0.0000001
  395. * @type {Number}
  396. * @constant
  397. */
  398. CesiumMath.EPSILON7 = 0.0000001;
  399. /**
  400. * 0.00000001
  401. * @type {Number}
  402. * @constant
  403. */
  404. CesiumMath.EPSILON8 = 0.00000001;
  405. /**
  406. * 0.000000001
  407. * @type {Number}
  408. * @constant
  409. */
  410. CesiumMath.EPSILON9 = 0.000000001;
  411. /**
  412. * 0.0000000001
  413. * @type {Number}
  414. * @constant
  415. */
  416. CesiumMath.EPSILON10 = 0.0000000001;
  417. /**
  418. * 0.00000000001
  419. * @type {Number}
  420. * @constant
  421. */
  422. CesiumMath.EPSILON11 = 0.00000000001;
  423. /**
  424. * 0.000000000001
  425. * @type {Number}
  426. * @constant
  427. */
  428. CesiumMath.EPSILON12 = 0.000000000001;
  429. /**
  430. * 0.0000000000001
  431. * @type {Number}
  432. * @constant
  433. */
  434. CesiumMath.EPSILON13 = 0.0000000000001;
  435. /**
  436. * 0.00000000000001
  437. * @type {Number}
  438. * @constant
  439. */
  440. CesiumMath.EPSILON14 = 0.00000000000001;
  441. /**
  442. * 0.000000000000001
  443. * @type {Number}
  444. * @constant
  445. */
  446. CesiumMath.EPSILON15 = 0.000000000000001;
  447. /**
  448. * 0.0000000000000001
  449. * @type {Number}
  450. * @constant
  451. */
  452. CesiumMath.EPSILON16 = 0.0000000000000001;
  453. /**
  454. * 0.00000000000000001
  455. * @type {Number}
  456. * @constant
  457. */
  458. CesiumMath.EPSILON17 = 0.00000000000000001;
  459. /**
  460. * 0.000000000000000001
  461. * @type {Number}
  462. * @constant
  463. */
  464. CesiumMath.EPSILON18 = 0.000000000000000001;
  465. /**
  466. * 0.0000000000000000001
  467. * @type {Number}
  468. * @constant
  469. */
  470. CesiumMath.EPSILON19 = 0.0000000000000000001;
  471. /**
  472. * 0.00000000000000000001
  473. * @type {Number}
  474. * @constant
  475. */
  476. CesiumMath.EPSILON20 = 0.00000000000000000001;
  477. /**
  478. * 3.986004418e14
  479. * @type {Number}
  480. * @constant
  481. */
  482. CesiumMath.GRAVITATIONALPARAMETER = 3.986004418e14;
  483. /**
  484. * Radius of the sun in meters: 6.955e8
  485. * @type {Number}
  486. * @constant
  487. */
  488. CesiumMath.SOLAR_RADIUS = 6.955e8;
  489. /**
  490. * The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on
  491. * Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000",
  492. * Celestial Mechanics 82: 83-110, 2002.
  493. * @type {Number}
  494. * @constant
  495. */
  496. CesiumMath.LUNAR_RADIUS = 1737400.0;
  497. /**
  498. * 64 * 1024
  499. * @type {Number}
  500. * @constant
  501. */
  502. CesiumMath.SIXTY_FOUR_KILOBYTES = 64 * 1024;
  503. /**
  504. * Returns the sign of the value; 1 if the value is positive, -1 if the value is
  505. * negative, or 0 if the value is 0.
  506. *
  507. * @param {Number} value The value to return the sign of.
  508. * @returns {Number} The sign of value.
  509. */
  510. CesiumMath.sign = function(value) {
  511. if (value > 0) {
  512. return 1;
  513. }
  514. if (value < 0) {
  515. return -1;
  516. }
  517. return 0;
  518. };
  519. /**
  520. * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
  521. * This is similar to {@link CesiumMath#sign} except that returns 1.0 instead of
  522. * 0.0 when the input value is 0.0.
  523. * @param {Number} value The value to return the sign of.
  524. * @returns {Number} The sign of value.
  525. */
  526. CesiumMath.signNotZero = function(value) {
  527. return value < 0.0 ? -1.0 : 1.0;
  528. };
  529. /**
  530. * Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMax]
  531. * @param {Number} value The scalar value in the range [-1.0, 1.0]
  532. * @param {Number} [rangeMax=255] The maximum value in the mapped range, 255 by default.
  533. * @returns {Number} A SNORM value, where 0 maps to -1.0 and rangeMax maps to 1.0.
  534. *
  535. * @see CesiumMath.fromSNorm
  536. */
  537. CesiumMath.toSNorm = function(value, rangeMax) {
  538. rangeMax = defaultValue(rangeMax, 255);
  539. return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMax);
  540. };
  541. /**
  542. * Converts a SNORM value in the range [0, rangeMax] to a scalar in the range [-1.0, 1.0].
  543. * @param {Number} value SNORM value in the range [0, 255]
  544. * @param {Number} [rangeMax=255] The maximum value in the SNORM range, 255 by default.
  545. * @returns {Number} Scalar in the range [-1.0, 1.0].
  546. *
  547. * @see CesiumMath.toSNorm
  548. */
  549. CesiumMath.fromSNorm = function(value, rangeMax) {
  550. rangeMax = defaultValue(rangeMax, 255);
  551. return CesiumMath.clamp(value, 0.0, rangeMax) / rangeMax * 2.0 - 1.0;
  552. };
  553. /**
  554. * Returns the hyperbolic sine of a number.
  555. * The hyperbolic sine of <em>value</em> is defined to be
  556. * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
  557. * where <i>e</i> is Euler's number, approximately 2.71828183.
  558. *
  559. * <p>Special cases:
  560. * <ul>
  561. * <li>If the argument is NaN, then the result is NaN.</li>
  562. *
  563. * <li>If the argument is infinite, then the result is an infinity
  564. * with the same sign as the argument.</li>
  565. *
  566. * <li>If the argument is zero, then the result is a zero with the
  567. * same sign as the argument.</li>
  568. * </ul>
  569. *</p>
  570. *
  571. * @param {Number} value The number whose hyperbolic sine is to be returned.
  572. * @returns {Number} The hyperbolic sine of <code>value</code>.
  573. */
  574. CesiumMath.sinh = function(value) {
  575. var part1 = Math.pow(Math.E, value);
  576. var part2 = Math.pow(Math.E, -1.0 * value);
  577. return (part1 - part2) * 0.5;
  578. };
  579. /**
  580. * Returns the hyperbolic cosine of a number.
  581. * The hyperbolic cosine of <strong>value</strong> is defined to be
  582. * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
  583. * where <i>e</i> is Euler's number, approximately 2.71828183.
  584. *
  585. * <p>Special cases:
  586. * <ul>
  587. * <li>If the argument is NaN, then the result is NaN.</li>
  588. *
  589. * <li>If the argument is infinite, then the result is positive infinity.</li>
  590. *
  591. * <li>If the argument is zero, then the result is 1.0.</li>
  592. * </ul>
  593. *</p>
  594. *
  595. * @param {Number} value The number whose hyperbolic cosine is to be returned.
  596. * @returns {Number} The hyperbolic cosine of <code>value</code>.
  597. */
  598. CesiumMath.cosh = function(value) {
  599. var part1 = Math.pow(Math.E, value);
  600. var part2 = Math.pow(Math.E, -1.0 * value);
  601. return (part1 + part2) * 0.5;
  602. };
  603. /**
  604. * Computes the linear interpolation of two values.
  605. *
  606. * @param {Number} p The start value to interpolate.
  607. * @param {Number} q The end value to interpolate.
  608. * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
  609. * @returns {Number} The linearly interpolated value.
  610. *
  611. * @example
  612. * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
  613. */
  614. CesiumMath.lerp = function(p, q, time) {
  615. return ((1.0 - time) * p) + (time * q);
  616. };
  617. /**
  618. * pi
  619. *
  620. * @type {Number}
  621. * @constant
  622. */
  623. CesiumMath.PI = Math.PI;
  624. /**
  625. * 1/pi
  626. *
  627. * @type {Number}
  628. * @constant
  629. */
  630. CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
  631. /**
  632. * pi/2
  633. *
  634. * @type {Number}
  635. * @constant
  636. */
  637. CesiumMath.PI_OVER_TWO = Math.PI * 0.5;
  638. /**
  639. * pi/3
  640. *
  641. * @type {Number}
  642. * @constant
  643. */
  644. CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
  645. /**
  646. * pi/4
  647. *
  648. * @type {Number}
  649. * @constant
  650. */
  651. CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
  652. /**
  653. * pi/6
  654. *
  655. * @type {Number}
  656. * @constant
  657. */
  658. CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
  659. /**
  660. * 3pi/2
  661. *
  662. * @type {Number}
  663. * @constant
  664. */
  665. CesiumMath.THREE_PI_OVER_TWO = (3.0 * Math.PI) * 0.5;
  666. /**
  667. * 2pi
  668. *
  669. * @type {Number}
  670. * @constant
  671. */
  672. CesiumMath.TWO_PI = 2.0 * Math.PI;
  673. /**
  674. * 1/2pi
  675. *
  676. * @type {Number}
  677. * @constant
  678. */
  679. CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
  680. /**
  681. * The number of radians in a degree.
  682. *
  683. * @type {Number}
  684. * @constant
  685. * @default Math.PI / 180.0
  686. */
  687. CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
  688. /**
  689. * The number of degrees in a radian.
  690. *
  691. * @type {Number}
  692. * @constant
  693. * @default 180.0 / Math.PI
  694. */
  695. CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
  696. /**
  697. * The number of radians in an arc second.
  698. *
  699. * @type {Number}
  700. * @constant
  701. * @default {@link CesiumMath.RADIANS_PER_DEGREE} / 3600.0
  702. */
  703. CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  704. /**
  705. * Converts degrees to radians.
  706. * @param {Number} degrees The angle to convert in degrees.
  707. * @returns {Number} The corresponding angle in radians.
  708. */
  709. CesiumMath.toRadians = function(degrees) {
  710. if (!defined(degrees)) {
  711. throw new DeveloperError('degrees is required.');
  712. }
  713. return degrees * CesiumMath.RADIANS_PER_DEGREE;
  714. };
  715. /**
  716. * Converts radians to degrees.
  717. * @param {Number} radians The angle to convert in radians.
  718. * @returns {Number} The corresponding angle in degrees.
  719. */
  720. CesiumMath.toDegrees = function(radians) {
  721. if (!defined(radians)) {
  722. throw new DeveloperError('radians is required.');
  723. }
  724. return radians * CesiumMath.DEGREES_PER_RADIAN;
  725. };
  726. /**
  727. * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  728. *
  729. * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  730. * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  731. *
  732. * @example
  733. * // Convert 270 degrees to -90 degrees longitude
  734. * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
  735. */
  736. CesiumMath.convertLongitudeRange = function(angle) {
  737. if (!defined(angle)) {
  738. throw new DeveloperError('angle is required.');
  739. }
  740. var twoPi = CesiumMath.TWO_PI;
  741. var simplified = angle - Math.floor(angle / twoPi) * twoPi;
  742. if (simplified < -Math.PI) {
  743. return simplified + twoPi;
  744. }
  745. if (simplified >= Math.PI) {
  746. return simplified - twoPi;
  747. }
  748. return simplified;
  749. };
  750. /**
  751. * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  752. * Useful for sanitizing data before use in objects requiring correct range.
  753. *
  754. * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  755. * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  756. *
  757. * @example
  758. * // Clamp 108 degrees latitude to 90 degrees latitude
  759. * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
  760. */
  761. CesiumMath.clampToLatitudeRange = function(angle) {
  762. if (!defined(angle)) {
  763. throw new DeveloperError('angle is required.');
  764. }
  765. return CesiumMath.clamp(angle, -1*CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  766. };
  767. /**
  768. * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
  769. *
  770. * @param {Number} angle in radians
  771. * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
  772. */
  773. CesiumMath.negativePiToPi = function(x) {
  774. if (!defined(x)) {
  775. throw new DeveloperError('x is required.');
  776. }
  777. return CesiumMath.zeroToTwoPi(x + CesiumMath.PI) - CesiumMath.PI;
  778. };
  779. /**
  780. * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
  781. *
  782. * @param {Number} angle in radians
  783. * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
  784. */
  785. CesiumMath.zeroToTwoPi = function(x) {
  786. if (!defined(x)) {
  787. throw new DeveloperError('x is required.');
  788. }
  789. var mod = CesiumMath.mod(x, CesiumMath.TWO_PI);
  790. if (Math.abs(mod) < CesiumMath.EPSILON14 && Math.abs(x) > CesiumMath.EPSILON14) {
  791. return CesiumMath.TWO_PI;
  792. }
  793. return mod;
  794. };
  795. /**
  796. * The modulo operation that also works for negative dividends.
  797. *
  798. * @param {Number} m The dividend.
  799. * @param {Number} n The divisor.
  800. * @returns {Number} The remainder.
  801. */
  802. CesiumMath.mod = function(m, n) {
  803. if (!defined(m)) {
  804. throw new DeveloperError('m is required.');
  805. }
  806. if (!defined(n)) {
  807. throw new DeveloperError('n is required.');
  808. }
  809. return ((m % n) + n) % n;
  810. };
  811. /**
  812. * Determines if two values are equal using an absolute or relative tolerance test. This is useful
  813. * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
  814. * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
  815. * Use this test if you are unsure of the magnitudes of left and right.
  816. *
  817. * @param {Number} left The first value to compare.
  818. * @param {Number} right The other value to compare.
  819. * @param {Number} relativeEpsilon The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
  820. * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
  821. * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
  822. *
  823. * @example
  824. * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
  825. * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2); // false
  826. * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
  827. * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
  828. */
  829. CesiumMath.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  830. if (!defined(left)) {
  831. throw new DeveloperError('left is required.');
  832. }
  833. if (!defined(right)) {
  834. throw new DeveloperError('right is required.');
  835. }
  836. if (!defined(relativeEpsilon)) {
  837. throw new DeveloperError('relativeEpsilon is required.');
  838. }
  839. absoluteEpsilon = defaultValue(absoluteEpsilon, relativeEpsilon);
  840. var absDiff = Math.abs(left - right);
  841. return absDiff <= absoluteEpsilon || absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right));
  842. };
  843. var factorials = [1];
  844. /**
  845. * Computes the factorial of the provided number.
  846. *
  847. * @param {Number} n The number whose factorial is to be computed.
  848. * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
  849. *
  850. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  851. *
  852. *
  853. * @example
  854. * //Compute 7!, which is equal to 5040
  855. * var computedFactorial = Cesium.Math.factorial(7);
  856. *
  857. * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
  858. */
  859. CesiumMath.factorial = function(n) {
  860. if (typeof n !== 'number' || n < 0) {
  861. throw new DeveloperError('A number greater than or equal to 0 is required.');
  862. }
  863. var length = factorials.length;
  864. if (n >= length) {
  865. var sum = factorials[length - 1];
  866. for (var i = length; i <= n; i++) {
  867. factorials.push(sum * i);
  868. }
  869. }
  870. return factorials[n];
  871. };
  872. /**
  873. * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
  874. *
  875. * @param {Number} [n] The number to be incremented.
  876. * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
  877. * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
  878. * @returns {Number} The incremented number.
  879. *
  880. * @exception {DeveloperError} Maximum value must be greater than minimum value.
  881. *
  882. * @example
  883. * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
  884. * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
  885. */
  886. CesiumMath.incrementWrap = function(n, maximumValue, minimumValue) {
  887. minimumValue = defaultValue(minimumValue, 0.0);
  888. if (!defined(n)) {
  889. throw new DeveloperError('n is required.');
  890. }
  891. if (maximumValue <= minimumValue) {
  892. throw new DeveloperError('maximumValue must be greater than minimumValue.');
  893. }
  894. ++n;
  895. if (n > maximumValue) {
  896. n = minimumValue;
  897. }
  898. return n;
  899. };
  900. /**
  901. * Determines if a positive integer is a power of two.
  902. *
  903. * @param {Number} n The positive integer to test.
  904. * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
  905. *
  906. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  907. *
  908. * @example
  909. * var t = Cesium.Math.isPowerOfTwo(16); // true
  910. * var f = Cesium.Math.isPowerOfTwo(20); // false
  911. */
  912. CesiumMath.isPowerOfTwo = function(n) {
  913. if (typeof n !== 'number' || n < 0) {
  914. throw new DeveloperError('A number greater than or equal to 0 is required.');
  915. }
  916. return (n !== 0) && ((n & (n - 1)) === 0);
  917. };
  918. /**
  919. * Computes the next power-of-two integer greater than or equal to the provided positive integer.
  920. *
  921. * @param {Number} n The positive integer to test.
  922. * @returns {Number} The next power-of-two integer.
  923. *
  924. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  925. *
  926. * @example
  927. * var n = Cesium.Math.nextPowerOfTwo(29); // 32
  928. * var m = Cesium.Math.nextPowerOfTwo(32); // 32
  929. */
  930. CesiumMath.nextPowerOfTwo = function(n) {
  931. if (typeof n !== 'number' || n < 0) {
  932. throw new DeveloperError('A number greater than or equal to 0 is required.');
  933. }
  934. // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  935. --n;
  936. n |= n >> 1;
  937. n |= n >> 2;
  938. n |= n >> 4;
  939. n |= n >> 8;
  940. n |= n >> 16;
  941. ++n;
  942. return n;
  943. };
  944. /**
  945. * Constraint a value to lie between two values.
  946. *
  947. * @param {Number} value The value to constrain.
  948. * @param {Number} min The minimum value.
  949. * @param {Number} max The maximum value.
  950. * @returns {Number} The value clamped so that min <= value <= max.
  951. */
  952. CesiumMath.clamp = function(value, min, max) {
  953. if (!defined(value)) {
  954. throw new DeveloperError('value is required');
  955. }
  956. if (!defined(min)) {
  957. throw new DeveloperError('min is required.');
  958. }
  959. if (!defined(max)) {
  960. throw new DeveloperError('max is required.');
  961. }
  962. return value < min ? min : value > max ? max : value;
  963. };
  964. var randomNumberGenerator = new MersenneTwister();
  965. /**
  966. * Sets the seed used by the random number generator
  967. * in {@link CesiumMath#nextRandomNumber}.
  968. *
  969. * @param {Number} seed An integer used as the seed.
  970. */
  971. CesiumMath.setRandomNumberSeed = function(seed) {
  972. if (!defined(seed)) {
  973. throw new DeveloperError('seed is required.');
  974. }
  975. randomNumberGenerator = new MersenneTwister(seed);
  976. };
  977. /**
  978. * Generates a random number in the range of [0.0, 1.0)
  979. * using a Mersenne twister.
  980. *
  981. * @returns {Number} A random number in the range of [0.0, 1.0).
  982. *
  983. * @see CesiumMath.setRandomNumberSeed
  984. * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
  985. */
  986. CesiumMath.nextRandomNumber = function() {
  987. return randomNumberGenerator.random();
  988. };
  989. /**
  990. * Computes <code>Math.acos(value)</acode>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  991. * so that the function will never return NaN.
  992. *
  993. * @param {Number} value The value for which to compute acos.
  994. * @returns {Number} The acos of the value if the value is in the range [-1.0, 1.0], or the acos of -1.0 or 1.0,
  995. * whichever is closer, if the value is outside the range.
  996. */
  997. CesiumMath.acosClamped = function(value) {
  998. if (!defined(value)) {
  999. throw new DeveloperError('value is required.');
  1000. }
  1001. return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
  1002. };
  1003. /**
  1004. * Computes <code>Math.asin(value)</acode>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  1005. * so that the function will never return NaN.
  1006. *
  1007. * @param {Number} value The value for which to compute asin.
  1008. * @returns {Number} The asin of the value if the value is in the range [-1.0, 1.0], or the asin of -1.0 or 1.0,
  1009. * whichever is closer, if the value is outside the range.
  1010. */
  1011. CesiumMath.asinClamped = function(value) {
  1012. if (!defined(value)) {
  1013. throw new DeveloperError('value is required.');
  1014. }
  1015. return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
  1016. };
  1017. /**
  1018. * Finds the chord length between two points given the circle's radius and the angle between the points.
  1019. *
  1020. * @param {Number} angle The angle between the two points.
  1021. * @param {Number} radius The radius of the circle.
  1022. * @returns {Number} The chord length.
  1023. */
  1024. CesiumMath.chordLength = function(angle, radius) {
  1025. if (!defined(angle)) {
  1026. throw new DeveloperError('angle is required.');
  1027. }
  1028. if (!defined(radius)) {
  1029. throw new DeveloperError('radius is required.');
  1030. }
  1031. return 2.0 * radius * Math.sin(angle * 0.5);
  1032. };
  1033. /**
  1034. * Finds the logarithm of a number to a base.
  1035. *
  1036. * @param {Number} number The number.
  1037. * @param {Number} base The base.
  1038. * @returns {Number} The result.
  1039. */
  1040. CesiumMath.logBase = function(number, base) {
  1041. if (!defined(number)) {
  1042. throw new DeveloperError('number is required.');
  1043. }
  1044. if (!defined(base)) {
  1045. throw new DeveloperError('base is required.');
  1046. }
  1047. return Math.log(number) / Math.log(base);
  1048. };
  1049. /**
  1050. * @private
  1051. */
  1052. CesiumMath.fog = function(distanceToCamera, density) {
  1053. var scalar = distanceToCamera * density;
  1054. return 1.0 - Math.exp(-(scalar * scalar));
  1055. };
  1056. return CesiumMath;
  1057. });
  1058. /*global define*/
  1059. define('Core/Cartesian2',[
  1060. './defaultValue',
  1061. './defined',
  1062. './DeveloperError',
  1063. './freezeObject',
  1064. './Math'
  1065. ], function(
  1066. defaultValue,
  1067. defined,
  1068. DeveloperError,
  1069. freezeObject,
  1070. CesiumMath) {
  1071. 'use strict';
  1072. /**
  1073. * A 2D Cartesian point.
  1074. * @alias Cartesian2
  1075. * @constructor
  1076. *
  1077. * @param {Number} [x=0.0] The X component.
  1078. * @param {Number} [y=0.0] The Y component.
  1079. *
  1080. * @see Cartesian3
  1081. * @see Cartesian4
  1082. * @see Packable
  1083. */
  1084. function Cartesian2(x, y) {
  1085. /**
  1086. * The X component.
  1087. * @type {Number}
  1088. * @default 0.0
  1089. */
  1090. this.x = defaultValue(x, 0.0);
  1091. /**
  1092. * The Y component.
  1093. * @type {Number}
  1094. * @default 0.0
  1095. */
  1096. this.y = defaultValue(y, 0.0);
  1097. }
  1098. /**
  1099. * Creates a Cartesian2 instance from x and y coordinates.
  1100. *
  1101. * @param {Number} x The x coordinate.
  1102. * @param {Number} y The y coordinate.
  1103. * @param {Cartesian2} [result] The object onto which to store the result.
  1104. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1105. */
  1106. Cartesian2.fromElements = function(x, y, result) {
  1107. if (!defined(result)) {
  1108. return new Cartesian2(x, y);
  1109. }
  1110. result.x = x;
  1111. result.y = y;
  1112. return result;
  1113. };
  1114. /**
  1115. * Duplicates a Cartesian2 instance.
  1116. *
  1117. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  1118. * @param {Cartesian2} [result] The object onto which to store the result.
  1119. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  1120. */
  1121. Cartesian2.clone = function(cartesian, result) {
  1122. if (!defined(cartesian)) {
  1123. return undefined;
  1124. }
  1125. if (!defined(result)) {
  1126. return new Cartesian2(cartesian.x, cartesian.y);
  1127. }
  1128. result.x = cartesian.x;
  1129. result.y = cartesian.y;
  1130. return result;
  1131. };
  1132. /**
  1133. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  1134. * x and y properties of the Cartesian3 and drops z.
  1135. * @function
  1136. *
  1137. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  1138. * @param {Cartesian2} [result] The object onto which to store the result.
  1139. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1140. */
  1141. Cartesian2.fromCartesian3 = Cartesian2.clone;
  1142. /**
  1143. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  1144. * x and y properties of the Cartesian4 and drops z and w.
  1145. * @function
  1146. *
  1147. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  1148. * @param {Cartesian2} [result] The object onto which to store the result.
  1149. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1150. */
  1151. Cartesian2.fromCartesian4 = Cartesian2.clone;
  1152. /**
  1153. * The number of elements used to pack the object into an array.
  1154. * @type {Number}
  1155. */
  1156. Cartesian2.packedLength = 2;
  1157. /**
  1158. * Stores the provided instance into the provided array.
  1159. *
  1160. * @param {Cartesian2} value The value to pack.
  1161. * @param {Number[]} array The array to pack into.
  1162. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1163. *
  1164. * @returns {Number[]} The array that was packed into
  1165. */
  1166. Cartesian2.pack = function(value, array, startingIndex) {
  1167. if (!defined(value)) {
  1168. throw new DeveloperError('value is required');
  1169. }
  1170. if (!defined(array)) {
  1171. throw new DeveloperError('array is required');
  1172. }
  1173. startingIndex = defaultValue(startingIndex, 0);
  1174. array[startingIndex++] = value.x;
  1175. array[startingIndex] = value.y;
  1176. return array;
  1177. };
  1178. /**
  1179. * Retrieves an instance from a packed array.
  1180. *
  1181. * @param {Number[]} array The packed array.
  1182. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1183. * @param {Cartesian2} [result] The object into which to store the result.
  1184. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1185. */
  1186. Cartesian2.unpack = function(array, startingIndex, result) {
  1187. if (!defined(array)) {
  1188. throw new DeveloperError('array is required');
  1189. }
  1190. startingIndex = defaultValue(startingIndex, 0);
  1191. if (!defined(result)) {
  1192. result = new Cartesian2();
  1193. }
  1194. result.x = array[startingIndex++];
  1195. result.y = array[startingIndex];
  1196. return result;
  1197. };
  1198. /**
  1199. * Flattens an array of Cartesian2s into and array of components.
  1200. *
  1201. * @param {Cartesian2[]} array The array of cartesians to pack.
  1202. * @param {Number[]} result The array onto which to store the result.
  1203. * @returns {Number[]} The packed array.
  1204. */
  1205. Cartesian2.packArray = function(array, result) {
  1206. if (!defined(array)) {
  1207. throw new DeveloperError('array is required');
  1208. }
  1209. var length = array.length;
  1210. if (!defined(result)) {
  1211. result = new Array(length * 2);
  1212. } else {
  1213. result.length = length * 2;
  1214. }
  1215. for (var i = 0; i < length; ++i) {
  1216. Cartesian2.pack(array[i], result, i * 2);
  1217. }
  1218. return result;
  1219. };
  1220. /**
  1221. * Unpacks an array of cartesian components into and array of Cartesian2s.
  1222. *
  1223. * @param {Number[]} array The array of components to unpack.
  1224. * @param {Cartesian2[]} result The array onto which to store the result.
  1225. * @returns {Cartesian2[]} The unpacked array.
  1226. */
  1227. Cartesian2.unpackArray = function(array, result) {
  1228. if (!defined(array)) {
  1229. throw new DeveloperError('array is required');
  1230. }
  1231. var length = array.length;
  1232. if (!defined(result)) {
  1233. result = new Array(length / 2);
  1234. } else {
  1235. result.length = length / 2;
  1236. }
  1237. for (var i = 0; i < length; i += 2) {
  1238. var index = i / 2;
  1239. result[index] = Cartesian2.unpack(array, i, result[index]);
  1240. }
  1241. return result;
  1242. };
  1243. /**
  1244. * Creates a Cartesian2 from two consecutive elements in an array.
  1245. * @function
  1246. *
  1247. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  1248. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  1249. * @param {Cartesian2} [result] The object onto which to store the result.
  1250. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1251. *
  1252. * @example
  1253. * // Create a Cartesian2 with (1.0, 2.0)
  1254. * var v = [1.0, 2.0];
  1255. * var p = Cesium.Cartesian2.fromArray(v);
  1256. *
  1257. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  1258. * var v2 = [0.0, 0.0, 1.0, 2.0];
  1259. * var p2 = Cesium.Cartesian2.fromArray(v2, 2);
  1260. */
  1261. Cartesian2.fromArray = Cartesian2.unpack;
  1262. /**
  1263. * Computes the value of the maximum component for the supplied Cartesian.
  1264. *
  1265. * @param {Cartesian2} cartesian The cartesian to use.
  1266. * @returns {Number} The value of the maximum component.
  1267. */
  1268. Cartesian2.maximumComponent = function(cartesian) {
  1269. if (!defined(cartesian)) {
  1270. throw new DeveloperError('cartesian is required');
  1271. }
  1272. return Math.max(cartesian.x, cartesian.y);
  1273. };
  1274. /**
  1275. * Computes the value of the minimum component for the supplied Cartesian.
  1276. *
  1277. * @param {Cartesian2} cartesian The cartesian to use.
  1278. * @returns {Number} The value of the minimum component.
  1279. */
  1280. Cartesian2.minimumComponent = function(cartesian) {
  1281. if (!defined(cartesian)) {
  1282. throw new DeveloperError('cartesian is required');
  1283. }
  1284. return Math.min(cartesian.x, cartesian.y);
  1285. };
  1286. /**
  1287. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  1288. *
  1289. * @param {Cartesian2} first A cartesian to compare.
  1290. * @param {Cartesian2} second A cartesian to compare.
  1291. * @param {Cartesian2} result The object into which to store the result.
  1292. * @returns {Cartesian2} A cartesian with the minimum components.
  1293. */
  1294. Cartesian2.minimumByComponent = function(first, second, result) {
  1295. if (!defined(first)) {
  1296. throw new DeveloperError('first is required.');
  1297. }
  1298. if (!defined(second)) {
  1299. throw new DeveloperError('second is required.');
  1300. }
  1301. if (!defined(result)) {
  1302. throw new DeveloperError('result is required.');
  1303. }
  1304. result.x = Math.min(first.x, second.x);
  1305. result.y = Math.min(first.y, second.y);
  1306. return result;
  1307. };
  1308. /**
  1309. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  1310. *
  1311. * @param {Cartesian2} first A cartesian to compare.
  1312. * @param {Cartesian2} second A cartesian to compare.
  1313. * @param {Cartesian2} result The object into which to store the result.
  1314. * @returns {Cartesian2} A cartesian with the maximum components.
  1315. */
  1316. Cartesian2.maximumByComponent = function(first, second, result) {
  1317. if (!defined(first)) {
  1318. throw new DeveloperError('first is required.');
  1319. }
  1320. if (!defined(second)) {
  1321. throw new DeveloperError('second is required.');
  1322. }
  1323. if (!defined(result)) {
  1324. throw new DeveloperError('result is required.');
  1325. }
  1326. result.x = Math.max(first.x, second.x);
  1327. result.y = Math.max(first.y, second.y);
  1328. return result;
  1329. };
  1330. /**
  1331. * Computes the provided Cartesian's squared magnitude.
  1332. *
  1333. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  1334. * @returns {Number} The squared magnitude.
  1335. */
  1336. Cartesian2.magnitudeSquared = function(cartesian) {
  1337. if (!defined(cartesian)) {
  1338. throw new DeveloperError('cartesian is required');
  1339. }
  1340. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  1341. };
  1342. /**
  1343. * Computes the Cartesian's magnitude (length).
  1344. *
  1345. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  1346. * @returns {Number} The magnitude.
  1347. */
  1348. Cartesian2.magnitude = function(cartesian) {
  1349. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  1350. };
  1351. var distanceScratch = new Cartesian2();
  1352. /**
  1353. * Computes the distance between two points.
  1354. *
  1355. * @param {Cartesian2} left The first point to compute the distance from.
  1356. * @param {Cartesian2} right The second point to compute the distance to.
  1357. * @returns {Number} The distance between two points.
  1358. *
  1359. * @example
  1360. * // Returns 1.0
  1361. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  1362. */
  1363. Cartesian2.distance = function(left, right) {
  1364. if (!defined(left) || !defined(right)) {
  1365. throw new DeveloperError('left and right are required.');
  1366. }
  1367. Cartesian2.subtract(left, right, distanceScratch);
  1368. return Cartesian2.magnitude(distanceScratch);
  1369. };
  1370. /**
  1371. * Computes the squared distance between two points. Comparing squared distances
  1372. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  1373. *
  1374. * @param {Cartesian2} left The first point to compute the distance from.
  1375. * @param {Cartesian2} right The second point to compute the distance to.
  1376. * @returns {Number} The distance between two points.
  1377. *
  1378. * @example
  1379. * // Returns 4.0, not 2.0
  1380. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  1381. */
  1382. Cartesian2.distanceSquared = function(left, right) {
  1383. if (!defined(left) || !defined(right)) {
  1384. throw new DeveloperError('left and right are required.');
  1385. }
  1386. Cartesian2.subtract(left, right, distanceScratch);
  1387. return Cartesian2.magnitudeSquared(distanceScratch);
  1388. };
  1389. /**
  1390. * Computes the normalized form of the supplied Cartesian.
  1391. *
  1392. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  1393. * @param {Cartesian2} result The object onto which to store the result.
  1394. * @returns {Cartesian2} The modified result parameter.
  1395. */
  1396. Cartesian2.normalize = function(cartesian, result) {
  1397. if (!defined(cartesian)) {
  1398. throw new DeveloperError('cartesian is required');
  1399. }
  1400. if (!defined(result)) {
  1401. throw new DeveloperError('result is required');
  1402. }
  1403. var magnitude = Cartesian2.magnitude(cartesian);
  1404. result.x = cartesian.x / magnitude;
  1405. result.y = cartesian.y / magnitude;
  1406. if (isNaN(result.x) || isNaN(result.y)) {
  1407. throw new DeveloperError('normalized result is not a number');
  1408. }
  1409. return result;
  1410. };
  1411. /**
  1412. * Computes the dot (scalar) product of two Cartesians.
  1413. *
  1414. * @param {Cartesian2} left The first Cartesian.
  1415. * @param {Cartesian2} right The second Cartesian.
  1416. * @returns {Number} The dot product.
  1417. */
  1418. Cartesian2.dot = function(left, right) {
  1419. if (!defined(left)) {
  1420. throw new DeveloperError('left is required');
  1421. }
  1422. if (!defined(right)) {
  1423. throw new DeveloperError('right is required');
  1424. }
  1425. return left.x * right.x + left.y * right.y;
  1426. };
  1427. /**
  1428. * Computes the componentwise product of two Cartesians.
  1429. *
  1430. * @param {Cartesian2} left The first Cartesian.
  1431. * @param {Cartesian2} right The second Cartesian.
  1432. * @param {Cartesian2} result The object onto which to store the result.
  1433. * @returns {Cartesian2} The modified result parameter.
  1434. */
  1435. Cartesian2.multiplyComponents = function(left, right, result) {
  1436. if (!defined(left)) {
  1437. throw new DeveloperError('left is required');
  1438. }
  1439. if (!defined(right)) {
  1440. throw new DeveloperError('right is required');
  1441. }
  1442. if (!defined(result)) {
  1443. throw new DeveloperError('result is required');
  1444. }
  1445. result.x = left.x * right.x;
  1446. result.y = left.y * right.y;
  1447. return result;
  1448. };
  1449. /**
  1450. * Computes the componentwise quotient of two Cartesians.
  1451. *
  1452. * @param {Cartesian2} left The first Cartesian.
  1453. * @param {Cartesian2} right The second Cartesian.
  1454. * @param {Cartesian2} result The object onto which to store the result.
  1455. * @returns {Cartesian2} The modified result parameter.
  1456. */
  1457. Cartesian2.divideComponents = function(left, right, result) {
  1458. if (!defined(left)) {
  1459. throw new DeveloperError('left is required');
  1460. }
  1461. if (!defined(right)) {
  1462. throw new DeveloperError('right is required');
  1463. }
  1464. if (!defined(result)) {
  1465. throw new DeveloperError('result is required');
  1466. }
  1467. result.x = left.x / right.x;
  1468. result.y = left.y / right.y;
  1469. return result;
  1470. };
  1471. /**
  1472. * Computes the componentwise sum of two Cartesians.
  1473. *
  1474. * @param {Cartesian2} left The first Cartesian.
  1475. * @param {Cartesian2} right The second Cartesian.
  1476. * @param {Cartesian2} result The object onto which to store the result.
  1477. * @returns {Cartesian2} The modified result parameter.
  1478. */
  1479. Cartesian2.add = function(left, right, result) {
  1480. if (!defined(left)) {
  1481. throw new DeveloperError('left is required');
  1482. }
  1483. if (!defined(right)) {
  1484. throw new DeveloperError('right is required');
  1485. }
  1486. if (!defined(result)) {
  1487. throw new DeveloperError('result is required');
  1488. }
  1489. result.x = left.x + right.x;
  1490. result.y = left.y + right.y;
  1491. return result;
  1492. };
  1493. /**
  1494. * Computes the componentwise difference of two Cartesians.
  1495. *
  1496. * @param {Cartesian2} left The first Cartesian.
  1497. * @param {Cartesian2} right The second Cartesian.
  1498. * @param {Cartesian2} result The object onto which to store the result.
  1499. * @returns {Cartesian2} The modified result parameter.
  1500. */
  1501. Cartesian2.subtract = function(left, right, result) {
  1502. if (!defined(left)) {
  1503. throw new DeveloperError('left is required');
  1504. }
  1505. if (!defined(right)) {
  1506. throw new DeveloperError('right is required');
  1507. }
  1508. if (!defined(result)) {
  1509. throw new DeveloperError('result is required');
  1510. }
  1511. result.x = left.x - right.x;
  1512. result.y = left.y - right.y;
  1513. return result;
  1514. };
  1515. /**
  1516. * Multiplies the provided Cartesian componentwise by the provided scalar.
  1517. *
  1518. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  1519. * @param {Number} scalar The scalar to multiply with.
  1520. * @param {Cartesian2} result The object onto which to store the result.
  1521. * @returns {Cartesian2} The modified result parameter.
  1522. */
  1523. Cartesian2.multiplyByScalar = function(cartesian, scalar, result) {
  1524. if (!defined(cartesian)) {
  1525. throw new DeveloperError('cartesian is required');
  1526. }
  1527. if (typeof scalar !== 'number') {
  1528. throw new DeveloperError('scalar is required and must be a number.');
  1529. }
  1530. if (!defined(result)) {
  1531. throw new DeveloperError('result is required');
  1532. }
  1533. result.x = cartesian.x * scalar;
  1534. result.y = cartesian.y * scalar;
  1535. return result;
  1536. };
  1537. /**
  1538. * Divides the provided Cartesian componentwise by the provided scalar.
  1539. *
  1540. * @param {Cartesian2} cartesian The Cartesian to be divided.
  1541. * @param {Number} scalar The scalar to divide by.
  1542. * @param {Cartesian2} result The object onto which to store the result.
  1543. * @returns {Cartesian2} The modified result parameter.
  1544. */
  1545. Cartesian2.divideByScalar = function(cartesian, scalar, result) {
  1546. if (!defined(cartesian)) {
  1547. throw new DeveloperError('cartesian is required');
  1548. }
  1549. if (typeof scalar !== 'number') {
  1550. throw new DeveloperError('scalar is required and must be a number.');
  1551. }
  1552. if (!defined(result)) {
  1553. throw new DeveloperError('result is required');
  1554. }
  1555. result.x = cartesian.x / scalar;
  1556. result.y = cartesian.y / scalar;
  1557. return result;
  1558. };
  1559. /**
  1560. * Negates the provided Cartesian.
  1561. *
  1562. * @param {Cartesian2} cartesian The Cartesian to be negated.
  1563. * @param {Cartesian2} result The object onto which to store the result.
  1564. * @returns {Cartesian2} The modified result parameter.
  1565. */
  1566. Cartesian2.negate = function(cartesian, result) {
  1567. if (!defined(cartesian)) {
  1568. throw new DeveloperError('cartesian is required');
  1569. }
  1570. if (!defined(result)) {
  1571. throw new DeveloperError('result is required');
  1572. }
  1573. result.x = -cartesian.x;
  1574. result.y = -cartesian.y;
  1575. return result;
  1576. };
  1577. /**
  1578. * Computes the absolute value of the provided Cartesian.
  1579. *
  1580. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  1581. * @param {Cartesian2} result The object onto which to store the result.
  1582. * @returns {Cartesian2} The modified result parameter.
  1583. */
  1584. Cartesian2.abs = function(cartesian, result) {
  1585. if (!defined(cartesian)) {
  1586. throw new DeveloperError('cartesian is required');
  1587. }
  1588. if (!defined(result)) {
  1589. throw new DeveloperError('result is required');
  1590. }
  1591. result.x = Math.abs(cartesian.x);
  1592. result.y = Math.abs(cartesian.y);
  1593. return result;
  1594. };
  1595. var lerpScratch = new Cartesian2();
  1596. /**
  1597. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  1598. *
  1599. * @param {Cartesian2} start The value corresponding to t at 0.0.
  1600. * @param {Cartesian2} end The value corresponding to t at 1.0.
  1601. * @param {Number} t The point along t at which to interpolate.
  1602. * @param {Cartesian2} result The object onto which to store the result.
  1603. * @returns {Cartesian2} The modified result parameter.
  1604. */
  1605. Cartesian2.lerp = function(start, end, t, result) {
  1606. if (!defined(start)) {
  1607. throw new DeveloperError('start is required.');
  1608. }
  1609. if (!defined(end)) {
  1610. throw new DeveloperError('end is required.');
  1611. }
  1612. if (typeof t !== 'number') {
  1613. throw new DeveloperError('t is required and must be a number.');
  1614. }
  1615. if (!defined(result)) {
  1616. throw new DeveloperError('result is required.');
  1617. }
  1618. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  1619. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  1620. return Cartesian2.add(lerpScratch, result, result);
  1621. };
  1622. var angleBetweenScratch = new Cartesian2();
  1623. var angleBetweenScratch2 = new Cartesian2();
  1624. /**
  1625. * Returns the angle, in radians, between the provided Cartesians.
  1626. *
  1627. * @param {Cartesian2} left The first Cartesian.
  1628. * @param {Cartesian2} right The second Cartesian.
  1629. * @returns {Number} The angle between the Cartesians.
  1630. */
  1631. Cartesian2.angleBetween = function(left, right) {
  1632. if (!defined(left)) {
  1633. throw new DeveloperError('left is required');
  1634. }
  1635. if (!defined(right)) {
  1636. throw new DeveloperError('right is required');
  1637. }
  1638. Cartesian2.normalize(left, angleBetweenScratch);
  1639. Cartesian2.normalize(right, angleBetweenScratch2);
  1640. return CesiumMath.acosClamped(Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2));
  1641. };
  1642. var mostOrthogonalAxisScratch = new Cartesian2();
  1643. /**
  1644. * Returns the axis that is most orthogonal to the provided Cartesian.
  1645. *
  1646. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  1647. * @param {Cartesian2} result The object onto which to store the result.
  1648. * @returns {Cartesian2} The most orthogonal axis.
  1649. */
  1650. Cartesian2.mostOrthogonalAxis = function(cartesian, result) {
  1651. if (!defined(cartesian)) {
  1652. throw new DeveloperError('cartesian is required.');
  1653. }
  1654. if (!defined(result)) {
  1655. throw new DeveloperError('result is required.');
  1656. }
  1657. var f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  1658. Cartesian2.abs(f, f);
  1659. if (f.x <= f.y) {
  1660. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  1661. } else {
  1662. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  1663. }
  1664. return result;
  1665. };
  1666. /**
  1667. * Compares the provided Cartesians componentwise and returns
  1668. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1669. *
  1670. * @param {Cartesian2} [left] The first Cartesian.
  1671. * @param {Cartesian2} [right] The second Cartesian.
  1672. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1673. */
  1674. Cartesian2.equals = function(left, right) {
  1675. return (left === right) ||
  1676. ((defined(left)) &&
  1677. (defined(right)) &&
  1678. (left.x === right.x) &&
  1679. (left.y === right.y));
  1680. };
  1681. /**
  1682. * @private
  1683. */
  1684. Cartesian2.equalsArray = function(cartesian, array, offset) {
  1685. return cartesian.x === array[offset] &&
  1686. cartesian.y === array[offset + 1];
  1687. };
  1688. /**
  1689. * Compares the provided Cartesians componentwise and returns
  1690. * <code>true</code> if they pass an absolute or relative tolerance test,
  1691. * <code>false</code> otherwise.
  1692. *
  1693. * @param {Cartesian2} [left] The first Cartesian.
  1694. * @param {Cartesian2} [right] The second Cartesian.
  1695. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1696. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1697. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1698. */
  1699. Cartesian2.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  1700. return (left === right) ||
  1701. (defined(left) &&
  1702. defined(right) &&
  1703. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  1704. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon));
  1705. };
  1706. /**
  1707. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  1708. *
  1709. * @type {Cartesian2}
  1710. * @constant
  1711. */
  1712. Cartesian2.ZERO = freezeObject(new Cartesian2(0.0, 0.0));
  1713. /**
  1714. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  1715. *
  1716. * @type {Cartesian2}
  1717. * @constant
  1718. */
  1719. Cartesian2.UNIT_X = freezeObject(new Cartesian2(1.0, 0.0));
  1720. /**
  1721. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  1722. *
  1723. * @type {Cartesian2}
  1724. * @constant
  1725. */
  1726. Cartesian2.UNIT_Y = freezeObject(new Cartesian2(0.0, 1.0));
  1727. /**
  1728. * Duplicates this Cartesian2 instance.
  1729. *
  1730. * @param {Cartesian2} [result] The object onto which to store the result.
  1731. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  1732. */
  1733. Cartesian2.prototype.clone = function(result) {
  1734. return Cartesian2.clone(this, result);
  1735. };
  1736. /**
  1737. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1738. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1739. *
  1740. * @param {Cartesian2} [right] The right hand side Cartesian.
  1741. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1742. */
  1743. Cartesian2.prototype.equals = function(right) {
  1744. return Cartesian2.equals(this, right);
  1745. };
  1746. /**
  1747. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1748. * <code>true</code> if they pass an absolute or relative tolerance test,
  1749. * <code>false</code> otherwise.
  1750. *
  1751. * @param {Cartesian2} [right] The right hand side Cartesian.
  1752. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1753. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1754. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1755. */
  1756. Cartesian2.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  1757. return Cartesian2.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  1758. };
  1759. /**
  1760. * Creates a string representing this Cartesian in the format '(x, y)'.
  1761. *
  1762. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  1763. */
  1764. Cartesian2.prototype.toString = function() {
  1765. return '(' + this.x + ', ' + this.y + ')';
  1766. };
  1767. return Cartesian2;
  1768. });
  1769. /*global define*/
  1770. define('Core/isArray',[
  1771. './defined'
  1772. ], function(
  1773. defined) {
  1774. 'use strict';
  1775. /**
  1776. * Tests an object to see if it is an array.
  1777. * @exports isArray
  1778. *
  1779. * @param {Object} value The value to test.
  1780. * @returns {Boolean} true if the value is an array, false otherwise.
  1781. */
  1782. var isArray = Array.isArray;
  1783. if (!defined(isArray)) {
  1784. isArray = function(value) {
  1785. return Object.prototype.toString.call(value) === '[object Array]';
  1786. };
  1787. }
  1788. return isArray;
  1789. });
  1790. /*global define*/
  1791. define('Core/Check',[
  1792. './defaultValue',
  1793. './defined',
  1794. './DeveloperError',
  1795. './isArray'
  1796. ], function(
  1797. defaultValue,
  1798. defined,
  1799. DeveloperError,
  1800. isArray) {
  1801. 'use strict';
  1802. /**
  1803. * Contains functions for checking that supplied arguments are of a specified type
  1804. * or meet specified conditions
  1805. * @private
  1806. */
  1807. var Check = {};
  1808. /**
  1809. * Contains type checking functions, all using the typeof operator
  1810. */
  1811. Check.typeOf = {};
  1812. /**
  1813. * Contains functions for checking numeric conditions such as minimum and maximum values
  1814. */
  1815. Check.numeric = {};
  1816. function getUndefinedErrorMessage(name) {
  1817. return name + ' was required but undefined.';
  1818. }
  1819. function getFailedTypeErrorMessage(actual, expected, name) {
  1820. return 'Expected ' + name + ' to be typeof ' + expected + ', got ' + actual;
  1821. }
  1822. /**
  1823. * Throws if test is not defined
  1824. *
  1825. * @param {*} test The value that is to be checked
  1826. * @param {String} name The name of the variable being tested
  1827. * @exception {DeveloperError} test must be defined
  1828. */
  1829. Check.defined = function (test, name) {
  1830. if (!defined(test)) {
  1831. throw new DeveloperError(getUndefinedErrorMessage(name));
  1832. }
  1833. };
  1834. /**
  1835. * Throws if test is greater than maximum
  1836. *
  1837. * @param {Number} test The value to test
  1838. * @param {Number} maximum The maximum allowed value
  1839. * @exception {DeveloperError} test must not be greater than maximum
  1840. * @exception {DeveloperError} Both test and maximum must be typeof 'number'
  1841. */
  1842. Check.numeric.maximum = function (test, maximum) {
  1843. Check.typeOf.number(test);
  1844. Check.typeOf.number(maximum);
  1845. if (test > maximum) {
  1846. throw new DeveloperError('Expected ' + test + ' to be at most ' + maximum);
  1847. }
  1848. };
  1849. /**
  1850. * Throws if test is less than minimum
  1851. *
  1852. * @param {Number} test The value to test
  1853. * @param {Number} minimum The minimum allowed value
  1854. * @exception {DeveloperError} test must not be less than mininum
  1855. * @exception {DeveloperError} Both test and maximum must be typeof 'number'
  1856. */
  1857. Check.numeric.minimum = function (test, minimum) {
  1858. Check.typeOf.number(test);
  1859. Check.typeOf.number(minimum);
  1860. if (test < minimum) {
  1861. throw new DeveloperError('Expected ' + test + ' to be at least ' + minimum);
  1862. }
  1863. };
  1864. /**
  1865. * Throws if test is not typeof 'function'
  1866. *
  1867. * @param {*} test The value to test
  1868. * @param {String} name The name of the variable being tested
  1869. * @exception {DeveloperError} test must be typeof 'function'
  1870. */
  1871. Check.typeOf.function = function (test, name) {
  1872. if (typeof test !== 'function') {
  1873. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'function', name));
  1874. }
  1875. };
  1876. /**
  1877. * Throws if test is not typeof 'string'
  1878. *
  1879. * @param {*} test The value to test
  1880. * @param {String} name The name of the variable being tested
  1881. * @exception {DeveloperError} test must be typeof 'string'
  1882. */
  1883. Check.typeOf.string = function (test, name) {
  1884. if (typeof test !== 'string') {
  1885. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'string', name));
  1886. }
  1887. };
  1888. /**
  1889. * Throws if test is not typeof 'number'
  1890. *
  1891. * @param {*} test The value to test
  1892. * @param {String} name The name of the variable being tested
  1893. * @exception {DeveloperError} test must be typeof 'number'
  1894. */
  1895. Check.typeOf.number = function (test, name) {
  1896. if (typeof test !== 'number') {
  1897. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'number', name));
  1898. }
  1899. };
  1900. /**
  1901. * Throws if test is not typeof 'object'
  1902. *
  1903. * @param {*} test The value to test
  1904. * @param {String} name The name of the variable being tested
  1905. * @exception {DeveloperError} test must be typeof 'object'
  1906. */
  1907. Check.typeOf.object = function (test, name) {
  1908. if (typeof test !== 'object') {
  1909. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'object', name));
  1910. }
  1911. };
  1912. /**
  1913. * Throws if test is not typeof 'boolean'
  1914. *
  1915. * @param {*} test The value to test
  1916. * @param {String} name The name of the variable being tested
  1917. * @exception {DeveloperError} test must be typeof 'boolean'
  1918. */
  1919. Check.typeOf.boolean = function (test, name) {
  1920. if (typeof test !== 'boolean') {
  1921. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'boolean', name));
  1922. }
  1923. };
  1924. return Check;
  1925. });
  1926. /*global define*/
  1927. define('Core/Cartesian3',[
  1928. './Check',
  1929. './defaultValue',
  1930. './defined',
  1931. './DeveloperError',
  1932. './freezeObject',
  1933. './Math'
  1934. ], function(
  1935. Check,
  1936. defaultValue,
  1937. defined,
  1938. DeveloperError,
  1939. freezeObject,
  1940. CesiumMath) {
  1941. 'use strict';
  1942. /**
  1943. * A 3D Cartesian point.
  1944. * @alias Cartesian3
  1945. * @constructor
  1946. *
  1947. * @param {Number} [x=0.0] The X component.
  1948. * @param {Number} [y=0.0] The Y component.
  1949. * @param {Number} [z=0.0] The Z component.
  1950. *
  1951. * @see Cartesian2
  1952. * @see Cartesian4
  1953. * @see Packable
  1954. */
  1955. function Cartesian3(x, y, z) {
  1956. /**
  1957. * The X component.
  1958. * @type {Number}
  1959. * @default 0.0
  1960. */
  1961. this.x = defaultValue(x, 0.0);
  1962. /**
  1963. * The Y component.
  1964. * @type {Number}
  1965. * @default 0.0
  1966. */
  1967. this.y = defaultValue(y, 0.0);
  1968. /**
  1969. * The Z component.
  1970. * @type {Number}
  1971. * @default 0.0
  1972. */
  1973. this.z = defaultValue(z, 0.0);
  1974. }
  1975. /**
  1976. * Converts the provided Spherical into Cartesian3 coordinates.
  1977. *
  1978. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  1979. * @param {Cartesian3} [result] The object onto which to store the result.
  1980. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1981. */
  1982. Cartesian3.fromSpherical = function(spherical, result) {
  1983. Check.typeOf.object(spherical, 'spherical');
  1984. if (!defined(result)) {
  1985. result = new Cartesian3();
  1986. }
  1987. var clock = spherical.clock;
  1988. var cone = spherical.cone;
  1989. var magnitude = defaultValue(spherical.magnitude, 1.0);
  1990. var radial = magnitude * Math.sin(cone);
  1991. result.x = radial * Math.cos(clock);
  1992. result.y = radial * Math.sin(clock);
  1993. result.z = magnitude * Math.cos(cone);
  1994. return result;
  1995. };
  1996. /**
  1997. * Creates a Cartesian3 instance from x, y and z coordinates.
  1998. *
  1999. * @param {Number} x The x coordinate.
  2000. * @param {Number} y The y coordinate.
  2001. * @param {Number} z The z coordinate.
  2002. * @param {Cartesian3} [result] The object onto which to store the result.
  2003. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2004. */
  2005. Cartesian3.fromElements = function(x, y, z, result) {
  2006. if (!defined(result)) {
  2007. return new Cartesian3(x, y, z);
  2008. }
  2009. result.x = x;
  2010. result.y = y;
  2011. result.z = z;
  2012. return result;
  2013. };
  2014. /**
  2015. * Duplicates a Cartesian3 instance.
  2016. *
  2017. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  2018. * @param {Cartesian3} [result] The object onto which to store the result.
  2019. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  2020. */
  2021. Cartesian3.clone = function(cartesian, result) {
  2022. if (!defined(cartesian)) {
  2023. return undefined;
  2024. }
  2025. if (!defined(result)) {
  2026. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  2027. }
  2028. result.x = cartesian.x;
  2029. result.y = cartesian.y;
  2030. result.z = cartesian.z;
  2031. return result;
  2032. };
  2033. /**
  2034. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  2035. * x, y, and z properties of the Cartesian4 and drops w.
  2036. * @function
  2037. *
  2038. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  2039. * @param {Cartesian3} [result] The object onto which to store the result.
  2040. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2041. */
  2042. Cartesian3.fromCartesian4 = Cartesian3.clone;
  2043. /**
  2044. * The number of elements used to pack the object into an array.
  2045. * @type {Number}
  2046. */
  2047. Cartesian3.packedLength = 3;
  2048. /**
  2049. * Stores the provided instance into the provided array.
  2050. *
  2051. * @param {Cartesian3} value The value to pack.
  2052. * @param {Number[]} array The array to pack into.
  2053. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2054. *
  2055. * @returns {Number[]} The array that was packed into
  2056. */
  2057. Cartesian3.pack = function(value, array, startingIndex) {
  2058. Check.typeOf.object(value, 'value');
  2059. Check.defined(array, 'array');
  2060. startingIndex = defaultValue(startingIndex, 0);
  2061. array[startingIndex++] = value.x;
  2062. array[startingIndex++] = value.y;
  2063. array[startingIndex] = value.z;
  2064. return array;
  2065. };
  2066. /**
  2067. * Retrieves an instance from a packed array.
  2068. *
  2069. * @param {Number[]} array The packed array.
  2070. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2071. * @param {Cartesian3} [result] The object into which to store the result.
  2072. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2073. */
  2074. Cartesian3.unpack = function(array, startingIndex, result) {
  2075. Check.defined(array, 'array');
  2076. startingIndex = defaultValue(startingIndex, 0);
  2077. if (!defined(result)) {
  2078. result = new Cartesian3();
  2079. }
  2080. result.x = array[startingIndex++];
  2081. result.y = array[startingIndex++];
  2082. result.z = array[startingIndex];
  2083. return result;
  2084. };
  2085. /**
  2086. * Flattens an array of Cartesian3s into an array of components.
  2087. *
  2088. * @param {Cartesian3[]} array The array of cartesians to pack.
  2089. * @param {Number[]} result The array onto which to store the result.
  2090. * @returns {Number[]} The packed array.
  2091. */
  2092. Cartesian3.packArray = function(array, result) {
  2093. Check.defined(array, 'array');
  2094. var length = array.length;
  2095. if (!defined(result)) {
  2096. result = new Array(length * 3);
  2097. } else {
  2098. result.length = length * 3;
  2099. }
  2100. for (var i = 0; i < length; ++i) {
  2101. Cartesian3.pack(array[i], result, i * 3);
  2102. }
  2103. return result;
  2104. };
  2105. /**
  2106. * Unpacks an array of cartesian components into an array of Cartesian3s.
  2107. *
  2108. * @param {Number[]} array The array of components to unpack.
  2109. * @param {Cartesian3[]} result The array onto which to store the result.
  2110. * @returns {Cartesian3[]} The unpacked array.
  2111. */
  2112. Cartesian3.unpackArray = function(array, result) {
  2113. Check.defined(array, 'array');
  2114. Check.numeric.minimum(array.length, 3);
  2115. if (array.length % 3 !== 0) {
  2116. throw new DeveloperError('array length must be a multiple of 3.');
  2117. }
  2118. var length = array.length;
  2119. if (!defined(result)) {
  2120. result = new Array(length / 3);
  2121. } else {
  2122. result.length = length / 3;
  2123. }
  2124. for (var i = 0; i < length; i += 3) {
  2125. var index = i / 3;
  2126. result[index] = Cartesian3.unpack(array, i, result[index]);
  2127. }
  2128. return result;
  2129. };
  2130. /**
  2131. * Creates a Cartesian3 from three consecutive elements in an array.
  2132. * @function
  2133. *
  2134. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  2135. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  2136. * @param {Cartesian3} [result] The object onto which to store the result.
  2137. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2138. *
  2139. * @example
  2140. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  2141. * var v = [1.0, 2.0, 3.0];
  2142. * var p = Cesium.Cartesian3.fromArray(v);
  2143. *
  2144. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  2145. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  2146. * var p2 = Cesium.Cartesian3.fromArray(v2, 2);
  2147. */
  2148. Cartesian3.fromArray = Cartesian3.unpack;
  2149. /**
  2150. * Computes the value of the maximum component for the supplied Cartesian.
  2151. *
  2152. * @param {Cartesian3} cartesian The cartesian to use.
  2153. * @returns {Number} The value of the maximum component.
  2154. */
  2155. Cartesian3.maximumComponent = function(cartesian) {
  2156. Check.typeOf.object(cartesian, 'cartesian');
  2157. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  2158. };
  2159. /**
  2160. * Computes the value of the minimum component for the supplied Cartesian.
  2161. *
  2162. * @param {Cartesian3} cartesian The cartesian to use.
  2163. * @returns {Number} The value of the minimum component.
  2164. */
  2165. Cartesian3.minimumComponent = function(cartesian) {
  2166. Check.typeOf.object(cartesian, 'cartesian');
  2167. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  2168. };
  2169. /**
  2170. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  2171. *
  2172. * @param {Cartesian3} first A cartesian to compare.
  2173. * @param {Cartesian3} second A cartesian to compare.
  2174. * @param {Cartesian3} result The object into which to store the result.
  2175. * @returns {Cartesian3} A cartesian with the minimum components.
  2176. */
  2177. Cartesian3.minimumByComponent = function(first, second, result) {
  2178. Check.typeOf.object(first, 'first');
  2179. Check.typeOf.object(second, 'second');
  2180. Check.typeOf.object(result, 'result');
  2181. result.x = Math.min(first.x, second.x);
  2182. result.y = Math.min(first.y, second.y);
  2183. result.z = Math.min(first.z, second.z);
  2184. return result;
  2185. };
  2186. /**
  2187. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  2188. *
  2189. * @param {Cartesian3} first A cartesian to compare.
  2190. * @param {Cartesian3} second A cartesian to compare.
  2191. * @param {Cartesian3} result The object into which to store the result.
  2192. * @returns {Cartesian3} A cartesian with the maximum components.
  2193. */
  2194. Cartesian3.maximumByComponent = function(first, second, result) {
  2195. Check.typeOf.object(first, 'first');
  2196. Check.typeOf.object(second, 'second');
  2197. Check.typeOf.object(result, 'result');
  2198. result.x = Math.max(first.x, second.x);
  2199. result.y = Math.max(first.y, second.y);
  2200. result.z = Math.max(first.z, second.z);
  2201. return result;
  2202. };
  2203. /**
  2204. * Computes the provided Cartesian's squared magnitude.
  2205. *
  2206. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  2207. * @returns {Number} The squared magnitude.
  2208. */
  2209. Cartesian3.magnitudeSquared = function(cartesian) {
  2210. Check.typeOf.object(cartesian, 'cartesian');
  2211. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z;
  2212. };
  2213. /**
  2214. * Computes the Cartesian's magnitude (length).
  2215. *
  2216. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  2217. * @returns {Number} The magnitude.
  2218. */
  2219. Cartesian3.magnitude = function(cartesian) {
  2220. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  2221. };
  2222. var distanceScratch = new Cartesian3();
  2223. /**
  2224. * Computes the distance between two points.
  2225. *
  2226. * @param {Cartesian3} left The first point to compute the distance from.
  2227. * @param {Cartesian3} right The second point to compute the distance to.
  2228. * @returns {Number} The distance between two points.
  2229. *
  2230. * @example
  2231. * // Returns 1.0
  2232. * var d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  2233. */
  2234. Cartesian3.distance = function(left, right) {
  2235. Check.typeOf.object(left, 'left');
  2236. Check.typeOf.object(right, 'right');
  2237. Cartesian3.subtract(left, right, distanceScratch);
  2238. return Cartesian3.magnitude(distanceScratch);
  2239. };
  2240. /**
  2241. * Computes the squared distance between two points. Comparing squared distances
  2242. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  2243. *
  2244. * @param {Cartesian3} left The first point to compute the distance from.
  2245. * @param {Cartesian3} right The second point to compute the distance to.
  2246. * @returns {Number} The distance between two points.
  2247. *
  2248. * @example
  2249. * // Returns 4.0, not 2.0
  2250. * var d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  2251. */
  2252. Cartesian3.distanceSquared = function(left, right) {
  2253. Check.typeOf.object(left, 'left');
  2254. Check.typeOf.object(right, 'right');
  2255. Cartesian3.subtract(left, right, distanceScratch);
  2256. return Cartesian3.magnitudeSquared(distanceScratch);
  2257. };
  2258. /**
  2259. * Computes the normalized form of the supplied Cartesian.
  2260. *
  2261. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  2262. * @param {Cartesian3} result The object onto which to store the result.
  2263. * @returns {Cartesian3} The modified result parameter.
  2264. */
  2265. Cartesian3.normalize = function(cartesian, result) {
  2266. Check.typeOf.object(cartesian, 'cartesian');
  2267. Check.typeOf.object(result, 'result');
  2268. var magnitude = Cartesian3.magnitude(cartesian);
  2269. result.x = cartesian.x / magnitude;
  2270. result.y = cartesian.y / magnitude;
  2271. result.z = cartesian.z / magnitude;
  2272. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  2273. throw new DeveloperError('normalized result is not a number');
  2274. }
  2275. return result;
  2276. };
  2277. /**
  2278. * Computes the dot (scalar) product of two Cartesians.
  2279. *
  2280. * @param {Cartesian3} left The first Cartesian.
  2281. * @param {Cartesian3} right The second Cartesian.
  2282. * @returns {Number} The dot product.
  2283. */
  2284. Cartesian3.dot = function(left, right) {
  2285. Check.typeOf.object(left, 'left');
  2286. Check.typeOf.object(right, 'right');
  2287. return left.x * right.x + left.y * right.y + left.z * right.z;
  2288. };
  2289. /**
  2290. * Computes the componentwise product of two Cartesians.
  2291. *
  2292. * @param {Cartesian3} left The first Cartesian.
  2293. * @param {Cartesian3} right The second Cartesian.
  2294. * @param {Cartesian3} result The object onto which to store the result.
  2295. * @returns {Cartesian3} The modified result parameter.
  2296. */
  2297. Cartesian3.multiplyComponents = function(left, right, result) {
  2298. Check.typeOf.object(left, 'left');
  2299. Check.typeOf.object(right, 'right');
  2300. Check.typeOf.object(result, 'result');
  2301. result.x = left.x * right.x;
  2302. result.y = left.y * right.y;
  2303. result.z = left.z * right.z;
  2304. return result;
  2305. };
  2306. /**
  2307. * Computes the componentwise quotient of two Cartesians.
  2308. *
  2309. * @param {Cartesian3} left The first Cartesian.
  2310. * @param {Cartesian3} right The second Cartesian.
  2311. * @param {Cartesian3} result The object onto which to store the result.
  2312. * @returns {Cartesian3} The modified result parameter.
  2313. */
  2314. Cartesian3.divideComponents = function(left, right, result) {
  2315. if (!defined(left)) {
  2316. throw new DeveloperError('left is required');
  2317. }
  2318. if (!defined(right)) {
  2319. throw new DeveloperError('right is required');
  2320. }
  2321. if (!defined(result)) {
  2322. throw new DeveloperError('result is required');
  2323. }
  2324. result.x = left.x / right.x;
  2325. result.y = left.y / right.y;
  2326. result.z = left.z / right.z;
  2327. return result;
  2328. };
  2329. /**
  2330. * Computes the componentwise sum of two Cartesians.
  2331. *
  2332. * @param {Cartesian3} left The first Cartesian.
  2333. * @param {Cartesian3} right The second Cartesian.
  2334. * @param {Cartesian3} result The object onto which to store the result.
  2335. * @returns {Cartesian3} The modified result parameter.
  2336. */
  2337. Cartesian3.add = function(left, right, result) {
  2338. Check.typeOf.object(left, 'left');
  2339. Check.typeOf.object(right, 'right');
  2340. Check.typeOf.object(result, 'result');
  2341. result.x = left.x + right.x;
  2342. result.y = left.y + right.y;
  2343. result.z = left.z + right.z;
  2344. return result;
  2345. };
  2346. /**
  2347. * Computes the componentwise difference of two Cartesians.
  2348. *
  2349. * @param {Cartesian3} left The first Cartesian.
  2350. * @param {Cartesian3} right The second Cartesian.
  2351. * @param {Cartesian3} result The object onto which to store the result.
  2352. * @returns {Cartesian3} The modified result parameter.
  2353. */
  2354. Cartesian3.subtract = function(left, right, result) {
  2355. Check.typeOf.object(left, 'left');
  2356. Check.typeOf.object(right, 'right');
  2357. Check.typeOf.object(result, 'result');
  2358. result.x = left.x - right.x;
  2359. result.y = left.y - right.y;
  2360. result.z = left.z - right.z;
  2361. return result;
  2362. };
  2363. /**
  2364. * Multiplies the provided Cartesian componentwise by the provided scalar.
  2365. *
  2366. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  2367. * @param {Number} scalar The scalar to multiply with.
  2368. * @param {Cartesian3} result The object onto which to store the result.
  2369. * @returns {Cartesian3} The modified result parameter.
  2370. */
  2371. Cartesian3.multiplyByScalar = function(cartesian, scalar, result) {
  2372. Check.typeOf.object(cartesian, 'cartesian');
  2373. Check.typeOf.number(scalar, 'scalar');
  2374. Check.typeOf.object(result, 'result');
  2375. result.x = cartesian.x * scalar;
  2376. result.y = cartesian.y * scalar;
  2377. result.z = cartesian.z * scalar;
  2378. return result;
  2379. };
  2380. /**
  2381. * Divides the provided Cartesian componentwise by the provided scalar.
  2382. *
  2383. * @param {Cartesian3} cartesian The Cartesian to be divided.
  2384. * @param {Number} scalar The scalar to divide by.
  2385. * @param {Cartesian3} result The object onto which to store the result.
  2386. * @returns {Cartesian3} The modified result parameter.
  2387. */
  2388. Cartesian3.divideByScalar = function(cartesian, scalar, result) {
  2389. Check.typeOf.object(cartesian, 'cartesian');
  2390. Check.typeOf.number(scalar, 'scalar');
  2391. Check.typeOf.object(result, 'result');
  2392. result.x = cartesian.x / scalar;
  2393. result.y = cartesian.y / scalar;
  2394. result.z = cartesian.z / scalar;
  2395. return result;
  2396. };
  2397. /**
  2398. * Negates the provided Cartesian.
  2399. *
  2400. * @param {Cartesian3} cartesian The Cartesian to be negated.
  2401. * @param {Cartesian3} result The object onto which to store the result.
  2402. * @returns {Cartesian3} The modified result parameter.
  2403. */
  2404. Cartesian3.negate = function(cartesian, result) {
  2405. Check.typeOf.object(cartesian, 'cartesian');
  2406. Check.typeOf.object(result, 'result');
  2407. result.x = -cartesian.x;
  2408. result.y = -cartesian.y;
  2409. result.z = -cartesian.z;
  2410. return result;
  2411. };
  2412. /**
  2413. * Computes the absolute value of the provided Cartesian.
  2414. *
  2415. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  2416. * @param {Cartesian3} result The object onto which to store the result.
  2417. * @returns {Cartesian3} The modified result parameter.
  2418. */
  2419. Cartesian3.abs = function(cartesian, result) {
  2420. Check.typeOf.object(cartesian, 'cartesian');
  2421. Check.typeOf.object(result, 'result');
  2422. result.x = Math.abs(cartesian.x);
  2423. result.y = Math.abs(cartesian.y);
  2424. result.z = Math.abs(cartesian.z);
  2425. return result;
  2426. };
  2427. var lerpScratch = new Cartesian3();
  2428. /**
  2429. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  2430. *
  2431. * @param {Cartesian3} start The value corresponding to t at 0.0.
  2432. * @param {Cartesian3} end The value corresponding to t at 1.0.
  2433. * @param {Number} t The point along t at which to interpolate.
  2434. * @param {Cartesian3} result The object onto which to store the result.
  2435. * @returns {Cartesian3} The modified result parameter.
  2436. */
  2437. Cartesian3.lerp = function(start, end, t, result) {
  2438. Check.typeOf.object(start, 'start');
  2439. Check.typeOf.object(end, 'end');
  2440. Check.typeOf.number(t, 't');
  2441. Check.typeOf.object(result, 'result');
  2442. Cartesian3.multiplyByScalar(end, t, lerpScratch);
  2443. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  2444. return Cartesian3.add(lerpScratch, result, result);
  2445. };
  2446. var angleBetweenScratch = new Cartesian3();
  2447. var angleBetweenScratch2 = new Cartesian3();
  2448. /**
  2449. * Returns the angle, in radians, between the provided Cartesians.
  2450. *
  2451. * @param {Cartesian3} left The first Cartesian.
  2452. * @param {Cartesian3} right The second Cartesian.
  2453. * @returns {Number} The angle between the Cartesians.
  2454. */
  2455. Cartesian3.angleBetween = function(left, right) {
  2456. Check.typeOf.object(left, 'left');
  2457. Check.typeOf.object(right, 'right');
  2458. Cartesian3.normalize(left, angleBetweenScratch);
  2459. Cartesian3.normalize(right, angleBetweenScratch2);
  2460. var cosine = Cartesian3.dot(angleBetweenScratch, angleBetweenScratch2);
  2461. var sine = Cartesian3.magnitude(Cartesian3.cross(angleBetweenScratch, angleBetweenScratch2, angleBetweenScratch));
  2462. return Math.atan2(sine, cosine);
  2463. };
  2464. var mostOrthogonalAxisScratch = new Cartesian3();
  2465. /**
  2466. * Returns the axis that is most orthogonal to the provided Cartesian.
  2467. *
  2468. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  2469. * @param {Cartesian3} result The object onto which to store the result.
  2470. * @returns {Cartesian3} The most orthogonal axis.
  2471. */
  2472. Cartesian3.mostOrthogonalAxis = function(cartesian, result) {
  2473. Check.typeOf.object(cartesian, 'cartesian');
  2474. Check.typeOf.object(result, 'result');
  2475. var f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch);
  2476. Cartesian3.abs(f, f);
  2477. if (f.x <= f.y) {
  2478. if (f.x <= f.z) {
  2479. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  2480. } else {
  2481. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  2482. }
  2483. } else {
  2484. if (f.y <= f.z) {
  2485. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  2486. } else {
  2487. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  2488. }
  2489. }
  2490. return result;
  2491. };
  2492. /**
  2493. * Compares the provided Cartesians componentwise and returns
  2494. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2495. *
  2496. * @param {Cartesian3} [left] The first Cartesian.
  2497. * @param {Cartesian3} [right] The second Cartesian.
  2498. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2499. */
  2500. Cartesian3.equals = function(left, right) {
  2501. return (left === right) ||
  2502. ((defined(left)) &&
  2503. (defined(right)) &&
  2504. (left.x === right.x) &&
  2505. (left.y === right.y) &&
  2506. (left.z === right.z));
  2507. };
  2508. /**
  2509. * @private
  2510. */
  2511. Cartesian3.equalsArray = function(cartesian, array, offset) {
  2512. return cartesian.x === array[offset] &&
  2513. cartesian.y === array[offset + 1] &&
  2514. cartesian.z === array[offset + 2];
  2515. };
  2516. /**
  2517. * Compares the provided Cartesians componentwise and returns
  2518. * <code>true</code> if they pass an absolute or relative tolerance test,
  2519. * <code>false</code> otherwise.
  2520. *
  2521. * @param {Cartesian3} [left] The first Cartesian.
  2522. * @param {Cartesian3} [right] The second Cartesian.
  2523. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  2524. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  2525. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2526. */
  2527. Cartesian3.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  2528. return (left === right) ||
  2529. (defined(left) &&
  2530. defined(right) &&
  2531. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  2532. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  2533. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon));
  2534. };
  2535. /**
  2536. * Computes the cross (outer) product of two Cartesians.
  2537. *
  2538. * @param {Cartesian3} left The first Cartesian.
  2539. * @param {Cartesian3} right The second Cartesian.
  2540. * @param {Cartesian3} result The object onto which to store the result.
  2541. * @returns {Cartesian3} The cross product.
  2542. */
  2543. Cartesian3.cross = function(left, right, result) {
  2544. Check.typeOf.object(left, 'left');
  2545. Check.typeOf.object(right, 'right');
  2546. Check.typeOf.object(result, 'result');
  2547. var leftX = left.x;
  2548. var leftY = left.y;
  2549. var leftZ = left.z;
  2550. var rightX = right.x;
  2551. var rightY = right.y;
  2552. var rightZ = right.z;
  2553. var x = leftY * rightZ - leftZ * rightY;
  2554. var y = leftZ * rightX - leftX * rightZ;
  2555. var z = leftX * rightY - leftY * rightX;
  2556. result.x = x;
  2557. result.y = y;
  2558. result.z = z;
  2559. return result;
  2560. };
  2561. /**
  2562. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  2563. *
  2564. * @param {Number} longitude The longitude, in degrees
  2565. * @param {Number} latitude The latitude, in degrees
  2566. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2567. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2568. * @param {Cartesian3} [result] The object onto which to store the result.
  2569. * @returns {Cartesian3} The position
  2570. *
  2571. * @example
  2572. * var position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  2573. */
  2574. Cartesian3.fromDegrees = function(longitude, latitude, height, ellipsoid, result) {
  2575. Check.typeOf.number(longitude, 'longitude');
  2576. Check.typeOf.number(latitude, 'latitude');
  2577. longitude = CesiumMath.toRadians(longitude);
  2578. latitude = CesiumMath.toRadians(latitude);
  2579. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  2580. };
  2581. var scratchN = new Cartesian3();
  2582. var scratchK = new Cartesian3();
  2583. var wgs84RadiiSquared = new Cartesian3(6378137.0 * 6378137.0, 6378137.0 * 6378137.0, 6356752.3142451793 * 6356752.3142451793);
  2584. /**
  2585. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  2586. *
  2587. * @param {Number} longitude The longitude, in radians
  2588. * @param {Number} latitude The latitude, in radians
  2589. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2590. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2591. * @param {Cartesian3} [result] The object onto which to store the result.
  2592. * @returns {Cartesian3} The position
  2593. *
  2594. * @example
  2595. * var position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  2596. */
  2597. Cartesian3.fromRadians = function(longitude, latitude, height, ellipsoid, result) {
  2598. Check.typeOf.number(longitude, 'longitude');
  2599. Check.typeOf.number(latitude, 'latitude');
  2600. height = defaultValue(height, 0.0);
  2601. var radiiSquared = defined(ellipsoid) ? ellipsoid.radiiSquared : wgs84RadiiSquared;
  2602. var cosLatitude = Math.cos(latitude);
  2603. scratchN.x = cosLatitude * Math.cos(longitude);
  2604. scratchN.y = cosLatitude * Math.sin(longitude);
  2605. scratchN.z = Math.sin(latitude);
  2606. scratchN = Cartesian3.normalize(scratchN, scratchN);
  2607. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  2608. var gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  2609. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  2610. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  2611. if (!defined(result)) {
  2612. result = new Cartesian3();
  2613. }
  2614. return Cartesian3.add(scratchK, scratchN, result);
  2615. };
  2616. /**
  2617. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  2618. *
  2619. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  2620. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  2621. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  2622. * @returns {Cartesian3[]} The array of positions.
  2623. *
  2624. * @example
  2625. * var positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  2626. */
  2627. Cartesian3.fromDegreesArray = function(coordinates, ellipsoid, result) {
  2628. Check.defined(coordinates, 'coordinates');
  2629. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  2630. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  2631. }
  2632. var length = coordinates.length;
  2633. if (!defined(result)) {
  2634. result = new Array(length / 2);
  2635. } else {
  2636. result.length = length / 2;
  2637. }
  2638. for (var i = 0; i < length; i += 2) {
  2639. var longitude = coordinates[i];
  2640. var latitude = coordinates[i + 1];
  2641. var index = i / 2;
  2642. result[index] = Cartesian3.fromDegrees(longitude, latitude, 0, ellipsoid, result[index]);
  2643. }
  2644. return result;
  2645. };
  2646. /**
  2647. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  2648. *
  2649. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  2650. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  2651. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  2652. * @returns {Cartesian3[]} The array of positions.
  2653. *
  2654. * @example
  2655. * var positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  2656. */
  2657. Cartesian3.fromRadiansArray = function(coordinates, ellipsoid, result) {
  2658. Check.defined(coordinates, 'coordinates');
  2659. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  2660. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  2661. }
  2662. var length = coordinates.length;
  2663. if (!defined(result)) {
  2664. result = new Array(length / 2);
  2665. } else {
  2666. result.length = length / 2;
  2667. }
  2668. for (var i = 0; i < length; i += 2) {
  2669. var longitude = coordinates[i];
  2670. var latitude = coordinates[i + 1];
  2671. var index = i / 2;
  2672. result[index] = Cartesian3.fromRadians(longitude, latitude, 0, ellipsoid, result[index]);
  2673. }
  2674. return result;
  2675. };
  2676. /**
  2677. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  2678. *
  2679. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  2680. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2681. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  2682. * @returns {Cartesian3[]} The array of positions.
  2683. *
  2684. * @example
  2685. * var positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  2686. */
  2687. Cartesian3.fromDegreesArrayHeights = function(coordinates, ellipsoid, result) {
  2688. Check.defined(coordinates, 'coordinates');
  2689. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  2690. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  2691. }
  2692. var length = coordinates.length;
  2693. if (!defined(result)) {
  2694. result = new Array(length / 3);
  2695. } else {
  2696. result.length = length / 3;
  2697. }
  2698. for (var i = 0; i < length; i += 3) {
  2699. var longitude = coordinates[i];
  2700. var latitude = coordinates[i + 1];
  2701. var height = coordinates[i + 2];
  2702. var index = i / 3;
  2703. result[index] = Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid, result[index]);
  2704. }
  2705. return result;
  2706. };
  2707. /**
  2708. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  2709. *
  2710. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  2711. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2712. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  2713. * @returns {Cartesian3[]} The array of positions.
  2714. *
  2715. * @example
  2716. * var positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  2717. */
  2718. Cartesian3.fromRadiansArrayHeights = function(coordinates, ellipsoid, result) {
  2719. Check.defined(coordinates, 'coordinates');
  2720. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  2721. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  2722. }
  2723. var length = coordinates.length;
  2724. if (!defined(result)) {
  2725. result = new Array(length / 3);
  2726. } else {
  2727. result.length = length / 3;
  2728. }
  2729. for (var i = 0; i < length; i += 3) {
  2730. var longitude = coordinates[i];
  2731. var latitude = coordinates[i + 1];
  2732. var height = coordinates[i + 2];
  2733. var index = i / 3;
  2734. result[index] = Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result[index]);
  2735. }
  2736. return result;
  2737. };
  2738. /**
  2739. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  2740. *
  2741. * @type {Cartesian3}
  2742. * @constant
  2743. */
  2744. Cartesian3.ZERO = freezeObject(new Cartesian3(0.0, 0.0, 0.0));
  2745. /**
  2746. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  2747. *
  2748. * @type {Cartesian3}
  2749. * @constant
  2750. */
  2751. Cartesian3.UNIT_X = freezeObject(new Cartesian3(1.0, 0.0, 0.0));
  2752. /**
  2753. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  2754. *
  2755. * @type {Cartesian3}
  2756. * @constant
  2757. */
  2758. Cartesian3.UNIT_Y = freezeObject(new Cartesian3(0.0, 1.0, 0.0));
  2759. /**
  2760. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  2761. *
  2762. * @type {Cartesian3}
  2763. * @constant
  2764. */
  2765. Cartesian3.UNIT_Z = freezeObject(new Cartesian3(0.0, 0.0, 1.0));
  2766. /**
  2767. * Duplicates this Cartesian3 instance.
  2768. *
  2769. * @param {Cartesian3} [result] The object onto which to store the result.
  2770. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2771. */
  2772. Cartesian3.prototype.clone = function(result) {
  2773. return Cartesian3.clone(this, result);
  2774. };
  2775. /**
  2776. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2777. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2778. *
  2779. * @param {Cartesian3} [right] The right hand side Cartesian.
  2780. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2781. */
  2782. Cartesian3.prototype.equals = function(right) {
  2783. return Cartesian3.equals(this, right);
  2784. };
  2785. /**
  2786. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2787. * <code>true</code> if they pass an absolute or relative tolerance test,
  2788. * <code>false</code> otherwise.
  2789. *
  2790. * @param {Cartesian3} [right] The right hand side Cartesian.
  2791. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  2792. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  2793. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  2794. */
  2795. Cartesian3.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  2796. return Cartesian3.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  2797. };
  2798. /**
  2799. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  2800. *
  2801. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  2802. */
  2803. Cartesian3.prototype.toString = function() {
  2804. return '(' + this.x + ', ' + this.y + ', ' + this.z + ')';
  2805. };
  2806. return Cartesian3;
  2807. });
  2808. /*global define*/
  2809. define('Core/AttributeCompression',[
  2810. './Cartesian2',
  2811. './Cartesian3',
  2812. './defined',
  2813. './DeveloperError',
  2814. './Math'
  2815. ], function(
  2816. Cartesian2,
  2817. Cartesian3,
  2818. defined,
  2819. DeveloperError,
  2820. CesiumMath) {
  2821. 'use strict';
  2822. /**
  2823. * Attribute compression and decompression functions.
  2824. *
  2825. * @exports AttributeCompression
  2826. *
  2827. * @private
  2828. */
  2829. var AttributeCompression = {};
  2830. /**
  2831. * Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding.
  2832. *
  2833. * Oct encoding is a compact representation of unit length vectors.
  2834. * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors",
  2835. * Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/}
  2836. *
  2837. * @param {Cartesian3} vector The normalized vector to be compressed into 2 component 'oct' encoding.
  2838. * @param {Cartesian2} result The 2 component oct-encoded unit length vector.
  2839. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  2840. * @returns {Cartesian2} The 2 component oct-encoded unit length vector.
  2841. *
  2842. * @exception {DeveloperError} vector must be normalized.
  2843. *
  2844. * @see AttributeCompression.octDecodeInRange
  2845. */
  2846. AttributeCompression.octEncodeInRange = function(vector, rangeMax, result) {
  2847. if (!defined(vector)) {
  2848. throw new DeveloperError('vector is required.');
  2849. }
  2850. if (!defined(result)) {
  2851. throw new DeveloperError('result is required.');
  2852. }
  2853. var magSquared = Cartesian3.magnitudeSquared(vector);
  2854. if (Math.abs(magSquared - 1.0) > CesiumMath.EPSILON6) {
  2855. throw new DeveloperError('vector must be normalized.');
  2856. }
  2857. result.x = vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  2858. result.y = vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  2859. if (vector.z < 0) {
  2860. var x = result.x;
  2861. var y = result.y;
  2862. result.x = (1.0 - Math.abs(y)) * CesiumMath.signNotZero(x);
  2863. result.y = (1.0 - Math.abs(x)) * CesiumMath.signNotZero(y);
  2864. }
  2865. result.x = CesiumMath.toSNorm(result.x, rangeMax);
  2866. result.y = CesiumMath.toSNorm(result.y, rangeMax);
  2867. return result;
  2868. };
  2869. /**
  2870. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding.
  2871. *
  2872. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  2873. * @param {Cartesian2} result The 2 byte oct-encoded unit length vector.
  2874. * @returns {Cartesian2} The 2 byte oct-encoded unit length vector.
  2875. *
  2876. * @exception {DeveloperError} vector must be normalized.
  2877. *
  2878. * @see AttributeCompression.octEncodeInRange
  2879. * @see AttributeCompression.octDecode
  2880. */
  2881. AttributeCompression.octEncode = function(vector, result) {
  2882. return AttributeCompression.octEncodeInRange(vector, 255, result);
  2883. };
  2884. /**
  2885. * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
  2886. *
  2887. * @param {Number} x The x component of the oct-encoded unit length vector.
  2888. * @param {Number} y The y component of the oct-encoded unit length vector.
  2889. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  2890. * @param {Cartesian3} result The decoded and normalized vector
  2891. * @returns {Cartesian3} The decoded and normalized vector.
  2892. *
  2893. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and rangeMax.
  2894. *
  2895. * @see AttributeCompression.octEncodeInRange
  2896. */
  2897. AttributeCompression.octDecodeInRange = function(x, y, rangeMax, result) {
  2898. if (!defined(result)) {
  2899. throw new DeveloperError('result is required.');
  2900. }
  2901. if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) {
  2902. throw new DeveloperError('x and y must be a signed normalized integer between 0 and ' + rangeMax);
  2903. }
  2904. result.x = CesiumMath.fromSNorm(x, rangeMax);
  2905. result.y = CesiumMath.fromSNorm(y, rangeMax);
  2906. result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
  2907. if (result.z < 0.0)
  2908. {
  2909. var oldVX = result.x;
  2910. result.x = (1.0 - Math.abs(result.y)) * CesiumMath.signNotZero(oldVX);
  2911. result.y = (1.0 - Math.abs(oldVX)) * CesiumMath.signNotZero(result.y);
  2912. }
  2913. return Cartesian3.normalize(result, result);
  2914. };
  2915. /**
  2916. * Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
  2917. *
  2918. * @param {Number} x The x component of the oct-encoded unit length vector.
  2919. * @param {Number} y The y component of the oct-encoded unit length vector.
  2920. * @param {Cartesian3} result The decoded and normalized vector.
  2921. * @returns {Cartesian3} The decoded and normalized vector.
  2922. *
  2923. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and 255.
  2924. *
  2925. * @see AttributeCompression.octDecodeInRange
  2926. */
  2927. AttributeCompression.octDecode = function(x, y, result) {
  2928. return AttributeCompression.octDecodeInRange(x, y, 255, result);
  2929. };
  2930. /**
  2931. * Packs an oct encoded vector into a single floating-point number.
  2932. *
  2933. * @param {Cartesian2} encoded The oct encoded vector.
  2934. * @returns {Number} The oct encoded vector packed into a single float.
  2935. *
  2936. */
  2937. AttributeCompression.octPackFloat = function(encoded) {
  2938. if (!defined(encoded)) {
  2939. throw new DeveloperError('encoded is required.');
  2940. }
  2941. return 256.0 * encoded.x + encoded.y;
  2942. };
  2943. var scratchEncodeCart2 = new Cartesian2();
  2944. /**
  2945. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding and
  2946. * stores those values in a single float-point number.
  2947. *
  2948. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  2949. * @returns {Number} The 2 byte oct-encoded unit length vector.
  2950. *
  2951. * @exception {DeveloperError} vector must be normalized.
  2952. */
  2953. AttributeCompression.octEncodeFloat = function(vector) {
  2954. AttributeCompression.octEncode(vector, scratchEncodeCart2);
  2955. return AttributeCompression.octPackFloat(scratchEncodeCart2);
  2956. };
  2957. /**
  2958. * Decodes a unit-length vector in 'oct' encoding packed in a floating-point number to a normalized 3-component vector.
  2959. *
  2960. * @param {Number} value The oct-encoded unit length vector stored as a single floating-point number.
  2961. * @param {Cartesian3} result The decoded and normalized vector
  2962. * @returns {Cartesian3} The decoded and normalized vector.
  2963. *
  2964. */
  2965. AttributeCompression.octDecodeFloat = function(value, result) {
  2966. if (!defined(value)) {
  2967. throw new DeveloperError('value is required.');
  2968. }
  2969. var temp = value / 256.0;
  2970. var x = Math.floor(temp);
  2971. var y = (temp - x) * 256.0;
  2972. return AttributeCompression.octDecode(x, y, result);
  2973. };
  2974. /**
  2975. * Encodes three normalized vectors into 6 SNORM values in the range of [0-255] following the 'oct' encoding and
  2976. * packs those into two floating-point numbers.
  2977. *
  2978. * @param {Cartesian3} v1 A normalized vector to be compressed.
  2979. * @param {Cartesian3} v2 A normalized vector to be compressed.
  2980. * @param {Cartesian3} v3 A normalized vector to be compressed.
  2981. * @param {Cartesian2} result The 'oct' encoded vectors packed into two floating-point numbers.
  2982. * @returns {Cartesian2} The 'oct' encoded vectors packed into two floating-point numbers.
  2983. *
  2984. */
  2985. AttributeCompression.octPack = function(v1, v2, v3, result) {
  2986. if (!defined(v1)) {
  2987. throw new DeveloperError('v1 is required.');
  2988. }
  2989. if (!defined(v2)) {
  2990. throw new DeveloperError('v2 is required.');
  2991. }
  2992. if (!defined(v3)) {
  2993. throw new DeveloperError('v3 is required.');
  2994. }
  2995. if (!defined(result)) {
  2996. throw new DeveloperError('result is required.');
  2997. }
  2998. var encoded1 = AttributeCompression.octEncodeFloat(v1);
  2999. var encoded2 = AttributeCompression.octEncodeFloat(v2);
  3000. var encoded3 = AttributeCompression.octEncode(v3, scratchEncodeCart2);
  3001. result.x = 65536.0 * encoded3.x + encoded1;
  3002. result.y = 65536.0 * encoded3.y + encoded2;
  3003. return result;
  3004. };
  3005. /**
  3006. * Decodes three unit-length vectors in 'oct' encoding packed into a floating-point number to a normalized 3-component vector.
  3007. *
  3008. * @param {Cartesian2} packed The three oct-encoded unit length vectors stored as two floating-point number.
  3009. * @param {Cartesian3} v1 One decoded and normalized vector.
  3010. * @param {Cartesian3} v2 One decoded and normalized vector.
  3011. * @param {Cartesian3} v3 One decoded and normalized vector.
  3012. */
  3013. AttributeCompression.octUnpack = function(packed, v1, v2, v3) {
  3014. if (!defined(packed)) {
  3015. throw new DeveloperError('packed is required.');
  3016. }
  3017. if (!defined(v1)) {
  3018. throw new DeveloperError('v1 is required.');
  3019. }
  3020. if (!defined(v2)) {
  3021. throw new DeveloperError('v2 is required.');
  3022. }
  3023. if (!defined(v3)) {
  3024. throw new DeveloperError('v3 is required.');
  3025. }
  3026. var temp = packed.x / 65536.0;
  3027. var x = Math.floor(temp);
  3028. var encodedFloat1 = (temp - x) * 65536.0;
  3029. temp = packed.y / 65536.0;
  3030. var y = Math.floor(temp);
  3031. var encodedFloat2 = (temp - y) * 65536.0;
  3032. AttributeCompression.octDecodeFloat(encodedFloat1, v1);
  3033. AttributeCompression.octDecodeFloat(encodedFloat2, v2);
  3034. AttributeCompression.octDecode(x, y, v3);
  3035. };
  3036. /**
  3037. * Pack texture coordinates into a single float. The texture coordinates will only preserve 12 bits of precision.
  3038. *
  3039. * @param {Cartesian2} textureCoordinates The texture coordinates to compress. Both coordinates must be in the range 0.0-1.0.
  3040. * @returns {Number} The packed texture coordinates.
  3041. *
  3042. */
  3043. AttributeCompression.compressTextureCoordinates = function(textureCoordinates) {
  3044. if (!defined(textureCoordinates)) {
  3045. throw new DeveloperError('textureCoordinates is required.');
  3046. }
  3047. // Move x and y to the range 0-4095;
  3048. var x = (textureCoordinates.x * 4095.0) | 0;
  3049. var y = (textureCoordinates.y * 4095.0) | 0;
  3050. return 4096.0 * x + y;
  3051. };
  3052. /**
  3053. * Decompresses texture coordinates that were packed into a single float.
  3054. *
  3055. * @param {Number} compressed The compressed texture coordinates.
  3056. * @param {Cartesian2} result The decompressed texture coordinates.
  3057. * @returns {Cartesian2} The modified result parameter.
  3058. *
  3059. */
  3060. AttributeCompression.decompressTextureCoordinates = function(compressed, result) {
  3061. if (!defined(compressed)) {
  3062. throw new DeveloperError('compressed is required.');
  3063. }
  3064. if (!defined(result)) {
  3065. throw new DeveloperError('result is required.');
  3066. }
  3067. var temp = compressed / 4096.0;
  3068. var xZeroTo4095 = Math.floor(temp);
  3069. result.x = xZeroTo4095 / 4095.0;
  3070. result.y = (compressed - xZeroTo4095 * 4096) / 4095;
  3071. return result;
  3072. };
  3073. return AttributeCompression;
  3074. });
  3075. /*global define*/
  3076. define('Core/scaleToGeodeticSurface',[
  3077. './Cartesian3',
  3078. './defined',
  3079. './DeveloperError',
  3080. './Math'
  3081. ], function(
  3082. Cartesian3,
  3083. defined,
  3084. DeveloperError,
  3085. CesiumMath) {
  3086. 'use strict';
  3087. var scaleToGeodeticSurfaceIntersection = new Cartesian3();
  3088. var scaleToGeodeticSurfaceGradient = new Cartesian3();
  3089. /**
  3090. * Scales the provided Cartesian position along the geodetic surface normal
  3091. * so that it is on the surface of this ellipsoid. If the position is
  3092. * at the center of the ellipsoid, this function returns undefined.
  3093. *
  3094. * @param {Cartesian3} cartesian The Cartesian position to scale.
  3095. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  3096. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  3097. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  3098. * @param {Cartesian3} [result] The object onto which to store the result.
  3099. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  3100. *
  3101. * @exports scaleToGeodeticSurface
  3102. *
  3103. * @private
  3104. */
  3105. function scaleToGeodeticSurface(cartesian, oneOverRadii, oneOverRadiiSquared, centerToleranceSquared, result) {
  3106. if (!defined(cartesian)) {
  3107. throw new DeveloperError('cartesian is required.');
  3108. }
  3109. if (!defined(oneOverRadii)) {
  3110. throw new DeveloperError('oneOverRadii is required.');
  3111. }
  3112. if (!defined(oneOverRadiiSquared)) {
  3113. throw new DeveloperError('oneOverRadiiSquared is required.');
  3114. }
  3115. if (!defined(centerToleranceSquared)) {
  3116. throw new DeveloperError('centerToleranceSquared is required.');
  3117. }
  3118. var positionX = cartesian.x;
  3119. var positionY = cartesian.y;
  3120. var positionZ = cartesian.z;
  3121. var oneOverRadiiX = oneOverRadii.x;
  3122. var oneOverRadiiY = oneOverRadii.y;
  3123. var oneOverRadiiZ = oneOverRadii.z;
  3124. var x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  3125. var y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  3126. var z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  3127. // Compute the squared ellipsoid norm.
  3128. var squaredNorm = x2 + y2 + z2;
  3129. var ratio = Math.sqrt(1.0 / squaredNorm);
  3130. // As an initial approximation, assume that the radial intersection is the projection point.
  3131. var intersection = Cartesian3.multiplyByScalar(cartesian, ratio, scaleToGeodeticSurfaceIntersection);
  3132. // If the position is near the center, the iteration will not converge.
  3133. if (squaredNorm < centerToleranceSquared) {
  3134. return !isFinite(ratio) ? undefined : Cartesian3.clone(intersection, result);
  3135. }
  3136. var oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  3137. var oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  3138. var oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  3139. // Use the gradient at the intersection point in place of the true unit normal.
  3140. // The difference in magnitude will be absorbed in the multiplier.
  3141. var gradient = scaleToGeodeticSurfaceGradient;
  3142. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  3143. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  3144. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  3145. // Compute the initial guess at the normal vector multiplier, lambda.
  3146. var lambda = (1.0 - ratio) * Cartesian3.magnitude(cartesian) / (0.5 * Cartesian3.magnitude(gradient));
  3147. var correction = 0.0;
  3148. var func;
  3149. var denominator;
  3150. var xMultiplier;
  3151. var yMultiplier;
  3152. var zMultiplier;
  3153. var xMultiplier2;
  3154. var yMultiplier2;
  3155. var zMultiplier2;
  3156. var xMultiplier3;
  3157. var yMultiplier3;
  3158. var zMultiplier3;
  3159. do {
  3160. lambda -= correction;
  3161. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  3162. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  3163. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  3164. xMultiplier2 = xMultiplier * xMultiplier;
  3165. yMultiplier2 = yMultiplier * yMultiplier;
  3166. zMultiplier2 = zMultiplier * zMultiplier;
  3167. xMultiplier3 = xMultiplier2 * xMultiplier;
  3168. yMultiplier3 = yMultiplier2 * yMultiplier;
  3169. zMultiplier3 = zMultiplier2 * zMultiplier;
  3170. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  3171. // "denominator" here refers to the use of this expression in the velocity and acceleration
  3172. // computations in the sections to follow.
  3173. denominator = x2 * xMultiplier3 * oneOverRadiiSquaredX + y2 * yMultiplier3 * oneOverRadiiSquaredY + z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  3174. var derivative = -2.0 * denominator;
  3175. correction = func / derivative;
  3176. } while (Math.abs(func) > CesiumMath.EPSILON12);
  3177. if (!defined(result)) {
  3178. return new Cartesian3(positionX * xMultiplier, positionY * yMultiplier, positionZ * zMultiplier);
  3179. }
  3180. result.x = positionX * xMultiplier;
  3181. result.y = positionY * yMultiplier;
  3182. result.z = positionZ * zMultiplier;
  3183. return result;
  3184. }
  3185. return scaleToGeodeticSurface;
  3186. });
  3187. /*global define*/
  3188. define('Core/Cartographic',[
  3189. './Cartesian3',
  3190. './defaultValue',
  3191. './defined',
  3192. './DeveloperError',
  3193. './freezeObject',
  3194. './Math',
  3195. './scaleToGeodeticSurface'
  3196. ], function(
  3197. Cartesian3,
  3198. defaultValue,
  3199. defined,
  3200. DeveloperError,
  3201. freezeObject,
  3202. CesiumMath,
  3203. scaleToGeodeticSurface) {
  3204. 'use strict';
  3205. /**
  3206. * A position defined by longitude, latitude, and height.
  3207. * @alias Cartographic
  3208. * @constructor
  3209. *
  3210. * @param {Number} [longitude=0.0] The longitude, in radians.
  3211. * @param {Number} [latitude=0.0] The latitude, in radians.
  3212. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  3213. *
  3214. * @see Ellipsoid
  3215. */
  3216. function Cartographic(longitude, latitude, height) {
  3217. /**
  3218. * The longitude, in radians.
  3219. * @type {Number}
  3220. * @default 0.0
  3221. */
  3222. this.longitude = defaultValue(longitude, 0.0);
  3223. /**
  3224. * The latitude, in radians.
  3225. * @type {Number}
  3226. * @default 0.0
  3227. */
  3228. this.latitude = defaultValue(latitude, 0.0);
  3229. /**
  3230. * The height, in meters, above the ellipsoid.
  3231. * @type {Number}
  3232. * @default 0.0
  3233. */
  3234. this.height = defaultValue(height, 0.0);
  3235. }
  3236. /**
  3237. * Creates a new Cartographic instance from longitude and latitude
  3238. * specified in radians.
  3239. *
  3240. * @param {Number} longitude The longitude, in radians.
  3241. * @param {Number} latitude The latitude, in radians.
  3242. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  3243. * @param {Cartographic} [result] The object onto which to store the result.
  3244. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  3245. */
  3246. Cartographic.fromRadians = function(longitude, latitude, height, result) {
  3247. if (!defined(longitude)) {
  3248. throw new DeveloperError('longitude is required.');
  3249. }
  3250. if (!defined(latitude)) {
  3251. throw new DeveloperError('latitude is required.');
  3252. }
  3253. height = defaultValue(height, 0.0);
  3254. if (!defined(result)) {
  3255. return new Cartographic(longitude, latitude, height);
  3256. }
  3257. result.longitude = longitude;
  3258. result.latitude = latitude;
  3259. result.height = height;
  3260. return result;
  3261. };
  3262. /**
  3263. * Creates a new Cartographic instance from longitude and latitude
  3264. * specified in degrees. The values in the resulting object will
  3265. * be in radians.
  3266. *
  3267. * @param {Number} longitude The longitude, in degrees.
  3268. * @param {Number} latitude The latitude, in degrees.
  3269. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  3270. * @param {Cartographic} [result] The object onto which to store the result.
  3271. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  3272. */
  3273. Cartographic.fromDegrees = function(longitude, latitude, height, result) {
  3274. if (!defined(longitude)) {
  3275. throw new DeveloperError('longitude is required.');
  3276. }
  3277. if (!defined(latitude)) {
  3278. throw new DeveloperError('latitude is required.');
  3279. }
  3280. longitude = CesiumMath.toRadians(longitude);
  3281. latitude = CesiumMath.toRadians(latitude);
  3282. return Cartographic.fromRadians(longitude, latitude, height, result);
  3283. };
  3284. var cartesianToCartographicN = new Cartesian3();
  3285. var cartesianToCartographicP = new Cartesian3();
  3286. var cartesianToCartographicH = new Cartesian3();
  3287. var wgs84OneOverRadii = new Cartesian3(1.0 / 6378137.0, 1.0 / 6378137.0, 1.0 / 6356752.3142451793);
  3288. var wgs84OneOverRadiiSquared = new Cartesian3(1.0 / (6378137.0 * 6378137.0), 1.0 / (6378137.0 * 6378137.0), 1.0 / (6356752.3142451793 * 6356752.3142451793));
  3289. var wgs84CenterToleranceSquared = CesiumMath.EPSILON1;
  3290. /**
  3291. * Creates a new Cartographic instance from a Cartesian position. The values in the
  3292. * resulting object will be in radians.
  3293. *
  3294. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  3295. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  3296. * @param {Cartographic} [result] The object onto which to store the result.
  3297. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  3298. */
  3299. Cartographic.fromCartesian = function(cartesian, ellipsoid, result) {
  3300. var oneOverRadii = defined(ellipsoid) ? ellipsoid.oneOverRadii : wgs84OneOverRadii;
  3301. var oneOverRadiiSquared = defined(ellipsoid) ? ellipsoid.oneOverRadiiSquared : wgs84OneOverRadiiSquared;
  3302. var centerToleranceSquared = defined(ellipsoid) ? ellipsoid._centerToleranceSquared : wgs84CenterToleranceSquared;
  3303. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  3304. var p = scaleToGeodeticSurface(cartesian, oneOverRadii, oneOverRadiiSquared, centerToleranceSquared, cartesianToCartographicP);
  3305. if (!defined(p)) {
  3306. return undefined;
  3307. }
  3308. var n = Cartesian3.multiplyComponents(p, oneOverRadiiSquared, cartesianToCartographicN);
  3309. n = Cartesian3.normalize(n, n);
  3310. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  3311. var longitude = Math.atan2(n.y, n.x);
  3312. var latitude = Math.asin(n.z);
  3313. var height = CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  3314. if (!defined(result)) {
  3315. return new Cartographic(longitude, latitude, height);
  3316. }
  3317. result.longitude = longitude;
  3318. result.latitude = latitude;
  3319. result.height = height;
  3320. return result;
  3321. };
  3322. /**
  3323. * Duplicates a Cartographic instance.
  3324. *
  3325. * @param {Cartographic} cartographic The cartographic to duplicate.
  3326. * @param {Cartographic} [result] The object onto which to store the result.
  3327. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  3328. */
  3329. Cartographic.clone = function(cartographic, result) {
  3330. if (!defined(cartographic)) {
  3331. return undefined;
  3332. }
  3333. if (!defined(result)) {
  3334. return new Cartographic(cartographic.longitude, cartographic.latitude, cartographic.height);
  3335. }
  3336. result.longitude = cartographic.longitude;
  3337. result.latitude = cartographic.latitude;
  3338. result.height = cartographic.height;
  3339. return result;
  3340. };
  3341. /**
  3342. * Compares the provided cartographics componentwise and returns
  3343. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3344. *
  3345. * @param {Cartographic} [left] The first cartographic.
  3346. * @param {Cartographic} [right] The second cartographic.
  3347. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3348. */
  3349. Cartographic.equals = function(left, right) {
  3350. return (left === right) ||
  3351. ((defined(left)) &&
  3352. (defined(right)) &&
  3353. (left.longitude === right.longitude) &&
  3354. (left.latitude === right.latitude) &&
  3355. (left.height === right.height));
  3356. };
  3357. /**
  3358. * Compares the provided cartographics componentwise and returns
  3359. * <code>true</code> if they are within the provided epsilon,
  3360. * <code>false</code> otherwise.
  3361. *
  3362. * @param {Cartographic} [left] The first cartographic.
  3363. * @param {Cartographic} [right] The second cartographic.
  3364. * @param {Number} epsilon The epsilon to use for equality testing.
  3365. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3366. */
  3367. Cartographic.equalsEpsilon = function(left, right, epsilon) {
  3368. if (typeof epsilon !== 'number') {
  3369. throw new DeveloperError('epsilon is required and must be a number.');
  3370. }
  3371. return (left === right) ||
  3372. ((defined(left)) &&
  3373. (defined(right)) &&
  3374. (Math.abs(left.longitude - right.longitude) <= epsilon) &&
  3375. (Math.abs(left.latitude - right.latitude) <= epsilon) &&
  3376. (Math.abs(left.height - right.height) <= epsilon));
  3377. };
  3378. /**
  3379. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  3380. *
  3381. * @type {Cartographic}
  3382. * @constant
  3383. */
  3384. Cartographic.ZERO = freezeObject(new Cartographic(0.0, 0.0, 0.0));
  3385. /**
  3386. * Duplicates this instance.
  3387. *
  3388. * @param {Cartographic} [result] The object onto which to store the result.
  3389. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  3390. */
  3391. Cartographic.prototype.clone = function(result) {
  3392. return Cartographic.clone(this, result);
  3393. };
  3394. /**
  3395. * Compares the provided against this cartographic componentwise and returns
  3396. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3397. *
  3398. * @param {Cartographic} [right] The second cartographic.
  3399. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3400. */
  3401. Cartographic.prototype.equals = function(right) {
  3402. return Cartographic.equals(this, right);
  3403. };
  3404. /**
  3405. * Compares the provided against this cartographic componentwise and returns
  3406. * <code>true</code> if they are within the provided epsilon,
  3407. * <code>false</code> otherwise.
  3408. *
  3409. * @param {Cartographic} [right] The second cartographic.
  3410. * @param {Number} epsilon The epsilon to use for equality testing.
  3411. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3412. */
  3413. Cartographic.prototype.equalsEpsilon = function(right, epsilon) {
  3414. return Cartographic.equalsEpsilon(this, right, epsilon);
  3415. };
  3416. /**
  3417. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  3418. *
  3419. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  3420. */
  3421. Cartographic.prototype.toString = function() {
  3422. return '(' + this.longitude + ', ' + this.latitude + ', ' + this.height + ')';
  3423. };
  3424. return Cartographic;
  3425. });
  3426. /*global define*/
  3427. define('Core/defineProperties',[
  3428. './defined'
  3429. ], function(
  3430. defined) {
  3431. 'use strict';
  3432. var definePropertyWorks = (function() {
  3433. try {
  3434. return 'x' in Object.defineProperty({}, 'x', {});
  3435. } catch (e) {
  3436. return false;
  3437. }
  3438. })();
  3439. /**
  3440. * Defines properties on an object, using Object.defineProperties if available,
  3441. * otherwise returns the object unchanged. This function should be used in
  3442. * setup code to prevent errors from completely halting JavaScript execution
  3443. * in legacy browsers.
  3444. *
  3445. * @private
  3446. *
  3447. * @exports defineProperties
  3448. */
  3449. var defineProperties = Object.defineProperties;
  3450. if (!definePropertyWorks || !defined(defineProperties)) {
  3451. defineProperties = function(o) {
  3452. return o;
  3453. };
  3454. }
  3455. return defineProperties;
  3456. });
  3457. /*global define*/
  3458. define('Core/Ellipsoid',[
  3459. './Cartesian3',
  3460. './Cartographic',
  3461. './defaultValue',
  3462. './defined',
  3463. './defineProperties',
  3464. './DeveloperError',
  3465. './freezeObject',
  3466. './Math',
  3467. './scaleToGeodeticSurface'
  3468. ], function(
  3469. Cartesian3,
  3470. Cartographic,
  3471. defaultValue,
  3472. defined,
  3473. defineProperties,
  3474. DeveloperError,
  3475. freezeObject,
  3476. CesiumMath,
  3477. scaleToGeodeticSurface) {
  3478. 'use strict';
  3479. function initialize(ellipsoid, x, y, z) {
  3480. x = defaultValue(x, 0.0);
  3481. y = defaultValue(y, 0.0);
  3482. z = defaultValue(z, 0.0);
  3483. if (x < 0.0 || y < 0.0 || z < 0.0) {
  3484. throw new DeveloperError('All radii components must be greater than or equal to zero.');
  3485. }
  3486. ellipsoid._radii = new Cartesian3(x, y, z);
  3487. ellipsoid._radiiSquared = new Cartesian3(x * x,
  3488. y * y,
  3489. z * z);
  3490. ellipsoid._radiiToTheFourth = new Cartesian3(x * x * x * x,
  3491. y * y * y * y,
  3492. z * z * z * z);
  3493. ellipsoid._oneOverRadii = new Cartesian3(x === 0.0 ? 0.0 : 1.0 / x,
  3494. y === 0.0 ? 0.0 : 1.0 / y,
  3495. z === 0.0 ? 0.0 : 1.0 / z);
  3496. ellipsoid._oneOverRadiiSquared = new Cartesian3(x === 0.0 ? 0.0 : 1.0 / (x * x),
  3497. y === 0.0 ? 0.0 : 1.0 / (y * y),
  3498. z === 0.0 ? 0.0 : 1.0 / (z * z));
  3499. ellipsoid._minimumRadius = Math.min(x, y, z);
  3500. ellipsoid._maximumRadius = Math.max(x, y, z);
  3501. ellipsoid._centerToleranceSquared = CesiumMath.EPSILON1;
  3502. if (ellipsoid._radiiSquared.z !== 0) {
  3503. ellipsoid._sqauredXOverSquaredZ = ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  3504. }
  3505. }
  3506. /**
  3507. * A quadratic surface defined in Cartesian coordinates by the equation
  3508. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  3509. * by Cesium to represent the shape of planetary bodies.
  3510. *
  3511. * Rather than constructing this object directly, one of the provided
  3512. * constants is normally used.
  3513. * @alias Ellipsoid
  3514. * @constructor
  3515. *
  3516. * @param {Number} [x=0] The radius in the x direction.
  3517. * @param {Number} [y=0] The radius in the y direction.
  3518. * @param {Number} [z=0] The radius in the z direction.
  3519. *
  3520. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  3521. *
  3522. * @see Ellipsoid.fromCartesian3
  3523. * @see Ellipsoid.WGS84
  3524. * @see Ellipsoid.UNIT_SPHERE
  3525. */
  3526. function Ellipsoid(x, y, z) {
  3527. this._radii = undefined;
  3528. this._radiiSquared = undefined;
  3529. this._radiiToTheFourth = undefined;
  3530. this._oneOverRadii = undefined;
  3531. this._oneOverRadiiSquared = undefined;
  3532. this._minimumRadius = undefined;
  3533. this._maximumRadius = undefined;
  3534. this._centerToleranceSquared = undefined;
  3535. this._sqauredXOverSquaredZ = undefined;
  3536. initialize(this, x, y, z);
  3537. }
  3538. defineProperties(Ellipsoid.prototype, {
  3539. /**
  3540. * Gets the radii of the ellipsoid.
  3541. * @memberof Ellipsoid.prototype
  3542. * @type {Cartesian3}
  3543. * @readonly
  3544. */
  3545. radii : {
  3546. get: function() {
  3547. return this._radii;
  3548. }
  3549. },
  3550. /**
  3551. * Gets the squared radii of the ellipsoid.
  3552. * @memberof Ellipsoid.prototype
  3553. * @type {Cartesian3}
  3554. * @readonly
  3555. */
  3556. radiiSquared : {
  3557. get : function() {
  3558. return this._radiiSquared;
  3559. }
  3560. },
  3561. /**
  3562. * Gets the radii of the ellipsoid raise to the fourth power.
  3563. * @memberof Ellipsoid.prototype
  3564. * @type {Cartesian3}
  3565. * @readonly
  3566. */
  3567. radiiToTheFourth : {
  3568. get : function() {
  3569. return this._radiiToTheFourth;
  3570. }
  3571. },
  3572. /**
  3573. * Gets one over the radii of the ellipsoid.
  3574. * @memberof Ellipsoid.prototype
  3575. * @type {Cartesian3}
  3576. * @readonly
  3577. */
  3578. oneOverRadii : {
  3579. get : function() {
  3580. return this._oneOverRadii;
  3581. }
  3582. },
  3583. /**
  3584. * Gets one over the squared radii of the ellipsoid.
  3585. * @memberof Ellipsoid.prototype
  3586. * @type {Cartesian3}
  3587. * @readonly
  3588. */
  3589. oneOverRadiiSquared : {
  3590. get : function() {
  3591. return this._oneOverRadiiSquared;
  3592. }
  3593. },
  3594. /**
  3595. * Gets the minimum radius of the ellipsoid.
  3596. * @memberof Ellipsoid.prototype
  3597. * @type {Number}
  3598. * @readonly
  3599. */
  3600. minimumRadius : {
  3601. get : function() {
  3602. return this._minimumRadius;
  3603. }
  3604. },
  3605. /**
  3606. * Gets the maximum radius of the ellipsoid.
  3607. * @memberof Ellipsoid.prototype
  3608. * @type {Number}
  3609. * @readonly
  3610. */
  3611. maximumRadius : {
  3612. get : function() {
  3613. return this._maximumRadius;
  3614. }
  3615. }
  3616. });
  3617. /**
  3618. * Duplicates an Ellipsoid instance.
  3619. *
  3620. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  3621. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  3622. * instance should be created.
  3623. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  3624. */
  3625. Ellipsoid.clone = function(ellipsoid, result) {
  3626. if (!defined(ellipsoid)) {
  3627. return undefined;
  3628. }
  3629. var radii = ellipsoid._radii;
  3630. if (!defined(result)) {
  3631. return new Ellipsoid(radii.x, radii.y, radii.z);
  3632. }
  3633. Cartesian3.clone(radii, result._radii);
  3634. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  3635. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  3636. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  3637. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  3638. result._minimumRadius = ellipsoid._minimumRadius;
  3639. result._maximumRadius = ellipsoid._maximumRadius;
  3640. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  3641. return result;
  3642. };
  3643. /**
  3644. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  3645. *
  3646. * @param {Cartesian3} [radii=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  3647. * @returns {Ellipsoid} A new Ellipsoid instance.
  3648. *
  3649. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  3650. *
  3651. * @see Ellipsoid.WGS84
  3652. * @see Ellipsoid.UNIT_SPHERE
  3653. */
  3654. Ellipsoid.fromCartesian3 = function(cartesian, result) {
  3655. if (!defined(result)) {
  3656. result = new Ellipsoid();
  3657. }
  3658. if (!defined(cartesian)) {
  3659. return result;
  3660. }
  3661. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  3662. return result;
  3663. };
  3664. /**
  3665. * An Ellipsoid instance initialized to the WGS84 standard.
  3666. *
  3667. * @type {Ellipsoid}
  3668. * @constant
  3669. */
  3670. Ellipsoid.WGS84 = freezeObject(new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793));
  3671. /**
  3672. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  3673. *
  3674. * @type {Ellipsoid}
  3675. * @constant
  3676. */
  3677. Ellipsoid.UNIT_SPHERE = freezeObject(new Ellipsoid(1.0, 1.0, 1.0));
  3678. /**
  3679. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  3680. *
  3681. * @type {Ellipsoid}
  3682. * @constant
  3683. */
  3684. Ellipsoid.MOON = freezeObject(new Ellipsoid(CesiumMath.LUNAR_RADIUS, CesiumMath.LUNAR_RADIUS, CesiumMath.LUNAR_RADIUS));
  3685. /**
  3686. * Duplicates an Ellipsoid instance.
  3687. *
  3688. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  3689. * instance should be created.
  3690. * @returns {Ellipsoid} The cloned Ellipsoid.
  3691. */
  3692. Ellipsoid.prototype.clone = function(result) {
  3693. return Ellipsoid.clone(this, result);
  3694. };
  3695. /**
  3696. * The number of elements used to pack the object into an array.
  3697. * @type {Number}
  3698. */
  3699. Ellipsoid.packedLength = Cartesian3.packedLength;
  3700. /**
  3701. * Stores the provided instance into the provided array.
  3702. *
  3703. * @param {Ellipsoid} value The value to pack.
  3704. * @param {Number[]} array The array to pack into.
  3705. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3706. *
  3707. * @returns {Number[]} The array that was packed into
  3708. */
  3709. Ellipsoid.pack = function(value, array, startingIndex) {
  3710. if (!defined(value)) {
  3711. throw new DeveloperError('value is required');
  3712. }
  3713. if (!defined(array)) {
  3714. throw new DeveloperError('array is required');
  3715. }
  3716. startingIndex = defaultValue(startingIndex, 0);
  3717. Cartesian3.pack(value._radii, array, startingIndex);
  3718. return array;
  3719. };
  3720. /**
  3721. * Retrieves an instance from a packed array.
  3722. *
  3723. * @param {Number[]} array The packed array.
  3724. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3725. * @param {Ellipsoid} [result] The object into which to store the result.
  3726. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  3727. */
  3728. Ellipsoid.unpack = function(array, startingIndex, result) {
  3729. if (!defined(array)) {
  3730. throw new DeveloperError('array is required');
  3731. }
  3732. startingIndex = defaultValue(startingIndex, 0);
  3733. var radii = Cartesian3.unpack(array, startingIndex);
  3734. return Ellipsoid.fromCartesian3(radii, result);
  3735. };
  3736. /**
  3737. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  3738. * @function
  3739. *
  3740. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  3741. * @param {Cartesian3} [result] The object onto which to store the result.
  3742. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  3743. */
  3744. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  3745. /**
  3746. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  3747. *
  3748. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  3749. * @param {Cartesian3} [result] The object onto which to store the result.
  3750. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  3751. */
  3752. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function(cartographic, result) {
  3753. if (!defined(cartographic)) {
  3754. throw new DeveloperError('cartographic is required.');
  3755. }
  3756. var longitude = cartographic.longitude;
  3757. var latitude = cartographic.latitude;
  3758. var cosLatitude = Math.cos(latitude);
  3759. var x = cosLatitude * Math.cos(longitude);
  3760. var y = cosLatitude * Math.sin(longitude);
  3761. var z = Math.sin(latitude);
  3762. if (!defined(result)) {
  3763. result = new Cartesian3();
  3764. }
  3765. result.x = x;
  3766. result.y = y;
  3767. result.z = z;
  3768. return Cartesian3.normalize(result, result);
  3769. };
  3770. /**
  3771. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  3772. *
  3773. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  3774. * @param {Cartesian3} [result] The object onto which to store the result.
  3775. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  3776. */
  3777. Ellipsoid.prototype.geodeticSurfaceNormal = function(cartesian, result) {
  3778. if (!defined(result)) {
  3779. result = new Cartesian3();
  3780. }
  3781. result = Cartesian3.multiplyComponents(cartesian, this._oneOverRadiiSquared, result);
  3782. return Cartesian3.normalize(result, result);
  3783. };
  3784. var cartographicToCartesianNormal = new Cartesian3();
  3785. var cartographicToCartesianK = new Cartesian3();
  3786. /**
  3787. * Converts the provided cartographic to Cartesian representation.
  3788. *
  3789. * @param {Cartographic} cartographic The cartographic position.
  3790. * @param {Cartesian3} [result] The object onto which to store the result.
  3791. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  3792. *
  3793. * @example
  3794. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  3795. * var position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  3796. * var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  3797. */
  3798. Ellipsoid.prototype.cartographicToCartesian = function(cartographic, result) {
  3799. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  3800. var n = cartographicToCartesianNormal;
  3801. var k = cartographicToCartesianK;
  3802. this.geodeticSurfaceNormalCartographic(cartographic, n);
  3803. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  3804. var gamma = Math.sqrt(Cartesian3.dot(n, k));
  3805. Cartesian3.divideByScalar(k, gamma, k);
  3806. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  3807. if (!defined(result)) {
  3808. result = new Cartesian3();
  3809. }
  3810. return Cartesian3.add(k, n, result);
  3811. };
  3812. /**
  3813. * Converts the provided array of cartographics to an array of Cartesians.
  3814. *
  3815. * @param {Cartographic[]} cartographics An array of cartographic positions.
  3816. * @param {Cartesian3[]} [result] The object onto which to store the result.
  3817. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  3818. *
  3819. * @example
  3820. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  3821. * var positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  3822. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  3823. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  3824. * var cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  3825. */
  3826. Ellipsoid.prototype.cartographicArrayToCartesianArray = function(cartographics, result) {
  3827. if (!defined(cartographics)) {
  3828. throw new DeveloperError('cartographics is required.');
  3829. }
  3830. var length = cartographics.length;
  3831. if (!defined(result)) {
  3832. result = new Array(length);
  3833. } else {
  3834. result.length = length;
  3835. }
  3836. for ( var i = 0; i < length; i++) {
  3837. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  3838. }
  3839. return result;
  3840. };
  3841. var cartesianToCartographicN = new Cartesian3();
  3842. var cartesianToCartographicP = new Cartesian3();
  3843. var cartesianToCartographicH = new Cartesian3();
  3844. /**
  3845. * Converts the provided cartesian to cartographic representation.
  3846. * The cartesian is undefined at the center of the ellipsoid.
  3847. *
  3848. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  3849. * @param {Cartographic} [result] The object onto which to store the result.
  3850. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  3851. *
  3852. * @example
  3853. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  3854. * var position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  3855. * var cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  3856. */
  3857. Ellipsoid.prototype.cartesianToCartographic = function(cartesian, result) {
  3858. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  3859. var p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  3860. if (!defined(p)) {
  3861. return undefined;
  3862. }
  3863. var n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  3864. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  3865. var longitude = Math.atan2(n.y, n.x);
  3866. var latitude = Math.asin(n.z);
  3867. var height = CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  3868. if (!defined(result)) {
  3869. return new Cartographic(longitude, latitude, height);
  3870. }
  3871. result.longitude = longitude;
  3872. result.latitude = latitude;
  3873. result.height = height;
  3874. return result;
  3875. };
  3876. /**
  3877. * Converts the provided array of cartesians to an array of cartographics.
  3878. *
  3879. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  3880. * @param {Cartographic[]} [result] The object onto which to store the result.
  3881. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  3882. *
  3883. * @example
  3884. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  3885. * var positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  3886. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  3887. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  3888. * var cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  3889. */
  3890. Ellipsoid.prototype.cartesianArrayToCartographicArray = function(cartesians, result) {
  3891. if (!defined(cartesians)) {
  3892. throw new DeveloperError('cartesians is required.');
  3893. }
  3894. var length = cartesians.length;
  3895. if (!defined(result)) {
  3896. result = new Array(length);
  3897. } else {
  3898. result.length = length;
  3899. }
  3900. for ( var i = 0; i < length; ++i) {
  3901. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  3902. }
  3903. return result;
  3904. };
  3905. /**
  3906. * Scales the provided Cartesian position along the geodetic surface normal
  3907. * so that it is on the surface of this ellipsoid. If the position is
  3908. * at the center of the ellipsoid, this function returns undefined.
  3909. *
  3910. * @param {Cartesian3} cartesian The Cartesian position to scale.
  3911. * @param {Cartesian3} [result] The object onto which to store the result.
  3912. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  3913. */
  3914. Ellipsoid.prototype.scaleToGeodeticSurface = function(cartesian, result) {
  3915. return scaleToGeodeticSurface(cartesian, this._oneOverRadii, this._oneOverRadiiSquared, this._centerToleranceSquared, result);
  3916. };
  3917. /**
  3918. * Scales the provided Cartesian position along the geocentric surface normal
  3919. * so that it is on the surface of this ellipsoid.
  3920. *
  3921. * @param {Cartesian3} cartesian The Cartesian position to scale.
  3922. * @param {Cartesian3} [result] The object onto which to store the result.
  3923. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  3924. */
  3925. Ellipsoid.prototype.scaleToGeocentricSurface = function(cartesian, result) {
  3926. if (!defined(cartesian)) {
  3927. throw new DeveloperError('cartesian is required.');
  3928. }
  3929. if (!defined(result)) {
  3930. result = new Cartesian3();
  3931. }
  3932. var positionX = cartesian.x;
  3933. var positionY = cartesian.y;
  3934. var positionZ = cartesian.z;
  3935. var oneOverRadiiSquared = this._oneOverRadiiSquared;
  3936. var beta = 1.0 / Math.sqrt((positionX * positionX) * oneOverRadiiSquared.x +
  3937. (positionY * positionY) * oneOverRadiiSquared.y +
  3938. (positionZ * positionZ) * oneOverRadiiSquared.z);
  3939. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  3940. };
  3941. /**
  3942. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  3943. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  3944. *
  3945. * @param {Cartesian3} position The position to transform.
  3946. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  3947. * return a new instance.
  3948. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  3949. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  3950. */
  3951. Ellipsoid.prototype.transformPositionToScaledSpace = function(position, result) {
  3952. if (!defined(result)) {
  3953. result = new Cartesian3();
  3954. }
  3955. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  3956. };
  3957. /**
  3958. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  3959. * its components by the result of {@link Ellipsoid#radii}.
  3960. *
  3961. * @param {Cartesian3} position The position to transform.
  3962. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  3963. * return a new instance.
  3964. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  3965. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  3966. */
  3967. Ellipsoid.prototype.transformPositionFromScaledSpace = function(position, result) {
  3968. if (!defined(result)) {
  3969. result = new Cartesian3();
  3970. }
  3971. return Cartesian3.multiplyComponents(position, this._radii, result);
  3972. };
  3973. /**
  3974. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  3975. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3976. *
  3977. * @param {Ellipsoid} [right] The other Ellipsoid.
  3978. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3979. */
  3980. Ellipsoid.prototype.equals = function(right) {
  3981. return (this === right) ||
  3982. (defined(right) &&
  3983. Cartesian3.equals(this._radii, right._radii));
  3984. };
  3985. /**
  3986. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  3987. *
  3988. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  3989. */
  3990. Ellipsoid.prototype.toString = function() {
  3991. return this._radii.toString();
  3992. };
  3993. /**
  3994. * Computes a point which is the intersection of the surface normal with the z-axis.
  3995. *
  3996. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  3997. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  3998. * In earth case, with common earth datums, there is no need for this buffer since the intersection point is always (relatively) very close to the center.
  3999. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  4000. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  4001. * @param {Cartesian} [result] The cartesian to which to copy the result, or undefined to create and
  4002. * return a new instance.
  4003. * @returns {Cartesian | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  4004. *
  4005. * @exception {DeveloperError} position is required.
  4006. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  4007. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  4008. */
  4009. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function(position, buffer, result) {
  4010. if (!defined(position)) {
  4011. throw new DeveloperError('position is required.');
  4012. }
  4013. if (!CesiumMath.equalsEpsilon(this._radii.x, this._radii.y, CesiumMath.EPSILON15)) {
  4014. throw new DeveloperError('Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)');
  4015. }
  4016. if (this._radii.z === 0) {
  4017. throw new DeveloperError('Ellipsoid.radii.z must be greater than 0');
  4018. }
  4019. buffer = defaultValue(buffer, 0.0);
  4020. var sqauredXOverSquaredZ = this._sqauredXOverSquaredZ;
  4021. if (!defined(result)) {
  4022. result = new Cartesian3();
  4023. }
  4024. result.x = 0.0;
  4025. result.y = 0.0;
  4026. result.z = position.z * (1 - sqauredXOverSquaredZ);
  4027. if (Math.abs(result.z) >= this._radii.z - buffer) {
  4028. return undefined;
  4029. }
  4030. return result;
  4031. };
  4032. return Ellipsoid;
  4033. });
  4034. /*global define*/
  4035. define('Core/GeographicProjection',[
  4036. './Cartesian3',
  4037. './Cartographic',
  4038. './defaultValue',
  4039. './defined',
  4040. './defineProperties',
  4041. './DeveloperError',
  4042. './Ellipsoid'
  4043. ], function(
  4044. Cartesian3,
  4045. Cartographic,
  4046. defaultValue,
  4047. defined,
  4048. defineProperties,
  4049. DeveloperError,
  4050. Ellipsoid) {
  4051. 'use strict';
  4052. /**
  4053. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  4054. * them by the {@link Ellipsoid#maximumRadius}. This projection
  4055. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  4056. * is also known as EPSG:4326.
  4057. *
  4058. * @alias GeographicProjection
  4059. * @constructor
  4060. *
  4061. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  4062. *
  4063. * @see WebMercatorProjection
  4064. */
  4065. function GeographicProjection(ellipsoid) {
  4066. this._ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  4067. this._semimajorAxis = this._ellipsoid.maximumRadius;
  4068. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  4069. }
  4070. defineProperties(GeographicProjection.prototype, {
  4071. /**
  4072. * Gets the {@link Ellipsoid}.
  4073. *
  4074. * @memberof GeographicProjection.prototype
  4075. *
  4076. * @type {Ellipsoid}
  4077. * @readonly
  4078. */
  4079. ellipsoid : {
  4080. get : function() {
  4081. return this._ellipsoid;
  4082. }
  4083. }
  4084. });
  4085. /**
  4086. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  4087. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  4088. * ellipsoid. Z is the unmodified height.
  4089. *
  4090. * @param {Cartographic} cartographic The coordinates to project.
  4091. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  4092. * undefined, a new instance is created and returned.
  4093. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  4094. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  4095. * created and returned.
  4096. */
  4097. GeographicProjection.prototype.project = function(cartographic, result) {
  4098. // Actually this is the special case of equidistant cylindrical called the plate carree
  4099. var semimajorAxis = this._semimajorAxis;
  4100. var x = cartographic.longitude * semimajorAxis;
  4101. var y = cartographic.latitude * semimajorAxis;
  4102. var z = cartographic.height;
  4103. if (!defined(result)) {
  4104. return new Cartesian3(x, y, z);
  4105. }
  4106. result.x = x;
  4107. result.y = y;
  4108. result.z = z;
  4109. return result;
  4110. };
  4111. /**
  4112. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  4113. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  4114. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  4115. *
  4116. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  4117. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  4118. * undefined, a new instance is created and returned.
  4119. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  4120. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  4121. * created and returned.
  4122. */
  4123. GeographicProjection.prototype.unproject = function(cartesian, result) {
  4124. if (!defined(cartesian)) {
  4125. throw new DeveloperError('cartesian is required');
  4126. }
  4127. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  4128. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  4129. var latitude = cartesian.y * oneOverEarthSemimajorAxis;
  4130. var height = cartesian.z;
  4131. if (!defined(result)) {
  4132. return new Cartographic(longitude, latitude, height);
  4133. }
  4134. result.longitude = longitude;
  4135. result.latitude = latitude;
  4136. result.height = height;
  4137. return result;
  4138. };
  4139. return GeographicProjection;
  4140. });
  4141. /*global define*/
  4142. define('Core/Intersect',[
  4143. './freezeObject'
  4144. ], function(
  4145. freezeObject) {
  4146. 'use strict';
  4147. /**
  4148. * This enumerated type is used in determining where, relative to the frustum, an
  4149. * object is located. The object can either be fully contained within the frustum (INSIDE),
  4150. * partially inside the frustum and partially outside (INTERSECTING), or somwhere entirely
  4151. * outside of the frustum's 6 planes (OUTSIDE).
  4152. *
  4153. * @exports Intersect
  4154. */
  4155. var Intersect = {
  4156. /**
  4157. * Represents that an object is not contained within the frustum.
  4158. *
  4159. * @type {Number}
  4160. * @constant
  4161. */
  4162. OUTSIDE : -1,
  4163. /**
  4164. * Represents that an object intersects one of the frustum's planes.
  4165. *
  4166. * @type {Number}
  4167. * @constant
  4168. */
  4169. INTERSECTING : 0,
  4170. /**
  4171. * Represents that an object is fully within the frustum.
  4172. *
  4173. * @type {Number}
  4174. * @constant
  4175. */
  4176. INSIDE : 1
  4177. };
  4178. return freezeObject(Intersect);
  4179. });
  4180. /*global define*/
  4181. define('Core/Interval',[
  4182. './defaultValue'
  4183. ], function(
  4184. defaultValue) {
  4185. 'use strict';
  4186. /**
  4187. * Represents the closed interval [start, stop].
  4188. * @alias Interval
  4189. * @constructor
  4190. *
  4191. * @param {Number} [start=0.0] The beginning of the interval.
  4192. * @param {Number} [stop=0.0] The end of the interval.
  4193. */
  4194. function Interval(start, stop) {
  4195. /**
  4196. * The beginning of the interval.
  4197. * @type {Number}
  4198. * @default 0.0
  4199. */
  4200. this.start = defaultValue(start, 0.0);
  4201. /**
  4202. * The end of the interval.
  4203. * @type {Number}
  4204. * @default 0.0
  4205. */
  4206. this.stop = defaultValue(stop, 0.0);
  4207. }
  4208. return Interval;
  4209. });
  4210. /*global define*/
  4211. define('Core/Matrix3',[
  4212. './Cartesian3',
  4213. './defaultValue',
  4214. './defined',
  4215. './defineProperties',
  4216. './DeveloperError',
  4217. './freezeObject',
  4218. './Math'
  4219. ], function(
  4220. Cartesian3,
  4221. defaultValue,
  4222. defined,
  4223. defineProperties,
  4224. DeveloperError,
  4225. freezeObject,
  4226. CesiumMath) {
  4227. 'use strict';
  4228. /**
  4229. * A 3x3 matrix, indexable as a column-major order array.
  4230. * Constructor parameters are in row-major order for code readability.
  4231. * @alias Matrix3
  4232. * @constructor
  4233. *
  4234. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  4235. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  4236. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  4237. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  4238. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  4239. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  4240. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  4241. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  4242. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  4243. *
  4244. * @see Matrix3.fromColumnMajorArray
  4245. * @see Matrix3.fromRowMajorArray
  4246. * @see Matrix3.fromQuaternion
  4247. * @see Matrix3.fromScale
  4248. * @see Matrix3.fromUniformScale
  4249. * @see Matrix2
  4250. * @see Matrix4
  4251. */
  4252. function Matrix3(column0Row0, column1Row0, column2Row0,
  4253. column0Row1, column1Row1, column2Row1,
  4254. column0Row2, column1Row2, column2Row2) {
  4255. this[0] = defaultValue(column0Row0, 0.0);
  4256. this[1] = defaultValue(column0Row1, 0.0);
  4257. this[2] = defaultValue(column0Row2, 0.0);
  4258. this[3] = defaultValue(column1Row0, 0.0);
  4259. this[4] = defaultValue(column1Row1, 0.0);
  4260. this[5] = defaultValue(column1Row2, 0.0);
  4261. this[6] = defaultValue(column2Row0, 0.0);
  4262. this[7] = defaultValue(column2Row1, 0.0);
  4263. this[8] = defaultValue(column2Row2, 0.0);
  4264. }
  4265. /**
  4266. * The number of elements used to pack the object into an array.
  4267. * @type {Number}
  4268. */
  4269. Matrix3.packedLength = 9;
  4270. /**
  4271. * Stores the provided instance into the provided array.
  4272. *
  4273. * @param {Matrix3} value The value to pack.
  4274. * @param {Number[]} array The array to pack into.
  4275. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4276. *
  4277. * @returns {Number[]} The array that was packed into
  4278. */
  4279. Matrix3.pack = function(value, array, startingIndex) {
  4280. if (!defined(value)) {
  4281. throw new DeveloperError('value is required');
  4282. }
  4283. if (!defined(array)) {
  4284. throw new DeveloperError('array is required');
  4285. }
  4286. startingIndex = defaultValue(startingIndex, 0);
  4287. array[startingIndex++] = value[0];
  4288. array[startingIndex++] = value[1];
  4289. array[startingIndex++] = value[2];
  4290. array[startingIndex++] = value[3];
  4291. array[startingIndex++] = value[4];
  4292. array[startingIndex++] = value[5];
  4293. array[startingIndex++] = value[6];
  4294. array[startingIndex++] = value[7];
  4295. array[startingIndex++] = value[8];
  4296. return array;
  4297. };
  4298. /**
  4299. * Retrieves an instance from a packed array.
  4300. *
  4301. * @param {Number[]} array The packed array.
  4302. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4303. * @param {Matrix3} [result] The object into which to store the result.
  4304. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  4305. */
  4306. Matrix3.unpack = function(array, startingIndex, result) {
  4307. if (!defined(array)) {
  4308. throw new DeveloperError('array is required');
  4309. }
  4310. startingIndex = defaultValue(startingIndex, 0);
  4311. if (!defined(result)) {
  4312. result = new Matrix3();
  4313. }
  4314. result[0] = array[startingIndex++];
  4315. result[1] = array[startingIndex++];
  4316. result[2] = array[startingIndex++];
  4317. result[3] = array[startingIndex++];
  4318. result[4] = array[startingIndex++];
  4319. result[5] = array[startingIndex++];
  4320. result[6] = array[startingIndex++];
  4321. result[7] = array[startingIndex++];
  4322. result[8] = array[startingIndex++];
  4323. return result;
  4324. };
  4325. /**
  4326. * Duplicates a Matrix3 instance.
  4327. *
  4328. * @param {Matrix3} matrix The matrix to duplicate.
  4329. * @param {Matrix3} [result] The object onto which to store the result.
  4330. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  4331. */
  4332. Matrix3.clone = function(values, result) {
  4333. if (!defined(values)) {
  4334. return undefined;
  4335. }
  4336. if (!defined(result)) {
  4337. return new Matrix3(values[0], values[3], values[6],
  4338. values[1], values[4], values[7],
  4339. values[2], values[5], values[8]);
  4340. }
  4341. result[0] = values[0];
  4342. result[1] = values[1];
  4343. result[2] = values[2];
  4344. result[3] = values[3];
  4345. result[4] = values[4];
  4346. result[5] = values[5];
  4347. result[6] = values[6];
  4348. result[7] = values[7];
  4349. result[8] = values[8];
  4350. return result;
  4351. };
  4352. /**
  4353. * Creates a Matrix3 from 9 consecutive elements in an array.
  4354. *
  4355. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  4356. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  4357. * @param {Matrix3} [result] The object onto which to store the result.
  4358. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  4359. *
  4360. * @example
  4361. * // Create the Matrix3:
  4362. * // [1.0, 2.0, 3.0]
  4363. * // [1.0, 2.0, 3.0]
  4364. * // [1.0, 2.0, 3.0]
  4365. *
  4366. * var v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  4367. * var m = Cesium.Matrix3.fromArray(v);
  4368. *
  4369. * // Create same Matrix3 with using an offset into an array
  4370. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  4371. * var m2 = Cesium.Matrix3.fromArray(v2, 2);
  4372. */
  4373. Matrix3.fromArray = function(array, startingIndex, result) {
  4374. if (!defined(array)) {
  4375. throw new DeveloperError('array is required');
  4376. }
  4377. startingIndex = defaultValue(startingIndex, 0);
  4378. if (!defined(result)) {
  4379. result = new Matrix3();
  4380. }
  4381. result[0] = array[startingIndex];
  4382. result[1] = array[startingIndex + 1];
  4383. result[2] = array[startingIndex + 2];
  4384. result[3] = array[startingIndex + 3];
  4385. result[4] = array[startingIndex + 4];
  4386. result[5] = array[startingIndex + 5];
  4387. result[6] = array[startingIndex + 6];
  4388. result[7] = array[startingIndex + 7];
  4389. result[8] = array[startingIndex + 8];
  4390. return result;
  4391. };
  4392. /**
  4393. * Creates a Matrix3 instance from a column-major order array.
  4394. *
  4395. * @param {Number[]} values The column-major order array.
  4396. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4397. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4398. */
  4399. Matrix3.fromColumnMajorArray = function(values, result) {
  4400. if (!defined(values)) {
  4401. throw new DeveloperError('values parameter is required');
  4402. }
  4403. return Matrix3.clone(values, result);
  4404. };
  4405. /**
  4406. * Creates a Matrix3 instance from a row-major order array.
  4407. * The resulting matrix will be in column-major order.
  4408. *
  4409. * @param {Number[]} values The row-major order array.
  4410. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4411. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4412. */
  4413. Matrix3.fromRowMajorArray = function(values, result) {
  4414. if (!defined(values)) {
  4415. throw new DeveloperError('values is required.');
  4416. }
  4417. if (!defined(result)) {
  4418. return new Matrix3(values[0], values[1], values[2],
  4419. values[3], values[4], values[5],
  4420. values[6], values[7], values[8]);
  4421. }
  4422. result[0] = values[0];
  4423. result[1] = values[3];
  4424. result[2] = values[6];
  4425. result[3] = values[1];
  4426. result[4] = values[4];
  4427. result[5] = values[7];
  4428. result[6] = values[2];
  4429. result[7] = values[5];
  4430. result[8] = values[8];
  4431. return result;
  4432. };
  4433. /**
  4434. * Computes a 3x3 rotation matrix from the provided quaternion.
  4435. *
  4436. * @param {Quaternion} quaternion the quaternion to use.
  4437. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4438. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  4439. */
  4440. Matrix3.fromQuaternion = function(quaternion, result) {
  4441. if (!defined(quaternion)) {
  4442. throw new DeveloperError('quaternion is required');
  4443. }
  4444. var x2 = quaternion.x * quaternion.x;
  4445. var xy = quaternion.x * quaternion.y;
  4446. var xz = quaternion.x * quaternion.z;
  4447. var xw = quaternion.x * quaternion.w;
  4448. var y2 = quaternion.y * quaternion.y;
  4449. var yz = quaternion.y * quaternion.z;
  4450. var yw = quaternion.y * quaternion.w;
  4451. var z2 = quaternion.z * quaternion.z;
  4452. var zw = quaternion.z * quaternion.w;
  4453. var w2 = quaternion.w * quaternion.w;
  4454. var m00 = x2 - y2 - z2 + w2;
  4455. var m01 = 2.0 * (xy - zw);
  4456. var m02 = 2.0 * (xz + yw);
  4457. var m10 = 2.0 * (xy + zw);
  4458. var m11 = -x2 + y2 - z2 + w2;
  4459. var m12 = 2.0 * (yz - xw);
  4460. var m20 = 2.0 * (xz - yw);
  4461. var m21 = 2.0 * (yz + xw);
  4462. var m22 = -x2 - y2 + z2 + w2;
  4463. if (!defined(result)) {
  4464. return new Matrix3(m00, m01, m02,
  4465. m10, m11, m12,
  4466. m20, m21, m22);
  4467. }
  4468. result[0] = m00;
  4469. result[1] = m10;
  4470. result[2] = m20;
  4471. result[3] = m01;
  4472. result[4] = m11;
  4473. result[5] = m21;
  4474. result[6] = m02;
  4475. result[7] = m12;
  4476. result[8] = m22;
  4477. return result;
  4478. };
  4479. /**
  4480. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  4481. *
  4482. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  4483. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4484. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  4485. */
  4486. Matrix3.fromHeadingPitchRoll = function(headingPitchRoll, result) {
  4487. if (!defined(headingPitchRoll)) {
  4488. throw new DeveloperError('headingPitchRoll is required');
  4489. }
  4490. var cosTheta = Math.cos(-headingPitchRoll.pitch);
  4491. var cosPsi = Math.cos(-headingPitchRoll.heading);
  4492. var cosPhi = Math.cos(headingPitchRoll.roll);
  4493. var sinTheta = Math.sin(-headingPitchRoll.pitch);
  4494. var sinPsi = Math.sin(-headingPitchRoll.heading);
  4495. var sinPhi = Math.sin(headingPitchRoll.roll);
  4496. var m00 = cosTheta * cosPsi;
  4497. var m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  4498. var m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  4499. var m10 = cosTheta * sinPsi;
  4500. var m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  4501. var m12 = -sinTheta * cosPhi + cosPhi * sinTheta * sinPsi;
  4502. var m20 = -sinTheta;
  4503. var m21 = sinPhi * cosTheta;
  4504. var m22 = cosPhi * cosTheta;
  4505. if (!defined(result)) {
  4506. return new Matrix3(m00, m01, m02,
  4507. m10, m11, m12,
  4508. m20, m21, m22);
  4509. }
  4510. result[0] = m00;
  4511. result[1] = m10;
  4512. result[2] = m20;
  4513. result[3] = m01;
  4514. result[4] = m11;
  4515. result[5] = m21;
  4516. result[6] = m02;
  4517. result[7] = m12;
  4518. result[8] = m22;
  4519. return result;
  4520. };
  4521. /**
  4522. * Computes a Matrix3 instance representing a non-uniform scale.
  4523. *
  4524. * @param {Cartesian3} scale The x, y, and z scale factors.
  4525. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4526. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4527. *
  4528. * @example
  4529. * // Creates
  4530. * // [7.0, 0.0, 0.0]
  4531. * // [0.0, 8.0, 0.0]
  4532. * // [0.0, 0.0, 9.0]
  4533. * var m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  4534. */
  4535. Matrix3.fromScale = function(scale, result) {
  4536. if (!defined(scale)) {
  4537. throw new DeveloperError('scale is required.');
  4538. }
  4539. if (!defined(result)) {
  4540. return new Matrix3(
  4541. scale.x, 0.0, 0.0,
  4542. 0.0, scale.y, 0.0,
  4543. 0.0, 0.0, scale.z);
  4544. }
  4545. result[0] = scale.x;
  4546. result[1] = 0.0;
  4547. result[2] = 0.0;
  4548. result[3] = 0.0;
  4549. result[4] = scale.y;
  4550. result[5] = 0.0;
  4551. result[6] = 0.0;
  4552. result[7] = 0.0;
  4553. result[8] = scale.z;
  4554. return result;
  4555. };
  4556. /**
  4557. * Computes a Matrix3 instance representing a uniform scale.
  4558. *
  4559. * @param {Number} scale The uniform scale factor.
  4560. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4561. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4562. *
  4563. * @example
  4564. * // Creates
  4565. * // [2.0, 0.0, 0.0]
  4566. * // [0.0, 2.0, 0.0]
  4567. * // [0.0, 0.0, 2.0]
  4568. * var m = Cesium.Matrix3.fromUniformScale(2.0);
  4569. */
  4570. Matrix3.fromUniformScale = function(scale, result) {
  4571. if (typeof scale !== 'number') {
  4572. throw new DeveloperError('scale is required.');
  4573. }
  4574. if (!defined(result)) {
  4575. return new Matrix3(
  4576. scale, 0.0, 0.0,
  4577. 0.0, scale, 0.0,
  4578. 0.0, 0.0, scale);
  4579. }
  4580. result[0] = scale;
  4581. result[1] = 0.0;
  4582. result[2] = 0.0;
  4583. result[3] = 0.0;
  4584. result[4] = scale;
  4585. result[5] = 0.0;
  4586. result[6] = 0.0;
  4587. result[7] = 0.0;
  4588. result[8] = scale;
  4589. return result;
  4590. };
  4591. /**
  4592. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  4593. *
  4594. * @param {Cartesian3} the vector on the left hand side of the cross product operation.
  4595. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4596. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4597. *
  4598. * @example
  4599. * // Creates
  4600. * // [0.0, -9.0, 8.0]
  4601. * // [9.0, 0.0, -7.0]
  4602. * // [-8.0, 7.0, 0.0]
  4603. * var m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  4604. */
  4605. Matrix3.fromCrossProduct = function(vector, result) {
  4606. if (!defined(vector)) {
  4607. throw new DeveloperError('vector is required.');
  4608. }
  4609. if (!defined(result)) {
  4610. return new Matrix3(
  4611. 0.0, -vector.z, vector.y,
  4612. vector.z, 0.0, -vector.x,
  4613. -vector.y, vector.x, 0.0);
  4614. }
  4615. result[0] = 0.0;
  4616. result[1] = vector.z;
  4617. result[2] = -vector.y;
  4618. result[3] = -vector.z;
  4619. result[4] = 0.0;
  4620. result[5] = vector.x;
  4621. result[6] = vector.y;
  4622. result[7] = -vector.x;
  4623. result[8] = 0.0;
  4624. return result;
  4625. };
  4626. /**
  4627. * Creates a rotation matrix around the x-axis.
  4628. *
  4629. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  4630. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4631. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4632. *
  4633. * @example
  4634. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  4635. * var p = new Cesium.Cartesian3(5, 6, 7);
  4636. * var m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  4637. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  4638. */
  4639. Matrix3.fromRotationX = function(angle, result) {
  4640. if (!defined(angle)) {
  4641. throw new DeveloperError('angle is required.');
  4642. }
  4643. var cosAngle = Math.cos(angle);
  4644. var sinAngle = Math.sin(angle);
  4645. if (!defined(result)) {
  4646. return new Matrix3(
  4647. 1.0, 0.0, 0.0,
  4648. 0.0, cosAngle, -sinAngle,
  4649. 0.0, sinAngle, cosAngle);
  4650. }
  4651. result[0] = 1.0;
  4652. result[1] = 0.0;
  4653. result[2] = 0.0;
  4654. result[3] = 0.0;
  4655. result[4] = cosAngle;
  4656. result[5] = sinAngle;
  4657. result[6] = 0.0;
  4658. result[7] = -sinAngle;
  4659. result[8] = cosAngle;
  4660. return result;
  4661. };
  4662. /**
  4663. * Creates a rotation matrix around the y-axis.
  4664. *
  4665. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  4666. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4667. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4668. *
  4669. * @example
  4670. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  4671. * var p = new Cesium.Cartesian3(5, 6, 7);
  4672. * var m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  4673. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  4674. */
  4675. Matrix3.fromRotationY = function(angle, result) {
  4676. if (!defined(angle)) {
  4677. throw new DeveloperError('angle is required.');
  4678. }
  4679. var cosAngle = Math.cos(angle);
  4680. var sinAngle = Math.sin(angle);
  4681. if (!defined(result)) {
  4682. return new Matrix3(
  4683. cosAngle, 0.0, sinAngle,
  4684. 0.0, 1.0, 0.0,
  4685. -sinAngle, 0.0, cosAngle);
  4686. }
  4687. result[0] = cosAngle;
  4688. result[1] = 0.0;
  4689. result[2] = -sinAngle;
  4690. result[3] = 0.0;
  4691. result[4] = 1.0;
  4692. result[5] = 0.0;
  4693. result[6] = sinAngle;
  4694. result[7] = 0.0;
  4695. result[8] = cosAngle;
  4696. return result;
  4697. };
  4698. /**
  4699. * Creates a rotation matrix around the z-axis.
  4700. *
  4701. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  4702. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  4703. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  4704. *
  4705. * @example
  4706. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  4707. * var p = new Cesium.Cartesian3(5, 6, 7);
  4708. * var m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  4709. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  4710. */
  4711. Matrix3.fromRotationZ = function(angle, result) {
  4712. if (!defined(angle)) {
  4713. throw new DeveloperError('angle is required.');
  4714. }
  4715. var cosAngle = Math.cos(angle);
  4716. var sinAngle = Math.sin(angle);
  4717. if (!defined(result)) {
  4718. return new Matrix3(
  4719. cosAngle, -sinAngle, 0.0,
  4720. sinAngle, cosAngle, 0.0,
  4721. 0.0, 0.0, 1.0);
  4722. }
  4723. result[0] = cosAngle;
  4724. result[1] = sinAngle;
  4725. result[2] = 0.0;
  4726. result[3] = -sinAngle;
  4727. result[4] = cosAngle;
  4728. result[5] = 0.0;
  4729. result[6] = 0.0;
  4730. result[7] = 0.0;
  4731. result[8] = 1.0;
  4732. return result;
  4733. };
  4734. /**
  4735. * Creates an Array from the provided Matrix3 instance.
  4736. * The array will be in column-major order.
  4737. *
  4738. * @param {Matrix3} matrix The matrix to use..
  4739. * @param {Number[]} [result] The Array onto which to store the result.
  4740. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  4741. */
  4742. Matrix3.toArray = function(matrix, result) {
  4743. if (!defined(matrix)) {
  4744. throw new DeveloperError('matrix is required');
  4745. }
  4746. if (!defined(result)) {
  4747. return [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8]];
  4748. }
  4749. result[0] = matrix[0];
  4750. result[1] = matrix[1];
  4751. result[2] = matrix[2];
  4752. result[3] = matrix[3];
  4753. result[4] = matrix[4];
  4754. result[5] = matrix[5];
  4755. result[6] = matrix[6];
  4756. result[7] = matrix[7];
  4757. result[8] = matrix[8];
  4758. return result;
  4759. };
  4760. /**
  4761. * Computes the array index of the element at the provided row and column.
  4762. *
  4763. * @param {Number} row The zero-based index of the row.
  4764. * @param {Number} column The zero-based index of the column.
  4765. * @returns {Number} The index of the element at the provided row and column.
  4766. *
  4767. * @exception {DeveloperError} row must be 0, 1, or 2.
  4768. * @exception {DeveloperError} column must be 0, 1, or 2.
  4769. *
  4770. * @example
  4771. * var myMatrix = new Cesium.Matrix3();
  4772. * var column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  4773. * var column1Row0 = myMatrix[column1Row0Index]
  4774. * myMatrix[column1Row0Index] = 10.0;
  4775. */
  4776. Matrix3.getElementIndex = function(column, row) {
  4777. if (typeof row !== 'number' || row < 0 || row > 2) {
  4778. throw new DeveloperError('row must be 0, 1, or 2.');
  4779. }
  4780. if (typeof column !== 'number' || column < 0 || column > 2) {
  4781. throw new DeveloperError('column must be 0, 1, or 2.');
  4782. }
  4783. return column * 3 + row;
  4784. };
  4785. /**
  4786. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  4787. *
  4788. * @param {Matrix3} matrix The matrix to use.
  4789. * @param {Number} index The zero-based index of the column to retrieve.
  4790. * @param {Cartesian3} result The object onto which to store the result.
  4791. * @returns {Cartesian3} The modified result parameter.
  4792. *
  4793. * @exception {DeveloperError} index must be 0, 1, or 2.
  4794. */
  4795. Matrix3.getColumn = function(matrix, index, result) {
  4796. if (!defined(matrix)) {
  4797. throw new DeveloperError('matrix is required.');
  4798. }
  4799. if (typeof index !== 'number' || index < 0 || index > 2) {
  4800. throw new DeveloperError('index must be 0, 1, or 2.');
  4801. }
  4802. if (!defined(result)) {
  4803. throw new DeveloperError('result is required');
  4804. }
  4805. var startIndex = index * 3;
  4806. var x = matrix[startIndex];
  4807. var y = matrix[startIndex + 1];
  4808. var z = matrix[startIndex + 2];
  4809. result.x = x;
  4810. result.y = y;
  4811. result.z = z;
  4812. return result;
  4813. };
  4814. /**
  4815. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  4816. *
  4817. * @param {Matrix3} matrix The matrix to use.
  4818. * @param {Number} index The zero-based index of the column to set.
  4819. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  4820. * @param {Matrix3} result The object onto which to store the result.
  4821. * @returns {Matrix3} The modified result parameter.
  4822. *
  4823. * @exception {DeveloperError} index must be 0, 1, or 2.
  4824. */
  4825. Matrix3.setColumn = function(matrix, index, cartesian, result) {
  4826. if (!defined(matrix)) {
  4827. throw new DeveloperError('matrix is required');
  4828. }
  4829. if (!defined(cartesian)) {
  4830. throw new DeveloperError('cartesian is required');
  4831. }
  4832. if (typeof index !== 'number' || index < 0 || index > 2) {
  4833. throw new DeveloperError('index must be 0, 1, or 2.');
  4834. }
  4835. if (!defined(result)) {
  4836. throw new DeveloperError('result is required');
  4837. }
  4838. result = Matrix3.clone(matrix, result);
  4839. var startIndex = index * 3;
  4840. result[startIndex] = cartesian.x;
  4841. result[startIndex + 1] = cartesian.y;
  4842. result[startIndex + 2] = cartesian.z;
  4843. return result;
  4844. };
  4845. /**
  4846. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  4847. *
  4848. * @param {Matrix3} matrix The matrix to use.
  4849. * @param {Number} index The zero-based index of the row to retrieve.
  4850. * @param {Cartesian3} result The object onto which to store the result.
  4851. * @returns {Cartesian3} The modified result parameter.
  4852. *
  4853. * @exception {DeveloperError} index must be 0, 1, or 2.
  4854. */
  4855. Matrix3.getRow = function(matrix, index, result) {
  4856. if (!defined(matrix)) {
  4857. throw new DeveloperError('matrix is required.');
  4858. }
  4859. if (typeof index !== 'number' || index < 0 || index > 2) {
  4860. throw new DeveloperError('index must be 0, 1, or 2.');
  4861. }
  4862. if (!defined(result)) {
  4863. throw new DeveloperError('result is required');
  4864. }
  4865. var x = matrix[index];
  4866. var y = matrix[index + 3];
  4867. var z = matrix[index + 6];
  4868. result.x = x;
  4869. result.y = y;
  4870. result.z = z;
  4871. return result;
  4872. };
  4873. /**
  4874. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  4875. *
  4876. * @param {Matrix3} matrix The matrix to use.
  4877. * @param {Number} index The zero-based index of the row to set.
  4878. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  4879. * @param {Matrix3} result The object onto which to store the result.
  4880. * @returns {Matrix3} The modified result parameter.
  4881. *
  4882. * @exception {DeveloperError} index must be 0, 1, or 2.
  4883. */
  4884. Matrix3.setRow = function(matrix, index, cartesian, result) {
  4885. if (!defined(matrix)) {
  4886. throw new DeveloperError('matrix is required');
  4887. }
  4888. if (!defined(cartesian)) {
  4889. throw new DeveloperError('cartesian is required');
  4890. }
  4891. if (typeof index !== 'number' || index < 0 || index > 2) {
  4892. throw new DeveloperError('index must be 0, 1, or 2.');
  4893. }
  4894. if (!defined(result)) {
  4895. throw new DeveloperError('result is required');
  4896. }
  4897. result = Matrix3.clone(matrix, result);
  4898. result[index] = cartesian.x;
  4899. result[index + 3] = cartesian.y;
  4900. result[index + 6] = cartesian.z;
  4901. return result;
  4902. };
  4903. var scratchColumn = new Cartesian3();
  4904. /**
  4905. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  4906. *
  4907. * @param {Matrix3} matrix The matrix.
  4908. * @param {Cartesian3} result The object onto which to store the result.
  4909. * @returns {Cartesian3} The modified result parameter.
  4910. */
  4911. Matrix3.getScale = function(matrix, result) {
  4912. if (!defined(matrix)) {
  4913. throw new DeveloperError('matrix is required.');
  4914. }
  4915. if (!defined(result)) {
  4916. throw new DeveloperError('result is required');
  4917. }
  4918. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  4919. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn));
  4920. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn));
  4921. return result;
  4922. };
  4923. var scratchScale = new Cartesian3();
  4924. /**
  4925. * Computes the maximum scale assuming the matrix is an affine transformation.
  4926. * The maximum scale is the maximum length of the column vectors.
  4927. *
  4928. * @param {Matrix3} matrix The matrix.
  4929. * @returns {Number} The maximum scale.
  4930. */
  4931. Matrix3.getMaximumScale = function(matrix) {
  4932. Matrix3.getScale(matrix, scratchScale);
  4933. return Cartesian3.maximumComponent(scratchScale);
  4934. };
  4935. /**
  4936. * Computes the product of two matrices.
  4937. *
  4938. * @param {Matrix3} left The first matrix.
  4939. * @param {Matrix3} right The second matrix.
  4940. * @param {Matrix3} result The object onto which to store the result.
  4941. * @returns {Matrix3} The modified result parameter.
  4942. */
  4943. Matrix3.multiply = function(left, right, result) {
  4944. if (!defined(left)) {
  4945. throw new DeveloperError('left is required');
  4946. }
  4947. if (!defined(right)) {
  4948. throw new DeveloperError('right is required');
  4949. }
  4950. if (!defined(result)) {
  4951. throw new DeveloperError('result is required');
  4952. }
  4953. var column0Row0 = left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  4954. var column0Row1 = left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  4955. var column0Row2 = left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  4956. var column1Row0 = left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  4957. var column1Row1 = left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  4958. var column1Row2 = left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  4959. var column2Row0 = left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  4960. var column2Row1 = left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  4961. var column2Row2 = left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  4962. result[0] = column0Row0;
  4963. result[1] = column0Row1;
  4964. result[2] = column0Row2;
  4965. result[3] = column1Row0;
  4966. result[4] = column1Row1;
  4967. result[5] = column1Row2;
  4968. result[6] = column2Row0;
  4969. result[7] = column2Row1;
  4970. result[8] = column2Row2;
  4971. return result;
  4972. };
  4973. /**
  4974. * Computes the sum of two matrices.
  4975. *
  4976. * @param {Matrix3} left The first matrix.
  4977. * @param {Matrix3} right The second matrix.
  4978. * @param {Matrix3} result The object onto which to store the result.
  4979. * @returns {Matrix3} The modified result parameter.
  4980. */
  4981. Matrix3.add = function(left, right, result) {
  4982. if (!defined(left)) {
  4983. throw new DeveloperError('left is required');
  4984. }
  4985. if (!defined(right)) {
  4986. throw new DeveloperError('right is required');
  4987. }
  4988. if (!defined(result)) {
  4989. throw new DeveloperError('result is required');
  4990. }
  4991. result[0] = left[0] + right[0];
  4992. result[1] = left[1] + right[1];
  4993. result[2] = left[2] + right[2];
  4994. result[3] = left[3] + right[3];
  4995. result[4] = left[4] + right[4];
  4996. result[5] = left[5] + right[5];
  4997. result[6] = left[6] + right[6];
  4998. result[7] = left[7] + right[7];
  4999. result[8] = left[8] + right[8];
  5000. return result;
  5001. };
  5002. /**
  5003. * Computes the difference of two matrices.
  5004. *
  5005. * @param {Matrix3} left The first matrix.
  5006. * @param {Matrix3} right The second matrix.
  5007. * @param {Matrix3} result The object onto which to store the result.
  5008. * @returns {Matrix3} The modified result parameter.
  5009. */
  5010. Matrix3.subtract = function(left, right, result) {
  5011. if (!defined(left)) {
  5012. throw new DeveloperError('left is required');
  5013. }
  5014. if (!defined(right)) {
  5015. throw new DeveloperError('right is required');
  5016. }
  5017. if (!defined(result)) {
  5018. throw new DeveloperError('result is required');
  5019. }
  5020. result[0] = left[0] - right[0];
  5021. result[1] = left[1] - right[1];
  5022. result[2] = left[2] - right[2];
  5023. result[3] = left[3] - right[3];
  5024. result[4] = left[4] - right[4];
  5025. result[5] = left[5] - right[5];
  5026. result[6] = left[6] - right[6];
  5027. result[7] = left[7] - right[7];
  5028. result[8] = left[8] - right[8];
  5029. return result;
  5030. };
  5031. /**
  5032. * Computes the product of a matrix and a column vector.
  5033. *
  5034. * @param {Matrix3} matrix The matrix.
  5035. * @param {Cartesian3} cartesian The column.
  5036. * @param {Cartesian3} result The object onto which to store the result.
  5037. * @returns {Cartesian3} The modified result parameter.
  5038. */
  5039. Matrix3.multiplyByVector = function(matrix, cartesian, result) {
  5040. if (!defined(matrix)) {
  5041. throw new DeveloperError('matrix is required');
  5042. }
  5043. if (!defined(cartesian)) {
  5044. throw new DeveloperError('cartesian is required');
  5045. }
  5046. if (!defined(result)) {
  5047. throw new DeveloperError('result is required');
  5048. }
  5049. var vX = cartesian.x;
  5050. var vY = cartesian.y;
  5051. var vZ = cartesian.z;
  5052. var x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  5053. var y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  5054. var z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  5055. result.x = x;
  5056. result.y = y;
  5057. result.z = z;
  5058. return result;
  5059. };
  5060. /**
  5061. * Computes the product of a matrix and a scalar.
  5062. *
  5063. * @param {Matrix3} matrix The matrix.
  5064. * @param {Number} scalar The number to multiply by.
  5065. * @param {Matrix3} result The object onto which to store the result.
  5066. * @returns {Matrix3} The modified result parameter.
  5067. */
  5068. Matrix3.multiplyByScalar = function(matrix, scalar, result) {
  5069. if (!defined(matrix)) {
  5070. throw new DeveloperError('matrix is required');
  5071. }
  5072. if (typeof scalar !== 'number') {
  5073. throw new DeveloperError('scalar must be a number');
  5074. }
  5075. if (!defined(result)) {
  5076. throw new DeveloperError('result is required');
  5077. }
  5078. result[0] = matrix[0] * scalar;
  5079. result[1] = matrix[1] * scalar;
  5080. result[2] = matrix[2] * scalar;
  5081. result[3] = matrix[3] * scalar;
  5082. result[4] = matrix[4] * scalar;
  5083. result[5] = matrix[5] * scalar;
  5084. result[6] = matrix[6] * scalar;
  5085. result[7] = matrix[7] * scalar;
  5086. result[8] = matrix[8] * scalar;
  5087. return result;
  5088. };
  5089. /**
  5090. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  5091. *
  5092. * @param {Matrix3} matrix The matrix on the left-hand side.
  5093. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  5094. * @param {Matrix3} result The object onto which to store the result.
  5095. * @returns {Matrix3} The modified result parameter.
  5096. *
  5097. *
  5098. * @example
  5099. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  5100. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  5101. *
  5102. * @see Matrix3.fromScale
  5103. * @see Matrix3.multiplyByUniformScale
  5104. */
  5105. Matrix3.multiplyByScale = function(matrix, scale, result) {
  5106. if (!defined(matrix)) {
  5107. throw new DeveloperError('matrix is required');
  5108. }
  5109. if (!defined(scale)) {
  5110. throw new DeveloperError('scale is required');
  5111. }
  5112. if (!defined(result)) {
  5113. throw new DeveloperError('result is required');
  5114. }
  5115. result[0] = matrix[0] * scale.x;
  5116. result[1] = matrix[1] * scale.x;
  5117. result[2] = matrix[2] * scale.x;
  5118. result[3] = matrix[3] * scale.y;
  5119. result[4] = matrix[4] * scale.y;
  5120. result[5] = matrix[5] * scale.y;
  5121. result[6] = matrix[6] * scale.z;
  5122. result[7] = matrix[7] * scale.z;
  5123. result[8] = matrix[8] * scale.z;
  5124. return result;
  5125. };
  5126. /**
  5127. * Creates a negated copy of the provided matrix.
  5128. *
  5129. * @param {Matrix3} matrix The matrix to negate.
  5130. * @param {Matrix3} result The object onto which to store the result.
  5131. * @returns {Matrix3} The modified result parameter.
  5132. */
  5133. Matrix3.negate = function(matrix, result) {
  5134. if (!defined(matrix)) {
  5135. throw new DeveloperError('matrix is required');
  5136. }
  5137. if (!defined(result)) {
  5138. throw new DeveloperError('result is required');
  5139. }
  5140. result[0] = -matrix[0];
  5141. result[1] = -matrix[1];
  5142. result[2] = -matrix[2];
  5143. result[3] = -matrix[3];
  5144. result[4] = -matrix[4];
  5145. result[5] = -matrix[5];
  5146. result[6] = -matrix[6];
  5147. result[7] = -matrix[7];
  5148. result[8] = -matrix[8];
  5149. return result;
  5150. };
  5151. /**
  5152. * Computes the transpose of the provided matrix.
  5153. *
  5154. * @param {Matrix3} matrix The matrix to transpose.
  5155. * @param {Matrix3} result The object onto which to store the result.
  5156. * @returns {Matrix3} The modified result parameter.
  5157. */
  5158. Matrix3.transpose = function(matrix, result) {
  5159. if (!defined(matrix)) {
  5160. throw new DeveloperError('matrix is required');
  5161. }
  5162. if (!defined(result)) {
  5163. throw new DeveloperError('result is required');
  5164. }
  5165. var column0Row0 = matrix[0];
  5166. var column0Row1 = matrix[3];
  5167. var column0Row2 = matrix[6];
  5168. var column1Row0 = matrix[1];
  5169. var column1Row1 = matrix[4];
  5170. var column1Row2 = matrix[7];
  5171. var column2Row0 = matrix[2];
  5172. var column2Row1 = matrix[5];
  5173. var column2Row2 = matrix[8];
  5174. result[0] = column0Row0;
  5175. result[1] = column0Row1;
  5176. result[2] = column0Row2;
  5177. result[3] = column1Row0;
  5178. result[4] = column1Row1;
  5179. result[5] = column1Row2;
  5180. result[6] = column2Row0;
  5181. result[7] = column2Row1;
  5182. result[8] = column2Row2;
  5183. return result;
  5184. };
  5185. function computeFrobeniusNorm(matrix) {
  5186. var norm = 0.0;
  5187. for (var i = 0; i < 9; ++i) {
  5188. var temp = matrix[i];
  5189. norm += temp * temp;
  5190. }
  5191. return Math.sqrt(norm);
  5192. }
  5193. var rowVal = [1, 0, 0];
  5194. var colVal = [2, 2, 1];
  5195. function offDiagonalFrobeniusNorm(matrix) {
  5196. // Computes the "off-diagonal" Frobenius norm.
  5197. // Assumes matrix is symmetric.
  5198. var norm = 0.0;
  5199. for (var i = 0; i < 3; ++i) {
  5200. var temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  5201. norm += 2.0 * temp * temp;
  5202. }
  5203. return Math.sqrt(norm);
  5204. }
  5205. function shurDecomposition(matrix, result) {
  5206. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  5207. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  5208. //
  5209. // The routine takes a matrix, which is assumed to be symmetric, and
  5210. // finds the largest off-diagonal term, and then creates
  5211. // a matrix (result) which can be used to help reduce it
  5212. var tolerance = CesiumMath.EPSILON15;
  5213. var maxDiagonal = 0.0;
  5214. var rotAxis = 1;
  5215. // find pivot (rotAxis) based on max diagonal of matrix
  5216. for (var i = 0; i < 3; ++i) {
  5217. var temp = Math.abs(matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]);
  5218. if (temp > maxDiagonal) {
  5219. rotAxis = i;
  5220. maxDiagonal = temp;
  5221. }
  5222. }
  5223. var c = 1.0;
  5224. var s = 0.0;
  5225. var p = rowVal[rotAxis];
  5226. var q = colVal[rotAxis];
  5227. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  5228. var qq = matrix[Matrix3.getElementIndex(q, q)];
  5229. var pp = matrix[Matrix3.getElementIndex(p, p)];
  5230. var qp = matrix[Matrix3.getElementIndex(q, p)];
  5231. var tau = (qq - pp) / 2.0 / qp;
  5232. var t;
  5233. if (tau < 0.0) {
  5234. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  5235. } else {
  5236. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  5237. }
  5238. c = 1.0 / Math.sqrt(1.0 + t * t);
  5239. s = t * c;
  5240. }
  5241. result = Matrix3.clone(Matrix3.IDENTITY, result);
  5242. result[Matrix3.getElementIndex(p, p)] = result[Matrix3.getElementIndex(q, q)] = c;
  5243. result[Matrix3.getElementIndex(q, p)] = s;
  5244. result[Matrix3.getElementIndex(p, q)] = -s;
  5245. return result;
  5246. }
  5247. var jMatrix = new Matrix3();
  5248. var jMatrixTranspose = new Matrix3();
  5249. /**
  5250. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  5251. * <p>
  5252. * Returns a diagonal matrix and unitary matrix such that:
  5253. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  5254. * </p>
  5255. * <p>
  5256. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  5257. * of the unitary matrix are the corresponding eigenvectors.
  5258. * </p>
  5259. *
  5260. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  5261. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  5262. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  5263. *
  5264. * @example
  5265. * var a = //... symetric matrix
  5266. * var result = {
  5267. * unitary : new Cesium.Matrix3(),
  5268. * diagonal : new Cesium.Matrix3()
  5269. * };
  5270. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  5271. *
  5272. * var unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  5273. * var b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  5274. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  5275. *
  5276. * var lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  5277. * var v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  5278. * var c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  5279. */
  5280. Matrix3.computeEigenDecomposition = function(matrix, result) {
  5281. if (!defined(matrix)) {
  5282. throw new DeveloperError('matrix is required.');
  5283. }
  5284. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  5285. // section 8.4.3 The Classical Jacobi Algorithm
  5286. var tolerance = CesiumMath.EPSILON20;
  5287. var maxSweeps = 10;
  5288. var count = 0;
  5289. var sweep = 0;
  5290. if (!defined(result)) {
  5291. result = {};
  5292. }
  5293. var unitaryMatrix = result.unitary = Matrix3.clone(Matrix3.IDENTITY, result.unitary);
  5294. var diagMatrix = result.diagonal = Matrix3.clone(matrix, result.diagonal);
  5295. var epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  5296. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  5297. shurDecomposition(diagMatrix, jMatrix);
  5298. Matrix3.transpose(jMatrix, jMatrixTranspose);
  5299. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  5300. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  5301. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  5302. if (++count > 2) {
  5303. ++sweep;
  5304. count = 0;
  5305. }
  5306. }
  5307. return result;
  5308. };
  5309. /**
  5310. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  5311. *
  5312. * @param {Matrix3} matrix The matrix with signed elements.
  5313. * @param {Matrix3} result The object onto which to store the result.
  5314. * @returns {Matrix3} The modified result parameter.
  5315. */
  5316. Matrix3.abs = function(matrix, result) {
  5317. if (!defined(matrix)) {
  5318. throw new DeveloperError('matrix is required');
  5319. }
  5320. if (!defined(result)) {
  5321. throw new DeveloperError('result is required');
  5322. }
  5323. result[0] = Math.abs(matrix[0]);
  5324. result[1] = Math.abs(matrix[1]);
  5325. result[2] = Math.abs(matrix[2]);
  5326. result[3] = Math.abs(matrix[3]);
  5327. result[4] = Math.abs(matrix[4]);
  5328. result[5] = Math.abs(matrix[5]);
  5329. result[6] = Math.abs(matrix[6]);
  5330. result[7] = Math.abs(matrix[7]);
  5331. result[8] = Math.abs(matrix[8]);
  5332. return result;
  5333. };
  5334. /**
  5335. * Computes the determinant of the provided matrix.
  5336. *
  5337. * @param {Matrix3} matrix The matrix to use.
  5338. * @returns {Number} The value of the determinant of the matrix.
  5339. */
  5340. Matrix3.determinant = function(matrix) {
  5341. if (!defined(matrix)) {
  5342. throw new DeveloperError('matrix is required');
  5343. }
  5344. var m11 = matrix[0];
  5345. var m21 = matrix[3];
  5346. var m31 = matrix[6];
  5347. var m12 = matrix[1];
  5348. var m22 = matrix[4];
  5349. var m32 = matrix[7];
  5350. var m13 = matrix[2];
  5351. var m23 = matrix[5];
  5352. var m33 = matrix[8];
  5353. return m11 * (m22 * m33 - m23 * m32) + m12 * (m23 * m31 - m21 * m33) + m13 * (m21 * m32 - m22 * m31);
  5354. };
  5355. /**
  5356. * Computes the inverse of the provided matrix.
  5357. *
  5358. * @param {Matrix3} matrix The matrix to invert.
  5359. * @param {Matrix3} result The object onto which to store the result.
  5360. * @returns {Matrix3} The modified result parameter.
  5361. *
  5362. * @exception {DeveloperError} matrix is not invertible.
  5363. */
  5364. Matrix3.inverse = function(matrix, result) {
  5365. if (!defined(matrix)) {
  5366. throw new DeveloperError('matrix is required');
  5367. }
  5368. if (!defined(result)) {
  5369. throw new DeveloperError('result is required');
  5370. }
  5371. var m11 = matrix[0];
  5372. var m21 = matrix[1];
  5373. var m31 = matrix[2];
  5374. var m12 = matrix[3];
  5375. var m22 = matrix[4];
  5376. var m32 = matrix[5];
  5377. var m13 = matrix[6];
  5378. var m23 = matrix[7];
  5379. var m33 = matrix[8];
  5380. var determinant = Matrix3.determinant(matrix);
  5381. if (Math.abs(determinant) <= CesiumMath.EPSILON15) {
  5382. throw new DeveloperError('matrix is not invertible');
  5383. }
  5384. result[0] = m22 * m33 - m23 * m32;
  5385. result[1] = m23 * m31 - m21 * m33;
  5386. result[2] = m21 * m32 - m22 * m31;
  5387. result[3] = m13 * m32 - m12 * m33;
  5388. result[4] = m11 * m33 - m13 * m31;
  5389. result[5] = m12 * m31 - m11 * m32;
  5390. result[6] = m12 * m23 - m13 * m22;
  5391. result[7] = m13 * m21 - m11 * m23;
  5392. result[8] = m11 * m22 - m12 * m21;
  5393. var scale = 1.0 / determinant;
  5394. return Matrix3.multiplyByScalar(result, scale, result);
  5395. };
  5396. /**
  5397. * Compares the provided matrices componentwise and returns
  5398. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5399. *
  5400. * @param {Matrix3} [left] The first matrix.
  5401. * @param {Matrix3} [right] The second matrix.
  5402. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  5403. */
  5404. Matrix3.equals = function(left, right) {
  5405. return (left === right) ||
  5406. (defined(left) &&
  5407. defined(right) &&
  5408. left[0] === right[0] &&
  5409. left[1] === right[1] &&
  5410. left[2] === right[2] &&
  5411. left[3] === right[3] &&
  5412. left[4] === right[4] &&
  5413. left[5] === right[5] &&
  5414. left[6] === right[6] &&
  5415. left[7] === right[7] &&
  5416. left[8] === right[8]);
  5417. };
  5418. /**
  5419. * Compares the provided matrices componentwise and returns
  5420. * <code>true</code> if they are within the provided epsilon,
  5421. * <code>false</code> otherwise.
  5422. *
  5423. * @param {Matrix3} [left] The first matrix.
  5424. * @param {Matrix3} [right] The second matrix.
  5425. * @param {Number} epsilon The epsilon to use for equality testing.
  5426. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  5427. */
  5428. Matrix3.equalsEpsilon = function(left, right, epsilon) {
  5429. if (typeof epsilon !== 'number') {
  5430. throw new DeveloperError('epsilon must be a number');
  5431. }
  5432. return (left === right) ||
  5433. (defined(left) &&
  5434. defined(right) &&
  5435. Math.abs(left[0] - right[0]) <= epsilon &&
  5436. Math.abs(left[1] - right[1]) <= epsilon &&
  5437. Math.abs(left[2] - right[2]) <= epsilon &&
  5438. Math.abs(left[3] - right[3]) <= epsilon &&
  5439. Math.abs(left[4] - right[4]) <= epsilon &&
  5440. Math.abs(left[5] - right[5]) <= epsilon &&
  5441. Math.abs(left[6] - right[6]) <= epsilon &&
  5442. Math.abs(left[7] - right[7]) <= epsilon &&
  5443. Math.abs(left[8] - right[8]) <= epsilon);
  5444. };
  5445. /**
  5446. * An immutable Matrix3 instance initialized to the identity matrix.
  5447. *
  5448. * @type {Matrix3}
  5449. * @constant
  5450. */
  5451. Matrix3.IDENTITY = freezeObject(new Matrix3(1.0, 0.0, 0.0,
  5452. 0.0, 1.0, 0.0,
  5453. 0.0, 0.0, 1.0));
  5454. /**
  5455. * An immutable Matrix3 instance initialized to the zero matrix.
  5456. *
  5457. * @type {Matrix3}
  5458. * @constant
  5459. */
  5460. Matrix3.ZERO = freezeObject(new Matrix3(0.0, 0.0, 0.0,
  5461. 0.0, 0.0, 0.0,
  5462. 0.0, 0.0, 0.0));
  5463. /**
  5464. * The index into Matrix3 for column 0, row 0.
  5465. *
  5466. * @type {Number}
  5467. * @constant
  5468. */
  5469. Matrix3.COLUMN0ROW0 = 0;
  5470. /**
  5471. * The index into Matrix3 for column 0, row 1.
  5472. *
  5473. * @type {Number}
  5474. * @constant
  5475. */
  5476. Matrix3.COLUMN0ROW1 = 1;
  5477. /**
  5478. * The index into Matrix3 for column 0, row 2.
  5479. *
  5480. * @type {Number}
  5481. * @constant
  5482. */
  5483. Matrix3.COLUMN0ROW2 = 2;
  5484. /**
  5485. * The index into Matrix3 for column 1, row 0.
  5486. *
  5487. * @type {Number}
  5488. * @constant
  5489. */
  5490. Matrix3.COLUMN1ROW0 = 3;
  5491. /**
  5492. * The index into Matrix3 for column 1, row 1.
  5493. *
  5494. * @type {Number}
  5495. * @constant
  5496. */
  5497. Matrix3.COLUMN1ROW1 = 4;
  5498. /**
  5499. * The index into Matrix3 for column 1, row 2.
  5500. *
  5501. * @type {Number}
  5502. * @constant
  5503. */
  5504. Matrix3.COLUMN1ROW2 = 5;
  5505. /**
  5506. * The index into Matrix3 for column 2, row 0.
  5507. *
  5508. * @type {Number}
  5509. * @constant
  5510. */
  5511. Matrix3.COLUMN2ROW0 = 6;
  5512. /**
  5513. * The index into Matrix3 for column 2, row 1.
  5514. *
  5515. * @type {Number}
  5516. * @constant
  5517. */
  5518. Matrix3.COLUMN2ROW1 = 7;
  5519. /**
  5520. * The index into Matrix3 for column 2, row 2.
  5521. *
  5522. * @type {Number}
  5523. * @constant
  5524. */
  5525. Matrix3.COLUMN2ROW2 = 8;
  5526. defineProperties(Matrix3.prototype, {
  5527. /**
  5528. * Gets the number of items in the collection.
  5529. * @memberof Matrix3.prototype
  5530. *
  5531. * @type {Number}
  5532. */
  5533. length : {
  5534. get : function() {
  5535. return Matrix3.packedLength;
  5536. }
  5537. }
  5538. });
  5539. /**
  5540. * Duplicates the provided Matrix3 instance.
  5541. *
  5542. * @param {Matrix3} [result] The object onto which to store the result.
  5543. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  5544. */
  5545. Matrix3.prototype.clone = function(result) {
  5546. return Matrix3.clone(this, result);
  5547. };
  5548. /**
  5549. * Compares this matrix to the provided matrix componentwise and returns
  5550. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5551. *
  5552. * @param {Matrix3} [right] The right hand side matrix.
  5553. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  5554. */
  5555. Matrix3.prototype.equals = function(right) {
  5556. return Matrix3.equals(this, right);
  5557. };
  5558. /**
  5559. * @private
  5560. */
  5561. Matrix3.equalsArray = function(matrix, array, offset) {
  5562. return matrix[0] === array[offset] &&
  5563. matrix[1] === array[offset + 1] &&
  5564. matrix[2] === array[offset + 2] &&
  5565. matrix[3] === array[offset + 3] &&
  5566. matrix[4] === array[offset + 4] &&
  5567. matrix[5] === array[offset + 5] &&
  5568. matrix[6] === array[offset + 6] &&
  5569. matrix[7] === array[offset + 7] &&
  5570. matrix[8] === array[offset + 8];
  5571. };
  5572. /**
  5573. * Compares this matrix to the provided matrix componentwise and returns
  5574. * <code>true</code> if they are within the provided epsilon,
  5575. * <code>false</code> otherwise.
  5576. *
  5577. * @param {Matrix3} [right] The right hand side matrix.
  5578. * @param {Number} epsilon The epsilon to use for equality testing.
  5579. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  5580. */
  5581. Matrix3.prototype.equalsEpsilon = function(right, epsilon) {
  5582. return Matrix3.equalsEpsilon(this, right, epsilon);
  5583. };
  5584. /**
  5585. * Creates a string representing this Matrix with each row being
  5586. * on a separate line and in the format '(column0, column1, column2)'.
  5587. *
  5588. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  5589. */
  5590. Matrix3.prototype.toString = function() {
  5591. return '(' + this[0] + ', ' + this[3] + ', ' + this[6] + ')\n' +
  5592. '(' + this[1] + ', ' + this[4] + ', ' + this[7] + ')\n' +
  5593. '(' + this[2] + ', ' + this[5] + ', ' + this[8] + ')';
  5594. };
  5595. return Matrix3;
  5596. });
  5597. /*global define*/
  5598. define('Core/Cartesian4',[
  5599. './defaultValue',
  5600. './defined',
  5601. './DeveloperError',
  5602. './freezeObject',
  5603. './Math'
  5604. ], function(
  5605. defaultValue,
  5606. defined,
  5607. DeveloperError,
  5608. freezeObject,
  5609. CesiumMath) {
  5610. 'use strict';
  5611. /**
  5612. * A 4D Cartesian point.
  5613. * @alias Cartesian4
  5614. * @constructor
  5615. *
  5616. * @param {Number} [x=0.0] The X component.
  5617. * @param {Number} [y=0.0] The Y component.
  5618. * @param {Number} [z=0.0] The Z component.
  5619. * @param {Number} [w=0.0] The W component.
  5620. *
  5621. * @see Cartesian2
  5622. * @see Cartesian3
  5623. * @see Packable
  5624. */
  5625. function Cartesian4(x, y, z, w) {
  5626. /**
  5627. * The X component.
  5628. * @type {Number}
  5629. * @default 0.0
  5630. */
  5631. this.x = defaultValue(x, 0.0);
  5632. /**
  5633. * The Y component.
  5634. * @type {Number}
  5635. * @default 0.0
  5636. */
  5637. this.y = defaultValue(y, 0.0);
  5638. /**
  5639. * The Z component.
  5640. * @type {Number}
  5641. * @default 0.0
  5642. */
  5643. this.z = defaultValue(z, 0.0);
  5644. /**
  5645. * The W component.
  5646. * @type {Number}
  5647. * @default 0.0
  5648. */
  5649. this.w = defaultValue(w, 0.0);
  5650. }
  5651. /**
  5652. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  5653. *
  5654. * @param {Number} x The x coordinate.
  5655. * @param {Number} y The y coordinate.
  5656. * @param {Number} z The z coordinate.
  5657. * @param {Number} w The w coordinate.
  5658. * @param {Cartesian4} [result] The object onto which to store the result.
  5659. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5660. */
  5661. Cartesian4.fromElements = function(x, y, z, w, result) {
  5662. if (!defined(result)) {
  5663. return new Cartesian4(x, y, z, w);
  5664. }
  5665. result.x = x;
  5666. result.y = y;
  5667. result.z = z;
  5668. result.w = w;
  5669. return result;
  5670. };
  5671. /**
  5672. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  5673. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  5674. *
  5675. * @param {Color} color The source color.
  5676. * @param {Cartesian4} [result] The object onto which to store the result.
  5677. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5678. */
  5679. Cartesian4.fromColor = function(color, result) {
  5680. if (!defined(color)) {
  5681. throw new DeveloperError('color is required');
  5682. }
  5683. if (!defined(result)) {
  5684. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  5685. }
  5686. result.x = color.red;
  5687. result.y = color.green;
  5688. result.z = color.blue;
  5689. result.w = color.alpha;
  5690. return result;
  5691. };
  5692. /**
  5693. * Duplicates a Cartesian4 instance.
  5694. *
  5695. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  5696. * @param {Cartesian4} [result] The object onto which to store the result.
  5697. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  5698. */
  5699. Cartesian4.clone = function(cartesian, result) {
  5700. if (!defined(cartesian)) {
  5701. return undefined;
  5702. }
  5703. if (!defined(result)) {
  5704. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  5705. }
  5706. result.x = cartesian.x;
  5707. result.y = cartesian.y;
  5708. result.z = cartesian.z;
  5709. result.w = cartesian.w;
  5710. return result;
  5711. };
  5712. /**
  5713. * The number of elements used to pack the object into an array.
  5714. * @type {Number}
  5715. */
  5716. Cartesian4.packedLength = 4;
  5717. /**
  5718. * Stores the provided instance into the provided array.
  5719. *
  5720. * @param {Cartesian4} value The value to pack.
  5721. * @param {Number[]} array The array to pack into.
  5722. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  5723. *
  5724. * @returns {Number[]} The array that was packed into
  5725. */
  5726. Cartesian4.pack = function(value, array, startingIndex) {
  5727. if (!defined(value)) {
  5728. throw new DeveloperError('value is required');
  5729. }
  5730. if (!defined(array)) {
  5731. throw new DeveloperError('array is required');
  5732. }
  5733. startingIndex = defaultValue(startingIndex, 0);
  5734. array[startingIndex++] = value.x;
  5735. array[startingIndex++] = value.y;
  5736. array[startingIndex++] = value.z;
  5737. array[startingIndex] = value.w;
  5738. return array;
  5739. };
  5740. /**
  5741. * Retrieves an instance from a packed array.
  5742. *
  5743. * @param {Number[]} array The packed array.
  5744. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  5745. * @param {Cartesian4} [result] The object into which to store the result.
  5746. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5747. */
  5748. Cartesian4.unpack = function(array, startingIndex, result) {
  5749. if (!defined(array)) {
  5750. throw new DeveloperError('array is required');
  5751. }
  5752. startingIndex = defaultValue(startingIndex, 0);
  5753. if (!defined(result)) {
  5754. result = new Cartesian4();
  5755. }
  5756. result.x = array[startingIndex++];
  5757. result.y = array[startingIndex++];
  5758. result.z = array[startingIndex++];
  5759. result.w = array[startingIndex];
  5760. return result;
  5761. };
  5762. /**
  5763. * Flattens an array of Cartesian4s into and array of components.
  5764. *
  5765. * @param {Cartesian4[]} array The array of cartesians to pack.
  5766. * @param {Number[]} result The array onto which to store the result.
  5767. * @returns {Number[]} The packed array.
  5768. */
  5769. Cartesian4.packArray = function(array, result) {
  5770. if (!defined(array)) {
  5771. throw new DeveloperError('array is required');
  5772. }
  5773. var length = array.length;
  5774. if (!defined(result)) {
  5775. result = new Array(length * 4);
  5776. } else {
  5777. result.length = length * 4;
  5778. }
  5779. for (var i = 0; i < length; ++i) {
  5780. Cartesian4.pack(array[i], result, i * 4);
  5781. }
  5782. return result;
  5783. };
  5784. /**
  5785. * Unpacks an array of cartesian components into and array of Cartesian4s.
  5786. *
  5787. * @param {Number[]} array The array of components to unpack.
  5788. * @param {Cartesian4[]} result The array onto which to store the result.
  5789. * @returns {Cartesian4[]} The unpacked array.
  5790. */
  5791. Cartesian4.unpackArray = function(array, result) {
  5792. if (!defined(array)) {
  5793. throw new DeveloperError('array is required');
  5794. }
  5795. var length = array.length;
  5796. if (!defined(result)) {
  5797. result = new Array(length / 4);
  5798. } else {
  5799. result.length = length / 4;
  5800. }
  5801. for (var i = 0; i < length; i += 4) {
  5802. var index = i / 4;
  5803. result[index] = Cartesian4.unpack(array, i, result[index]);
  5804. }
  5805. return result;
  5806. };
  5807. /**
  5808. * Creates a Cartesian4 from four consecutive elements in an array.
  5809. * @function
  5810. *
  5811. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  5812. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  5813. * @param {Cartesian4} [result] The object onto which to store the result.
  5814. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5815. *
  5816. * @example
  5817. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  5818. * var v = [1.0, 2.0, 3.0, 4.0];
  5819. * var p = Cesium.Cartesian4.fromArray(v);
  5820. *
  5821. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  5822. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  5823. * var p2 = Cesium.Cartesian4.fromArray(v2, 2);
  5824. */
  5825. Cartesian4.fromArray = Cartesian4.unpack;
  5826. /**
  5827. * Computes the value of the maximum component for the supplied Cartesian.
  5828. *
  5829. * @param {Cartesian4} cartesian The cartesian to use.
  5830. * @returns {Number} The value of the maximum component.
  5831. */
  5832. Cartesian4.maximumComponent = function(cartesian) {
  5833. if (!defined(cartesian)) {
  5834. throw new DeveloperError('cartesian is required');
  5835. }
  5836. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  5837. };
  5838. /**
  5839. * Computes the value of the minimum component for the supplied Cartesian.
  5840. *
  5841. * @param {Cartesian4} cartesian The cartesian to use.
  5842. * @returns {Number} The value of the minimum component.
  5843. */
  5844. Cartesian4.minimumComponent = function(cartesian) {
  5845. if (!defined(cartesian)) {
  5846. throw new DeveloperError('cartesian is required');
  5847. }
  5848. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  5849. };
  5850. /**
  5851. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  5852. *
  5853. * @param {Cartesian4} first A cartesian to compare.
  5854. * @param {Cartesian4} second A cartesian to compare.
  5855. * @param {Cartesian4} result The object into which to store the result.
  5856. * @returns {Cartesian4} A cartesian with the minimum components.
  5857. */
  5858. Cartesian4.minimumByComponent = function(first, second, result) {
  5859. if (!defined(first)) {
  5860. throw new DeveloperError('first is required.');
  5861. }
  5862. if (!defined(second)) {
  5863. throw new DeveloperError('second is required.');
  5864. }
  5865. if (!defined(result)) {
  5866. throw new DeveloperError('result is required.');
  5867. }
  5868. result.x = Math.min(first.x, second.x);
  5869. result.y = Math.min(first.y, second.y);
  5870. result.z = Math.min(first.z, second.z);
  5871. result.w = Math.min(first.w, second.w);
  5872. return result;
  5873. };
  5874. /**
  5875. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  5876. *
  5877. * @param {Cartesian4} first A cartesian to compare.
  5878. * @param {Cartesian4} second A cartesian to compare.
  5879. * @param {Cartesian4} result The object into which to store the result.
  5880. * @returns {Cartesian4} A cartesian with the maximum components.
  5881. */
  5882. Cartesian4.maximumByComponent = function(first, second, result) {
  5883. if (!defined(first)) {
  5884. throw new DeveloperError('first is required.');
  5885. }
  5886. if (!defined(second)) {
  5887. throw new DeveloperError('second is required.');
  5888. }
  5889. if (!defined(result)) {
  5890. throw new DeveloperError('result is required.');
  5891. }
  5892. result.x = Math.max(first.x, second.x);
  5893. result.y = Math.max(first.y, second.y);
  5894. result.z = Math.max(first.z, second.z);
  5895. result.w = Math.max(first.w, second.w);
  5896. return result;
  5897. };
  5898. /**
  5899. * Computes the provided Cartesian's squared magnitude.
  5900. *
  5901. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  5902. * @returns {Number} The squared magnitude.
  5903. */
  5904. Cartesian4.magnitudeSquared = function(cartesian) {
  5905. if (!defined(cartesian)) {
  5906. throw new DeveloperError('cartesian is required');
  5907. }
  5908. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z + cartesian.w * cartesian.w;
  5909. };
  5910. /**
  5911. * Computes the Cartesian's magnitude (length).
  5912. *
  5913. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  5914. * @returns {Number} The magnitude.
  5915. */
  5916. Cartesian4.magnitude = function(cartesian) {
  5917. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  5918. };
  5919. var distanceScratch = new Cartesian4();
  5920. /**
  5921. * Computes the 4-space distance between two points.
  5922. *
  5923. * @param {Cartesian4} left The first point to compute the distance from.
  5924. * @param {Cartesian4} right The second point to compute the distance to.
  5925. * @returns {Number} The distance between two points.
  5926. *
  5927. * @example
  5928. * // Returns 1.0
  5929. * var d = Cesium.Cartesian4.distance(
  5930. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  5931. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  5932. */
  5933. Cartesian4.distance = function(left, right) {
  5934. if (!defined(left) || !defined(right)) {
  5935. throw new DeveloperError('left and right are required.');
  5936. }
  5937. Cartesian4.subtract(left, right, distanceScratch);
  5938. return Cartesian4.magnitude(distanceScratch);
  5939. };
  5940. /**
  5941. * Computes the squared distance between two points. Comparing squared distances
  5942. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  5943. *
  5944. * @param {Cartesian4} left The first point to compute the distance from.
  5945. * @param {Cartesian4} right The second point to compute the distance to.
  5946. * @returns {Number} The distance between two points.
  5947. *
  5948. * @example
  5949. * // Returns 4.0, not 2.0
  5950. * var d = Cesium.Cartesian4.distance(
  5951. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  5952. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  5953. */
  5954. Cartesian4.distanceSquared = function(left, right) {
  5955. if (!defined(left) || !defined(right)) {
  5956. throw new DeveloperError('left and right are required.');
  5957. }
  5958. Cartesian4.subtract(left, right, distanceScratch);
  5959. return Cartesian4.magnitudeSquared(distanceScratch);
  5960. };
  5961. /**
  5962. * Computes the normalized form of the supplied Cartesian.
  5963. *
  5964. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  5965. * @param {Cartesian4} result The object onto which to store the result.
  5966. * @returns {Cartesian4} The modified result parameter.
  5967. */
  5968. Cartesian4.normalize = function(cartesian, result) {
  5969. if (!defined(cartesian)) {
  5970. throw new DeveloperError('cartesian is required');
  5971. }
  5972. if (!defined(result)) {
  5973. throw new DeveloperError('result is required');
  5974. }
  5975. var magnitude = Cartesian4.magnitude(cartesian);
  5976. result.x = cartesian.x / magnitude;
  5977. result.y = cartesian.y / magnitude;
  5978. result.z = cartesian.z / magnitude;
  5979. result.w = cartesian.w / magnitude;
  5980. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z) || isNaN(result.w)) {
  5981. throw new DeveloperError('normalized result is not a number');
  5982. }
  5983. return result;
  5984. };
  5985. /**
  5986. * Computes the dot (scalar) product of two Cartesians.
  5987. *
  5988. * @param {Cartesian4} left The first Cartesian.
  5989. * @param {Cartesian4} right The second Cartesian.
  5990. * @returns {Number} The dot product.
  5991. */
  5992. Cartesian4.dot = function(left, right) {
  5993. if (!defined(left)) {
  5994. throw new DeveloperError('left is required');
  5995. }
  5996. if (!defined(right)) {
  5997. throw new DeveloperError('right is required');
  5998. }
  5999. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  6000. };
  6001. /**
  6002. * Computes the componentwise product of two Cartesians.
  6003. *
  6004. * @param {Cartesian4} left The first Cartesian.
  6005. * @param {Cartesian4} right The second Cartesian.
  6006. * @param {Cartesian4} result The object onto which to store the result.
  6007. * @returns {Cartesian4} The modified result parameter.
  6008. */
  6009. Cartesian4.multiplyComponents = function(left, right, result) {
  6010. if (!defined(left)) {
  6011. throw new DeveloperError('left is required');
  6012. }
  6013. if (!defined(right)) {
  6014. throw new DeveloperError('right is required');
  6015. }
  6016. if (!defined(result)) {
  6017. throw new DeveloperError('result is required');
  6018. }
  6019. result.x = left.x * right.x;
  6020. result.y = left.y * right.y;
  6021. result.z = left.z * right.z;
  6022. result.w = left.w * right.w;
  6023. return result;
  6024. };
  6025. /**
  6026. * Computes the componentwise quotient of two Cartesians.
  6027. *
  6028. * @param {Cartesian4} left The first Cartesian.
  6029. * @param {Cartesian4} right The second Cartesian.
  6030. * @param {Cartesian4} result The object onto which to store the result.
  6031. * @returns {Cartesian4} The modified result parameter.
  6032. */
  6033. Cartesian4.divideComponents = function(left, right, result) {
  6034. if (!defined(left)) {
  6035. throw new DeveloperError('left is required');
  6036. }
  6037. if (!defined(right)) {
  6038. throw new DeveloperError('right is required');
  6039. }
  6040. if (!defined(result)) {
  6041. throw new DeveloperError('result is required');
  6042. }
  6043. result.x = left.x / right.x;
  6044. result.y = left.y / right.y;
  6045. result.z = left.z / right.z;
  6046. result.w = left.w / right.w;
  6047. return result;
  6048. };
  6049. /**
  6050. * Computes the componentwise sum of two Cartesians.
  6051. *
  6052. * @param {Cartesian4} left The first Cartesian.
  6053. * @param {Cartesian4} right The second Cartesian.
  6054. * @param {Cartesian4} result The object onto which to store the result.
  6055. * @returns {Cartesian4} The modified result parameter.
  6056. */
  6057. Cartesian4.add = function(left, right, result) {
  6058. if (!defined(left)) {
  6059. throw new DeveloperError('left is required');
  6060. }
  6061. if (!defined(right)) {
  6062. throw new DeveloperError('right is required');
  6063. }
  6064. if (!defined(result)) {
  6065. throw new DeveloperError('result is required');
  6066. }
  6067. result.x = left.x + right.x;
  6068. result.y = left.y + right.y;
  6069. result.z = left.z + right.z;
  6070. result.w = left.w + right.w;
  6071. return result;
  6072. };
  6073. /**
  6074. * Computes the componentwise difference of two Cartesians.
  6075. *
  6076. * @param {Cartesian4} left The first Cartesian.
  6077. * @param {Cartesian4} right The second Cartesian.
  6078. * @param {Cartesian4} result The object onto which to store the result.
  6079. * @returns {Cartesian4} The modified result parameter.
  6080. */
  6081. Cartesian4.subtract = function(left, right, result) {
  6082. if (!defined(left)) {
  6083. throw new DeveloperError('left is required');
  6084. }
  6085. if (!defined(right)) {
  6086. throw new DeveloperError('right is required');
  6087. }
  6088. if (!defined(result)) {
  6089. throw new DeveloperError('result is required');
  6090. }
  6091. result.x = left.x - right.x;
  6092. result.y = left.y - right.y;
  6093. result.z = left.z - right.z;
  6094. result.w = left.w - right.w;
  6095. return result;
  6096. };
  6097. /**
  6098. * Multiplies the provided Cartesian componentwise by the provided scalar.
  6099. *
  6100. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  6101. * @param {Number} scalar The scalar to multiply with.
  6102. * @param {Cartesian4} result The object onto which to store the result.
  6103. * @returns {Cartesian4} The modified result parameter.
  6104. */
  6105. Cartesian4.multiplyByScalar = function(cartesian, scalar, result) {
  6106. if (!defined(cartesian)) {
  6107. throw new DeveloperError('cartesian is required');
  6108. }
  6109. if (typeof scalar !== 'number') {
  6110. throw new DeveloperError('scalar is required and must be a number.');
  6111. }
  6112. if (!defined(result)) {
  6113. throw new DeveloperError('result is required');
  6114. }
  6115. result.x = cartesian.x * scalar;
  6116. result.y = cartesian.y * scalar;
  6117. result.z = cartesian.z * scalar;
  6118. result.w = cartesian.w * scalar;
  6119. return result;
  6120. };
  6121. /**
  6122. * Divides the provided Cartesian componentwise by the provided scalar.
  6123. *
  6124. * @param {Cartesian4} cartesian The Cartesian to be divided.
  6125. * @param {Number} scalar The scalar to divide by.
  6126. * @param {Cartesian4} result The object onto which to store the result.
  6127. * @returns {Cartesian4} The modified result parameter.
  6128. */
  6129. Cartesian4.divideByScalar = function(cartesian, scalar, result) {
  6130. if (!defined(cartesian)) {
  6131. throw new DeveloperError('cartesian is required');
  6132. }
  6133. if (typeof scalar !== 'number') {
  6134. throw new DeveloperError('scalar is required and must be a number.');
  6135. }
  6136. if (!defined(result)) {
  6137. throw new DeveloperError('result is required');
  6138. }
  6139. result.x = cartesian.x / scalar;
  6140. result.y = cartesian.y / scalar;
  6141. result.z = cartesian.z / scalar;
  6142. result.w = cartesian.w / scalar;
  6143. return result;
  6144. };
  6145. /**
  6146. * Negates the provided Cartesian.
  6147. *
  6148. * @param {Cartesian4} cartesian The Cartesian to be negated.
  6149. * @param {Cartesian4} result The object onto which to store the result.
  6150. * @returns {Cartesian4} The modified result parameter.
  6151. */
  6152. Cartesian4.negate = function(cartesian, result) {
  6153. if (!defined(cartesian)) {
  6154. throw new DeveloperError('cartesian is required');
  6155. }
  6156. if (!defined(result)) {
  6157. throw new DeveloperError('result is required');
  6158. }
  6159. result.x = -cartesian.x;
  6160. result.y = -cartesian.y;
  6161. result.z = -cartesian.z;
  6162. result.w = -cartesian.w;
  6163. return result;
  6164. };
  6165. /**
  6166. * Computes the absolute value of the provided Cartesian.
  6167. *
  6168. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  6169. * @param {Cartesian4} result The object onto which to store the result.
  6170. * @returns {Cartesian4} The modified result parameter.
  6171. */
  6172. Cartesian4.abs = function(cartesian, result) {
  6173. if (!defined(cartesian)) {
  6174. throw new DeveloperError('cartesian is required');
  6175. }
  6176. if (!defined(result)) {
  6177. throw new DeveloperError('result is required');
  6178. }
  6179. result.x = Math.abs(cartesian.x);
  6180. result.y = Math.abs(cartesian.y);
  6181. result.z = Math.abs(cartesian.z);
  6182. result.w = Math.abs(cartesian.w);
  6183. return result;
  6184. };
  6185. var lerpScratch = new Cartesian4();
  6186. /**
  6187. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  6188. *
  6189. * @param {Cartesian4} start The value corresponding to t at 0.0.
  6190. * @param {Cartesian4}end The value corresponding to t at 1.0.
  6191. * @param {Number} t The point along t at which to interpolate.
  6192. * @param {Cartesian4} result The object onto which to store the result.
  6193. * @returns {Cartesian4} The modified result parameter.
  6194. */
  6195. Cartesian4.lerp = function(start, end, t, result) {
  6196. if (!defined(start)) {
  6197. throw new DeveloperError('start is required.');
  6198. }
  6199. if (!defined(end)) {
  6200. throw new DeveloperError('end is required.');
  6201. }
  6202. if (typeof t !== 'number') {
  6203. throw new DeveloperError('t is required and must be a number.');
  6204. }
  6205. if (!defined(result)) {
  6206. throw new DeveloperError('result is required.');
  6207. }
  6208. Cartesian4.multiplyByScalar(end, t, lerpScratch);
  6209. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  6210. return Cartesian4.add(lerpScratch, result, result);
  6211. };
  6212. var mostOrthogonalAxisScratch = new Cartesian4();
  6213. /**
  6214. * Returns the axis that is most orthogonal to the provided Cartesian.
  6215. *
  6216. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  6217. * @param {Cartesian4} result The object onto which to store the result.
  6218. * @returns {Cartesian4} The most orthogonal axis.
  6219. */
  6220. Cartesian4.mostOrthogonalAxis = function(cartesian, result) {
  6221. if (!defined(cartesian)) {
  6222. throw new DeveloperError('cartesian is required.');
  6223. }
  6224. if (!defined(result)) {
  6225. throw new DeveloperError('result is required.');
  6226. }
  6227. var f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch);
  6228. Cartesian4.abs(f, f);
  6229. if (f.x <= f.y) {
  6230. if (f.x <= f.z) {
  6231. if (f.x <= f.w) {
  6232. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  6233. } else {
  6234. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6235. }
  6236. } else if (f.z <= f.w) {
  6237. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  6238. } else {
  6239. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6240. }
  6241. } else if (f.y <= f.z) {
  6242. if (f.y <= f.w) {
  6243. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  6244. } else {
  6245. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6246. }
  6247. } else if (f.z <= f.w) {
  6248. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  6249. } else {
  6250. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  6251. }
  6252. return result;
  6253. };
  6254. /**
  6255. * Compares the provided Cartesians componentwise and returns
  6256. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6257. *
  6258. * @param {Cartesian4} [left] The first Cartesian.
  6259. * @param {Cartesian4} [right] The second Cartesian.
  6260. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  6261. */
  6262. Cartesian4.equals = function(left, right) {
  6263. return (left === right) ||
  6264. ((defined(left)) &&
  6265. (defined(right)) &&
  6266. (left.x === right.x) &&
  6267. (left.y === right.y) &&
  6268. (left.z === right.z) &&
  6269. (left.w === right.w));
  6270. };
  6271. /**
  6272. * @private
  6273. */
  6274. Cartesian4.equalsArray = function(cartesian, array, offset) {
  6275. return cartesian.x === array[offset] &&
  6276. cartesian.y === array[offset + 1] &&
  6277. cartesian.z === array[offset + 2] &&
  6278. cartesian.w === array[offset + 3];
  6279. };
  6280. /**
  6281. * Compares the provided Cartesians componentwise and returns
  6282. * <code>true</code> if they pass an absolute or relative tolerance test,
  6283. * <code>false</code> otherwise.
  6284. *
  6285. * @param {Cartesian4} [left] The first Cartesian.
  6286. * @param {Cartesian4} [right] The second Cartesian.
  6287. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  6288. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  6289. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  6290. */
  6291. Cartesian4.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  6292. return (left === right) ||
  6293. (defined(left) &&
  6294. defined(right) &&
  6295. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  6296. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  6297. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon) &&
  6298. CesiumMath.equalsEpsilon(left.w, right.w, relativeEpsilon, absoluteEpsilon));
  6299. };
  6300. /**
  6301. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  6302. *
  6303. * @type {Cartesian4}
  6304. * @constant
  6305. */
  6306. Cartesian4.ZERO = freezeObject(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  6307. /**
  6308. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  6309. *
  6310. * @type {Cartesian4}
  6311. * @constant
  6312. */
  6313. Cartesian4.UNIT_X = freezeObject(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  6314. /**
  6315. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  6316. *
  6317. * @type {Cartesian4}
  6318. * @constant
  6319. */
  6320. Cartesian4.UNIT_Y = freezeObject(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  6321. /**
  6322. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  6323. *
  6324. * @type {Cartesian4}
  6325. * @constant
  6326. */
  6327. Cartesian4.UNIT_Z = freezeObject(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  6328. /**
  6329. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  6330. *
  6331. * @type {Cartesian4}
  6332. * @constant
  6333. */
  6334. Cartesian4.UNIT_W = freezeObject(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  6335. /**
  6336. * Duplicates this Cartesian4 instance.
  6337. *
  6338. * @param {Cartesian4} [result] The object onto which to store the result.
  6339. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  6340. */
  6341. Cartesian4.prototype.clone = function(result) {
  6342. return Cartesian4.clone(this, result);
  6343. };
  6344. /**
  6345. * Compares this Cartesian against the provided Cartesian componentwise and returns
  6346. * <code>true</code> if they are equal, <code>false</code> otherwise.
  6347. *
  6348. * @param {Cartesian4} [right] The right hand side Cartesian.
  6349. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  6350. */
  6351. Cartesian4.prototype.equals = function(right) {
  6352. return Cartesian4.equals(this, right);
  6353. };
  6354. /**
  6355. * Compares this Cartesian against the provided Cartesian componentwise and returns
  6356. * <code>true</code> if they pass an absolute or relative tolerance test,
  6357. * <code>false</code> otherwise.
  6358. *
  6359. * @param {Cartesian4} [right] The right hand side Cartesian.
  6360. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  6361. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  6362. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  6363. */
  6364. Cartesian4.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  6365. return Cartesian4.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  6366. };
  6367. /**
  6368. * Creates a string representing this Cartesian in the format '(x, y)'.
  6369. *
  6370. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  6371. */
  6372. Cartesian4.prototype.toString = function() {
  6373. return '(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  6374. };
  6375. return Cartesian4;
  6376. });
  6377. /*global define*/
  6378. define('Core/RuntimeError',[
  6379. './defined'
  6380. ], function(
  6381. defined) {
  6382. 'use strict';
  6383. /**
  6384. * Constructs an exception object that is thrown due to an error that can occur at runtime, e.g.,
  6385. * out of memory, could not compile shader, etc. If a function may throw this
  6386. * exception, the calling code should be prepared to catch it.
  6387. * <br /><br />
  6388. * On the other hand, a {@link DeveloperError} indicates an exception due
  6389. * to a developer error, e.g., invalid argument, that usually indicates a bug in the
  6390. * calling code.
  6391. *
  6392. * @alias RuntimeError
  6393. * @constructor
  6394. * @extends Error
  6395. *
  6396. * @param {String} [message] The error message for this exception.
  6397. *
  6398. * @see DeveloperError
  6399. */
  6400. function RuntimeError(message) {
  6401. /**
  6402. * 'RuntimeError' indicating that this exception was thrown due to a runtime error.
  6403. * @type {String}
  6404. * @readonly
  6405. */
  6406. this.name = 'RuntimeError';
  6407. /**
  6408. * The explanation for why this exception was thrown.
  6409. * @type {String}
  6410. * @readonly
  6411. */
  6412. this.message = message;
  6413. //Browsers such as IE don't have a stack property until you actually throw the error.
  6414. var stack;
  6415. try {
  6416. throw new Error();
  6417. } catch (e) {
  6418. stack = e.stack;
  6419. }
  6420. /**
  6421. * The stack trace of this exception, if available.
  6422. * @type {String}
  6423. * @readonly
  6424. */
  6425. this.stack = stack;
  6426. }
  6427. if (defined(Object.create)) {
  6428. RuntimeError.prototype = Object.create(Error.prototype);
  6429. RuntimeError.prototype.constructor = RuntimeError;
  6430. }
  6431. RuntimeError.prototype.toString = function() {
  6432. var str = this.name + ': ' + this.message;
  6433. if (defined(this.stack)) {
  6434. str += '\n' + this.stack.toString();
  6435. }
  6436. return str;
  6437. };
  6438. return RuntimeError;
  6439. });
  6440. /*global define*/
  6441. define('Core/Matrix4',[
  6442. './Cartesian3',
  6443. './Cartesian4',
  6444. './defaultValue',
  6445. './defined',
  6446. './defineProperties',
  6447. './DeveloperError',
  6448. './freezeObject',
  6449. './Math',
  6450. './Matrix3',
  6451. './RuntimeError'
  6452. ], function(
  6453. Cartesian3,
  6454. Cartesian4,
  6455. defaultValue,
  6456. defined,
  6457. defineProperties,
  6458. DeveloperError,
  6459. freezeObject,
  6460. CesiumMath,
  6461. Matrix3,
  6462. RuntimeError) {
  6463. 'use strict';
  6464. /**
  6465. * A 4x4 matrix, indexable as a column-major order array.
  6466. * Constructor parameters are in row-major order for code readability.
  6467. * @alias Matrix4
  6468. * @constructor
  6469. *
  6470. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  6471. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  6472. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  6473. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  6474. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  6475. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  6476. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  6477. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  6478. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  6479. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  6480. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  6481. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  6482. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  6483. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  6484. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  6485. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  6486. *
  6487. * @see Matrix4.fromColumnMajorArray
  6488. * @see Matrix4.fromRowMajorArray
  6489. * @see Matrix4.fromRotationTranslation
  6490. * @see Matrix4.fromTranslationRotationScale
  6491. * @see Matrix4.fromTranslationQuaternionRotationScale
  6492. * @see Matrix4.fromTranslation
  6493. * @see Matrix4.fromScale
  6494. * @see Matrix4.fromUniformScale
  6495. * @see Matrix4.fromCamera
  6496. * @see Matrix4.computePerspectiveFieldOfView
  6497. * @see Matrix4.computeOrthographicOffCenter
  6498. * @see Matrix4.computePerspectiveOffCenter
  6499. * @see Matrix4.computeInfinitePerspectiveOffCenter
  6500. * @see Matrix4.computeViewportTransformation
  6501. * @see Matrix4.computeView
  6502. * @see Matrix2
  6503. * @see Matrix3
  6504. * @see Packable
  6505. */
  6506. function Matrix4(column0Row0, column1Row0, column2Row0, column3Row0,
  6507. column0Row1, column1Row1, column2Row1, column3Row1,
  6508. column0Row2, column1Row2, column2Row2, column3Row2,
  6509. column0Row3, column1Row3, column2Row3, column3Row3) {
  6510. this[0] = defaultValue(column0Row0, 0.0);
  6511. this[1] = defaultValue(column0Row1, 0.0);
  6512. this[2] = defaultValue(column0Row2, 0.0);
  6513. this[3] = defaultValue(column0Row3, 0.0);
  6514. this[4] = defaultValue(column1Row0, 0.0);
  6515. this[5] = defaultValue(column1Row1, 0.0);
  6516. this[6] = defaultValue(column1Row2, 0.0);
  6517. this[7] = defaultValue(column1Row3, 0.0);
  6518. this[8] = defaultValue(column2Row0, 0.0);
  6519. this[9] = defaultValue(column2Row1, 0.0);
  6520. this[10] = defaultValue(column2Row2, 0.0);
  6521. this[11] = defaultValue(column2Row3, 0.0);
  6522. this[12] = defaultValue(column3Row0, 0.0);
  6523. this[13] = defaultValue(column3Row1, 0.0);
  6524. this[14] = defaultValue(column3Row2, 0.0);
  6525. this[15] = defaultValue(column3Row3, 0.0);
  6526. }
  6527. /**
  6528. * The number of elements used to pack the object into an array.
  6529. * @type {Number}
  6530. */
  6531. Matrix4.packedLength = 16;
  6532. /**
  6533. * Stores the provided instance into the provided array.
  6534. *
  6535. * @param {Matrix4} value The value to pack.
  6536. * @param {Number[]} array The array to pack into.
  6537. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  6538. *
  6539. * @returns {Number[]} The array that was packed into
  6540. */
  6541. Matrix4.pack = function(value, array, startingIndex) {
  6542. if (!defined(value)) {
  6543. throw new DeveloperError('value is required');
  6544. }
  6545. if (!defined(array)) {
  6546. throw new DeveloperError('array is required');
  6547. }
  6548. startingIndex = defaultValue(startingIndex, 0);
  6549. array[startingIndex++] = value[0];
  6550. array[startingIndex++] = value[1];
  6551. array[startingIndex++] = value[2];
  6552. array[startingIndex++] = value[3];
  6553. array[startingIndex++] = value[4];
  6554. array[startingIndex++] = value[5];
  6555. array[startingIndex++] = value[6];
  6556. array[startingIndex++] = value[7];
  6557. array[startingIndex++] = value[8];
  6558. array[startingIndex++] = value[9];
  6559. array[startingIndex++] = value[10];
  6560. array[startingIndex++] = value[11];
  6561. array[startingIndex++] = value[12];
  6562. array[startingIndex++] = value[13];
  6563. array[startingIndex++] = value[14];
  6564. array[startingIndex] = value[15];
  6565. return array;
  6566. };
  6567. /**
  6568. * Retrieves an instance from a packed array.
  6569. *
  6570. * @param {Number[]} array The packed array.
  6571. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  6572. * @param {Matrix4} [result] The object into which to store the result.
  6573. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  6574. */
  6575. Matrix4.unpack = function(array, startingIndex, result) {
  6576. if (!defined(array)) {
  6577. throw new DeveloperError('array is required');
  6578. }
  6579. startingIndex = defaultValue(startingIndex, 0);
  6580. if (!defined(result)) {
  6581. result = new Matrix4();
  6582. }
  6583. result[0] = array[startingIndex++];
  6584. result[1] = array[startingIndex++];
  6585. result[2] = array[startingIndex++];
  6586. result[3] = array[startingIndex++];
  6587. result[4] = array[startingIndex++];
  6588. result[5] = array[startingIndex++];
  6589. result[6] = array[startingIndex++];
  6590. result[7] = array[startingIndex++];
  6591. result[8] = array[startingIndex++];
  6592. result[9] = array[startingIndex++];
  6593. result[10] = array[startingIndex++];
  6594. result[11] = array[startingIndex++];
  6595. result[12] = array[startingIndex++];
  6596. result[13] = array[startingIndex++];
  6597. result[14] = array[startingIndex++];
  6598. result[15] = array[startingIndex];
  6599. return result;
  6600. };
  6601. /**
  6602. * Duplicates a Matrix4 instance.
  6603. *
  6604. * @param {Matrix4} matrix The matrix to duplicate.
  6605. * @param {Matrix4} [result] The object onto which to store the result.
  6606. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  6607. */
  6608. Matrix4.clone = function(matrix, result) {
  6609. if (!defined(matrix)) {
  6610. return undefined;
  6611. }
  6612. if (!defined(result)) {
  6613. return new Matrix4(matrix[0], matrix[4], matrix[8], matrix[12],
  6614. matrix[1], matrix[5], matrix[9], matrix[13],
  6615. matrix[2], matrix[6], matrix[10], matrix[14],
  6616. matrix[3], matrix[7], matrix[11], matrix[15]);
  6617. }
  6618. result[0] = matrix[0];
  6619. result[1] = matrix[1];
  6620. result[2] = matrix[2];
  6621. result[3] = matrix[3];
  6622. result[4] = matrix[4];
  6623. result[5] = matrix[5];
  6624. result[6] = matrix[6];
  6625. result[7] = matrix[7];
  6626. result[8] = matrix[8];
  6627. result[9] = matrix[9];
  6628. result[10] = matrix[10];
  6629. result[11] = matrix[11];
  6630. result[12] = matrix[12];
  6631. result[13] = matrix[13];
  6632. result[14] = matrix[14];
  6633. result[15] = matrix[15];
  6634. return result;
  6635. };
  6636. /**
  6637. * Creates a Matrix4 from 16 consecutive elements in an array.
  6638. * @function
  6639. *
  6640. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  6641. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  6642. * @param {Matrix4} [result] The object onto which to store the result.
  6643. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  6644. *
  6645. * @example
  6646. * // Create the Matrix4:
  6647. * // [1.0, 2.0, 3.0, 4.0]
  6648. * // [1.0, 2.0, 3.0, 4.0]
  6649. * // [1.0, 2.0, 3.0, 4.0]
  6650. * // [1.0, 2.0, 3.0, 4.0]
  6651. *
  6652. * 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];
  6653. * var m = Cesium.Matrix4.fromArray(v);
  6654. *
  6655. * // Create same Matrix4 with using an offset into an array
  6656. * 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];
  6657. * var m2 = Cesium.Matrix4.fromArray(v2, 2);
  6658. */
  6659. Matrix4.fromArray = Matrix4.unpack;
  6660. /**
  6661. * Computes a Matrix4 instance from a column-major order array.
  6662. *
  6663. * @param {Number[]} values The column-major order array.
  6664. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6665. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6666. */
  6667. Matrix4.fromColumnMajorArray = function(values, result) {
  6668. if (!defined(values)) {
  6669. throw new DeveloperError('values is required');
  6670. }
  6671. return Matrix4.clone(values, result);
  6672. };
  6673. /**
  6674. * Computes a Matrix4 instance from a row-major order array.
  6675. * The resulting matrix will be in column-major order.
  6676. *
  6677. * @param {Number[]} values The row-major order array.
  6678. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6679. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6680. */
  6681. Matrix4.fromRowMajorArray = function(values, result) {
  6682. if (!defined(values)) {
  6683. throw new DeveloperError('values is required.');
  6684. }
  6685. if (!defined(result)) {
  6686. return new Matrix4(values[0], values[1], values[2], values[3],
  6687. values[4], values[5], values[6], values[7],
  6688. values[8], values[9], values[10], values[11],
  6689. values[12], values[13], values[14], values[15]);
  6690. }
  6691. result[0] = values[0];
  6692. result[1] = values[4];
  6693. result[2] = values[8];
  6694. result[3] = values[12];
  6695. result[4] = values[1];
  6696. result[5] = values[5];
  6697. result[6] = values[9];
  6698. result[7] = values[13];
  6699. result[8] = values[2];
  6700. result[9] = values[6];
  6701. result[10] = values[10];
  6702. result[11] = values[14];
  6703. result[12] = values[3];
  6704. result[13] = values[7];
  6705. result[14] = values[11];
  6706. result[15] = values[15];
  6707. return result;
  6708. };
  6709. /**
  6710. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  6711. * and a Cartesian3 representing the translation.
  6712. *
  6713. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  6714. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  6715. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6716. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6717. */
  6718. Matrix4.fromRotationTranslation = function(rotation, translation, result) {
  6719. if (!defined(rotation)) {
  6720. throw new DeveloperError('rotation is required.');
  6721. }
  6722. translation = defaultValue(translation, Cartesian3.ZERO);
  6723. if (!defined(result)) {
  6724. return new Matrix4(rotation[0], rotation[3], rotation[6], translation.x,
  6725. rotation[1], rotation[4], rotation[7], translation.y,
  6726. rotation[2], rotation[5], rotation[8], translation.z,
  6727. 0.0, 0.0, 0.0, 1.0);
  6728. }
  6729. result[0] = rotation[0];
  6730. result[1] = rotation[1];
  6731. result[2] = rotation[2];
  6732. result[3] = 0.0;
  6733. result[4] = rotation[3];
  6734. result[5] = rotation[4];
  6735. result[6] = rotation[5];
  6736. result[7] = 0.0;
  6737. result[8] = rotation[6];
  6738. result[9] = rotation[7];
  6739. result[10] = rotation[8];
  6740. result[11] = 0.0;
  6741. result[12] = translation.x;
  6742. result[13] = translation.y;
  6743. result[14] = translation.z;
  6744. result[15] = 1.0;
  6745. return result;
  6746. };
  6747. /**
  6748. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  6749. * representation with the rotation represented as a quaternion.
  6750. *
  6751. * @param {Cartesian3} translation The translation transformation.
  6752. * @param {Quaternion} rotation The rotation transformation.
  6753. * @param {Cartesian3} scale The non-uniform scale transformation.
  6754. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6755. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6756. *
  6757. * @example
  6758. * var result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  6759. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  6760. * Cesium.Quaternion.IDENTITY, // rotation
  6761. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  6762. * result);
  6763. */
  6764. Matrix4.fromTranslationQuaternionRotationScale = function(translation, rotation, scale, result) {
  6765. if (!defined(translation)) {
  6766. throw new DeveloperError('translation is required.');
  6767. }
  6768. if (!defined(rotation)) {
  6769. throw new DeveloperError('rotation is required.');
  6770. }
  6771. if (!defined(scale)) {
  6772. throw new DeveloperError('scale is required.');
  6773. }
  6774. if (!defined(result)) {
  6775. result = new Matrix4();
  6776. }
  6777. var scaleX = scale.x;
  6778. var scaleY = scale.y;
  6779. var scaleZ = scale.z;
  6780. var x2 = rotation.x * rotation.x;
  6781. var xy = rotation.x * rotation.y;
  6782. var xz = rotation.x * rotation.z;
  6783. var xw = rotation.x * rotation.w;
  6784. var y2 = rotation.y * rotation.y;
  6785. var yz = rotation.y * rotation.z;
  6786. var yw = rotation.y * rotation.w;
  6787. var z2 = rotation.z * rotation.z;
  6788. var zw = rotation.z * rotation.w;
  6789. var w2 = rotation.w * rotation.w;
  6790. var m00 = x2 - y2 - z2 + w2;
  6791. var m01 = 2.0 * (xy - zw);
  6792. var m02 = 2.0 * (xz + yw);
  6793. var m10 = 2.0 * (xy + zw);
  6794. var m11 = -x2 + y2 - z2 + w2;
  6795. var m12 = 2.0 * (yz - xw);
  6796. var m20 = 2.0 * (xz - yw);
  6797. var m21 = 2.0 * (yz + xw);
  6798. var m22 = -x2 - y2 + z2 + w2;
  6799. result[0] = m00 * scaleX;
  6800. result[1] = m10 * scaleX;
  6801. result[2] = m20 * scaleX;
  6802. result[3] = 0.0;
  6803. result[4] = m01 * scaleY;
  6804. result[5] = m11 * scaleY;
  6805. result[6] = m21 * scaleY;
  6806. result[7] = 0.0;
  6807. result[8] = m02 * scaleZ;
  6808. result[9] = m12 * scaleZ;
  6809. result[10] = m22 * scaleZ;
  6810. result[11] = 0.0;
  6811. result[12] = translation.x;
  6812. result[13] = translation.y;
  6813. result[14] = translation.z;
  6814. result[15] = 1.0;
  6815. return result;
  6816. };
  6817. /**
  6818. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  6819. *
  6820. * @param {TranslationRotationScale} translationRotationScale The instance.
  6821. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6822. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6823. */
  6824. Matrix4.fromTranslationRotationScale = function(translationRotationScale, result) {
  6825. if (!defined(translationRotationScale)) {
  6826. throw new DeveloperError('translationRotationScale is required.');
  6827. }
  6828. return Matrix4.fromTranslationQuaternionRotationScale(translationRotationScale.translation, translationRotationScale.rotation, translationRotationScale.scale, result);
  6829. };
  6830. /**
  6831. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  6832. *
  6833. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  6834. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6835. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6836. *
  6837. * @see Matrix4.multiplyByTranslation
  6838. */
  6839. Matrix4.fromTranslation = function(translation, result) {
  6840. if (!defined(translation)) {
  6841. throw new DeveloperError('translation is required.');
  6842. }
  6843. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  6844. };
  6845. /**
  6846. * Computes a Matrix4 instance representing a non-uniform scale.
  6847. *
  6848. * @param {Cartesian3} scale The x, y, and z scale factors.
  6849. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6850. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6851. *
  6852. * @example
  6853. * // Creates
  6854. * // [7.0, 0.0, 0.0, 0.0]
  6855. * // [0.0, 8.0, 0.0, 0.0]
  6856. * // [0.0, 0.0, 9.0, 0.0]
  6857. * // [0.0, 0.0, 0.0, 1.0]
  6858. * var m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  6859. */
  6860. Matrix4.fromScale = function(scale, result) {
  6861. if (!defined(scale)) {
  6862. throw new DeveloperError('scale is required.');
  6863. }
  6864. if (!defined(result)) {
  6865. return new Matrix4(
  6866. scale.x, 0.0, 0.0, 0.0,
  6867. 0.0, scale.y, 0.0, 0.0,
  6868. 0.0, 0.0, scale.z, 0.0,
  6869. 0.0, 0.0, 0.0, 1.0);
  6870. }
  6871. result[0] = scale.x;
  6872. result[1] = 0.0;
  6873. result[2] = 0.0;
  6874. result[3] = 0.0;
  6875. result[4] = 0.0;
  6876. result[5] = scale.y;
  6877. result[6] = 0.0;
  6878. result[7] = 0.0;
  6879. result[8] = 0.0;
  6880. result[9] = 0.0;
  6881. result[10] = scale.z;
  6882. result[11] = 0.0;
  6883. result[12] = 0.0;
  6884. result[13] = 0.0;
  6885. result[14] = 0.0;
  6886. result[15] = 1.0;
  6887. return result;
  6888. };
  6889. /**
  6890. * Computes a Matrix4 instance representing a uniform scale.
  6891. *
  6892. * @param {Number} scale The uniform scale factor.
  6893. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6894. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6895. *
  6896. * @example
  6897. * // Creates
  6898. * // [2.0, 0.0, 0.0, 0.0]
  6899. * // [0.0, 2.0, 0.0, 0.0]
  6900. * // [0.0, 0.0, 2.0, 0.0]
  6901. * // [0.0, 0.0, 0.0, 1.0]
  6902. * var m = Cesium.Matrix4.fromUniformScale(2.0);
  6903. */
  6904. Matrix4.fromUniformScale = function(scale, result) {
  6905. if (typeof scale !== 'number') {
  6906. throw new DeveloperError('scale is required.');
  6907. }
  6908. if (!defined(result)) {
  6909. return new Matrix4(scale, 0.0, 0.0, 0.0,
  6910. 0.0, scale, 0.0, 0.0,
  6911. 0.0, 0.0, scale, 0.0,
  6912. 0.0, 0.0, 0.0, 1.0);
  6913. }
  6914. result[0] = scale;
  6915. result[1] = 0.0;
  6916. result[2] = 0.0;
  6917. result[3] = 0.0;
  6918. result[4] = 0.0;
  6919. result[5] = scale;
  6920. result[6] = 0.0;
  6921. result[7] = 0.0;
  6922. result[8] = 0.0;
  6923. result[9] = 0.0;
  6924. result[10] = scale;
  6925. result[11] = 0.0;
  6926. result[12] = 0.0;
  6927. result[13] = 0.0;
  6928. result[14] = 0.0;
  6929. result[15] = 1.0;
  6930. return result;
  6931. };
  6932. var fromCameraF = new Cartesian3();
  6933. var fromCameraR = new Cartesian3();
  6934. var fromCameraU = new Cartesian3();
  6935. /**
  6936. * Computes a Matrix4 instance from a Camera.
  6937. *
  6938. * @param {Camera} camera The camera to use.
  6939. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  6940. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  6941. */
  6942. Matrix4.fromCamera = function(camera, result) {
  6943. if (!defined(camera)) {
  6944. throw new DeveloperError('camera is required.');
  6945. }
  6946. var position = camera.position;
  6947. var direction = camera.direction;
  6948. var up = camera.up;
  6949. if (!defined(position)) {
  6950. throw new DeveloperError('camera.position is required.');
  6951. }
  6952. if (!defined(direction)) {
  6953. throw new DeveloperError('camera.direction is required.');
  6954. }
  6955. if (!defined(up)) {
  6956. throw new DeveloperError('camera.up is required.');
  6957. }
  6958. Cartesian3.normalize(direction, fromCameraF);
  6959. Cartesian3.normalize(Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR);
  6960. Cartesian3.normalize(Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU);
  6961. var sX = fromCameraR.x;
  6962. var sY = fromCameraR.y;
  6963. var sZ = fromCameraR.z;
  6964. var fX = fromCameraF.x;
  6965. var fY = fromCameraF.y;
  6966. var fZ = fromCameraF.z;
  6967. var uX = fromCameraU.x;
  6968. var uY = fromCameraU.y;
  6969. var uZ = fromCameraU.z;
  6970. var positionX = position.x;
  6971. var positionY = position.y;
  6972. var positionZ = position.z;
  6973. var t0 = sX * -positionX + sY * -positionY+ sZ * -positionZ;
  6974. var t1 = uX * -positionX + uY * -positionY+ uZ * -positionZ;
  6975. var t2 = fX * positionX + fY * positionY + fZ * positionZ;
  6976. // The code below this comment is an optimized
  6977. // version of the commented lines.
  6978. // Rather that create two matrices and then multiply,
  6979. // we just bake in the multiplcation as part of creation.
  6980. // var rotation = new Matrix4(
  6981. // sX, sY, sZ, 0.0,
  6982. // uX, uY, uZ, 0.0,
  6983. // -fX, -fY, -fZ, 0.0,
  6984. // 0.0, 0.0, 0.0, 1.0);
  6985. // var translation = new Matrix4(
  6986. // 1.0, 0.0, 0.0, -position.x,
  6987. // 0.0, 1.0, 0.0, -position.y,
  6988. // 0.0, 0.0, 1.0, -position.z,
  6989. // 0.0, 0.0, 0.0, 1.0);
  6990. // return rotation.multiply(translation);
  6991. if (!defined(result)) {
  6992. return new Matrix4(
  6993. sX, sY, sZ, t0,
  6994. uX, uY, uZ, t1,
  6995. -fX, -fY, -fZ, t2,
  6996. 0.0, 0.0, 0.0, 1.0);
  6997. }
  6998. result[0] = sX;
  6999. result[1] = uX;
  7000. result[2] = -fX;
  7001. result[3] = 0.0;
  7002. result[4] = sY;
  7003. result[5] = uY;
  7004. result[6] = -fY;
  7005. result[7] = 0.0;
  7006. result[8] = sZ;
  7007. result[9] = uZ;
  7008. result[10] = -fZ;
  7009. result[11] = 0.0;
  7010. result[12] = t0;
  7011. result[13] = t1;
  7012. result[14] = t2;
  7013. result[15] = 1.0;
  7014. return result;
  7015. };
  7016. /**
  7017. * Computes a Matrix4 instance representing a perspective transformation matrix.
  7018. *
  7019. * @param {Number} fovY The field of view along the Y axis in radians.
  7020. * @param {Number} aspectRatio The aspect ratio.
  7021. * @param {Number} near The distance to the near plane in meters.
  7022. * @param {Number} far The distance to the far plane in meters.
  7023. * @param {Matrix4} result The object in which the result will be stored.
  7024. * @returns {Matrix4} The modified result parameter.
  7025. *
  7026. * @exception {DeveloperError} fovY must be in (0, PI].
  7027. * @exception {DeveloperError} aspectRatio must be greater than zero.
  7028. * @exception {DeveloperError} near must be greater than zero.
  7029. * @exception {DeveloperError} far must be greater than zero.
  7030. */
  7031. Matrix4.computePerspectiveFieldOfView = function(fovY, aspectRatio, near, far, result) {
  7032. if (fovY <= 0.0 || fovY > Math.PI) {
  7033. throw new DeveloperError('fovY must be in (0, PI].');
  7034. }
  7035. if (aspectRatio <= 0.0) {
  7036. throw new DeveloperError('aspectRatio must be greater than zero.');
  7037. }
  7038. if (near <= 0.0) {
  7039. throw new DeveloperError('near must be greater than zero.');
  7040. }
  7041. if (far <= 0.0) {
  7042. throw new DeveloperError('far must be greater than zero.');
  7043. }
  7044. if (!defined(result)) {
  7045. throw new DeveloperError('result is required');
  7046. }
  7047. var bottom = Math.tan(fovY * 0.5);
  7048. var column1Row1 = 1.0 / bottom;
  7049. var column0Row0 = column1Row1 / aspectRatio;
  7050. var column2Row2 = (far + near) / (near - far);
  7051. var column3Row2 = (2.0 * far * near) / (near - far);
  7052. result[0] = column0Row0;
  7053. result[1] = 0.0;
  7054. result[2] = 0.0;
  7055. result[3] = 0.0;
  7056. result[4] = 0.0;
  7057. result[5] = column1Row1;
  7058. result[6] = 0.0;
  7059. result[7] = 0.0;
  7060. result[8] = 0.0;
  7061. result[9] = 0.0;
  7062. result[10] = column2Row2;
  7063. result[11] = -1.0;
  7064. result[12] = 0.0;
  7065. result[13] = 0.0;
  7066. result[14] = column3Row2;
  7067. result[15] = 0.0;
  7068. return result;
  7069. };
  7070. /**
  7071. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  7072. *
  7073. * @param {Number} left The number of meters to the left of the camera that will be in view.
  7074. * @param {Number} right The number of meters to the right of the camera that will be in view.
  7075. * @param {Number} bottom The number of meters below of the camera that will be in view.
  7076. * @param {Number} top The number of meters above of the camera that will be in view.
  7077. * @param {Number} near The distance to the near plane in meters.
  7078. * @param {Number} far The distance to the far plane in meters.
  7079. * @param {Matrix4} result The object in which the result will be stored.
  7080. * @returns {Matrix4} The modified result parameter.
  7081. */
  7082. Matrix4.computeOrthographicOffCenter = function(left, right, bottom, top, near, far, result) {
  7083. if (!defined(left)) {
  7084. throw new DeveloperError('left is required.');
  7085. }
  7086. if (!defined(right)) {
  7087. throw new DeveloperError('right is required.');
  7088. }
  7089. if (!defined(bottom)) {
  7090. throw new DeveloperError('bottom is required.');
  7091. }
  7092. if (!defined(top)) {
  7093. throw new DeveloperError('top is required.');
  7094. }
  7095. if (!defined(near)) {
  7096. throw new DeveloperError('near is required.');
  7097. }
  7098. if (!defined(far)) {
  7099. throw new DeveloperError('far is required.');
  7100. }
  7101. if (!defined(result)) {
  7102. throw new DeveloperError('result is required');
  7103. }
  7104. var a = 1.0 / (right - left);
  7105. var b = 1.0 / (top - bottom);
  7106. var c = 1.0 / (far - near);
  7107. var tx = -(right + left) * a;
  7108. var ty = -(top + bottom) * b;
  7109. var tz = -(far + near) * c;
  7110. a *= 2.0;
  7111. b *= 2.0;
  7112. c *= -2.0;
  7113. result[0] = a;
  7114. result[1] = 0.0;
  7115. result[2] = 0.0;
  7116. result[3] = 0.0;
  7117. result[4] = 0.0;
  7118. result[5] = b;
  7119. result[6] = 0.0;
  7120. result[7] = 0.0;
  7121. result[8] = 0.0;
  7122. result[9] = 0.0;
  7123. result[10] = c;
  7124. result[11] = 0.0;
  7125. result[12] = tx;
  7126. result[13] = ty;
  7127. result[14] = tz;
  7128. result[15] = 1.0;
  7129. return result;
  7130. };
  7131. /**
  7132. * Computes a Matrix4 instance representing an off center perspective transformation.
  7133. *
  7134. * @param {Number} left The number of meters to the left of the camera that will be in view.
  7135. * @param {Number} right The number of meters to the right of the camera that will be in view.
  7136. * @param {Number} bottom The number of meters below of the camera that will be in view.
  7137. * @param {Number} top The number of meters above of the camera that will be in view.
  7138. * @param {Number} near The distance to the near plane in meters.
  7139. * @param {Number} far The distance to the far plane in meters.
  7140. * @param {Matrix4} result The object in which the result will be stored.
  7141. * @returns {Matrix4} The modified result parameter.
  7142. */
  7143. Matrix4.computePerspectiveOffCenter = function(left, right, bottom, top, near, far, result) {
  7144. if (!defined(left)) {
  7145. throw new DeveloperError('left is required.');
  7146. }
  7147. if (!defined(right)) {
  7148. throw new DeveloperError('right is required.');
  7149. }
  7150. if (!defined(bottom)) {
  7151. throw new DeveloperError('bottom is required.');
  7152. }
  7153. if (!defined(top)) {
  7154. throw new DeveloperError('top is required.');
  7155. }
  7156. if (!defined(near)) {
  7157. throw new DeveloperError('near is required.');
  7158. }
  7159. if (!defined(far)) {
  7160. throw new DeveloperError('far is required.');
  7161. }
  7162. if (!defined(result)) {
  7163. throw new DeveloperError('result is required');
  7164. }
  7165. var column0Row0 = 2.0 * near / (right - left);
  7166. var column1Row1 = 2.0 * near / (top - bottom);
  7167. var column2Row0 = (right + left) / (right - left);
  7168. var column2Row1 = (top + bottom) / (top - bottom);
  7169. var column2Row2 = -(far + near) / (far - near);
  7170. var column2Row3 = -1.0;
  7171. var column3Row2 = -2.0 * far * near / (far - near);
  7172. result[0] = column0Row0;
  7173. result[1] = 0.0;
  7174. result[2] = 0.0;
  7175. result[3] = 0.0;
  7176. result[4] = 0.0;
  7177. result[5] = column1Row1;
  7178. result[6] = 0.0;
  7179. result[7] = 0.0;
  7180. result[8] = column2Row0;
  7181. result[9] = column2Row1;
  7182. result[10] = column2Row2;
  7183. result[11] = column2Row3;
  7184. result[12] = 0.0;
  7185. result[13] = 0.0;
  7186. result[14] = column3Row2;
  7187. result[15] = 0.0;
  7188. return result;
  7189. };
  7190. /**
  7191. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  7192. *
  7193. * @param {Number} left The number of meters to the left of the camera that will be in view.
  7194. * @param {Number} right The number of meters to the right of the camera that will be in view.
  7195. * @param {Number} bottom The number of meters below of the camera that will be in view.
  7196. * @param {Number} top The number of meters above of the camera that will be in view.
  7197. * @param {Number} near The distance to the near plane in meters.
  7198. * @param {Matrix4} result The object in which the result will be stored.
  7199. * @returns {Matrix4} The modified result parameter.
  7200. */
  7201. Matrix4.computeInfinitePerspectiveOffCenter = function(left, right, bottom, top, near, result) {
  7202. if (!defined(left)) {
  7203. throw new DeveloperError('left is required.');
  7204. }
  7205. if (!defined(right)) {
  7206. throw new DeveloperError('right is required.');
  7207. }
  7208. if (!defined(bottom)) {
  7209. throw new DeveloperError('bottom is required.');
  7210. }
  7211. if (!defined(top)) {
  7212. throw new DeveloperError('top is required.');
  7213. }
  7214. if (!defined(near)) {
  7215. throw new DeveloperError('near is required.');
  7216. }
  7217. if (!defined(result)) {
  7218. throw new DeveloperError('result is required');
  7219. }
  7220. var column0Row0 = 2.0 * near / (right - left);
  7221. var column1Row1 = 2.0 * near / (top - bottom);
  7222. var column2Row0 = (right + left) / (right - left);
  7223. var column2Row1 = (top + bottom) / (top - bottom);
  7224. var column2Row2 = -1.0;
  7225. var column2Row3 = -1.0;
  7226. var column3Row2 = -2.0 * near;
  7227. result[0] = column0Row0;
  7228. result[1] = 0.0;
  7229. result[2] = 0.0;
  7230. result[3] = 0.0;
  7231. result[4] = 0.0;
  7232. result[5] = column1Row1;
  7233. result[6] = 0.0;
  7234. result[7] = 0.0;
  7235. result[8] = column2Row0;
  7236. result[9] = column2Row1;
  7237. result[10] = column2Row2;
  7238. result[11] = column2Row3;
  7239. result[12] = 0.0;
  7240. result[13] = 0.0;
  7241. result[14] = column3Row2;
  7242. result[15] = 0.0;
  7243. return result;
  7244. };
  7245. /**
  7246. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  7247. *
  7248. * @param {Object}[viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  7249. * @param {Number}[nearDepthRange=0.0] The near plane distance in window coordinates.
  7250. * @param {Number}[farDepthRange=1.0] The far plane distance in window coordinates.
  7251. * @param {Matrix4} result The object in which the result will be stored.
  7252. * @returns {Matrix4} The modified result parameter.
  7253. *
  7254. * @example
  7255. * // Create viewport transformation using an explicit viewport and depth range.
  7256. * var m = Cesium.Matrix4.computeViewportTransformation({
  7257. * x : 0.0,
  7258. * y : 0.0,
  7259. * width : 1024.0,
  7260. * height : 768.0
  7261. * }, 0.0, 1.0, new Cesium.Matrix4());
  7262. */
  7263. Matrix4.computeViewportTransformation = function(viewport, nearDepthRange, farDepthRange, result) {
  7264. if (!defined(result)) {
  7265. throw new DeveloperError('result is required');
  7266. }
  7267. viewport = defaultValue(viewport, defaultValue.EMPTY_OBJECT);
  7268. var x = defaultValue(viewport.x, 0.0);
  7269. var y = defaultValue(viewport.y, 0.0);
  7270. var width = defaultValue(viewport.width, 0.0);
  7271. var height = defaultValue(viewport.height, 0.0);
  7272. nearDepthRange = defaultValue(nearDepthRange, 0.0);
  7273. farDepthRange = defaultValue(farDepthRange, 1.0);
  7274. var halfWidth = width * 0.5;
  7275. var halfHeight = height * 0.5;
  7276. var halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  7277. var column0Row0 = halfWidth;
  7278. var column1Row1 = halfHeight;
  7279. var column2Row2 = halfDepth;
  7280. var column3Row0 = x + halfWidth;
  7281. var column3Row1 = y + halfHeight;
  7282. var column3Row2 = nearDepthRange + halfDepth;
  7283. var column3Row3 = 1.0;
  7284. result[0] = column0Row0;
  7285. result[1] = 0.0;
  7286. result[2] = 0.0;
  7287. result[3] = 0.0;
  7288. result[4] = 0.0;
  7289. result[5] = column1Row1;
  7290. result[6] = 0.0;
  7291. result[7] = 0.0;
  7292. result[8] = 0.0;
  7293. result[9] = 0.0;
  7294. result[10] = column2Row2;
  7295. result[11] = 0.0;
  7296. result[12] = column3Row0;
  7297. result[13] = column3Row1;
  7298. result[14] = column3Row2;
  7299. result[15] = column3Row3;
  7300. return result;
  7301. };
  7302. /**
  7303. * Computes a Matrix4 instance that transforms from world space to view space.
  7304. *
  7305. * @param {Cartesian3} position The position of the camera.
  7306. * @param {Cartesian3} direction The forward direction.
  7307. * @param {Cartesian3} up The up direction.
  7308. * @param {Cartesian3} right The right direction.
  7309. * @param {Matrix4} result The object in which the result will be stored.
  7310. * @returns {Matrix4} The modified result parameter.
  7311. */
  7312. Matrix4.computeView = function(position, direction, up, right, result) {
  7313. if (!defined(position)) {
  7314. throw new DeveloperError('position is required');
  7315. }
  7316. if (!defined(direction)) {
  7317. throw new DeveloperError('direction is required');
  7318. }
  7319. if (!defined(up)) {
  7320. throw new DeveloperError('up is required');
  7321. }
  7322. if (!defined(right)) {
  7323. throw new DeveloperError('right is required');
  7324. }
  7325. if (!defined(result)) {
  7326. throw new DeveloperError('result is required');
  7327. }
  7328. result[0] = right.x;
  7329. result[1] = up.x;
  7330. result[2] = -direction.x;
  7331. result[3] = 0.0;
  7332. result[4] = right.y;
  7333. result[5] = up.y;
  7334. result[6] = -direction.y;
  7335. result[7] = 0.0;
  7336. result[8] = right.z;
  7337. result[9] = up.z;
  7338. result[10] = -direction.z;
  7339. result[11] = 0.0;
  7340. result[12] = -Cartesian3.dot(right, position);
  7341. result[13] = -Cartesian3.dot(up, position);
  7342. result[14] = Cartesian3.dot(direction, position);
  7343. result[15] = 1.0;
  7344. return result;
  7345. };
  7346. /**
  7347. * Computes an Array from the provided Matrix4 instance.
  7348. * The array will be in column-major order.
  7349. *
  7350. * @param {Matrix4} matrix The matrix to use..
  7351. * @param {Number[]} [result] The Array onto which to store the result.
  7352. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  7353. *
  7354. * @example
  7355. * //create an array from an instance of Matrix4
  7356. * // m = [10.0, 14.0, 18.0, 22.0]
  7357. * // [11.0, 15.0, 19.0, 23.0]
  7358. * // [12.0, 16.0, 20.0, 24.0]
  7359. * // [13.0, 17.0, 21.0, 25.0]
  7360. * var a = Cesium.Matrix4.toArray(m);
  7361. *
  7362. * // m remains the same
  7363. * //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]
  7364. */
  7365. Matrix4.toArray = function(matrix, result) {
  7366. if (!defined(matrix)) {
  7367. throw new DeveloperError('matrix is required');
  7368. }
  7369. if (!defined(result)) {
  7370. return [matrix[0], matrix[1], matrix[2], matrix[3],
  7371. matrix[4], matrix[5], matrix[6], matrix[7],
  7372. matrix[8], matrix[9], matrix[10], matrix[11],
  7373. matrix[12], matrix[13], matrix[14], matrix[15]];
  7374. }
  7375. result[0] = matrix[0];
  7376. result[1] = matrix[1];
  7377. result[2] = matrix[2];
  7378. result[3] = matrix[3];
  7379. result[4] = matrix[4];
  7380. result[5] = matrix[5];
  7381. result[6] = matrix[6];
  7382. result[7] = matrix[7];
  7383. result[8] = matrix[8];
  7384. result[9] = matrix[9];
  7385. result[10] = matrix[10];
  7386. result[11] = matrix[11];
  7387. result[12] = matrix[12];
  7388. result[13] = matrix[13];
  7389. result[14] = matrix[14];
  7390. result[15] = matrix[15];
  7391. return result;
  7392. };
  7393. /**
  7394. * Computes the array index of the element at the provided row and column.
  7395. *
  7396. * @param {Number} row The zero-based index of the row.
  7397. * @param {Number} column The zero-based index of the column.
  7398. * @returns {Number} The index of the element at the provided row and column.
  7399. *
  7400. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  7401. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  7402. *
  7403. * @example
  7404. * var myMatrix = new Cesium.Matrix4();
  7405. * var column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  7406. * var column1Row0 = myMatrix[column1Row0Index];
  7407. * myMatrix[column1Row0Index] = 10.0;
  7408. */
  7409. Matrix4.getElementIndex = function(column, row) {
  7410. if (typeof row !== 'number' || row < 0 || row > 3) {
  7411. throw new DeveloperError('row must be 0, 1, 2, or 3.');
  7412. }
  7413. if (typeof column !== 'number' || column < 0 || column > 3) {
  7414. throw new DeveloperError('column must be 0, 1, 2, or 3.');
  7415. }
  7416. return column * 4 + row;
  7417. };
  7418. /**
  7419. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  7420. *
  7421. * @param {Matrix4} matrix The matrix to use.
  7422. * @param {Number} index The zero-based index of the column to retrieve.
  7423. * @param {Cartesian4} result The object onto which to store the result.
  7424. * @returns {Cartesian4} The modified result parameter.
  7425. *
  7426. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7427. *
  7428. * @example
  7429. * //returns a Cartesian4 instance with values from the specified column
  7430. * // m = [10.0, 11.0, 12.0, 13.0]
  7431. * // [14.0, 15.0, 16.0, 17.0]
  7432. * // [18.0, 19.0, 20.0, 21.0]
  7433. * // [22.0, 23.0, 24.0, 25.0]
  7434. *
  7435. * //Example 1: Creates an instance of Cartesian
  7436. * var a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  7437. *
  7438. * @example
  7439. * //Example 2: Sets values for Cartesian instance
  7440. * var a = new Cesium.Cartesian4();
  7441. * Cesium.Matrix4.getColumn(m, 2, a);
  7442. *
  7443. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  7444. */
  7445. Matrix4.getColumn = function(matrix, index, result) {
  7446. if (!defined(matrix)) {
  7447. throw new DeveloperError('matrix is required.');
  7448. }
  7449. if (typeof index !== 'number' || index < 0 || index > 3) {
  7450. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7451. }
  7452. if (!defined(result)) {
  7453. throw new DeveloperError('result is required');
  7454. }
  7455. var startIndex = index * 4;
  7456. var x = matrix[startIndex];
  7457. var y = matrix[startIndex + 1];
  7458. var z = matrix[startIndex + 2];
  7459. var w = matrix[startIndex + 3];
  7460. result.x = x;
  7461. result.y = y;
  7462. result.z = z;
  7463. result.w = w;
  7464. return result;
  7465. };
  7466. /**
  7467. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  7468. *
  7469. * @param {Matrix4} matrix The matrix to use.
  7470. * @param {Number} index The zero-based index of the column to set.
  7471. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  7472. * @param {Matrix4} result The object onto which to store the result.
  7473. * @returns {Matrix4} The modified result parameter.
  7474. *
  7475. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7476. *
  7477. * @example
  7478. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  7479. * // m = [10.0, 11.0, 12.0, 13.0]
  7480. * // [14.0, 15.0, 16.0, 17.0]
  7481. * // [18.0, 19.0, 20.0, 21.0]
  7482. * // [22.0, 23.0, 24.0, 25.0]
  7483. *
  7484. * var a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  7485. *
  7486. * // m remains the same
  7487. * // a = [10.0, 11.0, 99.0, 13.0]
  7488. * // [14.0, 15.0, 98.0, 17.0]
  7489. * // [18.0, 19.0, 97.0, 21.0]
  7490. * // [22.0, 23.0, 96.0, 25.0]
  7491. */
  7492. Matrix4.setColumn = function(matrix, index, cartesian, result) {
  7493. if (!defined(matrix)) {
  7494. throw new DeveloperError('matrix is required');
  7495. }
  7496. if (!defined(cartesian)) {
  7497. throw new DeveloperError('cartesian is required');
  7498. }
  7499. if (typeof index !== 'number' || index < 0 || index > 3) {
  7500. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7501. }
  7502. if (!defined(result)) {
  7503. throw new DeveloperError('result is required');
  7504. }
  7505. result = Matrix4.clone(matrix, result);
  7506. var startIndex = index * 4;
  7507. result[startIndex] = cartesian.x;
  7508. result[startIndex + 1] = cartesian.y;
  7509. result[startIndex + 2] = cartesian.z;
  7510. result[startIndex + 3] = cartesian.w;
  7511. return result;
  7512. };
  7513. /**
  7514. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  7515. * matrix with the provided translation. This assumes the matrix is an affine transformation
  7516. *
  7517. * @param {Matrix4} matrix The matrix to use.
  7518. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  7519. * @param {Cartesian4} result The object onto which to store the result.
  7520. * @returns {Matrix4} The modified result parameter.
  7521. */
  7522. Matrix4.setTranslation = function(matrix, translation, result) {
  7523. if (!defined(matrix)) {
  7524. throw new DeveloperError('matrix is required');
  7525. }
  7526. if (!defined(translation)) {
  7527. throw new DeveloperError('translation is required');
  7528. }
  7529. if (!defined(result)) {
  7530. throw new DeveloperError('result is required');
  7531. }
  7532. result[0] = matrix[0];
  7533. result[1] = matrix[1];
  7534. result[2] = matrix[2];
  7535. result[3] = matrix[3];
  7536. result[4] = matrix[4];
  7537. result[5] = matrix[5];
  7538. result[6] = matrix[6];
  7539. result[7] = matrix[7];
  7540. result[8] = matrix[8];
  7541. result[9] = matrix[9];
  7542. result[10] = matrix[10];
  7543. result[11] = matrix[11];
  7544. result[12] = translation.x;
  7545. result[13] = translation.y;
  7546. result[14] = translation.z;
  7547. result[15] = matrix[15];
  7548. return result;
  7549. };
  7550. /**
  7551. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  7552. *
  7553. * @param {Matrix4} matrix The matrix to use.
  7554. * @param {Number} index The zero-based index of the row to retrieve.
  7555. * @param {Cartesian4} result The object onto which to store the result.
  7556. * @returns {Cartesian4} The modified result parameter.
  7557. *
  7558. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7559. *
  7560. * @example
  7561. * //returns a Cartesian4 instance with values from the specified column
  7562. * // m = [10.0, 11.0, 12.0, 13.0]
  7563. * // [14.0, 15.0, 16.0, 17.0]
  7564. * // [18.0, 19.0, 20.0, 21.0]
  7565. * // [22.0, 23.0, 24.0, 25.0]
  7566. *
  7567. * //Example 1: Returns an instance of Cartesian
  7568. * var a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  7569. *
  7570. * @example
  7571. * //Example 2: Sets values for a Cartesian instance
  7572. * var a = new Cesium.Cartesian4();
  7573. * Cesium.Matrix4.getRow(m, 2, a);
  7574. *
  7575. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  7576. */
  7577. Matrix4.getRow = function(matrix, index, result) {
  7578. if (!defined(matrix)) {
  7579. throw new DeveloperError('matrix is required.');
  7580. }
  7581. if (typeof index !== 'number' || index < 0 || index > 3) {
  7582. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7583. }
  7584. if (!defined(result)) {
  7585. throw new DeveloperError('result is required');
  7586. }
  7587. var x = matrix[index];
  7588. var y = matrix[index + 4];
  7589. var z = matrix[index + 8];
  7590. var w = matrix[index + 12];
  7591. result.x = x;
  7592. result.y = y;
  7593. result.z = z;
  7594. result.w = w;
  7595. return result;
  7596. };
  7597. /**
  7598. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  7599. *
  7600. * @param {Matrix4} matrix The matrix to use.
  7601. * @param {Number} index The zero-based index of the row to set.
  7602. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  7603. * @param {Matrix4} result The object onto which to store the result.
  7604. * @returns {Matrix4} The modified result parameter.
  7605. *
  7606. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  7607. *
  7608. * @example
  7609. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  7610. * // m = [10.0, 11.0, 12.0, 13.0]
  7611. * // [14.0, 15.0, 16.0, 17.0]
  7612. * // [18.0, 19.0, 20.0, 21.0]
  7613. * // [22.0, 23.0, 24.0, 25.0]
  7614. *
  7615. * var a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  7616. *
  7617. * // m remains the same
  7618. * // a = [10.0, 11.0, 12.0, 13.0]
  7619. * // [14.0, 15.0, 16.0, 17.0]
  7620. * // [99.0, 98.0, 97.0, 96.0]
  7621. * // [22.0, 23.0, 24.0, 25.0]
  7622. */
  7623. Matrix4.setRow = function(matrix, index, cartesian, result) {
  7624. if (!defined(matrix)) {
  7625. throw new DeveloperError('matrix is required');
  7626. }
  7627. if (!defined(cartesian)) {
  7628. throw new DeveloperError('cartesian is required');
  7629. }
  7630. if (typeof index !== 'number' || index < 0 || index > 3) {
  7631. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  7632. }
  7633. if (!defined(result)) {
  7634. throw new DeveloperError('result is required');
  7635. }
  7636. result = Matrix4.clone(matrix, result);
  7637. result[index] = cartesian.x;
  7638. result[index + 4] = cartesian.y;
  7639. result[index + 8] = cartesian.z;
  7640. result[index + 12] = cartesian.w;
  7641. return result;
  7642. };
  7643. var scratchColumn = new Cartesian3();
  7644. /**
  7645. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  7646. *
  7647. * @param {Matrix4} matrix The matrix.
  7648. * @param {Cartesian3} result The object onto which to store the result.
  7649. * @returns {Cartesian3} The modified result parameter
  7650. */
  7651. Matrix4.getScale = function(matrix, result) {
  7652. if (!defined(matrix)) {
  7653. throw new DeveloperError('matrix is required.');
  7654. }
  7655. if (!defined(result)) {
  7656. throw new DeveloperError('result is required');
  7657. }
  7658. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  7659. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn));
  7660. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn));
  7661. return result;
  7662. };
  7663. var scratchScale = new Cartesian3();
  7664. /**
  7665. * Computes the maximum scale assuming the matrix is an affine transformation.
  7666. * The maximum scale is the maximum length of the column vectors in the upper-left
  7667. * 3x3 matrix.
  7668. *
  7669. * @param {Matrix4} matrix The matrix.
  7670. * @returns {Number} The maximum scale.
  7671. */
  7672. Matrix4.getMaximumScale = function(matrix) {
  7673. Matrix4.getScale(matrix, scratchScale);
  7674. return Cartesian3.maximumComponent(scratchScale);
  7675. };
  7676. /**
  7677. * Computes the product of two matrices.
  7678. *
  7679. * @param {Matrix4} left The first matrix.
  7680. * @param {Matrix4} right The second matrix.
  7681. * @param {Matrix4} result The object onto which to store the result.
  7682. * @returns {Matrix4} The modified result parameter.
  7683. */
  7684. Matrix4.multiply = function(left, right, result) {
  7685. if (!defined(left)) {
  7686. throw new DeveloperError('left is required');
  7687. }
  7688. if (!defined(right)) {
  7689. throw new DeveloperError('right is required');
  7690. }
  7691. if (!defined(result)) {
  7692. throw new DeveloperError('result is required');
  7693. }
  7694. var left0 = left[0];
  7695. var left1 = left[1];
  7696. var left2 = left[2];
  7697. var left3 = left[3];
  7698. var left4 = left[4];
  7699. var left5 = left[5];
  7700. var left6 = left[6];
  7701. var left7 = left[7];
  7702. var left8 = left[8];
  7703. var left9 = left[9];
  7704. var left10 = left[10];
  7705. var left11 = left[11];
  7706. var left12 = left[12];
  7707. var left13 = left[13];
  7708. var left14 = left[14];
  7709. var left15 = left[15];
  7710. var right0 = right[0];
  7711. var right1 = right[1];
  7712. var right2 = right[2];
  7713. var right3 = right[3];
  7714. var right4 = right[4];
  7715. var right5 = right[5];
  7716. var right6 = right[6];
  7717. var right7 = right[7];
  7718. var right8 = right[8];
  7719. var right9 = right[9];
  7720. var right10 = right[10];
  7721. var right11 = right[11];
  7722. var right12 = right[12];
  7723. var right13 = right[13];
  7724. var right14 = right[14];
  7725. var right15 = right[15];
  7726. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  7727. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  7728. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  7729. var column0Row3 = left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  7730. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  7731. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  7732. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  7733. var column1Row3 = left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  7734. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  7735. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  7736. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  7737. var column2Row3 = left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  7738. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  7739. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  7740. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  7741. var column3Row3 = left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  7742. result[0] = column0Row0;
  7743. result[1] = column0Row1;
  7744. result[2] = column0Row2;
  7745. result[3] = column0Row3;
  7746. result[4] = column1Row0;
  7747. result[5] = column1Row1;
  7748. result[6] = column1Row2;
  7749. result[7] = column1Row3;
  7750. result[8] = column2Row0;
  7751. result[9] = column2Row1;
  7752. result[10] = column2Row2;
  7753. result[11] = column2Row3;
  7754. result[12] = column3Row0;
  7755. result[13] = column3Row1;
  7756. result[14] = column3Row2;
  7757. result[15] = column3Row3;
  7758. return result;
  7759. };
  7760. /**
  7761. * Computes the sum of two matrices.
  7762. *
  7763. * @param {Matrix4} left The first matrix.
  7764. * @param {Matrix4} right The second matrix.
  7765. * @param {Matrix4} result The object onto which to store the result.
  7766. * @returns {Matrix4} The modified result parameter.
  7767. */
  7768. Matrix4.add = function(left, right, result) {
  7769. if (!defined(left)) {
  7770. throw new DeveloperError('left is required');
  7771. }
  7772. if (!defined(right)) {
  7773. throw new DeveloperError('right is required');
  7774. }
  7775. if (!defined(result)) {
  7776. throw new DeveloperError('result is required');
  7777. }
  7778. result[0] = left[0] + right[0];
  7779. result[1] = left[1] + right[1];
  7780. result[2] = left[2] + right[2];
  7781. result[3] = left[3] + right[3];
  7782. result[4] = left[4] + right[4];
  7783. result[5] = left[5] + right[5];
  7784. result[6] = left[6] + right[6];
  7785. result[7] = left[7] + right[7];
  7786. result[8] = left[8] + right[8];
  7787. result[9] = left[9] + right[9];
  7788. result[10] = left[10] + right[10];
  7789. result[11] = left[11] + right[11];
  7790. result[12] = left[12] + right[12];
  7791. result[13] = left[13] + right[13];
  7792. result[14] = left[14] + right[14];
  7793. result[15] = left[15] + right[15];
  7794. return result;
  7795. };
  7796. /**
  7797. * Computes the difference of two matrices.
  7798. *
  7799. * @param {Matrix4} left The first matrix.
  7800. * @param {Matrix4} right The second matrix.
  7801. * @param {Matrix4} result The object onto which to store the result.
  7802. * @returns {Matrix4} The modified result parameter.
  7803. */
  7804. Matrix4.subtract = function(left, right, result) {
  7805. if (!defined(left)) {
  7806. throw new DeveloperError('left is required');
  7807. }
  7808. if (!defined(right)) {
  7809. throw new DeveloperError('right is required');
  7810. }
  7811. if (!defined(result)) {
  7812. throw new DeveloperError('result is required');
  7813. }
  7814. result[0] = left[0] - right[0];
  7815. result[1] = left[1] - right[1];
  7816. result[2] = left[2] - right[2];
  7817. result[3] = left[3] - right[3];
  7818. result[4] = left[4] - right[4];
  7819. result[5] = left[5] - right[5];
  7820. result[6] = left[6] - right[6];
  7821. result[7] = left[7] - right[7];
  7822. result[8] = left[8] - right[8];
  7823. result[9] = left[9] - right[9];
  7824. result[10] = left[10] - right[10];
  7825. result[11] = left[11] - right[11];
  7826. result[12] = left[12] - right[12];
  7827. result[13] = left[13] - right[13];
  7828. result[14] = left[14] - right[14];
  7829. result[15] = left[15] - right[15];
  7830. return result;
  7831. };
  7832. /**
  7833. * Computes the product of two matrices assuming the matrices are
  7834. * affine transformation matrices, where the upper left 3x3 elements
  7835. * are a rotation matrix, and the upper three elements in the fourth
  7836. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  7837. * The matrix is not verified to be in the proper form.
  7838. * This method is faster than computing the product for general 4x4
  7839. * matrices using {@link Matrix4.multiply}.
  7840. *
  7841. * @param {Matrix4} left The first matrix.
  7842. * @param {Matrix4} right The second matrix.
  7843. * @param {Matrix4} result The object onto which to store the result.
  7844. * @returns {Matrix4} The modified result parameter.
  7845. *
  7846. * @example
  7847. * 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);
  7848. * var m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  7849. * var m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  7850. */
  7851. Matrix4.multiplyTransformation = function(left, right, result) {
  7852. if (!defined(left)) {
  7853. throw new DeveloperError('left is required');
  7854. }
  7855. if (!defined(right)) {
  7856. throw new DeveloperError('right is required');
  7857. }
  7858. if (!defined(result)) {
  7859. throw new DeveloperError('result is required');
  7860. }
  7861. var left0 = left[0];
  7862. var left1 = left[1];
  7863. var left2 = left[2];
  7864. var left4 = left[4];
  7865. var left5 = left[5];
  7866. var left6 = left[6];
  7867. var left8 = left[8];
  7868. var left9 = left[9];
  7869. var left10 = left[10];
  7870. var left12 = left[12];
  7871. var left13 = left[13];
  7872. var left14 = left[14];
  7873. var right0 = right[0];
  7874. var right1 = right[1];
  7875. var right2 = right[2];
  7876. var right4 = right[4];
  7877. var right5 = right[5];
  7878. var right6 = right[6];
  7879. var right8 = right[8];
  7880. var right9 = right[9];
  7881. var right10 = right[10];
  7882. var right12 = right[12];
  7883. var right13 = right[13];
  7884. var right14 = right[14];
  7885. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  7886. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  7887. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  7888. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  7889. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  7890. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  7891. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  7892. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  7893. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  7894. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12;
  7895. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13;
  7896. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14;
  7897. result[0] = column0Row0;
  7898. result[1] = column0Row1;
  7899. result[2] = column0Row2;
  7900. result[3] = 0.0;
  7901. result[4] = column1Row0;
  7902. result[5] = column1Row1;
  7903. result[6] = column1Row2;
  7904. result[7] = 0.0;
  7905. result[8] = column2Row0;
  7906. result[9] = column2Row1;
  7907. result[10] = column2Row2;
  7908. result[11] = 0.0;
  7909. result[12] = column3Row0;
  7910. result[13] = column3Row1;
  7911. result[14] = column3Row2;
  7912. result[15] = 1.0;
  7913. return result;
  7914. };
  7915. /**
  7916. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7917. * by a 3x3 rotation matrix. This is an optimization
  7918. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  7919. *
  7920. * @param {Matrix4} matrix The matrix on the left-hand side.
  7921. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  7922. * @param {Matrix4} result The object onto which to store the result.
  7923. * @returns {Matrix4} The modified result parameter.
  7924. *
  7925. * @example
  7926. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  7927. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  7928. */
  7929. Matrix4.multiplyByMatrix3 = function(matrix, rotation, result) {
  7930. if (!defined(matrix)) {
  7931. throw new DeveloperError('matrix is required');
  7932. }
  7933. if (!defined(rotation)) {
  7934. throw new DeveloperError('rotation is required');
  7935. }
  7936. if (!defined(result)) {
  7937. throw new DeveloperError('result is required');
  7938. }
  7939. var left0 = matrix[0];
  7940. var left1 = matrix[1];
  7941. var left2 = matrix[2];
  7942. var left4 = matrix[4];
  7943. var left5 = matrix[5];
  7944. var left6 = matrix[6];
  7945. var left8 = matrix[8];
  7946. var left9 = matrix[9];
  7947. var left10 = matrix[10];
  7948. var right0 = rotation[0];
  7949. var right1 = rotation[1];
  7950. var right2 = rotation[2];
  7951. var right4 = rotation[3];
  7952. var right5 = rotation[4];
  7953. var right6 = rotation[5];
  7954. var right8 = rotation[6];
  7955. var right9 = rotation[7];
  7956. var right10 = rotation[8];
  7957. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  7958. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  7959. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  7960. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  7961. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  7962. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  7963. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  7964. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  7965. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  7966. result[0] = column0Row0;
  7967. result[1] = column0Row1;
  7968. result[2] = column0Row2;
  7969. result[3] = 0.0;
  7970. result[4] = column1Row0;
  7971. result[5] = column1Row1;
  7972. result[6] = column1Row2;
  7973. result[7] = 0.0;
  7974. result[8] = column2Row0;
  7975. result[9] = column2Row1;
  7976. result[10] = column2Row2;
  7977. result[11] = 0.0;
  7978. result[12] = matrix[12];
  7979. result[13] = matrix[13];
  7980. result[14] = matrix[14];
  7981. result[15] = matrix[15];
  7982. return result;
  7983. };
  7984. /**
  7985. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7986. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  7987. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  7988. *
  7989. * @param {Matrix4} matrix The matrix on the left-hand side.
  7990. * @param {Cartesian3} translation The translation on the right-hand side.
  7991. * @param {Matrix4} result The object onto which to store the result.
  7992. * @returns {Matrix4} The modified result parameter.
  7993. *
  7994. * @example
  7995. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  7996. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  7997. */
  7998. Matrix4.multiplyByTranslation = function(matrix, translation, result) {
  7999. if (!defined(matrix)) {
  8000. throw new DeveloperError('matrix is required');
  8001. }
  8002. if (!defined(translation)) {
  8003. throw new DeveloperError('translation is required');
  8004. }
  8005. if (!defined(result)) {
  8006. throw new DeveloperError('result is required');
  8007. }
  8008. var x = translation.x;
  8009. var y = translation.y;
  8010. var z = translation.z;
  8011. var tx = (x * matrix[0]) + (y * matrix[4]) + (z * matrix[8]) + matrix[12];
  8012. var ty = (x * matrix[1]) + (y * matrix[5]) + (z * matrix[9]) + matrix[13];
  8013. var tz = (x * matrix[2]) + (y * matrix[6]) + (z * matrix[10]) + matrix[14];
  8014. result[0] = matrix[0];
  8015. result[1] = matrix[1];
  8016. result[2] = matrix[2];
  8017. result[3] = matrix[3];
  8018. result[4] = matrix[4];
  8019. result[5] = matrix[5];
  8020. result[6] = matrix[6];
  8021. result[7] = matrix[7];
  8022. result[8] = matrix[8];
  8023. result[9] = matrix[9];
  8024. result[10] = matrix[10];
  8025. result[11] = matrix[11];
  8026. result[12] = tx;
  8027. result[13] = ty;
  8028. result[14] = tz;
  8029. result[15] = matrix[15];
  8030. return result;
  8031. };
  8032. var uniformScaleScratch = new Cartesian3();
  8033. /**
  8034. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  8035. * by an implicit uniform scale matrix. This is an optimization
  8036. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  8037. * <code>m</code> must be an affine matrix.
  8038. * This function performs fewer allocations and arithmetic operations.
  8039. *
  8040. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  8041. * @param {Number} scale The uniform scale on the right-hand side.
  8042. * @param {Matrix4} result The object onto which to store the result.
  8043. * @returns {Matrix4} The modified result parameter.
  8044. *
  8045. *
  8046. * @example
  8047. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  8048. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  8049. *
  8050. * @see Matrix4.fromUniformScale
  8051. * @see Matrix4.multiplyByScale
  8052. */
  8053. Matrix4.multiplyByUniformScale = function(matrix, scale, result) {
  8054. if (!defined(matrix)) {
  8055. throw new DeveloperError('matrix is required');
  8056. }
  8057. if (typeof scale !== 'number') {
  8058. throw new DeveloperError('scale is required');
  8059. }
  8060. if (!defined(result)) {
  8061. throw new DeveloperError('result is required');
  8062. }
  8063. uniformScaleScratch.x = scale;
  8064. uniformScaleScratch.y = scale;
  8065. uniformScaleScratch.z = scale;
  8066. return Matrix4.multiplyByScale(matrix, uniformScaleScratch, result);
  8067. };
  8068. /**
  8069. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  8070. * by an implicit non-uniform scale matrix. This is an optimization
  8071. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  8072. * <code>m</code> must be an affine matrix.
  8073. * This function performs fewer allocations and arithmetic operations.
  8074. *
  8075. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  8076. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  8077. * @param {Matrix4} result The object onto which to store the result.
  8078. * @returns {Matrix4} The modified result parameter.
  8079. *
  8080. *
  8081. * @example
  8082. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  8083. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  8084. *
  8085. * @see Matrix4.fromScale
  8086. * @see Matrix4.multiplyByUniformScale
  8087. */
  8088. Matrix4.multiplyByScale = function(matrix, scale, result) {
  8089. if (!defined(matrix)) {
  8090. throw new DeveloperError('matrix is required');
  8091. }
  8092. if (!defined(scale)) {
  8093. throw new DeveloperError('scale is required');
  8094. }
  8095. if (!defined(result)) {
  8096. throw new DeveloperError('result is required');
  8097. }
  8098. var scaleX = scale.x;
  8099. var scaleY = scale.y;
  8100. var scaleZ = scale.z;
  8101. // Faster than Cartesian3.equals
  8102. if ((scaleX === 1.0) && (scaleY === 1.0) && (scaleZ === 1.0)) {
  8103. return Matrix4.clone(matrix, result);
  8104. }
  8105. result[0] = scaleX * matrix[0];
  8106. result[1] = scaleX * matrix[1];
  8107. result[2] = scaleX * matrix[2];
  8108. result[3] = 0.0;
  8109. result[4] = scaleY * matrix[4];
  8110. result[5] = scaleY * matrix[5];
  8111. result[6] = scaleY * matrix[6];
  8112. result[7] = 0.0;
  8113. result[8] = scaleZ * matrix[8];
  8114. result[9] = scaleZ * matrix[9];
  8115. result[10] = scaleZ * matrix[10];
  8116. result[11] = 0.0;
  8117. result[12] = matrix[12];
  8118. result[13] = matrix[13];
  8119. result[14] = matrix[14];
  8120. result[15] = 1.0;
  8121. return result;
  8122. };
  8123. /**
  8124. * Computes the product of a matrix and a column vector.
  8125. *
  8126. * @param {Matrix4} matrix The matrix.
  8127. * @param {Cartesian4} cartesian The vector.
  8128. * @param {Cartesian4} result The object onto which to store the result.
  8129. * @returns {Cartesian4} The modified result parameter.
  8130. */
  8131. Matrix4.multiplyByVector = function(matrix, cartesian, result) {
  8132. if (!defined(matrix)) {
  8133. throw new DeveloperError('matrix is required');
  8134. }
  8135. if (!defined(cartesian)) {
  8136. throw new DeveloperError('cartesian is required');
  8137. }
  8138. if (!defined(result)) {
  8139. throw new DeveloperError('result is required');
  8140. }
  8141. var vX = cartesian.x;
  8142. var vY = cartesian.y;
  8143. var vZ = cartesian.z;
  8144. var vW = cartesian.w;
  8145. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  8146. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  8147. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  8148. var w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  8149. result.x = x;
  8150. result.y = y;
  8151. result.z = z;
  8152. result.w = w;
  8153. return result;
  8154. };
  8155. /**
  8156. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  8157. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  8158. *
  8159. * @param {Matrix4} matrix The matrix.
  8160. * @param {Cartesian3} cartesian The point.
  8161. * @param {Cartesian3} result The object onto which to store the result.
  8162. * @returns {Cartesian3} The modified result parameter.
  8163. *
  8164. * @example
  8165. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  8166. * var result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  8167. * // A shortcut for
  8168. * // Cartesian3 p = ...
  8169. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  8170. */
  8171. Matrix4.multiplyByPointAsVector = function(matrix, cartesian, result) {
  8172. if (!defined(matrix)) {
  8173. throw new DeveloperError('matrix is required');
  8174. }
  8175. if (!defined(cartesian)) {
  8176. throw new DeveloperError('cartesian is required');
  8177. }
  8178. if (!defined(result)) {
  8179. throw new DeveloperError('result is required');
  8180. }
  8181. var vX = cartesian.x;
  8182. var vY = cartesian.y;
  8183. var vZ = cartesian.z;
  8184. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  8185. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  8186. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  8187. result.x = x;
  8188. result.y = y;
  8189. result.z = z;
  8190. return result;
  8191. };
  8192. /**
  8193. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  8194. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  8195. *
  8196. * @param {Matrix4} matrix The matrix.
  8197. * @param {Cartesian3} cartesian The point.
  8198. * @param {Cartesian3} result The object onto which to store the result.
  8199. * @returns {Cartesian3} The modified result parameter.
  8200. *
  8201. * @example
  8202. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  8203. * var result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  8204. */
  8205. Matrix4.multiplyByPoint = function(matrix, cartesian, result) {
  8206. if (!defined(matrix)) {
  8207. throw new DeveloperError('matrix is required');
  8208. }
  8209. if (!defined(cartesian)) {
  8210. throw new DeveloperError('cartesian is required');
  8211. }
  8212. if (!defined(result)) {
  8213. throw new DeveloperError('result is required');
  8214. }
  8215. var vX = cartesian.x;
  8216. var vY = cartesian.y;
  8217. var vZ = cartesian.z;
  8218. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  8219. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  8220. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  8221. result.x = x;
  8222. result.y = y;
  8223. result.z = z;
  8224. return result;
  8225. };
  8226. /**
  8227. * Computes the product of a matrix and a scalar.
  8228. *
  8229. * @param {Matrix4} matrix The matrix.
  8230. * @param {Number} scalar The number to multiply by.
  8231. * @param {Matrix4} result The object onto which to store the result.
  8232. * @returns {Matrix4} The modified result parameter.
  8233. *
  8234. * @example
  8235. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  8236. * // m = [10.0, 11.0, 12.0, 13.0]
  8237. * // [14.0, 15.0, 16.0, 17.0]
  8238. * // [18.0, 19.0, 20.0, 21.0]
  8239. * // [22.0, 23.0, 24.0, 25.0]
  8240. *
  8241. * var a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  8242. *
  8243. * // m remains the same
  8244. * // a = [-20.0, -22.0, -24.0, -26.0]
  8245. * // [-28.0, -30.0, -32.0, -34.0]
  8246. * // [-36.0, -38.0, -40.0, -42.0]
  8247. * // [-44.0, -46.0, -48.0, -50.0]
  8248. */
  8249. Matrix4.multiplyByScalar = function(matrix, scalar, result) {
  8250. if (!defined(matrix)) {
  8251. throw new DeveloperError('matrix is required');
  8252. }
  8253. if (typeof scalar !== 'number') {
  8254. throw new DeveloperError('scalar must be a number');
  8255. }
  8256. if (!defined(result)) {
  8257. throw new DeveloperError('result is required');
  8258. }
  8259. result[0] = matrix[0] * scalar;
  8260. result[1] = matrix[1] * scalar;
  8261. result[2] = matrix[2] * scalar;
  8262. result[3] = matrix[3] * scalar;
  8263. result[4] = matrix[4] * scalar;
  8264. result[5] = matrix[5] * scalar;
  8265. result[6] = matrix[6] * scalar;
  8266. result[7] = matrix[7] * scalar;
  8267. result[8] = matrix[8] * scalar;
  8268. result[9] = matrix[9] * scalar;
  8269. result[10] = matrix[10] * scalar;
  8270. result[11] = matrix[11] * scalar;
  8271. result[12] = matrix[12] * scalar;
  8272. result[13] = matrix[13] * scalar;
  8273. result[14] = matrix[14] * scalar;
  8274. result[15] = matrix[15] * scalar;
  8275. return result;
  8276. };
  8277. /**
  8278. * Computes a negated copy of the provided matrix.
  8279. *
  8280. * @param {Matrix4} matrix The matrix to negate.
  8281. * @param {Matrix4} result The object onto which to store the result.
  8282. * @returns {Matrix4} The modified result parameter.
  8283. *
  8284. * @example
  8285. * //create a new Matrix4 instance which is a negation of a Matrix4
  8286. * // m = [10.0, 11.0, 12.0, 13.0]
  8287. * // [14.0, 15.0, 16.0, 17.0]
  8288. * // [18.0, 19.0, 20.0, 21.0]
  8289. * // [22.0, 23.0, 24.0, 25.0]
  8290. *
  8291. * var a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  8292. *
  8293. * // m remains the same
  8294. * // a = [-10.0, -11.0, -12.0, -13.0]
  8295. * // [-14.0, -15.0, -16.0, -17.0]
  8296. * // [-18.0, -19.0, -20.0, -21.0]
  8297. * // [-22.0, -23.0, -24.0, -25.0]
  8298. */
  8299. Matrix4.negate = function(matrix, result) {
  8300. if (!defined(matrix)) {
  8301. throw new DeveloperError('matrix is required');
  8302. }
  8303. if (!defined(result)) {
  8304. throw new DeveloperError('result is required');
  8305. }
  8306. result[0] = -matrix[0];
  8307. result[1] = -matrix[1];
  8308. result[2] = -matrix[2];
  8309. result[3] = -matrix[3];
  8310. result[4] = -matrix[4];
  8311. result[5] = -matrix[5];
  8312. result[6] = -matrix[6];
  8313. result[7] = -matrix[7];
  8314. result[8] = -matrix[8];
  8315. result[9] = -matrix[9];
  8316. result[10] = -matrix[10];
  8317. result[11] = -matrix[11];
  8318. result[12] = -matrix[12];
  8319. result[13] = -matrix[13];
  8320. result[14] = -matrix[14];
  8321. result[15] = -matrix[15];
  8322. return result;
  8323. };
  8324. /**
  8325. * Computes the transpose of the provided matrix.
  8326. *
  8327. * @param {Matrix4} matrix The matrix to transpose.
  8328. * @param {Matrix4} result The object onto which to store the result.
  8329. * @returns {Matrix4} The modified result parameter.
  8330. *
  8331. * @example
  8332. * //returns transpose of a Matrix4
  8333. * // m = [10.0, 11.0, 12.0, 13.0]
  8334. * // [14.0, 15.0, 16.0, 17.0]
  8335. * // [18.0, 19.0, 20.0, 21.0]
  8336. * // [22.0, 23.0, 24.0, 25.0]
  8337. *
  8338. * var a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  8339. *
  8340. * // m remains the same
  8341. * // a = [10.0, 14.0, 18.0, 22.0]
  8342. * // [11.0, 15.0, 19.0, 23.0]
  8343. * // [12.0, 16.0, 20.0, 24.0]
  8344. * // [13.0, 17.0, 21.0, 25.0]
  8345. */
  8346. Matrix4.transpose = function(matrix, result) {
  8347. if (!defined(matrix)) {
  8348. throw new DeveloperError('matrix is required');
  8349. }
  8350. if (!defined(result)) {
  8351. throw new DeveloperError('result is required');
  8352. }
  8353. var matrix1 = matrix[1];
  8354. var matrix2 = matrix[2];
  8355. var matrix3 = matrix[3];
  8356. var matrix6 = matrix[6];
  8357. var matrix7 = matrix[7];
  8358. var matrix11 = matrix[11];
  8359. result[0] = matrix[0];
  8360. result[1] = matrix[4];
  8361. result[2] = matrix[8];
  8362. result[3] = matrix[12];
  8363. result[4] = matrix1;
  8364. result[5] = matrix[5];
  8365. result[6] = matrix[9];
  8366. result[7] = matrix[13];
  8367. result[8] = matrix2;
  8368. result[9] = matrix6;
  8369. result[10] = matrix[10];
  8370. result[11] = matrix[14];
  8371. result[12] = matrix3;
  8372. result[13] = matrix7;
  8373. result[14] = matrix11;
  8374. result[15] = matrix[15];
  8375. return result;
  8376. };
  8377. /**
  8378. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  8379. *
  8380. * @param {Matrix4} matrix The matrix with signed elements.
  8381. * @param {Matrix4} result The object onto which to store the result.
  8382. * @returns {Matrix4} The modified result parameter.
  8383. */
  8384. Matrix4.abs = function(matrix, result) {
  8385. if (!defined(matrix)) {
  8386. throw new DeveloperError('matrix is required');
  8387. }
  8388. if (!defined(result)) {
  8389. throw new DeveloperError('result is required');
  8390. }
  8391. result[0] = Math.abs(matrix[0]);
  8392. result[1] = Math.abs(matrix[1]);
  8393. result[2] = Math.abs(matrix[2]);
  8394. result[3] = Math.abs(matrix[3]);
  8395. result[4] = Math.abs(matrix[4]);
  8396. result[5] = Math.abs(matrix[5]);
  8397. result[6] = Math.abs(matrix[6]);
  8398. result[7] = Math.abs(matrix[7]);
  8399. result[8] = Math.abs(matrix[8]);
  8400. result[9] = Math.abs(matrix[9]);
  8401. result[10] = Math.abs(matrix[10]);
  8402. result[11] = Math.abs(matrix[11]);
  8403. result[12] = Math.abs(matrix[12]);
  8404. result[13] = Math.abs(matrix[13]);
  8405. result[14] = Math.abs(matrix[14]);
  8406. result[15] = Math.abs(matrix[15]);
  8407. return result;
  8408. };
  8409. /**
  8410. * Compares the provided matrices componentwise and returns
  8411. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8412. *
  8413. * @param {Matrix4} [left] The first matrix.
  8414. * @param {Matrix4} [right] The second matrix.
  8415. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  8416. *
  8417. * @example
  8418. * //compares two Matrix4 instances
  8419. *
  8420. * // a = [10.0, 14.0, 18.0, 22.0]
  8421. * // [11.0, 15.0, 19.0, 23.0]
  8422. * // [12.0, 16.0, 20.0, 24.0]
  8423. * // [13.0, 17.0, 21.0, 25.0]
  8424. *
  8425. * // b = [10.0, 14.0, 18.0, 22.0]
  8426. * // [11.0, 15.0, 19.0, 23.0]
  8427. * // [12.0, 16.0, 20.0, 24.0]
  8428. * // [13.0, 17.0, 21.0, 25.0]
  8429. *
  8430. * if(Cesium.Matrix4.equals(a,b)) {
  8431. * console.log("Both matrices are equal");
  8432. * } else {
  8433. * console.log("They are not equal");
  8434. * }
  8435. *
  8436. * //Prints "Both matrices are equal" on the console
  8437. */
  8438. Matrix4.equals = function(left, right) {
  8439. // Given that most matrices will be transformation matrices, the elements
  8440. // are tested in order such that the test is likely to fail as early
  8441. // as possible. I _think_ this is just as friendly to the L1 cache
  8442. // as testing in index order. It is certainty faster in practice.
  8443. return (left === right) ||
  8444. (defined(left) &&
  8445. defined(right) &&
  8446. // Translation
  8447. left[12] === right[12] &&
  8448. left[13] === right[13] &&
  8449. left[14] === right[14] &&
  8450. // Rotation/scale
  8451. left[0] === right[0] &&
  8452. left[1] === right[1] &&
  8453. left[2] === right[2] &&
  8454. left[4] === right[4] &&
  8455. left[5] === right[5] &&
  8456. left[6] === right[6] &&
  8457. left[8] === right[8] &&
  8458. left[9] === right[9] &&
  8459. left[10] === right[10] &&
  8460. // Bottom row
  8461. left[3] === right[3] &&
  8462. left[7] === right[7] &&
  8463. left[11] === right[11] &&
  8464. left[15] === right[15]);
  8465. };
  8466. /**
  8467. * Compares the provided matrices componentwise and returns
  8468. * <code>true</code> if they are within the provided epsilon,
  8469. * <code>false</code> otherwise.
  8470. *
  8471. * @param {Matrix4} [left] The first matrix.
  8472. * @param {Matrix4} [right] The second matrix.
  8473. * @param {Number} epsilon The epsilon to use for equality testing.
  8474. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  8475. *
  8476. * @example
  8477. * //compares two Matrix4 instances
  8478. *
  8479. * // a = [10.5, 14.5, 18.5, 22.5]
  8480. * // [11.5, 15.5, 19.5, 23.5]
  8481. * // [12.5, 16.5, 20.5, 24.5]
  8482. * // [13.5, 17.5, 21.5, 25.5]
  8483. *
  8484. * // b = [10.0, 14.0, 18.0, 22.0]
  8485. * // [11.0, 15.0, 19.0, 23.0]
  8486. * // [12.0, 16.0, 20.0, 24.0]
  8487. * // [13.0, 17.0, 21.0, 25.0]
  8488. *
  8489. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  8490. * console.log("Difference between both the matrices is less than 0.1");
  8491. * } else {
  8492. * console.log("Difference between both the matrices is not less than 0.1");
  8493. * }
  8494. *
  8495. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  8496. */
  8497. Matrix4.equalsEpsilon = function(left, right, epsilon) {
  8498. if (typeof epsilon !== 'number') {
  8499. throw new DeveloperError('epsilon must be a number');
  8500. }
  8501. return (left === right) ||
  8502. (defined(left) &&
  8503. defined(right) &&
  8504. Math.abs(left[0] - right[0]) <= epsilon &&
  8505. Math.abs(left[1] - right[1]) <= epsilon &&
  8506. Math.abs(left[2] - right[2]) <= epsilon &&
  8507. Math.abs(left[3] - right[3]) <= epsilon &&
  8508. Math.abs(left[4] - right[4]) <= epsilon &&
  8509. Math.abs(left[5] - right[5]) <= epsilon &&
  8510. Math.abs(left[6] - right[6]) <= epsilon &&
  8511. Math.abs(left[7] - right[7]) <= epsilon &&
  8512. Math.abs(left[8] - right[8]) <= epsilon &&
  8513. Math.abs(left[9] - right[9]) <= epsilon &&
  8514. Math.abs(left[10] - right[10]) <= epsilon &&
  8515. Math.abs(left[11] - right[11]) <= epsilon &&
  8516. Math.abs(left[12] - right[12]) <= epsilon &&
  8517. Math.abs(left[13] - right[13]) <= epsilon &&
  8518. Math.abs(left[14] - right[14]) <= epsilon &&
  8519. Math.abs(left[15] - right[15]) <= epsilon);
  8520. };
  8521. /**
  8522. * Gets the translation portion of the provided matrix, assuming the matrix is a affine transformation matrix.
  8523. *
  8524. * @param {Matrix4} matrix The matrix to use.
  8525. * @param {Cartesian3} result The object onto which to store the result.
  8526. * @returns {Cartesian3} The modified result parameter.
  8527. */
  8528. Matrix4.getTranslation = function(matrix, result) {
  8529. if (!defined(matrix)) {
  8530. throw new DeveloperError('matrix is required');
  8531. }
  8532. if (!defined(result)) {
  8533. throw new DeveloperError('result is required');
  8534. }
  8535. result.x = matrix[12];
  8536. result.y = matrix[13];
  8537. result.z = matrix[14];
  8538. return result;
  8539. };
  8540. /**
  8541. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is a affine transformation matrix.
  8542. *
  8543. * @param {Matrix4} matrix The matrix to use.
  8544. * @param {Matrix3} result The object onto which to store the result.
  8545. * @returns {Matrix3} The modified result parameter.
  8546. *
  8547. * @example
  8548. * // returns a Matrix3 instance from a Matrix4 instance
  8549. *
  8550. * // m = [10.0, 14.0, 18.0, 22.0]
  8551. * // [11.0, 15.0, 19.0, 23.0]
  8552. * // [12.0, 16.0, 20.0, 24.0]
  8553. * // [13.0, 17.0, 21.0, 25.0]
  8554. *
  8555. * var b = new Cesium.Matrix3();
  8556. * Cesium.Matrix4.getRotation(m,b);
  8557. *
  8558. * // b = [10.0, 14.0, 18.0]
  8559. * // [11.0, 15.0, 19.0]
  8560. * // [12.0, 16.0, 20.0]
  8561. */
  8562. Matrix4.getRotation = function(matrix, result) {
  8563. if (!defined(matrix)) {
  8564. throw new DeveloperError('matrix is required');
  8565. }
  8566. if (!defined(result)) {
  8567. throw new DeveloperError('result is required');
  8568. }
  8569. result[0] = matrix[0];
  8570. result[1] = matrix[1];
  8571. result[2] = matrix[2];
  8572. result[3] = matrix[4];
  8573. result[4] = matrix[5];
  8574. result[5] = matrix[6];
  8575. result[6] = matrix[8];
  8576. result[7] = matrix[9];
  8577. result[8] = matrix[10];
  8578. return result;
  8579. };
  8580. var scratchInverseRotation = new Matrix3();
  8581. var scratchMatrix3Zero = new Matrix3();
  8582. var scratchBottomRow = new Cartesian4();
  8583. var scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  8584. /**
  8585. * Computes the inverse of the provided matrix using Cramers Rule.
  8586. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  8587. * If the matrix is an affine transformation matrix, it is more efficient
  8588. * to invert it with {@link Matrix4.inverseTransformation}.
  8589. *
  8590. * @param {Matrix4} matrix The matrix to invert.
  8591. * @param {Matrix4} result The object onto which to store the result.
  8592. * @returns {Matrix4} The modified result parameter.
  8593. *
  8594. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  8595. */
  8596. Matrix4.inverse = function(matrix, result) {
  8597. if (!defined(matrix)) {
  8598. throw new DeveloperError('matrix is required');
  8599. }
  8600. if (!defined(result)) {
  8601. throw new DeveloperError('result is required');
  8602. }
  8603. // Special case for a zero scale matrix that can occur, for example,
  8604. // when a model's node has a [0, 0, 0] scale.
  8605. if (Matrix3.equalsEpsilon(Matrix4.getRotation(matrix, scratchInverseRotation), scratchMatrix3Zero, CesiumMath.EPSILON7) &&
  8606. Cartesian4.equals(Matrix4.getRow(matrix, 3, scratchBottomRow), scratchExpectedBottomRow)) {
  8607. result[0] = 0.0;
  8608. result[1] = 0.0;
  8609. result[2] = 0.0;
  8610. result[3] = 0.0;
  8611. result[4] = 0.0;
  8612. result[5] = 0.0;
  8613. result[6] = 0.0;
  8614. result[7] = 0.0;
  8615. result[8] = 0.0;
  8616. result[9] = 0.0;
  8617. result[10] = 0.0;
  8618. result[11] = 0.0;
  8619. result[12] = -matrix[12];
  8620. result[13] = -matrix[13];
  8621. result[14] = -matrix[14];
  8622. result[15] = 1.0;
  8623. return result;
  8624. }
  8625. //
  8626. // Ported from:
  8627. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  8628. //
  8629. var src0 = matrix[0];
  8630. var src1 = matrix[4];
  8631. var src2 = matrix[8];
  8632. var src3 = matrix[12];
  8633. var src4 = matrix[1];
  8634. var src5 = matrix[5];
  8635. var src6 = matrix[9];
  8636. var src7 = matrix[13];
  8637. var src8 = matrix[2];
  8638. var src9 = matrix[6];
  8639. var src10 = matrix[10];
  8640. var src11 = matrix[14];
  8641. var src12 = matrix[3];
  8642. var src13 = matrix[7];
  8643. var src14 = matrix[11];
  8644. var src15 = matrix[15];
  8645. // calculate pairs for first 8 elements (cofactors)
  8646. var tmp0 = src10 * src15;
  8647. var tmp1 = src11 * src14;
  8648. var tmp2 = src9 * src15;
  8649. var tmp3 = src11 * src13;
  8650. var tmp4 = src9 * src14;
  8651. var tmp5 = src10 * src13;
  8652. var tmp6 = src8 * src15;
  8653. var tmp7 = src11 * src12;
  8654. var tmp8 = src8 * src14;
  8655. var tmp9 = src10 * src12;
  8656. var tmp10 = src8 * src13;
  8657. var tmp11 = src9 * src12;
  8658. // calculate first 8 elements (cofactors)
  8659. var dst0 = (tmp0 * src5 + tmp3 * src6 + tmp4 * src7) - (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  8660. var dst1 = (tmp1 * src4 + tmp6 * src6 + tmp9 * src7) - (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  8661. var dst2 = (tmp2 * src4 + tmp7 * src5 + tmp10 * src7) - (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  8662. var dst3 = (tmp5 * src4 + tmp8 * src5 + tmp11 * src6) - (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  8663. var dst4 = (tmp1 * src1 + tmp2 * src2 + tmp5 * src3) - (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  8664. var dst5 = (tmp0 * src0 + tmp7 * src2 + tmp8 * src3) - (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  8665. var dst6 = (tmp3 * src0 + tmp6 * src1 + tmp11 * src3) - (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  8666. var dst7 = (tmp4 * src0 + tmp9 * src1 + tmp10 * src2) - (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  8667. // calculate pairs for second 8 elements (cofactors)
  8668. tmp0 = src2 * src7;
  8669. tmp1 = src3 * src6;
  8670. tmp2 = src1 * src7;
  8671. tmp3 = src3 * src5;
  8672. tmp4 = src1 * src6;
  8673. tmp5 = src2 * src5;
  8674. tmp6 = src0 * src7;
  8675. tmp7 = src3 * src4;
  8676. tmp8 = src0 * src6;
  8677. tmp9 = src2 * src4;
  8678. tmp10 = src0 * src5;
  8679. tmp11 = src1 * src4;
  8680. // calculate second 8 elements (cofactors)
  8681. var dst8 = (tmp0 * src13 + tmp3 * src14 + tmp4 * src15) - (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  8682. var dst9 = (tmp1 * src12 + tmp6 * src14 + tmp9 * src15) - (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  8683. var dst10 = (tmp2 * src12 + tmp7 * src13 + tmp10 * src15) - (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  8684. var dst11 = (tmp5 * src12 + tmp8 * src13 + tmp11 * src14) - (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  8685. var dst12 = (tmp2 * src10 + tmp5 * src11 + tmp1 * src9) - (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  8686. var dst13 = (tmp8 * src11 + tmp0 * src8 + tmp7 * src10) - (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  8687. var dst14 = (tmp6 * src9 + tmp11 * src11 + tmp3 * src8) - (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  8688. var dst15 = (tmp10 * src10 + tmp4 * src8 + tmp9 * src9) - (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  8689. // calculate determinant
  8690. var det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  8691. if (Math.abs(det) < CesiumMath.EPSILON20) {
  8692. throw new RuntimeError('matrix is not invertible because its determinate is zero.');
  8693. }
  8694. // calculate matrix inverse
  8695. det = 1.0 / det;
  8696. result[0] = dst0 * det;
  8697. result[1] = dst1 * det;
  8698. result[2] = dst2 * det;
  8699. result[3] = dst3 * det;
  8700. result[4] = dst4 * det;
  8701. result[5] = dst5 * det;
  8702. result[6] = dst6 * det;
  8703. result[7] = dst7 * det;
  8704. result[8] = dst8 * det;
  8705. result[9] = dst9 * det;
  8706. result[10] = dst10 * det;
  8707. result[11] = dst11 * det;
  8708. result[12] = dst12 * det;
  8709. result[13] = dst13 * det;
  8710. result[14] = dst14 * det;
  8711. result[15] = dst15 * det;
  8712. return result;
  8713. };
  8714. /**
  8715. * Computes the inverse of the provided matrix assuming it is
  8716. * an affine transformation matrix, where the upper left 3x3 elements
  8717. * are a rotation matrix, and the upper three elements in the fourth
  8718. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  8719. * The matrix is not verified to be in the proper form.
  8720. * This method is faster than computing the inverse for a general 4x4
  8721. * matrix using {@link Matrix4.inverse}.
  8722. *
  8723. * @param {Matrix4} matrix The matrix to invert.
  8724. * @param {Matrix4} result The object onto which to store the result.
  8725. * @returns {Matrix4} The modified result parameter.
  8726. */
  8727. Matrix4.inverseTransformation = function(matrix, result) {
  8728. if (!defined(matrix)) {
  8729. throw new DeveloperError('matrix is required');
  8730. }
  8731. if (!defined(result)) {
  8732. throw new DeveloperError('result is required');
  8733. }
  8734. //This function is an optimized version of the below 4 lines.
  8735. //var rT = Matrix3.transpose(Matrix4.getRotation(matrix));
  8736. //var rTN = Matrix3.negate(rT);
  8737. //var rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  8738. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  8739. var matrix0 = matrix[0];
  8740. var matrix1 = matrix[1];
  8741. var matrix2 = matrix[2];
  8742. var matrix4 = matrix[4];
  8743. var matrix5 = matrix[5];
  8744. var matrix6 = matrix[6];
  8745. var matrix8 = matrix[8];
  8746. var matrix9 = matrix[9];
  8747. var matrix10 = matrix[10];
  8748. var vX = matrix[12];
  8749. var vY = matrix[13];
  8750. var vZ = matrix[14];
  8751. var x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  8752. var y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  8753. var z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  8754. result[0] = matrix0;
  8755. result[1] = matrix4;
  8756. result[2] = matrix8;
  8757. result[3] = 0.0;
  8758. result[4] = matrix1;
  8759. result[5] = matrix5;
  8760. result[6] = matrix9;
  8761. result[7] = 0.0;
  8762. result[8] = matrix2;
  8763. result[9] = matrix6;
  8764. result[10] = matrix10;
  8765. result[11] = 0.0;
  8766. result[12] = x;
  8767. result[13] = y;
  8768. result[14] = z;
  8769. result[15] = 1.0;
  8770. return result;
  8771. };
  8772. /**
  8773. * An immutable Matrix4 instance initialized to the identity matrix.
  8774. *
  8775. * @type {Matrix4}
  8776. * @constant
  8777. */
  8778. Matrix4.IDENTITY = freezeObject(new Matrix4(1.0, 0.0, 0.0, 0.0,
  8779. 0.0, 1.0, 0.0, 0.0,
  8780. 0.0, 0.0, 1.0, 0.0,
  8781. 0.0, 0.0, 0.0, 1.0));
  8782. /**
  8783. * An immutable Matrix4 instance initialized to the zero matrix.
  8784. *
  8785. * @type {Matrix4}
  8786. * @constant
  8787. */
  8788. Matrix4.ZERO = freezeObject(new Matrix4(0.0, 0.0, 0.0, 0.0,
  8789. 0.0, 0.0, 0.0, 0.0,
  8790. 0.0, 0.0, 0.0, 0.0,
  8791. 0.0, 0.0, 0.0, 0.0));
  8792. /**
  8793. * The index into Matrix4 for column 0, row 0.
  8794. *
  8795. * @type {Number}
  8796. * @constant
  8797. */
  8798. Matrix4.COLUMN0ROW0 = 0;
  8799. /**
  8800. * The index into Matrix4 for column 0, row 1.
  8801. *
  8802. * @type {Number}
  8803. * @constant
  8804. */
  8805. Matrix4.COLUMN0ROW1 = 1;
  8806. /**
  8807. * The index into Matrix4 for column 0, row 2.
  8808. *
  8809. * @type {Number}
  8810. * @constant
  8811. */
  8812. Matrix4.COLUMN0ROW2 = 2;
  8813. /**
  8814. * The index into Matrix4 for column 0, row 3.
  8815. *
  8816. * @type {Number}
  8817. * @constant
  8818. */
  8819. Matrix4.COLUMN0ROW3 = 3;
  8820. /**
  8821. * The index into Matrix4 for column 1, row 0.
  8822. *
  8823. * @type {Number}
  8824. * @constant
  8825. */
  8826. Matrix4.COLUMN1ROW0 = 4;
  8827. /**
  8828. * The index into Matrix4 for column 1, row 1.
  8829. *
  8830. * @type {Number}
  8831. * @constant
  8832. */
  8833. Matrix4.COLUMN1ROW1 = 5;
  8834. /**
  8835. * The index into Matrix4 for column 1, row 2.
  8836. *
  8837. * @type {Number}
  8838. * @constant
  8839. */
  8840. Matrix4.COLUMN1ROW2 = 6;
  8841. /**
  8842. * The index into Matrix4 for column 1, row 3.
  8843. *
  8844. * @type {Number}
  8845. * @constant
  8846. */
  8847. Matrix4.COLUMN1ROW3 = 7;
  8848. /**
  8849. * The index into Matrix4 for column 2, row 0.
  8850. *
  8851. * @type {Number}
  8852. * @constant
  8853. */
  8854. Matrix4.COLUMN2ROW0 = 8;
  8855. /**
  8856. * The index into Matrix4 for column 2, row 1.
  8857. *
  8858. * @type {Number}
  8859. * @constant
  8860. */
  8861. Matrix4.COLUMN2ROW1 = 9;
  8862. /**
  8863. * The index into Matrix4 for column 2, row 2.
  8864. *
  8865. * @type {Number}
  8866. * @constant
  8867. */
  8868. Matrix4.COLUMN2ROW2 = 10;
  8869. /**
  8870. * The index into Matrix4 for column 2, row 3.
  8871. *
  8872. * @type {Number}
  8873. * @constant
  8874. */
  8875. Matrix4.COLUMN2ROW3 = 11;
  8876. /**
  8877. * The index into Matrix4 for column 3, row 0.
  8878. *
  8879. * @type {Number}
  8880. * @constant
  8881. */
  8882. Matrix4.COLUMN3ROW0 = 12;
  8883. /**
  8884. * The index into Matrix4 for column 3, row 1.
  8885. *
  8886. * @type {Number}
  8887. * @constant
  8888. */
  8889. Matrix4.COLUMN3ROW1 = 13;
  8890. /**
  8891. * The index into Matrix4 for column 3, row 2.
  8892. *
  8893. * @type {Number}
  8894. * @constant
  8895. */
  8896. Matrix4.COLUMN3ROW2 = 14;
  8897. /**
  8898. * The index into Matrix4 for column 3, row 3.
  8899. *
  8900. * @type {Number}
  8901. * @constant
  8902. */
  8903. Matrix4.COLUMN3ROW3 = 15;
  8904. defineProperties(Matrix4.prototype, {
  8905. /**
  8906. * Gets the number of items in the collection.
  8907. * @memberof Matrix4.prototype
  8908. *
  8909. * @type {Number}
  8910. */
  8911. length : {
  8912. get : function() {
  8913. return Matrix4.packedLength;
  8914. }
  8915. }
  8916. });
  8917. /**
  8918. * Duplicates the provided Matrix4 instance.
  8919. *
  8920. * @param {Matrix4} [result] The object onto which to store the result.
  8921. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  8922. */
  8923. Matrix4.prototype.clone = function(result) {
  8924. return Matrix4.clone(this, result);
  8925. };
  8926. /**
  8927. * Compares this matrix to the provided matrix componentwise and returns
  8928. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8929. *
  8930. * @param {Matrix4} [right] The right hand side matrix.
  8931. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  8932. */
  8933. Matrix4.prototype.equals = function(right) {
  8934. return Matrix4.equals(this, right);
  8935. };
  8936. /**
  8937. * @private
  8938. */
  8939. Matrix4.equalsArray = function(matrix, array, offset) {
  8940. return matrix[0] === array[offset] &&
  8941. matrix[1] === array[offset + 1] &&
  8942. matrix[2] === array[offset + 2] &&
  8943. matrix[3] === array[offset + 3] &&
  8944. matrix[4] === array[offset + 4] &&
  8945. matrix[5] === array[offset + 5] &&
  8946. matrix[6] === array[offset + 6] &&
  8947. matrix[7] === array[offset + 7] &&
  8948. matrix[8] === array[offset + 8] &&
  8949. matrix[9] === array[offset + 9] &&
  8950. matrix[10] === array[offset + 10] &&
  8951. matrix[11] === array[offset + 11] &&
  8952. matrix[12] === array[offset + 12] &&
  8953. matrix[13] === array[offset + 13] &&
  8954. matrix[14] === array[offset + 14] &&
  8955. matrix[15] === array[offset + 15];
  8956. };
  8957. /**
  8958. * Compares this matrix to the provided matrix componentwise and returns
  8959. * <code>true</code> if they are within the provided epsilon,
  8960. * <code>false</code> otherwise.
  8961. *
  8962. * @param {Matrix4} [right] The right hand side matrix.
  8963. * @param {Number} epsilon The epsilon to use for equality testing.
  8964. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  8965. */
  8966. Matrix4.prototype.equalsEpsilon = function(right, epsilon) {
  8967. return Matrix4.equalsEpsilon(this, right, epsilon);
  8968. };
  8969. /**
  8970. * Computes a string representing this Matrix with each row being
  8971. * on a separate line and in the format '(column0, column1, column2, column3)'.
  8972. *
  8973. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  8974. */
  8975. Matrix4.prototype.toString = function() {
  8976. return '(' + this[0] + ', ' + this[4] + ', ' + this[8] + ', ' + this[12] +')\n' +
  8977. '(' + this[1] + ', ' + this[5] + ', ' + this[9] + ', ' + this[13] +')\n' +
  8978. '(' + this[2] + ', ' + this[6] + ', ' + this[10] + ', ' + this[14] +')\n' +
  8979. '(' + this[3] + ', ' + this[7] + ', ' + this[11] + ', ' + this[15] +')';
  8980. };
  8981. return Matrix4;
  8982. });
  8983. /*global define*/
  8984. define('Core/Rectangle',[
  8985. './Cartographic',
  8986. './defaultValue',
  8987. './defined',
  8988. './defineProperties',
  8989. './DeveloperError',
  8990. './Ellipsoid',
  8991. './freezeObject',
  8992. './Math'
  8993. ], function(
  8994. Cartographic,
  8995. defaultValue,
  8996. defined,
  8997. defineProperties,
  8998. DeveloperError,
  8999. Ellipsoid,
  9000. freezeObject,
  9001. CesiumMath) {
  9002. 'use strict';
  9003. /**
  9004. * A two dimensional region specified as longitude and latitude coordinates.
  9005. *
  9006. * @alias Rectangle
  9007. * @constructor
  9008. *
  9009. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  9010. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  9011. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  9012. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  9013. *
  9014. * @see Packable
  9015. */
  9016. function Rectangle(west, south, east, north) {
  9017. /**
  9018. * The westernmost longitude in radians in the range [-Pi, Pi].
  9019. *
  9020. * @type {Number}
  9021. * @default 0.0
  9022. */
  9023. this.west = defaultValue(west, 0.0);
  9024. /**
  9025. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  9026. *
  9027. * @type {Number}
  9028. * @default 0.0
  9029. */
  9030. this.south = defaultValue(south, 0.0);
  9031. /**
  9032. * The easternmost longitude in radians in the range [-Pi, Pi].
  9033. *
  9034. * @type {Number}
  9035. * @default 0.0
  9036. */
  9037. this.east = defaultValue(east, 0.0);
  9038. /**
  9039. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  9040. *
  9041. * @type {Number}
  9042. * @default 0.0
  9043. */
  9044. this.north = defaultValue(north, 0.0);
  9045. }
  9046. defineProperties(Rectangle.prototype, {
  9047. /**
  9048. * Gets the width of the rectangle in radians.
  9049. * @memberof Rectangle.prototype
  9050. * @type {Number}
  9051. */
  9052. width : {
  9053. get : function() {
  9054. return Rectangle.computeWidth(this);
  9055. }
  9056. },
  9057. /**
  9058. * Gets the height of the rectangle in radians.
  9059. * @memberof Rectangle.prototype
  9060. * @type {Number}
  9061. */
  9062. height : {
  9063. get : function() {
  9064. return Rectangle.computeHeight(this);
  9065. }
  9066. }
  9067. });
  9068. /**
  9069. * The number of elements used to pack the object into an array.
  9070. * @type {Number}
  9071. */
  9072. Rectangle.packedLength = 4;
  9073. /**
  9074. * Stores the provided instance into the provided array.
  9075. *
  9076. * @param {Rectangle} value The value to pack.
  9077. * @param {Number[]} array The array to pack into.
  9078. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9079. *
  9080. * @returns {Number[]} The array that was packed into
  9081. */
  9082. Rectangle.pack = function(value, array, startingIndex) {
  9083. if (!defined(value)) {
  9084. throw new DeveloperError('value is required');
  9085. }
  9086. if (!defined(array)) {
  9087. throw new DeveloperError('array is required');
  9088. }
  9089. startingIndex = defaultValue(startingIndex, 0);
  9090. array[startingIndex++] = value.west;
  9091. array[startingIndex++] = value.south;
  9092. array[startingIndex++] = value.east;
  9093. array[startingIndex] = value.north;
  9094. return array;
  9095. };
  9096. /**
  9097. * Retrieves an instance from a packed array.
  9098. *
  9099. * @param {Number[]} array The packed array.
  9100. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  9101. * @param {Rectangle} [result] The object into which to store the result.
  9102. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  9103. */
  9104. Rectangle.unpack = function(array, startingIndex, result) {
  9105. if (!defined(array)) {
  9106. throw new DeveloperError('array is required');
  9107. }
  9108. startingIndex = defaultValue(startingIndex, 0);
  9109. if (!defined(result)) {
  9110. result = new Rectangle();
  9111. }
  9112. result.west = array[startingIndex++];
  9113. result.south = array[startingIndex++];
  9114. result.east = array[startingIndex++];
  9115. result.north = array[startingIndex];
  9116. return result;
  9117. };
  9118. /**
  9119. * Computes the width of a rectangle in radians.
  9120. * @param {Rectangle} rectangle The rectangle to compute the width of.
  9121. * @returns {Number} The width.
  9122. */
  9123. Rectangle.computeWidth = function(rectangle) {
  9124. if (!defined(rectangle)) {
  9125. throw new DeveloperError('rectangle is required.');
  9126. }
  9127. var east = rectangle.east;
  9128. var west = rectangle.west;
  9129. if (east < west) {
  9130. east += CesiumMath.TWO_PI;
  9131. }
  9132. return east - west;
  9133. };
  9134. /**
  9135. * Computes the height of a rectangle in radians.
  9136. * @param {Rectangle} rectangle The rectangle to compute the height of.
  9137. * @returns {Number} The height.
  9138. */
  9139. Rectangle.computeHeight = function(rectangle) {
  9140. if (!defined(rectangle)) {
  9141. throw new DeveloperError('rectangle is required.');
  9142. }
  9143. return rectangle.north - rectangle.south;
  9144. };
  9145. /**
  9146. * Creates an rectangle given the boundary longitude and latitude in degrees.
  9147. *
  9148. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  9149. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  9150. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  9151. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  9152. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  9153. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  9154. *
  9155. * @example
  9156. * var rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  9157. */
  9158. Rectangle.fromDegrees = function(west, south, east, north, result) {
  9159. west = CesiumMath.toRadians(defaultValue(west, 0.0));
  9160. south = CesiumMath.toRadians(defaultValue(south, 0.0));
  9161. east = CesiumMath.toRadians(defaultValue(east, 0.0));
  9162. north = CesiumMath.toRadians(defaultValue(north, 0.0));
  9163. if (!defined(result)) {
  9164. return new Rectangle(west, south, east, north);
  9165. }
  9166. result.west = west;
  9167. result.south = south;
  9168. result.east = east;
  9169. result.north = north;
  9170. return result;
  9171. };
  9172. /**
  9173. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  9174. *
  9175. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  9176. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  9177. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  9178. */
  9179. Rectangle.fromCartographicArray = function(cartographics, result) {
  9180. if (!defined(cartographics)) {
  9181. throw new DeveloperError('cartographics is required.');
  9182. }
  9183. var west = Number.MAX_VALUE;
  9184. var east = -Number.MAX_VALUE;
  9185. var westOverIDL = Number.MAX_VALUE;
  9186. var eastOverIDL = -Number.MAX_VALUE;
  9187. var south = Number.MAX_VALUE;
  9188. var north = -Number.MAX_VALUE;
  9189. for ( var i = 0, len = cartographics.length; i < len; i++) {
  9190. var position = cartographics[i];
  9191. west = Math.min(west, position.longitude);
  9192. east = Math.max(east, position.longitude);
  9193. south = Math.min(south, position.latitude);
  9194. north = Math.max(north, position.latitude);
  9195. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + CesiumMath.TWO_PI;
  9196. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  9197. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  9198. }
  9199. if(east - west > eastOverIDL - westOverIDL) {
  9200. west = westOverIDL;
  9201. east = eastOverIDL;
  9202. if (east > CesiumMath.PI) {
  9203. east = east - CesiumMath.TWO_PI;
  9204. }
  9205. if (west > CesiumMath.PI) {
  9206. west = west - CesiumMath.TWO_PI;
  9207. }
  9208. }
  9209. if (!defined(result)) {
  9210. return new Rectangle(west, south, east, north);
  9211. }
  9212. result.west = west;
  9213. result.south = south;
  9214. result.east = east;
  9215. result.north = north;
  9216. return result;
  9217. };
  9218. /**
  9219. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  9220. *
  9221. * @param {Cartesian[]} cartesians The list of Cartesian instances.
  9222. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  9223. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  9224. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  9225. */
  9226. Rectangle.fromCartesianArray = function(cartesians, ellipsoid, result) {
  9227. if (!defined(cartesians)) {
  9228. throw new DeveloperError('cartesians is required.');
  9229. }
  9230. var west = Number.MAX_VALUE;
  9231. var east = -Number.MAX_VALUE;
  9232. var westOverIDL = Number.MAX_VALUE;
  9233. var eastOverIDL = -Number.MAX_VALUE;
  9234. var south = Number.MAX_VALUE;
  9235. var north = -Number.MAX_VALUE;
  9236. for ( var i = 0, len = cartesians.length; i < len; i++) {
  9237. var position = ellipsoid.cartesianToCartographic(cartesians[i]);
  9238. west = Math.min(west, position.longitude);
  9239. east = Math.max(east, position.longitude);
  9240. south = Math.min(south, position.latitude);
  9241. north = Math.max(north, position.latitude);
  9242. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + CesiumMath.TWO_PI;
  9243. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  9244. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  9245. }
  9246. if(east - west > eastOverIDL - westOverIDL) {
  9247. west = westOverIDL;
  9248. east = eastOverIDL;
  9249. if (east > CesiumMath.PI) {
  9250. east = east - CesiumMath.TWO_PI;
  9251. }
  9252. if (west > CesiumMath.PI) {
  9253. west = west - CesiumMath.TWO_PI;
  9254. }
  9255. }
  9256. if (!defined(result)) {
  9257. return new Rectangle(west, south, east, north);
  9258. }
  9259. result.west = west;
  9260. result.south = south;
  9261. result.east = east;
  9262. result.north = north;
  9263. return result;
  9264. };
  9265. /**
  9266. * Duplicates an Rectangle.
  9267. *
  9268. * @param {Rectangle} rectangle The rectangle to clone.
  9269. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  9270. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  9271. */
  9272. Rectangle.clone = function(rectangle, result) {
  9273. if (!defined(rectangle)) {
  9274. return undefined;
  9275. }
  9276. if (!defined(result)) {
  9277. return new Rectangle(rectangle.west, rectangle.south, rectangle.east, rectangle.north);
  9278. }
  9279. result.west = rectangle.west;
  9280. result.south = rectangle.south;
  9281. result.east = rectangle.east;
  9282. result.north = rectangle.north;
  9283. return result;
  9284. };
  9285. /**
  9286. * Duplicates this Rectangle.
  9287. *
  9288. * @param {Rectangle} [result] The object onto which to store the result.
  9289. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  9290. */
  9291. Rectangle.prototype.clone = function(result) {
  9292. return Rectangle.clone(this, result);
  9293. };
  9294. /**
  9295. * Compares the provided Rectangle with this Rectangle componentwise and returns
  9296. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9297. *
  9298. * @param {Rectangle} [other] The Rectangle to compare.
  9299. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  9300. */
  9301. Rectangle.prototype.equals = function(other) {
  9302. return Rectangle.equals(this, other);
  9303. };
  9304. /**
  9305. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  9306. * <code>false</code> otherwise.
  9307. *
  9308. * @param {Rectangle} [left] The first Rectangle.
  9309. * @param {Rectangle} [right] The second Rectangle.
  9310. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  9311. */
  9312. Rectangle.equals = function(left, right) {
  9313. return (left === right) ||
  9314. ((defined(left)) &&
  9315. (defined(right)) &&
  9316. (left.west === right.west) &&
  9317. (left.south === right.south) &&
  9318. (left.east === right.east) &&
  9319. (left.north === right.north));
  9320. };
  9321. /**
  9322. * Compares the provided Rectangle with this Rectangle componentwise and returns
  9323. * <code>true</code> if they are within the provided epsilon,
  9324. * <code>false</code> otherwise.
  9325. *
  9326. * @param {Rectangle} [other] The Rectangle to compare.
  9327. * @param {Number} epsilon The epsilon to use for equality testing.
  9328. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  9329. */
  9330. Rectangle.prototype.equalsEpsilon = function(other, epsilon) {
  9331. if (typeof epsilon !== 'number') {
  9332. throw new DeveloperError('epsilon is required and must be a number.');
  9333. }
  9334. return defined(other) &&
  9335. (Math.abs(this.west - other.west) <= epsilon) &&
  9336. (Math.abs(this.south - other.south) <= epsilon) &&
  9337. (Math.abs(this.east - other.east) <= epsilon) &&
  9338. (Math.abs(this.north - other.north) <= epsilon);
  9339. };
  9340. /**
  9341. * Checks an Rectangle's properties and throws if they are not in valid ranges.
  9342. *
  9343. * @param {Rectangle} rectangle The rectangle to validate
  9344. *
  9345. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  9346. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  9347. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  9348. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  9349. */
  9350. Rectangle.validate = function(rectangle) {
  9351. if (!defined(rectangle)) {
  9352. throw new DeveloperError('rectangle is required');
  9353. }
  9354. var north = rectangle.north;
  9355. if (typeof north !== 'number') {
  9356. throw new DeveloperError('north is required to be a number.');
  9357. }
  9358. if (north < -CesiumMath.PI_OVER_TWO || north > CesiumMath.PI_OVER_TWO) {
  9359. throw new DeveloperError('north must be in the interval [-Pi/2, Pi/2].');
  9360. }
  9361. var south = rectangle.south;
  9362. if (typeof south !== 'number') {
  9363. throw new DeveloperError('south is required to be a number.');
  9364. }
  9365. if (south < -CesiumMath.PI_OVER_TWO || south > CesiumMath.PI_OVER_TWO) {
  9366. throw new DeveloperError('south must be in the interval [-Pi/2, Pi/2].');
  9367. }
  9368. var west = rectangle.west;
  9369. if (typeof west !== 'number') {
  9370. throw new DeveloperError('west is required to be a number.');
  9371. }
  9372. if (west < -Math.PI || west > Math.PI) {
  9373. throw new DeveloperError('west must be in the interval [-Pi, Pi].');
  9374. }
  9375. var east = rectangle.east;
  9376. if (typeof east !== 'number') {
  9377. throw new DeveloperError('east is required to be a number.');
  9378. }
  9379. if (east < -Math.PI || east > Math.PI) {
  9380. throw new DeveloperError('east must be in the interval [-Pi, Pi].');
  9381. }
  9382. };
  9383. /**
  9384. * Computes the southwest corner of an rectangle.
  9385. *
  9386. * @param {Rectangle} rectangle The rectangle for which to find the corner
  9387. * @param {Cartographic} [result] The object onto which to store the result.
  9388. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  9389. */
  9390. Rectangle.southwest = function(rectangle, result) {
  9391. if (!defined(rectangle)) {
  9392. throw new DeveloperError('rectangle is required');
  9393. }
  9394. if (!defined(result)) {
  9395. return new Cartographic(rectangle.west, rectangle.south);
  9396. }
  9397. result.longitude = rectangle.west;
  9398. result.latitude = rectangle.south;
  9399. result.height = 0.0;
  9400. return result;
  9401. };
  9402. /**
  9403. * Computes the northwest corner of an rectangle.
  9404. *
  9405. * @param {Rectangle} rectangle The rectangle for which to find the corner
  9406. * @param {Cartographic} [result] The object onto which to store the result.
  9407. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  9408. */
  9409. Rectangle.northwest = function(rectangle, result) {
  9410. if (!defined(rectangle)) {
  9411. throw new DeveloperError('rectangle is required');
  9412. }
  9413. if (!defined(result)) {
  9414. return new Cartographic(rectangle.west, rectangle.north);
  9415. }
  9416. result.longitude = rectangle.west;
  9417. result.latitude = rectangle.north;
  9418. result.height = 0.0;
  9419. return result;
  9420. };
  9421. /**
  9422. * Computes the northeast corner of an rectangle.
  9423. *
  9424. * @param {Rectangle} rectangle The rectangle for which to find the corner
  9425. * @param {Cartographic} [result] The object onto which to store the result.
  9426. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  9427. */
  9428. Rectangle.northeast = function(rectangle, result) {
  9429. if (!defined(rectangle)) {
  9430. throw new DeveloperError('rectangle is required');
  9431. }
  9432. if (!defined(result)) {
  9433. return new Cartographic(rectangle.east, rectangle.north);
  9434. }
  9435. result.longitude = rectangle.east;
  9436. result.latitude = rectangle.north;
  9437. result.height = 0.0;
  9438. return result;
  9439. };
  9440. /**
  9441. * Computes the southeast corner of an rectangle.
  9442. *
  9443. * @param {Rectangle} rectangle The rectangle for which to find the corner
  9444. * @param {Cartographic} [result] The object onto which to store the result.
  9445. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  9446. */
  9447. Rectangle.southeast = function(rectangle, result) {
  9448. if (!defined(rectangle)) {
  9449. throw new DeveloperError('rectangle is required');
  9450. }
  9451. if (!defined(result)) {
  9452. return new Cartographic(rectangle.east, rectangle.south);
  9453. }
  9454. result.longitude = rectangle.east;
  9455. result.latitude = rectangle.south;
  9456. result.height = 0.0;
  9457. return result;
  9458. };
  9459. /**
  9460. * Computes the center of an rectangle.
  9461. *
  9462. * @param {Rectangle} rectangle The rectangle for which to find the center
  9463. * @param {Cartographic} [result] The object onto which to store the result.
  9464. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  9465. */
  9466. Rectangle.center = function(rectangle, result) {
  9467. if (!defined(rectangle)) {
  9468. throw new DeveloperError('rectangle is required');
  9469. }
  9470. var east = rectangle.east;
  9471. var west = rectangle.west;
  9472. if (east < west) {
  9473. east += CesiumMath.TWO_PI;
  9474. }
  9475. var longitude = CesiumMath.negativePiToPi((west + east) * 0.5);
  9476. var latitude = (rectangle.south + rectangle.north) * 0.5;
  9477. if (!defined(result)) {
  9478. return new Cartographic(longitude, latitude);
  9479. }
  9480. result.longitude = longitude;
  9481. result.latitude = latitude;
  9482. result.height = 0.0;
  9483. return result;
  9484. };
  9485. /**
  9486. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  9487. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  9488. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  9489. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  9490. * coordinates, see {@link Rectangle.simpleIntersection}.
  9491. *
  9492. * @param {Rectangle} rectangle On rectangle to find an intersection
  9493. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  9494. * @param {Rectangle} [result] The object onto which to store the result.
  9495. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  9496. */
  9497. Rectangle.intersection = function(rectangle, otherRectangle, result) {
  9498. if (!defined(rectangle)) {
  9499. throw new DeveloperError('rectangle is required');
  9500. }
  9501. if (!defined(otherRectangle)) {
  9502. throw new DeveloperError('otherRectangle is required.');
  9503. }
  9504. var rectangleEast = rectangle.east;
  9505. var rectangleWest = rectangle.west;
  9506. var otherRectangleEast = otherRectangle.east;
  9507. var otherRectangleWest = otherRectangle.west;
  9508. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  9509. rectangleEast += CesiumMath.TWO_PI;
  9510. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  9511. otherRectangleEast += CesiumMath.TWO_PI;
  9512. }
  9513. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  9514. otherRectangleWest += CesiumMath.TWO_PI;
  9515. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  9516. rectangleWest += CesiumMath.TWO_PI;
  9517. }
  9518. var west = CesiumMath.negativePiToPi(Math.max(rectangleWest, otherRectangleWest));
  9519. var east = CesiumMath.negativePiToPi(Math.min(rectangleEast, otherRectangleEast));
  9520. if ((rectangle.west < rectangle.east || otherRectangle.west < otherRectangle.east) && east <= west) {
  9521. return undefined;
  9522. }
  9523. var south = Math.max(rectangle.south, otherRectangle.south);
  9524. var north = Math.min(rectangle.north, otherRectangle.north);
  9525. if (south >= north) {
  9526. return undefined;
  9527. }
  9528. if (!defined(result)) {
  9529. return new Rectangle(west, south, east, north);
  9530. }
  9531. result.west = west;
  9532. result.south = south;
  9533. result.east = east;
  9534. result.north = north;
  9535. return result;
  9536. };
  9537. /**
  9538. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  9539. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  9540. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  9541. * and longitude (i.e. projected coordinates).
  9542. *
  9543. * @param {Rectangle} rectangle On rectangle to find an intersection
  9544. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  9545. * @param {Rectangle} [result] The object onto which to store the result.
  9546. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  9547. */
  9548. Rectangle.simpleIntersection = function(rectangle, otherRectangle, result) {
  9549. if (!defined(rectangle)) {
  9550. throw new DeveloperError('rectangle is required');
  9551. }
  9552. if (!defined(otherRectangle)) {
  9553. throw new DeveloperError('otherRectangle is required.');
  9554. }
  9555. var west = Math.max(rectangle.west, otherRectangle.west);
  9556. var south = Math.max(rectangle.south, otherRectangle.south);
  9557. var east = Math.min(rectangle.east, otherRectangle.east);
  9558. var north = Math.min(rectangle.north, otherRectangle.north);
  9559. if (south >= north || west >= east) {
  9560. return undefined;
  9561. }
  9562. if (!defined(result)) {
  9563. return new Rectangle(west, south, east, north);
  9564. }
  9565. result.west = west;
  9566. result.south = south;
  9567. result.east = east;
  9568. result.north = north;
  9569. return result;
  9570. };
  9571. /**
  9572. * Computes a rectangle that is the union of two rectangles.
  9573. *
  9574. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  9575. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  9576. * @param {Rectangle} [result] The object onto which to store the result.
  9577. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  9578. */
  9579. Rectangle.union = function(rectangle, otherRectangle, result) {
  9580. if (!defined(rectangle)) {
  9581. throw new DeveloperError('rectangle is required');
  9582. }
  9583. if (!defined(otherRectangle)) {
  9584. throw new DeveloperError('otherRectangle is required.');
  9585. }
  9586. if (!defined(result)) {
  9587. result = new Rectangle();
  9588. }
  9589. var rectangleEast = rectangle.east;
  9590. var rectangleWest = rectangle.west;
  9591. var otherRectangleEast = otherRectangle.east;
  9592. var otherRectangleWest = otherRectangle.west;
  9593. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  9594. rectangleEast += CesiumMath.TWO_PI;
  9595. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  9596. otherRectangleEast += CesiumMath.TWO_PI;
  9597. }
  9598. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  9599. otherRectangleWest += CesiumMath.TWO_PI;
  9600. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  9601. rectangleWest += CesiumMath.TWO_PI;
  9602. }
  9603. var west = CesiumMath.convertLongitudeRange(Math.min(rectangleWest, otherRectangleWest));
  9604. var east = CesiumMath.convertLongitudeRange(Math.max(rectangleEast, otherRectangleEast));
  9605. result.west = west;
  9606. result.south = Math.min(rectangle.south, otherRectangle.south);
  9607. result.east = east;
  9608. result.north = Math.max(rectangle.north, otherRectangle.north);
  9609. return result;
  9610. };
  9611. /**
  9612. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  9613. *
  9614. * @param {Rectangle} rectangle A rectangle to expand.
  9615. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  9616. * @param {Rectangle} [result] The object onto which to store the result.
  9617. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  9618. */
  9619. Rectangle.expand = function(rectangle, cartographic, result) {
  9620. if (!defined(rectangle)) {
  9621. throw new DeveloperError('rectangle is required.');
  9622. }
  9623. if (!defined(cartographic)) {
  9624. throw new DeveloperError('cartographic is required.');
  9625. }
  9626. if (!defined(result)) {
  9627. result = new Rectangle();
  9628. }
  9629. result.west = Math.min(rectangle.west, cartographic.longitude);
  9630. result.south = Math.min(rectangle.south, cartographic.latitude);
  9631. result.east = Math.max(rectangle.east, cartographic.longitude);
  9632. result.north = Math.max(rectangle.north, cartographic.latitude);
  9633. return result;
  9634. };
  9635. /**
  9636. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  9637. *
  9638. * @param {Rectangle} rectangle The rectangle
  9639. * @param {Cartographic} cartographic The cartographic to test.
  9640. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  9641. */
  9642. Rectangle.contains = function(rectangle, cartographic) {
  9643. if (!defined(rectangle)) {
  9644. throw new DeveloperError('rectangle is required');
  9645. }
  9646. if (!defined(cartographic)) {
  9647. throw new DeveloperError('cartographic is required.');
  9648. }
  9649. var longitude = cartographic.longitude;
  9650. var latitude = cartographic.latitude;
  9651. var west = rectangle.west;
  9652. var east = rectangle.east;
  9653. if (east < west) {
  9654. east += CesiumMath.TWO_PI;
  9655. if (longitude < 0.0) {
  9656. longitude += CesiumMath.TWO_PI;
  9657. }
  9658. }
  9659. return (longitude > west || CesiumMath.equalsEpsilon(longitude, west, CesiumMath.EPSILON14)) &&
  9660. (longitude < east || CesiumMath.equalsEpsilon(longitude, east, CesiumMath.EPSILON14)) &&
  9661. latitude >= rectangle.south &&
  9662. latitude <= rectangle.north;
  9663. };
  9664. var subsampleLlaScratch = new Cartographic();
  9665. /**
  9666. * Samples an rectangle so that it includes a list of Cartesian points suitable for passing to
  9667. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  9668. * for rectangles that cover the poles or cross the equator.
  9669. *
  9670. * @param {Rectangle} rectangle The rectangle to subsample.
  9671. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  9672. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  9673. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  9674. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  9675. */
  9676. Rectangle.subsample = function(rectangle, ellipsoid, surfaceHeight, result) {
  9677. if (!defined(rectangle)) {
  9678. throw new DeveloperError('rectangle is required');
  9679. }
  9680. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  9681. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  9682. if (!defined(result)) {
  9683. result = [];
  9684. }
  9685. var length = 0;
  9686. var north = rectangle.north;
  9687. var south = rectangle.south;
  9688. var east = rectangle.east;
  9689. var west = rectangle.west;
  9690. var lla = subsampleLlaScratch;
  9691. lla.height = surfaceHeight;
  9692. lla.longitude = west;
  9693. lla.latitude = north;
  9694. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9695. length++;
  9696. lla.longitude = east;
  9697. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9698. length++;
  9699. lla.latitude = south;
  9700. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9701. length++;
  9702. lla.longitude = west;
  9703. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9704. length++;
  9705. if (north < 0.0) {
  9706. lla.latitude = north;
  9707. } else if (south > 0.0) {
  9708. lla.latitude = south;
  9709. } else {
  9710. lla.latitude = 0.0;
  9711. }
  9712. for ( var i = 1; i < 8; ++i) {
  9713. lla.longitude = -Math.PI + i * CesiumMath.PI_OVER_TWO;
  9714. if (Rectangle.contains(rectangle, lla)) {
  9715. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9716. length++;
  9717. }
  9718. }
  9719. if (lla.latitude === 0.0) {
  9720. lla.longitude = west;
  9721. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9722. length++;
  9723. lla.longitude = east;
  9724. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  9725. length++;
  9726. }
  9727. result.length = length;
  9728. return result;
  9729. };
  9730. /**
  9731. * The largest possible rectangle.
  9732. *
  9733. * @type {Rectangle}
  9734. * @constant
  9735. */
  9736. Rectangle.MAX_VALUE = freezeObject(new Rectangle(-Math.PI, -CesiumMath.PI_OVER_TWO, Math.PI, CesiumMath.PI_OVER_TWO));
  9737. return Rectangle;
  9738. });
  9739. /*global define*/
  9740. define('Core/BoundingSphere',[
  9741. './Cartesian3',
  9742. './Cartographic',
  9743. './defaultValue',
  9744. './defined',
  9745. './DeveloperError',
  9746. './Ellipsoid',
  9747. './GeographicProjection',
  9748. './Intersect',
  9749. './Interval',
  9750. './Matrix3',
  9751. './Matrix4',
  9752. './Rectangle'
  9753. ], function(
  9754. Cartesian3,
  9755. Cartographic,
  9756. defaultValue,
  9757. defined,
  9758. DeveloperError,
  9759. Ellipsoid,
  9760. GeographicProjection,
  9761. Intersect,
  9762. Interval,
  9763. Matrix3,
  9764. Matrix4,
  9765. Rectangle) {
  9766. 'use strict';
  9767. /**
  9768. * A bounding sphere with a center and a radius.
  9769. * @alias BoundingSphere
  9770. * @constructor
  9771. *
  9772. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  9773. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  9774. *
  9775. * @see AxisAlignedBoundingBox
  9776. * @see BoundingRectangle
  9777. * @see Packable
  9778. */
  9779. function BoundingSphere(center, radius) {
  9780. /**
  9781. * The center point of the sphere.
  9782. * @type {Cartesian3}
  9783. * @default {@link Cartesian3.ZERO}
  9784. */
  9785. this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
  9786. /**
  9787. * The radius of the sphere.
  9788. * @type {Number}
  9789. * @default 0.0
  9790. */
  9791. this.radius = defaultValue(radius, 0.0);
  9792. }
  9793. var fromPointsXMin = new Cartesian3();
  9794. var fromPointsYMin = new Cartesian3();
  9795. var fromPointsZMin = new Cartesian3();
  9796. var fromPointsXMax = new Cartesian3();
  9797. var fromPointsYMax = new Cartesian3();
  9798. var fromPointsZMax = new Cartesian3();
  9799. var fromPointsCurrentPos = new Cartesian3();
  9800. var fromPointsScratch = new Cartesian3();
  9801. var fromPointsRitterCenter = new Cartesian3();
  9802. var fromPointsMinBoxPt = new Cartesian3();
  9803. var fromPointsMaxBoxPt = new Cartesian3();
  9804. var fromPointsNaiveCenterScratch = new Cartesian3();
  9805. /**
  9806. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  9807. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  9808. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  9809. *
  9810. * @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.
  9811. * @param {BoundingSphere} [result] The object onto which to store the result.
  9812. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9813. *
  9814. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  9815. */
  9816. BoundingSphere.fromPoints = function(positions, result) {
  9817. if (!defined(result)) {
  9818. result = new BoundingSphere();
  9819. }
  9820. if (!defined(positions) || positions.length === 0) {
  9821. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9822. result.radius = 0.0;
  9823. return result;
  9824. }
  9825. var currentPos = Cartesian3.clone(positions[0], fromPointsCurrentPos);
  9826. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  9827. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  9828. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  9829. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  9830. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  9831. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  9832. var numPositions = positions.length;
  9833. for (var i = 1; i < numPositions; i++) {
  9834. Cartesian3.clone(positions[i], currentPos);
  9835. var x = currentPos.x;
  9836. var y = currentPos.y;
  9837. var z = currentPos.z;
  9838. // Store points containing the the smallest and largest components
  9839. if (x < xMin.x) {
  9840. Cartesian3.clone(currentPos, xMin);
  9841. }
  9842. if (x > xMax.x) {
  9843. Cartesian3.clone(currentPos, xMax);
  9844. }
  9845. if (y < yMin.y) {
  9846. Cartesian3.clone(currentPos, yMin);
  9847. }
  9848. if (y > yMax.y) {
  9849. Cartesian3.clone(currentPos, yMax);
  9850. }
  9851. if (z < zMin.z) {
  9852. Cartesian3.clone(currentPos, zMin);
  9853. }
  9854. if (z > zMax.z) {
  9855. Cartesian3.clone(currentPos, zMax);
  9856. }
  9857. }
  9858. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  9859. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  9860. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  9861. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  9862. // Set the diameter endpoints to the largest span.
  9863. var diameter1 = xMin;
  9864. var diameter2 = xMax;
  9865. var maxSpan = xSpan;
  9866. if (ySpan > maxSpan) {
  9867. maxSpan = ySpan;
  9868. diameter1 = yMin;
  9869. diameter2 = yMax;
  9870. }
  9871. if (zSpan > maxSpan) {
  9872. maxSpan = zSpan;
  9873. diameter1 = zMin;
  9874. diameter2 = zMax;
  9875. }
  9876. // Calculate the center of the initial sphere found by Ritter's algorithm
  9877. var ritterCenter = fromPointsRitterCenter;
  9878. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  9879. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  9880. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  9881. // Calculate the radius of the initial sphere found by Ritter's algorithm
  9882. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  9883. var ritterRadius = Math.sqrt(radiusSquared);
  9884. // Find the center of the sphere found using the Naive method.
  9885. var minBoxPt = fromPointsMinBoxPt;
  9886. minBoxPt.x = xMin.x;
  9887. minBoxPt.y = yMin.y;
  9888. minBoxPt.z = zMin.z;
  9889. var maxBoxPt = fromPointsMaxBoxPt;
  9890. maxBoxPt.x = xMax.x;
  9891. maxBoxPt.y = yMax.y;
  9892. maxBoxPt.z = zMax.z;
  9893. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  9894. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  9895. var naiveRadius = 0;
  9896. for (i = 0; i < numPositions; i++) {
  9897. Cartesian3.clone(positions[i], currentPos);
  9898. // Find the furthest point from the naive center to calculate the naive radius.
  9899. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  9900. if (r > naiveRadius) {
  9901. naiveRadius = r;
  9902. }
  9903. // Make adjustments to the Ritter Sphere to include all points.
  9904. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  9905. if (oldCenterToPointSquared > radiusSquared) {
  9906. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  9907. // Calculate new radius to include the point that lies outside
  9908. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  9909. radiusSquared = ritterRadius * ritterRadius;
  9910. // Calculate center of new Ritter sphere
  9911. var oldToNew = oldCenterToPoint - ritterRadius;
  9912. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  9913. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  9914. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  9915. }
  9916. }
  9917. if (ritterRadius < naiveRadius) {
  9918. Cartesian3.clone(ritterCenter, result.center);
  9919. result.radius = ritterRadius;
  9920. } else {
  9921. Cartesian3.clone(naiveCenter, result.center);
  9922. result.radius = naiveRadius;
  9923. }
  9924. return result;
  9925. };
  9926. var defaultProjection = new GeographicProjection();
  9927. var fromRectangle2DLowerLeft = new Cartesian3();
  9928. var fromRectangle2DUpperRight = new Cartesian3();
  9929. var fromRectangle2DSouthwest = new Cartographic();
  9930. var fromRectangle2DNortheast = new Cartographic();
  9931. /**
  9932. * Computes a bounding sphere from an rectangle projected in 2D.
  9933. *
  9934. * @param {Rectangle} rectangle The rectangle around which to create a bounding sphere.
  9935. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  9936. * @param {BoundingSphere} [result] The object onto which to store the result.
  9937. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9938. */
  9939. BoundingSphere.fromRectangle2D = function(rectangle, projection, result) {
  9940. return BoundingSphere.fromRectangleWithHeights2D(rectangle, projection, 0.0, 0.0, result);
  9941. };
  9942. /**
  9943. * Computes a bounding sphere from an rectangle projected in 2D. The bounding sphere accounts for the
  9944. * object's minimum and maximum heights over the rectangle.
  9945. *
  9946. * @param {Rectangle} rectangle The rectangle around which to create a bounding sphere.
  9947. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  9948. * @param {Number} [minimumHeight=0.0] The minimum height over the rectangle.
  9949. * @param {Number} [maximumHeight=0.0] The maximum height over the rectangle.
  9950. * @param {BoundingSphere} [result] The object onto which to store the result.
  9951. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9952. */
  9953. BoundingSphere.fromRectangleWithHeights2D = function(rectangle, projection, minimumHeight, maximumHeight, result) {
  9954. if (!defined(result)) {
  9955. result = new BoundingSphere();
  9956. }
  9957. if (!defined(rectangle)) {
  9958. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9959. result.radius = 0.0;
  9960. return result;
  9961. }
  9962. projection = defaultValue(projection, defaultProjection);
  9963. Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  9964. fromRectangle2DSouthwest.height = minimumHeight;
  9965. Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  9966. fromRectangle2DNortheast.height = maximumHeight;
  9967. var lowerLeft = projection.project(fromRectangle2DSouthwest, fromRectangle2DLowerLeft);
  9968. var upperRight = projection.project(fromRectangle2DNortheast, fromRectangle2DUpperRight);
  9969. var width = upperRight.x - lowerLeft.x;
  9970. var height = upperRight.y - lowerLeft.y;
  9971. var elevation = upperRight.z - lowerLeft.z;
  9972. result.radius = Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  9973. var center = result.center;
  9974. center.x = lowerLeft.x + width * 0.5;
  9975. center.y = lowerLeft.y + height * 0.5;
  9976. center.z = lowerLeft.z + elevation * 0.5;
  9977. return result;
  9978. };
  9979. var fromRectangle3DScratch = [];
  9980. /**
  9981. * Computes a bounding sphere from an rectangle in 3D. The bounding sphere is created using a subsample of points
  9982. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  9983. *
  9984. * @param {Rectangle} rectangle The valid rectangle used to create a bounding sphere.
  9985. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  9986. * @param {Number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  9987. * @param {BoundingSphere} [result] The object onto which to store the result.
  9988. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9989. */
  9990. BoundingSphere.fromRectangle3D = function(rectangle, ellipsoid, surfaceHeight, result) {
  9991. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  9992. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  9993. var positions;
  9994. if (defined(rectangle)) {
  9995. positions = Rectangle.subsample(rectangle, ellipsoid, surfaceHeight, fromRectangle3DScratch);
  9996. }
  9997. return BoundingSphere.fromPoints(positions, result);
  9998. };
  9999. /**
  10000. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  10001. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  10002. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  10003. * ensure a tight fit.
  10004. *
  10005. * @param {Number[]} positions An array of points that the bounding sphere will enclose. Each point
  10006. * is formed from three elements in the array in the order X, Y, Z.
  10007. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  10008. * origin of the coordinate system. This is useful when the positions are to be used for
  10009. * relative-to-center (RTC) rendering.
  10010. * @param {Number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  10011. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  10012. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  10013. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  10014. * the stride is 5, however, two array elements are skipped and the next position begins at array
  10015. * index 5.
  10016. * @param {BoundingSphere} [result] The object onto which to store the result.
  10017. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  10018. *
  10019. * @example
  10020. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  10021. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  10022. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  10023. * var center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  10024. * var points = [1.0, 2.0, 3.0, 0.1, 0.2,
  10025. * 4.0, 5.0, 6.0, 0.1, 0.2,
  10026. * 7.0, 8.0, 9.0, 0.1, 0.2];
  10027. * var sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  10028. *
  10029. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  10030. */
  10031. BoundingSphere.fromVertices = function(positions, center, stride, result) {
  10032. if (!defined(result)) {
  10033. result = new BoundingSphere();
  10034. }
  10035. if (!defined(positions) || positions.length === 0) {
  10036. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  10037. result.radius = 0.0;
  10038. return result;
  10039. }
  10040. center = defaultValue(center, Cartesian3.ZERO);
  10041. stride = defaultValue(stride, 3);
  10042. if (stride < 3) {
  10043. throw new DeveloperError('stride must be 3 or greater.');
  10044. }
  10045. var currentPos = fromPointsCurrentPos;
  10046. currentPos.x = positions[0] + center.x;
  10047. currentPos.y = positions[1] + center.y;
  10048. currentPos.z = positions[2] + center.z;
  10049. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  10050. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  10051. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  10052. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  10053. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  10054. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  10055. var numElements = positions.length;
  10056. for (var i = 0; i < numElements; i += stride) {
  10057. var x = positions[i] + center.x;
  10058. var y = positions[i + 1] + center.y;
  10059. var z = positions[i + 2] + center.z;
  10060. currentPos.x = x;
  10061. currentPos.y = y;
  10062. currentPos.z = z;
  10063. // Store points containing the the smallest and largest components
  10064. if (x < xMin.x) {
  10065. Cartesian3.clone(currentPos, xMin);
  10066. }
  10067. if (x > xMax.x) {
  10068. Cartesian3.clone(currentPos, xMax);
  10069. }
  10070. if (y < yMin.y) {
  10071. Cartesian3.clone(currentPos, yMin);
  10072. }
  10073. if (y > yMax.y) {
  10074. Cartesian3.clone(currentPos, yMax);
  10075. }
  10076. if (z < zMin.z) {
  10077. Cartesian3.clone(currentPos, zMin);
  10078. }
  10079. if (z > zMax.z) {
  10080. Cartesian3.clone(currentPos, zMax);
  10081. }
  10082. }
  10083. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  10084. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  10085. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  10086. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  10087. // Set the diameter endpoints to the largest span.
  10088. var diameter1 = xMin;
  10089. var diameter2 = xMax;
  10090. var maxSpan = xSpan;
  10091. if (ySpan > maxSpan) {
  10092. maxSpan = ySpan;
  10093. diameter1 = yMin;
  10094. diameter2 = yMax;
  10095. }
  10096. if (zSpan > maxSpan) {
  10097. maxSpan = zSpan;
  10098. diameter1 = zMin;
  10099. diameter2 = zMax;
  10100. }
  10101. // Calculate the center of the initial sphere found by Ritter's algorithm
  10102. var ritterCenter = fromPointsRitterCenter;
  10103. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  10104. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  10105. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  10106. // Calculate the radius of the initial sphere found by Ritter's algorithm
  10107. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  10108. var ritterRadius = Math.sqrt(radiusSquared);
  10109. // Find the center of the sphere found using the Naive method.
  10110. var minBoxPt = fromPointsMinBoxPt;
  10111. minBoxPt.x = xMin.x;
  10112. minBoxPt.y = yMin.y;
  10113. minBoxPt.z = zMin.z;
  10114. var maxBoxPt = fromPointsMaxBoxPt;
  10115. maxBoxPt.x = xMax.x;
  10116. maxBoxPt.y = yMax.y;
  10117. maxBoxPt.z = zMax.z;
  10118. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  10119. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  10120. var naiveRadius = 0;
  10121. for (i = 0; i < numElements; i += stride) {
  10122. currentPos.x = positions[i] + center.x;
  10123. currentPos.y = positions[i + 1] + center.y;
  10124. currentPos.z = positions[i + 2] + center.z;
  10125. // Find the furthest point from the naive center to calculate the naive radius.
  10126. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  10127. if (r > naiveRadius) {
  10128. naiveRadius = r;
  10129. }
  10130. // Make adjustments to the Ritter Sphere to include all points.
  10131. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  10132. if (oldCenterToPointSquared > radiusSquared) {
  10133. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  10134. // Calculate new radius to include the point that lies outside
  10135. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  10136. radiusSquared = ritterRadius * ritterRadius;
  10137. // Calculate center of new Ritter sphere
  10138. var oldToNew = oldCenterToPoint - ritterRadius;
  10139. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  10140. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  10141. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  10142. }
  10143. }
  10144. if (ritterRadius < naiveRadius) {
  10145. Cartesian3.clone(ritterCenter, result.center);
  10146. result.radius = ritterRadius;
  10147. } else {
  10148. Cartesian3.clone(naiveCenter, result.center);
  10149. result.radius = naiveRadius;
  10150. }
  10151. return result;
  10152. };
  10153. /**
  10154. * Computes a tight-fitting bounding sphere enclosing a list of {@link EncodedCartesian3}s, where the points are
  10155. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  10156. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  10157. * ensure a tight fit.
  10158. *
  10159. * @param {Number[]} positionsHigh An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  10160. * is formed from three elements in the array in the order X, Y, Z.
  10161. * @param {Number[]} positionsLow An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  10162. * is formed from three elements in the array in the order X, Y, Z.
  10163. * @param {BoundingSphere} [result] The object onto which to store the result.
  10164. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  10165. *
  10166. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  10167. */
  10168. BoundingSphere.fromEncodedCartesianVertices = function(positionsHigh, positionsLow, result) {
  10169. if (!defined(result)) {
  10170. result = new BoundingSphere();
  10171. }
  10172. if (!defined(positionsHigh) || !defined(positionsLow) || positionsHigh.length !== positionsLow.length || positionsHigh.length === 0) {
  10173. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  10174. result.radius = 0.0;
  10175. return result;
  10176. }
  10177. var currentPos = fromPointsCurrentPos;
  10178. currentPos.x = positionsHigh[0] + positionsLow[0];
  10179. currentPos.y = positionsHigh[1] + positionsLow[1];
  10180. currentPos.z = positionsHigh[2] + positionsLow[2];
  10181. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  10182. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  10183. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  10184. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  10185. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  10186. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  10187. var numElements = positionsHigh.length;
  10188. for (var i = 0; i < numElements; i += 3) {
  10189. var x = positionsHigh[i] + positionsLow[i];
  10190. var y = positionsHigh[i + 1] + positionsLow[i + 1];
  10191. var z = positionsHigh[i + 2] + positionsLow[i + 2];
  10192. currentPos.x = x;
  10193. currentPos.y = y;
  10194. currentPos.z = z;
  10195. // Store points containing the the smallest and largest components
  10196. if (x < xMin.x) {
  10197. Cartesian3.clone(currentPos, xMin);
  10198. }
  10199. if (x > xMax.x) {
  10200. Cartesian3.clone(currentPos, xMax);
  10201. }
  10202. if (y < yMin.y) {
  10203. Cartesian3.clone(currentPos, yMin);
  10204. }
  10205. if (y > yMax.y) {
  10206. Cartesian3.clone(currentPos, yMax);
  10207. }
  10208. if (z < zMin.z) {
  10209. Cartesian3.clone(currentPos, zMin);
  10210. }
  10211. if (z > zMax.z) {
  10212. Cartesian3.clone(currentPos, zMax);
  10213. }
  10214. }
  10215. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  10216. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  10217. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  10218. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  10219. // Set the diameter endpoints to the largest span.
  10220. var diameter1 = xMin;
  10221. var diameter2 = xMax;
  10222. var maxSpan = xSpan;
  10223. if (ySpan > maxSpan) {
  10224. maxSpan = ySpan;
  10225. diameter1 = yMin;
  10226. diameter2 = yMax;
  10227. }
  10228. if (zSpan > maxSpan) {
  10229. maxSpan = zSpan;
  10230. diameter1 = zMin;
  10231. diameter2 = zMax;
  10232. }
  10233. // Calculate the center of the initial sphere found by Ritter's algorithm
  10234. var ritterCenter = fromPointsRitterCenter;
  10235. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  10236. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  10237. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  10238. // Calculate the radius of the initial sphere found by Ritter's algorithm
  10239. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  10240. var ritterRadius = Math.sqrt(radiusSquared);
  10241. // Find the center of the sphere found using the Naive method.
  10242. var minBoxPt = fromPointsMinBoxPt;
  10243. minBoxPt.x = xMin.x;
  10244. minBoxPt.y = yMin.y;
  10245. minBoxPt.z = zMin.z;
  10246. var maxBoxPt = fromPointsMaxBoxPt;
  10247. maxBoxPt.x = xMax.x;
  10248. maxBoxPt.y = yMax.y;
  10249. maxBoxPt.z = zMax.z;
  10250. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  10251. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  10252. var naiveRadius = 0;
  10253. for (i = 0; i < numElements; i += 3) {
  10254. currentPos.x = positionsHigh[i] + positionsLow[i];
  10255. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  10256. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  10257. // Find the furthest point from the naive center to calculate the naive radius.
  10258. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  10259. if (r > naiveRadius) {
  10260. naiveRadius = r;
  10261. }
  10262. // Make adjustments to the Ritter Sphere to include all points.
  10263. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  10264. if (oldCenterToPointSquared > radiusSquared) {
  10265. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  10266. // Calculate new radius to include the point that lies outside
  10267. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  10268. radiusSquared = ritterRadius * ritterRadius;
  10269. // Calculate center of new Ritter sphere
  10270. var oldToNew = oldCenterToPoint - ritterRadius;
  10271. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  10272. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  10273. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  10274. }
  10275. }
  10276. if (ritterRadius < naiveRadius) {
  10277. Cartesian3.clone(ritterCenter, result.center);
  10278. result.radius = ritterRadius;
  10279. } else {
  10280. Cartesian3.clone(naiveCenter, result.center);
  10281. result.radius = naiveRadius;
  10282. }
  10283. return result;
  10284. };
  10285. /**
  10286. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  10287. * tighly and fully encompases the box.
  10288. *
  10289. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  10290. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  10291. * @param {BoundingSphere} [result] The object onto which to store the result.
  10292. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10293. *
  10294. * @example
  10295. * // Create a bounding sphere around the unit cube
  10296. * var sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  10297. */
  10298. BoundingSphere.fromCornerPoints = function(corner, oppositeCorner, result) {
  10299. if (!defined(corner) || !defined(oppositeCorner)) {
  10300. throw new DeveloperError('corner and oppositeCorner are required.');
  10301. }
  10302. if (!defined(result)) {
  10303. result = new BoundingSphere();
  10304. }
  10305. var center = result.center;
  10306. Cartesian3.add(corner, oppositeCorner, center);
  10307. Cartesian3.multiplyByScalar(center, 0.5, center);
  10308. result.radius = Cartesian3.distance(center, oppositeCorner);
  10309. return result;
  10310. };
  10311. /**
  10312. * Creates a bounding sphere encompassing an ellipsoid.
  10313. *
  10314. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  10315. * @param {BoundingSphere} [result] The object onto which to store the result.
  10316. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10317. *
  10318. * @example
  10319. * var boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  10320. */
  10321. BoundingSphere.fromEllipsoid = function(ellipsoid, result) {
  10322. if (!defined(ellipsoid)) {
  10323. throw new DeveloperError('ellipsoid is required.');
  10324. }
  10325. if (!defined(result)) {
  10326. result = new BoundingSphere();
  10327. }
  10328. Cartesian3.clone(Cartesian3.ZERO, result.center);
  10329. result.radius = ellipsoid.maximumRadius;
  10330. return result;
  10331. };
  10332. var fromBoundingSpheresScratch = new Cartesian3();
  10333. /**
  10334. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  10335. *
  10336. * @param {BoundingSphere[]} boundingSpheres The array of bounding spheres.
  10337. * @param {BoundingSphere} [result] The object onto which to store the result.
  10338. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10339. */
  10340. BoundingSphere.fromBoundingSpheres = function(boundingSpheres, result) {
  10341. if (!defined(result)) {
  10342. result = new BoundingSphere();
  10343. }
  10344. if (!defined(boundingSpheres) || boundingSpheres.length === 0) {
  10345. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  10346. result.radius = 0.0;
  10347. return result;
  10348. }
  10349. var length = boundingSpheres.length;
  10350. if (length === 1) {
  10351. return BoundingSphere.clone(boundingSpheres[0], result);
  10352. }
  10353. if (length === 2) {
  10354. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  10355. }
  10356. var positions = [];
  10357. for (var i = 0; i < length; i++) {
  10358. positions.push(boundingSpheres[i].center);
  10359. }
  10360. result = BoundingSphere.fromPoints(positions, result);
  10361. var center = result.center;
  10362. var radius = result.radius;
  10363. for (i = 0; i < length; i++) {
  10364. var tmp = boundingSpheres[i];
  10365. radius = Math.max(radius, Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) + tmp.radius);
  10366. }
  10367. result.radius = radius;
  10368. return result;
  10369. };
  10370. var fromOrientedBoundingBoxScratchU = new Cartesian3();
  10371. var fromOrientedBoundingBoxScratchV = new Cartesian3();
  10372. var fromOrientedBoundingBoxScratchW = new Cartesian3();
  10373. /**
  10374. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  10375. *
  10376. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  10377. * @param {BoundingSphere} [result] The object onto which to store the result.
  10378. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10379. */
  10380. BoundingSphere.fromOrientedBoundingBox = function(orientedBoundingBox, result) {
  10381. if (!defined(result)) {
  10382. result = new BoundingSphere();
  10383. }
  10384. var halfAxes = orientedBoundingBox.halfAxes;
  10385. var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  10386. var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  10387. var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  10388. var uHalf = Cartesian3.magnitude(u);
  10389. var vHalf = Cartesian3.magnitude(v);
  10390. var wHalf = Cartesian3.magnitude(w);
  10391. result.center = Cartesian3.clone(orientedBoundingBox.center, result.center);
  10392. result.radius = Math.max(uHalf, vHalf, wHalf);
  10393. return result;
  10394. };
  10395. /**
  10396. * Duplicates a BoundingSphere instance.
  10397. *
  10398. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  10399. * @param {BoundingSphere} [result] The object onto which to store the result.
  10400. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  10401. */
  10402. BoundingSphere.clone = function(sphere, result) {
  10403. if (!defined(sphere)) {
  10404. return undefined;
  10405. }
  10406. if (!defined(result)) {
  10407. return new BoundingSphere(sphere.center, sphere.radius);
  10408. }
  10409. result.center = Cartesian3.clone(sphere.center, result.center);
  10410. result.radius = sphere.radius;
  10411. return result;
  10412. };
  10413. /**
  10414. * The number of elements used to pack the object into an array.
  10415. * @type {Number}
  10416. */
  10417. BoundingSphere.packedLength = 4;
  10418. /**
  10419. * Stores the provided instance into the provided array.
  10420. *
  10421. * @param {BoundingSphere} value The value to pack.
  10422. * @param {Number[]} array The array to pack into.
  10423. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  10424. *
  10425. * @returns {Number[]} The array that was packed into
  10426. */
  10427. BoundingSphere.pack = function(value, array, startingIndex) {
  10428. if (!defined(value)) {
  10429. throw new DeveloperError('value is required');
  10430. }
  10431. if (!defined(array)) {
  10432. throw new DeveloperError('array is required');
  10433. }
  10434. startingIndex = defaultValue(startingIndex, 0);
  10435. var center = value.center;
  10436. array[startingIndex++] = center.x;
  10437. array[startingIndex++] = center.y;
  10438. array[startingIndex++] = center.z;
  10439. array[startingIndex] = value.radius;
  10440. return array;
  10441. };
  10442. /**
  10443. * Retrieves an instance from a packed array.
  10444. *
  10445. * @param {Number[]} array The packed array.
  10446. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  10447. * @param {BoundingSphere} [result] The object into which to store the result.
  10448. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  10449. */
  10450. BoundingSphere.unpack = function(array, startingIndex, result) {
  10451. if (!defined(array)) {
  10452. throw new DeveloperError('array is required');
  10453. }
  10454. startingIndex = defaultValue(startingIndex, 0);
  10455. if (!defined(result)) {
  10456. result = new BoundingSphere();
  10457. }
  10458. var center = result.center;
  10459. center.x = array[startingIndex++];
  10460. center.y = array[startingIndex++];
  10461. center.z = array[startingIndex++];
  10462. result.radius = array[startingIndex];
  10463. return result;
  10464. };
  10465. var unionScratch = new Cartesian3();
  10466. var unionScratchCenter = new Cartesian3();
  10467. /**
  10468. * Computes a bounding sphere that contains both the left and right bounding spheres.
  10469. *
  10470. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  10471. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  10472. * @param {BoundingSphere} [result] The object onto which to store the result.
  10473. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10474. */
  10475. BoundingSphere.union = function(left, right, result) {
  10476. if (!defined(left)) {
  10477. throw new DeveloperError('left is required.');
  10478. }
  10479. if (!defined(right)) {
  10480. throw new DeveloperError('right is required.');
  10481. }
  10482. if (!defined(result)) {
  10483. result = new BoundingSphere();
  10484. }
  10485. var leftCenter = left.center;
  10486. var leftRadius = left.radius;
  10487. var rightCenter = right.center;
  10488. var rightRadius = right.radius;
  10489. var toRightCenter = Cartesian3.subtract(rightCenter, leftCenter, unionScratch);
  10490. var centerSeparation = Cartesian3.magnitude(toRightCenter);
  10491. if (leftRadius >= (centerSeparation + rightRadius)) {
  10492. // Left sphere wins.
  10493. left.clone(result);
  10494. return result;
  10495. }
  10496. if (rightRadius >= (centerSeparation + leftRadius)) {
  10497. // Right sphere wins.
  10498. right.clone(result);
  10499. return result;
  10500. }
  10501. // There are two tangent points, one on far side of each sphere.
  10502. var halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
  10503. // Compute the center point halfway between the two tangent points.
  10504. var center = Cartesian3.multiplyByScalar(toRightCenter,
  10505. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation, unionScratchCenter);
  10506. Cartesian3.add(center, leftCenter, center);
  10507. Cartesian3.clone(center, result.center);
  10508. result.radius = halfDistanceBetweenTangentPoints;
  10509. return result;
  10510. };
  10511. var expandScratch = new Cartesian3();
  10512. /**
  10513. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  10514. *
  10515. * @param {BoundingSphere} sphere A sphere to expand.
  10516. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  10517. * @param {BoundingSphere} [result] The object onto which to store the result.
  10518. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10519. */
  10520. BoundingSphere.expand = function(sphere, point, result) {
  10521. if (!defined(sphere)) {
  10522. throw new DeveloperError('sphere is required.');
  10523. }
  10524. if (!defined(point)) {
  10525. throw new DeveloperError('point is required.');
  10526. }
  10527. result = BoundingSphere.clone(sphere, result);
  10528. var radius = Cartesian3.magnitude(Cartesian3.subtract(point, result.center, expandScratch));
  10529. if (radius > result.radius) {
  10530. result.radius = radius;
  10531. }
  10532. return result;
  10533. };
  10534. /**
  10535. * Determines which side of a plane a sphere is located.
  10536. *
  10537. * @param {BoundingSphere} sphere The bounding sphere to test.
  10538. * @param {Plane} plane The plane to test against.
  10539. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  10540. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  10541. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  10542. * intersects the plane.
  10543. */
  10544. BoundingSphere.intersectPlane = function(sphere, plane) {
  10545. if (!defined(sphere)) {
  10546. throw new DeveloperError('sphere is required.');
  10547. }
  10548. if (!defined(plane)) {
  10549. throw new DeveloperError('plane is required.');
  10550. }
  10551. var center = sphere.center;
  10552. var radius = sphere.radius;
  10553. var normal = plane.normal;
  10554. var distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
  10555. if (distanceToPlane < -radius) {
  10556. // The center point is negative side of the plane normal
  10557. return Intersect.OUTSIDE;
  10558. } else if (distanceToPlane < radius) {
  10559. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  10560. return Intersect.INTERSECTING;
  10561. }
  10562. return Intersect.INSIDE;
  10563. };
  10564. /**
  10565. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  10566. *
  10567. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  10568. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  10569. * @param {BoundingSphere} [result] The object onto which to store the result.
  10570. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10571. */
  10572. BoundingSphere.transform = function(sphere, transform, result) {
  10573. if (!defined(sphere)) {
  10574. throw new DeveloperError('sphere is required.');
  10575. }
  10576. if (!defined(transform)) {
  10577. throw new DeveloperError('transform is required.');
  10578. }
  10579. if (!defined(result)) {
  10580. result = new BoundingSphere();
  10581. }
  10582. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  10583. result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
  10584. return result;
  10585. };
  10586. var distanceSquaredToScratch = new Cartesian3();
  10587. /**
  10588. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  10589. *
  10590. * @param {BoundingSphere} sphere The sphere.
  10591. * @param {Cartesian3} cartesian The point
  10592. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  10593. *
  10594. * @example
  10595. * // Sort bounding spheres from back to front
  10596. * spheres.sort(function(a, b) {
  10597. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  10598. * });
  10599. */
  10600. BoundingSphere.distanceSquaredTo = function(sphere, cartesian) {
  10601. if (!defined(sphere)) {
  10602. throw new DeveloperError('sphere is required.');
  10603. }
  10604. if (!defined(cartesian)) {
  10605. throw new DeveloperError('cartesian is required.');
  10606. }
  10607. var diff = Cartesian3.subtract(sphere.center, cartesian, distanceSquaredToScratch);
  10608. return Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
  10609. };
  10610. /**
  10611. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  10612. * The transformation matrix is not verified to have a uniform scale of 1.
  10613. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  10614. *
  10615. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  10616. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  10617. * @param {BoundingSphere} [result] The object onto which to store the result.
  10618. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10619. *
  10620. * @example
  10621. * var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  10622. * var boundingSphere = new Cesium.BoundingSphere();
  10623. * var newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  10624. */
  10625. BoundingSphere.transformWithoutScale = function(sphere, transform, result) {
  10626. if (!defined(sphere)) {
  10627. throw new DeveloperError('sphere is required.');
  10628. }
  10629. if (!defined(transform)) {
  10630. throw new DeveloperError('transform is required.');
  10631. }
  10632. if (!defined(result)) {
  10633. result = new BoundingSphere();
  10634. }
  10635. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  10636. result.radius = sphere.radius;
  10637. return result;
  10638. };
  10639. var scratchCartesian3 = new Cartesian3();
  10640. /**
  10641. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  10642. * plus/minus the radius of the bounding sphere.
  10643. * <br>
  10644. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  10645. * closest and farthest planes from position that intersect the bounding sphere.
  10646. *
  10647. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  10648. * @param {Cartesian3} position The position to calculate the distance from.
  10649. * @param {Cartesian3} direction The direction from position.
  10650. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  10651. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  10652. */
  10653. BoundingSphere.computePlaneDistances = function(sphere, position, direction, result) {
  10654. if (!defined(sphere)) {
  10655. throw new DeveloperError('sphere is required.');
  10656. }
  10657. if (!defined(position)) {
  10658. throw new DeveloperError('position is required.');
  10659. }
  10660. if (!defined(direction)) {
  10661. throw new DeveloperError('direction is required.');
  10662. }
  10663. if (!defined(result)) {
  10664. result = new Interval();
  10665. }
  10666. var toCenter = Cartesian3.subtract(sphere.center, position, scratchCartesian3);
  10667. var mag = Cartesian3.dot(direction, toCenter);
  10668. result.start = mag - sphere.radius;
  10669. result.stop = mag + sphere.radius;
  10670. return result;
  10671. };
  10672. var projectTo2DNormalScratch = new Cartesian3();
  10673. var projectTo2DEastScratch = new Cartesian3();
  10674. var projectTo2DNorthScratch = new Cartesian3();
  10675. var projectTo2DWestScratch = new Cartesian3();
  10676. var projectTo2DSouthScratch = new Cartesian3();
  10677. var projectTo2DCartographicScratch = new Cartographic();
  10678. var projectTo2DPositionsScratch = new Array(8);
  10679. for (var n = 0; n < 8; ++n) {
  10680. projectTo2DPositionsScratch[n] = new Cartesian3();
  10681. }
  10682. var projectTo2DProjection = new GeographicProjection();
  10683. /**
  10684. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  10685. *
  10686. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  10687. * @param {Object} [projection=GeographicProjection] The projection to 2D.
  10688. * @param {BoundingSphere} [result] The object onto which to store the result.
  10689. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10690. */
  10691. BoundingSphere.projectTo2D = function(sphere, projection, result) {
  10692. if (!defined(sphere)) {
  10693. throw new DeveloperError('sphere is required.');
  10694. }
  10695. projection = defaultValue(projection, projectTo2DProjection);
  10696. var ellipsoid = projection.ellipsoid;
  10697. var center = sphere.center;
  10698. var radius = sphere.radius;
  10699. var normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  10700. var east = Cartesian3.cross(Cartesian3.UNIT_Z, normal, projectTo2DEastScratch);
  10701. Cartesian3.normalize(east, east);
  10702. var north = Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  10703. Cartesian3.normalize(north, north);
  10704. Cartesian3.multiplyByScalar(normal, radius, normal);
  10705. Cartesian3.multiplyByScalar(north, radius, north);
  10706. Cartesian3.multiplyByScalar(east, radius, east);
  10707. var south = Cartesian3.negate(north, projectTo2DSouthScratch);
  10708. var west = Cartesian3.negate(east, projectTo2DWestScratch);
  10709. var positions = projectTo2DPositionsScratch;
  10710. // top NE corner
  10711. var corner = positions[0];
  10712. Cartesian3.add(normal, north, corner);
  10713. Cartesian3.add(corner, east, corner);
  10714. // top NW corner
  10715. corner = positions[1];
  10716. Cartesian3.add(normal, north, corner);
  10717. Cartesian3.add(corner, west, corner);
  10718. // top SW corner
  10719. corner = positions[2];
  10720. Cartesian3.add(normal, south, corner);
  10721. Cartesian3.add(corner, west, corner);
  10722. // top SE corner
  10723. corner = positions[3];
  10724. Cartesian3.add(normal, south, corner);
  10725. Cartesian3.add(corner, east, corner);
  10726. Cartesian3.negate(normal, normal);
  10727. // bottom NE corner
  10728. corner = positions[4];
  10729. Cartesian3.add(normal, north, corner);
  10730. Cartesian3.add(corner, east, corner);
  10731. // bottom NW corner
  10732. corner = positions[5];
  10733. Cartesian3.add(normal, north, corner);
  10734. Cartesian3.add(corner, west, corner);
  10735. // bottom SW corner
  10736. corner = positions[6];
  10737. Cartesian3.add(normal, south, corner);
  10738. Cartesian3.add(corner, west, corner);
  10739. // bottom SE corner
  10740. corner = positions[7];
  10741. Cartesian3.add(normal, south, corner);
  10742. Cartesian3.add(corner, east, corner);
  10743. var length = positions.length;
  10744. for (var i = 0; i < length; ++i) {
  10745. var position = positions[i];
  10746. Cartesian3.add(center, position, position);
  10747. var cartographic = ellipsoid.cartesianToCartographic(position, projectTo2DCartographicScratch);
  10748. projection.project(cartographic, position);
  10749. }
  10750. result = BoundingSphere.fromPoints(positions, result);
  10751. // swizzle center components
  10752. center = result.center;
  10753. var x = center.x;
  10754. var y = center.y;
  10755. var z = center.z;
  10756. center.x = z;
  10757. center.y = x;
  10758. center.z = y;
  10759. return result;
  10760. };
  10761. /**
  10762. * Determines whether or not a sphere is hidden from view by the occluder.
  10763. *
  10764. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  10765. * @param {Occluder} occluder The occluder.
  10766. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  10767. */
  10768. BoundingSphere.isOccluded = function(sphere, occluder) {
  10769. if (!defined(sphere)) {
  10770. throw new DeveloperError('sphere is required.');
  10771. }
  10772. if (!defined(occluder)) {
  10773. throw new DeveloperError('occluder is required.');
  10774. }
  10775. return !occluder.isBoundingSphereVisible(sphere);
  10776. };
  10777. /**
  10778. * Compares the provided BoundingSphere componentwise and returns
  10779. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10780. *
  10781. * @param {BoundingSphere} [left] The first BoundingSphere.
  10782. * @param {BoundingSphere} [right] The second BoundingSphere.
  10783. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  10784. */
  10785. BoundingSphere.equals = function(left, right) {
  10786. return (left === right) ||
  10787. ((defined(left)) &&
  10788. (defined(right)) &&
  10789. Cartesian3.equals(left.center, right.center) &&
  10790. left.radius === right.radius);
  10791. };
  10792. /**
  10793. * Determines which side of a plane the sphere is located.
  10794. *
  10795. * @param {Plane} plane The plane to test against.
  10796. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  10797. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  10798. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  10799. * intersects the plane.
  10800. */
  10801. BoundingSphere.prototype.intersectPlane = function(plane) {
  10802. return BoundingSphere.intersectPlane(this, plane);
  10803. };
  10804. /**
  10805. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  10806. *
  10807. * @param {Cartesian3} cartesian The point
  10808. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  10809. *
  10810. * @example
  10811. * // Sort bounding spheres from back to front
  10812. * spheres.sort(function(a, b) {
  10813. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  10814. * });
  10815. */
  10816. BoundingSphere.prototype.distanceSquaredTo = function(cartesian) {
  10817. return BoundingSphere.distanceSquaredTo(this, cartesian);
  10818. };
  10819. /**
  10820. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  10821. * plus/minus the radius of the bounding sphere.
  10822. * <br>
  10823. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  10824. * closest and farthest planes from position that intersect the bounding sphere.
  10825. *
  10826. * @param {Cartesian3} position The position to calculate the distance from.
  10827. * @param {Cartesian3} direction The direction from position.
  10828. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  10829. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  10830. */
  10831. BoundingSphere.prototype.computePlaneDistances = function(position, direction, result) {
  10832. return BoundingSphere.computePlaneDistances(this, position, direction, result);
  10833. };
  10834. /**
  10835. * Determines whether or not a sphere is hidden from view by the occluder.
  10836. *
  10837. * @param {Occluder} occluder The occluder.
  10838. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  10839. */
  10840. BoundingSphere.prototype.isOccluded = function(occluder) {
  10841. return BoundingSphere.isOccluded(this, occluder);
  10842. };
  10843. /**
  10844. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  10845. * <code>true</code> if they are equal, <code>false</code> otherwise.
  10846. *
  10847. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  10848. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  10849. */
  10850. BoundingSphere.prototype.equals = function(right) {
  10851. return BoundingSphere.equals(this, right);
  10852. };
  10853. /**
  10854. * Duplicates this BoundingSphere instance.
  10855. *
  10856. * @param {BoundingSphere} [result] The object onto which to store the result.
  10857. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  10858. */
  10859. BoundingSphere.prototype.clone = function(result) {
  10860. return BoundingSphere.clone(this, result);
  10861. };
  10862. return BoundingSphere;
  10863. });
  10864. /*global define*/
  10865. define('Core/EllipsoidalOccluder',[
  10866. './BoundingSphere',
  10867. './Cartesian3',
  10868. './defaultValue',
  10869. './defined',
  10870. './defineProperties',
  10871. './DeveloperError',
  10872. './Rectangle'
  10873. ], function(
  10874. BoundingSphere,
  10875. Cartesian3,
  10876. defaultValue,
  10877. defined,
  10878. defineProperties,
  10879. DeveloperError,
  10880. Rectangle) {
  10881. 'use strict';
  10882. /**
  10883. * Determine whether or not other objects are visible or hidden behind the visible horizon defined by
  10884. * an {@link Ellipsoid} and a camera position. The ellipsoid is assumed to be located at the
  10885. * origin of the coordinate system. This class uses the algorithm described in the
  10886. * {@link http://cesiumjs.org/2013/04/25/Horizon-culling/|Horizon Culling} blog post.
  10887. *
  10888. * @alias EllipsoidalOccluder
  10889. *
  10890. * @param {Ellipsoid} ellipsoid The ellipsoid to use as an occluder.
  10891. * @param {Cartesian3} [cameraPosition] The coordinate of the viewer/camera. If this parameter is not
  10892. * specified, {@link EllipsoidalOccluder#cameraPosition} must be called before
  10893. * testing visibility.
  10894. *
  10895. * @constructor
  10896. *
  10897. * @example
  10898. * // Construct an ellipsoidal occluder with radii 1.0, 1.1, and 0.9.
  10899. * var cameraPosition = new Cesium.Cartesian3(5.0, 6.0, 7.0);
  10900. * var occluderEllipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  10901. * var occluder = new Cesium.EllipsoidalOccluder(occluderEllipsoid, cameraPosition);
  10902. *
  10903. * @private
  10904. */
  10905. function EllipsoidalOccluder(ellipsoid, cameraPosition) {
  10906. if (!defined(ellipsoid)) {
  10907. throw new DeveloperError('ellipsoid is required.');
  10908. }
  10909. this._ellipsoid = ellipsoid;
  10910. this._cameraPosition = new Cartesian3();
  10911. this._cameraPositionInScaledSpace = new Cartesian3();
  10912. this._distanceToLimbInScaledSpaceSquared = 0.0;
  10913. // cameraPosition fills in the above values
  10914. if (defined(cameraPosition)) {
  10915. this.cameraPosition = cameraPosition;
  10916. }
  10917. }
  10918. defineProperties(EllipsoidalOccluder.prototype, {
  10919. /**
  10920. * Gets the occluding ellipsoid.
  10921. * @memberof EllipsoidalOccluder.prototype
  10922. * @type {Ellipsoid}
  10923. */
  10924. ellipsoid : {
  10925. get: function() {
  10926. return this._ellipsoid;
  10927. }
  10928. },
  10929. /**
  10930. * Gets or sets the position of the camera.
  10931. * @memberof EllipsoidalOccluder.prototype
  10932. * @type {Cartesian3}
  10933. */
  10934. cameraPosition : {
  10935. get : function() {
  10936. return this._cameraPosition;
  10937. },
  10938. set : function(cameraPosition) {
  10939. // See http://cesiumjs.org/2013/04/25/Horizon-culling/
  10940. var ellipsoid = this._ellipsoid;
  10941. var cv = ellipsoid.transformPositionToScaledSpace(cameraPosition, this._cameraPositionInScaledSpace);
  10942. var vhMagnitudeSquared = Cartesian3.magnitudeSquared(cv) - 1.0;
  10943. Cartesian3.clone(cameraPosition, this._cameraPosition);
  10944. this._cameraPositionInScaledSpace = cv;
  10945. this._distanceToLimbInScaledSpaceSquared = vhMagnitudeSquared;
  10946. }
  10947. }
  10948. });
  10949. var scratchCartesian = new Cartesian3();
  10950. /**
  10951. * Determines whether or not a point, the <code>occludee</code>, is hidden from view by the occluder.
  10952. *
  10953. * @param {Cartesian3} occludee The point to test for visibility.
  10954. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  10955. *
  10956. * @example
  10957. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  10958. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  10959. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  10960. * var point = new Cesium.Cartesian3(0, -3, -3);
  10961. * occluder.isPointVisible(point); //returns true
  10962. */
  10963. EllipsoidalOccluder.prototype.isPointVisible = function(occludee) {
  10964. var ellipsoid = this._ellipsoid;
  10965. var occludeeScaledSpacePosition = ellipsoid.transformPositionToScaledSpace(occludee, scratchCartesian);
  10966. return this.isScaledSpacePointVisible(occludeeScaledSpacePosition);
  10967. };
  10968. /**
  10969. * Determines whether or not a point expressed in the ellipsoid scaled space, is hidden from view by the
  10970. * occluder. To transform a Cartesian X, Y, Z position in the coordinate system aligned with the ellipsoid
  10971. * into the scaled space, call {@link Ellipsoid#transformPositionToScaledSpace}.
  10972. *
  10973. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space.
  10974. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  10975. *
  10976. * @example
  10977. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  10978. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  10979. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  10980. * var point = new Cesium.Cartesian3(0, -3, -3);
  10981. * var scaledSpacePoint = ellipsoid.transformPositionToScaledSpace(point);
  10982. * occluder.isScaledSpacePointVisible(scaledSpacePoint); //returns true
  10983. */
  10984. EllipsoidalOccluder.prototype.isScaledSpacePointVisible = function(occludeeScaledSpacePosition) {
  10985. // See http://cesiumjs.org/2013/04/25/Horizon-culling/
  10986. var cv = this._cameraPositionInScaledSpace;
  10987. var vhMagnitudeSquared = this._distanceToLimbInScaledSpaceSquared;
  10988. var vt = Cartesian3.subtract(occludeeScaledSpacePosition, cv, scratchCartesian);
  10989. var vtDotVc = -Cartesian3.dot(vt, cv);
  10990. // If vhMagnitudeSquared < 0 then we are below the surface of the ellipsoid and
  10991. // in this case, set the culling plane to be on V.
  10992. var isOccluded = vhMagnitudeSquared < 0 ? vtDotVc > 0 : (vtDotVc > vhMagnitudeSquared &&
  10993. vtDotVc * vtDotVc / Cartesian3.magnitudeSquared(vt) > vhMagnitudeSquared);
  10994. return !isOccluded;
  10995. };
  10996. /**
  10997. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  10998. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  10999. * is expressed in the ellipsoid-scaled space and is suitable for use with
  11000. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  11001. *
  11002. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  11003. * A reasonable direction to use is the direction from the center of the ellipsoid to
  11004. * the center of the bounding sphere computed from the positions. The direction need not
  11005. * be normalized.
  11006. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  11007. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  11008. * ellipsoid's axes.
  11009. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  11010. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  11011. */
  11012. EllipsoidalOccluder.prototype.computeHorizonCullingPoint = function(directionToPoint, positions, result) {
  11013. if (!defined(directionToPoint)) {
  11014. throw new DeveloperError('directionToPoint is required');
  11015. }
  11016. if (!defined(positions)) {
  11017. throw new DeveloperError('positions is required');
  11018. }
  11019. if (!defined(result)) {
  11020. result = new Cartesian3();
  11021. }
  11022. var ellipsoid = this._ellipsoid;
  11023. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  11024. var resultMagnitude = 0.0;
  11025. for (var i = 0, len = positions.length; i < len; ++i) {
  11026. var position = positions[i];
  11027. var candidateMagnitude = computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint);
  11028. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  11029. }
  11030. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  11031. };
  11032. var positionScratch = new Cartesian3();
  11033. /**
  11034. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  11035. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  11036. * is expressed in the ellipsoid-scaled space and is suitable for use with
  11037. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  11038. *
  11039. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  11040. * A reasonable direction to use is the direction from the center of the ellipsoid to
  11041. * the center of the bounding sphere computed from the positions. The direction need not
  11042. * be normalized.
  11043. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  11044. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  11045. * ellipsoid's axes.
  11046. * @param {Number} [stride=3]
  11047. * @param {Cartesian3} [center=Cartesian3.ZERO]
  11048. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  11049. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  11050. */
  11051. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVertices = function(directionToPoint, vertices, stride, center, result) {
  11052. if (!defined(directionToPoint)) {
  11053. throw new DeveloperError('directionToPoint is required');
  11054. }
  11055. if (!defined(vertices)) {
  11056. throw new DeveloperError('vertices is required');
  11057. }
  11058. if (!defined(stride)) {
  11059. throw new DeveloperError('stride is required');
  11060. }
  11061. if (!defined(result)) {
  11062. result = new Cartesian3();
  11063. }
  11064. center = defaultValue(center, Cartesian3.ZERO);
  11065. var ellipsoid = this._ellipsoid;
  11066. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  11067. var resultMagnitude = 0.0;
  11068. for (var i = 0, len = vertices.length; i < len; i += stride) {
  11069. positionScratch.x = vertices[i] + center.x;
  11070. positionScratch.y = vertices[i + 1] + center.y;
  11071. positionScratch.z = vertices[i + 2] + center.z;
  11072. var candidateMagnitude = computeMagnitude(ellipsoid, positionScratch, scaledSpaceDirectionToPoint);
  11073. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  11074. }
  11075. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  11076. };
  11077. var subsampleScratch = [];
  11078. /**
  11079. * Computes a point that can be used for horizon culling of an rectangle. If the point is below
  11080. * the horizon, the ellipsoid-conforming rectangle is guaranteed to be below the horizon as well.
  11081. * The returned point is expressed in the ellipsoid-scaled space and is suitable for use with
  11082. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  11083. *
  11084. * @param {Rectangle} rectangle The rectangle for which to compute the horizon culling point.
  11085. * @param {Ellipsoid} ellipsoid The ellipsoid on which the rectangle is defined. This may be different from
  11086. * the ellipsoid used by this instance for occlusion testing.
  11087. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  11088. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  11089. */
  11090. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromRectangle = function(rectangle, ellipsoid, result) {
  11091. if (!defined(rectangle)) {
  11092. throw new DeveloperError('rectangle is required.');
  11093. }
  11094. var positions = Rectangle.subsample(rectangle, ellipsoid, 0.0, subsampleScratch);
  11095. var bs = BoundingSphere.fromPoints(positions);
  11096. // If the bounding sphere center is too close to the center of the occluder, it doesn't make
  11097. // sense to try to horizon cull it.
  11098. if (Cartesian3.magnitude(bs.center) < 0.1 * ellipsoid.minimumRadius) {
  11099. return undefined;
  11100. }
  11101. return this.computeHorizonCullingPoint(bs.center, positions, result);
  11102. };
  11103. var scaledSpaceScratch = new Cartesian3();
  11104. var directionScratch = new Cartesian3();
  11105. function computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint) {
  11106. var scaledSpacePosition = ellipsoid.transformPositionToScaledSpace(position, scaledSpaceScratch);
  11107. var magnitudeSquared = Cartesian3.magnitudeSquared(scaledSpacePosition);
  11108. var magnitude = Math.sqrt(magnitudeSquared);
  11109. var direction = Cartesian3.divideByScalar(scaledSpacePosition, magnitude, directionScratch);
  11110. // For the purpose of this computation, points below the ellipsoid are consider to be on it instead.
  11111. magnitudeSquared = Math.max(1.0, magnitudeSquared);
  11112. magnitude = Math.max(1.0, magnitude);
  11113. var cosAlpha = Cartesian3.dot(direction, scaledSpaceDirectionToPoint);
  11114. var sinAlpha = Cartesian3.magnitude(Cartesian3.cross(direction, scaledSpaceDirectionToPoint, direction));
  11115. var cosBeta = 1.0 / magnitude;
  11116. var sinBeta = Math.sqrt(magnitudeSquared - 1.0) * cosBeta;
  11117. return 1.0 / (cosAlpha * cosBeta - sinAlpha * sinBeta);
  11118. }
  11119. function magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result) {
  11120. // The horizon culling point is undefined if there were no positions from which to compute it,
  11121. // the directionToPoint is pointing opposite all of the positions, or if we computed NaN or infinity.
  11122. if (resultMagnitude <= 0.0 || resultMagnitude === 1.0 / 0.0 || resultMagnitude !== resultMagnitude) {
  11123. return undefined;
  11124. }
  11125. return Cartesian3.multiplyByScalar(scaledSpaceDirectionToPoint, resultMagnitude, result);
  11126. }
  11127. var directionToPointScratch = new Cartesian3();
  11128. function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
  11129. if (Cartesian3.equals(directionToPoint, Cartesian3.ZERO)) {
  11130. return directionToPoint;
  11131. }
  11132. ellipsoid.transformPositionToScaledSpace(directionToPoint, directionToPointScratch);
  11133. return Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
  11134. }
  11135. return EllipsoidalOccluder;
  11136. });
  11137. /*global define*/
  11138. define('Core/WebGLConstants',[
  11139. './freezeObject'
  11140. ], function(
  11141. freezeObject) {
  11142. 'use strict';
  11143. /**
  11144. * Enum containing WebGL Constant values by name.
  11145. * for use without an active WebGL context, or in cases where certain constants are unavailable using the WebGL context
  11146. * (For example, in [Safari 9]{@link https://github.com/AnalyticalGraphicsInc/cesium/issues/2989}).
  11147. *
  11148. * These match the constants from the [WebGL 1.0]{@link https://www.khronos.org/registry/webgl/specs/latest/1.0/}
  11149. * and [WebGL 2.0]{@link https://www.khronos.org/registry/webgl/specs/latest/2.0/}
  11150. * specifications.
  11151. *
  11152. * @exports WebGLConstants
  11153. */
  11154. var WebGLConstants = {
  11155. DEPTH_BUFFER_BIT : 0x00000100,
  11156. STENCIL_BUFFER_BIT : 0x00000400,
  11157. COLOR_BUFFER_BIT : 0x00004000,
  11158. POINTS : 0x0000,
  11159. LINES : 0x0001,
  11160. LINE_LOOP : 0x0002,
  11161. LINE_STRIP : 0x0003,
  11162. TRIANGLES : 0x0004,
  11163. TRIANGLE_STRIP : 0x0005,
  11164. TRIANGLE_FAN : 0x0006,
  11165. ZERO : 0,
  11166. ONE : 1,
  11167. SRC_COLOR : 0x0300,
  11168. ONE_MINUS_SRC_COLOR : 0x0301,
  11169. SRC_ALPHA : 0x0302,
  11170. ONE_MINUS_SRC_ALPHA : 0x0303,
  11171. DST_ALPHA : 0x0304,
  11172. ONE_MINUS_DST_ALPHA : 0x0305,
  11173. DST_COLOR : 0x0306,
  11174. ONE_MINUS_DST_COLOR : 0x0307,
  11175. SRC_ALPHA_SATURATE : 0x0308,
  11176. FUNC_ADD : 0x8006,
  11177. BLEND_EQUATION : 0x8009,
  11178. BLEND_EQUATION_RGB : 0x8009, // same as BLEND_EQUATION
  11179. BLEND_EQUATION_ALPHA : 0x883D,
  11180. FUNC_SUBTRACT : 0x800A,
  11181. FUNC_REVERSE_SUBTRACT : 0x800B,
  11182. BLEND_DST_RGB : 0x80C8,
  11183. BLEND_SRC_RGB : 0x80C9,
  11184. BLEND_DST_ALPHA : 0x80CA,
  11185. BLEND_SRC_ALPHA : 0x80CB,
  11186. CONSTANT_COLOR : 0x8001,
  11187. ONE_MINUS_CONSTANT_COLOR : 0x8002,
  11188. CONSTANT_ALPHA : 0x8003,
  11189. ONE_MINUS_CONSTANT_ALPHA : 0x8004,
  11190. BLEND_COLOR : 0x8005,
  11191. ARRAY_BUFFER : 0x8892,
  11192. ELEMENT_ARRAY_BUFFER : 0x8893,
  11193. ARRAY_BUFFER_BINDING : 0x8894,
  11194. ELEMENT_ARRAY_BUFFER_BINDING : 0x8895,
  11195. STREAM_DRAW : 0x88E0,
  11196. STATIC_DRAW : 0x88E4,
  11197. DYNAMIC_DRAW : 0x88E8,
  11198. BUFFER_SIZE : 0x8764,
  11199. BUFFER_USAGE : 0x8765,
  11200. CURRENT_VERTEX_ATTRIB : 0x8626,
  11201. FRONT : 0x0404,
  11202. BACK : 0x0405,
  11203. FRONT_AND_BACK : 0x0408,
  11204. CULL_FACE : 0x0B44,
  11205. BLEND : 0x0BE2,
  11206. DITHER : 0x0BD0,
  11207. STENCIL_TEST : 0x0B90,
  11208. DEPTH_TEST : 0x0B71,
  11209. SCISSOR_TEST : 0x0C11,
  11210. POLYGON_OFFSET_FILL : 0x8037,
  11211. SAMPLE_ALPHA_TO_COVERAGE : 0x809E,
  11212. SAMPLE_COVERAGE : 0x80A0,
  11213. NO_ERROR : 0,
  11214. INVALID_ENUM : 0x0500,
  11215. INVALID_VALUE : 0x0501,
  11216. INVALID_OPERATION : 0x0502,
  11217. OUT_OF_MEMORY : 0x0505,
  11218. CW : 0x0900,
  11219. CCW : 0x0901,
  11220. LINE_WIDTH : 0x0B21,
  11221. ALIASED_POINT_SIZE_RANGE : 0x846D,
  11222. ALIASED_LINE_WIDTH_RANGE : 0x846E,
  11223. CULL_FACE_MODE : 0x0B45,
  11224. FRONT_FACE : 0x0B46,
  11225. DEPTH_RANGE : 0x0B70,
  11226. DEPTH_WRITEMASK : 0x0B72,
  11227. DEPTH_CLEAR_VALUE : 0x0B73,
  11228. DEPTH_FUNC : 0x0B74,
  11229. STENCIL_CLEAR_VALUE : 0x0B91,
  11230. STENCIL_FUNC : 0x0B92,
  11231. STENCIL_FAIL : 0x0B94,
  11232. STENCIL_PASS_DEPTH_FAIL : 0x0B95,
  11233. STENCIL_PASS_DEPTH_PASS : 0x0B96,
  11234. STENCIL_REF : 0x0B97,
  11235. STENCIL_VALUE_MASK : 0x0B93,
  11236. STENCIL_WRITEMASK : 0x0B98,
  11237. STENCIL_BACK_FUNC : 0x8800,
  11238. STENCIL_BACK_FAIL : 0x8801,
  11239. STENCIL_BACK_PASS_DEPTH_FAIL : 0x8802,
  11240. STENCIL_BACK_PASS_DEPTH_PASS : 0x8803,
  11241. STENCIL_BACK_REF : 0x8CA3,
  11242. STENCIL_BACK_VALUE_MASK : 0x8CA4,
  11243. STENCIL_BACK_WRITEMASK : 0x8CA5,
  11244. VIEWPORT : 0x0BA2,
  11245. SCISSOR_BOX : 0x0C10,
  11246. COLOR_CLEAR_VALUE : 0x0C22,
  11247. COLOR_WRITEMASK : 0x0C23,
  11248. UNPACK_ALIGNMENT : 0x0CF5,
  11249. PACK_ALIGNMENT : 0x0D05,
  11250. MAX_TEXTURE_SIZE : 0x0D33,
  11251. MAX_VIEWPORT_DIMS : 0x0D3A,
  11252. SUBPIXEL_BITS : 0x0D50,
  11253. RED_BITS : 0x0D52,
  11254. GREEN_BITS : 0x0D53,
  11255. BLUE_BITS : 0x0D54,
  11256. ALPHA_BITS : 0x0D55,
  11257. DEPTH_BITS : 0x0D56,
  11258. STENCIL_BITS : 0x0D57,
  11259. POLYGON_OFFSET_UNITS : 0x2A00,
  11260. POLYGON_OFFSET_FACTOR : 0x8038,
  11261. TEXTURE_BINDING_2D : 0x8069,
  11262. SAMPLE_BUFFERS : 0x80A8,
  11263. SAMPLES : 0x80A9,
  11264. SAMPLE_COVERAGE_VALUE : 0x80AA,
  11265. SAMPLE_COVERAGE_INVERT : 0x80AB,
  11266. COMPRESSED_TEXTURE_FORMATS : 0x86A3,
  11267. DONT_CARE : 0x1100,
  11268. FASTEST : 0x1101,
  11269. NICEST : 0x1102,
  11270. GENERATE_MIPMAP_HINT : 0x8192,
  11271. BYTE : 0x1400,
  11272. UNSIGNED_BYTE : 0x1401,
  11273. SHORT : 0x1402,
  11274. UNSIGNED_SHORT : 0x1403,
  11275. INT : 0x1404,
  11276. UNSIGNED_INT : 0x1405,
  11277. FLOAT : 0x1406,
  11278. DEPTH_COMPONENT : 0x1902,
  11279. ALPHA : 0x1906,
  11280. RGB : 0x1907,
  11281. RGBA : 0x1908,
  11282. LUMINANCE : 0x1909,
  11283. LUMINANCE_ALPHA : 0x190A,
  11284. UNSIGNED_SHORT_4_4_4_4 : 0x8033,
  11285. UNSIGNED_SHORT_5_5_5_1 : 0x8034,
  11286. UNSIGNED_SHORT_5_6_5 : 0x8363,
  11287. FRAGMENT_SHADER : 0x8B30,
  11288. VERTEX_SHADER : 0x8B31,
  11289. MAX_VERTEX_ATTRIBS : 0x8869,
  11290. MAX_VERTEX_UNIFORM_VECTORS : 0x8DFB,
  11291. MAX_VARYING_VECTORS : 0x8DFC,
  11292. MAX_COMBINED_TEXTURE_IMAGE_UNITS : 0x8B4D,
  11293. MAX_VERTEX_TEXTURE_IMAGE_UNITS : 0x8B4C,
  11294. MAX_TEXTURE_IMAGE_UNITS : 0x8872,
  11295. MAX_FRAGMENT_UNIFORM_VECTORS : 0x8DFD,
  11296. SHADER_TYPE : 0x8B4F,
  11297. DELETE_STATUS : 0x8B80,
  11298. LINK_STATUS : 0x8B82,
  11299. VALIDATE_STATUS : 0x8B83,
  11300. ATTACHED_SHADERS : 0x8B85,
  11301. ACTIVE_UNIFORMS : 0x8B86,
  11302. ACTIVE_ATTRIBUTES : 0x8B89,
  11303. SHADING_LANGUAGE_VERSION : 0x8B8C,
  11304. CURRENT_PROGRAM : 0x8B8D,
  11305. NEVER : 0x0200,
  11306. LESS : 0x0201,
  11307. EQUAL : 0x0202,
  11308. LEQUAL : 0x0203,
  11309. GREATER : 0x0204,
  11310. NOTEQUAL : 0x0205,
  11311. GEQUAL : 0x0206,
  11312. ALWAYS : 0x0207,
  11313. KEEP : 0x1E00,
  11314. REPLACE : 0x1E01,
  11315. INCR : 0x1E02,
  11316. DECR : 0x1E03,
  11317. INVERT : 0x150A,
  11318. INCR_WRAP : 0x8507,
  11319. DECR_WRAP : 0x8508,
  11320. VENDOR : 0x1F00,
  11321. RENDERER : 0x1F01,
  11322. VERSION : 0x1F02,
  11323. NEAREST : 0x2600,
  11324. LINEAR : 0x2601,
  11325. NEAREST_MIPMAP_NEAREST : 0x2700,
  11326. LINEAR_MIPMAP_NEAREST : 0x2701,
  11327. NEAREST_MIPMAP_LINEAR : 0x2702,
  11328. LINEAR_MIPMAP_LINEAR : 0x2703,
  11329. TEXTURE_MAG_FILTER : 0x2800,
  11330. TEXTURE_MIN_FILTER : 0x2801,
  11331. TEXTURE_WRAP_S : 0x2802,
  11332. TEXTURE_WRAP_T : 0x2803,
  11333. TEXTURE_2D : 0x0DE1,
  11334. TEXTURE : 0x1702,
  11335. TEXTURE_CUBE_MAP : 0x8513,
  11336. TEXTURE_BINDING_CUBE_MAP : 0x8514,
  11337. TEXTURE_CUBE_MAP_POSITIVE_X : 0x8515,
  11338. TEXTURE_CUBE_MAP_NEGATIVE_X : 0x8516,
  11339. TEXTURE_CUBE_MAP_POSITIVE_Y : 0x8517,
  11340. TEXTURE_CUBE_MAP_NEGATIVE_Y : 0x8518,
  11341. TEXTURE_CUBE_MAP_POSITIVE_Z : 0x8519,
  11342. TEXTURE_CUBE_MAP_NEGATIVE_Z : 0x851A,
  11343. MAX_CUBE_MAP_TEXTURE_SIZE : 0x851C,
  11344. TEXTURE0 : 0x84C0,
  11345. TEXTURE1 : 0x84C1,
  11346. TEXTURE2 : 0x84C2,
  11347. TEXTURE3 : 0x84C3,
  11348. TEXTURE4 : 0x84C4,
  11349. TEXTURE5 : 0x84C5,
  11350. TEXTURE6 : 0x84C6,
  11351. TEXTURE7 : 0x84C7,
  11352. TEXTURE8 : 0x84C8,
  11353. TEXTURE9 : 0x84C9,
  11354. TEXTURE10 : 0x84CA,
  11355. TEXTURE11 : 0x84CB,
  11356. TEXTURE12 : 0x84CC,
  11357. TEXTURE13 : 0x84CD,
  11358. TEXTURE14 : 0x84CE,
  11359. TEXTURE15 : 0x84CF,
  11360. TEXTURE16 : 0x84D0,
  11361. TEXTURE17 : 0x84D1,
  11362. TEXTURE18 : 0x84D2,
  11363. TEXTURE19 : 0x84D3,
  11364. TEXTURE20 : 0x84D4,
  11365. TEXTURE21 : 0x84D5,
  11366. TEXTURE22 : 0x84D6,
  11367. TEXTURE23 : 0x84D7,
  11368. TEXTURE24 : 0x84D8,
  11369. TEXTURE25 : 0x84D9,
  11370. TEXTURE26 : 0x84DA,
  11371. TEXTURE27 : 0x84DB,
  11372. TEXTURE28 : 0x84DC,
  11373. TEXTURE29 : 0x84DD,
  11374. TEXTURE30 : 0x84DE,
  11375. TEXTURE31 : 0x84DF,
  11376. ACTIVE_TEXTURE : 0x84E0,
  11377. REPEAT : 0x2901,
  11378. CLAMP_TO_EDGE : 0x812F,
  11379. MIRRORED_REPEAT : 0x8370,
  11380. FLOAT_VEC2 : 0x8B50,
  11381. FLOAT_VEC3 : 0x8B51,
  11382. FLOAT_VEC4 : 0x8B52,
  11383. INT_VEC2 : 0x8B53,
  11384. INT_VEC3 : 0x8B54,
  11385. INT_VEC4 : 0x8B55,
  11386. BOOL : 0x8B56,
  11387. BOOL_VEC2 : 0x8B57,
  11388. BOOL_VEC3 : 0x8B58,
  11389. BOOL_VEC4 : 0x8B59,
  11390. FLOAT_MAT2 : 0x8B5A,
  11391. FLOAT_MAT3 : 0x8B5B,
  11392. FLOAT_MAT4 : 0x8B5C,
  11393. SAMPLER_2D : 0x8B5E,
  11394. SAMPLER_CUBE : 0x8B60,
  11395. VERTEX_ATTRIB_ARRAY_ENABLED : 0x8622,
  11396. VERTEX_ATTRIB_ARRAY_SIZE : 0x8623,
  11397. VERTEX_ATTRIB_ARRAY_STRIDE : 0x8624,
  11398. VERTEX_ATTRIB_ARRAY_TYPE : 0x8625,
  11399. VERTEX_ATTRIB_ARRAY_NORMALIZED : 0x886A,
  11400. VERTEX_ATTRIB_ARRAY_POINTER : 0x8645,
  11401. VERTEX_ATTRIB_ARRAY_BUFFER_BINDING : 0x889F,
  11402. IMPLEMENTATION_COLOR_READ_TYPE : 0x8B9A,
  11403. IMPLEMENTATION_COLOR_READ_FORMAT : 0x8B9B,
  11404. COMPILE_STATUS : 0x8B81,
  11405. LOW_FLOAT : 0x8DF0,
  11406. MEDIUM_FLOAT : 0x8DF1,
  11407. HIGH_FLOAT : 0x8DF2,
  11408. LOW_INT : 0x8DF3,
  11409. MEDIUM_INT : 0x8DF4,
  11410. HIGH_INT : 0x8DF5,
  11411. FRAMEBUFFER : 0x8D40,
  11412. RENDERBUFFER : 0x8D41,
  11413. RGBA4 : 0x8056,
  11414. RGB5_A1 : 0x8057,
  11415. RGB565 : 0x8D62,
  11416. DEPTH_COMPONENT16 : 0x81A5,
  11417. STENCIL_INDEX : 0x1901,
  11418. STENCIL_INDEX8 : 0x8D48,
  11419. DEPTH_STENCIL : 0x84F9,
  11420. RENDERBUFFER_WIDTH : 0x8D42,
  11421. RENDERBUFFER_HEIGHT : 0x8D43,
  11422. RENDERBUFFER_INTERNAL_FORMAT : 0x8D44,
  11423. RENDERBUFFER_RED_SIZE : 0x8D50,
  11424. RENDERBUFFER_GREEN_SIZE : 0x8D51,
  11425. RENDERBUFFER_BLUE_SIZE : 0x8D52,
  11426. RENDERBUFFER_ALPHA_SIZE : 0x8D53,
  11427. RENDERBUFFER_DEPTH_SIZE : 0x8D54,
  11428. RENDERBUFFER_STENCIL_SIZE : 0x8D55,
  11429. FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE : 0x8CD0,
  11430. FRAMEBUFFER_ATTACHMENT_OBJECT_NAME : 0x8CD1,
  11431. FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL : 0x8CD2,
  11432. FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE : 0x8CD3,
  11433. COLOR_ATTACHMENT0 : 0x8CE0,
  11434. DEPTH_ATTACHMENT : 0x8D00,
  11435. STENCIL_ATTACHMENT : 0x8D20,
  11436. DEPTH_STENCIL_ATTACHMENT : 0x821A,
  11437. NONE : 0,
  11438. FRAMEBUFFER_COMPLETE : 0x8CD5,
  11439. FRAMEBUFFER_INCOMPLETE_ATTACHMENT : 0x8CD6,
  11440. FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : 0x8CD7,
  11441. FRAMEBUFFER_INCOMPLETE_DIMENSIONS : 0x8CD9,
  11442. FRAMEBUFFER_UNSUPPORTED : 0x8CDD,
  11443. FRAMEBUFFER_BINDING : 0x8CA6,
  11444. RENDERBUFFER_BINDING : 0x8CA7,
  11445. MAX_RENDERBUFFER_SIZE : 0x84E8,
  11446. INVALID_FRAMEBUFFER_OPERATION : 0x0506,
  11447. UNPACK_FLIP_Y_WEBGL : 0x9240,
  11448. UNPACK_PREMULTIPLY_ALPHA_WEBGL : 0x9241,
  11449. CONTEXT_LOST_WEBGL : 0x9242,
  11450. UNPACK_COLORSPACE_CONVERSION_WEBGL : 0x9243,
  11451. BROWSER_DEFAULT_WEBGL : 0x9244,
  11452. // Desktop OpenGL
  11453. DOUBLE : 0x140A,
  11454. // WebGL 2
  11455. READ_BUFFER : 0x0C02,
  11456. UNPACK_ROW_LENGTH : 0x0CF2,
  11457. UNPACK_SKIP_ROWS : 0x0CF3,
  11458. UNPACK_SKIP_PIXELS : 0x0CF4,
  11459. PACK_ROW_LENGTH : 0x0D02,
  11460. PACK_SKIP_ROWS : 0x0D03,
  11461. PACK_SKIP_PIXELS : 0x0D04,
  11462. COLOR : 0x1800,
  11463. DEPTH : 0x1801,
  11464. STENCIL : 0x1802,
  11465. RED : 0x1903,
  11466. RGB8 : 0x8051,
  11467. RGBA8 : 0x8058,
  11468. RGB10_A2 : 0x8059,
  11469. TEXTURE_BINDING_3D : 0x806A,
  11470. UNPACK_SKIP_IMAGES : 0x806D,
  11471. UNPACK_IMAGE_HEIGHT : 0x806E,
  11472. TEXTURE_3D : 0x806F,
  11473. TEXTURE_WRAP_R : 0x8072,
  11474. MAX_3D_TEXTURE_SIZE : 0x8073,
  11475. UNSIGNED_INT_2_10_10_10_REV : 0x8368,
  11476. MAX_ELEMENTS_VERTICES : 0x80E8,
  11477. MAX_ELEMENTS_INDICES : 0x80E9,
  11478. TEXTURE_MIN_LOD : 0x813A,
  11479. TEXTURE_MAX_LOD : 0x813B,
  11480. TEXTURE_BASE_LEVEL : 0x813C,
  11481. TEXTURE_MAX_LEVEL : 0x813D,
  11482. MIN : 0x8007,
  11483. MAX : 0x8008,
  11484. DEPTH_COMPONENT24 : 0x81A6,
  11485. MAX_TEXTURE_LOD_BIAS : 0x84FD,
  11486. TEXTURE_COMPARE_MODE : 0x884C,
  11487. TEXTURE_COMPARE_FUNC : 0x884D,
  11488. CURRENT_QUERY : 0x8865,
  11489. QUERY_RESULT : 0x8866,
  11490. QUERY_RESULT_AVAILABLE : 0x8867,
  11491. STREAM_READ : 0x88E1,
  11492. STREAM_COPY : 0x88E2,
  11493. STATIC_READ : 0x88E5,
  11494. STATIC_COPY : 0x88E6,
  11495. DYNAMIC_READ : 0x88E9,
  11496. DYNAMIC_COPY : 0x88EA,
  11497. MAX_DRAW_BUFFERS : 0x8824,
  11498. DRAW_BUFFER0 : 0x8825,
  11499. DRAW_BUFFER1 : 0x8826,
  11500. DRAW_BUFFER2 : 0x8827,
  11501. DRAW_BUFFER3 : 0x8828,
  11502. DRAW_BUFFER4 : 0x8829,
  11503. DRAW_BUFFER5 : 0x882A,
  11504. DRAW_BUFFER6 : 0x882B,
  11505. DRAW_BUFFER7 : 0x882C,
  11506. DRAW_BUFFER8 : 0x882D,
  11507. DRAW_BUFFER9 : 0x882E,
  11508. DRAW_BUFFER10 : 0x882F,
  11509. DRAW_BUFFER11 : 0x8830,
  11510. DRAW_BUFFER12 : 0x8831,
  11511. DRAW_BUFFER13 : 0x8832,
  11512. DRAW_BUFFER14 : 0x8833,
  11513. DRAW_BUFFER15 : 0x8834,
  11514. MAX_FRAGMENT_UNIFORM_COMPONENTS : 0x8B49,
  11515. MAX_VERTEX_UNIFORM_COMPONENTS : 0x8B4A,
  11516. SAMPLER_3D : 0x8B5F,
  11517. SAMPLER_2D_SHADOW : 0x8B62,
  11518. FRAGMENT_SHADER_DERIVATIVE_HINT : 0x8B8B,
  11519. PIXEL_PACK_BUFFER : 0x88EB,
  11520. PIXEL_UNPACK_BUFFER : 0x88EC,
  11521. PIXEL_PACK_BUFFER_BINDING : 0x88ED,
  11522. PIXEL_UNPACK_BUFFER_BINDING : 0x88EF,
  11523. FLOAT_MAT2x3 : 0x8B65,
  11524. FLOAT_MAT2x4 : 0x8B66,
  11525. FLOAT_MAT3x2 : 0x8B67,
  11526. FLOAT_MAT3x4 : 0x8B68,
  11527. FLOAT_MAT4x2 : 0x8B69,
  11528. FLOAT_MAT4x3 : 0x8B6A,
  11529. SRGB : 0x8C40,
  11530. SRGB8 : 0x8C41,
  11531. SRGB8_ALPHA8 : 0x8C43,
  11532. COMPARE_REF_TO_TEXTURE : 0x884E,
  11533. RGBA32F : 0x8814,
  11534. RGB32F : 0x8815,
  11535. RGBA16F : 0x881A,
  11536. RGB16F : 0x881B,
  11537. VERTEX_ATTRIB_ARRAY_INTEGER : 0x88FD,
  11538. MAX_ARRAY_TEXTURE_LAYERS : 0x88FF,
  11539. MIN_PROGRAM_TEXEL_OFFSET : 0x8904,
  11540. MAX_PROGRAM_TEXEL_OFFSET : 0x8905,
  11541. MAX_VARYING_COMPONENTS : 0x8B4B,
  11542. TEXTURE_2D_ARRAY : 0x8C1A,
  11543. TEXTURE_BINDING_2D_ARRAY : 0x8C1D,
  11544. R11F_G11F_B10F : 0x8C3A,
  11545. UNSIGNED_INT_10F_11F_11F_REV : 0x8C3B,
  11546. RGB9_E5 : 0x8C3D,
  11547. UNSIGNED_INT_5_9_9_9_REV : 0x8C3E,
  11548. TRANSFORM_FEEDBACK_BUFFER_MODE : 0x8C7F,
  11549. MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS : 0x8C80,
  11550. TRANSFORM_FEEDBACK_VARYINGS : 0x8C83,
  11551. TRANSFORM_FEEDBACK_BUFFER_START : 0x8C84,
  11552. TRANSFORM_FEEDBACK_BUFFER_SIZE : 0x8C85,
  11553. TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN : 0x8C88,
  11554. RASTERIZER_DISCARD : 0x8C89,
  11555. MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS : 0x8C8A,
  11556. MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS : 0x8C8B,
  11557. INTERLEAVED_ATTRIBS : 0x8C8C,
  11558. SEPARATE_ATTRIBS : 0x8C8D,
  11559. TRANSFORM_FEEDBACK_BUFFER : 0x8C8E,
  11560. TRANSFORM_FEEDBACK_BUFFER_BINDING : 0x8C8F,
  11561. RGBA32UI : 0x8D70,
  11562. RGB32UI : 0x8D71,
  11563. RGBA16UI : 0x8D76,
  11564. RGB16UI : 0x8D77,
  11565. RGBA8UI : 0x8D7C,
  11566. RGB8UI : 0x8D7D,
  11567. RGBA32I : 0x8D82,
  11568. RGB32I : 0x8D83,
  11569. RGBA16I : 0x8D88,
  11570. RGB16I : 0x8D89,
  11571. RGBA8I : 0x8D8E,
  11572. RGB8I : 0x8D8F,
  11573. RED_INTEGER : 0x8D94,
  11574. RGB_INTEGER : 0x8D98,
  11575. RGBA_INTEGER : 0x8D99,
  11576. SAMPLER_2D_ARRAY : 0x8DC1,
  11577. SAMPLER_2D_ARRAY_SHADOW : 0x8DC4,
  11578. SAMPLER_CUBE_SHADOW : 0x8DC5,
  11579. UNSIGNED_INT_VEC2 : 0x8DC6,
  11580. UNSIGNED_INT_VEC3 : 0x8DC7,
  11581. UNSIGNED_INT_VEC4 : 0x8DC8,
  11582. INT_SAMPLER_2D : 0x8DCA,
  11583. INT_SAMPLER_3D : 0x8DCB,
  11584. INT_SAMPLER_CUBE : 0x8DCC,
  11585. INT_SAMPLER_2D_ARRAY : 0x8DCF,
  11586. UNSIGNED_INT_SAMPLER_2D : 0x8DD2,
  11587. UNSIGNED_INT_SAMPLER_3D : 0x8DD3,
  11588. UNSIGNED_INT_SAMPLER_CUBE : 0x8DD4,
  11589. UNSIGNED_INT_SAMPLER_2D_ARRAY : 0x8DD7,
  11590. DEPTH_COMPONENT32F : 0x8CAC,
  11591. DEPTH32F_STENCIL8 : 0x8CAD,
  11592. FLOAT_32_UNSIGNED_INT_24_8_REV : 0x8DAD,
  11593. FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING : 0x8210,
  11594. FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE : 0x8211,
  11595. FRAMEBUFFER_ATTACHMENT_RED_SIZE : 0x8212,
  11596. FRAMEBUFFER_ATTACHMENT_GREEN_SIZE : 0x8213,
  11597. FRAMEBUFFER_ATTACHMENT_BLUE_SIZE : 0x8214,
  11598. FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE : 0x8215,
  11599. FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE : 0x8216,
  11600. FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE : 0x8217,
  11601. FRAMEBUFFER_DEFAULT : 0x8218,
  11602. UNSIGNED_INT_24_8 : 0x84FA,
  11603. DEPTH24_STENCIL8 : 0x88F0,
  11604. UNSIGNED_NORMALIZED : 0x8C17,
  11605. DRAW_FRAMEBUFFER_BINDING : 0x8CA6, // Same as FRAMEBUFFER_BINDING
  11606. READ_FRAMEBUFFER : 0x8CA8,
  11607. DRAW_FRAMEBUFFER : 0x8CA9,
  11608. READ_FRAMEBUFFER_BINDING : 0x8CAA,
  11609. RENDERBUFFER_SAMPLES : 0x8CAB,
  11610. FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER : 0x8CD4,
  11611. MAX_COLOR_ATTACHMENTS : 0x8CDF,
  11612. COLOR_ATTACHMENT1 : 0x8CE1,
  11613. COLOR_ATTACHMENT2 : 0x8CE2,
  11614. COLOR_ATTACHMENT3 : 0x8CE3,
  11615. COLOR_ATTACHMENT4 : 0x8CE4,
  11616. COLOR_ATTACHMENT5 : 0x8CE5,
  11617. COLOR_ATTACHMENT6 : 0x8CE6,
  11618. COLOR_ATTACHMENT7 : 0x8CE7,
  11619. COLOR_ATTACHMENT8 : 0x8CE8,
  11620. COLOR_ATTACHMENT9 : 0x8CE9,
  11621. COLOR_ATTACHMENT10 : 0x8CEA,
  11622. COLOR_ATTACHMENT11 : 0x8CEB,
  11623. COLOR_ATTACHMENT12 : 0x8CEC,
  11624. COLOR_ATTACHMENT13 : 0x8CED,
  11625. COLOR_ATTACHMENT14 : 0x8CEE,
  11626. COLOR_ATTACHMENT15 : 0x8CEF,
  11627. FRAMEBUFFER_INCOMPLETE_MULTISAMPLE : 0x8D56,
  11628. MAX_SAMPLES : 0x8D57,
  11629. HALF_FLOAT : 0x140B,
  11630. RG : 0x8227,
  11631. RG_INTEGER : 0x8228,
  11632. R8 : 0x8229,
  11633. RG8 : 0x822B,
  11634. R16F : 0x822D,
  11635. R32F : 0x822E,
  11636. RG16F : 0x822F,
  11637. RG32F : 0x8230,
  11638. R8I : 0x8231,
  11639. R8UI : 0x8232,
  11640. R16I : 0x8233,
  11641. R16UI : 0x8234,
  11642. R32I : 0x8235,
  11643. R32UI : 0x8236,
  11644. RG8I : 0x8237,
  11645. RG8UI : 0x8238,
  11646. RG16I : 0x8239,
  11647. RG16UI : 0x823A,
  11648. RG32I : 0x823B,
  11649. RG32UI : 0x823C,
  11650. VERTEX_ARRAY_BINDING : 0x85B5,
  11651. R8_SNORM : 0x8F94,
  11652. RG8_SNORM : 0x8F95,
  11653. RGB8_SNORM : 0x8F96,
  11654. RGBA8_SNORM : 0x8F97,
  11655. SIGNED_NORMALIZED : 0x8F9C,
  11656. COPY_READ_BUFFER : 0x8F36,
  11657. COPY_WRITE_BUFFER : 0x8F37,
  11658. COPY_READ_BUFFER_BINDING : 0x8F36, // Same as COPY_READ_BUFFER
  11659. COPY_WRITE_BUFFER_BINDING : 0x8F37, // Same as COPY_WRITE_BUFFER
  11660. UNIFORM_BUFFER : 0x8A11,
  11661. UNIFORM_BUFFER_BINDING : 0x8A28,
  11662. UNIFORM_BUFFER_START : 0x8A29,
  11663. UNIFORM_BUFFER_SIZE : 0x8A2A,
  11664. MAX_VERTEX_UNIFORM_BLOCKS : 0x8A2B,
  11665. MAX_FRAGMENT_UNIFORM_BLOCKS : 0x8A2D,
  11666. MAX_COMBINED_UNIFORM_BLOCKS : 0x8A2E,
  11667. MAX_UNIFORM_BUFFER_BINDINGS : 0x8A2F,
  11668. MAX_UNIFORM_BLOCK_SIZE : 0x8A30,
  11669. MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS : 0x8A31,
  11670. MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS : 0x8A33,
  11671. UNIFORM_BUFFER_OFFSET_ALIGNMENT : 0x8A34,
  11672. ACTIVE_UNIFORM_BLOCKS : 0x8A36,
  11673. UNIFORM_TYPE : 0x8A37,
  11674. UNIFORM_SIZE : 0x8A38,
  11675. UNIFORM_BLOCK_INDEX : 0x8A3A,
  11676. UNIFORM_OFFSET : 0x8A3B,
  11677. UNIFORM_ARRAY_STRIDE : 0x8A3C,
  11678. UNIFORM_MATRIX_STRIDE : 0x8A3D,
  11679. UNIFORM_IS_ROW_MAJOR : 0x8A3E,
  11680. UNIFORM_BLOCK_BINDING : 0x8A3F,
  11681. UNIFORM_BLOCK_DATA_SIZE : 0x8A40,
  11682. UNIFORM_BLOCK_ACTIVE_UNIFORMS : 0x8A42,
  11683. UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES : 0x8A43,
  11684. UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER : 0x8A44,
  11685. UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER : 0x8A46,
  11686. INVALID_INDEX : 0xFFFFFFFF,
  11687. MAX_VERTEX_OUTPUT_COMPONENTS : 0x9122,
  11688. MAX_FRAGMENT_INPUT_COMPONENTS : 0x9125,
  11689. MAX_SERVER_WAIT_TIMEOUT : 0x9111,
  11690. OBJECT_TYPE : 0x9112,
  11691. SYNC_CONDITION : 0x9113,
  11692. SYNC_STATUS : 0x9114,
  11693. SYNC_FLAGS : 0x9115,
  11694. SYNC_FENCE : 0x9116,
  11695. SYNC_GPU_COMMANDS_COMPLETE : 0x9117,
  11696. UNSIGNALED : 0x9118,
  11697. SIGNALED : 0x9119,
  11698. ALREADY_SIGNALED : 0x911A,
  11699. TIMEOUT_EXPIRED : 0x911B,
  11700. CONDITION_SATISFIED : 0x911C,
  11701. WAIT_FAILED : 0x911D,
  11702. SYNC_FLUSH_COMMANDS_BIT : 0x00000001,
  11703. VERTEX_ATTRIB_ARRAY_DIVISOR : 0x88FE,
  11704. ANY_SAMPLES_PASSED : 0x8C2F,
  11705. ANY_SAMPLES_PASSED_CONSERVATIVE : 0x8D6A,
  11706. SAMPLER_BINDING : 0x8919,
  11707. RGB10_A2UI : 0x906F,
  11708. INT_2_10_10_10_REV : 0x8D9F,
  11709. TRANSFORM_FEEDBACK : 0x8E22,
  11710. TRANSFORM_FEEDBACK_PAUSED : 0x8E23,
  11711. TRANSFORM_FEEDBACK_ACTIVE : 0x8E24,
  11712. TRANSFORM_FEEDBACK_BINDING : 0x8E25,
  11713. COMPRESSED_R11_EAC : 0x9270,
  11714. COMPRESSED_SIGNED_R11_EAC : 0x9271,
  11715. COMPRESSED_RG11_EAC : 0x9272,
  11716. COMPRESSED_SIGNED_RG11_EAC : 0x9273,
  11717. COMPRESSED_RGB8_ETC2 : 0x9274,
  11718. COMPRESSED_SRGB8_ETC2 : 0x9275,
  11719. COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 : 0x9276,
  11720. COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : 0x9277,
  11721. COMPRESSED_RGBA8_ETC2_EAC : 0x9278,
  11722. COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : 0x9279,
  11723. TEXTURE_IMMUTABLE_FORMAT : 0x912F,
  11724. MAX_ELEMENT_INDEX : 0x8D6B,
  11725. TEXTURE_IMMUTABLE_LEVELS : 0x82DF
  11726. };
  11727. return freezeObject(WebGLConstants);
  11728. });
  11729. /*global define*/
  11730. define('Core/IndexDatatype',[
  11731. './defined',
  11732. './DeveloperError',
  11733. './freezeObject',
  11734. './Math',
  11735. './WebGLConstants'
  11736. ], function(
  11737. defined,
  11738. DeveloperError,
  11739. freezeObject,
  11740. CesiumMath,
  11741. WebGLConstants) {
  11742. 'use strict';
  11743. /**
  11744. * Constants for WebGL index datatypes. These corresponds to the
  11745. * <code>type</code> parameter of {@link http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawElements.xml|drawElements}.
  11746. *
  11747. * @exports IndexDatatype
  11748. */
  11749. var IndexDatatype = {
  11750. /**
  11751. * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
  11752. * of an element in <code>Uint8Array</code>.
  11753. *
  11754. * @type {Number}
  11755. * @constant
  11756. */
  11757. UNSIGNED_BYTE : WebGLConstants.UNSIGNED_BYTE,
  11758. /**
  11759. * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
  11760. * of an element in <code>Uint16Array</code>.
  11761. *
  11762. * @type {Number}
  11763. * @constant
  11764. */
  11765. UNSIGNED_SHORT : WebGLConstants.UNSIGNED_SHORT,
  11766. /**
  11767. * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
  11768. * of an element in <code>Uint32Array</code>.
  11769. *
  11770. * @type {Number}
  11771. * @constant
  11772. */
  11773. UNSIGNED_INT : WebGLConstants.UNSIGNED_INT
  11774. };
  11775. /**
  11776. * Returns the size, in bytes, of the corresponding datatype.
  11777. *
  11778. * @param {IndexDatatype} indexDatatype The index datatype to get the size of.
  11779. * @returns {Number} The size in bytes.
  11780. *
  11781. * @example
  11782. * // Returns 2
  11783. * var size = Cesium.IndexDatatype.getSizeInBytes(Cesium.IndexDatatype.UNSIGNED_SHORT);
  11784. */
  11785. IndexDatatype.getSizeInBytes = function(indexDatatype) {
  11786. switch(indexDatatype) {
  11787. case IndexDatatype.UNSIGNED_BYTE:
  11788. return Uint8Array.BYTES_PER_ELEMENT;
  11789. case IndexDatatype.UNSIGNED_SHORT:
  11790. return Uint16Array.BYTES_PER_ELEMENT;
  11791. case IndexDatatype.UNSIGNED_INT:
  11792. return Uint32Array.BYTES_PER_ELEMENT;
  11793. }
  11794. throw new DeveloperError('indexDatatype is required and must be a valid IndexDatatype constant.');
  11795. };
  11796. /**
  11797. * Validates that the provided index datatype is a valid {@link IndexDatatype}.
  11798. *
  11799. * @param {IndexDatatype} indexDatatype The index datatype to validate.
  11800. * @returns {Boolean} <code>true</code> if the provided index datatype is a valid value; otherwise, <code>false</code>.
  11801. *
  11802. * @example
  11803. * if (!Cesium.IndexDatatype.validate(indexDatatype)) {
  11804. * throw new Cesium.DeveloperError('indexDatatype must be a valid value.');
  11805. * }
  11806. */
  11807. IndexDatatype.validate = function(indexDatatype) {
  11808. return defined(indexDatatype) &&
  11809. (indexDatatype === IndexDatatype.UNSIGNED_BYTE ||
  11810. indexDatatype === IndexDatatype.UNSIGNED_SHORT ||
  11811. indexDatatype === IndexDatatype.UNSIGNED_INT);
  11812. };
  11813. /**
  11814. * Creates a typed array that will store indices, using either <code><Uint16Array</code>
  11815. * or <code>Uint32Array</code> depending on the number of vertices.
  11816. *
  11817. * @param {Number} numberOfVertices Number of vertices that the indices will reference.
  11818. * @param {Any} indicesLengthOrArray Passed through to the typed array constructor.
  11819. * @returns {Uint16Array|Uint32Array} A <code>Uint16Array</code> or <code>Uint32Array</code> constructed with <code>indicesLengthOrArray</code>.
  11820. *
  11821. * @example
  11822. * this.indices = Cesium.IndexDatatype.createTypedArray(positions.length / 3, numberOfIndices);
  11823. */
  11824. IndexDatatype.createTypedArray = function(numberOfVertices, indicesLengthOrArray) {
  11825. if (!defined(numberOfVertices)) {
  11826. throw new DeveloperError('numberOfVertices is required.');
  11827. }
  11828. if (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  11829. return new Uint32Array(indicesLengthOrArray);
  11830. }
  11831. return new Uint16Array(indicesLengthOrArray);
  11832. };
  11833. /**
  11834. * Creates a typed array from a source array buffer. The resulting typed array will store indices, using either <code><Uint16Array</code>
  11835. * or <code>Uint32Array</code> depending on the number of vertices.
  11836. *
  11837. * @param {Number} numberOfVertices Number of vertices that the indices will reference.
  11838. * @param {ArrayBuffer} sourceArray Passed through to the typed array constructor.
  11839. * @param {Number} byteOffset Passed through to the typed array constructor.
  11840. * @param {Number} length Passed through to the typed array constructor.
  11841. * @returns {Uint16Array|Uint32Array} A <code>Uint16Array</code> or <code>Uint32Array</code> constructed with <code>sourceArray</code>, <code>byteOffset</code>, and <code>length</code>.
  11842. *
  11843. */
  11844. IndexDatatype.createTypedArrayFromArrayBuffer = function(numberOfVertices, sourceArray, byteOffset, length) {
  11845. if (!defined(numberOfVertices)) {
  11846. throw new DeveloperError('numberOfVertices is required.');
  11847. }
  11848. if (!defined(sourceArray)) {
  11849. throw new DeveloperError('sourceArray is required.');
  11850. }
  11851. if (!defined(byteOffset)) {
  11852. throw new DeveloperError('byteOffset is required.');
  11853. }
  11854. if (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  11855. return new Uint32Array(sourceArray, byteOffset, length);
  11856. }
  11857. return new Uint16Array(sourceArray, byteOffset, length);
  11858. };
  11859. return freezeObject(IndexDatatype);
  11860. });
  11861. /*global define*/
  11862. define('Core/Intersections2D',[
  11863. './Cartesian3',
  11864. './defined',
  11865. './DeveloperError'
  11866. ], function(
  11867. Cartesian3,
  11868. defined,
  11869. DeveloperError) {
  11870. 'use strict';
  11871. /**
  11872. * Contains functions for operating on 2D triangles.
  11873. *
  11874. * @exports Intersections2D
  11875. */
  11876. var Intersections2D = {};
  11877. /**
  11878. * Splits a 2D triangle at given axis-aligned threshold value and returns the resulting
  11879. * polygon on a given side of the threshold. The resulting polygon may have 0, 1, 2,
  11880. * 3, or 4 vertices.
  11881. *
  11882. * @param {Number} threshold The threshold coordinate value at which to clip the triangle.
  11883. * @param {Boolean} keepAbove true to keep the portion of the triangle above the threshold, or false
  11884. * to keep the portion below.
  11885. * @param {Number} u0 The coordinate of the first vertex in the triangle, in counter-clockwise order.
  11886. * @param {Number} u1 The coordinate of the second vertex in the triangle, in counter-clockwise order.
  11887. * @param {Number} u2 The coordinate of the third vertex in the triangle, in counter-clockwise order.
  11888. * @param {Number[]} [result] The array into which to copy the result. If this parameter is not supplied,
  11889. * a new array is constructed and returned.
  11890. * @returns {Number[]} The polygon that results after the clip, specified as a list of
  11891. * vertices. The vertices are specified in counter-clockwise order.
  11892. * Each vertex is either an index from the existing list (identified as
  11893. * a 0, 1, or 2) or -1 indicating a new vertex not in the original triangle.
  11894. * For new vertices, the -1 is followed by three additional numbers: the
  11895. * index of each of the two original vertices forming the line segment that
  11896. * the new vertex lies on, and the fraction of the distance from the first
  11897. * vertex to the second one.
  11898. *
  11899. * @example
  11900. * var result = Cesium.Intersections2D.clipTriangleAtAxisAlignedThreshold(0.5, false, 0.2, 0.6, 0.4);
  11901. * // result === [2, 0, -1, 1, 0, 0.25, -1, 1, 2, 0.5]
  11902. */
  11903. Intersections2D.clipTriangleAtAxisAlignedThreshold = function(threshold, keepAbove, u0, u1, u2, result) {
  11904. if (!defined(threshold)) {
  11905. throw new DeveloperError('threshold is required.');
  11906. }
  11907. if (!defined(keepAbove)) {
  11908. throw new DeveloperError('keepAbove is required.');
  11909. }
  11910. if (!defined(u0)) {
  11911. throw new DeveloperError('u0 is required.');
  11912. }
  11913. if (!defined(u1)) {
  11914. throw new DeveloperError('u1 is required.');
  11915. }
  11916. if (!defined(u2)) {
  11917. throw new DeveloperError('u2 is required.');
  11918. }
  11919. if (!defined(result)) {
  11920. result = [];
  11921. } else {
  11922. result.length = 0;
  11923. }
  11924. var u0Behind;
  11925. var u1Behind;
  11926. var u2Behind;
  11927. if (keepAbove) {
  11928. u0Behind = u0 < threshold;
  11929. u1Behind = u1 < threshold;
  11930. u2Behind = u2 < threshold;
  11931. } else {
  11932. u0Behind = u0 > threshold;
  11933. u1Behind = u1 > threshold;
  11934. u2Behind = u2 > threshold;
  11935. }
  11936. var numBehind = u0Behind + u1Behind + u2Behind;
  11937. var u01Ratio;
  11938. var u02Ratio;
  11939. var u12Ratio;
  11940. var u10Ratio;
  11941. var u20Ratio;
  11942. var u21Ratio;
  11943. if (numBehind === 1) {
  11944. if (u0Behind) {
  11945. u01Ratio = (threshold - u0) / (u1 - u0);
  11946. u02Ratio = (threshold - u0) / (u2 - u0);
  11947. result.push(1);
  11948. result.push(2);
  11949. if (u02Ratio !== 1.0) {
  11950. result.push(-1);
  11951. result.push(0);
  11952. result.push(2);
  11953. result.push(u02Ratio);
  11954. }
  11955. if (u01Ratio !== 1.0) {
  11956. result.push(-1);
  11957. result.push(0);
  11958. result.push(1);
  11959. result.push(u01Ratio);
  11960. }
  11961. } else if (u1Behind) {
  11962. u12Ratio = (threshold - u1) / (u2 - u1);
  11963. u10Ratio = (threshold - u1) / (u0 - u1);
  11964. result.push(2);
  11965. result.push(0);
  11966. if (u10Ratio !== 1.0) {
  11967. result.push(-1);
  11968. result.push(1);
  11969. result.push(0);
  11970. result.push(u10Ratio);
  11971. }
  11972. if (u12Ratio !== 1.0) {
  11973. result.push(-1);
  11974. result.push(1);
  11975. result.push(2);
  11976. result.push(u12Ratio);
  11977. }
  11978. } else if (u2Behind) {
  11979. u20Ratio = (threshold - u2) / (u0 - u2);
  11980. u21Ratio = (threshold - u2) / (u1 - u2);
  11981. result.push(0);
  11982. result.push(1);
  11983. if (u21Ratio !== 1.0) {
  11984. result.push(-1);
  11985. result.push(2);
  11986. result.push(1);
  11987. result.push(u21Ratio);
  11988. }
  11989. if (u20Ratio !== 1.0) {
  11990. result.push(-1);
  11991. result.push(2);
  11992. result.push(0);
  11993. result.push(u20Ratio);
  11994. }
  11995. }
  11996. } else if (numBehind === 2) {
  11997. if (!u0Behind && u0 !== threshold) {
  11998. u10Ratio = (threshold - u1) / (u0 - u1);
  11999. u20Ratio = (threshold - u2) / (u0 - u2);
  12000. result.push(0);
  12001. result.push(-1);
  12002. result.push(1);
  12003. result.push(0);
  12004. result.push(u10Ratio);
  12005. result.push(-1);
  12006. result.push(2);
  12007. result.push(0);
  12008. result.push(u20Ratio);
  12009. } else if (!u1Behind && u1 !== threshold) {
  12010. u21Ratio = (threshold - u2) / (u1 - u2);
  12011. u01Ratio = (threshold - u0) / (u1 - u0);
  12012. result.push(1);
  12013. result.push(-1);
  12014. result.push(2);
  12015. result.push(1);
  12016. result.push(u21Ratio);
  12017. result.push(-1);
  12018. result.push(0);
  12019. result.push(1);
  12020. result.push(u01Ratio);
  12021. } else if (!u2Behind && u2 !== threshold) {
  12022. u02Ratio = (threshold - u0) / (u2 - u0);
  12023. u12Ratio = (threshold - u1) / (u2 - u1);
  12024. result.push(2);
  12025. result.push(-1);
  12026. result.push(0);
  12027. result.push(2);
  12028. result.push(u02Ratio);
  12029. result.push(-1);
  12030. result.push(1);
  12031. result.push(2);
  12032. result.push(u12Ratio);
  12033. }
  12034. } else if (numBehind !== 3) {
  12035. // Completely in front of threshold
  12036. result.push(0);
  12037. result.push(1);
  12038. result.push(2);
  12039. }
  12040. // else Completely behind threshold
  12041. return result;
  12042. };
  12043. /**
  12044. * Compute the barycentric coordinates of a 2D position within a 2D triangle.
  12045. *
  12046. * @param {Number} x The x coordinate of the position for which to find the barycentric coordinates.
  12047. * @param {Number} y The y coordinate of the position for which to find the barycentric coordinates.
  12048. * @param {Number} x1 The x coordinate of the triangle's first vertex.
  12049. * @param {Number} y1 The y coordinate of the triangle's first vertex.
  12050. * @param {Number} x2 The x coordinate of the triangle's second vertex.
  12051. * @param {Number} y2 The y coordinate of the triangle's second vertex.
  12052. * @param {Number} x3 The x coordinate of the triangle's third vertex.
  12053. * @param {Number} y3 The y coordinate of the triangle's third vertex.
  12054. * @param {Cartesian3} [result] The instance into to which to copy the result. If this parameter
  12055. * is undefined, a new instance is created and returned.
  12056. * @returns {Cartesian3} The barycentric coordinates of the position within the triangle.
  12057. *
  12058. * @example
  12059. * var result = Cesium.Intersections2D.computeBarycentricCoordinates(0.0, 0.0, 0.0, 1.0, -1, -0.5, 1, -0.5);
  12060. * // result === new Cesium.Cartesian3(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0);
  12061. */
  12062. Intersections2D.computeBarycentricCoordinates = function(x, y, x1, y1, x2, y2, x3, y3, result) {
  12063. if (!defined(x)) {
  12064. throw new DeveloperError('x is required.');
  12065. }
  12066. if (!defined(y)) {
  12067. throw new DeveloperError('y is required.');
  12068. }
  12069. if (!defined(x1)) {
  12070. throw new DeveloperError('x1 is required.');
  12071. }
  12072. if (!defined(y1)) {
  12073. throw new DeveloperError('y1 is required.');
  12074. }
  12075. if (!defined(x2)) {
  12076. throw new DeveloperError('x2 is required.');
  12077. }
  12078. if (!defined(y2)) {
  12079. throw new DeveloperError('y2 is required.');
  12080. }
  12081. if (!defined(x3)) {
  12082. throw new DeveloperError('x3 is required.');
  12083. }
  12084. if (!defined(y3)) {
  12085. throw new DeveloperError('y3 is required.');
  12086. }
  12087. var x1mx3 = x1 - x3;
  12088. var x3mx2 = x3 - x2;
  12089. var y2my3 = y2 - y3;
  12090. var y1my3 = y1 - y3;
  12091. var inverseDeterminant = 1.0 / (y2my3 * x1mx3 + x3mx2 * y1my3);
  12092. var ymy3 = y - y3;
  12093. var xmx3 = x - x3;
  12094. var l1 = (y2my3 * xmx3 + x3mx2 * ymy3) * inverseDeterminant;
  12095. var l2 = (-y1my3 * xmx3 + x1mx3 * ymy3) * inverseDeterminant;
  12096. var l3 = 1.0 - l1 - l2;
  12097. if (defined(result)) {
  12098. result.x = l1;
  12099. result.y = l2;
  12100. result.z = l3;
  12101. return result;
  12102. } else {
  12103. return new Cartesian3(l1, l2, l3);
  12104. }
  12105. };
  12106. return Intersections2D;
  12107. });
  12108. /*global define*/
  12109. define('Core/AxisAlignedBoundingBox',[
  12110. './Cartesian3',
  12111. './defaultValue',
  12112. './defined',
  12113. './DeveloperError',
  12114. './Intersect'
  12115. ], function(
  12116. Cartesian3,
  12117. defaultValue,
  12118. defined,
  12119. DeveloperError,
  12120. Intersect) {
  12121. 'use strict';
  12122. /**
  12123. * Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
  12124. * @alias AxisAlignedBoundingBox
  12125. * @constructor
  12126. *
  12127. * @param {Cartesian3} [minimum=Cartesian3.ZERO] The minimum point along the x, y, and z axes.
  12128. * @param {Cartesian3} [maximum=Cartesian3.ZERO] The maximum point along the x, y, and z axes.
  12129. * @param {Cartesian3} [center] The center of the box; automatically computed if not supplied.
  12130. *
  12131. * @see BoundingSphere
  12132. * @see BoundingRectangle
  12133. */
  12134. function AxisAlignedBoundingBox(minimum, maximum, center) {
  12135. /**
  12136. * The minimum point defining the bounding box.
  12137. * @type {Cartesian3}
  12138. * @default {@link Cartesian3.ZERO}
  12139. */
  12140. this.minimum = Cartesian3.clone(defaultValue(minimum, Cartesian3.ZERO));
  12141. /**
  12142. * The maximum point defining the bounding box.
  12143. * @type {Cartesian3}
  12144. * @default {@link Cartesian3.ZERO}
  12145. */
  12146. this.maximum = Cartesian3.clone(defaultValue(maximum, Cartesian3.ZERO));
  12147. //If center was not defined, compute it.
  12148. if (!defined(center)) {
  12149. center = Cartesian3.add(this.minimum, this.maximum, new Cartesian3());
  12150. Cartesian3.multiplyByScalar(center, 0.5, center);
  12151. } else {
  12152. center = Cartesian3.clone(center);
  12153. }
  12154. /**
  12155. * The center point of the bounding box.
  12156. * @type {Cartesian3}
  12157. */
  12158. this.center = center;
  12159. }
  12160. /**
  12161. * Computes an instance of an AxisAlignedBoundingBox. The box is determined by
  12162. * finding the points spaced the farthest apart on the x, y, and z axes.
  12163. *
  12164. * @param {Cartesian3[]} positions List of points that the bounding box will enclose. Each point must have a <code>x</code>, <code>y</code>, and <code>z</code> properties.
  12165. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  12166. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  12167. *
  12168. * @example
  12169. * // Compute an axis aligned bounding box enclosing two points.
  12170. * var box = Cesium.AxisAlignedBoundingBox.fromPoints([new Cesium.Cartesian3(2, 0, 0), new Cesium.Cartesian3(-2, 0, 0)]);
  12171. */
  12172. AxisAlignedBoundingBox.fromPoints = function(positions, result) {
  12173. if (!defined(result)) {
  12174. result = new AxisAlignedBoundingBox();
  12175. }
  12176. if (!defined(positions) || positions.length === 0) {
  12177. result.minimum = Cartesian3.clone(Cartesian3.ZERO, result.minimum);
  12178. result.maximum = Cartesian3.clone(Cartesian3.ZERO, result.maximum);
  12179. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  12180. return result;
  12181. }
  12182. var minimumX = positions[0].x;
  12183. var minimumY = positions[0].y;
  12184. var minimumZ = positions[0].z;
  12185. var maximumX = positions[0].x;
  12186. var maximumY = positions[0].y;
  12187. var maximumZ = positions[0].z;
  12188. var length = positions.length;
  12189. for ( var i = 1; i < length; i++) {
  12190. var p = positions[i];
  12191. var x = p.x;
  12192. var y = p.y;
  12193. var z = p.z;
  12194. minimumX = Math.min(x, minimumX);
  12195. maximumX = Math.max(x, maximumX);
  12196. minimumY = Math.min(y, minimumY);
  12197. maximumY = Math.max(y, maximumY);
  12198. minimumZ = Math.min(z, minimumZ);
  12199. maximumZ = Math.max(z, maximumZ);
  12200. }
  12201. var minimum = result.minimum;
  12202. minimum.x = minimumX;
  12203. minimum.y = minimumY;
  12204. minimum.z = minimumZ;
  12205. var maximum = result.maximum;
  12206. maximum.x = maximumX;
  12207. maximum.y = maximumY;
  12208. maximum.z = maximumZ;
  12209. var center = Cartesian3.add(minimum, maximum, result.center);
  12210. Cartesian3.multiplyByScalar(center, 0.5, center);
  12211. return result;
  12212. };
  12213. /**
  12214. * Duplicates a AxisAlignedBoundingBox instance.
  12215. *
  12216. * @param {AxisAlignedBoundingBox} box The bounding box to duplicate.
  12217. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  12218. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
  12219. */
  12220. AxisAlignedBoundingBox.clone = function(box, result) {
  12221. if (!defined(box)) {
  12222. return undefined;
  12223. }
  12224. if (!defined(result)) {
  12225. return new AxisAlignedBoundingBox(box.minimum, box.maximum);
  12226. }
  12227. result.minimum = Cartesian3.clone(box.minimum, result.minimum);
  12228. result.maximum = Cartesian3.clone(box.maximum, result.maximum);
  12229. result.center = Cartesian3.clone(box.center, result.center);
  12230. return result;
  12231. };
  12232. /**
  12233. * Compares the provided AxisAlignedBoundingBox componentwise and returns
  12234. * <code>true</code> if they are equal, <code>false</code> otherwise.
  12235. *
  12236. * @param {AxisAlignedBoundingBox} [left] The first AxisAlignedBoundingBox.
  12237. * @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox.
  12238. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  12239. */
  12240. AxisAlignedBoundingBox.equals = function(left, right) {
  12241. return (left === right) ||
  12242. ((defined(left)) &&
  12243. (defined(right)) &&
  12244. Cartesian3.equals(left.center, right.center) &&
  12245. Cartesian3.equals(left.minimum, right.minimum) &&
  12246. Cartesian3.equals(left.maximum, right.maximum));
  12247. };
  12248. var intersectScratch = new Cartesian3();
  12249. /**
  12250. * Determines which side of a plane a box is located.
  12251. *
  12252. * @param {AxisAlignedBoundingBox} box The bounding box to test.
  12253. * @param {Plane} plane The plane to test against.
  12254. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  12255. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  12256. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  12257. * intersects the plane.
  12258. */
  12259. AxisAlignedBoundingBox.intersectPlane = function(box, plane) {
  12260. if (!defined(box)) {
  12261. throw new DeveloperError('box is required.');
  12262. }
  12263. if (!defined(plane)) {
  12264. throw new DeveloperError('plane is required.');
  12265. }
  12266. intersectScratch = Cartesian3.subtract(box.maximum, box.minimum, intersectScratch);
  12267. var h = Cartesian3.multiplyByScalar(intersectScratch, 0.5, intersectScratch); //The positive half diagonal
  12268. var normal = plane.normal;
  12269. var e = h.x * Math.abs(normal.x) + h.y * Math.abs(normal.y) + h.z * Math.abs(normal.z);
  12270. var s = Cartesian3.dot(box.center, normal) + plane.distance; //signed distance from center
  12271. if (s - e > 0) {
  12272. return Intersect.INSIDE;
  12273. }
  12274. if (s + e < 0) {
  12275. //Not in front because normals point inward
  12276. return Intersect.OUTSIDE;
  12277. }
  12278. return Intersect.INTERSECTING;
  12279. };
  12280. /**
  12281. * Duplicates this AxisAlignedBoundingBox instance.
  12282. *
  12283. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  12284. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  12285. */
  12286. AxisAlignedBoundingBox.prototype.clone = function(result) {
  12287. return AxisAlignedBoundingBox.clone(this, result);
  12288. };
  12289. /**
  12290. * Determines which side of a plane this box is located.
  12291. *
  12292. * @param {Plane} plane The plane to test against.
  12293. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  12294. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  12295. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  12296. * intersects the plane.
  12297. */
  12298. AxisAlignedBoundingBox.prototype.intersectPlane = function(plane) {
  12299. return AxisAlignedBoundingBox.intersectPlane(this, plane);
  12300. };
  12301. /**
  12302. * Compares this AxisAlignedBoundingBox against the provided AxisAlignedBoundingBox componentwise and returns
  12303. * <code>true</code> if they are equal, <code>false</code> otherwise.
  12304. *
  12305. * @param {AxisAlignedBoundingBox} [right] The right hand side AxisAlignedBoundingBox.
  12306. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  12307. */
  12308. AxisAlignedBoundingBox.prototype.equals = function(right) {
  12309. return AxisAlignedBoundingBox.equals(this, right);
  12310. };
  12311. return AxisAlignedBoundingBox;
  12312. });
  12313. /*global define*/
  12314. define('Core/QuadraticRealPolynomial',[
  12315. './DeveloperError',
  12316. './Math'
  12317. ], function(
  12318. DeveloperError,
  12319. CesiumMath) {
  12320. 'use strict';
  12321. /**
  12322. * Defines functions for 2nd order polynomial functions of one variable with only real coefficients.
  12323. *
  12324. * @exports QuadraticRealPolynomial
  12325. */
  12326. var QuadraticRealPolynomial = {};
  12327. /**
  12328. * Provides the discriminant of the quadratic equation from the supplied coefficients.
  12329. *
  12330. * @param {Number} a The coefficient of the 2nd order monomial.
  12331. * @param {Number} b The coefficient of the 1st order monomial.
  12332. * @param {Number} c The coefficient of the 0th order monomial.
  12333. * @returns {Number} The value of the discriminant.
  12334. */
  12335. QuadraticRealPolynomial.computeDiscriminant = function(a, b, c) {
  12336. if (typeof a !== 'number') {
  12337. throw new DeveloperError('a is a required number.');
  12338. }
  12339. if (typeof b !== 'number') {
  12340. throw new DeveloperError('b is a required number.');
  12341. }
  12342. if (typeof c !== 'number') {
  12343. throw new DeveloperError('c is a required number.');
  12344. }
  12345. var discriminant = b * b - 4.0 * a * c;
  12346. return discriminant;
  12347. };
  12348. function addWithCancellationCheck(left, right, tolerance) {
  12349. var difference = left + right;
  12350. if ((CesiumMath.sign(left) !== CesiumMath.sign(right)) &&
  12351. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance) {
  12352. return 0.0;
  12353. }
  12354. return difference;
  12355. }
  12356. /**
  12357. * Provides the real valued roots of the quadratic polynomial with the provided coefficients.
  12358. *
  12359. * @param {Number} a The coefficient of the 2nd order monomial.
  12360. * @param {Number} b The coefficient of the 1st order monomial.
  12361. * @param {Number} c The coefficient of the 0th order monomial.
  12362. * @returns {Number[]} The real valued roots.
  12363. */
  12364. QuadraticRealPolynomial.computeRealRoots = function(a, b, c) {
  12365. if (typeof a !== 'number') {
  12366. throw new DeveloperError('a is a required number.');
  12367. }
  12368. if (typeof b !== 'number') {
  12369. throw new DeveloperError('b is a required number.');
  12370. }
  12371. if (typeof c !== 'number') {
  12372. throw new DeveloperError('c is a required number.');
  12373. }
  12374. var ratio;
  12375. if (a === 0.0) {
  12376. if (b === 0.0) {
  12377. // Constant function: c = 0.
  12378. return [];
  12379. }
  12380. // Linear function: b * x + c = 0.
  12381. return [-c / b];
  12382. } else if (b === 0.0) {
  12383. if (c === 0.0) {
  12384. // 2nd order monomial: a * x^2 = 0.
  12385. return [0.0, 0.0];
  12386. }
  12387. var cMagnitude = Math.abs(c);
  12388. var aMagnitude = Math.abs(a);
  12389. if ((cMagnitude < aMagnitude) && (cMagnitude / aMagnitude < CesiumMath.EPSILON14)) { // c ~= 0.0.
  12390. // 2nd order monomial: a * x^2 = 0.
  12391. return [0.0, 0.0];
  12392. } else if ((cMagnitude > aMagnitude) && (aMagnitude / cMagnitude < CesiumMath.EPSILON14)) { // a ~= 0.0.
  12393. // Constant function: c = 0.
  12394. return [];
  12395. }
  12396. // a * x^2 + c = 0
  12397. ratio = -c / a;
  12398. if (ratio < 0.0) {
  12399. // Both roots are complex.
  12400. return [];
  12401. }
  12402. // Both roots are real.
  12403. var root = Math.sqrt(ratio);
  12404. return [-root, root];
  12405. } else if (c === 0.0) {
  12406. // a * x^2 + b * x = 0
  12407. ratio = -b / a;
  12408. if (ratio < 0.0) {
  12409. return [ratio, 0.0];
  12410. }
  12411. return [0.0, ratio];
  12412. }
  12413. // a * x^2 + b * x + c = 0
  12414. var b2 = b * b;
  12415. var four_ac = 4.0 * a * c;
  12416. var radicand = addWithCancellationCheck(b2, -four_ac, CesiumMath.EPSILON14);
  12417. if (radicand < 0.0) {
  12418. // Both roots are complex.
  12419. return [];
  12420. }
  12421. var q = -0.5 * addWithCancellationCheck(b, CesiumMath.sign(b) * Math.sqrt(radicand), CesiumMath.EPSILON14);
  12422. if (b > 0.0) {
  12423. return [q / a, c / q];
  12424. }
  12425. return [c / q, q / a];
  12426. };
  12427. return QuadraticRealPolynomial;
  12428. });
  12429. /*global define*/
  12430. define('Core/CubicRealPolynomial',[
  12431. './DeveloperError',
  12432. './QuadraticRealPolynomial'
  12433. ], function(
  12434. DeveloperError,
  12435. QuadraticRealPolynomial) {
  12436. 'use strict';
  12437. /**
  12438. * Defines functions for 3rd order polynomial functions of one variable with only real coefficients.
  12439. *
  12440. * @exports CubicRealPolynomial
  12441. */
  12442. var CubicRealPolynomial = {};
  12443. /**
  12444. * Provides the discriminant of the cubic equation from the supplied coefficients.
  12445. *
  12446. * @param {Number} a The coefficient of the 3rd order monomial.
  12447. * @param {Number} b The coefficient of the 2nd order monomial.
  12448. * @param {Number} c The coefficient of the 1st order monomial.
  12449. * @param {Number} d The coefficient of the 0th order monomial.
  12450. * @returns {Number} The value of the discriminant.
  12451. */
  12452. CubicRealPolynomial.computeDiscriminant = function(a, b, c, d) {
  12453. if (typeof a !== 'number') {
  12454. throw new DeveloperError('a is a required number.');
  12455. }
  12456. if (typeof b !== 'number') {
  12457. throw new DeveloperError('b is a required number.');
  12458. }
  12459. if (typeof c !== 'number') {
  12460. throw new DeveloperError('c is a required number.');
  12461. }
  12462. if (typeof d !== 'number') {
  12463. throw new DeveloperError('d is a required number.');
  12464. }
  12465. var a2 = a * a;
  12466. var b2 = b * b;
  12467. var c2 = c * c;
  12468. var d2 = d * d;
  12469. var discriminant = 18.0 * a * b * c * d + b2 * c2 - 27.0 * a2 * d2 - 4.0 * (a * c2 * c + b2 * b * d);
  12470. return discriminant;
  12471. };
  12472. function computeRealRoots(a, b, c, d) {
  12473. var A = a;
  12474. var B = b / 3.0;
  12475. var C = c / 3.0;
  12476. var D = d;
  12477. var AC = A * C;
  12478. var BD = B * D;
  12479. var B2 = B * B;
  12480. var C2 = C * C;
  12481. var delta1 = A * C - B2;
  12482. var delta2 = A * D - B * C;
  12483. var delta3 = B * D - C2;
  12484. var discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  12485. var temp;
  12486. var temp1;
  12487. if (discriminant < 0.0) {
  12488. var ABar;
  12489. var CBar;
  12490. var DBar;
  12491. if (B2 * BD >= AC * C2) {
  12492. ABar = A;
  12493. CBar = delta1;
  12494. DBar = -2.0 * B * delta1 + A * delta2;
  12495. } else {
  12496. ABar = D;
  12497. CBar = delta3;
  12498. DBar = -D * delta2 + 2.0 * C * delta3;
  12499. }
  12500. var s = (DBar < 0.0) ? -1.0 : 1.0; // This is not Math.Sign()!
  12501. var temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  12502. temp1 = -DBar + temp0;
  12503. var x = temp1 / 2.0;
  12504. var p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  12505. var q = (temp1 === temp0) ? -p : -CBar / p;
  12506. temp = (CBar <= 0.0) ? p + q : -DBar / (p * p + q * q + CBar);
  12507. if (B2 * BD >= AC * C2) {
  12508. return [(temp - B) / A];
  12509. }
  12510. return [-D / (temp + C)];
  12511. }
  12512. var CBarA = delta1;
  12513. var DBarA = -2.0 * B * delta1 + A * delta2;
  12514. var CBarD = delta3;
  12515. var DBarD = -D * delta2 + 2.0 * C * delta3;
  12516. var squareRootOfDiscriminant = Math.sqrt(discriminant);
  12517. var halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  12518. var theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  12519. temp = 2.0 * Math.sqrt(-CBarA);
  12520. var cosine = Math.cos(theta);
  12521. temp1 = temp * cosine;
  12522. var temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  12523. var numeratorLarge = (temp1 + temp3 > 2.0 * B) ? temp1 - B : temp3 - B;
  12524. var denominatorLarge = A;
  12525. var root1 = numeratorLarge / denominatorLarge;
  12526. theta = Math.abs(Math.atan2(D * squareRootOfDiscriminant, -DBarD) / 3.0);
  12527. temp = 2.0 * Math.sqrt(-CBarD);
  12528. cosine = Math.cos(theta);
  12529. temp1 = temp * cosine;
  12530. temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  12531. var numeratorSmall = -D;
  12532. var denominatorSmall = (temp1 + temp3 < 2.0 * C) ? temp1 + C : temp3 + C;
  12533. var root3 = numeratorSmall / denominatorSmall;
  12534. var E = denominatorLarge * denominatorSmall;
  12535. var F = -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  12536. var G = numeratorLarge * numeratorSmall;
  12537. var root2 = (C * F - B * G) / (-B * F + C * E);
  12538. if (root1 <= root2) {
  12539. if (root1 <= root3) {
  12540. if (root2 <= root3) {
  12541. return [root1, root2, root3];
  12542. }
  12543. return [root1, root3, root2];
  12544. }
  12545. return [root3, root1, root2];
  12546. }
  12547. if (root1 <= root3) {
  12548. return [root2, root1, root3];
  12549. }
  12550. if (root2 <= root3) {
  12551. return [root2, root3, root1];
  12552. }
  12553. return [root3, root2, root1];
  12554. }
  12555. /**
  12556. * Provides the real valued roots of the cubic polynomial with the provided coefficients.
  12557. *
  12558. * @param {Number} a The coefficient of the 3rd order monomial.
  12559. * @param {Number} b The coefficient of the 2nd order monomial.
  12560. * @param {Number} c The coefficient of the 1st order monomial.
  12561. * @param {Number} d The coefficient of the 0th order monomial.
  12562. * @returns {Number[]} The real valued roots.
  12563. */
  12564. CubicRealPolynomial.computeRealRoots = function(a, b, c, d) {
  12565. if (typeof a !== 'number') {
  12566. throw new DeveloperError('a is a required number.');
  12567. }
  12568. if (typeof b !== 'number') {
  12569. throw new DeveloperError('b is a required number.');
  12570. }
  12571. if (typeof c !== 'number') {
  12572. throw new DeveloperError('c is a required number.');
  12573. }
  12574. if (typeof d !== 'number') {
  12575. throw new DeveloperError('d is a required number.');
  12576. }
  12577. var roots;
  12578. var ratio;
  12579. if (a === 0.0) {
  12580. // Quadratic function: b * x^2 + c * x + d = 0.
  12581. return QuadraticRealPolynomial.computeRealRoots(b, c, d);
  12582. } else if (b === 0.0) {
  12583. if (c === 0.0) {
  12584. if (d === 0.0) {
  12585. // 3rd order monomial: a * x^3 = 0.
  12586. return [0.0, 0.0, 0.0];
  12587. }
  12588. // a * x^3 + d = 0
  12589. ratio = -d / a;
  12590. var root = (ratio < 0.0) ? -Math.pow(-ratio, 1.0 / 3.0) : Math.pow(ratio, 1.0 / 3.0);
  12591. return [root, root, root];
  12592. } else if (d === 0.0) {
  12593. // x * (a * x^2 + c) = 0.
  12594. roots = QuadraticRealPolynomial.computeRealRoots(a, 0, c);
  12595. // Return the roots in ascending order.
  12596. if (roots.Length === 0) {
  12597. return [0.0];
  12598. }
  12599. return [roots[0], 0.0, roots[1]];
  12600. }
  12601. // Deflated cubic polynomial: a * x^3 + c * x + d= 0.
  12602. return computeRealRoots(a, 0, c, d);
  12603. } else if (c === 0.0) {
  12604. if (d === 0.0) {
  12605. // x^2 * (a * x + b) = 0.
  12606. ratio = -b / a;
  12607. if (ratio < 0.0) {
  12608. return [ratio, 0.0, 0.0];
  12609. }
  12610. return [0.0, 0.0, ratio];
  12611. }
  12612. // a * x^3 + b * x^2 + d = 0.
  12613. return computeRealRoots(a, b, 0, d);
  12614. } else if (d === 0.0) {
  12615. // x * (a * x^2 + b * x + c) = 0
  12616. roots = QuadraticRealPolynomial.computeRealRoots(a, b, c);
  12617. // Return the roots in ascending order.
  12618. if (roots.length === 0) {
  12619. return [0.0];
  12620. } else if (roots[1] <= 0.0) {
  12621. return [roots[0], roots[1], 0.0];
  12622. } else if (roots[0] >= 0.0) {
  12623. return [0.0, roots[0], roots[1]];
  12624. }
  12625. return [roots[0], 0.0, roots[1]];
  12626. }
  12627. return computeRealRoots(a, b, c, d);
  12628. };
  12629. return CubicRealPolynomial;
  12630. });
  12631. /*global define*/
  12632. define('Core/QuarticRealPolynomial',[
  12633. './CubicRealPolynomial',
  12634. './DeveloperError',
  12635. './Math',
  12636. './QuadraticRealPolynomial'
  12637. ], function(
  12638. CubicRealPolynomial,
  12639. DeveloperError,
  12640. CesiumMath,
  12641. QuadraticRealPolynomial) {
  12642. 'use strict';
  12643. /**
  12644. * Defines functions for 4th order polynomial functions of one variable with only real coefficients.
  12645. *
  12646. * @exports QuarticRealPolynomial
  12647. */
  12648. var QuarticRealPolynomial = {};
  12649. /**
  12650. * Provides the discriminant of the quartic equation from the supplied coefficients.
  12651. *
  12652. * @param {Number} a The coefficient of the 4th order monomial.
  12653. * @param {Number} b The coefficient of the 3rd order monomial.
  12654. * @param {Number} c The coefficient of the 2nd order monomial.
  12655. * @param {Number} d The coefficient of the 1st order monomial.
  12656. * @param {Number} e The coefficient of the 0th order monomial.
  12657. * @returns {Number} The value of the discriminant.
  12658. */
  12659. QuarticRealPolynomial.computeDiscriminant = function(a, b, c, d, e) {
  12660. if (typeof a !== 'number') {
  12661. throw new DeveloperError('a is a required number.');
  12662. }
  12663. if (typeof b !== 'number') {
  12664. throw new DeveloperError('b is a required number.');
  12665. }
  12666. if (typeof c !== 'number') {
  12667. throw new DeveloperError('c is a required number.');
  12668. }
  12669. if (typeof d !== 'number') {
  12670. throw new DeveloperError('d is a required number.');
  12671. }
  12672. if (typeof e !== 'number') {
  12673. throw new DeveloperError('e is a required number.');
  12674. }
  12675. var a2 = a * a;
  12676. var a3 = a2 * a;
  12677. var b2 = b * b;
  12678. var b3 = b2 * b;
  12679. var c2 = c * c;
  12680. var c3 = c2 * c;
  12681. var d2 = d * d;
  12682. var d3 = d2 * d;
  12683. var e2 = e * e;
  12684. var e3 = e2 * e;
  12685. var discriminant = (b2 * c2 * d2 - 4.0 * b3 * d3 - 4.0 * a * c3 * d2 + 18 * a * b * c * d3 - 27.0 * a2 * d2 * d2 + 256.0 * a3 * e3) +
  12686. e * (18.0 * b3 * c * d - 4.0 * b2 * c3 + 16.0 * a * c2 * c2 - 80.0 * a * b * c2 * d - 6.0 * a * b2 * d2 + 144.0 * a2 * c * d2) +
  12687. e2 * (144.0 * a * b2 * c - 27.0 * b2 * b2 - 128.0 * a2 * c2 - 192.0 * a2 * b * d);
  12688. return discriminant;
  12689. };
  12690. function original(a3, a2, a1, a0) {
  12691. var a3Squared = a3 * a3;
  12692. var p = a2 - 3.0 * a3Squared / 8.0;
  12693. var q = a1 - a2 * a3 / 2.0 + a3Squared * a3 / 8.0;
  12694. var r = a0 - a1 * a3 / 4.0 + a2 * a3Squared / 16.0 - 3.0 * a3Squared * a3Squared / 256.0;
  12695. // Find the roots of the cubic equations: h^6 + 2 p h^4 + (p^2 - 4 r) h^2 - q^2 = 0.
  12696. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, 2.0 * p, p * p - 4.0 * r, -q * q);
  12697. if (cubicRoots.length > 0) {
  12698. var temp = -a3 / 4.0;
  12699. // Use the largest positive root.
  12700. var hSquared = cubicRoots[cubicRoots.length - 1];
  12701. if (Math.abs(hSquared) < CesiumMath.EPSILON14) {
  12702. // y^4 + p y^2 + r = 0.
  12703. var roots = QuadraticRealPolynomial.computeRealRoots(1.0, p, r);
  12704. if (roots.length === 2) {
  12705. var root0 = roots[0];
  12706. var root1 = roots[1];
  12707. var y;
  12708. if (root0 >= 0.0 && root1 >= 0.0) {
  12709. var y0 = Math.sqrt(root0);
  12710. var y1 = Math.sqrt(root1);
  12711. return [temp - y1, temp - y0, temp + y0, temp + y1];
  12712. } else if (root0 >= 0.0 && root1 < 0.0) {
  12713. y = Math.sqrt(root0);
  12714. return [temp - y, temp + y];
  12715. } else if (root0 < 0.0 && root1 >= 0.0) {
  12716. y = Math.sqrt(root1);
  12717. return [temp - y, temp + y];
  12718. }
  12719. }
  12720. return [];
  12721. } else if (hSquared > 0.0) {
  12722. var h = Math.sqrt(hSquared);
  12723. var m = (p + hSquared - q / h) / 2.0;
  12724. var n = (p + hSquared + q / h) / 2.0;
  12725. // Now solve the two quadratic factors: (y^2 + h y + m)(y^2 - h y + n);
  12726. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, h, m);
  12727. var roots2 = QuadraticRealPolynomial.computeRealRoots(1.0, -h, n);
  12728. if (roots1.length !== 0) {
  12729. roots1[0] += temp;
  12730. roots1[1] += temp;
  12731. if (roots2.length !== 0) {
  12732. roots2[0] += temp;
  12733. roots2[1] += temp;
  12734. if (roots1[1] <= roots2[0]) {
  12735. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  12736. } else if (roots2[1] <= roots1[0]) {
  12737. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  12738. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  12739. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  12740. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  12741. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  12742. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  12743. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  12744. }
  12745. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  12746. }
  12747. return roots1;
  12748. }
  12749. if (roots2.length !== 0) {
  12750. roots2[0] += temp;
  12751. roots2[1] += temp;
  12752. return roots2;
  12753. }
  12754. return [];
  12755. }
  12756. }
  12757. return [];
  12758. }
  12759. function neumark(a3, a2, a1, a0) {
  12760. var a1Squared = a1 * a1;
  12761. var a2Squared = a2 * a2;
  12762. var a3Squared = a3 * a3;
  12763. var p = -2.0 * a2;
  12764. var q = a1 * a3 + a2Squared - 4.0 * a0;
  12765. var r = a3Squared * a0 - a1 * a2 * a3 + a1Squared;
  12766. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, p, q, r);
  12767. if (cubicRoots.length > 0) {
  12768. // Use the most positive root
  12769. var y = cubicRoots[0];
  12770. var temp = (a2 - y);
  12771. var tempSquared = temp * temp;
  12772. var g1 = a3 / 2.0;
  12773. var h1 = temp / 2.0;
  12774. var m = tempSquared - 4.0 * a0;
  12775. var mError = tempSquared + 4.0 * Math.abs(a0);
  12776. var n = a3Squared - 4.0 * y;
  12777. var nError = a3Squared + 4.0 * Math.abs(y);
  12778. var g2;
  12779. var h2;
  12780. if (y < 0.0 || (m * nError < n * mError)) {
  12781. var squareRootOfN = Math.sqrt(n);
  12782. g2 = squareRootOfN / 2.0;
  12783. h2 = squareRootOfN === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfN;
  12784. } else {
  12785. var squareRootOfM = Math.sqrt(m);
  12786. g2 = squareRootOfM === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfM;
  12787. h2 = squareRootOfM / 2.0;
  12788. }
  12789. var G;
  12790. var g;
  12791. if (g1 === 0.0 && g2 === 0.0) {
  12792. G = 0.0;
  12793. g = 0.0;
  12794. } else if (CesiumMath.sign(g1) === CesiumMath.sign(g2)) {
  12795. G = g1 + g2;
  12796. g = y / G;
  12797. } else {
  12798. g = g1 - g2;
  12799. G = y / g;
  12800. }
  12801. var H;
  12802. var h;
  12803. if (h1 === 0.0 && h2 === 0.0) {
  12804. H = 0.0;
  12805. h = 0.0;
  12806. } else if (CesiumMath.sign(h1) === CesiumMath.sign(h2)) {
  12807. H = h1 + h2;
  12808. h = a0 / H;
  12809. } else {
  12810. h = h1 - h2;
  12811. H = a0 / h;
  12812. }
  12813. // Now solve the two quadratic factors: (y^2 + G y + H)(y^2 + g y + h);
  12814. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, G, H);
  12815. var roots2 = QuadraticRealPolynomial.computeRealRoots(1.0, g, h);
  12816. if (roots1.length !== 0) {
  12817. if (roots2.length !== 0) {
  12818. if (roots1[1] <= roots2[0]) {
  12819. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  12820. } else if (roots2[1] <= roots1[0]) {
  12821. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  12822. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  12823. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  12824. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  12825. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  12826. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  12827. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  12828. } else {
  12829. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  12830. }
  12831. }
  12832. return roots1;
  12833. }
  12834. if (roots2.length !== 0) {
  12835. return roots2;
  12836. }
  12837. }
  12838. return [];
  12839. }
  12840. /**
  12841. * Provides the real valued roots of the quartic polynomial with the provided coefficients.
  12842. *
  12843. * @param {Number} a The coefficient of the 4th order monomial.
  12844. * @param {Number} b The coefficient of the 3rd order monomial.
  12845. * @param {Number} c The coefficient of the 2nd order monomial.
  12846. * @param {Number} d The coefficient of the 1st order monomial.
  12847. * @param {Number} e The coefficient of the 0th order monomial.
  12848. * @returns {Number[]} The real valued roots.
  12849. */
  12850. QuarticRealPolynomial.computeRealRoots = function(a, b, c, d, e) {
  12851. if (typeof a !== 'number') {
  12852. throw new DeveloperError('a is a required number.');
  12853. }
  12854. if (typeof b !== 'number') {
  12855. throw new DeveloperError('b is a required number.');
  12856. }
  12857. if (typeof c !== 'number') {
  12858. throw new DeveloperError('c is a required number.');
  12859. }
  12860. if (typeof d !== 'number') {
  12861. throw new DeveloperError('d is a required number.');
  12862. }
  12863. if (typeof e !== 'number') {
  12864. throw new DeveloperError('e is a required number.');
  12865. }
  12866. if (Math.abs(a) < CesiumMath.EPSILON15) {
  12867. return CubicRealPolynomial.computeRealRoots(b, c, d, e);
  12868. }
  12869. var a3 = b / a;
  12870. var a2 = c / a;
  12871. var a1 = d / a;
  12872. var a0 = e / a;
  12873. var k = (a3 < 0.0) ? 1 : 0;
  12874. k += (a2 < 0.0) ? k + 1 : k;
  12875. k += (a1 < 0.0) ? k + 1 : k;
  12876. k += (a0 < 0.0) ? k + 1 : k;
  12877. switch (k) {
  12878. case 0:
  12879. return original(a3, a2, a1, a0);
  12880. case 1:
  12881. return neumark(a3, a2, a1, a0);
  12882. case 2:
  12883. return neumark(a3, a2, a1, a0);
  12884. case 3:
  12885. return original(a3, a2, a1, a0);
  12886. case 4:
  12887. return original(a3, a2, a1, a0);
  12888. case 5:
  12889. return neumark(a3, a2, a1, a0);
  12890. case 6:
  12891. return original(a3, a2, a1, a0);
  12892. case 7:
  12893. return original(a3, a2, a1, a0);
  12894. case 8:
  12895. return neumark(a3, a2, a1, a0);
  12896. case 9:
  12897. return original(a3, a2, a1, a0);
  12898. case 10:
  12899. return original(a3, a2, a1, a0);
  12900. case 11:
  12901. return neumark(a3, a2, a1, a0);
  12902. case 12:
  12903. return original(a3, a2, a1, a0);
  12904. case 13:
  12905. return original(a3, a2, a1, a0);
  12906. case 14:
  12907. return original(a3, a2, a1, a0);
  12908. case 15:
  12909. return original(a3, a2, a1, a0);
  12910. default:
  12911. return undefined;
  12912. }
  12913. };
  12914. return QuarticRealPolynomial;
  12915. });
  12916. /*global define*/
  12917. define('Core/Ray',[
  12918. './Cartesian3',
  12919. './defaultValue',
  12920. './defined',
  12921. './DeveloperError'
  12922. ], function(
  12923. Cartesian3,
  12924. defaultValue,
  12925. defined,
  12926. DeveloperError) {
  12927. 'use strict';
  12928. /**
  12929. * Represents a ray that extends infinitely from the provided origin in the provided direction.
  12930. * @alias Ray
  12931. * @constructor
  12932. *
  12933. * @param {Cartesian3} [origin=Cartesian3.ZERO] The origin of the ray.
  12934. * @param {Cartesian3} [direction=Cartesian3.ZERO] The direction of the ray.
  12935. */
  12936. function Ray(origin, direction) {
  12937. direction = Cartesian3.clone(defaultValue(direction, Cartesian3.ZERO));
  12938. if (!Cartesian3.equals(direction, Cartesian3.ZERO)) {
  12939. Cartesian3.normalize(direction, direction);
  12940. }
  12941. /**
  12942. * The origin of the ray.
  12943. * @type {Cartesian3}
  12944. * @default {@link Cartesian3.ZERO}
  12945. */
  12946. this.origin = Cartesian3.clone(defaultValue(origin, Cartesian3.ZERO));
  12947. /**
  12948. * The direction of the ray.
  12949. * @type {Cartesian3}
  12950. */
  12951. this.direction = direction;
  12952. }
  12953. /**
  12954. * Computes the point along the ray given by r(t) = o + t*d,
  12955. * where o is the origin of the ray and d is the direction.
  12956. *
  12957. * @param {Ray} ray The ray.
  12958. * @param {Number} t A scalar value.
  12959. * @param {Cartesian3} [result] The object in which the result will be stored.
  12960. * @returns {Cartesian3} The modified result parameter, or a new instance if none was provided.
  12961. *
  12962. * @example
  12963. * //Get the first intersection point of a ray and an ellipsoid.
  12964. * var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  12965. * var point = Cesium.Ray.getPoint(ray, intersection.start);
  12966. */
  12967. Ray.getPoint = function(ray, t, result) {
  12968. if (!defined(ray)){
  12969. throw new DeveloperError('ray is requred');
  12970. }
  12971. if (typeof t !== 'number') {
  12972. throw new DeveloperError('t is a required number');
  12973. }
  12974. if (!defined(result)) {
  12975. result = new Cartesian3();
  12976. }
  12977. result = Cartesian3.multiplyByScalar(ray.direction, t, result);
  12978. return Cartesian3.add(ray.origin, result, result);
  12979. };
  12980. return Ray;
  12981. });
  12982. /*global define*/
  12983. define('Core/IntersectionTests',[
  12984. './Cartesian3',
  12985. './Cartographic',
  12986. './defaultValue',
  12987. './defined',
  12988. './DeveloperError',
  12989. './Math',
  12990. './Matrix3',
  12991. './QuadraticRealPolynomial',
  12992. './QuarticRealPolynomial',
  12993. './Ray'
  12994. ], function(
  12995. Cartesian3,
  12996. Cartographic,
  12997. defaultValue,
  12998. defined,
  12999. DeveloperError,
  13000. CesiumMath,
  13001. Matrix3,
  13002. QuadraticRealPolynomial,
  13003. QuarticRealPolynomial,
  13004. Ray) {
  13005. 'use strict';
  13006. /**
  13007. * Functions for computing the intersection between geometries such as rays, planes, triangles, and ellipsoids.
  13008. *
  13009. * @exports IntersectionTests
  13010. */
  13011. var IntersectionTests = {};
  13012. /**
  13013. * Computes the intersection of a ray and a plane.
  13014. *
  13015. * @param {Ray} ray The ray.
  13016. * @param {Plane} plane The plane.
  13017. * @param {Cartesian3} [result] The object onto which to store the result.
  13018. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13019. */
  13020. IntersectionTests.rayPlane = function(ray, plane, result) {
  13021. if (!defined(ray)) {
  13022. throw new DeveloperError('ray is required.');
  13023. }
  13024. if (!defined(plane)) {
  13025. throw new DeveloperError('plane is required.');
  13026. }
  13027. if (!defined(result)) {
  13028. result = new Cartesian3();
  13029. }
  13030. var origin = ray.origin;
  13031. var direction = ray.direction;
  13032. var normal = plane.normal;
  13033. var denominator = Cartesian3.dot(normal, direction);
  13034. if (Math.abs(denominator) < CesiumMath.EPSILON15) {
  13035. // Ray is parallel to plane. The ray may be in the polygon's plane.
  13036. return undefined;
  13037. }
  13038. var t = (-plane.distance - Cartesian3.dot(normal, origin)) / denominator;
  13039. if (t < 0) {
  13040. return undefined;
  13041. }
  13042. result = Cartesian3.multiplyByScalar(direction, t, result);
  13043. return Cartesian3.add(origin, result, result);
  13044. };
  13045. var scratchEdge0 = new Cartesian3();
  13046. var scratchEdge1 = new Cartesian3();
  13047. var scratchPVec = new Cartesian3();
  13048. var scratchTVec = new Cartesian3();
  13049. var scratchQVec = new Cartesian3();
  13050. /**
  13051. * Computes the intersection of a ray and a triangle as a parametric distance along the input ray.
  13052. *
  13053. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  13054. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  13055. *
  13056. * @memberof IntersectionTests
  13057. *
  13058. * @param {Ray} ray The ray.
  13059. * @param {Cartesian3} p0 The first vertex of the triangle.
  13060. * @param {Cartesian3} p1 The second vertex of the triangle.
  13061. * @param {Cartesian3} p2 The third vertex of the triangle.
  13062. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13063. * and return undefined for intersections with the back face.
  13064. * @returns {Number} The intersection as a parametric distance along the ray, or undefined if there is no intersection.
  13065. */
  13066. IntersectionTests.rayTriangleParametric = function(ray, p0, p1, p2, cullBackFaces) {
  13067. if (!defined(ray)) {
  13068. throw new DeveloperError('ray is required.');
  13069. }
  13070. if (!defined(p0)) {
  13071. throw new DeveloperError('p0 is required.');
  13072. }
  13073. if (!defined(p1)) {
  13074. throw new DeveloperError('p1 is required.');
  13075. }
  13076. if (!defined(p2)) {
  13077. throw new DeveloperError('p2 is required.');
  13078. }
  13079. cullBackFaces = defaultValue(cullBackFaces, false);
  13080. var origin = ray.origin;
  13081. var direction = ray.direction;
  13082. var edge0 = Cartesian3.subtract(p1, p0, scratchEdge0);
  13083. var edge1 = Cartesian3.subtract(p2, p0, scratchEdge1);
  13084. var p = Cartesian3.cross(direction, edge1, scratchPVec);
  13085. var det = Cartesian3.dot(edge0, p);
  13086. var tvec;
  13087. var q;
  13088. var u;
  13089. var v;
  13090. var t;
  13091. if (cullBackFaces) {
  13092. if (det < CesiumMath.EPSILON6) {
  13093. return undefined;
  13094. }
  13095. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  13096. u = Cartesian3.dot(tvec, p);
  13097. if (u < 0.0 || u > det) {
  13098. return undefined;
  13099. }
  13100. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  13101. v = Cartesian3.dot(direction, q);
  13102. if (v < 0.0 || u + v > det) {
  13103. return undefined;
  13104. }
  13105. t = Cartesian3.dot(edge1, q) / det;
  13106. } else {
  13107. if (Math.abs(det) < CesiumMath.EPSILON6) {
  13108. return undefined;
  13109. }
  13110. var invDet = 1.0 / det;
  13111. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  13112. u = Cartesian3.dot(tvec, p) * invDet;
  13113. if (u < 0.0 || u > 1.0) {
  13114. return undefined;
  13115. }
  13116. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  13117. v = Cartesian3.dot(direction, q) * invDet;
  13118. if (v < 0.0 || u + v > 1.0) {
  13119. return undefined;
  13120. }
  13121. t = Cartesian3.dot(edge1, q) * invDet;
  13122. }
  13123. return t;
  13124. };
  13125. /**
  13126. * Computes the intersection of a ray and a triangle as a Cartesian3 coordinate.
  13127. *
  13128. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  13129. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  13130. *
  13131. * @memberof IntersectionTests
  13132. *
  13133. * @param {Ray} ray The ray.
  13134. * @param {Cartesian3} p0 The first vertex of the triangle.
  13135. * @param {Cartesian3} p1 The second vertex of the triangle.
  13136. * @param {Cartesian3} p2 The third vertex of the triangle.
  13137. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13138. * and return undefined for intersections with the back face.
  13139. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  13140. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13141. */
  13142. IntersectionTests.rayTriangle = function(ray, p0, p1, p2, cullBackFaces, result) {
  13143. var t = IntersectionTests.rayTriangleParametric(ray, p0, p1, p2, cullBackFaces);
  13144. if (!defined(t) || t < 0.0) {
  13145. return undefined;
  13146. }
  13147. if (!defined(result)) {
  13148. result = new Cartesian3();
  13149. }
  13150. Cartesian3.multiplyByScalar(ray.direction, t, result);
  13151. return Cartesian3.add(ray.origin, result, result);
  13152. };
  13153. var scratchLineSegmentTriangleRay = new Ray();
  13154. /**
  13155. * Computes the intersection of a line segment and a triangle.
  13156. * @memberof IntersectionTests
  13157. *
  13158. * @param {Cartesian3} v0 The an end point of the line segment.
  13159. * @param {Cartesian3} v1 The other end point of the line segment.
  13160. * @param {Cartesian3} p0 The first vertex of the triangle.
  13161. * @param {Cartesian3} p1 The second vertex of the triangle.
  13162. * @param {Cartesian3} p2 The third vertex of the triangle.
  13163. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13164. * and return undefined for intersections with the back face.
  13165. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  13166. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13167. */
  13168. IntersectionTests.lineSegmentTriangle = function(v0, v1, p0, p1, p2, cullBackFaces, result) {
  13169. if (!defined(v0)) {
  13170. throw new DeveloperError('v0 is required.');
  13171. }
  13172. if (!defined(v1)) {
  13173. throw new DeveloperError('v1 is required.');
  13174. }
  13175. if (!defined(p0)) {
  13176. throw new DeveloperError('p0 is required.');
  13177. }
  13178. if (!defined(p1)) {
  13179. throw new DeveloperError('p1 is required.');
  13180. }
  13181. if (!defined(p2)) {
  13182. throw new DeveloperError('p2 is required.');
  13183. }
  13184. var ray = scratchLineSegmentTriangleRay;
  13185. Cartesian3.clone(v0, ray.origin);
  13186. Cartesian3.subtract(v1, v0, ray.direction);
  13187. Cartesian3.normalize(ray.direction, ray.direction);
  13188. var t = IntersectionTests.rayTriangleParametric(ray, p0, p1, p2, cullBackFaces);
  13189. if (!defined(t) || t < 0.0 || t > Cartesian3.distance(v0, v1)) {
  13190. return undefined;
  13191. }
  13192. if (!defined(result)) {
  13193. result = new Cartesian3();
  13194. }
  13195. Cartesian3.multiplyByScalar(ray.direction, t, result);
  13196. return Cartesian3.add(ray.origin, result, result);
  13197. };
  13198. function solveQuadratic(a, b, c, result) {
  13199. var det = b * b - 4.0 * a * c;
  13200. if (det < 0.0) {
  13201. return undefined;
  13202. } else if (det > 0.0) {
  13203. var denom = 1.0 / (2.0 * a);
  13204. var disc = Math.sqrt(det);
  13205. var root0 = (-b + disc) * denom;
  13206. var root1 = (-b - disc) * denom;
  13207. if (root0 < root1) {
  13208. result.root0 = root0;
  13209. result.root1 = root1;
  13210. } else {
  13211. result.root0 = root1;
  13212. result.root1 = root0;
  13213. }
  13214. return result;
  13215. }
  13216. var root = -b / (2.0 * a);
  13217. if (root === 0.0) {
  13218. return undefined;
  13219. }
  13220. result.root0 = result.root1 = root;
  13221. return result;
  13222. }
  13223. var raySphereRoots = {
  13224. root0 : 0.0,
  13225. root1 : 0.0
  13226. };
  13227. function raySphere(ray, sphere, result) {
  13228. if (!defined(result)) {
  13229. result = {};
  13230. }
  13231. var origin = ray.origin;
  13232. var direction = ray.direction;
  13233. var center = sphere.center;
  13234. var radiusSquared = sphere.radius * sphere.radius;
  13235. var diff = Cartesian3.subtract(origin, center, scratchPVec);
  13236. var a = Cartesian3.dot(direction, direction);
  13237. var b = 2.0 * Cartesian3.dot(direction, diff);
  13238. var c = Cartesian3.magnitudeSquared(diff) - radiusSquared;
  13239. var roots = solveQuadratic(a, b, c, raySphereRoots);
  13240. if (!defined(roots)) {
  13241. return undefined;
  13242. }
  13243. result.start = roots.root0;
  13244. result.stop = roots.root1;
  13245. return result;
  13246. }
  13247. /**
  13248. * Computes the intersection points of a ray with a sphere.
  13249. * @memberof IntersectionTests
  13250. *
  13251. * @param {Ray} ray The ray.
  13252. * @param {BoundingSphere} sphere The sphere.
  13253. * @param {Object} [result] The result onto which to store the result.
  13254. * @returns {Object} An object with the first (<code>start</code>) and the second (<code>stop</code>) intersection scalars for points along the ray or undefined if there are no intersections.
  13255. */
  13256. IntersectionTests.raySphere = function(ray, sphere, result) {
  13257. if (!defined(ray)) {
  13258. throw new DeveloperError('ray is required.');
  13259. }
  13260. if (!defined(sphere)) {
  13261. throw new DeveloperError('sphere is required.');
  13262. }
  13263. result = raySphere(ray, sphere, result);
  13264. if (!defined(result) || result.stop < 0.0) {
  13265. return undefined;
  13266. }
  13267. result.start = Math.max(result.start, 0.0);
  13268. return result;
  13269. };
  13270. var scratchLineSegmentRay = new Ray();
  13271. /**
  13272. * Computes the intersection points of a line segment with a sphere.
  13273. * @memberof IntersectionTests
  13274. *
  13275. * @param {Cartesian3} p0 An end point of the line segment.
  13276. * @param {Cartesian3} p1 The other end point of the line segment.
  13277. * @param {BoundingSphere} sphere The sphere.
  13278. * @param {Object} [result] The result onto which to store the result.
  13279. * @returns {Object} An object with the first (<code>start</code>) and the second (<code>stop</code>) intersection scalars for points along the line segment or undefined if there are no intersections.
  13280. */
  13281. IntersectionTests.lineSegmentSphere = function(p0, p1, sphere, result) {
  13282. if (!defined(p0)) {
  13283. throw new DeveloperError('p0 is required.');
  13284. }
  13285. if (!defined(p1)) {
  13286. throw new DeveloperError('p1 is required.');
  13287. }
  13288. if (!defined(sphere)) {
  13289. throw new DeveloperError('sphere is required.');
  13290. }
  13291. var ray = scratchLineSegmentRay;
  13292. Cartesian3.clone(p0, ray.origin);
  13293. var direction = Cartesian3.subtract(p1, p0, ray.direction);
  13294. var maxT = Cartesian3.magnitude(direction);
  13295. Cartesian3.normalize(direction, direction);
  13296. result = raySphere(ray, sphere, result);
  13297. if (!defined(result) || result.stop < 0.0 || result.start > maxT) {
  13298. return undefined;
  13299. }
  13300. result.start = Math.max(result.start, 0.0);
  13301. result.stop = Math.min(result.stop, maxT);
  13302. return result;
  13303. };
  13304. var scratchQ = new Cartesian3();
  13305. var scratchW = new Cartesian3();
  13306. /**
  13307. * Computes the intersection points of a ray with an ellipsoid.
  13308. *
  13309. * @param {Ray} ray The ray.
  13310. * @param {Ellipsoid} ellipsoid The ellipsoid.
  13311. * @returns {Object} An object with the first (<code>start</code>) and the second (<code>stop</code>) intersection scalars for points along the ray or undefined if there are no intersections.
  13312. */
  13313. IntersectionTests.rayEllipsoid = function(ray, ellipsoid) {
  13314. if (!defined(ray)) {
  13315. throw new DeveloperError('ray is required.');
  13316. }
  13317. if (!defined(ellipsoid)) {
  13318. throw new DeveloperError('ellipsoid is required.');
  13319. }
  13320. var inverseRadii = ellipsoid.oneOverRadii;
  13321. var q = Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  13322. var w = Cartesian3.multiplyComponents(inverseRadii, ray.direction, scratchW);
  13323. var q2 = Cartesian3.magnitudeSquared(q);
  13324. var qw = Cartesian3.dot(q, w);
  13325. var difference, w2, product, discriminant, temp;
  13326. if (q2 > 1.0) {
  13327. // Outside ellipsoid.
  13328. if (qw >= 0.0) {
  13329. // Looking outward or tangent (0 intersections).
  13330. return undefined;
  13331. }
  13332. // qw < 0.0.
  13333. var qw2 = qw * qw;
  13334. difference = q2 - 1.0; // Positively valued.
  13335. w2 = Cartesian3.magnitudeSquared(w);
  13336. product = w2 * difference;
  13337. if (qw2 < product) {
  13338. // Imaginary roots (0 intersections).
  13339. return undefined;
  13340. } else if (qw2 > product) {
  13341. // Distinct roots (2 intersections).
  13342. discriminant = qw * qw - product;
  13343. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  13344. var root0 = temp / w2;
  13345. var root1 = difference / temp;
  13346. if (root0 < root1) {
  13347. return {
  13348. start : root0,
  13349. stop : root1
  13350. };
  13351. }
  13352. return {
  13353. start : root1,
  13354. stop : root0
  13355. };
  13356. } else {
  13357. // qw2 == product. Repeated roots (2 intersections).
  13358. var root = Math.sqrt(difference / w2);
  13359. return {
  13360. start : root,
  13361. stop : root
  13362. };
  13363. }
  13364. } else if (q2 < 1.0) {
  13365. // Inside ellipsoid (2 intersections).
  13366. difference = q2 - 1.0; // Negatively valued.
  13367. w2 = Cartesian3.magnitudeSquared(w);
  13368. product = w2 * difference; // Negatively valued.
  13369. discriminant = qw * qw - product;
  13370. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  13371. return {
  13372. start : 0.0,
  13373. stop : temp / w2
  13374. };
  13375. } else {
  13376. // q2 == 1.0. On ellipsoid.
  13377. if (qw < 0.0) {
  13378. // Looking inward.
  13379. w2 = Cartesian3.magnitudeSquared(w);
  13380. return {
  13381. start : 0.0,
  13382. stop : -qw / w2
  13383. };
  13384. }
  13385. // qw >= 0.0. Looking outward or tangent.
  13386. return undefined;
  13387. }
  13388. };
  13389. function addWithCancellationCheck(left, right, tolerance) {
  13390. var difference = left + right;
  13391. if ((CesiumMath.sign(left) !== CesiumMath.sign(right)) &&
  13392. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance) {
  13393. return 0.0;
  13394. }
  13395. return difference;
  13396. }
  13397. function quadraticVectorExpression(A, b, c, x, w) {
  13398. var xSquared = x * x;
  13399. var wSquared = w * w;
  13400. var l2 = (A[Matrix3.COLUMN1ROW1] - A[Matrix3.COLUMN2ROW2]) * wSquared;
  13401. var l1 = w * (x * addWithCancellationCheck(A[Matrix3.COLUMN1ROW0], A[Matrix3.COLUMN0ROW1], CesiumMath.EPSILON15) + b.y);
  13402. var l0 = (A[Matrix3.COLUMN0ROW0] * xSquared + A[Matrix3.COLUMN2ROW2] * wSquared) + x * b.x + c;
  13403. var r1 = wSquared * addWithCancellationCheck(A[Matrix3.COLUMN2ROW1], A[Matrix3.COLUMN1ROW2], CesiumMath.EPSILON15);
  13404. var r0 = w * (x * addWithCancellationCheck(A[Matrix3.COLUMN2ROW0], A[Matrix3.COLUMN0ROW2]) + b.z);
  13405. var cosines;
  13406. var solutions = [];
  13407. if (r0 === 0.0 && r1 === 0.0) {
  13408. cosines = QuadraticRealPolynomial.computeRealRoots(l2, l1, l0);
  13409. if (cosines.length === 0) {
  13410. return solutions;
  13411. }
  13412. var cosine0 = cosines[0];
  13413. var sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  13414. solutions.push(new Cartesian3(x, w * cosine0, w * -sine0));
  13415. solutions.push(new Cartesian3(x, w * cosine0, w * sine0));
  13416. if (cosines.length === 2) {
  13417. var cosine1 = cosines[1];
  13418. var sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  13419. solutions.push(new Cartesian3(x, w * cosine1, w * -sine1));
  13420. solutions.push(new Cartesian3(x, w * cosine1, w * sine1));
  13421. }
  13422. return solutions;
  13423. }
  13424. var r0Squared = r0 * r0;
  13425. var r1Squared = r1 * r1;
  13426. var l2Squared = l2 * l2;
  13427. var r0r1 = r0 * r1;
  13428. var c4 = l2Squared + r1Squared;
  13429. var c3 = 2.0 * (l1 * l2 + r0r1);
  13430. var c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  13431. var c1 = 2.0 * (l0 * l1 - r0r1);
  13432. var c0 = l0 * l0 - r0Squared;
  13433. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  13434. return solutions;
  13435. }
  13436. cosines = QuarticRealPolynomial.computeRealRoots(c4, c3, c2, c1, c0);
  13437. var length = cosines.length;
  13438. if (length === 0) {
  13439. return solutions;
  13440. }
  13441. for ( var i = 0; i < length; ++i) {
  13442. var cosine = cosines[i];
  13443. var cosineSquared = cosine * cosine;
  13444. var sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  13445. var sine = Math.sqrt(sineSquared);
  13446. //var left = l2 * cosineSquared + l1 * cosine + l0;
  13447. var left;
  13448. if (CesiumMath.sign(l2) === CesiumMath.sign(l0)) {
  13449. left = addWithCancellationCheck(l2 * cosineSquared + l0, l1 * cosine, CesiumMath.EPSILON12);
  13450. } else if (CesiumMath.sign(l0) === CesiumMath.sign(l1 * cosine)) {
  13451. left = addWithCancellationCheck(l2 * cosineSquared, l1 * cosine + l0, CesiumMath.EPSILON12);
  13452. } else {
  13453. left = addWithCancellationCheck(l2 * cosineSquared + l1 * cosine, l0, CesiumMath.EPSILON12);
  13454. }
  13455. var right = addWithCancellationCheck(r1 * cosine, r0, CesiumMath.EPSILON15);
  13456. var product = left * right;
  13457. if (product < 0.0) {
  13458. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  13459. } else if (product > 0.0) {
  13460. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  13461. } else if (sine !== 0.0) {
  13462. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  13463. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  13464. ++i;
  13465. } else {
  13466. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  13467. }
  13468. }
  13469. return solutions;
  13470. }
  13471. var firstAxisScratch = new Cartesian3();
  13472. var secondAxisScratch = new Cartesian3();
  13473. var thirdAxisScratch = new Cartesian3();
  13474. var referenceScratch = new Cartesian3();
  13475. var bCart = new Cartesian3();
  13476. var bScratch = new Matrix3();
  13477. var btScratch = new Matrix3();
  13478. var diScratch = new Matrix3();
  13479. var dScratch = new Matrix3();
  13480. var cScratch = new Matrix3();
  13481. var tempMatrix = new Matrix3();
  13482. var aScratch = new Matrix3();
  13483. var sScratch = new Cartesian3();
  13484. var closestScratch = new Cartesian3();
  13485. var surfPointScratch = new Cartographic();
  13486. /**
  13487. * Provides the point along the ray which is nearest to the ellipsoid.
  13488. *
  13489. * @param {Ray} ray The ray.
  13490. * @param {Ellipsoid} ellipsoid The ellipsoid.
  13491. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  13492. */
  13493. IntersectionTests.grazingAltitudeLocation = function(ray, ellipsoid) {
  13494. if (!defined(ray)) {
  13495. throw new DeveloperError('ray is required.');
  13496. }
  13497. if (!defined(ellipsoid)) {
  13498. throw new DeveloperError('ellipsoid is required.');
  13499. }
  13500. var position = ray.origin;
  13501. var direction = ray.direction;
  13502. if (!Cartesian3.equals(position, Cartesian3.ZERO)) {
  13503. var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  13504. if (Cartesian3.dot(direction, normal) >= 0.0) { // The location provided is the closest point in altitude
  13505. return position;
  13506. }
  13507. }
  13508. var intersects = defined(this.rayEllipsoid(ray, ellipsoid));
  13509. // Compute the scaled direction vector.
  13510. var f = ellipsoid.transformPositionToScaledSpace(direction, firstAxisScratch);
  13511. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  13512. var firstAxis = Cartesian3.normalize(f, f);
  13513. var reference = Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  13514. var secondAxis = Cartesian3.normalize(Cartesian3.cross(reference, firstAxis, secondAxisScratch), secondAxisScratch);
  13515. var thirdAxis = Cartesian3.normalize(Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch), thirdAxisScratch);
  13516. var B = bScratch;
  13517. B[0] = firstAxis.x;
  13518. B[1] = firstAxis.y;
  13519. B[2] = firstAxis.z;
  13520. B[3] = secondAxis.x;
  13521. B[4] = secondAxis.y;
  13522. B[5] = secondAxis.z;
  13523. B[6] = thirdAxis.x;
  13524. B[7] = thirdAxis.y;
  13525. B[8] = thirdAxis.z;
  13526. var B_T = Matrix3.transpose(B, btScratch);
  13527. // Get the scaling matrix and its inverse.
  13528. var D_I = Matrix3.fromScale(ellipsoid.radii, diScratch);
  13529. var D = Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  13530. var C = cScratch;
  13531. C[0] = 0.0;
  13532. C[1] = -direction.z;
  13533. C[2] = direction.y;
  13534. C[3] = direction.z;
  13535. C[4] = 0.0;
  13536. C[5] = -direction.x;
  13537. C[6] = -direction.y;
  13538. C[7] = direction.x;
  13539. C[8] = 0.0;
  13540. var temp = Matrix3.multiply(Matrix3.multiply(B_T, D, tempMatrix), C, tempMatrix);
  13541. var A = Matrix3.multiply(Matrix3.multiply(temp, D_I, aScratch), B, aScratch);
  13542. var b = Matrix3.multiplyByVector(temp, position, bCart);
  13543. // Solve for the solutions to the expression in standard form:
  13544. var solutions = quadraticVectorExpression(A, Cartesian3.negate(b, firstAxisScratch), 0.0, 0.0, 1.0);
  13545. var s;
  13546. var altitude;
  13547. var length = solutions.length;
  13548. if (length > 0) {
  13549. var closest = Cartesian3.clone(Cartesian3.ZERO, closestScratch);
  13550. var maximumValue = Number.NEGATIVE_INFINITY;
  13551. for ( var i = 0; i < length; ++i) {
  13552. s = Matrix3.multiplyByVector(D_I, Matrix3.multiplyByVector(B, solutions[i], sScratch), sScratch);
  13553. var v = Cartesian3.normalize(Cartesian3.subtract(s, position, referenceScratch), referenceScratch);
  13554. var dotProduct = Cartesian3.dot(v, direction);
  13555. if (dotProduct > maximumValue) {
  13556. maximumValue = dotProduct;
  13557. closest = Cartesian3.clone(s, closest);
  13558. }
  13559. }
  13560. var surfacePoint = ellipsoid.cartesianToCartographic(closest, surfPointScratch);
  13561. maximumValue = CesiumMath.clamp(maximumValue, 0.0, 1.0);
  13562. altitude = Cartesian3.magnitude(Cartesian3.subtract(closest, position, referenceScratch)) * Math.sqrt(1.0 - maximumValue * maximumValue);
  13563. altitude = intersects ? -altitude : altitude;
  13564. surfacePoint.height = altitude;
  13565. return ellipsoid.cartographicToCartesian(surfacePoint, new Cartesian3());
  13566. }
  13567. return undefined;
  13568. };
  13569. var lineSegmentPlaneDifference = new Cartesian3();
  13570. /**
  13571. * Computes the intersection of a line segment and a plane.
  13572. *
  13573. * @param {Cartesian3} endPoint0 An end point of the line segment.
  13574. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  13575. * @param {Plane} plane The plane.
  13576. * @param {Cartesian3} [result] The object onto which to store the result.
  13577. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  13578. *
  13579. * @example
  13580. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  13581. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  13582. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  13583. *
  13584. * var p0 = new Cesium.Cartesian3(...);
  13585. * var p1 = new Cesium.Cartesian3(...);
  13586. *
  13587. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  13588. * var intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  13589. */
  13590. IntersectionTests.lineSegmentPlane = function(endPoint0, endPoint1, plane, result) {
  13591. if (!defined(endPoint0)) {
  13592. throw new DeveloperError('endPoint0 is required.');
  13593. }
  13594. if (!defined(endPoint1)) {
  13595. throw new DeveloperError('endPoint1 is required.');
  13596. }
  13597. if (!defined(plane)) {
  13598. throw new DeveloperError('plane is required.');
  13599. }
  13600. if (!defined(result)) {
  13601. result = new Cartesian3();
  13602. }
  13603. var difference = Cartesian3.subtract(endPoint1, endPoint0, lineSegmentPlaneDifference);
  13604. var normal = plane.normal;
  13605. var nDotDiff = Cartesian3.dot(normal, difference);
  13606. // check if the segment and plane are parallel
  13607. if (Math.abs(nDotDiff) < CesiumMath.EPSILON6) {
  13608. return undefined;
  13609. }
  13610. var nDotP0 = Cartesian3.dot(normal, endPoint0);
  13611. var t = -(plane.distance + nDotP0) / nDotDiff;
  13612. // intersection only if t is in [0, 1]
  13613. if (t < 0.0 || t > 1.0) {
  13614. return undefined;
  13615. }
  13616. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  13617. Cartesian3.multiplyByScalar(difference, t, result);
  13618. Cartesian3.add(endPoint0, result, result);
  13619. return result;
  13620. };
  13621. /**
  13622. * Computes the intersection of a triangle and a plane
  13623. *
  13624. * @param {Cartesian3} p0 First point of the triangle
  13625. * @param {Cartesian3} p1 Second point of the triangle
  13626. * @param {Cartesian3} p2 Third point of the triangle
  13627. * @param {Plane} plane Intersection plane
  13628. * @returns {Object} An object with properties <code>positions</code> and <code>indices</code>, which are arrays that represent three triangles that do not cross the plane. (Undefined if no intersection exists)
  13629. *
  13630. * @example
  13631. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  13632. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  13633. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  13634. *
  13635. * var p0 = new Cesium.Cartesian3(...);
  13636. * var p1 = new Cesium.Cartesian3(...);
  13637. * var p2 = new Cesium.Cartesian3(...);
  13638. *
  13639. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  13640. * var triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  13641. */
  13642. IntersectionTests.trianglePlaneIntersection = function(p0, p1, p2, plane) {
  13643. if ((!defined(p0)) ||
  13644. (!defined(p1)) ||
  13645. (!defined(p2)) ||
  13646. (!defined(plane))) {
  13647. throw new DeveloperError('p0, p1, p2, and plane are required.');
  13648. }
  13649. var planeNormal = plane.normal;
  13650. var planeD = plane.distance;
  13651. var p0Behind = (Cartesian3.dot(planeNormal, p0) + planeD) < 0.0;
  13652. var p1Behind = (Cartesian3.dot(planeNormal, p1) + planeD) < 0.0;
  13653. var p2Behind = (Cartesian3.dot(planeNormal, p2) + planeD) < 0.0;
  13654. // Given these dots products, the calls to lineSegmentPlaneIntersection
  13655. // always have defined results.
  13656. var numBehind = 0;
  13657. numBehind += p0Behind ? 1 : 0;
  13658. numBehind += p1Behind ? 1 : 0;
  13659. numBehind += p2Behind ? 1 : 0;
  13660. var u1, u2;
  13661. if (numBehind === 1 || numBehind === 2) {
  13662. u1 = new Cartesian3();
  13663. u2 = new Cartesian3();
  13664. }
  13665. if (numBehind === 1) {
  13666. if (p0Behind) {
  13667. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  13668. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  13669. return {
  13670. positions : [p0, p1, p2, u1, u2 ],
  13671. indices : [
  13672. // Behind
  13673. 0, 3, 4,
  13674. // In front
  13675. 1, 2, 4,
  13676. 1, 4, 3
  13677. ]
  13678. };
  13679. } else if (p1Behind) {
  13680. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  13681. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  13682. return {
  13683. positions : [p0, p1, p2, u1, u2 ],
  13684. indices : [
  13685. // Behind
  13686. 1, 3, 4,
  13687. // In front
  13688. 2, 0, 4,
  13689. 2, 4, 3
  13690. ]
  13691. };
  13692. } else if (p2Behind) {
  13693. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  13694. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  13695. return {
  13696. positions : [p0, p1, p2, u1, u2 ],
  13697. indices : [
  13698. // Behind
  13699. 2, 3, 4,
  13700. // In front
  13701. 0, 1, 4,
  13702. 0, 4, 3
  13703. ]
  13704. };
  13705. }
  13706. } else if (numBehind === 2) {
  13707. if (!p0Behind) {
  13708. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  13709. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  13710. return {
  13711. positions : [p0, p1, p2, u1, u2 ],
  13712. indices : [
  13713. // Behind
  13714. 1, 2, 4,
  13715. 1, 4, 3,
  13716. // In front
  13717. 0, 3, 4
  13718. ]
  13719. };
  13720. } else if (!p1Behind) {
  13721. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  13722. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  13723. return {
  13724. positions : [p0, p1, p2, u1, u2 ],
  13725. indices : [
  13726. // Behind
  13727. 2, 0, 4,
  13728. 2, 4, 3,
  13729. // In front
  13730. 1, 3, 4
  13731. ]
  13732. };
  13733. } else if (!p2Behind) {
  13734. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  13735. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  13736. return {
  13737. positions : [p0, p1, p2, u1, u2 ],
  13738. indices : [
  13739. // Behind
  13740. 0, 1, 4,
  13741. 0, 4, 3,
  13742. // In front
  13743. 2, 3, 4
  13744. ]
  13745. };
  13746. }
  13747. }
  13748. // if numBehind is 3, the triangle is completely behind the plane;
  13749. // otherwise, it is completely in front (numBehind is 0).
  13750. return undefined;
  13751. };
  13752. return IntersectionTests;
  13753. });
  13754. /*global define*/
  13755. define('Core/Plane',[
  13756. './Cartesian3',
  13757. './defined',
  13758. './DeveloperError',
  13759. './freezeObject'
  13760. ], function(
  13761. Cartesian3,
  13762. defined,
  13763. DeveloperError,
  13764. freezeObject) {
  13765. 'use strict';
  13766. /**
  13767. * A plane in Hessian Normal Form defined by
  13768. * <pre>
  13769. * ax + by + cz + d = 0
  13770. * </pre>
  13771. * where (a, b, c) is the plane's <code>normal</code>, d is the signed
  13772. * <code>distance</code> to the plane, and (x, y, z) is any point on
  13773. * the plane.
  13774. *
  13775. * @alias Plane
  13776. * @constructor
  13777. *
  13778. * @param {Cartesian3} normal The plane's normal (normalized).
  13779. * @param {Number} distance The shortest distance from the origin to the plane. The sign of
  13780. * <code>distance</code> determines which side of the plane the origin
  13781. * is on. If <code>distance</code> is positive, the origin is in the half-space
  13782. * in the direction of the normal; if negative, the origin is in the half-space
  13783. * opposite to the normal; if zero, the plane passes through the origin.
  13784. *
  13785. * @example
  13786. * // The plane x=0
  13787. * var plane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X, 0.0);
  13788. */
  13789. function Plane(normal, distance) {
  13790. if (!defined(normal)) {
  13791. throw new DeveloperError('normal is required.');
  13792. }
  13793. if (!defined(distance)) {
  13794. throw new DeveloperError('distance is required.');
  13795. }
  13796. /**
  13797. * The plane's normal.
  13798. *
  13799. * @type {Cartesian3}
  13800. */
  13801. this.normal = Cartesian3.clone(normal);
  13802. /**
  13803. * The shortest distance from the origin to the plane. The sign of
  13804. * <code>distance</code> determines which side of the plane the origin
  13805. * is on. If <code>distance</code> is positive, the origin is in the half-space
  13806. * in the direction of the normal; if negative, the origin is in the half-space
  13807. * opposite to the normal; if zero, the plane passes through the origin.
  13808. *
  13809. * @type {Number}
  13810. */
  13811. this.distance = distance;
  13812. }
  13813. /**
  13814. * Creates a plane from a normal and a point on the plane.
  13815. *
  13816. * @param {Cartesian3} point The point on the plane.
  13817. * @param {Cartesian3} normal The plane's normal (normalized).
  13818. * @param {Plane} [result] The object onto which to store the result.
  13819. * @returns {Plane} A new plane instance or the modified result parameter.
  13820. *
  13821. * @example
  13822. * var point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  13823. * var normal = ellipsoid.geodeticSurfaceNormal(point);
  13824. * var tangentPlane = Cesium.Plane.fromPointNormal(point, normal);
  13825. */
  13826. Plane.fromPointNormal = function(point, normal, result) {
  13827. if (!defined(point)) {
  13828. throw new DeveloperError('point is required.');
  13829. }
  13830. if (!defined(normal)) {
  13831. throw new DeveloperError('normal is required.');
  13832. }
  13833. var distance = -Cartesian3.dot(normal, point);
  13834. if (!defined(result)) {
  13835. return new Plane(normal, distance);
  13836. }
  13837. Cartesian3.clone(normal, result.normal);
  13838. result.distance = distance;
  13839. return result;
  13840. };
  13841. var scratchNormal = new Cartesian3();
  13842. /**
  13843. * Creates a plane from the general equation
  13844. *
  13845. * @param {Cartesian4} coefficients The plane's normal (normalized).
  13846. * @param {Plane} [result] The object onto which to store the result.
  13847. * @returns {Plane} A new plane instance or the modified result parameter.
  13848. */
  13849. Plane.fromCartesian4 = function(coefficients, result) {
  13850. if (!defined(coefficients)) {
  13851. throw new DeveloperError('coefficients is required.');
  13852. }
  13853. var normal = Cartesian3.fromCartesian4(coefficients, scratchNormal);
  13854. var distance = coefficients.w;
  13855. if (!defined(result)) {
  13856. return new Plane(normal, distance);
  13857. } else {
  13858. Cartesian3.clone(normal, result.normal);
  13859. result.distance = distance;
  13860. return result;
  13861. }
  13862. };
  13863. /**
  13864. * Computes the signed shortest distance of a point to a plane.
  13865. * The sign of the distance determines which side of the plane the point
  13866. * is on. If the distance is positive, the point is in the half-space
  13867. * in the direction of the normal; if negative, the point is in the half-space
  13868. * opposite to the normal; if zero, the plane passes through the point.
  13869. *
  13870. * @param {Plane} plane The plane.
  13871. * @param {Cartesian3} point The point.
  13872. * @returns {Number} The signed shortest distance of the point to the plane.
  13873. */
  13874. Plane.getPointDistance = function(plane, point) {
  13875. if (!defined(plane)) {
  13876. throw new DeveloperError('plane is required.');
  13877. }
  13878. if (!defined(point)) {
  13879. throw new DeveloperError('point is required.');
  13880. }
  13881. return Cartesian3.dot(plane.normal, point) + plane.distance;
  13882. };
  13883. /**
  13884. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  13885. *
  13886. * @type {Plane}
  13887. * @constant
  13888. */
  13889. Plane.ORIGIN_XY_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Z, 0.0));
  13890. /**
  13891. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  13892. *
  13893. * @type {Plane}
  13894. * @constant
  13895. */
  13896. Plane.ORIGIN_YZ_PLANE = freezeObject(new Plane(Cartesian3.UNIT_X, 0.0));
  13897. /**
  13898. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  13899. *
  13900. * @type {Plane}
  13901. * @constant
  13902. */
  13903. Plane.ORIGIN_ZX_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Y, 0.0));
  13904. return Plane;
  13905. });
  13906. /**
  13907. @license
  13908. when.js - https://github.com/cujojs/when
  13909. MIT License (c) copyright B Cavalier & J Hann
  13910. * A lightweight CommonJS Promises/A and when() implementation
  13911. * when is part of the cujo.js family of libraries (http://cujojs.com/)
  13912. *
  13913. * Licensed under the MIT License at:
  13914. * http://www.opensource.org/licenses/mit-license.php
  13915. *
  13916. * @version 1.7.1
  13917. */
  13918. (function(define) { 'use strict';
  13919. define('ThirdParty/when',[],function () {
  13920. var reduceArray, slice, undef;
  13921. //
  13922. // Public API
  13923. //
  13924. when.defer = defer; // Create a deferred
  13925. when.resolve = resolve; // Create a resolved promise
  13926. when.reject = reject; // Create a rejected promise
  13927. when.join = join; // Join 2 or more promises
  13928. when.all = all; // Resolve a list of promises
  13929. when.map = map; // Array.map() for promises
  13930. when.reduce = reduce; // Array.reduce() for promises
  13931. when.any = any; // One-winner race
  13932. when.some = some; // Multi-winner race
  13933. when.chain = chain; // Make a promise trigger another resolver
  13934. when.isPromise = isPromise; // Determine if a thing is a promise
  13935. /**
  13936. * Register an observer for a promise or immediate value.
  13937. *
  13938. * @param {*} promiseOrValue
  13939. * @param {function?} [onFulfilled] callback to be called when promiseOrValue is
  13940. * successfully fulfilled. If promiseOrValue is an immediate value, callback
  13941. * will be invoked immediately.
  13942. * @param {function?} [onRejected] callback to be called when promiseOrValue is
  13943. * rejected.
  13944. * @param {function?} [onProgress] callback to be called when progress updates
  13945. * are issued for promiseOrValue.
  13946. * @returns {Promise} a new {@link Promise} that will complete with the return
  13947. * value of callback or errback or the completion value of promiseOrValue if
  13948. * callback and/or errback is not supplied.
  13949. */
  13950. function when(promiseOrValue, onFulfilled, onRejected, onProgress) {
  13951. // Get a trusted promise for the input promiseOrValue, and then
  13952. // register promise handlers
  13953. return resolve(promiseOrValue).then(onFulfilled, onRejected, onProgress);
  13954. }
  13955. /**
  13956. * Returns promiseOrValue if promiseOrValue is a {@link Promise}, a new Promise if
  13957. * promiseOrValue is a foreign promise, or a new, already-fulfilled {@link Promise}
  13958. * whose value is promiseOrValue if promiseOrValue is an immediate value.
  13959. *
  13960. * @param {*} promiseOrValue
  13961. * @returns Guaranteed to return a trusted Promise. If promiseOrValue is a when.js {@link Promise}
  13962. * returns promiseOrValue, otherwise, returns a new, already-resolved, when.js {@link Promise}
  13963. * whose resolution value is:
  13964. * * the resolution value of promiseOrValue if it's a foreign promise, or
  13965. * * promiseOrValue if it's a value
  13966. */
  13967. function resolve(promiseOrValue) {
  13968. var promise, deferred;
  13969. if(promiseOrValue instanceof Promise) {
  13970. // It's a when.js promise, so we trust it
  13971. promise = promiseOrValue;
  13972. } else {
  13973. // It's not a when.js promise. See if it's a foreign promise or a value.
  13974. if(isPromise(promiseOrValue)) {
  13975. // It's a thenable, but we don't know where it came from, so don't trust
  13976. // its implementation entirely. Introduce a trusted middleman when.js promise
  13977. deferred = defer();
  13978. // IMPORTANT: This is the only place when.js should ever call .then() on an
  13979. // untrusted promise. Don't expose the return value to the untrusted promise
  13980. promiseOrValue.then(
  13981. function(value) { deferred.resolve(value); },
  13982. function(reason) { deferred.reject(reason); },
  13983. function(update) { deferred.progress(update); }
  13984. );
  13985. promise = deferred.promise;
  13986. } else {
  13987. // It's a value, not a promise. Create a resolved promise for it.
  13988. promise = fulfilled(promiseOrValue);
  13989. }
  13990. }
  13991. return promise;
  13992. }
  13993. /**
  13994. * Returns a rejected promise for the supplied promiseOrValue. The returned
  13995. * promise will be rejected with:
  13996. * - promiseOrValue, if it is a value, or
  13997. * - if promiseOrValue is a promise
  13998. * - promiseOrValue's value after it is fulfilled
  13999. * - promiseOrValue's reason after it is rejected
  14000. * @param {*} promiseOrValue the rejected value of the returned {@link Promise}
  14001. * @returns {Promise} rejected {@link Promise}
  14002. */
  14003. function reject(promiseOrValue) {
  14004. return when(promiseOrValue, rejected);
  14005. }
  14006. /**
  14007. * Trusted Promise constructor. A Promise created from this constructor is
  14008. * a trusted when.js promise. Any other duck-typed promise is considered
  14009. * untrusted.
  14010. * @constructor
  14011. * @name Promise
  14012. */
  14013. function Promise(then) {
  14014. this.then = then;
  14015. }
  14016. Promise.prototype = {
  14017. /**
  14018. * Register a callback that will be called when a promise is
  14019. * fulfilled or rejected. Optionally also register a progress handler.
  14020. * Shortcut for .then(onFulfilledOrRejected, onFulfilledOrRejected, onProgress)
  14021. * @param {function?} [onFulfilledOrRejected]
  14022. * @param {function?} [onProgress]
  14023. * @returns {Promise}
  14024. */
  14025. always: function(onFulfilledOrRejected, onProgress) {
  14026. return this.then(onFulfilledOrRejected, onFulfilledOrRejected, onProgress);
  14027. },
  14028. /**
  14029. * Register a rejection handler. Shortcut for .then(undefined, onRejected)
  14030. * @param {function?} onRejected
  14031. * @returns {Promise}
  14032. */
  14033. otherwise: function(onRejected) {
  14034. return this.then(undef, onRejected);
  14035. },
  14036. /**
  14037. * Shortcut for .then(function() { return value; })
  14038. * @param {*} value
  14039. * @returns {Promise} a promise that:
  14040. * - is fulfilled if value is not a promise, or
  14041. * - if value is a promise, will fulfill with its value, or reject
  14042. * with its reason.
  14043. */
  14044. yield: function(value) {
  14045. return this.then(function() {
  14046. return value;
  14047. });
  14048. },
  14049. /**
  14050. * Assumes that this promise will fulfill with an array, and arranges
  14051. * for the onFulfilled to be called with the array as its argument list
  14052. * i.e. onFulfilled.spread(undefined, array).
  14053. * @param {function} onFulfilled function to receive spread arguments
  14054. * @returns {Promise}
  14055. */
  14056. spread: function(onFulfilled) {
  14057. return this.then(function(array) {
  14058. // array may contain promises, so resolve its contents.
  14059. return all(array, function(array) {
  14060. return onFulfilled.apply(undef, array);
  14061. });
  14062. });
  14063. }
  14064. };
  14065. /**
  14066. * Create an already-resolved promise for the supplied value
  14067. * @private
  14068. *
  14069. * @param {*} value
  14070. * @returns {Promise} fulfilled promise
  14071. */
  14072. function fulfilled(value) {
  14073. var p = new Promise(function(onFulfilled) {
  14074. // TODO: Promises/A+ check typeof onFulfilled
  14075. try {
  14076. return resolve(onFulfilled ? onFulfilled(value) : value);
  14077. } catch(e) {
  14078. return rejected(e);
  14079. }
  14080. });
  14081. return p;
  14082. }
  14083. /**
  14084. * Create an already-rejected {@link Promise} with the supplied
  14085. * rejection reason.
  14086. * @private
  14087. *
  14088. * @param {*} reason
  14089. * @returns {Promise} rejected promise
  14090. */
  14091. function rejected(reason) {
  14092. var p = new Promise(function(_, onRejected) {
  14093. // TODO: Promises/A+ check typeof onRejected
  14094. try {
  14095. return onRejected ? resolve(onRejected(reason)) : rejected(reason);
  14096. } catch(e) {
  14097. return rejected(e);
  14098. }
  14099. });
  14100. return p;
  14101. }
  14102. /**
  14103. * Creates a new, Deferred with fully isolated resolver and promise parts,
  14104. * either or both of which may be given out safely to consumers.
  14105. * The Deferred itself has the full API: resolve, reject, progress, and
  14106. * then. The resolver has resolve, reject, and progress. The promise
  14107. * only has then.
  14108. *
  14109. * @returns {Deferred}
  14110. */
  14111. function defer() {
  14112. var deferred, promise, handlers, progressHandlers,
  14113. _then, _progress, _resolve;
  14114. /**
  14115. * The promise for the new deferred
  14116. * @type {Promise}
  14117. */
  14118. promise = new Promise(then);
  14119. /**
  14120. * The full Deferred object, with {@link Promise} and {@link Resolver} parts
  14121. * @class Deferred
  14122. * @name Deferred
  14123. */
  14124. deferred = {
  14125. then: then, // DEPRECATED: use deferred.promise.then
  14126. resolve: promiseResolve,
  14127. reject: promiseReject,
  14128. // TODO: Consider renaming progress() to notify()
  14129. progress: promiseProgress,
  14130. promise: promise,
  14131. resolver: {
  14132. resolve: promiseResolve,
  14133. reject: promiseReject,
  14134. progress: promiseProgress
  14135. }
  14136. };
  14137. handlers = [];
  14138. progressHandlers = [];
  14139. /**
  14140. * Pre-resolution then() that adds the supplied callback, errback, and progback
  14141. * functions to the registered listeners
  14142. * @private
  14143. *
  14144. * @param {function?} [onFulfilled] resolution handler
  14145. * @param {function?} [onRejected] rejection handler
  14146. * @param {function?} [onProgress] progress handler
  14147. */
  14148. _then = function(onFulfilled, onRejected, onProgress) {
  14149. // TODO: Promises/A+ check typeof onFulfilled, onRejected, onProgress
  14150. var deferred, progressHandler;
  14151. deferred = defer();
  14152. progressHandler = typeof onProgress === 'function'
  14153. ? function(update) {
  14154. try {
  14155. // Allow progress handler to transform progress event
  14156. deferred.progress(onProgress(update));
  14157. } catch(e) {
  14158. // Use caught value as progress
  14159. deferred.progress(e);
  14160. }
  14161. }
  14162. : function(update) { deferred.progress(update); };
  14163. handlers.push(function(promise) {
  14164. promise.then(onFulfilled, onRejected)
  14165. .then(deferred.resolve, deferred.reject, progressHandler);
  14166. });
  14167. progressHandlers.push(progressHandler);
  14168. return deferred.promise;
  14169. };
  14170. /**
  14171. * Issue a progress event, notifying all progress listeners
  14172. * @private
  14173. * @param {*} update progress event payload to pass to all listeners
  14174. */
  14175. _progress = function(update) {
  14176. processQueue(progressHandlers, update);
  14177. return update;
  14178. };
  14179. /**
  14180. * Transition from pre-resolution state to post-resolution state, notifying
  14181. * all listeners of the resolution or rejection
  14182. * @private
  14183. * @param {*} value the value of this deferred
  14184. */
  14185. _resolve = function(value) {
  14186. value = resolve(value);
  14187. // Replace _then with one that directly notifies with the result.
  14188. _then = value.then;
  14189. // Replace _resolve so that this Deferred can only be resolved once
  14190. _resolve = resolve;
  14191. // Make _progress a noop, to disallow progress for the resolved promise.
  14192. _progress = noop;
  14193. // Notify handlers
  14194. processQueue(handlers, value);
  14195. // Free progressHandlers array since we'll never issue progress events
  14196. progressHandlers = handlers = undef;
  14197. return value;
  14198. };
  14199. return deferred;
  14200. /**
  14201. * Wrapper to allow _then to be replaced safely
  14202. * @param {function?} [onFulfilled] resolution handler
  14203. * @param {function?} [onRejected] rejection handler
  14204. * @param {function?} [onProgress] progress handler
  14205. * @returns {Promise} new promise
  14206. */
  14207. function then(onFulfilled, onRejected, onProgress) {
  14208. // TODO: Promises/A+ check typeof onFulfilled, onRejected, onProgress
  14209. return _then(onFulfilled, onRejected, onProgress);
  14210. }
  14211. /**
  14212. * Wrapper to allow _resolve to be replaced
  14213. */
  14214. function promiseResolve(val) {
  14215. return _resolve(val);
  14216. }
  14217. /**
  14218. * Wrapper to allow _reject to be replaced
  14219. */
  14220. function promiseReject(err) {
  14221. return _resolve(rejected(err));
  14222. }
  14223. /**
  14224. * Wrapper to allow _progress to be replaced
  14225. */
  14226. function promiseProgress(update) {
  14227. return _progress(update);
  14228. }
  14229. }
  14230. /**
  14231. * Determines if promiseOrValue is a promise or not. Uses the feature
  14232. * test from http://wiki.commonjs.org/wiki/Promises/A to determine if
  14233. * promiseOrValue is a promise.
  14234. *
  14235. * @param {*} promiseOrValue anything
  14236. * @returns {boolean} true if promiseOrValue is a {@link Promise}
  14237. */
  14238. function isPromise(promiseOrValue) {
  14239. return promiseOrValue && typeof promiseOrValue.then === 'function';
  14240. }
  14241. /**
  14242. * Initiates a competitive race, returning a promise that will resolve when
  14243. * howMany of the supplied promisesOrValues have resolved, or will reject when
  14244. * it becomes impossible for howMany to resolve, for example, when
  14245. * (promisesOrValues.length - howMany) + 1 input promises reject.
  14246. *
  14247. * @param {Array} promisesOrValues array of anything, may contain a mix
  14248. * of promises and values
  14249. * @param howMany {number} number of promisesOrValues to resolve
  14250. * @param {function?} [onFulfilled] resolution handler
  14251. * @param {function?} [onRejected] rejection handler
  14252. * @param {function?} [onProgress] progress handler
  14253. * @returns {Promise} promise that will resolve to an array of howMany values that
  14254. * resolved first, or will reject with an array of (promisesOrValues.length - howMany) + 1
  14255. * rejection reasons.
  14256. */
  14257. function some(promisesOrValues, howMany, onFulfilled, onRejected, onProgress) {
  14258. checkCallbacks(2, arguments);
  14259. return when(promisesOrValues, function(promisesOrValues) {
  14260. var toResolve, toReject, values, reasons, deferred, fulfillOne, rejectOne, progress, len, i;
  14261. len = promisesOrValues.length >>> 0;
  14262. toResolve = Math.max(0, Math.min(howMany, len));
  14263. values = [];
  14264. toReject = (len - toResolve) + 1;
  14265. reasons = [];
  14266. deferred = defer();
  14267. // No items in the input, resolve immediately
  14268. if (!toResolve) {
  14269. deferred.resolve(values);
  14270. } else {
  14271. progress = deferred.progress;
  14272. rejectOne = function(reason) {
  14273. reasons.push(reason);
  14274. if(!--toReject) {
  14275. fulfillOne = rejectOne = noop;
  14276. deferred.reject(reasons);
  14277. }
  14278. };
  14279. fulfillOne = function(val) {
  14280. // This orders the values based on promise resolution order
  14281. // Another strategy would be to use the original position of
  14282. // the corresponding promise.
  14283. values.push(val);
  14284. if (!--toResolve) {
  14285. fulfillOne = rejectOne = noop;
  14286. deferred.resolve(values);
  14287. }
  14288. };
  14289. for(i = 0; i < len; ++i) {
  14290. if(i in promisesOrValues) {
  14291. when(promisesOrValues[i], fulfiller, rejecter, progress);
  14292. }
  14293. }
  14294. }
  14295. return deferred.then(onFulfilled, onRejected, onProgress);
  14296. function rejecter(reason) {
  14297. rejectOne(reason);
  14298. }
  14299. function fulfiller(val) {
  14300. fulfillOne(val);
  14301. }
  14302. });
  14303. }
  14304. /**
  14305. * Initiates a competitive race, returning a promise that will resolve when
  14306. * any one of the supplied promisesOrValues has resolved or will reject when
  14307. * *all* promisesOrValues have rejected.
  14308. *
  14309. * @param {Array|Promise} promisesOrValues array of anything, may contain a mix
  14310. * of {@link Promise}s and values
  14311. * @param {function?} [onFulfilled] resolution handler
  14312. * @param {function?} [onRejected] rejection handler
  14313. * @param {function?} [onProgress] progress handler
  14314. * @returns {Promise} promise that will resolve to the value that resolved first, or
  14315. * will reject with an array of all rejected inputs.
  14316. */
  14317. function any(promisesOrValues, onFulfilled, onRejected, onProgress) {
  14318. function unwrapSingleResult(val) {
  14319. return onFulfilled ? onFulfilled(val[0]) : val[0];
  14320. }
  14321. return some(promisesOrValues, 1, unwrapSingleResult, onRejected, onProgress);
  14322. }
  14323. /**
  14324. * Return a promise that will resolve only once all the supplied promisesOrValues
  14325. * have resolved. The resolution value of the returned promise will be an array
  14326. * containing the resolution values of each of the promisesOrValues.
  14327. * @memberOf when
  14328. *
  14329. * @param {Array|Promise} promisesOrValues array of anything, may contain a mix
  14330. * of {@link Promise}s and values
  14331. * @param {function?} [onFulfilled] resolution handler
  14332. * @param {function?} [onRejected] rejection handler
  14333. * @param {function?} [onProgress] progress handler
  14334. * @returns {Promise}
  14335. */
  14336. function all(promisesOrValues, onFulfilled, onRejected, onProgress) {
  14337. checkCallbacks(1, arguments);
  14338. return map(promisesOrValues, identity).then(onFulfilled, onRejected, onProgress);
  14339. }
  14340. /**
  14341. * Joins multiple promises into a single returned promise.
  14342. * @returns {Promise} a promise that will fulfill when *all* the input promises
  14343. * have fulfilled, or will reject when *any one* of the input promises rejects.
  14344. */
  14345. function join(/* ...promises */) {
  14346. return map(arguments, identity);
  14347. }
  14348. /**
  14349. * Traditional map function, similar to `Array.prototype.map()`, but allows
  14350. * input to contain {@link Promise}s and/or values, and mapFunc may return
  14351. * either a value or a {@link Promise}
  14352. *
  14353. * @param {Array|Promise} promise array of anything, may contain a mix
  14354. * of {@link Promise}s and values
  14355. * @param {function} mapFunc mapping function mapFunc(value) which may return
  14356. * either a {@link Promise} or value
  14357. * @returns {Promise} a {@link Promise} that will resolve to an array containing
  14358. * the mapped output values.
  14359. */
  14360. function map(promise, mapFunc) {
  14361. return when(promise, function(array) {
  14362. var results, len, toResolve, resolve, i, d;
  14363. // Since we know the resulting length, we can preallocate the results
  14364. // array to avoid array expansions.
  14365. toResolve = len = array.length >>> 0;
  14366. results = [];
  14367. d = defer();
  14368. if(!toResolve) {
  14369. d.resolve(results);
  14370. } else {
  14371. resolve = function resolveOne(item, i) {
  14372. when(item, mapFunc).then(function(mapped) {
  14373. results[i] = mapped;
  14374. if(!--toResolve) {
  14375. d.resolve(results);
  14376. }
  14377. }, d.reject);
  14378. };
  14379. // Since mapFunc may be async, get all invocations of it into flight
  14380. for(i = 0; i < len; i++) {
  14381. if(i in array) {
  14382. resolve(array[i], i);
  14383. } else {
  14384. --toResolve;
  14385. }
  14386. }
  14387. }
  14388. return d.promise;
  14389. });
  14390. }
  14391. /**
  14392. * Traditional reduce function, similar to `Array.prototype.reduce()`, but
  14393. * input may contain promises and/or values, and reduceFunc
  14394. * may return either a value or a promise, *and* initialValue may
  14395. * be a promise for the starting value.
  14396. *
  14397. * @param {Array|Promise} promise array or promise for an array of anything,
  14398. * may contain a mix of promises and values.
  14399. * @param {function} reduceFunc reduce function reduce(currentValue, nextValue, index, total),
  14400. * where total is the total number of items being reduced, and will be the same
  14401. * in each call to reduceFunc.
  14402. * @returns {Promise} that will resolve to the final reduced value
  14403. */
  14404. function reduce(promise, reduceFunc /*, initialValue */) {
  14405. var args = slice.call(arguments, 1);
  14406. return when(promise, function(array) {
  14407. var total;
  14408. total = array.length;
  14409. // Wrap the supplied reduceFunc with one that handles promises and then
  14410. // delegates to the supplied.
  14411. args[0] = function (current, val, i) {
  14412. return when(current, function (c) {
  14413. return when(val, function (value) {
  14414. return reduceFunc(c, value, i, total);
  14415. });
  14416. });
  14417. };
  14418. return reduceArray.apply(array, args);
  14419. });
  14420. }
  14421. /**
  14422. * Ensure that resolution of promiseOrValue will trigger resolver with the
  14423. * value or reason of promiseOrValue, or instead with resolveValue if it is provided.
  14424. *
  14425. * @param promiseOrValue
  14426. * @param {Object} resolver
  14427. * @param {function} resolver.resolve
  14428. * @param {function} resolver.reject
  14429. * @param {*} [resolveValue]
  14430. * @returns {Promise}
  14431. */
  14432. function chain(promiseOrValue, resolver, resolveValue) {
  14433. var useResolveValue = arguments.length > 2;
  14434. return when(promiseOrValue,
  14435. function(val) {
  14436. val = useResolveValue ? resolveValue : val;
  14437. resolver.resolve(val);
  14438. return val;
  14439. },
  14440. function(reason) {
  14441. resolver.reject(reason);
  14442. return rejected(reason);
  14443. },
  14444. resolver.progress
  14445. );
  14446. }
  14447. //
  14448. // Utility functions
  14449. //
  14450. /**
  14451. * Apply all functions in queue to value
  14452. * @param {Array} queue array of functions to execute
  14453. * @param {*} value argument passed to each function
  14454. */
  14455. function processQueue(queue, value) {
  14456. var handler, i = 0;
  14457. while (handler = queue[i++]) {
  14458. handler(value);
  14459. }
  14460. }
  14461. /**
  14462. * Helper that checks arrayOfCallbacks to ensure that each element is either
  14463. * a function, or null or undefined.
  14464. * @private
  14465. * @param {number} start index at which to start checking items in arrayOfCallbacks
  14466. * @param {Array} arrayOfCallbacks array to check
  14467. * @throws {Error} if any element of arrayOfCallbacks is something other than
  14468. * a functions, null, or undefined.
  14469. */
  14470. function checkCallbacks(start, arrayOfCallbacks) {
  14471. // TODO: Promises/A+ update type checking and docs
  14472. var arg, i = arrayOfCallbacks.length;
  14473. while(i > start) {
  14474. arg = arrayOfCallbacks[--i];
  14475. if (arg != null && typeof arg != 'function') {
  14476. throw new Error('arg '+i+' must be a function');
  14477. }
  14478. }
  14479. }
  14480. /**
  14481. * No-Op function used in method replacement
  14482. * @private
  14483. */
  14484. function noop() {}
  14485. slice = [].slice;
  14486. // ES5 reduce implementation if native not available
  14487. // See: http://es5.github.com/#x15.4.4.21 as there are many
  14488. // specifics and edge cases.
  14489. reduceArray = [].reduce ||
  14490. function(reduceFunc /*, initialValue */) {
  14491. /*jshint maxcomplexity: 7*/
  14492. // ES5 dictates that reduce.length === 1
  14493. // This implementation deviates from ES5 spec in the following ways:
  14494. // 1. It does not check if reduceFunc is a Callable
  14495. var arr, args, reduced, len, i;
  14496. i = 0;
  14497. // This generates a jshint warning, despite being valid
  14498. // "Missing 'new' prefix when invoking a constructor."
  14499. // See https://github.com/jshint/jshint/issues/392
  14500. arr = Object(this);
  14501. len = arr.length >>> 0;
  14502. args = arguments;
  14503. // If no initialValue, use first item of array (we know length !== 0 here)
  14504. // and adjust i to start at second item
  14505. if(args.length <= 1) {
  14506. // Skip to the first real element in the array
  14507. for(;;) {
  14508. if(i in arr) {
  14509. reduced = arr[i++];
  14510. break;
  14511. }
  14512. // If we reached the end of the array without finding any real
  14513. // elements, it's a TypeError
  14514. if(++i >= len) {
  14515. throw new TypeError();
  14516. }
  14517. }
  14518. } else {
  14519. // If initialValue provided, use it
  14520. reduced = args[1];
  14521. }
  14522. // Do the actual reduce
  14523. for(;i < len; ++i) {
  14524. // Skip holes
  14525. if(i in arr) {
  14526. reduced = reduceFunc(reduced, arr[i], i, arr);
  14527. }
  14528. }
  14529. return reduced;
  14530. };
  14531. function identity(x) {
  14532. return x;
  14533. }
  14534. return when;
  14535. });
  14536. })(typeof define == 'function' && define.amd
  14537. ? define
  14538. : function (factory) { typeof exports === 'object'
  14539. ? (module.exports = factory())
  14540. : (this.when = factory());
  14541. }
  14542. // Boilerplate for AMD, Node, and browser global
  14543. );
  14544. /*global define*/
  14545. define('Core/oneTimeWarning',[
  14546. './defaultValue',
  14547. './defined',
  14548. './DeveloperError'
  14549. ], function(
  14550. defaultValue,
  14551. defined,
  14552. DeveloperError) {
  14553. "use strict";
  14554. var warnings = {};
  14555. /**
  14556. * Logs a one time message to the console. Use this function instead of
  14557. * <code>console.log</code> directly since this does not log duplicate messages
  14558. * unless it is called from multiple workers.
  14559. *
  14560. * @exports oneTimeWarning
  14561. *
  14562. * @param {String} identifier The unique identifier for this warning.
  14563. * @param {String} [message=identifier] The message to log to the console.
  14564. *
  14565. * @example
  14566. * for(var i=0;i<foo.length;++i) {
  14567. * if (!defined(foo[i].bar)) {
  14568. * // Something that can be recovered from but may happen a lot
  14569. * oneTimeWarning('foo.bar undefined', 'foo.bar is undefined. Setting to 0.');
  14570. * foo[i].bar = 0;
  14571. * // ...
  14572. * }
  14573. * }
  14574. *
  14575. * @private
  14576. */
  14577. function oneTimeWarning(identifier, message) {
  14578. if (!defined(identifier)) {
  14579. throw new DeveloperError('identifier is required.');
  14580. }
  14581. if (!defined(warnings[identifier])) {
  14582. warnings[identifier] = true;
  14583. console.log(defaultValue(message, identifier));
  14584. }
  14585. }
  14586. oneTimeWarning.geometryOutlines = 'Entity geometry outlines are unsupported on terrain. Outlines will be disabled. To enable outlines, disable geometry terrain clamping by explicitly setting height to 0.';
  14587. return oneTimeWarning;
  14588. });
  14589. /*global define*/
  14590. define('Core/deprecationWarning',[
  14591. './defined',
  14592. './DeveloperError',
  14593. './oneTimeWarning'
  14594. ], function(
  14595. defined,
  14596. DeveloperError,
  14597. oneTimeWarning) {
  14598. 'use strict';
  14599. /**
  14600. * Logs a deprecation message to the console. Use this function instead of
  14601. * <code>console.log</code> directly since this does not log duplicate messages
  14602. * unless it is called from multiple workers.
  14603. *
  14604. * @exports deprecationWarning
  14605. *
  14606. * @param {String} identifier The unique identifier for this deprecated API.
  14607. * @param {String} message The message to log to the console.
  14608. *
  14609. * @example
  14610. * // Deprecated function or class
  14611. * function Foo() {
  14612. * deprecationWarning('Foo', 'Foo was deprecated in Cesium 1.01. It will be removed in 1.03. Use newFoo instead.');
  14613. * // ...
  14614. * }
  14615. *
  14616. * // Deprecated function
  14617. * Bar.prototype.func = function() {
  14618. * deprecationWarning('Bar.func', 'Bar.func() was deprecated in Cesium 1.01. It will be removed in 1.03. Use Bar.newFunc() instead.');
  14619. * // ...
  14620. * };
  14621. *
  14622. * // Deprecated property
  14623. * defineProperties(Bar.prototype, {
  14624. * prop : {
  14625. * get : function() {
  14626. * deprecationWarning('Bar.prop', 'Bar.prop was deprecated in Cesium 1.01. It will be removed in 1.03. Use Bar.newProp instead.');
  14627. * // ...
  14628. * },
  14629. * set : function(value) {
  14630. * deprecationWarning('Bar.prop', 'Bar.prop was deprecated in Cesium 1.01. It will be removed in 1.03. Use Bar.newProp instead.');
  14631. * // ...
  14632. * }
  14633. * }
  14634. * });
  14635. *
  14636. * @private
  14637. */
  14638. function deprecationWarning(identifier, message) {
  14639. if (!defined(identifier) || !defined(message)) {
  14640. throw new DeveloperError('identifier and message are required.');
  14641. }
  14642. oneTimeWarning(identifier, message);
  14643. }
  14644. return deprecationWarning;
  14645. });
  14646. /*global define*/
  14647. define('Core/binarySearch',[
  14648. './defined',
  14649. './DeveloperError'
  14650. ], function(
  14651. defined,
  14652. DeveloperError) {
  14653. 'use strict';
  14654. /**
  14655. * Finds an item in a sorted array.
  14656. *
  14657. * @exports binarySearch
  14658. *
  14659. * @param {Array} array The sorted array to search.
  14660. * @param {Object} itemToFind The item to find in the array.
  14661. * @param {binarySearch~Comparator} comparator The function to use to compare the item to
  14662. * elements in the array.
  14663. * @returns {Number} The index of <code>itemToFind</code> in the array, if it exists. If <code>itemToFind</code>
  14664. * does not exist, the return value is a negative number which is the bitwise complement (~)
  14665. * of the index before which the itemToFind should be inserted in order to maintain the
  14666. * sorted order of the array.
  14667. *
  14668. * @example
  14669. * // Create a comparator function to search through an array of numbers.
  14670. * function comparator(a, b) {
  14671. * return a - b;
  14672. * };
  14673. * var numbers = [0, 2, 4, 6, 8];
  14674. * var index = Cesium.binarySearch(numbers, 6, comparator); // 3
  14675. */
  14676. function binarySearch(array, itemToFind, comparator) {
  14677. if (!defined(array)) {
  14678. throw new DeveloperError('array is required.');
  14679. }
  14680. if (!defined(itemToFind)) {
  14681. throw new DeveloperError('itemToFind is required.');
  14682. }
  14683. if (!defined(comparator)) {
  14684. throw new DeveloperError('comparator is required.');
  14685. }
  14686. var low = 0;
  14687. var high = array.length - 1;
  14688. var i;
  14689. var comparison;
  14690. while (low <= high) {
  14691. i = ~~((low + high) / 2);
  14692. comparison = comparator(array[i], itemToFind);
  14693. if (comparison < 0) {
  14694. low = i + 1;
  14695. continue;
  14696. }
  14697. if (comparison > 0) {
  14698. high = i - 1;
  14699. continue;
  14700. }
  14701. return i;
  14702. }
  14703. return ~(high + 1);
  14704. }
  14705. /**
  14706. * A function used to compare two items while performing a binary search.
  14707. * @callback binarySearch~Comparator
  14708. *
  14709. * @param {Object} a An item in the array.
  14710. * @param {Object} b The item being searched for.
  14711. * @returns {Number} Returns a negative value if <code>a</code> is less than <code>b</code>,
  14712. * a positive value if <code>a</code> is greater than <code>b</code>, or
  14713. * 0 if <code>a</code> is equal to <code>b</code>.
  14714. *
  14715. * @example
  14716. * function compareNumbers(a, b) {
  14717. * return a - b;
  14718. * }
  14719. */
  14720. return binarySearch;
  14721. });
  14722. /*global define*/
  14723. define('Core/EarthOrientationParametersSample',[],function() {
  14724. 'use strict';
  14725. /**
  14726. * A set of Earth Orientation Parameters (EOP) sampled at a time.
  14727. *
  14728. * @alias EarthOrientationParametersSample
  14729. * @constructor
  14730. *
  14731. * @param {Number} xPoleWander The pole wander about the X axis, in radians.
  14732. * @param {Number} yPoleWander The pole wander about the Y axis, in radians.
  14733. * @param {Number} xPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  14734. * @param {Number} yPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  14735. * @param {Number} ut1MinusUtc The difference in time standards, UT1 - UTC, in seconds.
  14736. *
  14737. * @private
  14738. */
  14739. function EarthOrientationParametersSample(xPoleWander, yPoleWander, xPoleOffset, yPoleOffset, ut1MinusUtc) {
  14740. /**
  14741. * The pole wander about the X axis, in radians.
  14742. * @type {Number}
  14743. */
  14744. this.xPoleWander = xPoleWander;
  14745. /**
  14746. * The pole wander about the Y axis, in radians.
  14747. * @type {Number}
  14748. */
  14749. this.yPoleWander = yPoleWander;
  14750. /**
  14751. * The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  14752. * @type {Number}
  14753. */
  14754. this.xPoleOffset = xPoleOffset;
  14755. /**
  14756. * The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  14757. * @type {Number}
  14758. */
  14759. this.yPoleOffset = yPoleOffset;
  14760. /**
  14761. * The difference in time standards, UT1 - UTC, in seconds.
  14762. * @type {Number}
  14763. */
  14764. this.ut1MinusUtc = ut1MinusUtc;
  14765. }
  14766. return EarthOrientationParametersSample;
  14767. });
  14768. /**
  14769. @license
  14770. sprintf.js from the php.js project - https://github.com/kvz/phpjs
  14771. Directly from https://github.com/kvz/phpjs/blob/master/functions/strings/sprintf.js
  14772. php.js is copyright 2012 Kevin van Zonneveld.
  14773. Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
  14774. (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
  14775. (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jack, Jonas
  14776. Raoni Soares Silva (http://www.jsfromhell.com), Philip Peterson, Legaev
  14777. Andrey, Ates Goral (http://magnetiq.com), Alex, Ratheous, Martijn Wieringa,
  14778. Rafa? Kukawski (http://blog.kukawski.pl), lmeyrick
  14779. (https://sourceforge.net/projects/bcmath-js/), Nate, Philippe Baumann,
  14780. Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/), Carlos R.
  14781. L. Rodrigues (http://www.jsfromhell.com), Ash Searle
  14782. (http://hexmen.com/blog/), Jani Hartikainen, travc, Ole Vrijenhoek,
  14783. Erkekjetter, Michael Grier, Rafa? Kukawski (http://kukawski.pl), Johnny
  14784. Mast (http://www.phpvrouwen.nl), T.Wild, d3x,
  14785. http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
  14786. Rafa? Kukawski (http://blog.kukawski.pl/), stag019, pilus, WebDevHobo
  14787. (http://webdevhobo.blogspot.com/), marrtins, GeekFG
  14788. (http://geekfg.blogspot.com), Andrea Giammarchi
  14789. (http://webreflection.blogspot.com), Arpad Ray (mailto:arpad@php.net),
  14790. gorthaur, Paul Smith, Tim de Koning (http://www.kingsquare.nl), Joris, Oleg
  14791. Eremeev, Steve Hilder, majak, gettimeofday, KELAN, Josh Fraser
  14792. (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
  14793. Marc Palau, Martin
  14794. (http://www.erlenwiese.de/), Breaking Par Consulting Inc
  14795. (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
  14796. Chris, Mirek Slugen, saulius, Alfonso Jimenez
  14797. (http://www.alfonsojimenez.com), Diplom@t (http://difane.com/), felix,
  14798. Mailfaker (http://www.weedem.fr/), Tyler Akins (http://rumkin.com), Caio
  14799. Ariede (http://caioariede.com), Robin, Kankrelune
  14800. (http://www.webfaktory.info/), Karol Kowalski, Imgen Tata
  14801. (http://www.myipdf.com/), mdsjack (http://www.mdsjack.bo.it), Dreamer,
  14802. Felix Geisendoerfer (http://www.debuggable.com/felix), Lars Fischer, AJ,
  14803. David, Aman Gupta, Michael White, Public Domain
  14804. (http://www.json.org/json2.js), Steven Levithan
  14805. (http://blog.stevenlevithan.com), Sakimori, Pellentesque Malesuada,
  14806. Thunder.m, Dj (http://phpjs.org/functions/htmlentities:425#comment_134018),
  14807. Steve Clay, David James, Francois, class_exists, nobbler, T. Wild, Itsacon
  14808. (http://www.itsacon.net/), date, Ole Vrijenhoek (http://www.nervous.nl/),
  14809. Fox, Raphael (Ao RUDLER), Marco, noname, Mateusz "loonquawl" Zalega, Frank
  14810. Forte, Arno, ger, mktime, john (http://www.jd-tech.net), Nick Kolosov
  14811. (http://sammy.ru), marc andreu, Scott Cariss, Douglas Crockford
  14812. (http://javascript.crockford.com), madipta, Slawomir Kaniecki,
  14813. ReverseSyntax, Nathan, Alex Wilson, kenneth, Bayron Guevara, Adam Wallner
  14814. (http://web2.bitbaro.hu/), paulo kuong, jmweb, Lincoln Ramsay, djmix,
  14815. Pyerre, Jon Hohle, Thiago Mata (http://thiagomata.blog.com), lmeyrick
  14816. (https://sourceforge.net/projects/bcmath-js/this.), Linuxworld, duncan,
  14817. Gilbert, Sanjoy Roy, Shingo, sankai, Oskar Larsson H?gfeldt
  14818. (http://oskar-lh.name/), Denny Wardhana, 0m3r, Everlasto, Subhasis Deb,
  14819. josh, jd, Pier Paolo Ramon (http://www.mastersoup.com/), P, merabi, Soren
  14820. Hansen, Eugene Bulkin (http://doubleaw.com/), Der Simon
  14821. (http://innerdom.sourceforge.net/), echo is bad, Ozh, XoraX
  14822. (http://www.xorax.info), EdorFaus, JB, J A R, Marc Jansen, Francesco, LH,
  14823. Stoyan Kyosev (http://www.svest.org/), nord_ua, omid
  14824. (http://phpjs.org/functions/380:380#comment_137122), Brad Touesnard, MeEtc
  14825. (http://yass.meetcweb.com), Peter-Paul Koch
  14826. (http://www.quirksmode.org/js/beat.html), Olivier Louvignes
  14827. (http://mg-crea.com/), T0bsn, Tim Wiel, Bryan Elliott, Jalal Berrami,
  14828. Martin, JT, David Randall, Thomas Beaucourt (http://www.webapp.fr), taith,
  14829. vlado houba, Pierre-Luc Paour, Kristof Coomans (SCK-CEN Belgian Nucleair
  14830. Research Centre), Martin Pool, Kirk Strobeck, Rick Waldron, Brant Messenger
  14831. (http://www.brantmessenger.com/), Devan Penner-Woelk, Saulo Vallory, Wagner
  14832. B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
  14833. (http://carrot.org/), Christoph, Daniel Esteban, strftime, Mick@el, rezna,
  14834. Simon Willison (http://simonwillison.net), Anton Ongson, Gabriel Paderni,
  14835. Marco van Oort, penutbutterjelly, Philipp Lenssen, Bjorn Roesbeke
  14836. (http://www.bjornroesbeke.be/), Bug?, Eric Nagel, Tomasz Wesolowski,
  14837. Evertjan Garretsen, Bobby Drake, Blues (http://tech.bluesmoon.info/), Luke
  14838. Godfrey, Pul, uestla, Alan C, Ulrich, Rafal Kukawski, Yves Sucaet,
  14839. sowberry, Norman "zEh" Fuchs, hitwork, Zahlii, johnrembo, Nick Callen,
  14840. Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Brian Tafoya
  14841. (http://www.premasolutions.com/), Philippe Jausions
  14842. (http://pear.php.net/user/jausions), Aidan Lister
  14843. (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
  14844. strcmp, Taras Bogach, jpfle, Alexander Ermolaev
  14845. (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
  14846. dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
  14847. (http://www.pedrotainha.com), James, Arnout Kazemier
  14848. (http://www.3rd-Eden.com), Chris McMacken, gabriel paderni, Yannoo,
  14849. FGFEmperor, baris ozdil, Tod Gentille, Greg Frazier, jakes, 3D-GRAF, Allan
  14850. Jensen (http://www.winternet.no), Howard Yeend, Benjamin Lupton, davook,
  14851. daniel airton wermann (http://wermann.com.br), Atli T¨®r, Maximusya, Ryan
  14852. W Tenney (http://ryan.10e.us), Alexander M Beedie, fearphage
  14853. (http://http/my.opera.com/fearphage/), Nathan Sepulveda, Victor, Matteo,
  14854. Billy, stensi, Cord, Manish, T.J. Leahy, Riddler
  14855. (http://www.frontierwebdev.com/), Rafa? Kukawski, FremyCompany, Matt
  14856. Bradley, Tim de Koning, Luis Salazar (http://www.freaky-media.com/), Diogo
  14857. Resende, Rival, Andrej Pavlovic, Garagoth, Le Torbi
  14858. (http://www.letorbi.de/), Dino, Josep Sanz (http://www.ws3.es/), rem,
  14859. Russell Walker (http://www.nbill.co.uk/), Jamie Beck
  14860. (http://www.terabit.ca/), setcookie, Michael, YUI Library:
  14861. http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
  14862. http://hacks.bluesmoon.info/strftime/strftime.js, Ben
  14863. (http://benblume.co.uk/), DtTvB
  14864. (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Andreas, William,
  14865. meo, incidence, Cagri Ekin, Amirouche, Amir Habibi
  14866. (http://www.residence-mixte.com/), Luke Smith (http://lucassmith.name),
  14867. Kheang Hok Chin (http://www.distantia.ca/), Jay Klehr, Lorenzo Pisani,
  14868. Tony, Yen-Wei Liu, Greenseed, mk.keck, Leslie Hoare, dude, booeyOH, Ben
  14869. Bryan
  14870. Licensed under the MIT (MIT-LICENSE.txt) license.
  14871. Permission is hereby granted, free of charge, to any person obtaining a
  14872. copy of this software and associated documentation files (the
  14873. "Software"), to deal in the Software without restriction, including
  14874. without limitation the rights to use, copy, modify, merge, publish,
  14875. distribute, sublicense, and/or sell copies of the Software, and to
  14876. permit persons to whom the Software is furnished to do so, subject to
  14877. the following conditions:
  14878. The above copyright notice and this permission notice shall be included
  14879. in all copies or substantial portions of the Software.
  14880. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14881. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14882. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14883. IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
  14884. OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  14885. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  14886. OTHER DEALINGS IN THE SOFTWARE.
  14887. */
  14888. /*global define*/
  14889. define('ThirdParty/sprintf',[],function() {
  14890. function sprintf () {
  14891. // http://kevin.vanzonneveld.net
  14892. // + original by: Ash Searle (http://hexmen.com/blog/)
  14893. // + namespaced by: Michael White (http://getsprink.com)
  14894. // + tweaked by: Jack
  14895. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  14896. // + input by: Paulo Freitas
  14897. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  14898. // + input by: Brett Zamir (http://brett-zamir.me)
  14899. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  14900. // + improved by: Dj
  14901. // + improved by: Allidylls
  14902. // * example 1: sprintf("%01.2f", 123.1);
  14903. // * returns 1: 123.10
  14904. // * example 2: sprintf("[%10s]", 'monkey');
  14905. // * returns 2: '[ monkey]'
  14906. // * example 3: sprintf("[%'#10s]", 'monkey');
  14907. // * returns 3: '[####monkey]'
  14908. // * example 4: sprintf("%d", 123456789012345);
  14909. // * returns 4: '123456789012345'
  14910. var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuideEfFgG])/g;
  14911. var a = arguments,
  14912. i = 0,
  14913. format = a[i++];
  14914. // pad()
  14915. var pad = function (str, len, chr, leftJustify) {
  14916. if (!chr) {
  14917. chr = ' ';
  14918. }
  14919. var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
  14920. return leftJustify ? str + padding : padding + str;
  14921. };
  14922. // justify()
  14923. var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
  14924. var diff = minWidth - value.length;
  14925. if (diff > 0) {
  14926. if (leftJustify || !zeroPad) {
  14927. value = pad(value, minWidth, customPadChar, leftJustify);
  14928. } else {
  14929. value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
  14930. }
  14931. }
  14932. return value;
  14933. };
  14934. // formatBaseX()
  14935. var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
  14936. // Note: casts negative numbers to positive ones
  14937. var number = value >>> 0;
  14938. prefix = prefix && number && {
  14939. '2': '0b',
  14940. '8': '0',
  14941. '16': '0x'
  14942. }[base] || '';
  14943. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  14944. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  14945. };
  14946. // formatString()
  14947. var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
  14948. if (precision != null) {
  14949. value = value.slice(0, precision);
  14950. }
  14951. return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
  14952. };
  14953. // doFormat()
  14954. var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
  14955. var number;
  14956. var prefix;
  14957. var method;
  14958. var textTransform;
  14959. var value;
  14960. if (substring == '%%') {
  14961. return '%';
  14962. }
  14963. // parse flags
  14964. var leftJustify = false,
  14965. positivePrefix = '',
  14966. zeroPad = false,
  14967. prefixBaseX = false,
  14968. customPadChar = ' ';
  14969. var flagsl = flags.length;
  14970. for (var j = 0; flags && j < flagsl; j++) {
  14971. switch (flags.charAt(j)) {
  14972. case ' ':
  14973. positivePrefix = ' ';
  14974. break;
  14975. case '+':
  14976. positivePrefix = '+';
  14977. break;
  14978. case '-':
  14979. leftJustify = true;
  14980. break;
  14981. case "'":
  14982. customPadChar = flags.charAt(j + 1);
  14983. break;
  14984. case '0':
  14985. zeroPad = true;
  14986. break;
  14987. case '#':
  14988. prefixBaseX = true;
  14989. break;
  14990. }
  14991. }
  14992. // parameters may be null, undefined, empty-string or real valued
  14993. // we want to ignore null, undefined and empty-string values
  14994. if (!minWidth) {
  14995. minWidth = 0;
  14996. } else if (minWidth == '*') {
  14997. minWidth = +a[i++];
  14998. } else if (minWidth.charAt(0) == '*') {
  14999. minWidth = +a[minWidth.slice(1, -1)];
  15000. } else {
  15001. minWidth = +minWidth;
  15002. }
  15003. // Note: undocumented perl feature:
  15004. if (minWidth < 0) {
  15005. minWidth = -minWidth;
  15006. leftJustify = true;
  15007. }
  15008. if (!isFinite(minWidth)) {
  15009. throw new Error('sprintf: (minimum-)width must be finite');
  15010. }
  15011. if (!precision) {
  15012. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
  15013. } else if (precision == '*') {
  15014. precision = +a[i++];
  15015. } else if (precision.charAt(0) == '*') {
  15016. precision = +a[precision.slice(1, -1)];
  15017. } else {
  15018. precision = +precision;
  15019. }
  15020. // grab value using valueIndex if required?
  15021. value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  15022. switch (type) {
  15023. case 's':
  15024. return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
  15025. case 'c':
  15026. return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
  15027. case 'b':
  15028. return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15029. case 'o':
  15030. return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15031. case 'x':
  15032. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15033. case 'X':
  15034. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
  15035. case 'u':
  15036. return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15037. case 'i':
  15038. case 'd':
  15039. number = +value || 0;
  15040. number = Math.round(number - number % 1); // Plain Math.round doesn't just truncate
  15041. prefix = number < 0 ? '-' : positivePrefix;
  15042. value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  15043. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  15044. case 'e':
  15045. case 'E':
  15046. case 'f': // Should handle locales (as per setlocale)
  15047. case 'F':
  15048. case 'g':
  15049. case 'G':
  15050. number = +value;
  15051. prefix = number < 0 ? '-' : positivePrefix;
  15052. method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  15053. textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  15054. value = prefix + Math.abs(number)[method](precision);
  15055. return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
  15056. default:
  15057. return substring;
  15058. }
  15059. };
  15060. return format.replace(regex, doFormat);
  15061. }
  15062. return sprintf;
  15063. });
  15064. /*global define*/
  15065. define('Core/GregorianDate',[],function() {
  15066. 'use strict';
  15067. /**
  15068. * Represents a Gregorian date in a more precise format than the JavaScript Date object.
  15069. * In addition to submillisecond precision, this object can also represent leap seconds.
  15070. * @alias GregorianDate
  15071. * @constructor
  15072. *
  15073. * @see JulianDate#toGregorianDate
  15074. */
  15075. function GregorianDate(year, month, day, hour, minute, second, millisecond, isLeapSecond) {
  15076. /**
  15077. * Gets or sets the year as a whole number.
  15078. * @type {Number}
  15079. */
  15080. this.year = year;
  15081. /**
  15082. * Gets or sets the month as a whole number with range [1, 12].
  15083. * @type {Number}
  15084. */
  15085. this.month = month;
  15086. /**
  15087. * Gets or sets the day of the month as a whole number starting at 1.
  15088. * @type {Number}
  15089. */
  15090. this.day = day;
  15091. /**
  15092. * Gets or sets the hour as a whole number with range [0, 23].
  15093. * @type {Number}
  15094. */
  15095. this.hour = hour;
  15096. /**
  15097. * Gets or sets the minute of the hour as a whole number with range [0, 59].
  15098. * @type {Number}
  15099. */
  15100. this.minute = minute;
  15101. /**
  15102. * Gets or sets the second of the minute as a whole number with range [0, 60], with 60 representing a leap second.
  15103. * @type {Number}
  15104. */
  15105. this.second = second;
  15106. /**
  15107. * Gets or sets the millisecond of the second as a floating point number with range [0.0, 1000.0).
  15108. * @type {Number}
  15109. */
  15110. this.millisecond = millisecond;
  15111. /**
  15112. * Gets or sets whether this time is during a leap second.
  15113. * @type {Boolean}
  15114. */
  15115. this.isLeapSecond = isLeapSecond;
  15116. }
  15117. return GregorianDate;
  15118. });
  15119. /*global define*/
  15120. define('Core/isLeapYear',[
  15121. './DeveloperError'
  15122. ], function(
  15123. DeveloperError) {
  15124. 'use strict';
  15125. /**
  15126. * Determines if a given date is a leap year.
  15127. *
  15128. * @exports isLeapYear
  15129. *
  15130. * @param {Number} year The year to be tested.
  15131. * @returns {Boolean} True if <code>year</code> is a leap year.
  15132. *
  15133. * @example
  15134. * var leapYear = Cesium.isLeapYear(2000); // true
  15135. */
  15136. function isLeapYear(year) {
  15137. if (year === null || isNaN(year)) {
  15138. throw new DeveloperError('year is required and must be a number.');
  15139. }
  15140. return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
  15141. }
  15142. return isLeapYear;
  15143. });
  15144. /*global define*/
  15145. define('Core/LeapSecond',[],function() {
  15146. 'use strict';
  15147. /**
  15148. * Describes a single leap second, which is constructed from a {@link JulianDate} and a
  15149. * numerical offset representing the number of seconds TAI is ahead of the UTC time standard.
  15150. * @alias LeapSecond
  15151. * @constructor
  15152. *
  15153. * @param {JulianDate} [date] A Julian date representing the time of the leap second.
  15154. * @param {Number} [offset] The cumulative number of seconds that TAI is ahead of UTC at the provided date.
  15155. */
  15156. function LeapSecond(date, offset) {
  15157. /**
  15158. * Gets or sets the date at which this leap second occurs.
  15159. * @type {JulianDate}
  15160. */
  15161. this.julianDate = date;
  15162. /**
  15163. * Gets or sets the cumulative number of seconds between the UTC and TAI time standards at the time
  15164. * of this leap second.
  15165. * @type {Number}
  15166. */
  15167. this.offset = offset;
  15168. }
  15169. return LeapSecond;
  15170. });
  15171. /*global define*/
  15172. define('Core/TimeConstants',[
  15173. './freezeObject'
  15174. ], function(
  15175. freezeObject) {
  15176. 'use strict';
  15177. /**
  15178. * Constants for time conversions like those done by {@link JulianDate}.
  15179. *
  15180. * @exports TimeConstants
  15181. *
  15182. * @see JulianDate
  15183. *
  15184. * @private
  15185. */
  15186. var TimeConstants = {
  15187. /**
  15188. * The number of seconds in one millisecond: <code>0.001</code>
  15189. * @type {Number}
  15190. * @constant
  15191. */
  15192. SECONDS_PER_MILLISECOND : 0.001,
  15193. /**
  15194. * The number of seconds in one minute: <code>60</code>.
  15195. * @type {Number}
  15196. * @constant
  15197. */
  15198. SECONDS_PER_MINUTE : 60.0,
  15199. /**
  15200. * The number of minutes in one hour: <code>60</code>.
  15201. * @type {Number}
  15202. * @constant
  15203. */
  15204. MINUTES_PER_HOUR : 60.0,
  15205. /**
  15206. * The number of hours in one day: <code>24</code>.
  15207. * @type {Number}
  15208. * @constant
  15209. */
  15210. HOURS_PER_DAY : 24.0,
  15211. /**
  15212. * The number of seconds in one hour: <code>3600</code>.
  15213. * @type {Number}
  15214. * @constant
  15215. */
  15216. SECONDS_PER_HOUR : 3600.0,
  15217. /**
  15218. * The number of minutes in one day: <code>1440</code>.
  15219. * @type {Number}
  15220. * @constant
  15221. */
  15222. MINUTES_PER_DAY : 1440.0,
  15223. /**
  15224. * The number of seconds in one day, ignoring leap seconds: <code>86400</code>.
  15225. * @type {Number}
  15226. * @constant
  15227. */
  15228. SECONDS_PER_DAY : 86400.0,
  15229. /**
  15230. * The number of days in one Julian century: <code>36525</code>.
  15231. * @type {Number}
  15232. * @constant
  15233. */
  15234. DAYS_PER_JULIAN_CENTURY : 36525.0,
  15235. /**
  15236. * One trillionth of a second.
  15237. * @type {Number}
  15238. * @constant
  15239. */
  15240. PICOSECOND : 0.000000001,
  15241. /**
  15242. * The number of days to subtract from a Julian date to determine the
  15243. * modified Julian date, which gives the number of days since midnight
  15244. * on November 17, 1858.
  15245. * @type {Number}
  15246. * @constant
  15247. */
  15248. MODIFIED_JULIAN_DATE_DIFFERENCE : 2400000.5
  15249. };
  15250. return freezeObject(TimeConstants);
  15251. });
  15252. /*global define*/
  15253. define('Core/TimeStandard',[
  15254. './freezeObject'
  15255. ], function(
  15256. freezeObject) {
  15257. 'use strict';
  15258. /**
  15259. * Provides the type of time standards which JulianDate can take as input.
  15260. *
  15261. * @exports TimeStandard
  15262. *
  15263. * @see JulianDate
  15264. */
  15265. var TimeStandard = {
  15266. /**
  15267. * Represents the coordinated Universal Time (UTC) time standard.
  15268. *
  15269. * UTC is related to TAI according to the relationship
  15270. * <code>UTC = TAI - deltaT</code> where <code>deltaT</code> is the number of leap
  15271. * seconds which have been introduced as of the time in TAI.
  15272. *
  15273. */
  15274. UTC : 0,
  15275. /**
  15276. * Represents the International Atomic Time (TAI) time standard.
  15277. * TAI is the principal time standard to which the other time standards are related.
  15278. */
  15279. TAI : 1
  15280. };
  15281. return freezeObject(TimeStandard);
  15282. });
  15283. /*global define*/
  15284. define('Core/JulianDate',[
  15285. '../ThirdParty/sprintf',
  15286. './binarySearch',
  15287. './defaultValue',
  15288. './defined',
  15289. './DeveloperError',
  15290. './GregorianDate',
  15291. './isLeapYear',
  15292. './LeapSecond',
  15293. './TimeConstants',
  15294. './TimeStandard'
  15295. ], function(
  15296. sprintf,
  15297. binarySearch,
  15298. defaultValue,
  15299. defined,
  15300. DeveloperError,
  15301. GregorianDate,
  15302. isLeapYear,
  15303. LeapSecond,
  15304. TimeConstants,
  15305. TimeStandard) {
  15306. 'use strict';
  15307. var gregorianDateScratch = new GregorianDate();
  15308. var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  15309. var daysInLeapFeburary = 29;
  15310. function compareLeapSecondDates(leapSecond, dateToFind) {
  15311. return JulianDate.compare(leapSecond.julianDate, dateToFind.julianDate);
  15312. }
  15313. // we don't really need a leap second instance, anything with a julianDate property will do
  15314. var binarySearchScratchLeapSecond = new LeapSecond();
  15315. function convertUtcToTai(julianDate) {
  15316. //Even though julianDate is in UTC, we'll treat it as TAI and
  15317. //search the leap second table for it.
  15318. binarySearchScratchLeapSecond.julianDate = julianDate;
  15319. var leapSeconds = JulianDate.leapSeconds;
  15320. var index = binarySearch(leapSeconds, binarySearchScratchLeapSecond, compareLeapSecondDates);
  15321. if (index < 0) {
  15322. index = ~index;
  15323. }
  15324. if (index >= leapSeconds.length) {
  15325. index = leapSeconds.length - 1;
  15326. }
  15327. var offset = leapSeconds[index].offset;
  15328. if (index > 0) {
  15329. //Now we have the index of the closest leap second that comes on or after our UTC time.
  15330. //However, if the difference between the UTC date being converted and the TAI
  15331. //defined leap second is greater than the offset, we are off by one and need to use
  15332. //the previous leap second.
  15333. var difference = JulianDate.secondsDifference(leapSeconds[index].julianDate, julianDate);
  15334. if (difference > offset) {
  15335. index--;
  15336. offset = leapSeconds[index].offset;
  15337. }
  15338. }
  15339. JulianDate.addSeconds(julianDate, offset, julianDate);
  15340. }
  15341. function convertTaiToUtc(julianDate, result) {
  15342. binarySearchScratchLeapSecond.julianDate = julianDate;
  15343. var leapSeconds = JulianDate.leapSeconds;
  15344. var index = binarySearch(leapSeconds, binarySearchScratchLeapSecond, compareLeapSecondDates);
  15345. if (index < 0) {
  15346. index = ~index;
  15347. }
  15348. //All times before our first leap second get the first offset.
  15349. if (index === 0) {
  15350. return JulianDate.addSeconds(julianDate, -leapSeconds[0].offset, result);
  15351. }
  15352. //All times after our leap second get the last offset.
  15353. if (index >= leapSeconds.length) {
  15354. return JulianDate.addSeconds(julianDate, -leapSeconds[index - 1].offset, result);
  15355. }
  15356. //Compute the difference between the found leap second and the time we are converting.
  15357. var difference = JulianDate.secondsDifference(leapSeconds[index].julianDate, julianDate);
  15358. if (difference === 0) {
  15359. //The date is in our leap second table.
  15360. return JulianDate.addSeconds(julianDate, -leapSeconds[index].offset, result);
  15361. }
  15362. if (difference <= 1.0) {
  15363. //The requested date is during the moment of a leap second, then we cannot convert to UTC
  15364. return undefined;
  15365. }
  15366. //The time is in between two leap seconds, index is the leap second after the date
  15367. //we're converting, so we subtract one to get the correct LeapSecond instance.
  15368. return JulianDate.addSeconds(julianDate, -leapSeconds[--index].offset, result);
  15369. }
  15370. function setComponents(wholeDays, secondsOfDay, julianDate) {
  15371. var extraDays = (secondsOfDay / TimeConstants.SECONDS_PER_DAY) | 0;
  15372. wholeDays += extraDays;
  15373. secondsOfDay -= TimeConstants.SECONDS_PER_DAY * extraDays;
  15374. if (secondsOfDay < 0) {
  15375. wholeDays--;
  15376. secondsOfDay += TimeConstants.SECONDS_PER_DAY;
  15377. }
  15378. julianDate.dayNumber = wholeDays;
  15379. julianDate.secondsOfDay = secondsOfDay;
  15380. return julianDate;
  15381. }
  15382. function computeJulianDateComponents(year, month, day, hour, minute, second, millisecond) {
  15383. // Algorithm from page 604 of the Explanatory Supplement to the
  15384. // Astronomical Almanac (Seidelmann 1992).
  15385. var a = ((month - 14) / 12) | 0;
  15386. var b = year + 4800 + a;
  15387. var dayNumber = (((1461 * b) / 4) | 0) + (((367 * (month - 2 - 12 * a)) / 12) | 0) - (((3 * (((b + 100) / 100) | 0)) / 4) | 0) + day - 32075;
  15388. // JulianDates are noon-based
  15389. hour = hour - 12;
  15390. if (hour < 0) {
  15391. hour += 24;
  15392. }
  15393. var secondsOfDay = second + ((hour * TimeConstants.SECONDS_PER_HOUR) + (minute * TimeConstants.SECONDS_PER_MINUTE) + (millisecond * TimeConstants.SECONDS_PER_MILLISECOND));
  15394. if (secondsOfDay >= 43200.0) {
  15395. dayNumber -= 1;
  15396. }
  15397. return [dayNumber, secondsOfDay];
  15398. }
  15399. //Regular expressions used for ISO8601 date parsing.
  15400. //YYYY
  15401. var matchCalendarYear = /^(\d{4})$/;
  15402. //YYYY-MM (YYYYMM is invalid)
  15403. var matchCalendarMonth = /^(\d{4})-(\d{2})$/;
  15404. //YYYY-DDD or YYYYDDD
  15405. var matchOrdinalDate = /^(\d{4})-?(\d{3})$/;
  15406. //YYYY-Www or YYYYWww or YYYY-Www-D or YYYYWwwD
  15407. var matchWeekDate = /^(\d{4})-?W(\d{2})-?(\d{1})?$/;
  15408. //YYYY-MM-DD or YYYYMMDD
  15409. var matchCalendarDate = /^(\d{4})-?(\d{2})-?(\d{2})$/;
  15410. // Match utc offset
  15411. var utcOffset = /([Z+\-])?(\d{2})?:?(\d{2})?$/;
  15412. // Match hours HH or HH.xxxxx
  15413. var matchHours = /^(\d{2})(\.\d+)?/.source + utcOffset.source;
  15414. // Match hours/minutes HH:MM HHMM.xxxxx
  15415. var matchHoursMinutes = /^(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  15416. // Match hours/minutes HH:MM:SS HHMMSS.xxxxx
  15417. var matchHoursMinutesSeconds = /^(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  15418. var iso8601ErrorMessage = 'Invalid ISO 8601 date.';
  15419. /**
  15420. * Represents an astronomical Julian date, which is the number of days since noon on January 1, -4712 (4713 BC).
  15421. * For increased precision, this class stores the whole number part of the date and the seconds
  15422. * part of the date in separate components. In order to be safe for arithmetic and represent
  15423. * leap seconds, the date is always stored in the International Atomic Time standard
  15424. * {@link TimeStandard.TAI}.
  15425. * @alias JulianDate
  15426. * @constructor
  15427. *
  15428. * @param {Number} [julianDayNumber=0.0] The Julian Day Number representing the number of whole days. Fractional days will also be handled correctly.
  15429. * @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.
  15430. * @param {TimeStandard} [timeStandard=TimeStandard.UTC] The time standard in which the first two parameters are defined.
  15431. */
  15432. function JulianDate(julianDayNumber, secondsOfDay, timeStandard) {
  15433. /**
  15434. * Gets or sets the number of whole days.
  15435. * @type {Number}
  15436. */
  15437. this.dayNumber = undefined;
  15438. /**
  15439. * Gets or sets the number of seconds into the current day.
  15440. * @type {Number}
  15441. */
  15442. this.secondsOfDay = undefined;
  15443. julianDayNumber = defaultValue(julianDayNumber, 0.0);
  15444. secondsOfDay = defaultValue(secondsOfDay, 0.0);
  15445. timeStandard = defaultValue(timeStandard, TimeStandard.UTC);
  15446. //If julianDayNumber is fractional, make it an integer and add the number of seconds the fraction represented.
  15447. var wholeDays = julianDayNumber | 0;
  15448. secondsOfDay = secondsOfDay + (julianDayNumber - wholeDays) * TimeConstants.SECONDS_PER_DAY;
  15449. setComponents(wholeDays, secondsOfDay, this);
  15450. if (timeStandard === TimeStandard.UTC) {
  15451. convertUtcToTai(this);
  15452. }
  15453. }
  15454. /**
  15455. * Creates a new instance from a JavaScript Date.
  15456. *
  15457. * @param {Date} date A JavaScript Date.
  15458. * @param {JulianDate} [result] An existing instance to use for the result.
  15459. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  15460. *
  15461. * @exception {DeveloperError} date must be a valid JavaScript Date.
  15462. */
  15463. JulianDate.fromDate = function(date, result) {
  15464. if (!(date instanceof Date) || isNaN(date.getTime())) {
  15465. throw new DeveloperError('date must be a valid JavaScript Date.');
  15466. }
  15467. var components = computeJulianDateComponents(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
  15468. if (!defined(result)) {
  15469. return new JulianDate(components[0], components[1], TimeStandard.UTC);
  15470. }
  15471. setComponents(components[0], components[1], result);
  15472. convertUtcToTai(result);
  15473. return result;
  15474. };
  15475. /**
  15476. * Creates a new instance from a from an {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} date.
  15477. * This method is superior to <code>Date.parse</code> because it will handle all valid formats defined by the ISO 8601
  15478. * specification, including leap seconds and sub-millisecond times, which discarded by most JavaScript implementations.
  15479. *
  15480. * @param {String} iso8601String An ISO 8601 date.
  15481. * @param {JulianDate} [result] An existing instance to use for the result.
  15482. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  15483. *
  15484. * @exception {DeveloperError} Invalid ISO 8601 date.
  15485. */
  15486. JulianDate.fromIso8601 = function(iso8601String, result) {
  15487. if (typeof iso8601String !== 'string') {
  15488. throw new DeveloperError(iso8601ErrorMessage);
  15489. }
  15490. //Comma and decimal point both indicate a fractional number according to ISO 8601,
  15491. //start out by blanket replacing , with . which is the only valid such symbol in JS.
  15492. iso8601String = iso8601String.replace(',', '.');
  15493. //Split the string into its date and time components, denoted by a mandatory T
  15494. var tokens = iso8601String.split('T');
  15495. var year;
  15496. var month = 1;
  15497. var day = 1;
  15498. var hour = 0;
  15499. var minute = 0;
  15500. var second = 0;
  15501. var millisecond = 0;
  15502. //Lacking a time is okay, but a missing date is illegal.
  15503. var date = tokens[0];
  15504. var time = tokens[1];
  15505. var tmp;
  15506. var inLeapYear;
  15507. if (!defined(date)) {
  15508. throw new DeveloperError(iso8601ErrorMessage);
  15509. }
  15510. var dashCount;
  15511. //First match the date against possible regular expressions.
  15512. tokens = date.match(matchCalendarDate);
  15513. if (tokens !== null) {
  15514. dashCount = date.split('-').length - 1;
  15515. if (dashCount > 0 && dashCount !== 2) {
  15516. throw new DeveloperError(iso8601ErrorMessage);
  15517. }
  15518. year = +tokens[1];
  15519. month = +tokens[2];
  15520. day = +tokens[3];
  15521. } else {
  15522. tokens = date.match(matchCalendarMonth);
  15523. if (tokens !== null) {
  15524. year = +tokens[1];
  15525. month = +tokens[2];
  15526. } else {
  15527. tokens = date.match(matchCalendarYear);
  15528. if (tokens !== null) {
  15529. year = +tokens[1];
  15530. } else {
  15531. //Not a year/month/day so it must be an ordinal date.
  15532. var dayOfYear;
  15533. tokens = date.match(matchOrdinalDate);
  15534. if (tokens !== null) {
  15535. year = +tokens[1];
  15536. dayOfYear = +tokens[2];
  15537. inLeapYear = isLeapYear(year);
  15538. //This validation is only applicable for this format.
  15539. if (dayOfYear < 1 || (inLeapYear && dayOfYear > 366) || (!inLeapYear && dayOfYear > 365)) {
  15540. throw new DeveloperError(iso8601ErrorMessage);
  15541. }
  15542. } else {
  15543. tokens = date.match(matchWeekDate);
  15544. if (tokens !== null) {
  15545. //ISO week date to ordinal date from
  15546. //http://en.wikipedia.org/w/index.php?title=ISO_week_date&oldid=474176775
  15547. year = +tokens[1];
  15548. var weekNumber = +tokens[2];
  15549. var dayOfWeek = +tokens[3] || 0;
  15550. dashCount = date.split('-').length - 1;
  15551. if (dashCount > 0 &&
  15552. ((!defined(tokens[3]) && dashCount !== 1) ||
  15553. (defined(tokens[3]) && dashCount !== 2))) {
  15554. throw new DeveloperError(iso8601ErrorMessage);
  15555. }
  15556. var january4 = new Date(Date.UTC(year, 0, 4));
  15557. dayOfYear = (weekNumber * 7) + dayOfWeek - january4.getUTCDay() - 3;
  15558. } else {
  15559. //None of our regular expressions succeeded in parsing the date properly.
  15560. throw new DeveloperError(iso8601ErrorMessage);
  15561. }
  15562. }
  15563. //Split an ordinal date into month/day.
  15564. tmp = new Date(Date.UTC(year, 0, 1));
  15565. tmp.setUTCDate(dayOfYear);
  15566. month = tmp.getUTCMonth() + 1;
  15567. day = tmp.getUTCDate();
  15568. }
  15569. }
  15570. }
  15571. //Now that we have all of the date components, validate them to make sure nothing is out of range.
  15572. inLeapYear = isLeapYear(year);
  15573. if (month < 1 || month > 12 || day < 1 || ((month !== 2 || !inLeapYear) && day > daysInMonth[month - 1]) || (inLeapYear && month === 2 && day > daysInLeapFeburary)) {
  15574. throw new DeveloperError(iso8601ErrorMessage);
  15575. }
  15576. //Not move onto the time string, which is much simpler.
  15577. var offsetIndex;
  15578. if (defined(time)) {
  15579. tokens = time.match(matchHoursMinutesSeconds);
  15580. if (tokens !== null) {
  15581. dashCount = time.split(':').length - 1;
  15582. if (dashCount > 0 && dashCount !== 2 && dashCount !== 3) {
  15583. throw new DeveloperError(iso8601ErrorMessage);
  15584. }
  15585. hour = +tokens[1];
  15586. minute = +tokens[2];
  15587. second = +tokens[3];
  15588. millisecond = +(tokens[4] || 0) * 1000.0;
  15589. offsetIndex = 5;
  15590. } else {
  15591. tokens = time.match(matchHoursMinutes);
  15592. if (tokens !== null) {
  15593. dashCount = time.split(':').length - 1;
  15594. if (dashCount > 2) {
  15595. throw new DeveloperError(iso8601ErrorMessage);
  15596. }
  15597. hour = +tokens[1];
  15598. minute = +tokens[2];
  15599. second = +(tokens[3] || 0) * 60.0;
  15600. offsetIndex = 4;
  15601. } else {
  15602. tokens = time.match(matchHours);
  15603. if (tokens !== null) {
  15604. hour = +tokens[1];
  15605. minute = +(tokens[2] || 0) * 60.0;
  15606. offsetIndex = 3;
  15607. } else {
  15608. throw new DeveloperError(iso8601ErrorMessage);
  15609. }
  15610. }
  15611. }
  15612. //Validate that all values are in proper range. Minutes and hours have special cases at 60 and 24.
  15613. if (minute >= 60 || second >= 61 || hour > 24 || (hour === 24 && (minute > 0 || second > 0 || millisecond > 0))) {
  15614. throw new DeveloperError(iso8601ErrorMessage);
  15615. }
  15616. //Check the UTC offset value, if no value exists, use local time
  15617. //a Z indicates UTC, + or - are offsets.
  15618. var offset = tokens[offsetIndex];
  15619. var offsetHours = +(tokens[offsetIndex + 1]);
  15620. var offsetMinutes = +(tokens[offsetIndex + 2] || 0);
  15621. switch (offset) {
  15622. case '+':
  15623. hour = hour - offsetHours;
  15624. minute = minute - offsetMinutes;
  15625. break;
  15626. case '-':
  15627. hour = hour + offsetHours;
  15628. minute = minute + offsetMinutes;
  15629. break;
  15630. case 'Z':
  15631. break;
  15632. default:
  15633. minute = minute + new Date(Date.UTC(year, month - 1, day, hour, minute)).getTimezoneOffset();
  15634. break;
  15635. }
  15636. } else {
  15637. //If no time is specified, it is considered the beginning of the day, local time.
  15638. minute = minute + new Date(year, month - 1, day).getTimezoneOffset();
  15639. }
  15640. //ISO8601 denotes a leap second by any time having a seconds component of 60 seconds.
  15641. //If that's the case, we need to temporarily subtract a second in order to build a UTC date.
  15642. //Then we add it back in after converting to TAI.
  15643. var isLeapSecond = second === 60;
  15644. if (isLeapSecond) {
  15645. second--;
  15646. }
  15647. //Even if we successfully parsed the string into its components, after applying UTC offset or
  15648. //special cases like 24:00:00 denoting midnight, we need to normalize the data appropriately.
  15649. //milliseconds can never be greater than 1000, and seconds can't be above 60, so we start with minutes
  15650. while (minute >= 60) {
  15651. minute -= 60;
  15652. hour++;
  15653. }
  15654. while (hour >= 24) {
  15655. hour -= 24;
  15656. day++;
  15657. }
  15658. tmp = (inLeapYear && month === 2) ? daysInLeapFeburary : daysInMonth[month - 1];
  15659. while (day > tmp) {
  15660. day -= tmp;
  15661. month++;
  15662. if (month > 12) {
  15663. month -= 12;
  15664. year++;
  15665. }
  15666. tmp = (inLeapYear && month === 2) ? daysInLeapFeburary : daysInMonth[month - 1];
  15667. }
  15668. //If UTC offset is at the beginning/end of the day, minutes can be negative.
  15669. while (minute < 0) {
  15670. minute += 60;
  15671. hour--;
  15672. }
  15673. while (hour < 0) {
  15674. hour += 24;
  15675. day--;
  15676. }
  15677. while (day < 1) {
  15678. month--;
  15679. if (month < 1) {
  15680. month += 12;
  15681. year--;
  15682. }
  15683. tmp = (inLeapYear && month === 2) ? daysInLeapFeburary : daysInMonth[month - 1];
  15684. day += tmp;
  15685. }
  15686. //Now create the JulianDate components from the Gregorian date and actually create our instance.
  15687. var components = computeJulianDateComponents(year, month, day, hour, minute, second, millisecond);
  15688. if (!defined(result)) {
  15689. result = new JulianDate(components[0], components[1], TimeStandard.UTC);
  15690. } else {
  15691. setComponents(components[0], components[1], result);
  15692. convertUtcToTai(result);
  15693. }
  15694. //If we were on a leap second, add it back.
  15695. if (isLeapSecond) {
  15696. JulianDate.addSeconds(result, 1, result);
  15697. }
  15698. return result;
  15699. };
  15700. /**
  15701. * Creates a new instance that represents the current system time.
  15702. * This is equivalent to calling <code>JulianDate.fromDate(new Date());</code>.
  15703. *
  15704. * @param {JulianDate} [result] An existing instance to use for the result.
  15705. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  15706. */
  15707. JulianDate.now = function(result) {
  15708. return JulianDate.fromDate(new Date(), result);
  15709. };
  15710. var toGregorianDateScratch = new JulianDate(0, 0, TimeStandard.TAI);
  15711. /**
  15712. * Creates a {@link GregorianDate} from the provided instance.
  15713. *
  15714. * @param {JulianDate} julianDate The date to be converted.
  15715. * @param {GregorianDate} [result] An existing instance to use for the result.
  15716. * @returns {GregorianDate} The modified result parameter or a new instance if none was provided.
  15717. */
  15718. JulianDate.toGregorianDate = function(julianDate, result) {
  15719. if (!defined(julianDate)) {
  15720. throw new DeveloperError('julianDate is required.');
  15721. }
  15722. var isLeapSecond = false;
  15723. var thisUtc = convertTaiToUtc(julianDate, toGregorianDateScratch);
  15724. if (!defined(thisUtc)) {
  15725. //Conversion to UTC will fail if we are during a leap second.
  15726. //If that's the case, subtract a second and convert again.
  15727. //JavaScript doesn't support leap seconds, so this results in second 59 being repeated twice.
  15728. JulianDate.addSeconds(julianDate, -1, toGregorianDateScratch);
  15729. thisUtc = convertTaiToUtc(toGregorianDateScratch, toGregorianDateScratch);
  15730. isLeapSecond = true;
  15731. }
  15732. var julianDayNumber = thisUtc.dayNumber;
  15733. var secondsOfDay = thisUtc.secondsOfDay;
  15734. if (secondsOfDay >= 43200.0) {
  15735. julianDayNumber += 1;
  15736. }
  15737. // Algorithm from page 604 of the Explanatory Supplement to the
  15738. // Astronomical Almanac (Seidelmann 1992).
  15739. var L = (julianDayNumber + 68569) | 0;
  15740. var N = (4 * L / 146097) | 0;
  15741. L = (L - (((146097 * N + 3) / 4) | 0)) | 0;
  15742. var I = ((4000 * (L + 1)) / 1461001) | 0;
  15743. L = (L - (((1461 * I) / 4) | 0) + 31) | 0;
  15744. var J = ((80 * L) / 2447) | 0;
  15745. var day = (L - (((2447 * J) / 80) | 0)) | 0;
  15746. L = (J / 11) | 0;
  15747. var month = (J + 2 - 12 * L) | 0;
  15748. var year = (100 * (N - 49) + I + L) | 0;
  15749. var hour = (secondsOfDay / TimeConstants.SECONDS_PER_HOUR) | 0;
  15750. var remainingSeconds = secondsOfDay - (hour * TimeConstants.SECONDS_PER_HOUR);
  15751. var minute = (remainingSeconds / TimeConstants.SECONDS_PER_MINUTE) | 0;
  15752. remainingSeconds = remainingSeconds - (minute * TimeConstants.SECONDS_PER_MINUTE);
  15753. var second = remainingSeconds | 0;
  15754. var millisecond = ((remainingSeconds - second) / TimeConstants.SECONDS_PER_MILLISECOND);
  15755. // JulianDates are noon-based
  15756. hour += 12;
  15757. if (hour > 23) {
  15758. hour -= 24;
  15759. }
  15760. //If we were on a leap second, add it back.
  15761. if (isLeapSecond) {
  15762. second += 1;
  15763. }
  15764. if (!defined(result)) {
  15765. return new GregorianDate(year, month, day, hour, minute, second, millisecond, isLeapSecond);
  15766. }
  15767. result.year = year;
  15768. result.month = month;
  15769. result.day = day;
  15770. result.hour = hour;
  15771. result.minute = minute;
  15772. result.second = second;
  15773. result.millisecond = millisecond;
  15774. result.isLeapSecond = isLeapSecond;
  15775. return result;
  15776. };
  15777. /**
  15778. * Creates a JavaScript Date from the provided instance.
  15779. * Since JavaScript dates are only accurate to the nearest millisecond and
  15780. * cannot represent a leap second, consider using {@link JulianDate.toGregorianDate} instead.
  15781. * If the provided JulianDate is during a leap second, the previous second is used.
  15782. *
  15783. * @param {JulianDate} julianDate The date to be converted.
  15784. * @returns {Date} A new instance representing the provided date.
  15785. */
  15786. JulianDate.toDate = function(julianDate) {
  15787. if (!defined(julianDate)) {
  15788. throw new DeveloperError('julianDate is required.');
  15789. }
  15790. var gDate = JulianDate.toGregorianDate(julianDate, gregorianDateScratch);
  15791. var second = gDate.second;
  15792. if (gDate.isLeapSecond) {
  15793. second -= 1;
  15794. }
  15795. return new Date(Date.UTC(gDate.year, gDate.month - 1, gDate.day, gDate.hour, gDate.minute, second, gDate.millisecond));
  15796. };
  15797. /**
  15798. * Creates an ISO8601 representation of the provided date.
  15799. *
  15800. * @param {JulianDate} julianDate The date to be converted.
  15801. * @param {Number} [precision] The number of fractional digits used to represent the seconds component. By default, the most precise representation is used.
  15802. * @returns {String} The ISO8601 representation of the provided date.
  15803. */
  15804. JulianDate.toIso8601 = function(julianDate, precision) {
  15805. if (!defined(julianDate)) {
  15806. throw new DeveloperError('julianDate is required.');
  15807. }
  15808. var gDate = JulianDate.toGregorianDate(julianDate, gDate);
  15809. var millisecondStr;
  15810. if (!defined(precision) && gDate.millisecond !== 0) {
  15811. //Forces milliseconds into a number with at least 3 digits to whatever the default toString() precision is.
  15812. millisecondStr = (gDate.millisecond * 0.01).toString().replace('.', '');
  15813. return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%sZ", gDate.year, gDate.month, gDate.day, gDate.hour, gDate.minute, gDate.second, millisecondStr);
  15814. }
  15815. //Precision is either 0 or milliseconds is 0 with undefined precision, in either case, leave off milliseconds entirely
  15816. if (!defined(precision) || precision === 0) {
  15817. return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", gDate.year, gDate.month, gDate.day, gDate.hour, gDate.minute, gDate.second);
  15818. }
  15819. //Forces milliseconds into a number with at least 3 digits to whatever the specified precision is.
  15820. millisecondStr = (gDate.millisecond * 0.01).toFixed(precision).replace('.', '').slice(0, precision);
  15821. return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%sZ", gDate.year, gDate.month, gDate.day, gDate.hour, gDate.minute, gDate.second, millisecondStr);
  15822. };
  15823. /**
  15824. * Duplicates a JulianDate instance.
  15825. *
  15826. * @param {JulianDate} julianDate The date to duplicate.
  15827. * @param {JulianDate} [result] An existing instance to use for the result.
  15828. * @returns {JulianDate} The modified result parameter or a new instance if none was provided. Returns undefined if julianDate is undefined.
  15829. */
  15830. JulianDate.clone = function(julianDate, result) {
  15831. if (!defined(julianDate)) {
  15832. return undefined;
  15833. }
  15834. if (!defined(result)) {
  15835. return new JulianDate(julianDate.dayNumber, julianDate.secondsOfDay, TimeStandard.TAI);
  15836. }
  15837. result.dayNumber = julianDate.dayNumber;
  15838. result.secondsOfDay = julianDate.secondsOfDay;
  15839. return result;
  15840. };
  15841. /**
  15842. * Compares two instances.
  15843. *
  15844. * @param {JulianDate} left The first instance.
  15845. * @param {JulianDate} right The second instance.
  15846. * @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.
  15847. */
  15848. JulianDate.compare = function(left, right) {
  15849. if (!defined(left)) {
  15850. throw new DeveloperError('left is required.');
  15851. }
  15852. if (!defined(right)) {
  15853. throw new DeveloperError('right is required.');
  15854. }
  15855. var julianDayNumberDifference = left.dayNumber - right.dayNumber;
  15856. if (julianDayNumberDifference !== 0) {
  15857. return julianDayNumberDifference;
  15858. }
  15859. return left.secondsOfDay - right.secondsOfDay;
  15860. };
  15861. /**
  15862. * Compares two instances and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  15863. *
  15864. * @param {JulianDate} [left] The first instance.
  15865. * @param {JulianDate} [right] The second instance.
  15866. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  15867. */
  15868. JulianDate.equals = function(left, right) {
  15869. return (left === right) ||
  15870. (defined(left) &&
  15871. defined(right) &&
  15872. left.dayNumber === right.dayNumber &&
  15873. left.secondsOfDay === right.secondsOfDay);
  15874. };
  15875. /**
  15876. * Compares two instances and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  15877. * each other. That is, in order for the dates to be considered equal (and for
  15878. * this function to return <code>true</code>), the absolute value of the difference between them, in
  15879. * seconds, must be less than <code>epsilon</code>.
  15880. *
  15881. * @param {JulianDate} [left] The first instance.
  15882. * @param {JulianDate} [right] The second instance.
  15883. * @param {Number} epsilon The maximum number of seconds that should separate the two instances.
  15884. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  15885. */
  15886. JulianDate.equalsEpsilon = function(left, right, epsilon) {
  15887. if (!defined(epsilon)) {
  15888. throw new DeveloperError('epsilon is required.');
  15889. }
  15890. return (left === right) ||
  15891. (defined(left) &&
  15892. defined(right) &&
  15893. Math.abs(JulianDate.secondsDifference(left, right)) <= epsilon);
  15894. };
  15895. /**
  15896. * Computes the total number of whole and fractional days represented by the provided instance.
  15897. *
  15898. * @param {JulianDate} julianDate The date.
  15899. * @returns {Number} The Julian date as single floating point number.
  15900. */
  15901. JulianDate.totalDays = function(julianDate) {
  15902. if (!defined(julianDate)) {
  15903. throw new DeveloperError('julianDate is required.');
  15904. }
  15905. return julianDate.dayNumber + (julianDate.secondsOfDay / TimeConstants.SECONDS_PER_DAY);
  15906. };
  15907. /**
  15908. * Computes the difference in seconds between the provided instance.
  15909. *
  15910. * @param {JulianDate} left The first instance.
  15911. * @param {JulianDate} right The second instance.
  15912. * @returns {Number} The difference, in seconds, when subtracting <code>right</code> from <code>left</code>.
  15913. */
  15914. JulianDate.secondsDifference = function(left, right) {
  15915. if (!defined(left)) {
  15916. throw new DeveloperError('left is required.');
  15917. }
  15918. if (!defined(right)) {
  15919. throw new DeveloperError('right is required.');
  15920. }
  15921. var dayDifference = (left.dayNumber - right.dayNumber) * TimeConstants.SECONDS_PER_DAY;
  15922. return (dayDifference + (left.secondsOfDay - right.secondsOfDay));
  15923. };
  15924. /**
  15925. * Computes the difference in days between the provided instance.
  15926. *
  15927. * @param {JulianDate} left The first instance.
  15928. * @param {JulianDate} right The second instance.
  15929. * @returns {Number} The difference, in days, when subtracting <code>right</code> from <code>left</code>.
  15930. */
  15931. JulianDate.daysDifference = function(left, right) {
  15932. if (!defined(left)) {
  15933. throw new DeveloperError('left is required.');
  15934. }
  15935. if (!defined(right)) {
  15936. throw new DeveloperError('right is required.');
  15937. }
  15938. var dayDifference = (left.dayNumber - right.dayNumber);
  15939. var secondDifference = (left.secondsOfDay - right.secondsOfDay) / TimeConstants.SECONDS_PER_DAY;
  15940. return dayDifference + secondDifference;
  15941. };
  15942. /**
  15943. * Computes the number of seconds the provided instance is ahead of UTC.
  15944. *
  15945. * @param {JulianDate} julianDate The date.
  15946. * @returns {Number} The number of seconds the provided instance is ahead of UTC
  15947. */
  15948. JulianDate.computeTaiMinusUtc = function(julianDate) {
  15949. binarySearchScratchLeapSecond.julianDate = julianDate;
  15950. var leapSeconds = JulianDate.leapSeconds;
  15951. var index = binarySearch(leapSeconds, binarySearchScratchLeapSecond, compareLeapSecondDates);
  15952. if (index < 0) {
  15953. index = ~index;
  15954. --index;
  15955. if (index < 0) {
  15956. index = 0;
  15957. }
  15958. }
  15959. return leapSeconds[index].offset;
  15960. };
  15961. /**
  15962. * Adds the provided number of seconds to the provided date instance.
  15963. *
  15964. * @param {JulianDate} julianDate The date.
  15965. * @param {Number} seconds The number of seconds to add or subtract.
  15966. * @param {JulianDate} result An existing instance to use for the result.
  15967. * @returns {JulianDate} The modified result parameter.
  15968. */
  15969. JulianDate.addSeconds = function(julianDate, seconds, result) {
  15970. if (!defined(julianDate)) {
  15971. throw new DeveloperError('julianDate is required.');
  15972. }
  15973. if (!defined(seconds)) {
  15974. throw new DeveloperError('seconds is required.');
  15975. }
  15976. if (!defined(result)) {
  15977. throw new DeveloperError('result is required.');
  15978. }
  15979. return setComponents(julianDate.dayNumber, julianDate.secondsOfDay + seconds, result);
  15980. };
  15981. /**
  15982. * Adds the provided number of minutes to the provided date instance.
  15983. *
  15984. * @param {JulianDate} julianDate The date.
  15985. * @param {Number} minutes The number of minutes to add or subtract.
  15986. * @param {JulianDate} result An existing instance to use for the result.
  15987. * @returns {JulianDate} The modified result parameter.
  15988. */
  15989. JulianDate.addMinutes = function(julianDate, minutes, result) {
  15990. if (!defined(julianDate)) {
  15991. throw new DeveloperError('julianDate is required.');
  15992. }
  15993. if (!defined(minutes)) {
  15994. throw new DeveloperError('minutes is required.');
  15995. }
  15996. if (!defined(result)) {
  15997. throw new DeveloperError('result is required.');
  15998. }
  15999. var newSecondsOfDay = julianDate.secondsOfDay + (minutes * TimeConstants.SECONDS_PER_MINUTE);
  16000. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  16001. };
  16002. /**
  16003. * Adds the provided number of hours to the provided date instance.
  16004. *
  16005. * @param {JulianDate} julianDate The date.
  16006. * @param {Number} hours The number of hours to add or subtract.
  16007. * @param {JulianDate} result An existing instance to use for the result.
  16008. * @returns {JulianDate} The modified result parameter.
  16009. */
  16010. JulianDate.addHours = function(julianDate, hours, result) {
  16011. if (!defined(julianDate)) {
  16012. throw new DeveloperError('julianDate is required.');
  16013. }
  16014. if (!defined(hours)) {
  16015. throw new DeveloperError('hours is required.');
  16016. }
  16017. if (!defined(result)) {
  16018. throw new DeveloperError('result is required.');
  16019. }
  16020. var newSecondsOfDay = julianDate.secondsOfDay + (hours * TimeConstants.SECONDS_PER_HOUR);
  16021. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  16022. };
  16023. /**
  16024. * Adds the provided number of days to the provided date instance.
  16025. *
  16026. * @param {JulianDate} julianDate The date.
  16027. * @param {Number} days The number of days to add or subtract.
  16028. * @param {JulianDate} result An existing instance to use for the result.
  16029. * @returns {JulianDate} The modified result parameter.
  16030. */
  16031. JulianDate.addDays = function(julianDate, days, result) {
  16032. if (!defined(julianDate)) {
  16033. throw new DeveloperError('julianDate is required.');
  16034. }
  16035. if (!defined(days)) {
  16036. throw new DeveloperError('days is required.');
  16037. }
  16038. if (!defined(result)) {
  16039. throw new DeveloperError('result is required.');
  16040. }
  16041. var newJulianDayNumber = julianDate.dayNumber + days;
  16042. return setComponents(newJulianDayNumber, julianDate.secondsOfDay, result);
  16043. };
  16044. /**
  16045. * Compares the provided instances and returns <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  16046. *
  16047. * @param {JulianDate} left The first instance.
  16048. * @param {JulianDate} right The second instance.
  16049. * @returns {Boolean} <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  16050. */
  16051. JulianDate.lessThan = function(left, right) {
  16052. return JulianDate.compare(left, right) < 0;
  16053. };
  16054. /**
  16055. * 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.
  16056. *
  16057. * @param {JulianDate} left The first instance.
  16058. * @param {JulianDate} right The second instance.
  16059. * @returns {Boolean} <code>true</code> if <code>left</code> is earlier than or equal to <code>right</code>, <code>false</code> otherwise.
  16060. */
  16061. JulianDate.lessThanOrEquals = function(left, right) {
  16062. return JulianDate.compare(left, right) <= 0;
  16063. };
  16064. /**
  16065. * Compares the provided instances and returns <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  16066. *
  16067. * @param {JulianDate} left The first instance.
  16068. * @param {JulianDate} right The second instance.
  16069. * @returns {Boolean} <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  16070. */
  16071. JulianDate.greaterThan = function(left, right) {
  16072. return JulianDate.compare(left, right) > 0;
  16073. };
  16074. /**
  16075. * 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.
  16076. *
  16077. * @param {JulianDate} left The first instance.
  16078. * @param {JulianDate} right The second instance.
  16079. * @returns {Boolean} <code>true</code> if <code>left</code> is later than or equal to <code>right</code>, <code>false</code> otherwise.
  16080. */
  16081. JulianDate.greaterThanOrEquals = function(left, right) {
  16082. return JulianDate.compare(left, right) >= 0;
  16083. };
  16084. /**
  16085. * Duplicates this instance.
  16086. *
  16087. * @param {JulianDate} [result] An existing instance to use for the result.
  16088. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  16089. */
  16090. JulianDate.prototype.clone = function(result) {
  16091. return JulianDate.clone(this, result);
  16092. };
  16093. /**
  16094. * Compares this and the provided instance and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  16095. *
  16096. * @param {JulianDate} [right] The second instance.
  16097. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  16098. */
  16099. JulianDate.prototype.equals = function(right) {
  16100. return JulianDate.equals(this, right);
  16101. };
  16102. /**
  16103. * Compares this and the provided instance and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  16104. * each other. That is, in order for the dates to be considered equal (and for
  16105. * this function to return <code>true</code>), the absolute value of the difference between them, in
  16106. * seconds, must be less than <code>epsilon</code>.
  16107. *
  16108. * @param {JulianDate} [right] The second instance.
  16109. * @param {Number} epsilon The maximum number of seconds that should separate the two instances.
  16110. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  16111. */
  16112. JulianDate.prototype.equalsEpsilon = function(right, epsilon) {
  16113. return JulianDate.equalsEpsilon(this, right, epsilon);
  16114. };
  16115. /**
  16116. * Creates a string representing this date in ISO8601 format.
  16117. *
  16118. * @returns {String} A string representing this date in ISO8601 format.
  16119. */
  16120. JulianDate.prototype.toString = function() {
  16121. return JulianDate.toIso8601(this);
  16122. };
  16123. /**
  16124. * Gets or sets the list of leap seconds used throughout Cesium.
  16125. * @memberof JulianDate
  16126. * @type {LeapSecond[]}
  16127. */
  16128. JulianDate.leapSeconds = [
  16129. new LeapSecond(new JulianDate(2441317, 43210.0, TimeStandard.TAI), 10), // January 1, 1972 00:00:00 UTC
  16130. new LeapSecond(new JulianDate(2441499, 43211.0, TimeStandard.TAI), 11), // July 1, 1972 00:00:00 UTC
  16131. new LeapSecond(new JulianDate(2441683, 43212.0, TimeStandard.TAI), 12), // January 1, 1973 00:00:00 UTC
  16132. new LeapSecond(new JulianDate(2442048, 43213.0, TimeStandard.TAI), 13), // January 1, 1974 00:00:00 UTC
  16133. new LeapSecond(new JulianDate(2442413, 43214.0, TimeStandard.TAI), 14), // January 1, 1975 00:00:00 UTC
  16134. new LeapSecond(new JulianDate(2442778, 43215.0, TimeStandard.TAI), 15), // January 1, 1976 00:00:00 UTC
  16135. new LeapSecond(new JulianDate(2443144, 43216.0, TimeStandard.TAI), 16), // January 1, 1977 00:00:00 UTC
  16136. new LeapSecond(new JulianDate(2443509, 43217.0, TimeStandard.TAI), 17), // January 1, 1978 00:00:00 UTC
  16137. new LeapSecond(new JulianDate(2443874, 43218.0, TimeStandard.TAI), 18), // January 1, 1979 00:00:00 UTC
  16138. new LeapSecond(new JulianDate(2444239, 43219.0, TimeStandard.TAI), 19), // January 1, 1980 00:00:00 UTC
  16139. new LeapSecond(new JulianDate(2444786, 43220.0, TimeStandard.TAI), 20), // July 1, 1981 00:00:00 UTC
  16140. new LeapSecond(new JulianDate(2445151, 43221.0, TimeStandard.TAI), 21), // July 1, 1982 00:00:00 UTC
  16141. new LeapSecond(new JulianDate(2445516, 43222.0, TimeStandard.TAI), 22), // July 1, 1983 00:00:00 UTC
  16142. new LeapSecond(new JulianDate(2446247, 43223.0, TimeStandard.TAI), 23), // July 1, 1985 00:00:00 UTC
  16143. new LeapSecond(new JulianDate(2447161, 43224.0, TimeStandard.TAI), 24), // January 1, 1988 00:00:00 UTC
  16144. new LeapSecond(new JulianDate(2447892, 43225.0, TimeStandard.TAI), 25), // January 1, 1990 00:00:00 UTC
  16145. new LeapSecond(new JulianDate(2448257, 43226.0, TimeStandard.TAI), 26), // January 1, 1991 00:00:00 UTC
  16146. new LeapSecond(new JulianDate(2448804, 43227.0, TimeStandard.TAI), 27), // July 1, 1992 00:00:00 UTC
  16147. new LeapSecond(new JulianDate(2449169, 43228.0, TimeStandard.TAI), 28), // July 1, 1993 00:00:00 UTC
  16148. new LeapSecond(new JulianDate(2449534, 43229.0, TimeStandard.TAI), 29), // July 1, 1994 00:00:00 UTC
  16149. new LeapSecond(new JulianDate(2450083, 43230.0, TimeStandard.TAI), 30), // January 1, 1996 00:00:00 UTC
  16150. new LeapSecond(new JulianDate(2450630, 43231.0, TimeStandard.TAI), 31), // July 1, 1997 00:00:00 UTC
  16151. new LeapSecond(new JulianDate(2451179, 43232.0, TimeStandard.TAI), 32), // January 1, 1999 00:00:00 UTC
  16152. new LeapSecond(new JulianDate(2453736, 43233.0, TimeStandard.TAI), 33), // January 1, 2006 00:00:00 UTC
  16153. new LeapSecond(new JulianDate(2454832, 43234.0, TimeStandard.TAI), 34), // January 1, 2009 00:00:00 UTC
  16154. new LeapSecond(new JulianDate(2456109, 43235.0, TimeStandard.TAI), 35), // July 1, 2012 00:00:00 UTC
  16155. new LeapSecond(new JulianDate(2457204, 43236.0, TimeStandard.TAI), 36), // July 1, 2015 00:00:00 UTC
  16156. new LeapSecond(new JulianDate(2457754, 43237.0, TimeStandard.TAI), 37) // January 1, 2017 00:00:00 UTC
  16157. ];
  16158. return JulianDate;
  16159. });
  16160. /*global define*/
  16161. define('Core/clone',[
  16162. './defaultValue'
  16163. ], function(
  16164. defaultValue) {
  16165. 'use strict';
  16166. /**
  16167. * Clones an object, returning a new object containing the same properties.
  16168. *
  16169. * @exports clone
  16170. *
  16171. * @param {Object} object The object to clone.
  16172. * @param {Boolean} [deep=false] If true, all properties will be deep cloned recursively.
  16173. * @returns {Object} The cloned object.
  16174. */
  16175. function clone(object, deep) {
  16176. if (object === null || typeof object !== 'object') {
  16177. return object;
  16178. }
  16179. deep = defaultValue(deep, false);
  16180. var result = new object.constructor();
  16181. for ( var propertyName in object) {
  16182. if (object.hasOwnProperty(propertyName)) {
  16183. var value = object[propertyName];
  16184. if (deep) {
  16185. value = clone(value, deep);
  16186. }
  16187. result[propertyName] = value;
  16188. }
  16189. }
  16190. return result;
  16191. }
  16192. return clone;
  16193. });
  16194. /*global define*/
  16195. define('Core/parseResponseHeaders',[], function() {
  16196. 'use strict';
  16197. /**
  16198. * Parses the result of XMLHttpRequest's getAllResponseHeaders() method into
  16199. * a dictionary.
  16200. *
  16201. * @exports parseResponseHeaders
  16202. *
  16203. * @param {String} headerString The header string returned by getAllResponseHeaders(). The format is
  16204. * described here: http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders()-method
  16205. * @returns {Object} A dictionary of key/value pairs, where each key is the name of a header and the corresponding value
  16206. * is that header's value.
  16207. *
  16208. * @private
  16209. */
  16210. function parseResponseHeaders(headerString) {
  16211. var headers = {};
  16212. if (!headerString) {
  16213. return headers;
  16214. }
  16215. var headerPairs = headerString.split('\u000d\u000a');
  16216. for (var i = 0; i < headerPairs.length; ++i) {
  16217. var headerPair = headerPairs[i];
  16218. // Can't use split() here because it does the wrong thing
  16219. // if the header value has the string ": " in it.
  16220. var index = headerPair.indexOf('\u003a\u0020');
  16221. if (index > 0) {
  16222. var key = headerPair.substring(0, index);
  16223. var val = headerPair.substring(index + 2);
  16224. headers[key] = val;
  16225. }
  16226. }
  16227. return headers;
  16228. }
  16229. return parseResponseHeaders;
  16230. });
  16231. /*global define*/
  16232. define('Core/RequestErrorEvent',[
  16233. './defined',
  16234. './parseResponseHeaders'
  16235. ], function(
  16236. defined,
  16237. parseResponseHeaders) {
  16238. 'use strict';
  16239. /**
  16240. * An event that is raised when a request encounters an error.
  16241. *
  16242. * @constructor
  16243. * @alias RequestErrorEvent
  16244. *
  16245. * @param {Number} [statusCode] The HTTP error status code, such as 404.
  16246. * @param {Object} [response] The response included along with the error.
  16247. * @param {String|Object} [responseHeaders] The response headers, represented either as an object literal or as a
  16248. * string in the format returned by XMLHttpRequest's getAllResponseHeaders() function.
  16249. */
  16250. function RequestErrorEvent(statusCode, response, responseHeaders) {
  16251. /**
  16252. * The HTTP error status code, such as 404. If the error does not have a particular
  16253. * HTTP code, this property will be undefined.
  16254. *
  16255. * @type {Number}
  16256. */
  16257. this.statusCode = statusCode;
  16258. /**
  16259. * The response included along with the error. If the error does not include a response,
  16260. * this property will be undefined.
  16261. *
  16262. * @type {Object}
  16263. */
  16264. this.response = response;
  16265. /**
  16266. * The headers included in the response, represented as an object literal of key/value pairs.
  16267. * If the error does not include any headers, this property will be undefined.
  16268. *
  16269. * @type {Object}
  16270. */
  16271. this.responseHeaders = responseHeaders;
  16272. if (typeof this.responseHeaders === 'string') {
  16273. this.responseHeaders = parseResponseHeaders(this.responseHeaders);
  16274. }
  16275. }
  16276. /**
  16277. * Creates a string representing this RequestErrorEvent.
  16278. * @memberof RequestErrorEvent
  16279. *
  16280. * @returns {String} A string representing the provided RequestErrorEvent.
  16281. */
  16282. RequestErrorEvent.prototype.toString = function() {
  16283. var str = 'Request has failed.';
  16284. if (defined(this.statusCode)) {
  16285. str += ' Status Code: ' + this.statusCode;
  16286. }
  16287. return str;
  16288. };
  16289. return RequestErrorEvent;
  16290. });
  16291. /**
  16292. * @license
  16293. *
  16294. * Grauw URI utilities
  16295. *
  16296. * See: http://hg.grauw.nl/grauw-lib/file/tip/src/uri.js
  16297. *
  16298. * @author Laurens Holst (http://www.grauw.nl/)
  16299. *
  16300. * Copyright 2012 Laurens Holst
  16301. *
  16302. * Licensed under the Apache License, Version 2.0 (the "License");
  16303. * you may not use this file except in compliance with the License.
  16304. * You may obtain a copy of the License at
  16305. *
  16306. * http://www.apache.org/licenses/LICENSE-2.0
  16307. *
  16308. * Unless required by applicable law or agreed to in writing, software
  16309. * distributed under the License is distributed on an "AS IS" BASIS,
  16310. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16311. * See the License for the specific language governing permissions and
  16312. * limitations under the License.
  16313. *
  16314. */
  16315. /*global define*/
  16316. define('ThirdParty/Uri',[],function() {
  16317. /**
  16318. * Constructs a URI object.
  16319. * @constructor
  16320. * @class Implementation of URI parsing and base URI resolving algorithm in RFC 3986.
  16321. * @param {string|URI} uri A string or URI object to create the object from.
  16322. */
  16323. function URI(uri) {
  16324. if (uri instanceof URI) { // copy constructor
  16325. this.scheme = uri.scheme;
  16326. this.authority = uri.authority;
  16327. this.path = uri.path;
  16328. this.query = uri.query;
  16329. this.fragment = uri.fragment;
  16330. } else if (uri) { // uri is URI string or cast to string
  16331. var c = parseRegex.exec(uri);
  16332. this.scheme = c[1];
  16333. this.authority = c[2];
  16334. this.path = c[3];
  16335. this.query = c[4];
  16336. this.fragment = c[5];
  16337. }
  16338. }
  16339. // Initial values on the prototype
  16340. URI.prototype.scheme = null;
  16341. URI.prototype.authority = null;
  16342. URI.prototype.path = '';
  16343. URI.prototype.query = null;
  16344. URI.prototype.fragment = null;
  16345. // Regular expression from RFC 3986 appendix B
  16346. var parseRegex = new RegExp('^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?$');
  16347. /**
  16348. * Returns the scheme part of the URI.
  16349. * In "http://example.com:80/a/b?x#y" this is "http".
  16350. */
  16351. URI.prototype.getScheme = function() {
  16352. return this.scheme;
  16353. };
  16354. /**
  16355. * Returns the authority part of the URI.
  16356. * In "http://example.com:80/a/b?x#y" this is "example.com:80".
  16357. */
  16358. URI.prototype.getAuthority = function() {
  16359. return this.authority;
  16360. };
  16361. /**
  16362. * Returns the path part of the URI.
  16363. * In "http://example.com:80/a/b?x#y" this is "/a/b".
  16364. * In "mailto:mike@example.com" this is "mike@example.com".
  16365. */
  16366. URI.prototype.getPath = function() {
  16367. return this.path;
  16368. };
  16369. /**
  16370. * Returns the query part of the URI.
  16371. * In "http://example.com:80/a/b?x#y" this is "x".
  16372. */
  16373. URI.prototype.getQuery = function() {
  16374. return this.query;
  16375. };
  16376. /**
  16377. * Returns the fragment part of the URI.
  16378. * In "http://example.com:80/a/b?x#y" this is "y".
  16379. */
  16380. URI.prototype.getFragment = function() {
  16381. return this.fragment;
  16382. };
  16383. /**
  16384. * Tests whether the URI is an absolute URI.
  16385. * See RFC 3986 section 4.3.
  16386. */
  16387. URI.prototype.isAbsolute = function() {
  16388. return !!this.scheme && !this.fragment;
  16389. };
  16390. ///**
  16391. //* Extensive validation of the URI against the ABNF in RFC 3986
  16392. //*/
  16393. //URI.prototype.validate
  16394. /**
  16395. * Tests whether the URI is a same-document reference.
  16396. * See RFC 3986 section 4.4.
  16397. *
  16398. * To perform more thorough comparison, you can normalise the URI objects.
  16399. */
  16400. URI.prototype.isSameDocumentAs = function(uri) {
  16401. return uri.scheme == this.scheme &&
  16402. uri.authority == this.authority &&
  16403. uri.path == this.path &&
  16404. uri.query == this.query;
  16405. };
  16406. /**
  16407. * Simple String Comparison of two URIs.
  16408. * See RFC 3986 section 6.2.1.
  16409. *
  16410. * To perform more thorough comparison, you can normalise the URI objects.
  16411. */
  16412. URI.prototype.equals = function(uri) {
  16413. return this.isSameDocumentAs(uri) && uri.fragment == this.fragment;
  16414. };
  16415. /**
  16416. * Normalizes the URI using syntax-based normalization.
  16417. * This includes case normalization, percent-encoding normalization and path segment normalization.
  16418. * XXX: Percent-encoding normalization does not escape characters that need to be escaped.
  16419. * (Although that would not be a valid URI in the first place. See validate().)
  16420. * See RFC 3986 section 6.2.2.
  16421. */
  16422. URI.prototype.normalize = function() {
  16423. this.removeDotSegments();
  16424. if (this.scheme)
  16425. this.scheme = this.scheme.toLowerCase();
  16426. if (this.authority)
  16427. this.authority = this.authority.replace(authorityRegex, replaceAuthority).
  16428. replace(caseRegex, replaceCase);
  16429. if (this.path)
  16430. this.path = this.path.replace(caseRegex, replaceCase);
  16431. if (this.query)
  16432. this.query = this.query.replace(caseRegex, replaceCase);
  16433. if (this.fragment)
  16434. this.fragment = this.fragment.replace(caseRegex, replaceCase);
  16435. };
  16436. var caseRegex = /%[0-9a-z]{2}/gi;
  16437. var percentRegex = /[a-zA-Z0-9\-\._~]/;
  16438. var authorityRegex = /(.*@)?([^@:]*)(:.*)?/;
  16439. function replaceCase(str) {
  16440. var dec = unescape(str);
  16441. return percentRegex.test(dec) ? dec : str.toUpperCase();
  16442. }
  16443. function replaceAuthority(str, p1, p2, p3) {
  16444. return (p1 || '') + p2.toLowerCase() + (p3 || '');
  16445. }
  16446. /**
  16447. * Resolve a relative URI (this) against a base URI.
  16448. * The base URI must be an absolute URI.
  16449. * See RFC 3986 section 5.2
  16450. */
  16451. URI.prototype.resolve = function(baseURI) {
  16452. var uri = new URI();
  16453. if (this.scheme) {
  16454. uri.scheme = this.scheme;
  16455. uri.authority = this.authority;
  16456. uri.path = this.path;
  16457. uri.query = this.query;
  16458. } else {
  16459. uri.scheme = baseURI.scheme;
  16460. if (this.authority) {
  16461. uri.authority = this.authority;
  16462. uri.path = this.path;
  16463. uri.query = this.query;
  16464. } else {
  16465. uri.authority = baseURI.authority;
  16466. if (this.path == '') {
  16467. uri.path = baseURI.path;
  16468. uri.query = this.query || baseURI.query;
  16469. } else {
  16470. if (this.path.charAt(0) == '/') {
  16471. uri.path = this.path;
  16472. uri.removeDotSegments();
  16473. } else {
  16474. if (baseURI.authority && baseURI.path == '') {
  16475. uri.path = '/' + this.path;
  16476. } else {
  16477. uri.path = baseURI.path.substring(0, baseURI.path.lastIndexOf('/') + 1) + this.path;
  16478. }
  16479. uri.removeDotSegments();
  16480. }
  16481. uri.query = this.query;
  16482. }
  16483. }
  16484. }
  16485. uri.fragment = this.fragment;
  16486. return uri;
  16487. };
  16488. /**
  16489. * Remove dot segments from path.
  16490. * See RFC 3986 section 5.2.4
  16491. * @private
  16492. */
  16493. URI.prototype.removeDotSegments = function() {
  16494. var input = this.path.split('/'),
  16495. output = [],
  16496. segment,
  16497. absPath = input[0] == '';
  16498. if (absPath)
  16499. input.shift();
  16500. var sFirst = input[0] == '' ? input.shift() : null;
  16501. while (input.length) {
  16502. segment = input.shift();
  16503. if (segment == '..') {
  16504. output.pop();
  16505. } else if (segment != '.') {
  16506. output.push(segment);
  16507. }
  16508. }
  16509. if (segment == '.' || segment == '..')
  16510. output.push('');
  16511. if (absPath)
  16512. output.unshift('');
  16513. this.path = output.join('/');
  16514. };
  16515. // We don't like this function because it builds up a cache that is never cleared.
  16516. // /**
  16517. // * Resolves a relative URI against an absolute base URI.
  16518. // * Convenience method.
  16519. // * @param {String} uri the relative URI to resolve
  16520. // * @param {String} baseURI the base URI (must be absolute) to resolve against
  16521. // */
  16522. // URI.resolve = function(sURI, sBaseURI) {
  16523. // var uri = cache[sURI] || (cache[sURI] = new URI(sURI));
  16524. // var baseURI = cache[sBaseURI] || (cache[sBaseURI] = new URI(sBaseURI));
  16525. // return uri.resolve(baseURI).toString();
  16526. // };
  16527. // var cache = {};
  16528. /**
  16529. * Serialises the URI to a string.
  16530. */
  16531. URI.prototype.toString = function() {
  16532. var result = '';
  16533. if (this.scheme)
  16534. result += this.scheme + ':';
  16535. if (this.authority)
  16536. result += '//' + this.authority;
  16537. result += this.path;
  16538. if (this.query)
  16539. result += '?' + this.query;
  16540. if (this.fragment)
  16541. result += '#' + this.fragment;
  16542. return result;
  16543. };
  16544. return URI;
  16545. });
  16546. /*global define*/
  16547. define('Core/TrustedServers',[
  16548. '../ThirdParty/Uri',
  16549. './defined',
  16550. './DeveloperError'
  16551. ], function(
  16552. Uri,
  16553. defined,
  16554. DeveloperError) {
  16555. 'use strict';
  16556. /**
  16557. * A singleton that contains all of the servers that are trusted. Credentials will be sent with
  16558. * any requests to these servers.
  16559. *
  16560. * @exports TrustedServers
  16561. *
  16562. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  16563. */
  16564. var TrustedServers = {};
  16565. var _servers = {};
  16566. /**
  16567. * Adds a trusted server to the registry
  16568. *
  16569. * @param {String} host The host to be added.
  16570. * @param {Number} port The port used to access the host.
  16571. *
  16572. * @example
  16573. * // Add a trusted server
  16574. * TrustedServers.add('my.server.com', 80);
  16575. */
  16576. TrustedServers.add = function(host, port) {
  16577. if (!defined(host)) {
  16578. throw new DeveloperError('host is required.');
  16579. }
  16580. if (!defined(port) || port <= 0) {
  16581. throw new DeveloperError('port is required to be greater than 0.');
  16582. }
  16583. var authority = host.toLowerCase() + ':' + port;
  16584. if (!defined(_servers[authority])) {
  16585. _servers[authority] = true;
  16586. }
  16587. };
  16588. /**
  16589. * Removes a trusted server from the registry
  16590. *
  16591. * @param {String} host The host to be removed.
  16592. * @param {Number} port The port used to access the host.
  16593. *
  16594. * @example
  16595. * // Remove a trusted server
  16596. * TrustedServers.remove('my.server.com', 80);
  16597. */
  16598. TrustedServers.remove = function(host, port) {
  16599. if (!defined(host)) {
  16600. throw new DeveloperError('host is required.');
  16601. }
  16602. if (!defined(port) || port <= 0) {
  16603. throw new DeveloperError('port is required to be greater than 0.');
  16604. }
  16605. var authority = host.toLowerCase() + ':' + port;
  16606. if (defined(_servers[authority])) {
  16607. delete _servers[authority];
  16608. }
  16609. };
  16610. function getAuthority(url) {
  16611. var uri = new Uri(url);
  16612. uri.normalize();
  16613. // Removes username:password@ so we just have host[:port]
  16614. var authority = uri.getAuthority();
  16615. if (!defined(authority)) {
  16616. return undefined; // Relative URL
  16617. }
  16618. if (authority.indexOf('@') !== -1) {
  16619. var parts = authority.split('@');
  16620. authority = parts[1];
  16621. }
  16622. // If the port is missing add one based on the scheme
  16623. if (authority.indexOf(':') === -1) {
  16624. var scheme = uri.getScheme();
  16625. if (!defined(scheme)) {
  16626. scheme = window.location.protocol;
  16627. scheme = scheme.substring(0, scheme.length-1);
  16628. }
  16629. if (scheme === 'http') {
  16630. authority += ':80';
  16631. } else if (scheme === 'https') {
  16632. authority += ':443';
  16633. } else {
  16634. return undefined;
  16635. }
  16636. }
  16637. return authority;
  16638. }
  16639. /**
  16640. * Tests whether a server is trusted or not. The server must have been added with the port if it is included in the url.
  16641. *
  16642. * @param {String} url The url to be tested against the trusted list
  16643. *
  16644. * @returns {boolean} Returns true if url is trusted, false otherwise.
  16645. *
  16646. * @example
  16647. * // Add server
  16648. * TrustedServers.add('my.server.com', 81);
  16649. *
  16650. * // Check if server is trusted
  16651. * if (TrustedServers.contains('https://my.server.com:81/path/to/file.png')) {
  16652. * // my.server.com:81 is trusted
  16653. * }
  16654. * if (TrustedServers.contains('https://my.server.com/path/to/file.png')) {
  16655. * // my.server.com isn't trusted
  16656. * }
  16657. */
  16658. TrustedServers.contains = function(url) {
  16659. if (!defined(url)) {
  16660. throw new DeveloperError('url is required.');
  16661. }
  16662. var authority = getAuthority(url);
  16663. if (defined(authority) && defined(_servers[authority])) {
  16664. return true;
  16665. }
  16666. return false;
  16667. };
  16668. /**
  16669. * Clears the registry
  16670. *
  16671. * @example
  16672. * // Remove a trusted server
  16673. * TrustedServers.clear();
  16674. */
  16675. TrustedServers.clear = function() {
  16676. _servers = {};
  16677. };
  16678. return TrustedServers;
  16679. });
  16680. /*global define*/
  16681. define('Core/loadWithXhr',[
  16682. '../ThirdParty/when',
  16683. './defaultValue',
  16684. './defined',
  16685. './DeveloperError',
  16686. './RequestErrorEvent',
  16687. './RuntimeError',
  16688. './TrustedServers'
  16689. ], function(
  16690. when,
  16691. defaultValue,
  16692. defined,
  16693. DeveloperError,
  16694. RequestErrorEvent,
  16695. RuntimeError,
  16696. TrustedServers) {
  16697. 'use strict';
  16698. /**
  16699. * Asynchronously loads the given URL. Returns a promise that will resolve to
  16700. * the result once loaded, or reject if the URL failed to load. The data is loaded
  16701. * using XMLHttpRequest, which means that in order to make requests to another origin,
  16702. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  16703. *
  16704. * @exports loadWithXhr
  16705. *
  16706. * @param {Object} options Object with the following properties:
  16707. * @param {String|Promise.<String>} options.url The URL of the data, or a promise for the URL.
  16708. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  16709. * @param {String} [options.method='GET'] The HTTP method to use.
  16710. * @param {String} [options.data] The data to send with the request, if any.
  16711. * @param {Object} [options.headers] HTTP headers to send with the request, if any.
  16712. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  16713. * @returns {Promise.<Object>} a promise that will resolve to the requested data when loaded.
  16714. *
  16715. *
  16716. * @example
  16717. * // Load a single URL asynchronously. In real code, you should use loadBlob instead.
  16718. * Cesium.loadWithXhr({
  16719. * url : 'some/url',
  16720. * responseType : 'blob'
  16721. * }).then(function(blob) {
  16722. * // use the data
  16723. * }).otherwise(function(error) {
  16724. * // an error occurred
  16725. * });
  16726. *
  16727. * @see loadArrayBuffer
  16728. * @see loadBlob
  16729. * @see loadJson
  16730. * @see loadText
  16731. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  16732. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  16733. */
  16734. function loadWithXhr(options) {
  16735. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  16736. if (!defined(options.url)) {
  16737. throw new DeveloperError('options.url is required.');
  16738. }
  16739. var responseType = options.responseType;
  16740. var method = defaultValue(options.method, 'GET');
  16741. var data = options.data;
  16742. var headers = options.headers;
  16743. var overrideMimeType = options.overrideMimeType;
  16744. return when(options.url, function(url) {
  16745. var deferred = when.defer();
  16746. loadWithXhr.load(url, responseType, method, data, headers, deferred, overrideMimeType);
  16747. return deferred.promise;
  16748. });
  16749. }
  16750. var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
  16751. function decodeDataUriText(isBase64, data) {
  16752. var result = decodeURIComponent(data);
  16753. if (isBase64) {
  16754. return atob(result);
  16755. }
  16756. return result;
  16757. }
  16758. function decodeDataUriArrayBuffer(isBase64, data) {
  16759. var byteString = decodeDataUriText(isBase64, data);
  16760. var buffer = new ArrayBuffer(byteString.length);
  16761. var view = new Uint8Array(buffer);
  16762. for (var i = 0; i < byteString.length; i++) {
  16763. view[i] = byteString.charCodeAt(i);
  16764. }
  16765. return buffer;
  16766. }
  16767. function decodeDataUri(dataUriRegexResult, responseType) {
  16768. responseType = defaultValue(responseType, '');
  16769. var mimeType = dataUriRegexResult[1];
  16770. var isBase64 = !!dataUriRegexResult[2];
  16771. var data = dataUriRegexResult[3];
  16772. switch (responseType) {
  16773. case '':
  16774. case 'text':
  16775. return decodeDataUriText(isBase64, data);
  16776. case 'arraybuffer':
  16777. return decodeDataUriArrayBuffer(isBase64, data);
  16778. case 'blob':
  16779. var buffer = decodeDataUriArrayBuffer(isBase64, data);
  16780. return new Blob([buffer], {
  16781. type : mimeType
  16782. });
  16783. case 'document':
  16784. var parser = new DOMParser();
  16785. return parser.parseFromString(decodeDataUriText(isBase64, data), mimeType);
  16786. case 'json':
  16787. return JSON.parse(decodeDataUriText(isBase64, data));
  16788. default:
  16789. throw new DeveloperError('Unhandled responseType: ' + responseType);
  16790. }
  16791. }
  16792. // This is broken out into a separate function so that it can be mocked for testing purposes.
  16793. loadWithXhr.load = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
  16794. var dataUriRegexResult = dataUriRegex.exec(url);
  16795. if (dataUriRegexResult !== null) {
  16796. deferred.resolve(decodeDataUri(dataUriRegexResult, responseType));
  16797. return;
  16798. }
  16799. var xhr = new XMLHttpRequest();
  16800. if (TrustedServers.contains(url)) {
  16801. xhr.withCredentials = true;
  16802. }
  16803. if (defined(overrideMimeType) && defined(xhr.overrideMimeType)) {
  16804. xhr.overrideMimeType(overrideMimeType);
  16805. }
  16806. xhr.open(method, url, true);
  16807. if (defined(headers)) {
  16808. for (var key in headers) {
  16809. if (headers.hasOwnProperty(key)) {
  16810. xhr.setRequestHeader(key, headers[key]);
  16811. }
  16812. }
  16813. }
  16814. if (defined(responseType)) {
  16815. xhr.responseType = responseType;
  16816. }
  16817. xhr.onload = function() {
  16818. if (xhr.status < 200 || xhr.status >= 300) {
  16819. deferred.reject(new RequestErrorEvent(xhr.status, xhr.response, xhr.getAllResponseHeaders()));
  16820. return;
  16821. }
  16822. var response = xhr.response;
  16823. var browserResponseType = xhr.responseType;
  16824. //All modern browsers will go into either the first if block or last else block.
  16825. //Other code paths support older browsers that either do not support the supplied responseType
  16826. //or do not support the xhr.response property.
  16827. if (defined(response) && (!defined(responseType) || (browserResponseType === responseType))) {
  16828. deferred.resolve(response);
  16829. } else if ((responseType === 'json') && typeof response === 'string') {
  16830. try {
  16831. deferred.resolve(JSON.parse(response));
  16832. } catch (e) {
  16833. deferred.reject(e);
  16834. }
  16835. } else if ((browserResponseType === '' || browserResponseType === 'document') && defined(xhr.responseXML) && xhr.responseXML.hasChildNodes()) {
  16836. deferred.resolve(xhr.responseXML);
  16837. } else if ((browserResponseType === '' || browserResponseType === 'text') && defined(xhr.responseText)) {
  16838. deferred.resolve(xhr.responseText);
  16839. } else {
  16840. deferred.reject(new RuntimeError('Invalid XMLHttpRequest response type.'));
  16841. }
  16842. };
  16843. xhr.onerror = function(e) {
  16844. deferred.reject(new RequestErrorEvent());
  16845. };
  16846. xhr.send(data);
  16847. };
  16848. loadWithXhr.defaultLoad = loadWithXhr.load;
  16849. return loadWithXhr;
  16850. });
  16851. /*global define*/
  16852. define('Core/loadText',[
  16853. './loadWithXhr'
  16854. ], function(
  16855. loadWithXhr) {
  16856. 'use strict';
  16857. /**
  16858. * Asynchronously loads the given URL as text. Returns a promise that will resolve to
  16859. * a String once loaded, or reject if the URL failed to load. The data is loaded
  16860. * using XMLHttpRequest, which means that in order to make requests to another origin,
  16861. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  16862. *
  16863. * @exports loadText
  16864. *
  16865. * @param {String|Promise.<String>} url The URL to request, or a promise for the URL.
  16866. * @param {Object} [headers] HTTP headers to send with the request.
  16867. * @returns {Promise.<String>} a promise that will resolve to the requested data when loaded.
  16868. *
  16869. *
  16870. * @example
  16871. * // load text from a URL, setting a custom header
  16872. * Cesium.loadText('http://someUrl.com/someJson.txt', {
  16873. * 'X-Custom-Header' : 'some value'
  16874. * }).then(function(text) {
  16875. * // Do something with the text
  16876. * }).otherwise(function(error) {
  16877. * // an error occurred
  16878. * });
  16879. *
  16880. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  16881. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  16882. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  16883. */
  16884. function loadText(url, headers) {
  16885. return loadWithXhr({
  16886. url : url,
  16887. headers : headers
  16888. });
  16889. }
  16890. return loadText;
  16891. });
  16892. /*global define*/
  16893. define('Core/loadJson',[
  16894. './clone',
  16895. './defined',
  16896. './DeveloperError',
  16897. './loadText'
  16898. ], function(
  16899. clone,
  16900. defined,
  16901. DeveloperError,
  16902. loadText) {
  16903. 'use strict';
  16904. var defaultHeaders = {
  16905. Accept : 'application/json,*/*;q=0.01'
  16906. };
  16907. // note: &#42;&#47;&#42; below is */* but that ends the comment block early
  16908. /**
  16909. * Asynchronously loads the given URL as JSON. Returns a promise that will resolve to
  16910. * a JSON object once loaded, or reject if the URL failed to load. The data is loaded
  16911. * using XMLHttpRequest, which means that in order to make requests to another origin,
  16912. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled. This function
  16913. * adds 'Accept: application/json,&#42;&#47;&#42;;q=0.01' to the request headers, if not
  16914. * already specified.
  16915. *
  16916. * @exports loadJson
  16917. *
  16918. * @param {String|Promise.<String>} url The URL to request, or a promise for the URL.
  16919. * @param {Object} [headers] HTTP headers to send with the request.
  16920. * 'Accept: application/json,&#42;&#47;&#42;;q=0.01' is added to the request headers automatically
  16921. * if not specified.
  16922. * @returns {Promise.<Object>} a promise that will resolve to the requested data when loaded.
  16923. *
  16924. *
  16925. * @example
  16926. * Cesium.loadJson('http://someUrl.com/someJson.txt').then(function(jsonData) {
  16927. * // Do something with the JSON object
  16928. * }).otherwise(function(error) {
  16929. * // an error occurred
  16930. * });
  16931. *
  16932. * @see loadText
  16933. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  16934. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  16935. */
  16936. function loadJson(url, headers) {
  16937. if (!defined(url)) {
  16938. throw new DeveloperError('url is required.');
  16939. }
  16940. if (!defined(headers)) {
  16941. headers = defaultHeaders;
  16942. } else if (!defined(headers.Accept)) {
  16943. // clone before adding the Accept header
  16944. headers = clone(headers);
  16945. headers.Accept = defaultHeaders.Accept;
  16946. }
  16947. return loadText(url, headers).then(function(value) {
  16948. return JSON.parse(value);
  16949. });
  16950. }
  16951. return loadJson;
  16952. });
  16953. /*global define*/
  16954. define('Core/EarthOrientationParameters',[
  16955. '../ThirdParty/when',
  16956. './binarySearch',
  16957. './defaultValue',
  16958. './defined',
  16959. './EarthOrientationParametersSample',
  16960. './freezeObject',
  16961. './JulianDate',
  16962. './LeapSecond',
  16963. './loadJson',
  16964. './RuntimeError',
  16965. './TimeConstants',
  16966. './TimeStandard'
  16967. ], function(
  16968. when,
  16969. binarySearch,
  16970. defaultValue,
  16971. defined,
  16972. EarthOrientationParametersSample,
  16973. freezeObject,
  16974. JulianDate,
  16975. LeapSecond,
  16976. loadJson,
  16977. RuntimeError,
  16978. TimeConstants,
  16979. TimeStandard) {
  16980. 'use strict';
  16981. /**
  16982. * Specifies Earth polar motion coordinates and the difference between UT1 and UTC.
  16983. * These Earth Orientation Parameters (EOP) are primarily used in the transformation from
  16984. * the International Celestial Reference Frame (ICRF) to the International Terrestrial
  16985. * Reference Frame (ITRF).
  16986. *
  16987. * @alias EarthOrientationParameters
  16988. * @constructor
  16989. *
  16990. * @param {Object} [options] Object with the following properties:
  16991. * @param {String} [options.url] The URL from which to obtain EOP data. If neither this
  16992. * parameter nor options.data is specified, all EOP values are assumed
  16993. * to be 0.0. If options.data is specified, this parameter is
  16994. * ignored.
  16995. * @param {Object} [options.data] The actual EOP data. If neither this
  16996. * parameter nor options.data is specified, all EOP values are assumed
  16997. * to be 0.0.
  16998. * @param {Boolean} [options.addNewLeapSeconds=true] True if leap seconds that
  16999. * are specified in the EOP data but not in {@link JulianDate.leapSeconds}
  17000. * should be added to {@link JulianDate.leapSeconds}. False if
  17001. * new leap seconds should be handled correctly in the context
  17002. * of the EOP data but otherwise ignored.
  17003. *
  17004. * @example
  17005. * // An example EOP data file, EOP.json:
  17006. * {
  17007. * "columnNames" : ["dateIso8601","modifiedJulianDateUtc","xPoleWanderRadians","yPoleWanderRadians","ut1MinusUtcSeconds","lengthOfDayCorrectionSeconds","xCelestialPoleOffsetRadians","yCelestialPoleOffsetRadians","taiMinusUtcSeconds"],
  17008. * "samples" : [
  17009. * "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,
  17010. * "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,
  17011. * "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
  17012. * ]
  17013. * }
  17014. *
  17015. * @example
  17016. * // Loading the EOP data
  17017. * var eop = new Cesium.EarthOrientationParameters({ url : 'Data/EOP.json' });
  17018. * Cesium.Transforms.earthOrientationParameters = eop;
  17019. *
  17020. * @private
  17021. */
  17022. function EarthOrientationParameters(options) {
  17023. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  17024. this._dates = undefined;
  17025. this._samples = undefined;
  17026. this._dateColumn = -1;
  17027. this._xPoleWanderRadiansColumn = -1;
  17028. this._yPoleWanderRadiansColumn = -1;
  17029. this._ut1MinusUtcSecondsColumn = -1;
  17030. this._xCelestialPoleOffsetRadiansColumn = -1;
  17031. this._yCelestialPoleOffsetRadiansColumn = -1;
  17032. this._taiMinusUtcSecondsColumn = -1;
  17033. this._columnCount = 0;
  17034. this._lastIndex = -1;
  17035. this._downloadPromise = undefined;
  17036. this._dataError = undefined;
  17037. this._addNewLeapSeconds = defaultValue(options.addNewLeapSeconds, true);
  17038. if (defined(options.data)) {
  17039. // Use supplied EOP data.
  17040. onDataReady(this, options.data);
  17041. } else if (defined(options.url)) {
  17042. // Download EOP data.
  17043. var that = this;
  17044. this._downloadPromise = when(loadJson(options.url), function(eopData) {
  17045. onDataReady(that, eopData);
  17046. }, function() {
  17047. that._dataError = 'An error occurred while retrieving the EOP data from the URL ' + options.url + '.';
  17048. });
  17049. } else {
  17050. // Use all zeros for EOP data.
  17051. onDataReady(this, {
  17052. 'columnNames' : ['dateIso8601', 'modifiedJulianDateUtc', 'xPoleWanderRadians', 'yPoleWanderRadians', 'ut1MinusUtcSeconds', 'lengthOfDayCorrectionSeconds', 'xCelestialPoleOffsetRadians', 'yCelestialPoleOffsetRadians', 'taiMinusUtcSeconds'],
  17053. 'samples' : []
  17054. });
  17055. }
  17056. }
  17057. /**
  17058. * A default {@link EarthOrientationParameters} instance that returns zero for all EOP values.
  17059. */
  17060. EarthOrientationParameters.NONE = freezeObject({
  17061. getPromiseToLoad : function() {
  17062. return when();
  17063. },
  17064. compute : function(date, result) {
  17065. if (!defined(result)) {
  17066. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  17067. } else {
  17068. result.xPoleWander = 0.0;
  17069. result.yPoleWander = 0.0;
  17070. result.xPoleOffset = 0.0;
  17071. result.yPoleOffset = 0.0;
  17072. result.ut1MinusUtc = 0.0;
  17073. }
  17074. return result;
  17075. }
  17076. });
  17077. /**
  17078. * Gets a promise that, when resolved, indicates that the EOP data has been loaded and is
  17079. * ready to use.
  17080. *
  17081. * @returns {Promise.<undefined>} The promise.
  17082. *
  17083. * @see when
  17084. */
  17085. EarthOrientationParameters.prototype.getPromiseToLoad = function() {
  17086. return when(this._downloadPromise);
  17087. };
  17088. /**
  17089. * Computes the Earth Orientation Parameters (EOP) for a given date by interpolating.
  17090. * If the EOP data has not yet been download, this method returns undefined.
  17091. *
  17092. * @param {JulianDate} date The date for each to evaluate the EOP.
  17093. * @param {EarthOrientationParametersSample} [result] The instance to which to copy the result.
  17094. * If this parameter is undefined, a new instance is created and returned.
  17095. * @returns {EarthOrientationParametersSample} The EOP evaluated at the given date, or
  17096. * undefined if the data necessary to evaluate EOP at the date has not yet been
  17097. * downloaded.
  17098. *
  17099. * @exception {RuntimeError} The loaded EOP data has an error and cannot be used.
  17100. *
  17101. * @see EarthOrientationParameters#getPromiseToLoad
  17102. */
  17103. EarthOrientationParameters.prototype.compute = function(date, result) {
  17104. // We cannot compute until the samples are available.
  17105. if (!defined(this._samples)) {
  17106. if (defined(this._dataError)) {
  17107. throw new RuntimeError(this._dataError);
  17108. }
  17109. return undefined;
  17110. }
  17111. if (!defined(result)) {
  17112. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  17113. }
  17114. if (this._samples.length === 0) {
  17115. result.xPoleWander = 0.0;
  17116. result.yPoleWander = 0.0;
  17117. result.xPoleOffset = 0.0;
  17118. result.yPoleOffset = 0.0;
  17119. result.ut1MinusUtc = 0.0;
  17120. return result;
  17121. }
  17122. var dates = this._dates;
  17123. var lastIndex = this._lastIndex;
  17124. var before = 0;
  17125. var after = 0;
  17126. if (defined(lastIndex)) {
  17127. var previousIndexDate = dates[lastIndex];
  17128. var nextIndexDate = dates[lastIndex + 1];
  17129. var isAfterPrevious = JulianDate.lessThanOrEquals(previousIndexDate, date);
  17130. var isAfterLastSample = !defined(nextIndexDate);
  17131. var isBeforeNext = isAfterLastSample || JulianDate.greaterThanOrEquals(nextIndexDate, date);
  17132. if (isAfterPrevious && isBeforeNext) {
  17133. before = lastIndex;
  17134. if (!isAfterLastSample && nextIndexDate.equals(date)) {
  17135. ++before;
  17136. }
  17137. after = before + 1;
  17138. interpolate(this, dates, this._samples, date, before, after, result);
  17139. return result;
  17140. }
  17141. }
  17142. var index = binarySearch(dates, date, JulianDate.compare, this._dateColumn);
  17143. if (index >= 0) {
  17144. // If the next entry is the same date, use the later entry. This way, if two entries
  17145. // describe the same moment, one before a leap second and the other after, then we will use
  17146. // the post-leap second data.
  17147. if (index < dates.length - 1 && dates[index + 1].equals(date)) {
  17148. ++index;
  17149. }
  17150. before = index;
  17151. after = index;
  17152. } else {
  17153. after = ~index;
  17154. before = after - 1;
  17155. // Use the first entry if the date requested is before the beginning of the data.
  17156. if (before < 0) {
  17157. before = 0;
  17158. }
  17159. }
  17160. this._lastIndex = before;
  17161. interpolate(this, dates, this._samples, date, before, after, result);
  17162. return result;
  17163. };
  17164. function compareLeapSecondDates(leapSecond, dateToFind) {
  17165. return JulianDate.compare(leapSecond.julianDate, dateToFind);
  17166. }
  17167. function onDataReady(eop, eopData) {
  17168. if (!defined(eopData.columnNames)) {
  17169. eop._dataError = 'Error in loaded EOP data: The columnNames property is required.';
  17170. return;
  17171. }
  17172. if (!defined(eopData.samples)) {
  17173. eop._dataError = 'Error in loaded EOP data: The samples property is required.';
  17174. return;
  17175. }
  17176. var dateColumn = eopData.columnNames.indexOf('modifiedJulianDateUtc');
  17177. var xPoleWanderRadiansColumn = eopData.columnNames.indexOf('xPoleWanderRadians');
  17178. var yPoleWanderRadiansColumn = eopData.columnNames.indexOf('yPoleWanderRadians');
  17179. var ut1MinusUtcSecondsColumn = eopData.columnNames.indexOf('ut1MinusUtcSeconds');
  17180. var xCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf('xCelestialPoleOffsetRadians');
  17181. var yCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf('yCelestialPoleOffsetRadians');
  17182. var taiMinusUtcSecondsColumn = eopData.columnNames.indexOf('taiMinusUtcSeconds');
  17183. if (dateColumn < 0 || xPoleWanderRadiansColumn < 0 || yPoleWanderRadiansColumn < 0 || ut1MinusUtcSecondsColumn < 0 || xCelestialPoleOffsetRadiansColumn < 0 || yCelestialPoleOffsetRadiansColumn < 0 || taiMinusUtcSecondsColumn < 0) {
  17184. eop._dataError = 'Error in loaded EOP data: The columnNames property must include modifiedJulianDateUtc, xPoleWanderRadians, yPoleWanderRadians, ut1MinusUtcSeconds, xCelestialPoleOffsetRadians, yCelestialPoleOffsetRadians, and taiMinusUtcSeconds columns';
  17185. return;
  17186. }
  17187. var samples = eop._samples = eopData.samples;
  17188. var dates = eop._dates = [];
  17189. eop._dateColumn = dateColumn;
  17190. eop._xPoleWanderRadiansColumn = xPoleWanderRadiansColumn;
  17191. eop._yPoleWanderRadiansColumn = yPoleWanderRadiansColumn;
  17192. eop._ut1MinusUtcSecondsColumn = ut1MinusUtcSecondsColumn;
  17193. eop._xCelestialPoleOffsetRadiansColumn = xCelestialPoleOffsetRadiansColumn;
  17194. eop._yCelestialPoleOffsetRadiansColumn = yCelestialPoleOffsetRadiansColumn;
  17195. eop._taiMinusUtcSecondsColumn = taiMinusUtcSecondsColumn;
  17196. eop._columnCount = eopData.columnNames.length;
  17197. eop._lastIndex = undefined;
  17198. var lastTaiMinusUtc;
  17199. var addNewLeapSeconds = eop._addNewLeapSeconds;
  17200. // Convert the ISO8601 dates to JulianDates.
  17201. for (var i = 0, len = samples.length; i < len; i += eop._columnCount) {
  17202. var mjd = samples[i + dateColumn];
  17203. var taiMinusUtc = samples[i + taiMinusUtcSecondsColumn];
  17204. var day = mjd + TimeConstants.MODIFIED_JULIAN_DATE_DIFFERENCE;
  17205. var date = new JulianDate(day, taiMinusUtc, TimeStandard.TAI);
  17206. dates.push(date);
  17207. if (addNewLeapSeconds) {
  17208. if (taiMinusUtc !== lastTaiMinusUtc && defined(lastTaiMinusUtc)) {
  17209. // We crossed a leap second boundary, so add the leap second
  17210. // if it does not already exist.
  17211. var leapSeconds = JulianDate.leapSeconds;
  17212. var leapSecondIndex = binarySearch(leapSeconds, date, compareLeapSecondDates);
  17213. if (leapSecondIndex < 0) {
  17214. var leapSecond = new LeapSecond(date, taiMinusUtc);
  17215. leapSeconds.splice(~leapSecondIndex, 0, leapSecond);
  17216. }
  17217. }
  17218. lastTaiMinusUtc = taiMinusUtc;
  17219. }
  17220. }
  17221. }
  17222. function fillResultFromIndex(eop, samples, index, columnCount, result) {
  17223. var start = index * columnCount;
  17224. result.xPoleWander = samples[start + eop._xPoleWanderRadiansColumn];
  17225. result.yPoleWander = samples[start + eop._yPoleWanderRadiansColumn];
  17226. result.xPoleOffset = samples[start + eop._xCelestialPoleOffsetRadiansColumn];
  17227. result.yPoleOffset = samples[start + eop._yCelestialPoleOffsetRadiansColumn];
  17228. result.ut1MinusUtc = samples[start + eop._ut1MinusUtcSecondsColumn];
  17229. }
  17230. function linearInterp(dx, y1, y2) {
  17231. return y1 + dx * (y2 - y1);
  17232. }
  17233. function interpolate(eop, dates, samples, date, before, after, result) {
  17234. var columnCount = eop._columnCount;
  17235. // First check the bounds on the EOP data
  17236. // If we are after the bounds of the data, return zeros.
  17237. // The 'before' index should never be less than zero.
  17238. if (after > dates.length - 1) {
  17239. result.xPoleWander = 0;
  17240. result.yPoleWander = 0;
  17241. result.xPoleOffset = 0;
  17242. result.yPoleOffset = 0;
  17243. result.ut1MinusUtc = 0;
  17244. return result;
  17245. }
  17246. var beforeDate = dates[before];
  17247. var afterDate = dates[after];
  17248. if (beforeDate.equals(afterDate) || date.equals(beforeDate)) {
  17249. fillResultFromIndex(eop, samples, before, columnCount, result);
  17250. return result;
  17251. } else if (date.equals(afterDate)) {
  17252. fillResultFromIndex(eop, samples, after, columnCount, result);
  17253. return result;
  17254. }
  17255. var factor = JulianDate.secondsDifference(date, beforeDate) / JulianDate.secondsDifference(afterDate, beforeDate);
  17256. var startBefore = before * columnCount;
  17257. var startAfter = after * columnCount;
  17258. // Handle UT1 leap second edge case
  17259. var beforeUt1MinusUtc = samples[startBefore + eop._ut1MinusUtcSecondsColumn];
  17260. var afterUt1MinusUtc = samples[startAfter + eop._ut1MinusUtcSecondsColumn];
  17261. var offsetDifference = afterUt1MinusUtc - beforeUt1MinusUtc;
  17262. if (offsetDifference > 0.5 || offsetDifference < -0.5) {
  17263. // The absolute difference between the values is more than 0.5, so we may have
  17264. // crossed a leap second. Check if this is the case and, if so, adjust the
  17265. // afterValue to account for the leap second. This way, our interpolation will
  17266. // produce reasonable results.
  17267. var beforeTaiMinusUtc = samples[startBefore + eop._taiMinusUtcSecondsColumn];
  17268. var afterTaiMinusUtc = samples[startAfter + eop._taiMinusUtcSecondsColumn];
  17269. if (beforeTaiMinusUtc !== afterTaiMinusUtc) {
  17270. if (afterDate.equals(date)) {
  17271. // If we are at the end of the leap second interval, take the second value
  17272. // Otherwise, the interpolation below will yield the wrong side of the
  17273. // discontinuity
  17274. // At the end of the leap second, we need to start accounting for the jump
  17275. beforeUt1MinusUtc = afterUt1MinusUtc;
  17276. } else {
  17277. // Otherwise, remove the leap second so that the interpolation is correct
  17278. afterUt1MinusUtc -= afterTaiMinusUtc - beforeTaiMinusUtc;
  17279. }
  17280. }
  17281. }
  17282. result.xPoleWander = linearInterp(factor, samples[startBefore + eop._xPoleWanderRadiansColumn], samples[startAfter + eop._xPoleWanderRadiansColumn]);
  17283. result.yPoleWander = linearInterp(factor, samples[startBefore + eop._yPoleWanderRadiansColumn], samples[startAfter + eop._yPoleWanderRadiansColumn]);
  17284. result.xPoleOffset = linearInterp(factor, samples[startBefore + eop._xCelestialPoleOffsetRadiansColumn], samples[startAfter + eop._xCelestialPoleOffsetRadiansColumn]);
  17285. result.yPoleOffset = linearInterp(factor, samples[startBefore + eop._yCelestialPoleOffsetRadiansColumn], samples[startAfter + eop._yCelestialPoleOffsetRadiansColumn]);
  17286. result.ut1MinusUtc = linearInterp(factor, beforeUt1MinusUtc, afterUt1MinusUtc);
  17287. return result;
  17288. }
  17289. return EarthOrientationParameters;
  17290. });
  17291. /*global define*/
  17292. define('Core/HeadingPitchRoll',[
  17293. './defaultValue',
  17294. './defined',
  17295. './DeveloperError',
  17296. './Math'
  17297. ], function(
  17298. defaultValue,
  17299. defined,
  17300. DeveloperError,
  17301. CesiumMath) {
  17302. "use strict";
  17303. /**
  17304. * A rotation expressed as a heading, pitch, and roll. Heading is the rotation about the
  17305. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  17306. * the positive x axis.
  17307. * @alias HeadingPitchRoll
  17308. * @constructor
  17309. *
  17310. * @param {Number} [heading=0.0] The heading component in radians.
  17311. * @param {Number} [pitch=0.0] The pitch component in radians.
  17312. * @param {Number} [roll=0.0] The roll component in radians.
  17313. */
  17314. function HeadingPitchRoll(heading, pitch, roll) {
  17315. this.heading = defaultValue(heading, 0.0);
  17316. this.pitch = defaultValue(pitch, 0.0);
  17317. this.roll = defaultValue(roll, 0.0);
  17318. }
  17319. /**
  17320. * Computes the heading, pitch and roll from a quaternion (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  17321. *
  17322. * @param {Quaternion} quaternion The quaternion from which to retrieve heading, pitch, and roll, all expressed in radians.
  17323. * @param {Quaternion} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  17324. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  17325. */
  17326. HeadingPitchRoll.fromQuaternion = function(quaternion, result) {
  17327. if (!defined(quaternion)) {
  17328. throw new DeveloperError('quaternion is required');
  17329. }
  17330. if (!defined(result)) {
  17331. result = new HeadingPitchRoll();
  17332. }
  17333. var test = 2 * (quaternion.w * quaternion.y - quaternion.z * quaternion.x);
  17334. var denominatorRoll = 1 - 2 * (quaternion.x * quaternion.x + quaternion.y * quaternion.y);
  17335. var numeratorRoll = 2 * (quaternion.w * quaternion.x + quaternion.y * quaternion.z);
  17336. var denominatorHeading = 1 - 2 * (quaternion.y * quaternion.y + quaternion.z * quaternion.z);
  17337. var numeratorHeading = 2 * (quaternion.w * quaternion.z + quaternion.x * quaternion.y);
  17338. result.heading = -Math.atan2(numeratorHeading, denominatorHeading);
  17339. result.roll = Math.atan2(numeratorRoll, denominatorRoll);
  17340. result.pitch = -Math.asin(test);
  17341. return result;
  17342. };
  17343. /**
  17344. * Returns a new HeadingPitchRoll instance from angles given in degrees.
  17345. *
  17346. * @param {Number} heading the heading in degrees
  17347. * @param {Number} pitch the pitch in degrees
  17348. * @param {Number} roll the heading in degrees
  17349. * @param {HeadingPitchRoll} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  17350. * @returns {HeadingPitchRoll} A new HeadingPitchRoll instance
  17351. */
  17352. HeadingPitchRoll.fromDegrees = function(heading, pitch, roll, result) {
  17353. if (!defined(heading)) {
  17354. throw new DeveloperError('heading is required');
  17355. }
  17356. if (!defined(pitch)) {
  17357. throw new DeveloperError('pitch is required');
  17358. }
  17359. if (!defined(roll)) {
  17360. throw new DeveloperError('roll is required');
  17361. }
  17362. if (!defined(result)) {
  17363. result = new HeadingPitchRoll();
  17364. }
  17365. result.heading = heading * CesiumMath.RADIANS_PER_DEGREE;
  17366. result.pitch = pitch * CesiumMath.RADIANS_PER_DEGREE;
  17367. result.roll = roll * CesiumMath.RADIANS_PER_DEGREE;
  17368. return result;
  17369. };
  17370. /**
  17371. * Duplicates a HeadingPitchRoll instance.
  17372. *
  17373. * @param {HeadingPitchRoll} headingPitchRoll The HeadingPitchRoll to duplicate.
  17374. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  17375. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided. (Returns undefined if headingPitchRoll is undefined)
  17376. */
  17377. HeadingPitchRoll.clone = function(headingPitchRoll, result) {
  17378. if (!defined(headingPitchRoll)) {
  17379. return undefined;
  17380. }
  17381. if (!defined(result)) {
  17382. return new HeadingPitchRoll(headingPitchRoll.heading, headingPitchRoll.pitch, headingPitchRoll.roll);
  17383. }
  17384. result.heading = headingPitchRoll.heading;
  17385. result.pitch = headingPitchRoll.pitch;
  17386. result.roll = headingPitchRoll.roll;
  17387. return result;
  17388. };
  17389. /**
  17390. * Compares the provided HeadingPitchRolls componentwise and returns
  17391. * <code>true</code> if they are equal, <code>false</code> otherwise.
  17392. *
  17393. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  17394. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  17395. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  17396. */
  17397. HeadingPitchRoll.equals = function(left, right) {
  17398. return (left === right) ||
  17399. ((defined(left)) &&
  17400. (defined(right)) &&
  17401. (left.heading === right.heading) &&
  17402. (left.pitch === right.pitch) &&
  17403. (left.roll === right.roll));
  17404. };
  17405. /**
  17406. * Compares the provided HeadingPitchRolls componentwise and returns
  17407. * <code>true</code> if they pass an absolute or relative tolerance test,
  17408. * <code>false</code> otherwise.
  17409. *
  17410. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  17411. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  17412. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  17413. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  17414. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  17415. */
  17416. HeadingPitchRoll.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  17417. return (left === right) ||
  17418. (defined(left) &&
  17419. defined(right) &&
  17420. CesiumMath.equalsEpsilon(left.heading, right.heading, relativeEpsilon, absoluteEpsilon) &&
  17421. CesiumMath.equalsEpsilon(left.pitch, right.pitch, relativeEpsilon, absoluteEpsilon) &&
  17422. CesiumMath.equalsEpsilon(left.roll, right.roll, relativeEpsilon, absoluteEpsilon));
  17423. };
  17424. /**
  17425. * Duplicates this HeadingPitchRoll instance.
  17426. *
  17427. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  17428. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  17429. */
  17430. HeadingPitchRoll.prototype.clone = function(result) {
  17431. return HeadingPitchRoll.clone(this, result);
  17432. };
  17433. /**
  17434. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  17435. * <code>true</code> if they are equal, <code>false</code> otherwise.
  17436. *
  17437. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  17438. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  17439. */
  17440. HeadingPitchRoll.prototype.equals = function(right) {
  17441. return HeadingPitchRoll.equals(this, right);
  17442. };
  17443. /**
  17444. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  17445. * <code>true</code> if they pass an absolute or relative tolerance test,
  17446. * <code>false</code> otherwise.
  17447. *
  17448. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  17449. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  17450. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  17451. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  17452. */
  17453. HeadingPitchRoll.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  17454. return HeadingPitchRoll.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  17455. };
  17456. /**
  17457. * Creates a string representing this HeadingPitchRoll in the format '(heading, pitch, roll)' in radians.
  17458. *
  17459. * @returns {String} A string representing the provided HeadingPitchRoll in the format '(heading, pitch, roll)'.
  17460. */
  17461. HeadingPitchRoll.prototype.toString = function() {
  17462. return '(' + this.heading + ', ' + this.pitch + ', ' + this.roll + ')';
  17463. };
  17464. return HeadingPitchRoll;
  17465. });
  17466. /*global define*/
  17467. define('Core/getAbsoluteUri',[
  17468. '../ThirdParty/Uri',
  17469. './defaultValue',
  17470. './defined',
  17471. './DeveloperError'
  17472. ], function(
  17473. Uri,
  17474. defaultValue,
  17475. defined,
  17476. DeveloperError) {
  17477. 'use strict';
  17478. /**
  17479. * Given a relative Uri and a base Uri, returns the absolute Uri of the relative Uri.
  17480. * @exports getAbsoluteUri
  17481. *
  17482. * @param {String} relative The relative Uri.
  17483. * @param {String} [base] The base Uri.
  17484. * @returns {String} The absolute Uri of the given relative Uri.
  17485. *
  17486. * @example
  17487. * //absolute Uri will be "https://test.com/awesome.png";
  17488. * var absoluteUri = Cesium.getAbsoluteUri('awesome.png', 'https://test.com');
  17489. */
  17490. function getAbsoluteUri(relative, base) {
  17491. if (!defined(relative)) {
  17492. throw new DeveloperError('relative uri is required.');
  17493. }
  17494. base = defaultValue(base, document.location.href);
  17495. var baseUri = new Uri(base);
  17496. var relativeUri = new Uri(relative);
  17497. return relativeUri.resolve(baseUri).toString();
  17498. }
  17499. return getAbsoluteUri;
  17500. });
  17501. /*global define*/
  17502. define('Core/joinUrls',[
  17503. '../ThirdParty/Uri',
  17504. './defaultValue',
  17505. './defined',
  17506. './DeveloperError'
  17507. ], function(
  17508. Uri,
  17509. defaultValue,
  17510. defined,
  17511. DeveloperError) {
  17512. 'use strict';
  17513. /**
  17514. * Function for joining URLs in a manner that is aware of query strings and fragments.
  17515. * This is useful when the base URL has a query string that needs to be maintained
  17516. * (e.g. a presigned base URL).
  17517. * @param {String|Uri} first The base URL.
  17518. * @param {String|Uri} second The URL path to join to the base URL. If this URL is absolute, it is returned unmodified.
  17519. * @param {Boolean} [appendSlash=true] The boolean determining whether there should be a forward slash between first and second.
  17520. * @private
  17521. */
  17522. function joinUrls(first, second, appendSlash) {
  17523. if (!defined(first)) {
  17524. throw new DeveloperError('first is required');
  17525. }
  17526. if (!defined(second)) {
  17527. throw new DeveloperError('second is required');
  17528. }
  17529. appendSlash = defaultValue(appendSlash, true);
  17530. if (!(first instanceof Uri)) {
  17531. first = new Uri(first);
  17532. }
  17533. if (!(second instanceof Uri)) {
  17534. second = new Uri(second);
  17535. }
  17536. // Uri.isAbsolute returns false for a URL like '//foo.com'. So if we have an authority but
  17537. // not a scheme, add a scheme matching the page's scheme.
  17538. if (defined(second.authority) && !defined(second.scheme)) {
  17539. if (typeof document !== 'undefined' && defined(document.location) && defined(document.location.href)) {
  17540. second.scheme = new Uri(document.location.href).scheme;
  17541. } else {
  17542. // Not in a browser? Use the first URL's scheme instead.
  17543. second.scheme = first.scheme;
  17544. }
  17545. }
  17546. // If the second URL is absolute, use it for the scheme, authority, and path.
  17547. var baseUri = first;
  17548. if (second.isAbsolute()) {
  17549. baseUri = second;
  17550. }
  17551. var url = '';
  17552. if (defined(baseUri.scheme)) {
  17553. url += baseUri.scheme + ':';
  17554. }
  17555. if (defined(baseUri.authority)) {
  17556. url += '//' + baseUri.authority;
  17557. if (baseUri.path !== '' && baseUri.path !== '/') {
  17558. url = url.replace(/\/?$/, '/');
  17559. baseUri.path = baseUri.path.replace(/^\/?/g, '');
  17560. }
  17561. }
  17562. // Combine the paths (only if second is relative).
  17563. if (baseUri === first) {
  17564. if (appendSlash) {
  17565. url += first.path.replace(/\/?$/, '/') + second.path.replace(/^\/?/g, '');
  17566. } else {
  17567. url += first.path + second.path;
  17568. }
  17569. } else {
  17570. url += second.path;
  17571. }
  17572. // Combine the queries and fragments.
  17573. var hasFirstQuery = defined(first.query);
  17574. var hasSecondQuery = defined(second.query);
  17575. if (hasFirstQuery && hasSecondQuery) {
  17576. url += '?' + first.query + '&' + second.query;
  17577. } else if (hasFirstQuery && !hasSecondQuery) {
  17578. url += '?' + first.query;
  17579. } else if (!hasFirstQuery && hasSecondQuery) {
  17580. url += '?' + second.query;
  17581. }
  17582. var hasSecondFragment = defined(second.fragment);
  17583. if (defined(first.fragment) && !hasSecondFragment) {
  17584. url += '#' + first.fragment;
  17585. } else if (hasSecondFragment) {
  17586. url += '#' + second.fragment;
  17587. }
  17588. return url;
  17589. }
  17590. return joinUrls;
  17591. });
  17592. /*global define*/
  17593. define('Core/buildModuleUrl',[
  17594. '../ThirdParty/Uri',
  17595. './defined',
  17596. './DeveloperError',
  17597. './getAbsoluteUri',
  17598. './joinUrls',
  17599. 'require'
  17600. ], function(
  17601. Uri,
  17602. defined,
  17603. DeveloperError,
  17604. getAbsoluteUri,
  17605. joinUrls,
  17606. require) {
  17607. 'use strict';
  17608. /*global CESIUM_BASE_URL*/
  17609. var cesiumScriptRegex = /((?:.*\/)|^)cesium[\w-]*\.js(?:\W|$)/i;
  17610. function getBaseUrlFromCesiumScript() {
  17611. var scripts = document.getElementsByTagName('script');
  17612. for ( var i = 0, len = scripts.length; i < len; ++i) {
  17613. var src = scripts[i].getAttribute('src');
  17614. var result = cesiumScriptRegex.exec(src);
  17615. if (result !== null) {
  17616. return result[1];
  17617. }
  17618. }
  17619. return undefined;
  17620. }
  17621. var baseUrl;
  17622. function getCesiumBaseUrl() {
  17623. if (defined(baseUrl)) {
  17624. return baseUrl;
  17625. }
  17626. var baseUrlString;
  17627. if (typeof CESIUM_BASE_URL !== 'undefined') {
  17628. baseUrlString = CESIUM_BASE_URL;
  17629. } else {
  17630. baseUrlString = getBaseUrlFromCesiumScript();
  17631. }
  17632. if (!defined(baseUrlString)) {
  17633. throw new DeveloperError('Unable to determine Cesium base URL automatically, try defining a global variable called CESIUM_BASE_URL.');
  17634. }
  17635. baseUrl = new Uri(getAbsoluteUri(baseUrlString));
  17636. return baseUrl;
  17637. }
  17638. function buildModuleUrlFromRequireToUrl(moduleID) {
  17639. //moduleID will be non-relative, so require it relative to this module, in Core.
  17640. return require.toUrl('../' + moduleID);
  17641. }
  17642. function buildModuleUrlFromBaseUrl(moduleID) {
  17643. return joinUrls(getCesiumBaseUrl(), moduleID);
  17644. }
  17645. var implementation;
  17646. var a;
  17647. /**
  17648. * Given a non-relative moduleID, returns an absolute URL to the file represented by that module ID,
  17649. * using, in order of preference, require.toUrl, the value of a global CESIUM_BASE_URL, or
  17650. * the base URL of the Cesium.js script.
  17651. *
  17652. * @private
  17653. */
  17654. function buildModuleUrl(moduleID) {
  17655. if (!defined(implementation)) {
  17656. //select implementation
  17657. if (defined(require.toUrl)) {
  17658. implementation = buildModuleUrlFromRequireToUrl;
  17659. } else {
  17660. implementation = buildModuleUrlFromBaseUrl;
  17661. }
  17662. }
  17663. if (!defined(a)) {
  17664. a = document.createElement('a');
  17665. }
  17666. var url = implementation(moduleID);
  17667. a.href = url;
  17668. a.href = a.href; // IE only absolutizes href on get, not set
  17669. return a.href;
  17670. }
  17671. // exposed for testing
  17672. buildModuleUrl._cesiumScriptRegex = cesiumScriptRegex;
  17673. /**
  17674. * Sets the base URL for resolving modules.
  17675. * @param {String} value The new base URL.
  17676. */
  17677. buildModuleUrl.setBaseUrl = function(value) {
  17678. baseUrl = new Uri(value).resolve(new Uri(document.location.href));
  17679. };
  17680. return buildModuleUrl;
  17681. });
  17682. /*global define*/
  17683. define('Core/Iau2006XysSample',[],function() {
  17684. 'use strict';
  17685. /**
  17686. * An IAU 2006 XYS value sampled at a particular time.
  17687. *
  17688. * @alias Iau2006XysSample
  17689. * @constructor
  17690. *
  17691. * @param {Number} x The X value.
  17692. * @param {Number} y The Y value.
  17693. * @param {Number} s The S value.
  17694. *
  17695. * @private
  17696. */
  17697. function Iau2006XysSample(x, y, s) {
  17698. /**
  17699. * The X value.
  17700. * @type {Number}
  17701. */
  17702. this.x = x;
  17703. /**
  17704. * The Y value.
  17705. * @type {Number}
  17706. */
  17707. this.y = y;
  17708. /**
  17709. * The S value.
  17710. * @type {Number}
  17711. */
  17712. this.s = s;
  17713. }
  17714. return Iau2006XysSample;
  17715. });
  17716. /*global define*/
  17717. define('Core/Iau2006XysData',[
  17718. '../ThirdParty/when',
  17719. './buildModuleUrl',
  17720. './defaultValue',
  17721. './defined',
  17722. './Iau2006XysSample',
  17723. './JulianDate',
  17724. './loadJson',
  17725. './TimeStandard'
  17726. ], function(
  17727. when,
  17728. buildModuleUrl,
  17729. defaultValue,
  17730. defined,
  17731. Iau2006XysSample,
  17732. JulianDate,
  17733. loadJson,
  17734. TimeStandard) {
  17735. 'use strict';
  17736. /**
  17737. * A set of IAU2006 XYS data that is used to evaluate the transformation between the International
  17738. * Celestial Reference Frame (ICRF) and the International Terrestrial Reference Frame (ITRF).
  17739. *
  17740. * @alias Iau2006XysData
  17741. * @constructor
  17742. *
  17743. * @param {Object} [options] Object with the following properties:
  17744. * @param {String} [options.xysFileUrlTemplate='Assets/IAU2006_XYS/IAU2006_XYS_{0}.json'] A template URL for obtaining the XYS data. In the template,
  17745. * `{0}` will be replaced with the file index.
  17746. * @param {Number} [options.interpolationOrder=9] The order of interpolation to perform on the XYS data.
  17747. * @param {Number} [options.sampleZeroJulianEphemerisDate=2442396.5] The Julian ephemeris date (JED) of the
  17748. * first XYS sample.
  17749. * @param {Number} [options.stepSizeDays=1.0] The step size, in days, between successive XYS samples.
  17750. * @param {Number} [options.samplesPerXysFile=1000] The number of samples in each XYS file.
  17751. * @param {Number} [options.totalSamples=27426] The total number of samples in all XYS files.
  17752. *
  17753. * @private
  17754. */
  17755. function Iau2006XysData(options) {
  17756. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  17757. this._xysFileUrlTemplate = options.xysFileUrlTemplate;
  17758. this._interpolationOrder = defaultValue(options.interpolationOrder, 9);
  17759. this._sampleZeroJulianEphemerisDate = defaultValue(options.sampleZeroJulianEphemerisDate, 2442396.5);
  17760. this._sampleZeroDateTT = new JulianDate(this._sampleZeroJulianEphemerisDate, 0.0, TimeStandard.TAI);
  17761. this._stepSizeDays = defaultValue(options.stepSizeDays, 1.0);
  17762. this._samplesPerXysFile = defaultValue(options.samplesPerXysFile, 1000);
  17763. this._totalSamples = defaultValue(options.totalSamples, 27426);
  17764. this._samples = new Array(this._totalSamples * 3);
  17765. this._chunkDownloadsInProgress = [];
  17766. var order = this._interpolationOrder;
  17767. // Compute denominators and X values for interpolation.
  17768. var denom = this._denominators = new Array(order + 1);
  17769. var xTable = this._xTable = new Array(order + 1);
  17770. var stepN = Math.pow(this._stepSizeDays, order);
  17771. for ( var i = 0; i <= order; ++i) {
  17772. denom[i] = stepN;
  17773. xTable[i] = i * this._stepSizeDays;
  17774. for ( var j = 0; j <= order; ++j) {
  17775. if (j !== i) {
  17776. denom[i] *= (i - j);
  17777. }
  17778. }
  17779. denom[i] = 1.0 / denom[i];
  17780. }
  17781. // Allocate scratch arrays for interpolation.
  17782. this._work = new Array(order + 1);
  17783. this._coef = new Array(order + 1);
  17784. }
  17785. var julianDateScratch = new JulianDate(0, 0.0, TimeStandard.TAI);
  17786. function getDaysSinceEpoch(xys, dayTT, secondTT) {
  17787. var dateTT = julianDateScratch;
  17788. dateTT.dayNumber = dayTT;
  17789. dateTT.secondsOfDay = secondTT;
  17790. return JulianDate.daysDifference(dateTT, xys._sampleZeroDateTT);
  17791. }
  17792. /**
  17793. * Preloads XYS data for a specified date range.
  17794. *
  17795. * @param {Number} startDayTT The Julian day number of the beginning of the interval to preload, expressed in
  17796. * the Terrestrial Time (TT) time standard.
  17797. * @param {Number} startSecondTT The seconds past noon of the beginning of the interval to preload, expressed in
  17798. * the Terrestrial Time (TT) time standard.
  17799. * @param {Number} stopDayTT The Julian day number of the end of the interval to preload, expressed in
  17800. * the Terrestrial Time (TT) time standard.
  17801. * @param {Number} stopSecondTT The seconds past noon of the end of the interval to preload, expressed in
  17802. * the Terrestrial Time (TT) time standard.
  17803. * @returns {Promise.<undefined>} A promise that, when resolved, indicates that the requested interval has been
  17804. * preloaded.
  17805. */
  17806. Iau2006XysData.prototype.preload = function(startDayTT, startSecondTT, stopDayTT, stopSecondTT) {
  17807. var startDaysSinceEpoch = getDaysSinceEpoch(this, startDayTT, startSecondTT);
  17808. var stopDaysSinceEpoch = getDaysSinceEpoch(this, stopDayTT, stopSecondTT);
  17809. var startIndex = (startDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) | 0;
  17810. if (startIndex < 0) {
  17811. startIndex = 0;
  17812. }
  17813. var stopIndex = (stopDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) | 0 + this._interpolationOrder;
  17814. if (stopIndex >= this._totalSamples) {
  17815. stopIndex = this._totalSamples - 1;
  17816. }
  17817. var startChunk = (startIndex / this._samplesPerXysFile) | 0;
  17818. var stopChunk = (stopIndex / this._samplesPerXysFile) | 0;
  17819. var promises = [];
  17820. for ( var i = startChunk; i <= stopChunk; ++i) {
  17821. promises.push(requestXysChunk(this, i));
  17822. }
  17823. return when.all(promises);
  17824. };
  17825. /**
  17826. * Computes the XYS values for a given date by interpolating. If the required data is not yet downloaded,
  17827. * this method will return undefined.
  17828. *
  17829. * @param {Number} dayTT The Julian day number for which to compute the XYS value, expressed in
  17830. * the Terrestrial Time (TT) time standard.
  17831. * @param {Number} secondTT The seconds past noon of the date for which to compute the XYS value, expressed in
  17832. * the Terrestrial Time (TT) time standard.
  17833. * @param {Iau2006XysSample} [result] The instance to which to copy the interpolated result. If this parameter
  17834. * is undefined, a new instance is allocated and returned.
  17835. * @returns {Iau2006XysSample} The interpolated XYS values, or undefined if the required data for this
  17836. * computation has not yet been downloaded.
  17837. *
  17838. * @see Iau2006XysData#preload
  17839. */
  17840. Iau2006XysData.prototype.computeXysRadians = function(dayTT, secondTT, result) {
  17841. var daysSinceEpoch = getDaysSinceEpoch(this, dayTT, secondTT);
  17842. if (daysSinceEpoch < 0.0) {
  17843. // Can't evaluate prior to the epoch of the data.
  17844. return undefined;
  17845. }
  17846. var centerIndex = (daysSinceEpoch / this._stepSizeDays) | 0;
  17847. if (centerIndex >= this._totalSamples) {
  17848. // Can't evaluate after the last sample in the data.
  17849. return undefined;
  17850. }
  17851. var degree = this._interpolationOrder;
  17852. var firstIndex = centerIndex - ((degree / 2) | 0);
  17853. if (firstIndex < 0) {
  17854. firstIndex = 0;
  17855. }
  17856. var lastIndex = firstIndex + degree;
  17857. if (lastIndex >= this._totalSamples) {
  17858. lastIndex = this._totalSamples - 1;
  17859. firstIndex = lastIndex - degree;
  17860. if (firstIndex < 0) {
  17861. firstIndex = 0;
  17862. }
  17863. }
  17864. // Are all the samples we need present?
  17865. // We can assume so if the first and last are present
  17866. var isDataMissing = false;
  17867. var samples = this._samples;
  17868. if (!defined(samples[firstIndex * 3])) {
  17869. requestXysChunk(this, (firstIndex / this._samplesPerXysFile) | 0);
  17870. isDataMissing = true;
  17871. }
  17872. if (!defined(samples[lastIndex * 3])) {
  17873. requestXysChunk(this, (lastIndex / this._samplesPerXysFile) | 0);
  17874. isDataMissing = true;
  17875. }
  17876. if (isDataMissing) {
  17877. return undefined;
  17878. }
  17879. if (!defined(result)) {
  17880. result = new Iau2006XysSample(0.0, 0.0, 0.0);
  17881. } else {
  17882. result.x = 0.0;
  17883. result.y = 0.0;
  17884. result.s = 0.0;
  17885. }
  17886. var x = daysSinceEpoch - firstIndex * this._stepSizeDays;
  17887. var work = this._work;
  17888. var denom = this._denominators;
  17889. var coef = this._coef;
  17890. var xTable = this._xTable;
  17891. var i, j;
  17892. for (i = 0; i <= degree; ++i) {
  17893. work[i] = x - xTable[i];
  17894. }
  17895. for (i = 0; i <= degree; ++i) {
  17896. coef[i] = 1.0;
  17897. for (j = 0; j <= degree; ++j) {
  17898. if (j !== i) {
  17899. coef[i] *= work[j];
  17900. }
  17901. }
  17902. coef[i] *= denom[i];
  17903. var sampleIndex = (firstIndex + i) * 3;
  17904. result.x += coef[i] * samples[sampleIndex++];
  17905. result.y += coef[i] * samples[sampleIndex++];
  17906. result.s += coef[i] * samples[sampleIndex];
  17907. }
  17908. return result;
  17909. };
  17910. function requestXysChunk(xysData, chunkIndex) {
  17911. if (xysData._chunkDownloadsInProgress[chunkIndex]) {
  17912. // Chunk has already been requested.
  17913. return xysData._chunkDownloadsInProgress[chunkIndex];
  17914. }
  17915. var deferred = when.defer();
  17916. xysData._chunkDownloadsInProgress[chunkIndex] = deferred;
  17917. var chunkUrl;
  17918. var xysFileUrlTemplate = xysData._xysFileUrlTemplate;
  17919. if (defined(xysFileUrlTemplate)) {
  17920. chunkUrl = xysFileUrlTemplate.replace('{0}', chunkIndex);
  17921. } else {
  17922. chunkUrl = buildModuleUrl('Assets/IAU2006_XYS/IAU2006_XYS_' + chunkIndex + '.json');
  17923. }
  17924. when(loadJson(chunkUrl), function(chunk) {
  17925. xysData._chunkDownloadsInProgress[chunkIndex] = false;
  17926. var samples = xysData._samples;
  17927. var newSamples = chunk.samples;
  17928. var startIndex = chunkIndex * xysData._samplesPerXysFile * 3;
  17929. for ( var i = 0, len = newSamples.length; i < len; ++i) {
  17930. samples[startIndex + i] = newSamples[i];
  17931. }
  17932. deferred.resolve();
  17933. });
  17934. return deferred.promise;
  17935. }
  17936. return Iau2006XysData;
  17937. });
  17938. /*global define*/
  17939. define('Core/Fullscreen',[
  17940. './defined',
  17941. './defineProperties'
  17942. ], function(
  17943. defined,
  17944. defineProperties) {
  17945. 'use strict';
  17946. var _supportsFullscreen;
  17947. var _names = {
  17948. requestFullscreen : undefined,
  17949. exitFullscreen : undefined,
  17950. fullscreenEnabled : undefined,
  17951. fullscreenElement : undefined,
  17952. fullscreenchange : undefined,
  17953. fullscreenerror : undefined
  17954. };
  17955. /**
  17956. * Browser-independent functions for working with the standard fullscreen API.
  17957. *
  17958. * @exports Fullscreen
  17959. *
  17960. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  17961. */
  17962. var Fullscreen = {};
  17963. defineProperties(Fullscreen, {
  17964. /**
  17965. * The element that is currently fullscreen, if any. To simply check if the
  17966. * browser is in fullscreen mode or not, use {@link Fullscreen#fullscreen}.
  17967. * @memberof Fullscreen
  17968. * @type {Object}
  17969. * @readonly
  17970. */
  17971. element : {
  17972. get : function() {
  17973. if (!Fullscreen.supportsFullscreen()) {
  17974. return undefined;
  17975. }
  17976. return document[_names.fullscreenElement];
  17977. }
  17978. },
  17979. /**
  17980. * The name of the event on the document that is fired when fullscreen is
  17981. * entered or exited. This event name is intended for use with addEventListener.
  17982. * In your event handler, to determine if the browser is in fullscreen mode or not,
  17983. * use {@link Fullscreen#fullscreen}.
  17984. * @memberof Fullscreen
  17985. * @type {String}
  17986. * @readonly
  17987. */
  17988. changeEventName : {
  17989. get : function() {
  17990. if (!Fullscreen.supportsFullscreen()) {
  17991. return undefined;
  17992. }
  17993. return _names.fullscreenchange;
  17994. }
  17995. },
  17996. /**
  17997. * The name of the event that is fired when a fullscreen error
  17998. * occurs. This event name is intended for use with addEventListener.
  17999. * @memberof Fullscreen
  18000. * @type {String}
  18001. * @readonly
  18002. */
  18003. errorEventName : {
  18004. get : function() {
  18005. if (!Fullscreen.supportsFullscreen()) {
  18006. return undefined;
  18007. }
  18008. return _names.fullscreenerror;
  18009. }
  18010. },
  18011. /**
  18012. * Determine whether the browser will allow an element to be made fullscreen, or not.
  18013. * For example, by default, iframes cannot go fullscreen unless the containing page
  18014. * adds an "allowfullscreen" attribute (or prefixed equivalent).
  18015. * @memberof Fullscreen
  18016. * @type {Boolean}
  18017. * @readonly
  18018. */
  18019. enabled : {
  18020. get : function() {
  18021. if (!Fullscreen.supportsFullscreen()) {
  18022. return undefined;
  18023. }
  18024. return document[_names.fullscreenEnabled];
  18025. }
  18026. },
  18027. /**
  18028. * Determines if the browser is currently in fullscreen mode.
  18029. * @memberof Fullscreen
  18030. * @type {Boolean}
  18031. * @readonly
  18032. */
  18033. fullscreen : {
  18034. get : function() {
  18035. if (!Fullscreen.supportsFullscreen()) {
  18036. return undefined;
  18037. }
  18038. return Fullscreen.element !== null;
  18039. }
  18040. }
  18041. });
  18042. /**
  18043. * Detects whether the browser supports the standard fullscreen API.
  18044. *
  18045. * @returns {Boolean} <code>true</code> if the browser supports the standard fullscreen API,
  18046. * <code>false</code> otherwise.
  18047. */
  18048. Fullscreen.supportsFullscreen = function() {
  18049. if (defined(_supportsFullscreen)) {
  18050. return _supportsFullscreen;
  18051. }
  18052. _supportsFullscreen = false;
  18053. var body = document.body;
  18054. if (typeof body.requestFullscreen === 'function') {
  18055. // go with the unprefixed, standard set of names
  18056. _names.requestFullscreen = 'requestFullscreen';
  18057. _names.exitFullscreen = 'exitFullscreen';
  18058. _names.fullscreenEnabled = 'fullscreenEnabled';
  18059. _names.fullscreenElement = 'fullscreenElement';
  18060. _names.fullscreenchange = 'fullscreenchange';
  18061. _names.fullscreenerror = 'fullscreenerror';
  18062. _supportsFullscreen = true;
  18063. return _supportsFullscreen;
  18064. }
  18065. //check for the correct combination of prefix plus the various names that browsers use
  18066. var prefixes = ['webkit', 'moz', 'o', 'ms', 'khtml'];
  18067. var name;
  18068. for (var i = 0, len = prefixes.length; i < len; ++i) {
  18069. var prefix = prefixes[i];
  18070. // casing of Fullscreen differs across browsers
  18071. name = prefix + 'RequestFullscreen';
  18072. if (typeof body[name] === 'function') {
  18073. _names.requestFullscreen = name;
  18074. _supportsFullscreen = true;
  18075. } else {
  18076. name = prefix + 'RequestFullScreen';
  18077. if (typeof body[name] === 'function') {
  18078. _names.requestFullscreen = name;
  18079. _supportsFullscreen = true;
  18080. }
  18081. }
  18082. // disagreement about whether it's "exit" as per spec, or "cancel"
  18083. name = prefix + 'ExitFullscreen';
  18084. if (typeof document[name] === 'function') {
  18085. _names.exitFullscreen = name;
  18086. } else {
  18087. name = prefix + 'CancelFullScreen';
  18088. if (typeof document[name] === 'function') {
  18089. _names.exitFullscreen = name;
  18090. }
  18091. }
  18092. // casing of Fullscreen differs across browsers
  18093. name = prefix + 'FullscreenEnabled';
  18094. if (document[name] !== undefined) {
  18095. _names.fullscreenEnabled = name;
  18096. } else {
  18097. name = prefix + 'FullScreenEnabled';
  18098. if (document[name] !== undefined) {
  18099. _names.fullscreenEnabled = name;
  18100. }
  18101. }
  18102. // casing of Fullscreen differs across browsers
  18103. name = prefix + 'FullscreenElement';
  18104. if (document[name] !== undefined) {
  18105. _names.fullscreenElement = name;
  18106. } else {
  18107. name = prefix + 'FullScreenElement';
  18108. if (document[name] !== undefined) {
  18109. _names.fullscreenElement = name;
  18110. }
  18111. }
  18112. // thankfully, event names are all lowercase per spec
  18113. name = prefix + 'fullscreenchange';
  18114. // event names do not have 'on' in the front, but the property on the document does
  18115. if (document['on' + name] !== undefined) {
  18116. //except on IE
  18117. if (prefix === 'ms') {
  18118. name = 'MSFullscreenChange';
  18119. }
  18120. _names.fullscreenchange = name;
  18121. }
  18122. name = prefix + 'fullscreenerror';
  18123. if (document['on' + name] !== undefined) {
  18124. //except on IE
  18125. if (prefix === 'ms') {
  18126. name = 'MSFullscreenError';
  18127. }
  18128. _names.fullscreenerror = name;
  18129. }
  18130. }
  18131. return _supportsFullscreen;
  18132. };
  18133. /**
  18134. * Asynchronously requests the browser to enter fullscreen mode on the given element.
  18135. * If fullscreen mode is not supported by the browser, does nothing.
  18136. *
  18137. * @param {Object} element The HTML element which will be placed into fullscreen mode.
  18138. * @param {HMDVRDevice} [vrDevice] The VR device.
  18139. *
  18140. * @example
  18141. * // Put the entire page into fullscreen.
  18142. * Cesium.Fullscreen.requestFullscreen(document.body)
  18143. *
  18144. * // Place only the Cesium canvas into fullscreen.
  18145. * Cesium.Fullscreen.requestFullscreen(scene.canvas)
  18146. */
  18147. Fullscreen.requestFullscreen = function(element, vrDevice) {
  18148. if (!Fullscreen.supportsFullscreen()) {
  18149. return;
  18150. }
  18151. element[_names.requestFullscreen]({ vrDisplay: vrDevice });
  18152. };
  18153. /**
  18154. * Asynchronously exits fullscreen mode. If the browser is not currently
  18155. * in fullscreen, or if fullscreen mode is not supported by the browser, does nothing.
  18156. */
  18157. Fullscreen.exitFullscreen = function() {
  18158. if (!Fullscreen.supportsFullscreen()) {
  18159. return;
  18160. }
  18161. document[_names.exitFullscreen]();
  18162. };
  18163. return Fullscreen;
  18164. });
  18165. /*global define*/
  18166. define('Core/FeatureDetection',[
  18167. './defaultValue',
  18168. './defined',
  18169. './Fullscreen'
  18170. ], function(
  18171. defaultValue,
  18172. defined,
  18173. Fullscreen) {
  18174. 'use strict';
  18175. var theNavigator;
  18176. if (typeof navigator !== 'undefined') {
  18177. theNavigator = navigator;
  18178. } else {
  18179. theNavigator = {};
  18180. }
  18181. function extractVersion(versionString) {
  18182. var parts = versionString.split('.');
  18183. for (var i = 0, len = parts.length; i < len; ++i) {
  18184. parts[i] = parseInt(parts[i], 10);
  18185. }
  18186. return parts;
  18187. }
  18188. var isChromeResult;
  18189. var chromeVersionResult;
  18190. function isChrome() {
  18191. if (!defined(isChromeResult)) {
  18192. isChromeResult = false;
  18193. // Edge contains Chrome in the user agent too
  18194. if (!isEdge()) {
  18195. var fields = (/ Chrome\/([\.0-9]+)/).exec(theNavigator.userAgent);
  18196. if (fields !== null) {
  18197. isChromeResult = true;
  18198. chromeVersionResult = extractVersion(fields[1]);
  18199. }
  18200. }
  18201. }
  18202. return isChromeResult;
  18203. }
  18204. function chromeVersion() {
  18205. return isChrome() && chromeVersionResult;
  18206. }
  18207. var isSafariResult;
  18208. var safariVersionResult;
  18209. function isSafari() {
  18210. if (!defined(isSafariResult)) {
  18211. isSafariResult = false;
  18212. // Chrome and Edge contain Safari in the user agent too
  18213. if (!isChrome() && !isEdge() && (/ Safari\/[\.0-9]+/).test(theNavigator.userAgent)) {
  18214. var fields = (/ Version\/([\.0-9]+)/).exec(theNavigator.userAgent);
  18215. if (fields !== null) {
  18216. isSafariResult = true;
  18217. safariVersionResult = extractVersion(fields[1]);
  18218. }
  18219. }
  18220. }
  18221. return isSafariResult;
  18222. }
  18223. function safariVersion() {
  18224. return isSafari() && safariVersionResult;
  18225. }
  18226. var isWebkitResult;
  18227. var webkitVersionResult;
  18228. function isWebkit() {
  18229. if (!defined(isWebkitResult)) {
  18230. isWebkitResult = false;
  18231. var fields = (/ AppleWebKit\/([\.0-9]+)(\+?)/).exec(theNavigator.userAgent);
  18232. if (fields !== null) {
  18233. isWebkitResult = true;
  18234. webkitVersionResult = extractVersion(fields[1]);
  18235. webkitVersionResult.isNightly = !!fields[2];
  18236. }
  18237. }
  18238. return isWebkitResult;
  18239. }
  18240. function webkitVersion() {
  18241. return isWebkit() && webkitVersionResult;
  18242. }
  18243. var isInternetExplorerResult;
  18244. var internetExplorerVersionResult;
  18245. function isInternetExplorer() {
  18246. if (!defined(isInternetExplorerResult)) {
  18247. isInternetExplorerResult = false;
  18248. var fields;
  18249. if (theNavigator.appName === 'Microsoft Internet Explorer') {
  18250. fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  18251. if (fields !== null) {
  18252. isInternetExplorerResult = true;
  18253. internetExplorerVersionResult = extractVersion(fields[1]);
  18254. }
  18255. } else if (theNavigator.appName === 'Netscape') {
  18256. fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  18257. if (fields !== null) {
  18258. isInternetExplorerResult = true;
  18259. internetExplorerVersionResult = extractVersion(fields[1]);
  18260. }
  18261. }
  18262. }
  18263. return isInternetExplorerResult;
  18264. }
  18265. function internetExplorerVersion() {
  18266. return isInternetExplorer() && internetExplorerVersionResult;
  18267. }
  18268. var isEdgeResult;
  18269. var edgeVersionResult;
  18270. function isEdge() {
  18271. if (!defined(isEdgeResult)) {
  18272. isEdgeResult = false;
  18273. var fields = (/ Edge\/([\.0-9]+)/).exec(theNavigator.userAgent);
  18274. if (fields !== null) {
  18275. isEdgeResult = true;
  18276. edgeVersionResult = extractVersion(fields[1]);
  18277. }
  18278. }
  18279. return isEdgeResult;
  18280. }
  18281. function edgeVersion() {
  18282. return isEdge() && edgeVersionResult;
  18283. }
  18284. var isFirefoxResult;
  18285. var firefoxVersionResult;
  18286. function isFirefox() {
  18287. if (!defined(isFirefoxResult)) {
  18288. isFirefoxResult = false;
  18289. var fields = /Firefox\/([\.0-9]+)/.exec(theNavigator.userAgent);
  18290. if (fields !== null) {
  18291. isFirefoxResult = true;
  18292. firefoxVersionResult = extractVersion(fields[1]);
  18293. }
  18294. }
  18295. return isFirefoxResult;
  18296. }
  18297. var isWindowsResult;
  18298. function isWindows() {
  18299. if (!defined(isWindowsResult)) {
  18300. isWindowsResult = /Windows/i.test(theNavigator.appVersion);
  18301. }
  18302. return isWindowsResult;
  18303. }
  18304. function firefoxVersion() {
  18305. return isFirefox() && firefoxVersionResult;
  18306. }
  18307. var hasPointerEvents;
  18308. function supportsPointerEvents() {
  18309. if (!defined(hasPointerEvents)) {
  18310. //While navigator.pointerEnabled is deprecated in the W3C specification
  18311. //we still need to use it if it exists in order to support browsers
  18312. //that rely on it, such as the Windows WebBrowser control which defines
  18313. //PointerEvent but sets navigator.pointerEnabled to false.
  18314. hasPointerEvents = typeof PointerEvent !== 'undefined' && (!defined(theNavigator.pointerEnabled) || theNavigator.pointerEnabled);
  18315. }
  18316. return hasPointerEvents;
  18317. }
  18318. var imageRenderingValueResult;
  18319. var supportsImageRenderingPixelatedResult;
  18320. function supportsImageRenderingPixelated() {
  18321. if (!defined(supportsImageRenderingPixelatedResult)) {
  18322. var canvas = document.createElement('canvas');
  18323. canvas.setAttribute('style',
  18324. 'image-rendering: -moz-crisp-edges;' +
  18325. 'image-rendering: pixelated;');
  18326. //canvas.style.imageRendering will be undefined, null or an empty string on unsupported browsers.
  18327. var tmp = canvas.style.imageRendering;
  18328. supportsImageRenderingPixelatedResult = defined(tmp) && tmp !== '';
  18329. if (supportsImageRenderingPixelatedResult) {
  18330. imageRenderingValueResult = tmp;
  18331. }
  18332. }
  18333. return supportsImageRenderingPixelatedResult;
  18334. }
  18335. function imageRenderingValue() {
  18336. return supportsImageRenderingPixelated() ? imageRenderingValueResult : undefined;
  18337. }
  18338. /**
  18339. * A set of functions to detect whether the current browser supports
  18340. * various features.
  18341. *
  18342. * @exports FeatureDetection
  18343. */
  18344. var FeatureDetection = {
  18345. isChrome : isChrome,
  18346. chromeVersion : chromeVersion,
  18347. isSafari : isSafari,
  18348. safariVersion : safariVersion,
  18349. isWebkit : isWebkit,
  18350. webkitVersion : webkitVersion,
  18351. isInternetExplorer : isInternetExplorer,
  18352. internetExplorerVersion : internetExplorerVersion,
  18353. isEdge : isEdge,
  18354. edgeVersion : edgeVersion,
  18355. isFirefox : isFirefox,
  18356. firefoxVersion : firefoxVersion,
  18357. isWindows : isWindows,
  18358. hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
  18359. supportsPointerEvents : supportsPointerEvents,
  18360. supportsImageRenderingPixelated: supportsImageRenderingPixelated,
  18361. imageRenderingValue: imageRenderingValue
  18362. };
  18363. /**
  18364. * Detects whether the current browser supports the full screen standard.
  18365. *
  18366. * @returns {Boolean} true if the browser supports the full screen standard, false if not.
  18367. *
  18368. * @see Fullscreen
  18369. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  18370. */
  18371. FeatureDetection.supportsFullscreen = function() {
  18372. return Fullscreen.supportsFullscreen();
  18373. };
  18374. /**
  18375. * Detects whether the current browser supports typed arrays.
  18376. *
  18377. * @returns {Boolean} true if the browser supports typed arrays, false if not.
  18378. *
  18379. * @see {@link http://www.khronos.org/registry/typedarray/specs/latest/|Typed Array Specification}
  18380. */
  18381. FeatureDetection.supportsTypedArrays = function() {
  18382. return typeof ArrayBuffer !== 'undefined';
  18383. };
  18384. /**
  18385. * Detects whether the current browser supports Web Workers.
  18386. *
  18387. * @returns {Boolean} true if the browsers supports Web Workers, false if not.
  18388. *
  18389. * @see {@link http://www.w3.org/TR/workers/}
  18390. */
  18391. FeatureDetection.supportsWebWorkers = function() {
  18392. return typeof Worker !== 'undefined';
  18393. };
  18394. return FeatureDetection;
  18395. });
  18396. /*global define*/
  18397. define('Core/Quaternion',[
  18398. './Cartesian3',
  18399. './defaultValue',
  18400. './defined',
  18401. './DeveloperError',
  18402. './FeatureDetection',
  18403. './freezeObject',
  18404. './Math',
  18405. './Matrix3'
  18406. ], function(
  18407. Cartesian3,
  18408. defaultValue,
  18409. defined,
  18410. DeveloperError,
  18411. FeatureDetection,
  18412. freezeObject,
  18413. CesiumMath,
  18414. Matrix3) {
  18415. 'use strict';
  18416. /**
  18417. * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
  18418. * @alias Quaternion
  18419. * @constructor
  18420. *
  18421. * @param {Number} [x=0.0] The X component.
  18422. * @param {Number} [y=0.0] The Y component.
  18423. * @param {Number} [z=0.0] The Z component.
  18424. * @param {Number} [w=0.0] The W component.
  18425. *
  18426. * @see PackableForInterpolation
  18427. */
  18428. function Quaternion(x, y, z, w) {
  18429. /**
  18430. * The X component.
  18431. * @type {Number}
  18432. * @default 0.0
  18433. */
  18434. this.x = defaultValue(x, 0.0);
  18435. /**
  18436. * The Y component.
  18437. * @type {Number}
  18438. * @default 0.0
  18439. */
  18440. this.y = defaultValue(y, 0.0);
  18441. /**
  18442. * The Z component.
  18443. * @type {Number}
  18444. * @default 0.0
  18445. */
  18446. this.z = defaultValue(z, 0.0);
  18447. /**
  18448. * The W component.
  18449. * @type {Number}
  18450. * @default 0.0
  18451. */
  18452. this.w = defaultValue(w, 0.0);
  18453. }
  18454. var fromAxisAngleScratch = new Cartesian3();
  18455. /**
  18456. * Computes a quaternion representing a rotation around an axis.
  18457. *
  18458. * @param {Cartesian3} axis The axis of rotation.
  18459. * @param {Number} angle The angle in radians to rotate around the axis.
  18460. * @param {Quaternion} [result] The object onto which to store the result.
  18461. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18462. */
  18463. Quaternion.fromAxisAngle = function(axis, angle, result) {
  18464. if (!defined(axis)) {
  18465. throw new DeveloperError('axis is required.');
  18466. }
  18467. if (typeof angle !== 'number') {
  18468. throw new DeveloperError('angle is required and must be a number.');
  18469. }
  18470. var halfAngle = angle / 2.0;
  18471. var s = Math.sin(halfAngle);
  18472. fromAxisAngleScratch = Cartesian3.normalize(axis, fromAxisAngleScratch);
  18473. var x = fromAxisAngleScratch.x * s;
  18474. var y = fromAxisAngleScratch.y * s;
  18475. var z = fromAxisAngleScratch.z * s;
  18476. var w = Math.cos(halfAngle);
  18477. if (!defined(result)) {
  18478. return new Quaternion(x, y, z, w);
  18479. }
  18480. result.x = x;
  18481. result.y = y;
  18482. result.z = z;
  18483. result.w = w;
  18484. return result;
  18485. };
  18486. var fromRotationMatrixNext = [1, 2, 0];
  18487. var fromRotationMatrixQuat = new Array(3);
  18488. /**
  18489. * Computes a Quaternion from the provided Matrix3 instance.
  18490. *
  18491. * @param {Matrix3} matrix The rotation matrix.
  18492. * @param {Quaternion} [result] The object onto which to store the result.
  18493. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18494. *
  18495. * @see Matrix3.fromQuaternion
  18496. */
  18497. Quaternion.fromRotationMatrix = function(matrix, result) {
  18498. if (!defined(matrix)) {
  18499. throw new DeveloperError('matrix is required.');
  18500. }
  18501. var root;
  18502. var x;
  18503. var y;
  18504. var z;
  18505. var w;
  18506. var m00 = matrix[Matrix3.COLUMN0ROW0];
  18507. var m11 = matrix[Matrix3.COLUMN1ROW1];
  18508. var m22 = matrix[Matrix3.COLUMN2ROW2];
  18509. var trace = m00 + m11 + m22;
  18510. if (trace > 0.0) {
  18511. // |w| > 1/2, may as well choose w > 1/2
  18512. root = Math.sqrt(trace + 1.0); // 2w
  18513. w = 0.5 * root;
  18514. root = 0.5 / root; // 1/(4w)
  18515. x = (matrix[Matrix3.COLUMN1ROW2] - matrix[Matrix3.COLUMN2ROW1]) * root;
  18516. y = (matrix[Matrix3.COLUMN2ROW0] - matrix[Matrix3.COLUMN0ROW2]) * root;
  18517. z = (matrix[Matrix3.COLUMN0ROW1] - matrix[Matrix3.COLUMN1ROW0]) * root;
  18518. } else {
  18519. // |w| <= 1/2
  18520. var next = fromRotationMatrixNext;
  18521. var i = 0;
  18522. if (m11 > m00) {
  18523. i = 1;
  18524. }
  18525. if (m22 > m00 && m22 > m11) {
  18526. i = 2;
  18527. }
  18528. var j = next[i];
  18529. var k = next[j];
  18530. root = Math.sqrt(matrix[Matrix3.getElementIndex(i, i)] - matrix[Matrix3.getElementIndex(j, j)] - matrix[Matrix3.getElementIndex(k, k)] + 1.0);
  18531. var quat = fromRotationMatrixQuat;
  18532. quat[i] = 0.5 * root;
  18533. root = 0.5 / root;
  18534. w = (matrix[Matrix3.getElementIndex(k, j)] - matrix[Matrix3.getElementIndex(j, k)]) * root;
  18535. quat[j] = (matrix[Matrix3.getElementIndex(j, i)] + matrix[Matrix3.getElementIndex(i, j)]) * root;
  18536. quat[k] = (matrix[Matrix3.getElementIndex(k, i)] + matrix[Matrix3.getElementIndex(i, k)]) * root;
  18537. x = -quat[0];
  18538. y = -quat[1];
  18539. z = -quat[2];
  18540. }
  18541. if (!defined(result)) {
  18542. return new Quaternion(x, y, z, w);
  18543. }
  18544. result.x = x;
  18545. result.y = y;
  18546. result.z = z;
  18547. result.w = w;
  18548. return result;
  18549. };
  18550. var scratchHPRQuaternion = new Quaternion();
  18551. /**
  18552. * Computes a rotation from the given heading, pitch and roll angles. Heading is the rotation about the
  18553. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  18554. * the positive x axis.
  18555. *
  18556. * @param {Number} heading The heading angle in radians.
  18557. * @param {Number} pitch The pitch angle in radians.
  18558. * @param {Number} roll The roll angle in radians.
  18559. * @param {Quaternion} [result] The object onto which to store the result.
  18560. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  18561. */
  18562. Quaternion.fromHeadingPitchRoll = function(heading, pitch, roll, result) {
  18563. if (!defined(heading)) {
  18564. throw new DeveloperError('heading is required.');
  18565. }
  18566. if (!defined(pitch)) {
  18567. throw new DeveloperError('pitch is required.');
  18568. }
  18569. if (!defined(roll)) {
  18570. throw new DeveloperError('roll is required.');
  18571. }
  18572. var rollQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_X, roll, scratchHPRQuaternion);
  18573. var pitchQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_Y, -pitch, result);
  18574. result = Quaternion.multiply(pitchQuaternion, rollQuaternion, pitchQuaternion);
  18575. var headingQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_Z, -heading, scratchHPRQuaternion);
  18576. return Quaternion.multiply(headingQuaternion, result, result);
  18577. };
  18578. var sampledQuaternionAxis = new Cartesian3();
  18579. var sampledQuaternionRotation = new Cartesian3();
  18580. var sampledQuaternionTempQuaternion = new Quaternion();
  18581. var sampledQuaternionQuaternion0 = new Quaternion();
  18582. var sampledQuaternionQuaternion0Conjugate = new Quaternion();
  18583. /**
  18584. * The number of elements used to pack the object into an array.
  18585. * @type {Number}
  18586. */
  18587. Quaternion.packedLength = 4;
  18588. /**
  18589. * Stores the provided instance into the provided array.
  18590. *
  18591. * @param {Quaternion} value The value to pack.
  18592. * @param {Number[]} array The array to pack into.
  18593. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  18594. *
  18595. * @returns {Number[]} The array that was packed into
  18596. */
  18597. Quaternion.pack = function(value, array, startingIndex) {
  18598. if (!defined(value)) {
  18599. throw new DeveloperError('value is required');
  18600. }
  18601. if (!defined(array)) {
  18602. throw new DeveloperError('array is required');
  18603. }
  18604. startingIndex = defaultValue(startingIndex, 0);
  18605. array[startingIndex++] = value.x;
  18606. array[startingIndex++] = value.y;
  18607. array[startingIndex++] = value.z;
  18608. array[startingIndex] = value.w;
  18609. return array;
  18610. };
  18611. /**
  18612. * Retrieves an instance from a packed array.
  18613. *
  18614. * @param {Number[]} array The packed array.
  18615. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  18616. * @param {Quaternion} [result] The object into which to store the result.
  18617. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18618. */
  18619. Quaternion.unpack = function(array, startingIndex, result) {
  18620. if (!defined(array)) {
  18621. throw new DeveloperError('array is required');
  18622. }
  18623. startingIndex = defaultValue(startingIndex, 0);
  18624. if (!defined(result)) {
  18625. result = new Quaternion();
  18626. }
  18627. result.x = array[startingIndex];
  18628. result.y = array[startingIndex + 1];
  18629. result.z = array[startingIndex + 2];
  18630. result.w = array[startingIndex + 3];
  18631. return result;
  18632. };
  18633. /**
  18634. * The number of elements used to store the object into an array in its interpolatable form.
  18635. * @type {Number}
  18636. */
  18637. Quaternion.packedInterpolationLength = 3;
  18638. /**
  18639. * Converts a packed array into a form suitable for interpolation.
  18640. *
  18641. * @param {Number[]} packedArray The packed array.
  18642. * @param {Number} [startingIndex=0] The index of the first element to be converted.
  18643. * @param {Number} [lastIndex=packedArray.length] The index of the last element to be converted.
  18644. * @param {Number[]} result The object into which to store the result.
  18645. */
  18646. Quaternion.convertPackedArrayForInterpolation = function(packedArray, startingIndex, lastIndex, result) {
  18647. Quaternion.unpack(packedArray, lastIndex * 4, sampledQuaternionQuaternion0Conjugate);
  18648. Quaternion.conjugate(sampledQuaternionQuaternion0Conjugate, sampledQuaternionQuaternion0Conjugate);
  18649. for (var i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  18650. var offset = i * 3;
  18651. Quaternion.unpack(packedArray, (startingIndex + i) * 4, sampledQuaternionTempQuaternion);
  18652. Quaternion.multiply(sampledQuaternionTempQuaternion, sampledQuaternionQuaternion0Conjugate, sampledQuaternionTempQuaternion);
  18653. if (sampledQuaternionTempQuaternion.w < 0) {
  18654. Quaternion.negate(sampledQuaternionTempQuaternion, sampledQuaternionTempQuaternion);
  18655. }
  18656. Quaternion.computeAxis(sampledQuaternionTempQuaternion, sampledQuaternionAxis);
  18657. var angle = Quaternion.computeAngle(sampledQuaternionTempQuaternion);
  18658. result[offset] = sampledQuaternionAxis.x * angle;
  18659. result[offset + 1] = sampledQuaternionAxis.y * angle;
  18660. result[offset + 2] = sampledQuaternionAxis.z * angle;
  18661. }
  18662. };
  18663. /**
  18664. * Retrieves an instance from a packed array converted with {@link convertPackedArrayForInterpolation}.
  18665. *
  18666. * @param {Number[]} array The array previously packed for interpolation.
  18667. * @param {Number[]} sourceArray The original packed array.
  18668. * @param {Number} [startingIndex=0] The startingIndex used to convert the array.
  18669. * @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  18670. * @param {Quaternion} [result] The object into which to store the result.
  18671. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18672. */
  18673. Quaternion.unpackInterpolationResult = function(array, sourceArray, firstIndex, lastIndex, result) {
  18674. if (!defined(result)) {
  18675. result = new Quaternion();
  18676. }
  18677. Cartesian3.fromArray(array, 0, sampledQuaternionRotation);
  18678. var magnitude = Cartesian3.magnitude(sampledQuaternionRotation);
  18679. Quaternion.unpack(sourceArray, lastIndex * 4, sampledQuaternionQuaternion0);
  18680. if (magnitude === 0) {
  18681. Quaternion.clone(Quaternion.IDENTITY, sampledQuaternionTempQuaternion);
  18682. } else {
  18683. Quaternion.fromAxisAngle(sampledQuaternionRotation, magnitude, sampledQuaternionTempQuaternion);
  18684. }
  18685. return Quaternion.multiply(sampledQuaternionTempQuaternion, sampledQuaternionQuaternion0, result);
  18686. };
  18687. /**
  18688. * Duplicates a Quaternion instance.
  18689. *
  18690. * @param {Quaternion} quaternion The quaternion to duplicate.
  18691. * @param {Quaternion} [result] The object onto which to store the result.
  18692. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided. (Returns undefined if quaternion is undefined)
  18693. */
  18694. Quaternion.clone = function(quaternion, result) {
  18695. if (!defined(quaternion)) {
  18696. return undefined;
  18697. }
  18698. if (!defined(result)) {
  18699. return new Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
  18700. }
  18701. result.x = quaternion.x;
  18702. result.y = quaternion.y;
  18703. result.z = quaternion.z;
  18704. result.w = quaternion.w;
  18705. return result;
  18706. };
  18707. /**
  18708. * Computes the conjugate of the provided quaternion.
  18709. *
  18710. * @param {Quaternion} quaternion The quaternion to conjugate.
  18711. * @param {Quaternion} result The object onto which to store the result.
  18712. * @returns {Quaternion} The modified result parameter.
  18713. */
  18714. Quaternion.conjugate = function(quaternion, result) {
  18715. if (!defined(quaternion)) {
  18716. throw new DeveloperError('quaternion is required');
  18717. }
  18718. if (!defined(result)) {
  18719. throw new DeveloperError('result is required');
  18720. }
  18721. result.x = -quaternion.x;
  18722. result.y = -quaternion.y;
  18723. result.z = -quaternion.z;
  18724. result.w = quaternion.w;
  18725. return result;
  18726. };
  18727. /**
  18728. * Computes magnitude squared for the provided quaternion.
  18729. *
  18730. * @param {Quaternion} quaternion The quaternion to conjugate.
  18731. * @returns {Number} The magnitude squared.
  18732. */
  18733. Quaternion.magnitudeSquared = function(quaternion) {
  18734. if (!defined(quaternion)) {
  18735. throw new DeveloperError('quaternion is required');
  18736. }
  18737. return quaternion.x * quaternion.x + quaternion.y * quaternion.y + quaternion.z * quaternion.z + quaternion.w * quaternion.w;
  18738. };
  18739. /**
  18740. * Computes magnitude for the provided quaternion.
  18741. *
  18742. * @param {Quaternion} quaternion The quaternion to conjugate.
  18743. * @returns {Number} The magnitude.
  18744. */
  18745. Quaternion.magnitude = function(quaternion) {
  18746. return Math.sqrt(Quaternion.magnitudeSquared(quaternion));
  18747. };
  18748. /**
  18749. * Computes the normalized form of the provided quaternion.
  18750. *
  18751. * @param {Quaternion} quaternion The quaternion to normalize.
  18752. * @param {Quaternion} result The object onto which to store the result.
  18753. * @returns {Quaternion} The modified result parameter.
  18754. */
  18755. Quaternion.normalize = function(quaternion, result) {
  18756. if (!defined(result)) {
  18757. throw new DeveloperError('result is required');
  18758. }
  18759. var inverseMagnitude = 1.0 / Quaternion.magnitude(quaternion);
  18760. var x = quaternion.x * inverseMagnitude;
  18761. var y = quaternion.y * inverseMagnitude;
  18762. var z = quaternion.z * inverseMagnitude;
  18763. var w = quaternion.w * inverseMagnitude;
  18764. result.x = x;
  18765. result.y = y;
  18766. result.z = z;
  18767. result.w = w;
  18768. return result;
  18769. };
  18770. /**
  18771. * Computes the inverse of the provided quaternion.
  18772. *
  18773. * @param {Quaternion} quaternion The quaternion to normalize.
  18774. * @param {Quaternion} result The object onto which to store the result.
  18775. * @returns {Quaternion} The modified result parameter.
  18776. */
  18777. Quaternion.inverse = function(quaternion, result) {
  18778. if (!defined(result)) {
  18779. throw new DeveloperError('result is required');
  18780. }
  18781. var magnitudeSquared = Quaternion.magnitudeSquared(quaternion);
  18782. result = Quaternion.conjugate(quaternion, result);
  18783. return Quaternion.multiplyByScalar(result, 1.0 / magnitudeSquared, result);
  18784. };
  18785. /**
  18786. * Computes the componentwise sum of two quaternions.
  18787. *
  18788. * @param {Quaternion} left The first quaternion.
  18789. * @param {Quaternion} right The second quaternion.
  18790. * @param {Quaternion} result The object onto which to store the result.
  18791. * @returns {Quaternion} The modified result parameter.
  18792. */
  18793. Quaternion.add = function(left, right, result) {
  18794. if (!defined(left)) {
  18795. throw new DeveloperError('left is required');
  18796. }
  18797. if (!defined(right)) {
  18798. throw new DeveloperError('right is required');
  18799. }
  18800. if (!defined(result)) {
  18801. throw new DeveloperError('result is required');
  18802. }
  18803. result.x = left.x + right.x;
  18804. result.y = left.y + right.y;
  18805. result.z = left.z + right.z;
  18806. result.w = left.w + right.w;
  18807. return result;
  18808. };
  18809. /**
  18810. * Computes the componentwise difference of two quaternions.
  18811. *
  18812. * @param {Quaternion} left The first quaternion.
  18813. * @param {Quaternion} right The second quaternion.
  18814. * @param {Quaternion} result The object onto which to store the result.
  18815. * @returns {Quaternion} The modified result parameter.
  18816. */
  18817. Quaternion.subtract = function(left, right, result) {
  18818. if (!defined(left)) {
  18819. throw new DeveloperError('left is required');
  18820. }
  18821. if (!defined(right)) {
  18822. throw new DeveloperError('right is required');
  18823. }
  18824. if (!defined(result)) {
  18825. throw new DeveloperError('result is required');
  18826. }
  18827. result.x = left.x - right.x;
  18828. result.y = left.y - right.y;
  18829. result.z = left.z - right.z;
  18830. result.w = left.w - right.w;
  18831. return result;
  18832. };
  18833. /**
  18834. * Negates the provided quaternion.
  18835. *
  18836. * @param {Quaternion} quaternion The quaternion to be negated.
  18837. * @param {Quaternion} result The object onto which to store the result.
  18838. * @returns {Quaternion} The modified result parameter.
  18839. */
  18840. Quaternion.negate = function(quaternion, result) {
  18841. if (!defined(quaternion)) {
  18842. throw new DeveloperError('quaternion is required');
  18843. }
  18844. if (!defined(result)) {
  18845. throw new DeveloperError('result is required');
  18846. }
  18847. result.x = -quaternion.x;
  18848. result.y = -quaternion.y;
  18849. result.z = -quaternion.z;
  18850. result.w = -quaternion.w;
  18851. return result;
  18852. };
  18853. /**
  18854. * Computes the dot (scalar) product of two quaternions.
  18855. *
  18856. * @param {Quaternion} left The first quaternion.
  18857. * @param {Quaternion} right The second quaternion.
  18858. * @returns {Number} The dot product.
  18859. */
  18860. Quaternion.dot = function(left, right) {
  18861. if (!defined(left)) {
  18862. throw new DeveloperError('left is required');
  18863. }
  18864. if (!defined(right)) {
  18865. throw new DeveloperError('right is required');
  18866. }
  18867. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  18868. };
  18869. /**
  18870. * Computes the product of two quaternions.
  18871. *
  18872. * @param {Quaternion} left The first quaternion.
  18873. * @param {Quaternion} right The second quaternion.
  18874. * @param {Quaternion} result The object onto which to store the result.
  18875. * @returns {Quaternion} The modified result parameter.
  18876. */
  18877. Quaternion.multiply = function(left, right, result) {
  18878. if (!defined(left)) {
  18879. throw new DeveloperError('left is required');
  18880. }
  18881. if (!defined(right)) {
  18882. throw new DeveloperError('right is required');
  18883. }
  18884. if (!defined(result)) {
  18885. throw new DeveloperError('result is required');
  18886. }
  18887. var leftX = left.x;
  18888. var leftY = left.y;
  18889. var leftZ = left.z;
  18890. var leftW = left.w;
  18891. var rightX = right.x;
  18892. var rightY = right.y;
  18893. var rightZ = right.z;
  18894. var rightW = right.w;
  18895. var x = leftW * rightX + leftX * rightW + leftY * rightZ - leftZ * rightY;
  18896. var y = leftW * rightY - leftX * rightZ + leftY * rightW + leftZ * rightX;
  18897. var z = leftW * rightZ + leftX * rightY - leftY * rightX + leftZ * rightW;
  18898. var w = leftW * rightW - leftX * rightX - leftY * rightY - leftZ * rightZ;
  18899. result.x = x;
  18900. result.y = y;
  18901. result.z = z;
  18902. result.w = w;
  18903. return result;
  18904. };
  18905. /**
  18906. * Multiplies the provided quaternion componentwise by the provided scalar.
  18907. *
  18908. * @param {Quaternion} quaternion The quaternion to be scaled.
  18909. * @param {Number} scalar The scalar to multiply with.
  18910. * @param {Quaternion} result The object onto which to store the result.
  18911. * @returns {Quaternion} The modified result parameter.
  18912. */
  18913. Quaternion.multiplyByScalar = function(quaternion, scalar, result) {
  18914. if (!defined(quaternion)) {
  18915. throw new DeveloperError('quaternion is required');
  18916. }
  18917. if (typeof scalar !== 'number') {
  18918. throw new DeveloperError('scalar is required and must be a number.');
  18919. }
  18920. if (!defined(result)) {
  18921. throw new DeveloperError('result is required');
  18922. }
  18923. result.x = quaternion.x * scalar;
  18924. result.y = quaternion.y * scalar;
  18925. result.z = quaternion.z * scalar;
  18926. result.w = quaternion.w * scalar;
  18927. return result;
  18928. };
  18929. /**
  18930. * Divides the provided quaternion componentwise by the provided scalar.
  18931. *
  18932. * @param {Quaternion} quaternion The quaternion to be divided.
  18933. * @param {Number} scalar The scalar to divide by.
  18934. * @param {Quaternion} result The object onto which to store the result.
  18935. * @returns {Quaternion} The modified result parameter.
  18936. */
  18937. Quaternion.divideByScalar = function(quaternion, scalar, result) {
  18938. if (!defined(quaternion)) {
  18939. throw new DeveloperError('quaternion is required');
  18940. }
  18941. if (typeof scalar !== 'number') {
  18942. throw new DeveloperError('scalar is required and must be a number.');
  18943. }
  18944. if (!defined(result)) {
  18945. throw new DeveloperError('result is required');
  18946. }
  18947. result.x = quaternion.x / scalar;
  18948. result.y = quaternion.y / scalar;
  18949. result.z = quaternion.z / scalar;
  18950. result.w = quaternion.w / scalar;
  18951. return result;
  18952. };
  18953. /**
  18954. * Computes the axis of rotation of the provided quaternion.
  18955. *
  18956. * @param {Quaternion} quaternion The quaternion to use.
  18957. * @param {Cartesian3} result The object onto which to store the result.
  18958. * @returns {Cartesian3} The modified result parameter.
  18959. */
  18960. Quaternion.computeAxis = function(quaternion, result) {
  18961. if (!defined(quaternion)) {
  18962. throw new DeveloperError('quaternion is required');
  18963. }
  18964. if (!defined(result)) {
  18965. throw new DeveloperError('result is required');
  18966. }
  18967. var w = quaternion.w;
  18968. if (Math.abs(w - 1.0) < CesiumMath.EPSILON6) {
  18969. result.x = result.y = result.z = 0;
  18970. return result;
  18971. }
  18972. var scalar = 1.0 / Math.sqrt(1.0 - (w * w));
  18973. result.x = quaternion.x * scalar;
  18974. result.y = quaternion.y * scalar;
  18975. result.z = quaternion.z * scalar;
  18976. return result;
  18977. };
  18978. /**
  18979. * Computes the angle of rotation of the provided quaternion.
  18980. *
  18981. * @param {Quaternion} quaternion The quaternion to use.
  18982. * @returns {Number} The angle of rotation.
  18983. */
  18984. Quaternion.computeAngle = function(quaternion) {
  18985. if (!defined(quaternion)) {
  18986. throw new DeveloperError('quaternion is required');
  18987. }
  18988. if (Math.abs(quaternion.w - 1.0) < CesiumMath.EPSILON6) {
  18989. return 0.0;
  18990. }
  18991. return 2.0 * Math.acos(quaternion.w);
  18992. };
  18993. var lerpScratch = new Quaternion();
  18994. /**
  18995. * Computes the linear interpolation or extrapolation at t using the provided quaternions.
  18996. *
  18997. * @param {Quaternion} start The value corresponding to t at 0.0.
  18998. * @param {Quaternion} end The value corresponding to t at 1.0.
  18999. * @param {Number} t The point along t at which to interpolate.
  19000. * @param {Quaternion} result The object onto which to store the result.
  19001. * @returns {Quaternion} The modified result parameter.
  19002. */
  19003. Quaternion.lerp = function(start, end, t, result) {
  19004. if (!defined(start)) {
  19005. throw new DeveloperError('start is required.');
  19006. }
  19007. if (!defined(end)) {
  19008. throw new DeveloperError('end is required.');
  19009. }
  19010. if (typeof t !== 'number') {
  19011. throw new DeveloperError('t is required and must be a number.');
  19012. }
  19013. if (!defined(result)) {
  19014. throw new DeveloperError('result is required');
  19015. }
  19016. lerpScratch = Quaternion.multiplyByScalar(end, t, lerpScratch);
  19017. result = Quaternion.multiplyByScalar(start, 1.0 - t, result);
  19018. return Quaternion.add(lerpScratch, result, result);
  19019. };
  19020. var slerpEndNegated = new Quaternion();
  19021. var slerpScaledP = new Quaternion();
  19022. var slerpScaledR = new Quaternion();
  19023. /**
  19024. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  19025. *
  19026. * @param {Quaternion} start The value corresponding to t at 0.0.
  19027. * @param {Quaternion} end The value corresponding to t at 1.0.
  19028. * @param {Number} t The point along t at which to interpolate.
  19029. * @param {Quaternion} result The object onto which to store the result.
  19030. * @returns {Quaternion} The modified result parameter.
  19031. *
  19032. * @see Quaternion#fastSlerp
  19033. */
  19034. Quaternion.slerp = function(start, end, t, result) {
  19035. if (!defined(start)) {
  19036. throw new DeveloperError('start is required.');
  19037. }
  19038. if (!defined(end)) {
  19039. throw new DeveloperError('end is required.');
  19040. }
  19041. if (typeof t !== 'number') {
  19042. throw new DeveloperError('t is required and must be a number.');
  19043. }
  19044. if (!defined(result)) {
  19045. throw new DeveloperError('result is required');
  19046. }
  19047. var dot = Quaternion.dot(start, end);
  19048. // The angle between start must be acute. Since q and -q represent
  19049. // the same rotation, negate q to get the acute angle.
  19050. var r = end;
  19051. if (dot < 0.0) {
  19052. dot = -dot;
  19053. r = slerpEndNegated = Quaternion.negate(end, slerpEndNegated);
  19054. }
  19055. // dot > 0, as the dot product approaches 1, the angle between the
  19056. // quaternions vanishes. use linear interpolation.
  19057. if (1.0 - dot < CesiumMath.EPSILON6) {
  19058. return Quaternion.lerp(start, r, t, result);
  19059. }
  19060. var theta = Math.acos(dot);
  19061. slerpScaledP = Quaternion.multiplyByScalar(start, Math.sin((1 - t) * theta), slerpScaledP);
  19062. slerpScaledR = Quaternion.multiplyByScalar(r, Math.sin(t * theta), slerpScaledR);
  19063. result = Quaternion.add(slerpScaledP, slerpScaledR, result);
  19064. return Quaternion.multiplyByScalar(result, 1.0 / Math.sin(theta), result);
  19065. };
  19066. /**
  19067. * The logarithmic quaternion function.
  19068. *
  19069. * @param {Quaternion} quaternion The unit quaternion.
  19070. * @param {Cartesian3} result The object onto which to store the result.
  19071. * @returns {Cartesian3} The modified result parameter.
  19072. */
  19073. Quaternion.log = function(quaternion, result) {
  19074. if (!defined(quaternion)) {
  19075. throw new DeveloperError('quaternion is required.');
  19076. }
  19077. if (!defined(result)) {
  19078. throw new DeveloperError('result is required');
  19079. }
  19080. var theta = CesiumMath.acosClamped(quaternion.w);
  19081. var thetaOverSinTheta = 0.0;
  19082. if (theta !== 0.0) {
  19083. thetaOverSinTheta = theta / Math.sin(theta);
  19084. }
  19085. return Cartesian3.multiplyByScalar(quaternion, thetaOverSinTheta, result);
  19086. };
  19087. /**
  19088. * The exponential quaternion function.
  19089. *
  19090. * @param {Cartesian3} cartesian The cartesian.
  19091. * @param {Quaternion} result The object onto which to store the result.
  19092. * @returns {Quaternion} The modified result parameter.
  19093. */
  19094. Quaternion.exp = function(cartesian, result) {
  19095. if (!defined(cartesian)) {
  19096. throw new DeveloperError('cartesian is required.');
  19097. }
  19098. if (!defined(result)) {
  19099. throw new DeveloperError('result is required');
  19100. }
  19101. var theta = Cartesian3.magnitude(cartesian);
  19102. var sinThetaOverTheta = 0.0;
  19103. if (theta !== 0.0) {
  19104. sinThetaOverTheta = Math.sin(theta) / theta;
  19105. }
  19106. result.x = cartesian.x * sinThetaOverTheta;
  19107. result.y = cartesian.y * sinThetaOverTheta;
  19108. result.z = cartesian.z * sinThetaOverTheta;
  19109. result.w = Math.cos(theta);
  19110. return result;
  19111. };
  19112. var squadScratchCartesian0 = new Cartesian3();
  19113. var squadScratchCartesian1 = new Cartesian3();
  19114. var squadScratchQuaternion0 = new Quaternion();
  19115. var squadScratchQuaternion1 = new Quaternion();
  19116. /**
  19117. * Computes an inner quadrangle point.
  19118. * <p>This will compute quaternions that ensure a squad curve is C<sup>1</sup>.</p>
  19119. *
  19120. * @param {Quaternion} q0 The first quaternion.
  19121. * @param {Quaternion} q1 The second quaternion.
  19122. * @param {Quaternion} q2 The third quaternion.
  19123. * @param {Quaternion} result The object onto which to store the result.
  19124. * @returns {Quaternion} The modified result parameter.
  19125. *
  19126. * @see Quaternion#squad
  19127. */
  19128. Quaternion.computeInnerQuadrangle = function(q0, q1, q2, result) {
  19129. if (!defined(q0) || !defined(q1) || !defined(q2)) {
  19130. throw new DeveloperError('q0, q1, and q2 are required.');
  19131. }
  19132. if (!defined(result)) {
  19133. throw new DeveloperError('result is required');
  19134. }
  19135. var qInv = Quaternion.conjugate(q1, squadScratchQuaternion0);
  19136. Quaternion.multiply(qInv, q2, squadScratchQuaternion1);
  19137. var cart0 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian0);
  19138. Quaternion.multiply(qInv, q0, squadScratchQuaternion1);
  19139. var cart1 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian1);
  19140. Cartesian3.add(cart0, cart1, cart0);
  19141. Cartesian3.multiplyByScalar(cart0, 0.25, cart0);
  19142. Cartesian3.negate(cart0, cart0);
  19143. Quaternion.exp(cart0, squadScratchQuaternion0);
  19144. return Quaternion.multiply(q1, squadScratchQuaternion0, result);
  19145. };
  19146. /**
  19147. * Computes the spherical quadrangle interpolation between quaternions.
  19148. *
  19149. * @param {Quaternion} q0 The first quaternion.
  19150. * @param {Quaternion} q1 The second quaternion.
  19151. * @param {Quaternion} s0 The first inner quadrangle.
  19152. * @param {Quaternion} s1 The second inner quadrangle.
  19153. * @param {Number} t The time in [0,1] used to interpolate.
  19154. * @param {Quaternion} result The object onto which to store the result.
  19155. * @returns {Quaternion} The modified result parameter.
  19156. *
  19157. *
  19158. * @example
  19159. * // 1. compute the squad interpolation between two quaternions on a curve
  19160. * var s0 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i - 1], quaternions[i], quaternions[i + 1], new Cesium.Quaternion());
  19161. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i], quaternions[i + 1], quaternions[i + 2], new Cesium.Quaternion());
  19162. * var q = Cesium.Quaternion.squad(quaternions[i], quaternions[i + 1], s0, s1, t, new Cesium.Quaternion());
  19163. *
  19164. * // 2. compute the squad interpolation as above but where the first quaternion is a end point.
  19165. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[0], quaternions[1], quaternions[2], new Cesium.Quaternion());
  19166. * var q = Cesium.Quaternion.squad(quaternions[0], quaternions[1], quaternions[0], s1, t, new Cesium.Quaternion());
  19167. *
  19168. * @see Quaternion#computeInnerQuadrangle
  19169. */
  19170. Quaternion.squad = function(q0, q1, s0, s1, t, result) {
  19171. if (!defined(q0) || !defined(q1) || !defined(s0) || !defined(s1)) {
  19172. throw new DeveloperError('q0, q1, s0, and s1 are required.');
  19173. }
  19174. if (typeof t !== 'number') {
  19175. throw new DeveloperError('t is required and must be a number.');
  19176. }
  19177. if (!defined(result)) {
  19178. throw new DeveloperError('result is required');
  19179. }
  19180. var slerp0 = Quaternion.slerp(q0, q1, t, squadScratchQuaternion0);
  19181. var slerp1 = Quaternion.slerp(s0, s1, t, squadScratchQuaternion1);
  19182. return Quaternion.slerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  19183. };
  19184. var fastSlerpScratchQuaternion = new Quaternion();
  19185. var opmu = 1.90110745351730037;
  19186. var u = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19187. var v = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19188. var bT = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19189. var bD = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19190. for (var i = 0; i < 7; ++i) {
  19191. var s = i + 1.0;
  19192. var t = 2.0 * s + 1.0;
  19193. u[i] = 1.0 / (s * t);
  19194. v[i] = s / t;
  19195. }
  19196. u[7] = opmu / (8.0 * 17.0);
  19197. v[7] = opmu * 8.0 / 17.0;
  19198. /**
  19199. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  19200. * This implementation is faster than {@link Quaternion#slerp}, but is only accurate up to 10<sup>-6</sup>.
  19201. *
  19202. * @param {Quaternion} start The value corresponding to t at 0.0.
  19203. * @param {Quaternion} end The value corresponding to t at 1.0.
  19204. * @param {Number} t The point along t at which to interpolate.
  19205. * @param {Quaternion} result The object onto which to store the result.
  19206. * @returns {Quaternion} The modified result parameter.
  19207. *
  19208. * @see Quaternion#slerp
  19209. */
  19210. Quaternion.fastSlerp = function(start, end, t, result) {
  19211. if (!defined(start)) {
  19212. throw new DeveloperError('start is required.');
  19213. }
  19214. if (!defined(end)) {
  19215. throw new DeveloperError('end is required.');
  19216. }
  19217. if (typeof t !== 'number') {
  19218. throw new DeveloperError('t is required and must be a number.');
  19219. }
  19220. if (!defined(result)) {
  19221. throw new DeveloperError('result is required');
  19222. }
  19223. var x = Quaternion.dot(start, end);
  19224. var sign;
  19225. if (x >= 0) {
  19226. sign = 1.0;
  19227. } else {
  19228. sign = -1.0;
  19229. x = -x;
  19230. }
  19231. var xm1 = x - 1.0;
  19232. var d = 1.0 - t;
  19233. var sqrT = t * t;
  19234. var sqrD = d * d;
  19235. for (var i = 7; i >= 0; --i) {
  19236. bT[i] = (u[i] * sqrT - v[i]) * xm1;
  19237. bD[i] = (u[i] * sqrD - v[i]) * xm1;
  19238. }
  19239. var cT = sign * t * (
  19240. 1.0 + bT[0] * (1.0 + bT[1] * (1.0 + bT[2] * (1.0 + bT[3] * (
  19241. 1.0 + bT[4] * (1.0 + bT[5] * (1.0 + bT[6] * (1.0 + bT[7]))))))));
  19242. var cD = d * (
  19243. 1.0 + bD[0] * (1.0 + bD[1] * (1.0 + bD[2] * (1.0 + bD[3] * (
  19244. 1.0 + bD[4] * (1.0 + bD[5] * (1.0 + bD[6] * (1.0 + bD[7]))))))));
  19245. var temp = Quaternion.multiplyByScalar(start, cD, fastSlerpScratchQuaternion);
  19246. Quaternion.multiplyByScalar(end, cT, result);
  19247. return Quaternion.add(temp, result, result);
  19248. };
  19249. /**
  19250. * Computes the spherical quadrangle interpolation between quaternions.
  19251. * An implementation that is faster than {@link Quaternion#squad}, but less accurate.
  19252. *
  19253. * @param {Quaternion} q0 The first quaternion.
  19254. * @param {Quaternion} q1 The second quaternion.
  19255. * @param {Quaternion} s0 The first inner quadrangle.
  19256. * @param {Quaternion} s1 The second inner quadrangle.
  19257. * @param {Number} t The time in [0,1] used to interpolate.
  19258. * @param {Quaternion} result The object onto which to store the result.
  19259. * @returns {Quaternion} The modified result parameter or a new instance if none was provided.
  19260. *
  19261. * @see Quaternion#squad
  19262. */
  19263. Quaternion.fastSquad = function(q0, q1, s0, s1, t, result) {
  19264. if (!defined(q0) || !defined(q1) || !defined(s0) || !defined(s1)) {
  19265. throw new DeveloperError('q0, q1, s0, and s1 are required.');
  19266. }
  19267. if (typeof t !== 'number') {
  19268. throw new DeveloperError('t is required and must be a number.');
  19269. }
  19270. if (!defined(result)) {
  19271. throw new DeveloperError('result is required');
  19272. }
  19273. var slerp0 = Quaternion.fastSlerp(q0, q1, t, squadScratchQuaternion0);
  19274. var slerp1 = Quaternion.fastSlerp(s0, s1, t, squadScratchQuaternion1);
  19275. return Quaternion.fastSlerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  19276. };
  19277. /**
  19278. * Compares the provided quaternions componentwise and returns
  19279. * <code>true</code> if they are equal, <code>false</code> otherwise.
  19280. *
  19281. * @param {Quaternion} [left] The first quaternion.
  19282. * @param {Quaternion} [right] The second quaternion.
  19283. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  19284. */
  19285. Quaternion.equals = function(left, right) {
  19286. return (left === right) ||
  19287. ((defined(left)) &&
  19288. (defined(right)) &&
  19289. (left.x === right.x) &&
  19290. (left.y === right.y) &&
  19291. (left.z === right.z) &&
  19292. (left.w === right.w));
  19293. };
  19294. /**
  19295. * Compares the provided quaternions componentwise and returns
  19296. * <code>true</code> if they are within the provided epsilon,
  19297. * <code>false</code> otherwise.
  19298. *
  19299. * @param {Quaternion} [left] The first quaternion.
  19300. * @param {Quaternion} [right] The second quaternion.
  19301. * @param {Number} epsilon The epsilon to use for equality testing.
  19302. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  19303. */
  19304. Quaternion.equalsEpsilon = function(left, right, epsilon) {
  19305. if (typeof epsilon !== 'number') {
  19306. throw new DeveloperError('epsilon is required and must be a number.');
  19307. }
  19308. return (left === right) ||
  19309. ((defined(left)) &&
  19310. (defined(right)) &&
  19311. (Math.abs(left.x - right.x) <= epsilon) &&
  19312. (Math.abs(left.y - right.y) <= epsilon) &&
  19313. (Math.abs(left.z - right.z) <= epsilon) &&
  19314. (Math.abs(left.w - right.w) <= epsilon));
  19315. };
  19316. /**
  19317. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 0.0).
  19318. *
  19319. * @type {Quaternion}
  19320. * @constant
  19321. */
  19322. Quaternion.ZERO = freezeObject(new Quaternion(0.0, 0.0, 0.0, 0.0));
  19323. /**
  19324. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 1.0).
  19325. *
  19326. * @type {Quaternion}
  19327. * @constant
  19328. */
  19329. Quaternion.IDENTITY = freezeObject(new Quaternion(0.0, 0.0, 0.0, 1.0));
  19330. /**
  19331. * Duplicates this Quaternion instance.
  19332. *
  19333. * @param {Quaternion} [result] The object onto which to store the result.
  19334. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  19335. */
  19336. Quaternion.prototype.clone = function(result) {
  19337. return Quaternion.clone(this, result);
  19338. };
  19339. /**
  19340. * Compares this and the provided quaternion componentwise and returns
  19341. * <code>true</code> if they are equal, <code>false</code> otherwise.
  19342. *
  19343. * @param {Quaternion} [right] The right hand side quaternion.
  19344. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  19345. */
  19346. Quaternion.prototype.equals = function(right) {
  19347. return Quaternion.equals(this, right);
  19348. };
  19349. /**
  19350. * Compares this and the provided quaternion componentwise and returns
  19351. * <code>true</code> if they are within the provided epsilon,
  19352. * <code>false</code> otherwise.
  19353. *
  19354. * @param {Quaternion} [right] The right hand side quaternion.
  19355. * @param {Number} epsilon The epsilon to use for equality testing.
  19356. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  19357. */
  19358. Quaternion.prototype.equalsEpsilon = function(right, epsilon) {
  19359. return Quaternion.equalsEpsilon(this, right, epsilon);
  19360. };
  19361. /**
  19362. * Returns a string representing this quaternion in the format (x, y, z, w).
  19363. *
  19364. * @returns {String} A string representing this Quaternion.
  19365. */
  19366. Quaternion.prototype.toString = function() {
  19367. return '(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  19368. };
  19369. return Quaternion;
  19370. });
  19371. /*global define*/
  19372. define('Core/Transforms',[
  19373. '../ThirdParty/when',
  19374. './Cartesian2',
  19375. './Cartesian3',
  19376. './Cartesian4',
  19377. './Cartographic',
  19378. './defaultValue',
  19379. './defined',
  19380. './deprecationWarning',
  19381. './DeveloperError',
  19382. './EarthOrientationParameters',
  19383. './EarthOrientationParametersSample',
  19384. './Ellipsoid',
  19385. './HeadingPitchRoll',
  19386. './Iau2006XysData',
  19387. './Iau2006XysSample',
  19388. './JulianDate',
  19389. './Math',
  19390. './Matrix3',
  19391. './Matrix4',
  19392. './Quaternion',
  19393. './TimeConstants'
  19394. ], function(
  19395. when,
  19396. Cartesian2,
  19397. Cartesian3,
  19398. Cartesian4,
  19399. Cartographic,
  19400. defaultValue,
  19401. defined,
  19402. deprecationWarning,
  19403. DeveloperError,
  19404. EarthOrientationParameters,
  19405. EarthOrientationParametersSample,
  19406. Ellipsoid,
  19407. HeadingPitchRoll,
  19408. Iau2006XysData,
  19409. Iau2006XysSample,
  19410. JulianDate,
  19411. CesiumMath,
  19412. Matrix3,
  19413. Matrix4,
  19414. Quaternion,
  19415. TimeConstants) {
  19416. 'use strict';
  19417. /**
  19418. * Contains functions for transforming positions to various reference frames.
  19419. *
  19420. * @exports Transforms
  19421. */
  19422. var Transforms = {};
  19423. var eastNorthUpToFixedFrameNormal = new Cartesian3();
  19424. var eastNorthUpToFixedFrameTangent = new Cartesian3();
  19425. var eastNorthUpToFixedFrameBitangent = new Cartesian3();
  19426. /**
  19427. * Computes a 4x4 transformation matrix from a reference frame with an east-north-up axes
  19428. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19429. * The local axes are defined as:
  19430. * <ul>
  19431. * <li>The <code>x</code> axis points in the local east direction.</li>
  19432. * <li>The <code>y</code> axis points in the local north direction.</li>
  19433. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  19434. * </ul>
  19435. *
  19436. * @param {Cartesian3} origin The center point of the local reference frame.
  19437. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19438. * @param {Matrix4} [result] The object onto which to store the result.
  19439. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19440. *
  19441. * @example
  19442. * // Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame.
  19443. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19444. * var transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);
  19445. */
  19446. Transforms.eastNorthUpToFixedFrame = function(origin, ellipsoid, result) {
  19447. if (!defined(origin)) {
  19448. throw new DeveloperError('origin is required.');
  19449. }
  19450. // If x and y are zero, assume origin is at a pole, which is a special case.
  19451. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19452. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19453. var sign = CesiumMath.sign(origin.z);
  19454. if (!defined(result)) {
  19455. return new Matrix4(
  19456. 0.0, -sign, 0.0, origin.x,
  19457. 1.0, 0.0, 0.0, origin.y,
  19458. 0.0, 0.0, sign, origin.z,
  19459. 0.0, 0.0, 0.0, 1.0);
  19460. }
  19461. result[0] = 0.0;
  19462. result[1] = 1.0;
  19463. result[2] = 0.0;
  19464. result[3] = 0.0;
  19465. result[4] = -sign;
  19466. result[5] = 0.0;
  19467. result[6] = 0.0;
  19468. result[7] = 0.0;
  19469. result[8] = 0.0;
  19470. result[9] = 0.0;
  19471. result[10] = sign;
  19472. result[11] = 0.0;
  19473. result[12] = origin.x;
  19474. result[13] = origin.y;
  19475. result[14] = origin.z;
  19476. result[15] = 1.0;
  19477. return result;
  19478. }
  19479. var normal = eastNorthUpToFixedFrameNormal;
  19480. var tangent = eastNorthUpToFixedFrameTangent;
  19481. var bitangent = eastNorthUpToFixedFrameBitangent;
  19482. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19483. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19484. tangent.x = -origin.y;
  19485. tangent.y = origin.x;
  19486. tangent.z = 0.0;
  19487. Cartesian3.normalize(tangent, tangent);
  19488. Cartesian3.cross(normal, tangent, bitangent);
  19489. if (!defined(result)) {
  19490. return new Matrix4(
  19491. tangent.x, bitangent.x, normal.x, origin.x,
  19492. tangent.y, bitangent.y, normal.y, origin.y,
  19493. tangent.z, bitangent.z, normal.z, origin.z,
  19494. 0.0, 0.0, 0.0, 1.0);
  19495. }
  19496. result[0] = tangent.x;
  19497. result[1] = tangent.y;
  19498. result[2] = tangent.z;
  19499. result[3] = 0.0;
  19500. result[4] = bitangent.x;
  19501. result[5] = bitangent.y;
  19502. result[6] = bitangent.z;
  19503. result[7] = 0.0;
  19504. result[8] = normal.x;
  19505. result[9] = normal.y;
  19506. result[10] = normal.z;
  19507. result[11] = 0.0;
  19508. result[12] = origin.x;
  19509. result[13] = origin.y;
  19510. result[14] = origin.z;
  19511. result[15] = 1.0;
  19512. return result;
  19513. };
  19514. var northEastDownToFixedFrameNormal = new Cartesian3();
  19515. var northEastDownToFixedFrameTangent = new Cartesian3();
  19516. var northEastDownToFixedFrameBitangent = new Cartesian3();
  19517. /**
  19518. * Computes a 4x4 transformation matrix from a reference frame with an north-east-down axes
  19519. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19520. * The local axes are defined as:
  19521. * <ul>
  19522. * <li>The <code>x</code> axis points in the local north direction.</li>
  19523. * <li>The <code>y</code> axis points in the local east direction.</li>
  19524. * <li>The <code>z</code> axis points in the opposite direction of the ellipsoid surface normal which passes through the position.</li>
  19525. * </ul>
  19526. *
  19527. * @param {Cartesian3} origin The center point of the local reference frame.
  19528. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19529. * @param {Matrix4} [result] The object onto which to store the result.
  19530. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19531. *
  19532. * @example
  19533. * // Get the transform from local north-east-down at cartographic (0.0, 0.0) to Earth's fixed frame.
  19534. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19535. * var transform = Cesium.Transforms.northEastDownToFixedFrame(center);
  19536. */
  19537. Transforms.northEastDownToFixedFrame = function(origin, ellipsoid, result) {
  19538. if (!defined(origin)) {
  19539. throw new DeveloperError('origin is required.');
  19540. }
  19541. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19542. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19543. // The poles are special cases. If x and y are zero, assume origin is at a pole.
  19544. var sign = CesiumMath.sign(origin.z);
  19545. if (!defined(result)) {
  19546. return new Matrix4(
  19547. -sign, 0.0, 0.0, origin.x,
  19548. 0.0, 1.0, 0.0, origin.y,
  19549. 0.0, 0.0, -sign, origin.z,
  19550. 0.0, 0.0, 0.0, 1.0);
  19551. }
  19552. result[0] = -sign;
  19553. result[1] = 0.0;
  19554. result[2] = 0.0;
  19555. result[3] = 0.0;
  19556. result[4] = 0.0;
  19557. result[5] = 1.0;
  19558. result[6] = 0.0;
  19559. result[7] = 0.0;
  19560. result[8] = 0.0;
  19561. result[9] = 0.0;
  19562. result[10] = -sign;
  19563. result[11] = 0.0;
  19564. result[12] = origin.x;
  19565. result[13] = origin.y;
  19566. result[14] = origin.z;
  19567. result[15] = 1.0;
  19568. return result;
  19569. }
  19570. var normal = northEastDownToFixedFrameNormal;
  19571. var tangent = northEastDownToFixedFrameTangent;
  19572. var bitangent = northEastDownToFixedFrameBitangent;
  19573. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19574. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19575. tangent.x = -origin.y;
  19576. tangent.y = origin.x;
  19577. tangent.z = 0.0;
  19578. Cartesian3.normalize(tangent, tangent);
  19579. Cartesian3.cross(normal, tangent, bitangent);
  19580. if (!defined(result)) {
  19581. return new Matrix4(
  19582. bitangent.x, tangent.x, -normal.x, origin.x,
  19583. bitangent.y, tangent.y, -normal.y, origin.y,
  19584. bitangent.z, tangent.z, -normal.z, origin.z,
  19585. 0.0, 0.0, 0.0, 1.0);
  19586. }
  19587. result[0] = bitangent.x;
  19588. result[1] = bitangent.y;
  19589. result[2] = bitangent.z;
  19590. result[3] = 0.0;
  19591. result[4] = tangent.x;
  19592. result[5] = tangent.y;
  19593. result[6] = tangent.z;
  19594. result[7] = 0.0;
  19595. result[8] = -normal.x;
  19596. result[9] = -normal.y;
  19597. result[10] = -normal.z;
  19598. result[11] = 0.0;
  19599. result[12] = origin.x;
  19600. result[13] = origin.y;
  19601. result[14] = origin.z;
  19602. result[15] = 1.0;
  19603. return result;
  19604. };
  19605. /**
  19606. * Computes a 4x4 transformation matrix from a reference frame with an north-up-east axes
  19607. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19608. * The local axes are defined as:
  19609. * <ul>
  19610. * <li>The <code>x</code> axis points in the local north direction.</li>
  19611. * <li>The <code>y</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  19612. * <li>The <code>z</code> axis points in the local east direction.</li>
  19613. * </ul>
  19614. *
  19615. * @param {Cartesian3} origin The center point of the local reference frame.
  19616. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19617. * @param {Matrix4} [result] The object onto which to store the result.
  19618. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19619. *
  19620. * @example
  19621. * // Get the transform from local north-up-east at cartographic (0.0, 0.0) to Earth's fixed frame.
  19622. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19623. * var transform = Cesium.Transforms.northUpEastToFixedFrame(center);
  19624. */
  19625. Transforms.northUpEastToFixedFrame = function(origin, ellipsoid, result) {
  19626. if (!defined(origin)) {
  19627. throw new DeveloperError('origin is required.');
  19628. }
  19629. // If x and y are zero, assume origin is at a pole, which is a special case.
  19630. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19631. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19632. var sign = CesiumMath.sign(origin.z);
  19633. if (!defined(result)) {
  19634. return new Matrix4(
  19635. -sign, 0.0, 0.0, origin.x,
  19636. 0.0, 0.0, 1.0, origin.y,
  19637. 0.0, sign, 0.0, origin.z,
  19638. 0.0, 0.0, 0.0, 1.0);
  19639. }
  19640. result[0] = -sign;
  19641. result[1] = 0.0;
  19642. result[2] = 0.0;
  19643. result[3] = 0.0;
  19644. result[4] = 0.0;
  19645. result[5] = 0.0;
  19646. result[6] = sign;
  19647. result[7] = 0.0;
  19648. result[8] = 0.0;
  19649. result[9] = 1.0;
  19650. result[10] = 0.0;
  19651. result[11] = 0.0;
  19652. result[12] = origin.x;
  19653. result[13] = origin.y;
  19654. result[14] = origin.z;
  19655. result[15] = 1.0;
  19656. return result;
  19657. }
  19658. var normal = eastNorthUpToFixedFrameNormal;
  19659. var tangent = eastNorthUpToFixedFrameTangent;
  19660. var bitangent = eastNorthUpToFixedFrameBitangent;
  19661. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19662. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19663. tangent.x = -origin.y;
  19664. tangent.y = origin.x;
  19665. tangent.z = 0.0;
  19666. Cartesian3.normalize(tangent, tangent);
  19667. Cartesian3.cross(normal, tangent, bitangent);
  19668. if (!defined(result)) {
  19669. return new Matrix4(
  19670. bitangent.x, normal.x, tangent.x, origin.x,
  19671. bitangent.y, normal.y, tangent.y, origin.y,
  19672. bitangent.z, normal.z, tangent.z, origin.z,
  19673. 0.0, 0.0, 0.0, 1.0);
  19674. }
  19675. result[0] = bitangent.x;
  19676. result[1] = bitangent.y;
  19677. result[2] = bitangent.z;
  19678. result[3] = 0.0;
  19679. result[4] = normal.x;
  19680. result[5] = normal.y;
  19681. result[6] = normal.z;
  19682. result[7] = 0.0;
  19683. result[8] = tangent.x;
  19684. result[9] = tangent.y;
  19685. result[10] = tangent.z;
  19686. result[11] = 0.0;
  19687. result[12] = origin.x;
  19688. result[13] = origin.y;
  19689. result[14] = origin.z;
  19690. result[15] = 1.0;
  19691. return result;
  19692. };
  19693. /**
  19694. * Computes a 4x4 transformation matrix from a reference frame with an north-west-up axes
  19695. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19696. * The local axes are defined as:
  19697. * <ul>
  19698. * <li>The <code>x</code> axis points in the local north direction.</li>
  19699. * <li>The <code>y</code> axis points in the local west direction.</li>
  19700. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  19701. * </ul>
  19702. *
  19703. * @param {Cartesian3} origin The center point of the local reference frame.
  19704. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19705. * @param {Matrix4} [result] The object onto which to store the result.
  19706. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19707. *
  19708. * @example
  19709. * // Get the transform from local north-West-Up at cartographic (0.0, 0.0) to Earth's fixed frame.
  19710. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19711. * var transform = Cesium.Transforms.northWestUpToFixedFrame(center);
  19712. */
  19713. Transforms.northWestUpToFixedFrame = function(origin, ellipsoid, result) {
  19714. if (!defined(origin)) {
  19715. throw new DeveloperError('origin is required.');
  19716. }
  19717. // If x and y are zero, assume origin is at a pole, which is a special case.
  19718. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19719. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19720. var sign = CesiumMath.sign(origin.z);
  19721. if (!defined(result)) {
  19722. return new Matrix4(
  19723. -sign, 0.0, 0.0, origin.x,
  19724. 0.0, -1.0, 0.0, origin.y,
  19725. 0.0, 0.0, sign, origin.z,
  19726. 0.0, 0.0, 0.0, 1.0);
  19727. }
  19728. result[0] = -sign;
  19729. result[1] = 0.0;
  19730. result[2] = 0.0;
  19731. result[3] = 0.0;
  19732. result[4] = 0.0;
  19733. result[5] = -1.0;
  19734. result[6] = 0.0;
  19735. result[7] = 0.0;
  19736. result[8] = 0.0;
  19737. result[9] = 0.0;
  19738. result[10] = sign;
  19739. result[11] = 0.0;
  19740. result[12] = origin.x;
  19741. result[13] = origin.y;
  19742. result[14] = origin.z;
  19743. result[15] = 1.0;
  19744. return result;
  19745. }
  19746. var normal = eastNorthUpToFixedFrameNormal;//Up
  19747. var tangent = eastNorthUpToFixedFrameTangent;//East
  19748. var bitangent = eastNorthUpToFixedFrameBitangent;//North
  19749. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19750. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19751. tangent.x = -origin.y;
  19752. tangent.y = origin.x;
  19753. tangent.z = 0.0;
  19754. Cartesian3.normalize(tangent, tangent);
  19755. Cartesian3.cross(normal, tangent, bitangent);
  19756. if (!defined(result)) {
  19757. return new Matrix4(
  19758. bitangent.x, -tangent.x, normal.x, origin.x,
  19759. bitangent.y, -tangent.y, normal.y, origin.y,
  19760. bitangent.z, -tangent.z, normal.z, origin.z,
  19761. 0.0, 0.0, 0.0, 1.0);
  19762. }
  19763. result[0] = bitangent.x;
  19764. result[1] = bitangent.y;
  19765. result[2] = bitangent.z;
  19766. result[3] = 0.0;
  19767. result[4] = -tangent.x;
  19768. result[5] = -tangent.y;
  19769. result[6] = -tangent.z;
  19770. result[7] = 0.0;
  19771. result[8] = normal.x;
  19772. result[9] = normal.y;
  19773. result[10] = normal.z;
  19774. result[11] = 0.0;
  19775. result[12] = origin.x;
  19776. result[13] = origin.y;
  19777. result[14] = origin.z;
  19778. result[15] = 1.0;
  19779. return result;
  19780. };
  19781. var scratchHPRQuaternion = new Quaternion();
  19782. var scratchScale = new Cartesian3(1.0, 1.0, 1.0);
  19783. var scratchHPRMatrix4 = new Matrix4();
  19784. /**
  19785. * Computes a 4x4 transformation matrix from a reference frame with axes computed from the heading-pitch-roll angles
  19786. * centered at the provided origin to the provided ellipsoid's fixed reference frame. Heading is the rotation from the local north
  19787. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  19788. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  19789. *
  19790. * @param {Cartesian3} origin The center point of the local reference frame.
  19791. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  19792. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19793. * @param {Matrix4} [result] The object onto which to store the result.
  19794. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19795. *
  19796. * @example
  19797. * // Get the transform from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  19798. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19799. * var heading = -Cesium.Math.PI_OVER_TWO;
  19800. * var pitch = Cesium.Math.PI_OVER_FOUR;
  19801. * var roll = 0.0;
  19802. * var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  19803. * var transform = Cesium.Transforms.headingPitchRollToFixedFrame(center, hpr);
  19804. */
  19805. Transforms.headingPitchRollToFixedFrame = function(origin, headingPitchRoll, pitch, roll, ellipsoid, result) {
  19806. var heading;
  19807. if (typeof headingPitchRoll === 'object') {
  19808. // Shift arguments using assignments to encourage JIT optimization.
  19809. ellipsoid = pitch;
  19810. result = roll;
  19811. heading = headingPitchRoll.heading;
  19812. pitch = headingPitchRoll.pitch;
  19813. roll = headingPitchRoll.roll;
  19814. } else {
  19815. deprecationWarning('headingPitchRollToFixedFrame', 'headingPitchRollToFixedFrame with separate heading, pitch, and roll arguments was deprecated in 1.27. It will be removed in 1.30. Use a HeadingPitchRoll object.');
  19816. heading = headingPitchRoll;
  19817. }
  19818. // checks for required parameters happen in the called functions
  19819. var hprQuaternion = Quaternion.fromHeadingPitchRoll(heading, pitch, roll, scratchHPRQuaternion);
  19820. var hprMatrix = Matrix4.fromTranslationQuaternionRotationScale(Cartesian3.ZERO, hprQuaternion, scratchScale, scratchHPRMatrix4);
  19821. result = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result);
  19822. return Matrix4.multiply(result, hprMatrix, result);
  19823. };
  19824. var scratchHPR = new HeadingPitchRoll();
  19825. var scratchENUMatrix4 = new Matrix4();
  19826. var scratchHPRMatrix3 = new Matrix3();
  19827. /**
  19828. * Computes a quaternion from a reference frame with axes computed from the heading-pitch-roll angles
  19829. * centered at the provided origin. Heading is the rotation from the local north
  19830. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  19831. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  19832. *
  19833. * @param {Cartesian3} origin The center point of the local reference frame.
  19834. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  19835. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19836. * @param {Quaternion} [result] The object onto which to store the result.
  19837. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  19838. *
  19839. * @example
  19840. * // Get the quaternion from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  19841. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19842. * var heading = -Cesium.Math.PI_OVER_TWO;
  19843. * var pitch = Cesium.Math.PI_OVER_FOUR;
  19844. * var roll = 0.0;
  19845. * var hpr = new HeadingPitchRoll(heading, pitch, roll);
  19846. * var quaternion = Cesium.Transforms.headingPitchRollQuaternion(center, hpr);
  19847. */
  19848. Transforms.headingPitchRollQuaternion = function(origin, headingPitchRoll, pitch, roll, ellipsoid, result) {
  19849. var hpr;
  19850. if (typeof headingPitchRoll === 'object') {
  19851. // Shift arguments using assignment to encourage JIT optimization.
  19852. hpr = headingPitchRoll;
  19853. ellipsoid = pitch;
  19854. result = roll;
  19855. } else {
  19856. deprecationWarning('headingPitchRollQuaternion', 'headingPitchRollQuaternion with separate heading, pitch, and roll arguments was deprecated in 1.27. It will be removed in 1.30. Use a HeadingPitchRoll object.');
  19857. scratchHPR.heading = headingPitchRoll;
  19858. scratchHPR.pitch = pitch;
  19859. scratchHPR.roll = roll;
  19860. hpr = scratchHPR;
  19861. }
  19862. // checks for required parameters happen in the called functions
  19863. var transform = Transforms.headingPitchRollToFixedFrame(origin, hpr, ellipsoid, scratchENUMatrix4);
  19864. var rotation = Matrix4.getRotation(transform, scratchHPRMatrix3);
  19865. return Quaternion.fromRotationMatrix(rotation, result);
  19866. };
  19867. var gmstConstant0 = 6 * 3600 + 41 * 60 + 50.54841;
  19868. var gmstConstant1 = 8640184.812866;
  19869. var gmstConstant2 = 0.093104;
  19870. var gmstConstant3 = -6.2E-6;
  19871. var rateCoef = 1.1772758384668e-19;
  19872. var wgs84WRPrecessing = 7.2921158553E-5;
  19873. var twoPiOverSecondsInDay = CesiumMath.TWO_PI / 86400.0;
  19874. var dateInUtc = new JulianDate();
  19875. /**
  19876. * Computes a rotation matrix to transform a point or vector from True Equator Mean Equinox (TEME) axes to the
  19877. * pseudo-fixed axes at a given time. This method treats the UT1 time standard as equivalent to UTC.
  19878. *
  19879. * @param {JulianDate} date The time at which to compute the rotation matrix.
  19880. * @param {Matrix3} [result] The object onto which to store the result.
  19881. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if none was provided.
  19882. *
  19883. * @example
  19884. * //Set the view to in the inertial frame.
  19885. * scene.preRender.addEventListener(function(scene, time) {
  19886. * var now = Cesium.JulianDate.now();
  19887. * var offset = Cesium.Matrix4.multiplyByPoint(camera.transform, camera.position, new Cesium.Cartesian3());
  19888. * var transform = Cesium.Matrix4.fromRotationTranslation(Cesium.Transforms.computeTemeToPseudoFixedMatrix(now));
  19889. * var inverseTransform = Cesium.Matrix4.inverseTransformation(transform, new Cesium.Matrix4());
  19890. * Cesium.Matrix4.multiplyByPoint(inverseTransform, offset, offset);
  19891. * camera.lookAtTransform(transform, offset);
  19892. * });
  19893. */
  19894. Transforms.computeTemeToPseudoFixedMatrix = function (date, result) {
  19895. if (!defined(date)) {
  19896. throw new DeveloperError('date is required.');
  19897. }
  19898. // GMST is actually computed using UT1. We're using UTC as an approximation of UT1.
  19899. // We do not want to use the function like convertTaiToUtc in JulianDate because
  19900. // we explicitly do not want to fail when inside the leap second.
  19901. dateInUtc = JulianDate.addSeconds(date, -JulianDate.computeTaiMinusUtc(date), dateInUtc);
  19902. var utcDayNumber = dateInUtc.dayNumber;
  19903. var utcSecondsIntoDay = dateInUtc.secondsOfDay;
  19904. var t;
  19905. var diffDays = utcDayNumber - 2451545;
  19906. if (utcSecondsIntoDay >= 43200.0) {
  19907. t = (diffDays + 0.5) / TimeConstants.DAYS_PER_JULIAN_CENTURY;
  19908. } else {
  19909. t = (diffDays - 0.5) / TimeConstants.DAYS_PER_JULIAN_CENTURY;
  19910. }
  19911. var gmst0 = gmstConstant0 + t * (gmstConstant1 + t * (gmstConstant2 + t * gmstConstant3));
  19912. var angle = (gmst0 * twoPiOverSecondsInDay) % CesiumMath.TWO_PI;
  19913. var ratio = wgs84WRPrecessing + rateCoef * (utcDayNumber - 2451545.5);
  19914. var secondsSinceMidnight = (utcSecondsIntoDay + TimeConstants.SECONDS_PER_DAY * 0.5) % TimeConstants.SECONDS_PER_DAY;
  19915. var gha = angle + (ratio * secondsSinceMidnight);
  19916. var cosGha = Math.cos(gha);
  19917. var sinGha = Math.sin(gha);
  19918. if (!defined(result)) {
  19919. return new Matrix3(cosGha, sinGha, 0.0,
  19920. -sinGha, cosGha, 0.0,
  19921. 0.0, 0.0, 1.0);
  19922. }
  19923. result[0] = cosGha;
  19924. result[1] = -sinGha;
  19925. result[2] = 0.0;
  19926. result[3] = sinGha;
  19927. result[4] = cosGha;
  19928. result[5] = 0.0;
  19929. result[6] = 0.0;
  19930. result[7] = 0.0;
  19931. result[8] = 1.0;
  19932. return result;
  19933. };
  19934. /**
  19935. * The source of IAU 2006 XYS data, used for computing the transformation between the
  19936. * Fixed and ICRF axes.
  19937. * @type {Iau2006XysData}
  19938. *
  19939. * @see Transforms.computeIcrfToFixedMatrix
  19940. * @see Transforms.computeFixedToIcrfMatrix
  19941. *
  19942. * @private
  19943. */
  19944. Transforms.iau2006XysData = new Iau2006XysData();
  19945. /**
  19946. * The source of Earth Orientation Parameters (EOP) data, used for computing the transformation
  19947. * between the Fixed and ICRF axes. By default, zero values are used for all EOP values,
  19948. * yielding a reasonable but not completely accurate representation of the ICRF axes.
  19949. * @type {EarthOrientationParameters}
  19950. *
  19951. * @see Transforms.computeIcrfToFixedMatrix
  19952. * @see Transforms.computeFixedToIcrfMatrix
  19953. *
  19954. * @private
  19955. */
  19956. Transforms.earthOrientationParameters = EarthOrientationParameters.NONE;
  19957. var ttMinusTai = 32.184;
  19958. var j2000ttDays = 2451545.0;
  19959. /**
  19960. * Preloads the data necessary to transform between the ICRF and Fixed axes, in either
  19961. * direction, over a given interval. This function returns a promise that, when resolved,
  19962. * indicates that the preload has completed.
  19963. *
  19964. * @param {TimeInterval} timeInterval The interval to preload.
  19965. * @returns {Promise.<undefined>} A promise that, when resolved, indicates that the preload has completed
  19966. * and evaluation of the transformation between the fixed and ICRF axes will
  19967. * no longer return undefined for a time inside the interval.
  19968. *
  19969. *
  19970. * @example
  19971. * var interval = new Cesium.TimeInterval(...);
  19972. * when(Cesium.Transforms.preloadIcrfFixed(interval), function() {
  19973. * // the data is now loaded
  19974. * });
  19975. *
  19976. * @see Transforms.computeIcrfToFixedMatrix
  19977. * @see Transforms.computeFixedToIcrfMatrix
  19978. * @see when
  19979. */
  19980. Transforms.preloadIcrfFixed = function(timeInterval) {
  19981. var startDayTT = timeInterval.start.dayNumber;
  19982. var startSecondTT = timeInterval.start.secondsOfDay + ttMinusTai;
  19983. var stopDayTT = timeInterval.stop.dayNumber;
  19984. var stopSecondTT = timeInterval.stop.secondsOfDay + ttMinusTai;
  19985. var xysPromise = Transforms.iau2006XysData.preload(startDayTT, startSecondTT, stopDayTT, stopSecondTT);
  19986. var eopPromise = Transforms.earthOrientationParameters.getPromiseToLoad();
  19987. return when.all([xysPromise, eopPromise]);
  19988. };
  19989. /**
  19990. * Computes a rotation matrix to transform a point or vector from the International Celestial
  19991. * Reference Frame (GCRF/ICRF) inertial frame axes to the Earth-Fixed frame axes (ITRF)
  19992. * at a given time. This function may return undefined if the data necessary to
  19993. * do the transformation is not yet loaded.
  19994. *
  19995. * @param {JulianDate} date The time at which to compute the rotation matrix.
  19996. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  19997. * not specified, a new instance is created and returned.
  19998. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  19999. * transformation is not yet loaded.
  20000. *
  20001. *
  20002. * @example
  20003. * scene.preRender.addEventListener(function(scene, time) {
  20004. * var icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time);
  20005. * if (Cesium.defined(icrfToFixed)) {
  20006. * var offset = Cesium.Matrix4.multiplyByPoint(camera.transform, camera.position, new Cesium.Cartesian3());
  20007. * var transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed)
  20008. * var inverseTransform = Cesium.Matrix4.inverseTransformation(transform, new Cesium.Matrix4());
  20009. * Cesium.Matrix4.multiplyByPoint(inverseTransform, offset, offset);
  20010. * camera.lookAtTransform(transform, offset);
  20011. * }
  20012. * });
  20013. *
  20014. * @see Transforms.preloadIcrfFixed
  20015. */
  20016. Transforms.computeIcrfToFixedMatrix = function(date, result) {
  20017. if (!defined(date)) {
  20018. throw new DeveloperError('date is required.');
  20019. }
  20020. if (!defined(result)) {
  20021. result = new Matrix3();
  20022. }
  20023. var fixedToIcrfMtx = Transforms.computeFixedToIcrfMatrix(date, result);
  20024. if (!defined(fixedToIcrfMtx)) {
  20025. return undefined;
  20026. }
  20027. return Matrix3.transpose(fixedToIcrfMtx, result);
  20028. };
  20029. var xysScratch = new Iau2006XysSample(0.0, 0.0, 0.0);
  20030. var eopScratch = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  20031. var rotation1Scratch = new Matrix3();
  20032. var rotation2Scratch = new Matrix3();
  20033. /**
  20034. * Computes a rotation matrix to transform a point or vector from the Earth-Fixed frame axes (ITRF)
  20035. * to the International Celestial Reference Frame (GCRF/ICRF) inertial frame axes
  20036. * at a given time. This function may return undefined if the data necessary to
  20037. * do the transformation is not yet loaded.
  20038. *
  20039. * @param {JulianDate} date The time at which to compute the rotation matrix.
  20040. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  20041. * not specified, a new instance is created and returned.
  20042. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  20043. * transformation is not yet loaded.
  20044. *
  20045. *
  20046. * @example
  20047. * // Transform a point from the ICRF axes to the Fixed axes.
  20048. * var now = Cesium.JulianDate.now();
  20049. * var pointInFixed = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  20050. * var fixedToIcrf = Cesium.Transforms.computeIcrfToFixedMatrix(now);
  20051. * var pointInInertial = new Cesium.Cartesian3();
  20052. * if (Cesium.defined(fixedToIcrf)) {
  20053. * pointInInertial = Cesium.Matrix3.multiplyByVector(fixedToIcrf, pointInFixed, pointInInertial);
  20054. * }
  20055. *
  20056. * @see Transforms.preloadIcrfFixed
  20057. */
  20058. Transforms.computeFixedToIcrfMatrix = function(date, result) {
  20059. if (!defined(date)) {
  20060. throw new DeveloperError('date is required.');
  20061. }
  20062. if (!defined(result)) {
  20063. result = new Matrix3();
  20064. }
  20065. // Compute pole wander
  20066. var eop = Transforms.earthOrientationParameters.compute(date, eopScratch);
  20067. if (!defined(eop)) {
  20068. return undefined;
  20069. }
  20070. // There is no external conversion to Terrestrial Time (TT).
  20071. // So use International Atomic Time (TAI) and convert using offsets.
  20072. // Here we are assuming that dayTT and secondTT are positive
  20073. var dayTT = date.dayNumber;
  20074. // It's possible here that secondTT could roll over 86400
  20075. // This does not seem to affect the precision (unit tests check for this)
  20076. var secondTT = date.secondsOfDay + ttMinusTai;
  20077. var xys = Transforms.iau2006XysData.computeXysRadians(dayTT, secondTT, xysScratch);
  20078. if (!defined(xys)) {
  20079. return undefined;
  20080. }
  20081. var x = xys.x + eop.xPoleOffset;
  20082. var y = xys.y + eop.yPoleOffset;
  20083. // Compute XYS rotation
  20084. var a = 1.0 / (1.0 + Math.sqrt(1.0 - x * x - y * y));
  20085. var rotation1 = rotation1Scratch;
  20086. rotation1[0] = 1.0 - a * x * x;
  20087. rotation1[3] = -a * x * y;
  20088. rotation1[6] = x;
  20089. rotation1[1] = -a * x * y;
  20090. rotation1[4] = 1 - a * y * y;
  20091. rotation1[7] = y;
  20092. rotation1[2] = -x;
  20093. rotation1[5] = -y;
  20094. rotation1[8] = 1 - a * (x * x + y * y);
  20095. var rotation2 = Matrix3.fromRotationZ(-xys.s, rotation2Scratch);
  20096. var matrixQ = Matrix3.multiply(rotation1, rotation2, rotation1Scratch);
  20097. // Similar to TT conversions above
  20098. // It's possible here that secondTT could roll over 86400
  20099. // This does not seem to affect the precision (unit tests check for this)
  20100. var dateUt1day = date.dayNumber;
  20101. var dateUt1sec = date.secondsOfDay - JulianDate.computeTaiMinusUtc(date) + eop.ut1MinusUtc;
  20102. // Compute Earth rotation angle
  20103. // The IERS standard for era is
  20104. // era = 0.7790572732640 + 1.00273781191135448 * Tu
  20105. // where
  20106. // Tu = JulianDateInUt1 - 2451545.0
  20107. // However, you get much more precision if you make the following simplification
  20108. // era = a + (1 + b) * (JulianDayNumber + FractionOfDay - 2451545)
  20109. // era = a + (JulianDayNumber - 2451545) + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  20110. // era = a + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  20111. // since (JulianDayNumber - 2451545) represents an integer number of revolutions which will be discarded anyway.
  20112. var daysSinceJ2000 = dateUt1day - 2451545;
  20113. var fractionOfDay = dateUt1sec / TimeConstants.SECONDS_PER_DAY;
  20114. var era = 0.7790572732640 + fractionOfDay + 0.00273781191135448 * (daysSinceJ2000 + fractionOfDay);
  20115. era = (era % 1.0) * CesiumMath.TWO_PI;
  20116. var earthRotation = Matrix3.fromRotationZ(era, rotation2Scratch);
  20117. // pseudoFixed to ICRF
  20118. var pfToIcrf = Matrix3.multiply(matrixQ, earthRotation, rotation1Scratch);
  20119. // Compute pole wander matrix
  20120. var cosxp = Math.cos(eop.xPoleWander);
  20121. var cosyp = Math.cos(eop.yPoleWander);
  20122. var sinxp = Math.sin(eop.xPoleWander);
  20123. var sinyp = Math.sin(eop.yPoleWander);
  20124. var ttt = (dayTT - j2000ttDays) + secondTT / TimeConstants.SECONDS_PER_DAY;
  20125. ttt /= 36525.0;
  20126. // approximate sp value in rad
  20127. var sp = -47.0e-6 * ttt * CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  20128. var cossp = Math.cos(sp);
  20129. var sinsp = Math.sin(sp);
  20130. var fToPfMtx = rotation2Scratch;
  20131. fToPfMtx[0] = cosxp * cossp;
  20132. fToPfMtx[1] = cosxp * sinsp;
  20133. fToPfMtx[2] = sinxp;
  20134. fToPfMtx[3] = -cosyp * sinsp + sinyp * sinxp * cossp;
  20135. fToPfMtx[4] = cosyp * cossp + sinyp * sinxp * sinsp;
  20136. fToPfMtx[5] = -sinyp * cosxp;
  20137. fToPfMtx[6] = -sinyp * sinsp - cosyp * sinxp * cossp;
  20138. fToPfMtx[7] = sinyp * cossp - cosyp * sinxp * sinsp;
  20139. fToPfMtx[8] = cosyp * cosxp;
  20140. return Matrix3.multiply(pfToIcrf, fToPfMtx, result);
  20141. };
  20142. var pointToWindowCoordinatesTemp = new Cartesian4();
  20143. /**
  20144. * Transform a point from model coordinates to window coordinates.
  20145. *
  20146. * @param {Matrix4} modelViewProjectionMatrix The 4x4 model-view-projection matrix.
  20147. * @param {Matrix4} viewportTransformation The 4x4 viewport transformation.
  20148. * @param {Cartesian3} point The point to transform.
  20149. * @param {Cartesian2} [result] The object onto which to store the result.
  20150. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  20151. */
  20152. Transforms.pointToWindowCoordinates = function (modelViewProjectionMatrix, viewportTransformation, point, result) {
  20153. result = Transforms.pointToGLWindowCoordinates(modelViewProjectionMatrix, viewportTransformation, point, result);
  20154. result.y = 2.0 * viewportTransformation[5] - result.y;
  20155. return result;
  20156. };
  20157. /**
  20158. * @private
  20159. */
  20160. Transforms.pointToGLWindowCoordinates = function(modelViewProjectionMatrix, viewportTransformation, point, result) {
  20161. if (!defined(modelViewProjectionMatrix)) {
  20162. throw new DeveloperError('modelViewProjectionMatrix is required.');
  20163. }
  20164. if (!defined(viewportTransformation)) {
  20165. throw new DeveloperError('viewportTransformation is required.');
  20166. }
  20167. if (!defined(point)) {
  20168. throw new DeveloperError('point is required.');
  20169. }
  20170. if (!defined(result)) {
  20171. result = new Cartesian2();
  20172. }
  20173. var tmp = pointToWindowCoordinatesTemp;
  20174. Matrix4.multiplyByVector(modelViewProjectionMatrix, Cartesian4.fromElements(point.x, point.y, point.z, 1, tmp), tmp);
  20175. Cartesian4.multiplyByScalar(tmp, 1.0 / tmp.w, tmp);
  20176. Matrix4.multiplyByVector(viewportTransformation, tmp, tmp);
  20177. return Cartesian2.fromCartesian4(tmp, result);
  20178. };
  20179. var normalScratch = new Cartesian3();
  20180. var rightScratch = new Cartesian3();
  20181. var upScratch = new Cartesian3();
  20182. /**
  20183. * @private
  20184. */
  20185. Transforms.rotationMatrixFromPositionVelocity = function(position, velocity, ellipsoid, result) {
  20186. if (!defined(position)) {
  20187. throw new DeveloperError('position is required.');
  20188. }
  20189. if (!defined(velocity)) {
  20190. throw new DeveloperError('velocity is required.');
  20191. }
  20192. var normal = defaultValue(ellipsoid, Ellipsoid.WGS84).geodeticSurfaceNormal(position, normalScratch);
  20193. var right = Cartesian3.cross(velocity, normal, rightScratch);
  20194. if (Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)) {
  20195. right = Cartesian3.clone(Cartesian3.UNIT_X, right);
  20196. }
  20197. var up = Cartesian3.cross(right, velocity, upScratch);
  20198. Cartesian3.cross(velocity, up, right);
  20199. Cartesian3.negate(right, right);
  20200. if (!defined(result)) {
  20201. result = new Matrix3();
  20202. }
  20203. result[0] = velocity.x;
  20204. result[1] = velocity.y;
  20205. result[2] = velocity.z;
  20206. result[3] = right.x;
  20207. result[4] = right.y;
  20208. result[5] = right.z;
  20209. result[6] = up.x;
  20210. result[7] = up.y;
  20211. result[8] = up.z;
  20212. return result;
  20213. };
  20214. var scratchCartographic = new Cartographic();
  20215. var scratchCartesian3Projection = new Cartesian3();
  20216. var scratchCartesian3 = new Cartesian3();
  20217. var scratchCartesian4Origin = new Cartesian4();
  20218. var scratchCartesian4NewOrigin = new Cartesian4();
  20219. var scratchCartesian4NewXAxis = new Cartesian4();
  20220. var scratchCartesian4NewYAxis = new Cartesian4();
  20221. var scratchCartesian4NewZAxis = new Cartesian4();
  20222. var scratchFromENU = new Matrix4();
  20223. var scratchToENU = new Matrix4();
  20224. /**
  20225. * @private
  20226. */
  20227. Transforms.basisTo2D = function(projection, matrix, result) {
  20228. if (!defined(projection)) {
  20229. throw new DeveloperError('projection is required.');
  20230. }
  20231. if (!defined(matrix)) {
  20232. throw new DeveloperError('matrix is required.');
  20233. }
  20234. if (!defined(result)) {
  20235. throw new DeveloperError('result is required.');
  20236. }
  20237. var ellipsoid = projection.ellipsoid;
  20238. var origin = Matrix4.getColumn(matrix, 3, scratchCartesian4Origin);
  20239. var cartographic = ellipsoid.cartesianToCartographic(origin, scratchCartographic);
  20240. var fromENU = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, scratchFromENU);
  20241. var toENU = Matrix4.inverseTransformation(fromENU, scratchToENU);
  20242. var projectedPosition = projection.project(cartographic, scratchCartesian3Projection);
  20243. var newOrigin = scratchCartesian4NewOrigin;
  20244. newOrigin.x = projectedPosition.z;
  20245. newOrigin.y = projectedPosition.x;
  20246. newOrigin.z = projectedPosition.y;
  20247. newOrigin.w = 1.0;
  20248. var xAxis = Matrix4.getColumn(matrix, 0, scratchCartesian3);
  20249. var xScale = Cartesian3.magnitude(xAxis);
  20250. var newXAxis = Matrix4.multiplyByVector(toENU, xAxis, scratchCartesian4NewXAxis);
  20251. Cartesian4.fromElements(newXAxis.z, newXAxis.x, newXAxis.y, 0.0, newXAxis);
  20252. var yAxis = Matrix4.getColumn(matrix, 1, scratchCartesian3);
  20253. var yScale = Cartesian3.magnitude(yAxis);
  20254. var newYAxis = Matrix4.multiplyByVector(toENU, yAxis, scratchCartesian4NewYAxis);
  20255. Cartesian4.fromElements(newYAxis.z, newYAxis.x, newYAxis.y, 0.0, newYAxis);
  20256. var zAxis = Matrix4.getColumn(matrix, 2, scratchCartesian3);
  20257. var zScale = Cartesian3.magnitude(zAxis);
  20258. var newZAxis = scratchCartesian4NewZAxis;
  20259. Cartesian3.cross(newXAxis, newYAxis, newZAxis);
  20260. Cartesian3.normalize(newZAxis, newZAxis);
  20261. Cartesian3.cross(newYAxis, newZAxis, newXAxis);
  20262. Cartesian3.normalize(newXAxis, newXAxis);
  20263. Cartesian3.cross(newZAxis, newXAxis, newYAxis);
  20264. Cartesian3.normalize(newYAxis, newYAxis);
  20265. Cartesian3.multiplyByScalar(newXAxis, xScale, newXAxis);
  20266. Cartesian3.multiplyByScalar(newYAxis, yScale, newYAxis);
  20267. Cartesian3.multiplyByScalar(newZAxis, zScale, newZAxis);
  20268. Matrix4.setColumn(result, 0, newXAxis, result);
  20269. Matrix4.setColumn(result, 1, newYAxis, result);
  20270. Matrix4.setColumn(result, 2, newZAxis, result);
  20271. Matrix4.setColumn(result, 3, newOrigin, result);
  20272. return result;
  20273. };
  20274. return Transforms;
  20275. });
  20276. /*global define*/
  20277. define('Core/EllipsoidTangentPlane',[
  20278. './AxisAlignedBoundingBox',
  20279. './Cartesian2',
  20280. './Cartesian3',
  20281. './Cartesian4',
  20282. './defaultValue',
  20283. './defined',
  20284. './defineProperties',
  20285. './DeveloperError',
  20286. './Ellipsoid',
  20287. './IntersectionTests',
  20288. './Matrix4',
  20289. './Plane',
  20290. './Ray',
  20291. './Transforms'
  20292. ], function(
  20293. AxisAlignedBoundingBox,
  20294. Cartesian2,
  20295. Cartesian3,
  20296. Cartesian4,
  20297. defaultValue,
  20298. defined,
  20299. defineProperties,
  20300. DeveloperError,
  20301. Ellipsoid,
  20302. IntersectionTests,
  20303. Matrix4,
  20304. Plane,
  20305. Ray,
  20306. Transforms) {
  20307. 'use strict';
  20308. var scratchCart4 = new Cartesian4();
  20309. /**
  20310. * A plane tangent to the provided ellipsoid at the provided origin.
  20311. * If origin is not on the surface of the ellipsoid, it's surface projection will be used.
  20312. * If origin is at the center of the ellipsoid, an exception will be thrown.
  20313. * @alias EllipsoidTangentPlane
  20314. * @constructor
  20315. *
  20316. * @param {Cartesian3} origin The point on the surface of the ellipsoid where the tangent plane touches.
  20317. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  20318. *
  20319. * @exception {DeveloperError} origin must not be at the center of the ellipsoid.
  20320. */
  20321. function EllipsoidTangentPlane(origin, ellipsoid) {
  20322. if (!defined(origin)) {
  20323. throw new DeveloperError('origin is required.');
  20324. }
  20325. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  20326. origin = ellipsoid.scaleToGeodeticSurface(origin);
  20327. if (!defined(origin)) {
  20328. throw new DeveloperError('origin must not be at the center of the ellipsoid.');
  20329. }
  20330. var eastNorthUp = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid);
  20331. this._ellipsoid = ellipsoid;
  20332. this._origin = origin;
  20333. this._xAxis = Cartesian3.fromCartesian4(Matrix4.getColumn(eastNorthUp, 0, scratchCart4));
  20334. this._yAxis = Cartesian3.fromCartesian4(Matrix4.getColumn(eastNorthUp, 1, scratchCart4));
  20335. var normal = Cartesian3.fromCartesian4(Matrix4.getColumn(eastNorthUp, 2, scratchCart4));
  20336. this._plane = Plane.fromPointNormal(origin, normal);
  20337. }
  20338. defineProperties(EllipsoidTangentPlane.prototype, {
  20339. /**
  20340. * Gets the ellipsoid.
  20341. * @memberof EllipsoidTangentPlane.prototype
  20342. * @type {Ellipsoid}
  20343. */
  20344. ellipsoid : {
  20345. get : function() {
  20346. return this._ellipsoid;
  20347. }
  20348. },
  20349. /**
  20350. * Gets the origin.
  20351. * @memberof EllipsoidTangentPlane.prototype
  20352. * @type {Cartesian3}
  20353. */
  20354. origin : {
  20355. get : function() {
  20356. return this._origin;
  20357. }
  20358. },
  20359. /**
  20360. * Gets the plane which is tangent to the ellipsoid.
  20361. * @memberof EllipsoidTangentPlane.prototype
  20362. * @readonly
  20363. * @type {Plane}
  20364. */
  20365. plane : {
  20366. get : function() {
  20367. return this._plane;
  20368. }
  20369. },
  20370. /**
  20371. * Gets the local X-axis (east) of the tangent plane.
  20372. * @memberof EllipsoidTangentPlane.prototype
  20373. * @readonly
  20374. * @type {Cartesian3}
  20375. */
  20376. xAxis : {
  20377. get : function() {
  20378. return this._xAxis;
  20379. }
  20380. },
  20381. /**
  20382. * Gets the local Y-axis (north) of the tangent plane.
  20383. * @memberof EllipsoidTangentPlane.prototype
  20384. * @readonly
  20385. * @type {Cartesian3}
  20386. */
  20387. yAxis : {
  20388. get : function() {
  20389. return this._yAxis;
  20390. }
  20391. },
  20392. /**
  20393. * Gets the local Z-axis (up) of the tangent plane.
  20394. * @member EllipsoidTangentPlane.prototype
  20395. * @readonly
  20396. * @type {Cartesian3}
  20397. */
  20398. zAxis : {
  20399. get : function() {
  20400. return this._plane.normal;
  20401. }
  20402. }
  20403. });
  20404. var tmp = new AxisAlignedBoundingBox();
  20405. /**
  20406. * Creates a new instance from the provided ellipsoid and the center
  20407. * point of the provided Cartesians.
  20408. *
  20409. * @param {Ellipsoid} ellipsoid The ellipsoid to use.
  20410. * @param {Cartesian3} cartesians The list of positions surrounding the center point.
  20411. */
  20412. EllipsoidTangentPlane.fromPoints = function(cartesians, ellipsoid) {
  20413. if (!defined(cartesians)) {
  20414. throw new DeveloperError('cartesians is required.');
  20415. }
  20416. var box = AxisAlignedBoundingBox.fromPoints(cartesians, tmp);
  20417. return new EllipsoidTangentPlane(box.center, ellipsoid);
  20418. };
  20419. var scratchProjectPointOntoPlaneRay = new Ray();
  20420. var scratchProjectPointOntoPlaneCartesian3 = new Cartesian3();
  20421. /**
  20422. * Computes the projection of the provided 3D position onto the 2D plane, radially outward from the {@link EllipsoidTangentPlane.ellipsoid} coordinate system origin.
  20423. *
  20424. * @param {Cartesian3} cartesian The point to project.
  20425. * @param {Cartesian2} [result] The object onto which to store the result.
  20426. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided. Undefined if there is no intersection point
  20427. */
  20428. EllipsoidTangentPlane.prototype.projectPointOntoPlane = function(cartesian, result) {
  20429. if (!defined(cartesian)) {
  20430. throw new DeveloperError('cartesian is required.');
  20431. }
  20432. var ray = scratchProjectPointOntoPlaneRay;
  20433. ray.origin = cartesian;
  20434. Cartesian3.normalize(cartesian, ray.direction);
  20435. var intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20436. if (!defined(intersectionPoint)) {
  20437. Cartesian3.negate(ray.direction, ray.direction);
  20438. intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20439. }
  20440. if (defined(intersectionPoint)) {
  20441. var v = Cartesian3.subtract(intersectionPoint, this._origin, intersectionPoint);
  20442. var x = Cartesian3.dot(this._xAxis, v);
  20443. var y = Cartesian3.dot(this._yAxis, v);
  20444. if (!defined(result)) {
  20445. return new Cartesian2(x, y);
  20446. }
  20447. result.x = x;
  20448. result.y = y;
  20449. return result;
  20450. }
  20451. return undefined;
  20452. };
  20453. /**
  20454. * Computes the projection of the provided 3D positions onto the 2D plane (where possible), radially outward from the global origin.
  20455. * The resulting array may be shorter than the input array - if a single projection is impossible it will not be included.
  20456. *
  20457. * @see EllipsoidTangentPlane.projectPointOntoPlane
  20458. *
  20459. * @param {Cartesian3[]} cartesians The array of points to project.
  20460. * @param {Cartesian2[]} [result] The array of Cartesian2 instances onto which to store results.
  20461. * @returns {Cartesian2[]} The modified result parameter or a new array of Cartesian2 instances if none was provided.
  20462. */
  20463. EllipsoidTangentPlane.prototype.projectPointsOntoPlane = function(cartesians, result) {
  20464. if (!defined(cartesians)) {
  20465. throw new DeveloperError('cartesians is required.');
  20466. }
  20467. if (!defined(result)) {
  20468. result = [];
  20469. }
  20470. var count = 0;
  20471. var length = cartesians.length;
  20472. for ( var i = 0; i < length; i++) {
  20473. var p = this.projectPointOntoPlane(cartesians[i], result[count]);
  20474. if (defined(p)) {
  20475. result[count] = p;
  20476. count++;
  20477. }
  20478. }
  20479. result.length = count;
  20480. return result;
  20481. };
  20482. /**
  20483. * Computes the projection of the provided 3D position onto the 2D plane, along the plane normal.
  20484. *
  20485. * @param {Cartesian3} cartesian The point to project.
  20486. * @param {Cartesian2} [result] The object onto which to store the result.
  20487. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  20488. */
  20489. EllipsoidTangentPlane.prototype.projectPointToNearestOnPlane = function(cartesian, result) {
  20490. if (!defined(cartesian)) {
  20491. throw new DeveloperError('cartesian is required.');
  20492. }
  20493. if (!defined(result)) {
  20494. result = new Cartesian2();
  20495. }
  20496. var ray = scratchProjectPointOntoPlaneRay;
  20497. ray.origin = cartesian;
  20498. Cartesian3.clone(this._plane.normal, ray.direction);
  20499. var intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20500. if (!defined(intersectionPoint)) {
  20501. Cartesian3.negate(ray.direction, ray.direction);
  20502. intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20503. }
  20504. var v = Cartesian3.subtract(intersectionPoint, this._origin, intersectionPoint);
  20505. var x = Cartesian3.dot(this._xAxis, v);
  20506. var y = Cartesian3.dot(this._yAxis, v);
  20507. result.x = x;
  20508. result.y = y;
  20509. return result;
  20510. };
  20511. /**
  20512. * Computes the projection of the provided 3D positions onto the 2D plane, along the plane normal.
  20513. *
  20514. * @see EllipsoidTangentPlane.projectPointToNearestOnPlane
  20515. *
  20516. * @param {Cartesian3[]} cartesians The array of points to project.
  20517. * @param {Cartesian2[]} [result] The array of Cartesian2 instances onto which to store results.
  20518. * @returns {Cartesian2[]} The modified result parameter or a new array of Cartesian2 instances if none was provided. This will have the same length as <code>cartesians</code>.
  20519. */
  20520. EllipsoidTangentPlane.prototype.projectPointsToNearestOnPlane = function(cartesians, result) {
  20521. if (!defined(cartesians)) {
  20522. throw new DeveloperError('cartesians is required.');
  20523. }
  20524. if (!defined(result)) {
  20525. result = [];
  20526. }
  20527. var length = cartesians.length;
  20528. result.length = length;
  20529. for (var i = 0; i < length; i++) {
  20530. result[i] = this.projectPointToNearestOnPlane(cartesians[i], result[i]);
  20531. }
  20532. return result;
  20533. };
  20534. var projectPointsOntoEllipsoidScratch = new Cartesian3();
  20535. /**
  20536. * Computes the projection of the provided 2D positions onto the 3D ellipsoid.
  20537. *
  20538. * @param {Cartesian2[]} cartesians The array of points to project.
  20539. * @param {Cartesian3[]} [result] The array of Cartesian3 instances onto which to store results.
  20540. * @returns {Cartesian3[]} The modified result parameter or a new array of Cartesian3 instances if none was provided.
  20541. */
  20542. EllipsoidTangentPlane.prototype.projectPointsOntoEllipsoid = function(cartesians, result) {
  20543. if (!defined(cartesians)) {
  20544. throw new DeveloperError('cartesians is required.');
  20545. }
  20546. var length = cartesians.length;
  20547. if (!defined(result)) {
  20548. result = new Array(length);
  20549. } else {
  20550. result.length = length;
  20551. }
  20552. var ellipsoid = this._ellipsoid;
  20553. var origin = this._origin;
  20554. var xAxis = this._xAxis;
  20555. var yAxis = this._yAxis;
  20556. var tmp = projectPointsOntoEllipsoidScratch;
  20557. for ( var i = 0; i < length; ++i) {
  20558. var position = cartesians[i];
  20559. Cartesian3.multiplyByScalar(xAxis, position.x, tmp);
  20560. if (!defined(result[i])) {
  20561. result[i] = new Cartesian3();
  20562. }
  20563. var point = Cartesian3.add(origin, tmp, result[i]);
  20564. Cartesian3.multiplyByScalar(yAxis, position.y, tmp);
  20565. Cartesian3.add(point, tmp, point);
  20566. ellipsoid.scaleToGeocentricSurface(point, point);
  20567. }
  20568. return result;
  20569. };
  20570. return EllipsoidTangentPlane;
  20571. });
  20572. /*global define*/
  20573. define('Core/OrientedBoundingBox',[
  20574. './BoundingSphere',
  20575. './Cartesian2',
  20576. './Cartesian3',
  20577. './Cartographic',
  20578. './defaultValue',
  20579. './defined',
  20580. './DeveloperError',
  20581. './Ellipsoid',
  20582. './EllipsoidTangentPlane',
  20583. './Intersect',
  20584. './Interval',
  20585. './Math',
  20586. './Matrix3',
  20587. './Plane',
  20588. './Rectangle'
  20589. ], function(
  20590. BoundingSphere,
  20591. Cartesian2,
  20592. Cartesian3,
  20593. Cartographic,
  20594. defaultValue,
  20595. defined,
  20596. DeveloperError,
  20597. Ellipsoid,
  20598. EllipsoidTangentPlane,
  20599. Intersect,
  20600. Interval,
  20601. CesiumMath,
  20602. Matrix3,
  20603. Plane,
  20604. Rectangle) {
  20605. 'use strict';
  20606. /**
  20607. * Creates an instance of an OrientedBoundingBox.
  20608. * An OrientedBoundingBox of some object is a closed and convex cuboid. It can provide a tighter bounding volume than {@link BoundingSphere} or {@link AxisAlignedBoundingBox} in many cases.
  20609. * @alias OrientedBoundingBox
  20610. * @constructor
  20611. *
  20612. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the box.
  20613. * @param {Matrix3} [halfAxes=Matrix3.ZERO] The three orthogonal half-axes of the bounding box.
  20614. * Equivalently, the transformation matrix, to rotate and scale a 2x2x2
  20615. * cube centered at the origin.
  20616. *
  20617. *
  20618. * @example
  20619. * // Create an OrientedBoundingBox using a transformation matrix, a position where the box will be translated, and a scale.
  20620. * var center = new Cesium.Cartesian3(1.0, 0.0, 0.0);
  20621. * var halfAxes = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(1.0, 3.0, 2.0), new Cesium.Matrix3());
  20622. *
  20623. * var obb = new Cesium.OrientedBoundingBox(center, halfAxes);
  20624. *
  20625. * @see BoundingSphere
  20626. * @see BoundingRectangle
  20627. */
  20628. function OrientedBoundingBox(center, halfAxes) {
  20629. /**
  20630. * The center of the box.
  20631. * @type {Cartesian3}
  20632. * @default {@link Cartesian3.ZERO}
  20633. */
  20634. this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
  20635. /**
  20636. * The transformation matrix, to rotate the box to the right position.
  20637. * @type {Matrix3}
  20638. * @default {@link Matrix3.IDENTITY}
  20639. */
  20640. this.halfAxes = Matrix3.clone(defaultValue(halfAxes, Matrix3.ZERO));
  20641. }
  20642. var scratchCartesian1 = new Cartesian3();
  20643. var scratchCartesian2 = new Cartesian3();
  20644. var scratchCartesian3 = new Cartesian3();
  20645. var scratchCartesian4 = new Cartesian3();
  20646. var scratchCartesian5 = new Cartesian3();
  20647. var scratchCartesian6 = new Cartesian3();
  20648. var scratchCovarianceResult = new Matrix3();
  20649. var scratchEigenResult = {
  20650. unitary : new Matrix3(),
  20651. diagonal : new Matrix3()
  20652. };
  20653. /**
  20654. * Computes an instance of an OrientedBoundingBox of the given positions.
  20655. * This is an implementation of Stefan Gottschalk's Collision Queries using Oriented Bounding Boxes solution (PHD thesis).
  20656. * Reference: http://gamma.cs.unc.edu/users/gottschalk/main.pdf
  20657. *
  20658. * @param {Cartesian3[]} positions List of {@link Cartesian3} points that the bounding box will enclose.
  20659. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  20660. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if one was not provided.
  20661. *
  20662. * @example
  20663. * // Compute an object oriented bounding box enclosing two points.
  20664. * var box = Cesium.OrientedBoundingBox.fromPoints([new Cesium.Cartesian3(2, 0, 0), new Cesium.Cartesian3(-2, 0, 0)]);
  20665. */
  20666. OrientedBoundingBox.fromPoints = function(positions, result) {
  20667. if (!defined(result)) {
  20668. result = new OrientedBoundingBox();
  20669. }
  20670. if (!defined(positions) || positions.length === 0) {
  20671. result.halfAxes = Matrix3.ZERO;
  20672. result.center = Cartesian3.ZERO;
  20673. return result;
  20674. }
  20675. var i;
  20676. var length = positions.length;
  20677. var meanPoint = Cartesian3.clone(positions[0], scratchCartesian1);
  20678. for (i = 1; i < length; i++) {
  20679. Cartesian3.add(meanPoint, positions[i], meanPoint);
  20680. }
  20681. var invLength = 1.0 / length;
  20682. Cartesian3.multiplyByScalar(meanPoint, invLength, meanPoint);
  20683. var exx = 0.0;
  20684. var exy = 0.0;
  20685. var exz = 0.0;
  20686. var eyy = 0.0;
  20687. var eyz = 0.0;
  20688. var ezz = 0.0;
  20689. var p;
  20690. for (i = 0; i < length; i++) {
  20691. p = Cartesian3.subtract(positions[i], meanPoint, scratchCartesian2);
  20692. exx += p.x * p.x;
  20693. exy += p.x * p.y;
  20694. exz += p.x * p.z;
  20695. eyy += p.y * p.y;
  20696. eyz += p.y * p.z;
  20697. ezz += p.z * p.z;
  20698. }
  20699. exx *= invLength;
  20700. exy *= invLength;
  20701. exz *= invLength;
  20702. eyy *= invLength;
  20703. eyz *= invLength;
  20704. ezz *= invLength;
  20705. var covarianceMatrix = scratchCovarianceResult;
  20706. covarianceMatrix[0] = exx;
  20707. covarianceMatrix[1] = exy;
  20708. covarianceMatrix[2] = exz;
  20709. covarianceMatrix[3] = exy;
  20710. covarianceMatrix[4] = eyy;
  20711. covarianceMatrix[5] = eyz;
  20712. covarianceMatrix[6] = exz;
  20713. covarianceMatrix[7] = eyz;
  20714. covarianceMatrix[8] = ezz;
  20715. var eigenDecomposition = Matrix3.computeEigenDecomposition(covarianceMatrix, scratchEigenResult);
  20716. var rotation = Matrix3.clone(eigenDecomposition.unitary, result.halfAxes);
  20717. var v1 = Matrix3.getColumn(rotation, 0, scratchCartesian4);
  20718. var v2 = Matrix3.getColumn(rotation, 1, scratchCartesian5);
  20719. var v3 = Matrix3.getColumn(rotation, 2, scratchCartesian6);
  20720. var u1 = -Number.MAX_VALUE;
  20721. var u2 = -Number.MAX_VALUE;
  20722. var u3 = -Number.MAX_VALUE;
  20723. var l1 = Number.MAX_VALUE;
  20724. var l2 = Number.MAX_VALUE;
  20725. var l3 = Number.MAX_VALUE;
  20726. for (i = 0; i < length; i++) {
  20727. p = positions[i];
  20728. u1 = Math.max(Cartesian3.dot(v1, p), u1);
  20729. u2 = Math.max(Cartesian3.dot(v2, p), u2);
  20730. u3 = Math.max(Cartesian3.dot(v3, p), u3);
  20731. l1 = Math.min(Cartesian3.dot(v1, p), l1);
  20732. l2 = Math.min(Cartesian3.dot(v2, p), l2);
  20733. l3 = Math.min(Cartesian3.dot(v3, p), l3);
  20734. }
  20735. v1 = Cartesian3.multiplyByScalar(v1, 0.5 * (l1 + u1), v1);
  20736. v2 = Cartesian3.multiplyByScalar(v2, 0.5 * (l2 + u2), v2);
  20737. v3 = Cartesian3.multiplyByScalar(v3, 0.5 * (l3 + u3), v3);
  20738. var center = Cartesian3.add(v1, v2, result.center);
  20739. center = Cartesian3.add(center, v3, center);
  20740. var scale = scratchCartesian3;
  20741. scale.x = u1 - l1;
  20742. scale.y = u2 - l2;
  20743. scale.z = u3 - l3;
  20744. Cartesian3.multiplyByScalar(scale, 0.5, scale);
  20745. Matrix3.multiplyByScale(result.halfAxes, scale, result.halfAxes);
  20746. return result;
  20747. };
  20748. var scratchOffset = new Cartesian3();
  20749. var scratchScale = new Cartesian3();
  20750. /**
  20751. * Computes an OrientedBoundingBox given extents in the east-north-up space of the tangent plane.
  20752. *
  20753. * @param {Number} minimumX Minimum X extent in tangent plane space.
  20754. * @param {Number} maximumX Maximum X extent in tangent plane space.
  20755. * @param {Number} minimumY Minimum Y extent in tangent plane space.
  20756. * @param {Number} maximumY Maximum Y extent in tangent plane space.
  20757. * @param {Number} minimumZ Minimum Z extent in tangent plane space.
  20758. * @param {Number} maximumZ Maximum Z extent in tangent plane space.
  20759. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  20760. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if one was not provided.
  20761. */
  20762. function fromTangentPlaneExtents(tangentPlane, minimumX, maximumX, minimumY, maximumY, minimumZ, maximumZ, result) {
  20763. if (!defined(minimumX) ||
  20764. !defined(maximumX) ||
  20765. !defined(minimumY) ||
  20766. !defined(maximumY) ||
  20767. !defined(minimumZ) ||
  20768. !defined(maximumZ)) {
  20769. throw new DeveloperError('all extents (minimum/maximum X/Y/Z) are required.');
  20770. }
  20771. if (!defined(result)) {
  20772. result = new OrientedBoundingBox();
  20773. }
  20774. var halfAxes = result.halfAxes;
  20775. Matrix3.setColumn(halfAxes, 0, tangentPlane.xAxis, halfAxes);
  20776. Matrix3.setColumn(halfAxes, 1, tangentPlane.yAxis, halfAxes);
  20777. Matrix3.setColumn(halfAxes, 2, tangentPlane.zAxis, halfAxes);
  20778. var centerOffset = scratchOffset;
  20779. centerOffset.x = (minimumX + maximumX) / 2.0;
  20780. centerOffset.y = (minimumY + maximumY) / 2.0;
  20781. centerOffset.z = (minimumZ + maximumZ) / 2.0;
  20782. var scale = scratchScale;
  20783. scale.x = (maximumX - minimumX) / 2.0;
  20784. scale.y = (maximumY - minimumY) / 2.0;
  20785. scale.z = (maximumZ - minimumZ) / 2.0;
  20786. var center = result.center;
  20787. centerOffset = Matrix3.multiplyByVector(halfAxes, centerOffset, centerOffset);
  20788. Cartesian3.add(tangentPlane.origin, centerOffset, center);
  20789. Matrix3.multiplyByScale(halfAxes, scale, halfAxes);
  20790. return result;
  20791. }
  20792. var scratchRectangleCenterCartographic = new Cartographic();
  20793. var scratchRectangleCenter = new Cartesian3();
  20794. var perimeterCartographicScratch = [new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic()];
  20795. var perimeterCartesianScratch = [new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3()];
  20796. var perimeterProjectedScratch = [new Cartesian2(), new Cartesian2(), new Cartesian2(), new Cartesian2(), new Cartesian2(), new Cartesian2(), new Cartesian2(), new Cartesian2()];
  20797. /**
  20798. * Computes an OrientedBoundingBox that bounds a {@link Rectangle} on the surface of an {@link Ellipsoid}.
  20799. * There are no guarantees about the orientation of the bounding box.
  20800. *
  20801. * @param {Rectangle} rectangle The cartographic rectangle on the surface of the ellipsoid.
  20802. * @param {Number} [minimumHeight=0.0] The minimum height (elevation) within the tile.
  20803. * @param {Number} [maximumHeight=0.0] The maximum height (elevation) within the tile.
  20804. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle is defined.
  20805. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  20806. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if none was provided.
  20807. *
  20808. * @exception {DeveloperError} rectangle.width must be between 0 and pi.
  20809. * @exception {DeveloperError} rectangle.height must be between 0 and pi.
  20810. * @exception {DeveloperError} ellipsoid must be an ellipsoid of revolution (<code>radii.x == radii.y</code>)
  20811. */
  20812. OrientedBoundingBox.fromRectangle = function(rectangle, minimumHeight, maximumHeight, ellipsoid, result) {
  20813. if (!defined(rectangle)) {
  20814. throw new DeveloperError('rectangle is required');
  20815. }
  20816. if (rectangle.width < 0.0 || rectangle.width > CesiumMath.PI) {
  20817. throw new DeveloperError('Rectangle width must be between 0 and pi');
  20818. }
  20819. if (rectangle.height < 0.0 || rectangle.height > CesiumMath.PI) {
  20820. throw new DeveloperError('Rectangle height must be between 0 and pi');
  20821. }
  20822. if (defined(ellipsoid) && !CesiumMath.equalsEpsilon(ellipsoid.radii.x, ellipsoid.radii.y, CesiumMath.EPSILON15)) {
  20823. throw new DeveloperError('Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)');
  20824. }
  20825. minimumHeight = defaultValue(minimumHeight, 0.0);
  20826. maximumHeight = defaultValue(maximumHeight, 0.0);
  20827. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  20828. // The bounding box will be aligned with the tangent plane at the center of the rectangle.
  20829. var tangentPointCartographic = Rectangle.center(rectangle, scratchRectangleCenterCartographic);
  20830. var tangentPoint = ellipsoid.cartographicToCartesian(tangentPointCartographic, scratchRectangleCenter);
  20831. var tangentPlane = new EllipsoidTangentPlane(tangentPoint, ellipsoid);
  20832. var plane = tangentPlane.plane;
  20833. // Corner arrangement:
  20834. // N/+y
  20835. // [0] [1] [2]
  20836. // W/-x [7] [3] E/+x
  20837. // [6] [5] [4]
  20838. // S/-y
  20839. // "C" refers to the central lat/long, which by default aligns with the tangent point (above).
  20840. // If the rectangle spans the equator, CW and CE are instead aligned with the equator.
  20841. var perimeterNW = perimeterCartographicScratch[0];
  20842. var perimeterNC = perimeterCartographicScratch[1];
  20843. var perimeterNE = perimeterCartographicScratch[2];
  20844. var perimeterCE = perimeterCartographicScratch[3];
  20845. var perimeterSE = perimeterCartographicScratch[4];
  20846. var perimeterSC = perimeterCartographicScratch[5];
  20847. var perimeterSW = perimeterCartographicScratch[6];
  20848. var perimeterCW = perimeterCartographicScratch[7];
  20849. var lonCenter = tangentPointCartographic.longitude;
  20850. var latCenter = (rectangle.south < 0.0 && rectangle.north > 0.0) ? 0.0 : tangentPointCartographic.latitude;
  20851. perimeterSW.latitude = perimeterSC.latitude = perimeterSE.latitude = rectangle.south;
  20852. perimeterCW.latitude = perimeterCE.latitude = latCenter;
  20853. perimeterNW.latitude = perimeterNC.latitude = perimeterNE.latitude = rectangle.north;
  20854. perimeterSW.longitude = perimeterCW.longitude = perimeterNW.longitude = rectangle.west;
  20855. perimeterSC.longitude = perimeterNC.longitude = lonCenter;
  20856. perimeterSE.longitude = perimeterCE.longitude = perimeterNE.longitude = rectangle.east;
  20857. // Compute XY extents using the rectangle at maximum height
  20858. perimeterNE.height = perimeterNC.height = perimeterNW.height = perimeterCW.height = perimeterSW.height = perimeterSC.height = perimeterSE.height = perimeterCE.height = maximumHeight;
  20859. ellipsoid.cartographicArrayToCartesianArray(perimeterCartographicScratch, perimeterCartesianScratch);
  20860. tangentPlane.projectPointsToNearestOnPlane(perimeterCartesianScratch, perimeterProjectedScratch);
  20861. // See the `perimeterXX` definitions above for what these are
  20862. var minX = Math.min(perimeterProjectedScratch[6].x, perimeterProjectedScratch[7].x, perimeterProjectedScratch[0].x);
  20863. var maxX = Math.max(perimeterProjectedScratch[2].x, perimeterProjectedScratch[3].x, perimeterProjectedScratch[4].x);
  20864. var minY = Math.min(perimeterProjectedScratch[4].y, perimeterProjectedScratch[5].y, perimeterProjectedScratch[6].y);
  20865. var maxY = Math.max(perimeterProjectedScratch[0].y, perimeterProjectedScratch[1].y, perimeterProjectedScratch[2].y);
  20866. // Compute minimum Z using the rectangle at minimum height
  20867. perimeterNE.height = perimeterNW.height = perimeterSE.height = perimeterSW.height = minimumHeight;
  20868. ellipsoid.cartographicArrayToCartesianArray(perimeterCartographicScratch, perimeterCartesianScratch);
  20869. var minZ = Math.min(Plane.getPointDistance(plane, perimeterCartesianScratch[0]),
  20870. Plane.getPointDistance(plane, perimeterCartesianScratch[2]),
  20871. Plane.getPointDistance(plane, perimeterCartesianScratch[4]),
  20872. Plane.getPointDistance(plane, perimeterCartesianScratch[6]));
  20873. var maxZ = maximumHeight; // Since the tangent plane touches the surface at height = 0, this is okay
  20874. return fromTangentPlaneExtents(tangentPlane, minX, maxX, minY, maxY, minZ, maxZ, result);
  20875. };
  20876. /**
  20877. * Duplicates a OrientedBoundingBox instance.
  20878. *
  20879. * @param {OrientedBoundingBox} box The bounding box to duplicate.
  20880. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  20881. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
  20882. */
  20883. OrientedBoundingBox.clone = function(box, result) {
  20884. if (!defined(box)) {
  20885. return undefined;
  20886. }
  20887. if (!defined(result)) {
  20888. return new OrientedBoundingBox(box.center, box.halfAxes);
  20889. }
  20890. Cartesian3.clone(box.center, result.center);
  20891. Matrix3.clone(box.halfAxes, result.halfAxes);
  20892. return result;
  20893. };
  20894. /**
  20895. * Determines which side of a plane the oriented bounding box is located.
  20896. *
  20897. * @param {OrientedBoundingBox} box The oriented bounding box to test.
  20898. * @param {Plane} plane The plane to test against.
  20899. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  20900. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  20901. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  20902. * intersects the plane.
  20903. */
  20904. OrientedBoundingBox.intersectPlane = function(box, plane) {
  20905. if (!defined(box)) {
  20906. throw new DeveloperError('box is required.');
  20907. }
  20908. if (!defined(plane)) {
  20909. throw new DeveloperError('plane is required.');
  20910. }
  20911. var center = box.center;
  20912. var normal = plane.normal;
  20913. var halfAxes = box.halfAxes;
  20914. var normalX = normal.x, normalY = normal.y, normalZ = normal.z;
  20915. // plane is used as if it is its normal; the first three components are assumed to be normalized
  20916. var radEffective = Math.abs(normalX * halfAxes[Matrix3.COLUMN0ROW0] + normalY * halfAxes[Matrix3.COLUMN0ROW1] + normalZ * halfAxes[Matrix3.COLUMN0ROW2]) +
  20917. Math.abs(normalX * halfAxes[Matrix3.COLUMN1ROW0] + normalY * halfAxes[Matrix3.COLUMN1ROW1] + normalZ * halfAxes[Matrix3.COLUMN1ROW2]) +
  20918. Math.abs(normalX * halfAxes[Matrix3.COLUMN2ROW0] + normalY * halfAxes[Matrix3.COLUMN2ROW1] + normalZ * halfAxes[Matrix3.COLUMN2ROW2]);
  20919. var distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
  20920. if (distanceToPlane <= -radEffective) {
  20921. // The entire box is on the negative side of the plane normal
  20922. return Intersect.OUTSIDE;
  20923. } else if (distanceToPlane >= radEffective) {
  20924. // The entire box is on the positive side of the plane normal
  20925. return Intersect.INSIDE;
  20926. }
  20927. return Intersect.INTERSECTING;
  20928. };
  20929. var scratchCartesianU = new Cartesian3();
  20930. var scratchCartesianV = new Cartesian3();
  20931. var scratchCartesianW = new Cartesian3();
  20932. var scratchPPrime = new Cartesian3();
  20933. /**
  20934. * Computes the estimated distance squared from the closest point on a bounding box to a point.
  20935. *
  20936. * @param {OrientedBoundingBox} box The box.
  20937. * @param {Cartesian3} cartesian The point
  20938. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  20939. *
  20940. * @example
  20941. * // Sort bounding boxes from back to front
  20942. * boxes.sort(function(a, b) {
  20943. * return Cesium.OrientedBoundingBox.distanceSquaredTo(b, camera.positionWC) - Cesium.OrientedBoundingBox.distanceSquaredTo(a, camera.positionWC);
  20944. * });
  20945. */
  20946. OrientedBoundingBox.distanceSquaredTo = function(box, cartesian) {
  20947. // See Geometric Tools for Computer Graphics 10.4.2
  20948. if (!defined(box)) {
  20949. throw new DeveloperError('box is required.');
  20950. }
  20951. if (!defined(cartesian)) {
  20952. throw new DeveloperError('cartesian is required.');
  20953. }
  20954. var offset = Cartesian3.subtract(cartesian, box.center, scratchOffset);
  20955. var halfAxes = box.halfAxes;
  20956. var u = Matrix3.getColumn(halfAxes, 0, scratchCartesianU);
  20957. var v = Matrix3.getColumn(halfAxes, 1, scratchCartesianV);
  20958. var w = Matrix3.getColumn(halfAxes, 2, scratchCartesianW);
  20959. var uHalf = Cartesian3.magnitude(u);
  20960. var vHalf = Cartesian3.magnitude(v);
  20961. var wHalf = Cartesian3.magnitude(w);
  20962. Cartesian3.normalize(u, u);
  20963. Cartesian3.normalize(v, v);
  20964. Cartesian3.normalize(w, w);
  20965. var pPrime = scratchPPrime;
  20966. pPrime.x = Cartesian3.dot(offset, u);
  20967. pPrime.y = Cartesian3.dot(offset, v);
  20968. pPrime.z = Cartesian3.dot(offset, w);
  20969. var distanceSquared = 0.0;
  20970. var d;
  20971. if (pPrime.x < -uHalf) {
  20972. d = pPrime.x + uHalf;
  20973. distanceSquared += d * d;
  20974. } else if (pPrime.x > uHalf) {
  20975. d = pPrime.x - uHalf;
  20976. distanceSquared += d * d;
  20977. }
  20978. if (pPrime.y < -vHalf) {
  20979. d = pPrime.y + vHalf;
  20980. distanceSquared += d * d;
  20981. } else if (pPrime.y > vHalf) {
  20982. d = pPrime.y - vHalf;
  20983. distanceSquared += d * d;
  20984. }
  20985. if (pPrime.z < -wHalf) {
  20986. d = pPrime.z + wHalf;
  20987. distanceSquared += d * d;
  20988. } else if (pPrime.z > wHalf) {
  20989. d = pPrime.z - wHalf;
  20990. distanceSquared += d * d;
  20991. }
  20992. return distanceSquared;
  20993. };
  20994. var scratchCorner = new Cartesian3();
  20995. var scratchToCenter = new Cartesian3();
  20996. /**
  20997. * The distances calculated by the vector from the center of the bounding box to position projected onto direction.
  20998. * <br>
  20999. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  21000. * closest and farthest planes from position that intersect the bounding box.
  21001. *
  21002. * @param {OrientedBoundingBox} box The bounding box to calculate the distance to.
  21003. * @param {Cartesian3} position The position to calculate the distance from.
  21004. * @param {Cartesian3} direction The direction from position.
  21005. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  21006. * @returns {Interval} The nearest and farthest distances on the bounding box from position in direction.
  21007. */
  21008. OrientedBoundingBox.computePlaneDistances = function(box, position, direction, result) {
  21009. if (!defined(box)) {
  21010. throw new DeveloperError('box is required.');
  21011. }
  21012. if (!defined(position)) {
  21013. throw new DeveloperError('position is required.');
  21014. }
  21015. if (!defined(direction)) {
  21016. throw new DeveloperError('direction is required.');
  21017. }
  21018. if (!defined(result)) {
  21019. result = new Interval();
  21020. }
  21021. var minDist = Number.POSITIVE_INFINITY;
  21022. var maxDist = Number.NEGATIVE_INFINITY;
  21023. var center = box.center;
  21024. var halfAxes = box.halfAxes;
  21025. var u = Matrix3.getColumn(halfAxes, 0, scratchCartesianU);
  21026. var v = Matrix3.getColumn(halfAxes, 1, scratchCartesianV);
  21027. var w = Matrix3.getColumn(halfAxes, 2, scratchCartesianW);
  21028. // project first corner
  21029. var corner = Cartesian3.add(u, v, scratchCorner);
  21030. Cartesian3.add(corner, w, corner);
  21031. Cartesian3.add(corner, center, corner);
  21032. var toCenter = Cartesian3.subtract(corner, position, scratchToCenter);
  21033. var mag = Cartesian3.dot(direction, toCenter);
  21034. minDist = Math.min(mag, minDist);
  21035. maxDist = Math.max(mag, maxDist);
  21036. // project second corner
  21037. Cartesian3.add(center, u, corner);
  21038. Cartesian3.add(corner, v, corner);
  21039. Cartesian3.subtract(corner, w, corner);
  21040. Cartesian3.subtract(corner, position, toCenter);
  21041. mag = Cartesian3.dot(direction, toCenter);
  21042. minDist = Math.min(mag, minDist);
  21043. maxDist = Math.max(mag, maxDist);
  21044. // project third corner
  21045. Cartesian3.add(center, u, corner);
  21046. Cartesian3.subtract(corner, v, corner);
  21047. Cartesian3.add(corner, w, corner);
  21048. Cartesian3.subtract(corner, position, toCenter);
  21049. mag = Cartesian3.dot(direction, toCenter);
  21050. minDist = Math.min(mag, minDist);
  21051. maxDist = Math.max(mag, maxDist);
  21052. // project fourth corner
  21053. Cartesian3.add(center, u, corner);
  21054. Cartesian3.subtract(corner, v, corner);
  21055. Cartesian3.subtract(corner, w, corner);
  21056. Cartesian3.subtract(corner, position, toCenter);
  21057. mag = Cartesian3.dot(direction, toCenter);
  21058. minDist = Math.min(mag, minDist);
  21059. maxDist = Math.max(mag, maxDist);
  21060. // project fifth corner
  21061. Cartesian3.subtract(center, u, corner);
  21062. Cartesian3.add(corner, v, corner);
  21063. Cartesian3.add(corner, w, corner);
  21064. Cartesian3.subtract(corner, position, toCenter);
  21065. mag = Cartesian3.dot(direction, toCenter);
  21066. minDist = Math.min(mag, minDist);
  21067. maxDist = Math.max(mag, maxDist);
  21068. // project sixth corner
  21069. Cartesian3.subtract(center, u, corner);
  21070. Cartesian3.add(corner, v, corner);
  21071. Cartesian3.subtract(corner, w, corner);
  21072. Cartesian3.subtract(corner, position, toCenter);
  21073. mag = Cartesian3.dot(direction, toCenter);
  21074. minDist = Math.min(mag, minDist);
  21075. maxDist = Math.max(mag, maxDist);
  21076. // project seventh corner
  21077. Cartesian3.subtract(center, u, corner);
  21078. Cartesian3.subtract(corner, v, corner);
  21079. Cartesian3.add(corner, w, corner);
  21080. Cartesian3.subtract(corner, position, toCenter);
  21081. mag = Cartesian3.dot(direction, toCenter);
  21082. minDist = Math.min(mag, minDist);
  21083. maxDist = Math.max(mag, maxDist);
  21084. // project eighth corner
  21085. Cartesian3.subtract(center, u, corner);
  21086. Cartesian3.subtract(corner, v, corner);
  21087. Cartesian3.subtract(corner, w, corner);
  21088. Cartesian3.subtract(corner, position, toCenter);
  21089. mag = Cartesian3.dot(direction, toCenter);
  21090. minDist = Math.min(mag, minDist);
  21091. maxDist = Math.max(mag, maxDist);
  21092. result.start = minDist;
  21093. result.stop = maxDist;
  21094. return result;
  21095. };
  21096. var scratchBoundingSphere = new BoundingSphere();
  21097. /**
  21098. * Determines whether or not a bounding box is hidden from view by the occluder.
  21099. *
  21100. * @param {OrientedBoundingBox} box The bounding box surrounding the occludee object.
  21101. * @param {Occluder} occluder The occluder.
  21102. * @returns {Boolean} <code>true</code> if the box is not visible; otherwise <code>false</code>.
  21103. */
  21104. OrientedBoundingBox.isOccluded = function(box, occluder) {
  21105. if (!defined(box)) {
  21106. throw new DeveloperError('box is required.');
  21107. }
  21108. if (!defined(occluder)) {
  21109. throw new DeveloperError('occluder is required.');
  21110. }
  21111. var sphere = BoundingSphere.fromOrientedBoundingBox(box, scratchBoundingSphere);
  21112. return !occluder.isBoundingSphereVisible(sphere);
  21113. };
  21114. /**
  21115. * Determines which side of a plane the oriented bounding box is located.
  21116. *
  21117. * @param {Plane} plane The plane to test against.
  21118. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  21119. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  21120. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  21121. * intersects the plane.
  21122. */
  21123. OrientedBoundingBox.prototype.intersectPlane = function(plane) {
  21124. return OrientedBoundingBox.intersectPlane(this, plane);
  21125. };
  21126. /**
  21127. * Computes the estimated distance squared from the closest point on a bounding box to a point.
  21128. *
  21129. * @param {Cartesian3} cartesian The point
  21130. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  21131. *
  21132. * @example
  21133. * // Sort bounding boxes from back to front
  21134. * boxes.sort(function(a, b) {
  21135. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  21136. * });
  21137. */
  21138. OrientedBoundingBox.prototype.distanceSquaredTo = function(cartesian) {
  21139. return OrientedBoundingBox.distanceSquaredTo(this, cartesian);
  21140. };
  21141. /**
  21142. * The distances calculated by the vector from the center of the bounding box to position projected onto direction.
  21143. * <br>
  21144. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  21145. * closest and farthest planes from position that intersect the bounding box.
  21146. *
  21147. * @param {Cartesian3} position The position to calculate the distance from.
  21148. * @param {Cartesian3} direction The direction from position.
  21149. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  21150. * @returns {Interval} The nearest and farthest distances on the bounding box from position in direction.
  21151. */
  21152. OrientedBoundingBox.prototype.computePlaneDistances = function(position, direction, result) {
  21153. return OrientedBoundingBox.computePlaneDistances(this, position, direction, result);
  21154. };
  21155. /**
  21156. * Determines whether or not a bounding box is hidden from view by the occluder.
  21157. *
  21158. * @param {Occluder} occluder The occluder.
  21159. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  21160. */
  21161. OrientedBoundingBox.prototype.isOccluded = function(occluder) {
  21162. return OrientedBoundingBox.isOccluded(this, occluder);
  21163. };
  21164. /**
  21165. * Compares the provided OrientedBoundingBox componentwise and returns
  21166. * <code>true</code> if they are equal, <code>false</code> otherwise.
  21167. *
  21168. * @param {OrientedBoundingBox} left The first OrientedBoundingBox.
  21169. * @param {OrientedBoundingBox} right The second OrientedBoundingBox.
  21170. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  21171. */
  21172. OrientedBoundingBox.equals = function(left, right) {
  21173. return (left === right) ||
  21174. ((defined(left)) &&
  21175. (defined(right)) &&
  21176. Cartesian3.equals(left.center, right.center) &&
  21177. Matrix3.equals(left.halfAxes, right.halfAxes));
  21178. };
  21179. /**
  21180. * Duplicates this OrientedBoundingBox instance.
  21181. *
  21182. * @param {OrientedBoundingBox} [result] The object onto which to store the result.
  21183. * @returns {OrientedBoundingBox} The modified result parameter or a new OrientedBoundingBox instance if one was not provided.
  21184. */
  21185. OrientedBoundingBox.prototype.clone = function(result) {
  21186. return OrientedBoundingBox.clone(this, result);
  21187. };
  21188. /**
  21189. * Compares this OrientedBoundingBox against the provided OrientedBoundingBox componentwise and returns
  21190. * <code>true</code> if they are equal, <code>false</code> otherwise.
  21191. *
  21192. * @param {OrientedBoundingBox} [right] The right hand side OrientedBoundingBox.
  21193. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  21194. */
  21195. OrientedBoundingBox.prototype.equals = function(right) {
  21196. return OrientedBoundingBox.equals(this, right);
  21197. };
  21198. return OrientedBoundingBox;
  21199. });
  21200. /*global define*/
  21201. define('Core/ComponentDatatype',[
  21202. './defaultValue',
  21203. './defined',
  21204. './DeveloperError',
  21205. './FeatureDetection',
  21206. './freezeObject',
  21207. './WebGLConstants'
  21208. ], function(
  21209. defaultValue,
  21210. defined,
  21211. DeveloperError,
  21212. FeatureDetection,
  21213. freezeObject,
  21214. WebGLConstants) {
  21215. 'use strict';
  21216. // Bail out if the browser doesn't support typed arrays, to prevent the setup function
  21217. // from failing, since we won't be able to create a WebGL context anyway.
  21218. if (!FeatureDetection.supportsTypedArrays()) {
  21219. return {};
  21220. }
  21221. /**
  21222. * WebGL component datatypes. Components are intrinsics,
  21223. * which form attributes, which form vertices.
  21224. *
  21225. * @exports ComponentDatatype
  21226. */
  21227. var ComponentDatatype = {
  21228. /**
  21229. * 8-bit signed byte corresponding to <code>gl.BYTE</code> and the type
  21230. * of an element in <code>Int8Array</code>.
  21231. *
  21232. * @type {Number}
  21233. * @constant
  21234. */
  21235. BYTE : WebGLConstants.BYTE,
  21236. /**
  21237. * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
  21238. * of an element in <code>Uint8Array</code>.
  21239. *
  21240. * @type {Number}
  21241. * @constant
  21242. */
  21243. UNSIGNED_BYTE : WebGLConstants.UNSIGNED_BYTE,
  21244. /**
  21245. * 16-bit signed short corresponding to <code>SHORT</code> and the type
  21246. * of an element in <code>Int16Array</code>.
  21247. *
  21248. * @type {Number}
  21249. * @constant
  21250. */
  21251. SHORT : WebGLConstants.SHORT,
  21252. /**
  21253. * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
  21254. * of an element in <code>Uint16Array</code>.
  21255. *
  21256. * @type {Number}
  21257. * @constant
  21258. */
  21259. UNSIGNED_SHORT : WebGLConstants.UNSIGNED_SHORT,
  21260. /**
  21261. * 32-bit signed int corresponding to <code>INT</code> and the type
  21262. * of an element in <code>Int32Array</code>.
  21263. *
  21264. * @memberOf ComponentDatatype
  21265. *
  21266. * @type {Number}
  21267. * @constant
  21268. */
  21269. INT : WebGLConstants.INT,
  21270. /**
  21271. * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
  21272. * of an element in <code>Uint32Array</code>.
  21273. *
  21274. * @memberOf ComponentDatatype
  21275. *
  21276. * @type {Number}
  21277. * @constant
  21278. */
  21279. UNSIGNED_INT : WebGLConstants.UNSIGNED_INT,
  21280. /**
  21281. * 32-bit floating-point corresponding to <code>FLOAT</code> and the type
  21282. * of an element in <code>Float32Array</code>.
  21283. *
  21284. * @type {Number}
  21285. * @constant
  21286. */
  21287. FLOAT : WebGLConstants.FLOAT,
  21288. /**
  21289. * 64-bit floating-point corresponding to <code>gl.DOUBLE</code> (in Desktop OpenGL;
  21290. * this is not supported in WebGL, and is emulated in Cesium via {@link GeometryPipeline.encodeAttribute})
  21291. * and the type of an element in <code>Float64Array</code>.
  21292. *
  21293. * @memberOf ComponentDatatype
  21294. *
  21295. * @type {Number}
  21296. * @constant
  21297. * @default 0x140A
  21298. */
  21299. DOUBLE : WebGLConstants.DOUBLE
  21300. };
  21301. /**
  21302. * Returns the size, in bytes, of the corresponding datatype.
  21303. *
  21304. * @param {ComponentDatatype} componentDatatype The component datatype to get the size of.
  21305. * @returns {Number} The size in bytes.
  21306. *
  21307. * @exception {DeveloperError} componentDatatype is not a valid value.
  21308. *
  21309. * @example
  21310. * // Returns Int8Array.BYTES_PER_ELEMENT
  21311. * var size = Cesium.ComponentDatatype.getSizeInBytes(Cesium.ComponentDatatype.BYTE);
  21312. */
  21313. ComponentDatatype.getSizeInBytes = function(componentDatatype){
  21314. if (!defined(componentDatatype)) {
  21315. throw new DeveloperError('value is required.');
  21316. }
  21317. switch (componentDatatype) {
  21318. case ComponentDatatype.BYTE:
  21319. return Int8Array.BYTES_PER_ELEMENT;
  21320. case ComponentDatatype.UNSIGNED_BYTE:
  21321. return Uint8Array.BYTES_PER_ELEMENT;
  21322. case ComponentDatatype.SHORT:
  21323. return Int16Array.BYTES_PER_ELEMENT;
  21324. case ComponentDatatype.UNSIGNED_SHORT:
  21325. return Uint16Array.BYTES_PER_ELEMENT;
  21326. case ComponentDatatype.INT:
  21327. return Int32Array.BYTES_PER_ELEMENT;
  21328. case ComponentDatatype.UNSIGNED_INT:
  21329. return Uint32Array.BYTES_PER_ELEMENT;
  21330. case ComponentDatatype.FLOAT:
  21331. return Float32Array.BYTES_PER_ELEMENT;
  21332. case ComponentDatatype.DOUBLE:
  21333. return Float64Array.BYTES_PER_ELEMENT;
  21334. default:
  21335. throw new DeveloperError('componentDatatype is not a valid value.');
  21336. }
  21337. };
  21338. /**
  21339. * Gets the {@link ComponentDatatype} for the provided TypedArray instance.
  21340. *
  21341. * @param {TypedArray} array The typed array.
  21342. * @returns {ComponentDatatype} The ComponentDatatype for the provided array, or undefined if the array is not a TypedArray.
  21343. */
  21344. ComponentDatatype.fromTypedArray = function(array) {
  21345. if (array instanceof Int8Array) {
  21346. return ComponentDatatype.BYTE;
  21347. }
  21348. if (array instanceof Uint8Array) {
  21349. return ComponentDatatype.UNSIGNED_BYTE;
  21350. }
  21351. if (array instanceof Int16Array) {
  21352. return ComponentDatatype.SHORT;
  21353. }
  21354. if (array instanceof Uint16Array) {
  21355. return ComponentDatatype.UNSIGNED_SHORT;
  21356. }
  21357. if (array instanceof Int32Array) {
  21358. return ComponentDatatype.INT;
  21359. }
  21360. if (array instanceof Uint32Array) {
  21361. return ComponentDatatype.UNSIGNED_INT;
  21362. }
  21363. if (array instanceof Float32Array) {
  21364. return ComponentDatatype.FLOAT;
  21365. }
  21366. if (array instanceof Float64Array) {
  21367. return ComponentDatatype.DOUBLE;
  21368. }
  21369. };
  21370. /**
  21371. * Validates that the provided component datatype is a valid {@link ComponentDatatype}
  21372. *
  21373. * @param {ComponentDatatype} componentDatatype The component datatype to validate.
  21374. * @returns {Boolean} <code>true</code> if the provided component datatype is a valid value; otherwise, <code>false</code>.
  21375. *
  21376. * @example
  21377. * if (!Cesium.ComponentDatatype.validate(componentDatatype)) {
  21378. * throw new Cesium.DeveloperError('componentDatatype must be a valid value.');
  21379. * }
  21380. */
  21381. ComponentDatatype.validate = function(componentDatatype) {
  21382. return defined(componentDatatype) &&
  21383. (componentDatatype === ComponentDatatype.BYTE ||
  21384. componentDatatype === ComponentDatatype.UNSIGNED_BYTE ||
  21385. componentDatatype === ComponentDatatype.SHORT ||
  21386. componentDatatype === ComponentDatatype.UNSIGNED_SHORT ||
  21387. componentDatatype === ComponentDatatype.INT ||
  21388. componentDatatype === ComponentDatatype.UNSIGNED_INT ||
  21389. componentDatatype === ComponentDatatype.FLOAT ||
  21390. componentDatatype === ComponentDatatype.DOUBLE);
  21391. };
  21392. /**
  21393. * Creates a typed array corresponding to component data type.
  21394. *
  21395. * @param {ComponentDatatype} componentDatatype The component data type.
  21396. * @param {Number|Array} valuesOrLength The length of the array to create or an array.
  21397. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array.
  21398. *
  21399. * @exception {DeveloperError} componentDatatype is not a valid value.
  21400. *
  21401. * @example
  21402. * // creates a Float32Array with length of 100
  21403. * var typedArray = Cesium.ComponentDatatype.createTypedArray(Cesium.ComponentDatatype.FLOAT, 100);
  21404. */
  21405. ComponentDatatype.createTypedArray = function(componentDatatype, valuesOrLength) {
  21406. if (!defined(componentDatatype)) {
  21407. throw new DeveloperError('componentDatatype is required.');
  21408. }
  21409. if (!defined(valuesOrLength)) {
  21410. throw new DeveloperError('valuesOrLength is required.');
  21411. }
  21412. switch (componentDatatype) {
  21413. case ComponentDatatype.BYTE:
  21414. return new Int8Array(valuesOrLength);
  21415. case ComponentDatatype.UNSIGNED_BYTE:
  21416. return new Uint8Array(valuesOrLength);
  21417. case ComponentDatatype.SHORT:
  21418. return new Int16Array(valuesOrLength);
  21419. case ComponentDatatype.UNSIGNED_SHORT:
  21420. return new Uint16Array(valuesOrLength);
  21421. case ComponentDatatype.INT:
  21422. return new Int32Array(valuesOrLength);
  21423. case ComponentDatatype.UNSIGNED_INT:
  21424. return new Uint32Array(valuesOrLength);
  21425. case ComponentDatatype.FLOAT:
  21426. return new Float32Array(valuesOrLength);
  21427. case ComponentDatatype.DOUBLE:
  21428. return new Float64Array(valuesOrLength);
  21429. default:
  21430. throw new DeveloperError('componentDatatype is not a valid value.');
  21431. }
  21432. };
  21433. /**
  21434. * Creates a typed view of an array of bytes.
  21435. *
  21436. * @param {ComponentDatatype} componentDatatype The type of the view to create.
  21437. * @param {ArrayBuffer} buffer The buffer storage to use for the view.
  21438. * @param {Number} [byteOffset] The offset, in bytes, to the first element in the view.
  21439. * @param {Number} [length] The number of elements in the view.
  21440. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array view of the buffer.
  21441. *
  21442. * @exception {DeveloperError} componentDatatype is not a valid value.
  21443. */
  21444. ComponentDatatype.createArrayBufferView = function(componentDatatype, buffer, byteOffset, length) {
  21445. if (!defined(componentDatatype)) {
  21446. throw new DeveloperError('componentDatatype is required.');
  21447. }
  21448. if (!defined(buffer)) {
  21449. throw new DeveloperError('buffer is required.');
  21450. }
  21451. byteOffset = defaultValue(byteOffset, 0);
  21452. length = defaultValue(length, (buffer.byteLength - byteOffset) / ComponentDatatype.getSizeInBytes(componentDatatype));
  21453. switch (componentDatatype) {
  21454. case ComponentDatatype.BYTE:
  21455. return new Int8Array(buffer, byteOffset, length);
  21456. case ComponentDatatype.UNSIGNED_BYTE:
  21457. return new Uint8Array(buffer, byteOffset, length);
  21458. case ComponentDatatype.SHORT:
  21459. return new Int16Array(buffer, byteOffset, length);
  21460. case ComponentDatatype.UNSIGNED_SHORT:
  21461. return new Uint16Array(buffer, byteOffset, length);
  21462. case ComponentDatatype.INT:
  21463. return new Int32Array(buffer, byteOffset, length);
  21464. case ComponentDatatype.UNSIGNED_INT:
  21465. return new Uint32Array(buffer, byteOffset, length);
  21466. case ComponentDatatype.FLOAT:
  21467. return new Float32Array(buffer, byteOffset, length);
  21468. case ComponentDatatype.DOUBLE:
  21469. return new Float64Array(buffer, byteOffset, length);
  21470. default:
  21471. throw new DeveloperError('componentDatatype is not a valid value.');
  21472. }
  21473. };
  21474. /**
  21475. * Get the ComponentDatatype from its name.
  21476. *
  21477. * @param {String} name The name of the ComponentDatatype.
  21478. * @returns {ComponentDatatype} The ComponentDatatype.
  21479. *
  21480. * @exception {DeveloperError} name is not a valid value.
  21481. */
  21482. ComponentDatatype.fromName = function(name) {
  21483. switch (name) {
  21484. case 'BYTE':
  21485. return ComponentDatatype.BYTE;
  21486. case 'UNSIGNED_BYTE':
  21487. return ComponentDatatype.UNSIGNED_BYTE;
  21488. case 'SHORT':
  21489. return ComponentDatatype.SHORT;
  21490. case 'UNSIGNED_SHORT':
  21491. return ComponentDatatype.UNSIGNED_SHORT;
  21492. case 'INT':
  21493. return ComponentDatatype.INT;
  21494. case 'UNSIGNED_INT':
  21495. return ComponentDatatype.UNSIGNED_INT;
  21496. case 'FLOAT':
  21497. return ComponentDatatype.FLOAT;
  21498. case 'DOUBLE':
  21499. return ComponentDatatype.DOUBLE;
  21500. default:
  21501. throw new DeveloperError('name is not a valid value.');
  21502. }
  21503. };
  21504. return freezeObject(ComponentDatatype);
  21505. });
  21506. /*global define*/
  21507. define('Core/TerrainQuantization',[
  21508. './freezeObject'
  21509. ], function(
  21510. freezeObject) {
  21511. 'use strict';
  21512. /**
  21513. * This enumerated type is used to determine how the vertices of the terrain mesh are compressed.
  21514. *
  21515. * @exports TerrainQuantization
  21516. *
  21517. * @private
  21518. */
  21519. var TerrainQuantization = {
  21520. /**
  21521. * The vertices are not compressed.
  21522. *
  21523. * @type {Number}
  21524. * @constant
  21525. */
  21526. NONE : 0,
  21527. /**
  21528. * The vertices are compressed to 12 bits.
  21529. *
  21530. * @type {Number}
  21531. * @constant
  21532. */
  21533. BITS12 : 1
  21534. };
  21535. return freezeObject(TerrainQuantization);
  21536. });
  21537. /*global define*/
  21538. define('Core/TerrainEncoding',[
  21539. './AttributeCompression',
  21540. './Cartesian2',
  21541. './Cartesian3',
  21542. './ComponentDatatype',
  21543. './defaultValue',
  21544. './defined',
  21545. './Math',
  21546. './Matrix4',
  21547. './TerrainQuantization'
  21548. ], function(
  21549. AttributeCompression,
  21550. Cartesian2,
  21551. Cartesian3,
  21552. ComponentDatatype,
  21553. defaultValue,
  21554. defined,
  21555. CesiumMath,
  21556. Matrix4,
  21557. TerrainQuantization) {
  21558. 'use strict';
  21559. var cartesian3Scratch = new Cartesian3();
  21560. var cartesian3DimScratch = new Cartesian3();
  21561. var cartesian2Scratch = new Cartesian2();
  21562. var matrix4Scratch = new Matrix4();
  21563. var matrix4Scratch2 = new Matrix4();
  21564. var SHIFT_LEFT_12 = Math.pow(2.0, 12.0);
  21565. /**
  21566. * Data used to quantize and pack the terrain mesh. The position can be unpacked for picking and all attributes
  21567. * are unpacked in the vertex shader.
  21568. *
  21569. * @alias TerrainEncoding
  21570. * @constructor
  21571. *
  21572. * @param {AxisAlignedBoundingBox} axisAlignedBoundingBox The bounds of the tile in the east-north-up coordinates at the tiles center.
  21573. * @param {Number} minimumHeight The minimum height.
  21574. * @param {Number} maximumHeight The maximum height.
  21575. * @param {Matrix4} fromENU The east-north-up to fixed frame matrix at the center of the terrain mesh.
  21576. * @param {Boolean} hasVertexNormals If the mesh has vertex normals.
  21577. * @param {Boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
  21578. *
  21579. * @private
  21580. */
  21581. function TerrainEncoding(axisAlignedBoundingBox, minimumHeight, maximumHeight, fromENU, hasVertexNormals, hasWebMercatorT) {
  21582. var quantization;
  21583. var center;
  21584. var toENU;
  21585. var matrix;
  21586. if (defined(axisAlignedBoundingBox) && defined(minimumHeight) && defined(maximumHeight) && defined(fromENU)) {
  21587. var minimum = axisAlignedBoundingBox.minimum;
  21588. var maximum = axisAlignedBoundingBox.maximum;
  21589. var dimensions = Cartesian3.subtract(maximum, minimum, cartesian3DimScratch);
  21590. var hDim = maximumHeight - minimumHeight;
  21591. var maxDim = Math.max(Cartesian3.maximumComponent(dimensions), hDim);
  21592. if (maxDim < SHIFT_LEFT_12 - 1.0) {
  21593. quantization = TerrainQuantization.BITS12;
  21594. } else {
  21595. quantization = TerrainQuantization.NONE;
  21596. }
  21597. center = axisAlignedBoundingBox.center;
  21598. toENU = Matrix4.inverseTransformation(fromENU, new Matrix4());
  21599. var translation = Cartesian3.negate(minimum, cartesian3Scratch);
  21600. Matrix4.multiply(Matrix4.fromTranslation(translation, matrix4Scratch), toENU, toENU);
  21601. var scale = cartesian3Scratch;
  21602. scale.x = 1.0 / dimensions.x;
  21603. scale.y = 1.0 / dimensions.y;
  21604. scale.z = 1.0 / dimensions.z;
  21605. Matrix4.multiply(Matrix4.fromScale(scale, matrix4Scratch), toENU, toENU);
  21606. matrix = Matrix4.clone(fromENU);
  21607. Matrix4.setTranslation(matrix, Cartesian3.ZERO, matrix);
  21608. fromENU = Matrix4.clone(fromENU, new Matrix4());
  21609. var translationMatrix = Matrix4.fromTranslation(minimum, matrix4Scratch);
  21610. var scaleMatrix = Matrix4.fromScale(dimensions, matrix4Scratch2);
  21611. var st = Matrix4.multiply(translationMatrix, scaleMatrix,matrix4Scratch);
  21612. Matrix4.multiply(fromENU, st, fromENU);
  21613. Matrix4.multiply(matrix, st, matrix);
  21614. }
  21615. /**
  21616. * How the vertices of the mesh were compressed.
  21617. * @type {TerrainQuantization}
  21618. */
  21619. this.quantization = quantization;
  21620. /**
  21621. * The minimum height of the tile including the skirts.
  21622. * @type {Number}
  21623. */
  21624. this.minimumHeight = minimumHeight;
  21625. /**
  21626. * The maximum height of the tile.
  21627. * @type {Number}
  21628. */
  21629. this.maximumHeight = maximumHeight;
  21630. /**
  21631. * The center of the tile.
  21632. * @type {Cartesian3}
  21633. */
  21634. this.center = center;
  21635. /**
  21636. * A matrix that takes a vertex from the tile, transforms it to east-north-up at the center and scales
  21637. * it so each component is in the [0, 1] range.
  21638. * @type {Matrix4}
  21639. */
  21640. this.toScaledENU = toENU;
  21641. /**
  21642. * A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
  21643. * @type {Matrix4}
  21644. */
  21645. this.fromScaledENU = fromENU;
  21646. /**
  21647. * The matrix used to decompress the terrain vertices in the shader for RTE rendering.
  21648. * @type {Matrix4}
  21649. */
  21650. this.matrix = matrix;
  21651. /**
  21652. * The terrain mesh contains normals.
  21653. * @type {Boolean}
  21654. */
  21655. this.hasVertexNormals = hasVertexNormals;
  21656. /**
  21657. * The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
  21658. * @type {Boolean}
  21659. */
  21660. this.hasWebMercatorT = defaultValue(hasWebMercatorT, false);
  21661. }
  21662. TerrainEncoding.prototype.encode = function(vertexBuffer, bufferIndex, position, uv, height, normalToPack, webMercatorT) {
  21663. var u = uv.x;
  21664. var v = uv.y;
  21665. if (this.quantization === TerrainQuantization.BITS12) {
  21666. position = Matrix4.multiplyByPoint(this.toScaledENU, position, cartesian3Scratch);
  21667. position.x = CesiumMath.clamp(position.x, 0.0, 1.0);
  21668. position.y = CesiumMath.clamp(position.y, 0.0, 1.0);
  21669. position.z = CesiumMath.clamp(position.z, 0.0, 1.0);
  21670. var hDim = this.maximumHeight - this.minimumHeight;
  21671. var h = CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
  21672. Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
  21673. var compressed0 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  21674. Cartesian2.fromElements(position.z, h, cartesian2Scratch);
  21675. var compressed1 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  21676. Cartesian2.fromElements(u, v, cartesian2Scratch);
  21677. var compressed2 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  21678. vertexBuffer[bufferIndex++] = compressed0;
  21679. vertexBuffer[bufferIndex++] = compressed1;
  21680. vertexBuffer[bufferIndex++] = compressed2;
  21681. if (this.hasWebMercatorT) {
  21682. Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
  21683. var compressed3 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  21684. vertexBuffer[bufferIndex++] = compressed3;
  21685. }
  21686. } else {
  21687. Cartesian3.subtract(position, this.center, cartesian3Scratch);
  21688. vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
  21689. vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
  21690. vertexBuffer[bufferIndex++] = cartesian3Scratch.z;
  21691. vertexBuffer[bufferIndex++] = height;
  21692. vertexBuffer[bufferIndex++] = u;
  21693. vertexBuffer[bufferIndex++] = v;
  21694. if (this.hasWebMercatorT) {
  21695. vertexBuffer[bufferIndex++] = webMercatorT;
  21696. }
  21697. }
  21698. if (this.hasVertexNormals) {
  21699. vertexBuffer[bufferIndex++] = AttributeCompression.octPackFloat(normalToPack);
  21700. }
  21701. return bufferIndex;
  21702. };
  21703. TerrainEncoding.prototype.decodePosition = function(buffer, index, result) {
  21704. if (!defined(result)) {
  21705. result = new Cartesian3();
  21706. }
  21707. index *= this.getStride();
  21708. if (this.quantization === TerrainQuantization.BITS12) {
  21709. var xy = AttributeCompression.decompressTextureCoordinates(buffer[index], cartesian2Scratch);
  21710. result.x = xy.x;
  21711. result.y = xy.y;
  21712. var zh = AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
  21713. result.z = zh.x;
  21714. return Matrix4.multiplyByPoint(this.fromScaledENU, result, result);
  21715. }
  21716. result.x = buffer[index];
  21717. result.y = buffer[index + 1];
  21718. result.z = buffer[index + 2];
  21719. return Cartesian3.add(result, this.center, result);
  21720. };
  21721. TerrainEncoding.prototype.decodeTextureCoordinates = function(buffer, index, result) {
  21722. if (!defined(result)) {
  21723. result = new Cartesian2();
  21724. }
  21725. index *= this.getStride();
  21726. if (this.quantization === TerrainQuantization.BITS12) {
  21727. return AttributeCompression.decompressTextureCoordinates(buffer[index + 2], result);
  21728. }
  21729. return Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
  21730. };
  21731. TerrainEncoding.prototype.decodeHeight = function(buffer, index) {
  21732. index *= this.getStride();
  21733. if (this.quantization === TerrainQuantization.BITS12) {
  21734. var zh = AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
  21735. return zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight;
  21736. }
  21737. return buffer[index + 3];
  21738. };
  21739. TerrainEncoding.prototype.getOctEncodedNormal = function(buffer, index, result) {
  21740. var stride = this.getStride();
  21741. index = (index + 1) * stride - 1;
  21742. var temp = buffer[index] / 256.0;
  21743. var x = Math.floor(temp);
  21744. var y = (temp - x) * 256.0;
  21745. return Cartesian2.fromElements(x, y, result);
  21746. };
  21747. TerrainEncoding.prototype.getStride = function() {
  21748. var vertexStride;
  21749. switch (this.quantization) {
  21750. case TerrainQuantization.BITS12:
  21751. vertexStride = 3;
  21752. break;
  21753. default:
  21754. vertexStride = 6;
  21755. }
  21756. if (this.hasWebMercatorT) {
  21757. ++vertexStride;
  21758. }
  21759. if (this.hasVertexNormals) {
  21760. ++vertexStride;
  21761. }
  21762. return vertexStride;
  21763. };
  21764. var attributesNone = {
  21765. position3DAndHeight : 0,
  21766. textureCoordAndEncodedNormals : 1
  21767. };
  21768. var attributes = {
  21769. compressed0 : 0,
  21770. compressed1 : 1
  21771. };
  21772. TerrainEncoding.prototype.getAttributes = function(buffer) {
  21773. var datatype = ComponentDatatype.FLOAT;
  21774. var sizeInBytes = ComponentDatatype.getSizeInBytes(datatype);
  21775. var stride;
  21776. if (this.quantization === TerrainQuantization.NONE) {
  21777. var position3DAndHeightLength = 4;
  21778. var numTexCoordComponents = 2;
  21779. if (this.hasWebMercatorT) {
  21780. ++numTexCoordComponents;
  21781. }
  21782. if (this.hasVertexNormals) {
  21783. ++numTexCoordComponents;
  21784. }
  21785. stride = (position3DAndHeightLength + numTexCoordComponents) * sizeInBytes;
  21786. return [{
  21787. index : attributesNone.position3DAndHeight,
  21788. vertexBuffer : buffer,
  21789. componentDatatype : datatype,
  21790. componentsPerAttribute : position3DAndHeightLength,
  21791. offsetInBytes : 0,
  21792. strideInBytes : stride
  21793. }, {
  21794. index : attributesNone.textureCoordAndEncodedNormals,
  21795. vertexBuffer : buffer,
  21796. componentDatatype : datatype,
  21797. componentsPerAttribute : numTexCoordComponents,
  21798. offsetInBytes : position3DAndHeightLength * sizeInBytes,
  21799. strideInBytes : stride
  21800. }];
  21801. }
  21802. var numCompressed0 = 3;
  21803. var numCompressed1 = 0;
  21804. if (this.hasWebMercatorT || this.hasVertexNormals) {
  21805. ++numCompressed0;
  21806. }
  21807. if (this.hasWebMercatorT && this.hasVertexNormals) {
  21808. ++numCompressed1;
  21809. stride = (numCompressed0 + numCompressed1) * sizeInBytes;
  21810. return [{
  21811. index : attributes.compressed0,
  21812. vertexBuffer : buffer,
  21813. componentDatatype : datatype,
  21814. componentsPerAttribute : numCompressed0,
  21815. offsetInBytes : 0,
  21816. strideInBytes : stride
  21817. }, {
  21818. index : attributes.compressed1,
  21819. vertexBuffer : buffer,
  21820. componentDatatype : datatype,
  21821. componentsPerAttribute : numCompressed1,
  21822. offsetInBytes : numCompressed0 * sizeInBytes,
  21823. strideInBytes : stride
  21824. }];
  21825. } else {
  21826. return [{
  21827. index : attributes.compressed0,
  21828. vertexBuffer : buffer,
  21829. componentDatatype : datatype,
  21830. componentsPerAttribute : numCompressed0
  21831. }];
  21832. }
  21833. };
  21834. TerrainEncoding.prototype.getAttributeLocations = function() {
  21835. if (this.quantization === TerrainQuantization.NONE) {
  21836. return attributesNone;
  21837. } else {
  21838. return attributes;
  21839. }
  21840. };
  21841. TerrainEncoding.clone = function(encoding, result) {
  21842. if (!defined(result)) {
  21843. result = new TerrainEncoding();
  21844. }
  21845. result.quantization = encoding.quantization;
  21846. result.minimumHeight = encoding.minimumHeight;
  21847. result.maximumHeight = encoding.maximumHeight;
  21848. result.center = Cartesian3.clone(encoding.center);
  21849. result.toScaledENU = Matrix4.clone(encoding.toScaledENU);
  21850. result.fromScaledENU = Matrix4.clone(encoding.fromScaledENU);
  21851. result.matrix = Matrix4.clone(encoding.matrix);
  21852. result.hasVertexNormals = encoding.hasVertexNormals;
  21853. result.hasWebMercatorT = encoding.hasWebMercatorT;
  21854. return result;
  21855. };
  21856. return TerrainEncoding;
  21857. });
  21858. /*global define*/
  21859. define('Core/formatError',[
  21860. './defined'
  21861. ], function(
  21862. defined) {
  21863. 'use strict';
  21864. /**
  21865. * Formats an error object into a String. If available, uses name, message, and stack
  21866. * properties, otherwise, falls back on toString().
  21867. *
  21868. * @exports formatError
  21869. *
  21870. * @param {Object} object The item to find in the array.
  21871. * @returns {String} A string containing the formatted error.
  21872. */
  21873. function formatError(object) {
  21874. var result;
  21875. var name = object.name;
  21876. var message = object.message;
  21877. if (defined(name) && defined(message)) {
  21878. result = name + ': ' + message;
  21879. } else {
  21880. result = object.toString();
  21881. }
  21882. var stack = object.stack;
  21883. if (defined(stack)) {
  21884. result += '\n' + stack;
  21885. }
  21886. return result;
  21887. }
  21888. return formatError;
  21889. });
  21890. /*global define*/
  21891. define('Workers/createTaskProcessorWorker',[
  21892. '../Core/defaultValue',
  21893. '../Core/defined',
  21894. '../Core/formatError'
  21895. ], function(
  21896. defaultValue,
  21897. defined,
  21898. formatError) {
  21899. 'use strict';
  21900. /**
  21901. * Creates an adapter function to allow a calculation function to operate as a Web Worker,
  21902. * paired with TaskProcessor, to receive tasks and return results.
  21903. *
  21904. * @exports createTaskProcessorWorker
  21905. *
  21906. * @param {createTaskProcessorWorker~WorkerFunction} workerFunction The calculation function,
  21907. * which takes parameters and returns a result.
  21908. * @returns {createTaskProcessorWorker~TaskProcessorWorkerFunction} A function that adapts the
  21909. * calculation function to work as a Web Worker onmessage listener with TaskProcessor.
  21910. *
  21911. *
  21912. * @example
  21913. * function doCalculation(parameters, transferableObjects) {
  21914. * // calculate some result using the inputs in parameters
  21915. * return result;
  21916. * }
  21917. *
  21918. * return Cesium.createTaskProcessorWorker(doCalculation);
  21919. * // the resulting function is compatible with TaskProcessor
  21920. *
  21921. * @see TaskProcessor
  21922. * @see {@link http://www.w3.org/TR/workers/|Web Workers}
  21923. * @see {@link http://www.w3.org/TR/html5/common-dom-interfaces.html#transferable-objects|Transferable objects}
  21924. */
  21925. function createTaskProcessorWorker(workerFunction) {
  21926. var postMessage;
  21927. var transferableObjects = [];
  21928. var responseMessage = {
  21929. id : undefined,
  21930. result : undefined,
  21931. error : undefined
  21932. };
  21933. return function(event) {
  21934. /*global self*/
  21935. var data = event.data;
  21936. transferableObjects.length = 0;
  21937. responseMessage.id = data.id;
  21938. responseMessage.error = undefined;
  21939. responseMessage.result = undefined;
  21940. try {
  21941. responseMessage.result = workerFunction(data.parameters, transferableObjects);
  21942. } catch (e) {
  21943. if (e instanceof Error) {
  21944. // Errors can't be posted in a message, copy the properties
  21945. responseMessage.error = {
  21946. name : e.name,
  21947. message : e.message,
  21948. stack : e.stack
  21949. };
  21950. } else {
  21951. responseMessage.error = e;
  21952. }
  21953. }
  21954. if (!defined(postMessage)) {
  21955. postMessage = defaultValue(self.webkitPostMessage, self.postMessage);
  21956. }
  21957. if (!data.canTransferArrayBuffer) {
  21958. transferableObjects.length = 0;
  21959. }
  21960. try {
  21961. postMessage(responseMessage, transferableObjects);
  21962. } catch (e) {
  21963. // something went wrong trying to post the message, post a simpler
  21964. // error that we can be sure will be cloneable
  21965. responseMessage.result = undefined;
  21966. responseMessage.error = 'postMessage failed with error: ' + formatError(e) + '\n with responseMessage: ' + JSON.stringify(responseMessage);
  21967. postMessage(responseMessage);
  21968. }
  21969. };
  21970. }
  21971. /**
  21972. * A function that performs a calculation in a Web Worker.
  21973. * @callback createTaskProcessorWorker~WorkerFunction
  21974. *
  21975. * @param {Object} parameters Parameters to the calculation.
  21976. * @param {Array} transferableObjects An array that should be filled with references to objects inside
  21977. * the result that should be transferred back to the main document instead of copied.
  21978. * @returns {Object} The result of the calculation.
  21979. *
  21980. * @example
  21981. * function calculate(parameters, transferableObjects) {
  21982. * // perform whatever calculation is necessary.
  21983. * var typedArray = new Float32Array(0);
  21984. *
  21985. * // typed arrays are transferable
  21986. * transferableObjects.push(typedArray)
  21987. *
  21988. * return {
  21989. * typedArray : typedArray
  21990. * };
  21991. * }
  21992. */
  21993. /**
  21994. * A Web Worker message event handler function that handles the interaction with TaskProcessor,
  21995. * specifically, task ID management and posting a response message containing the result.
  21996. * @callback createTaskProcessorWorker~TaskProcessorWorkerFunction
  21997. *
  21998. * @param {Object} event The onmessage event object.
  21999. */
  22000. return createTaskProcessorWorker;
  22001. });
  22002. /*global define*/
  22003. define('Workers/upsampleQuantizedTerrainMesh',[
  22004. '../Core/AttributeCompression',
  22005. '../Core/BoundingSphere',
  22006. '../Core/Cartesian2',
  22007. '../Core/Cartesian3',
  22008. '../Core/Cartographic',
  22009. '../Core/defined',
  22010. '../Core/Ellipsoid',
  22011. '../Core/EllipsoidalOccluder',
  22012. '../Core/IndexDatatype',
  22013. '../Core/Intersections2D',
  22014. '../Core/Math',
  22015. '../Core/OrientedBoundingBox',
  22016. '../Core/TerrainEncoding',
  22017. './createTaskProcessorWorker'
  22018. ], function(
  22019. AttributeCompression,
  22020. BoundingSphere,
  22021. Cartesian2,
  22022. Cartesian3,
  22023. Cartographic,
  22024. defined,
  22025. Ellipsoid,
  22026. EllipsoidalOccluder,
  22027. IndexDatatype,
  22028. Intersections2D,
  22029. CesiumMath,
  22030. OrientedBoundingBox,
  22031. TerrainEncoding,
  22032. createTaskProcessorWorker) {
  22033. 'use strict';
  22034. var maxShort = 32767;
  22035. var halfMaxShort = (maxShort / 2) | 0;
  22036. var clipScratch = [];
  22037. var clipScratch2 = [];
  22038. var verticesScratch = [];
  22039. var cartographicScratch = new Cartographic();
  22040. var cartesian3Scratch = new Cartesian3();
  22041. var uScratch = [];
  22042. var vScratch = [];
  22043. var heightScratch = [];
  22044. var indicesScratch = [];
  22045. var normalsScratch = [];
  22046. var horizonOcclusionPointScratch = new Cartesian3();
  22047. var boundingSphereScratch = new BoundingSphere();
  22048. var orientedBoundingBoxScratch = new OrientedBoundingBox();
  22049. var decodeTexCoordsScratch = new Cartesian2();
  22050. var octEncodedNormalScratch = new Cartesian3();
  22051. function upsampleQuantizedTerrainMesh(parameters, transferableObjects) {
  22052. var isEastChild = parameters.isEastChild;
  22053. var isNorthChild = parameters.isNorthChild;
  22054. var minU = isEastChild ? halfMaxShort : 0;
  22055. var maxU = isEastChild ? maxShort : halfMaxShort;
  22056. var minV = isNorthChild ? halfMaxShort : 0;
  22057. var maxV = isNorthChild ? maxShort : halfMaxShort;
  22058. var uBuffer = uScratch;
  22059. var vBuffer = vScratch;
  22060. var heightBuffer = heightScratch;
  22061. var normalBuffer = normalsScratch;
  22062. uBuffer.length = 0;
  22063. vBuffer.length = 0;
  22064. heightBuffer.length = 0;
  22065. normalBuffer.length = 0;
  22066. var indices = indicesScratch;
  22067. indices.length = 0;
  22068. var vertexMap = {};
  22069. var parentVertices = parameters.vertices;
  22070. var parentIndices = parameters.indices;
  22071. parentIndices = parentIndices.subarray(0, parameters.skirtIndex);
  22072. var encoding = TerrainEncoding.clone(parameters.encoding);
  22073. var hasVertexNormals = encoding.hasVertexNormals;
  22074. var exaggeration = parameters.exaggeration;
  22075. var vertexCount = 0;
  22076. var quantizedVertexCount = parameters.vertexCountWithoutSkirts;
  22077. var parentMinimumHeight = parameters.minimumHeight;
  22078. var parentMaximumHeight = parameters.maximumHeight;
  22079. var parentUBuffer = new Array(quantizedVertexCount);
  22080. var parentVBuffer = new Array(quantizedVertexCount);
  22081. var parentHeightBuffer = new Array(quantizedVertexCount);
  22082. var parentNormalBuffer = hasVertexNormals ? new Array(quantizedVertexCount * 2) : undefined;
  22083. var threshold = 20;
  22084. var height;
  22085. var i, n;
  22086. for (i = 0, n = 0; i < quantizedVertexCount; ++i, n += 2) {
  22087. var texCoords = encoding.decodeTextureCoordinates(parentVertices, i, decodeTexCoordsScratch);
  22088. height = encoding.decodeHeight(parentVertices, i) / exaggeration;
  22089. parentUBuffer[i] = CesiumMath.clamp((texCoords.x * maxShort) | 0, 0, maxShort);
  22090. parentVBuffer[i] = CesiumMath.clamp((texCoords.y * maxShort) | 0, 0, maxShort);
  22091. parentHeightBuffer[i] = CesiumMath.clamp((((height - parentMinimumHeight) / (parentMaximumHeight - parentMinimumHeight)) * maxShort) | 0, 0, maxShort);
  22092. if (parentUBuffer[i] < threshold) {
  22093. parentUBuffer[i] = 0;
  22094. }
  22095. if (parentVBuffer[i] < threshold) {
  22096. parentVBuffer[i] = 0;
  22097. }
  22098. if (maxShort - parentUBuffer[i] < threshold) {
  22099. parentUBuffer[i] = maxShort;
  22100. }
  22101. if (maxShort - parentVBuffer[i] < threshold) {
  22102. parentVBuffer[i] = maxShort;
  22103. }
  22104. if (hasVertexNormals) {
  22105. var encodedNormal = encoding.getOctEncodedNormal(parentVertices, i, octEncodedNormalScratch);
  22106. parentNormalBuffer[n] = encodedNormal.x;
  22107. parentNormalBuffer[n + 1] = encodedNormal.y;
  22108. }
  22109. }
  22110. var u, v;
  22111. for (i = 0, n = 0; i < quantizedVertexCount; ++i, n += 2) {
  22112. u = parentUBuffer[i];
  22113. v = parentVBuffer[i];
  22114. if ((isEastChild && u >= halfMaxShort || !isEastChild && u <= halfMaxShort) &&
  22115. (isNorthChild && v >= halfMaxShort || !isNorthChild && v <= halfMaxShort)) {
  22116. vertexMap[i] = vertexCount;
  22117. uBuffer.push(u);
  22118. vBuffer.push(v);
  22119. heightBuffer.push(parentHeightBuffer[i]);
  22120. if (hasVertexNormals) {
  22121. normalBuffer.push(parentNormalBuffer[n]);
  22122. normalBuffer.push(parentNormalBuffer[n + 1]);
  22123. }
  22124. ++vertexCount;
  22125. }
  22126. }
  22127. var triangleVertices = [];
  22128. triangleVertices.push(new Vertex());
  22129. triangleVertices.push(new Vertex());
  22130. triangleVertices.push(new Vertex());
  22131. var clippedTriangleVertices = [];
  22132. clippedTriangleVertices.push(new Vertex());
  22133. clippedTriangleVertices.push(new Vertex());
  22134. clippedTriangleVertices.push(new Vertex());
  22135. var clippedIndex;
  22136. var clipped2;
  22137. for (i = 0; i < parentIndices.length; i += 3) {
  22138. var i0 = parentIndices[i];
  22139. var i1 = parentIndices[i + 1];
  22140. var i2 = parentIndices[i + 2];
  22141. var u0 = parentUBuffer[i0];
  22142. var u1 = parentUBuffer[i1];
  22143. var u2 = parentUBuffer[i2];
  22144. triangleVertices[0].initializeIndexed(parentUBuffer, parentVBuffer, parentHeightBuffer, parentNormalBuffer, i0);
  22145. triangleVertices[1].initializeIndexed(parentUBuffer, parentVBuffer, parentHeightBuffer, parentNormalBuffer, i1);
  22146. triangleVertices[2].initializeIndexed(parentUBuffer, parentVBuffer, parentHeightBuffer, parentNormalBuffer, i2);
  22147. // Clip triangle on the east-west boundary.
  22148. var clipped = Intersections2D.clipTriangleAtAxisAlignedThreshold(halfMaxShort, isEastChild, u0, u1, u2, clipScratch);
  22149. // Get the first clipped triangle, if any.
  22150. clippedIndex = 0;
  22151. if (clippedIndex >= clipped.length) {
  22152. continue;
  22153. }
  22154. clippedIndex = clippedTriangleVertices[0].initializeFromClipResult(clipped, clippedIndex, triangleVertices);
  22155. if (clippedIndex >= clipped.length) {
  22156. continue;
  22157. }
  22158. clippedIndex = clippedTriangleVertices[1].initializeFromClipResult(clipped, clippedIndex, triangleVertices);
  22159. if (clippedIndex >= clipped.length) {
  22160. continue;
  22161. }
  22162. clippedIndex = clippedTriangleVertices[2].initializeFromClipResult(clipped, clippedIndex, triangleVertices);
  22163. // Clip the triangle against the North-south boundary.
  22164. clipped2 = Intersections2D.clipTriangleAtAxisAlignedThreshold(halfMaxShort, isNorthChild, clippedTriangleVertices[0].getV(), clippedTriangleVertices[1].getV(), clippedTriangleVertices[2].getV(), clipScratch2);
  22165. addClippedPolygon(uBuffer, vBuffer, heightBuffer, normalBuffer, indices, vertexMap, clipped2, clippedTriangleVertices, hasVertexNormals);
  22166. // If there's another vertex in the original clipped result,
  22167. // it forms a second triangle. Clip it as well.
  22168. if (clippedIndex < clipped.length) {
  22169. clippedTriangleVertices[2].clone(clippedTriangleVertices[1]);
  22170. clippedTriangleVertices[2].initializeFromClipResult(clipped, clippedIndex, triangleVertices);
  22171. clipped2 = Intersections2D.clipTriangleAtAxisAlignedThreshold(halfMaxShort, isNorthChild, clippedTriangleVertices[0].getV(), clippedTriangleVertices[1].getV(), clippedTriangleVertices[2].getV(), clipScratch2);
  22172. addClippedPolygon(uBuffer, vBuffer, heightBuffer, normalBuffer, indices, vertexMap, clipped2, clippedTriangleVertices, hasVertexNormals);
  22173. }
  22174. }
  22175. var uOffset = isEastChild ? -maxShort : 0;
  22176. var vOffset = isNorthChild ? -maxShort : 0;
  22177. var westIndices = [];
  22178. var southIndices = [];
  22179. var eastIndices = [];
  22180. var northIndices = [];
  22181. var minimumHeight = Number.MAX_VALUE;
  22182. var maximumHeight = -minimumHeight;
  22183. var cartesianVertices = verticesScratch;
  22184. cartesianVertices.length = 0;
  22185. var ellipsoid = Ellipsoid.clone(parameters.ellipsoid);
  22186. var rectangle = parameters.childRectangle;
  22187. var north = rectangle.north;
  22188. var south = rectangle.south;
  22189. var east = rectangle.east;
  22190. var west = rectangle.west;
  22191. if (east < west) {
  22192. east += CesiumMath.TWO_PI;
  22193. }
  22194. for (i = 0; i < uBuffer.length; ++i) {
  22195. u = Math.round(uBuffer[i]);
  22196. if (u <= minU) {
  22197. westIndices.push(i);
  22198. u = 0;
  22199. } else if (u >= maxU) {
  22200. eastIndices.push(i);
  22201. u = maxShort;
  22202. } else {
  22203. u = u * 2 + uOffset;
  22204. }
  22205. uBuffer[i] = u;
  22206. v = Math.round(vBuffer[i]);
  22207. if (v <= minV) {
  22208. southIndices.push(i);
  22209. v = 0;
  22210. } else if (v >= maxV) {
  22211. northIndices.push(i);
  22212. v = maxShort;
  22213. } else {
  22214. v = v * 2 + vOffset;
  22215. }
  22216. vBuffer[i] = v;
  22217. height = CesiumMath.lerp(parentMinimumHeight, parentMaximumHeight, heightBuffer[i] / maxShort);
  22218. if (height < minimumHeight) {
  22219. minimumHeight = height;
  22220. }
  22221. if (height > maximumHeight) {
  22222. maximumHeight = height;
  22223. }
  22224. heightBuffer[i] = height;
  22225. cartographicScratch.longitude = CesiumMath.lerp(west, east, u / maxShort);
  22226. cartographicScratch.latitude = CesiumMath.lerp(south, north, v / maxShort);
  22227. cartographicScratch.height = height;
  22228. ellipsoid.cartographicToCartesian(cartographicScratch, cartesian3Scratch);
  22229. cartesianVertices.push(cartesian3Scratch.x);
  22230. cartesianVertices.push(cartesian3Scratch.y);
  22231. cartesianVertices.push(cartesian3Scratch.z);
  22232. }
  22233. var boundingSphere = BoundingSphere.fromVertices(cartesianVertices, Cartesian3.ZERO, 3, boundingSphereScratch);
  22234. var orientedBoundingBox = OrientedBoundingBox.fromRectangle(rectangle, minimumHeight, maximumHeight, ellipsoid, orientedBoundingBoxScratch);
  22235. var occluder = new EllipsoidalOccluder(ellipsoid);
  22236. var horizonOcclusionPoint = occluder.computeHorizonCullingPointFromVertices(boundingSphere.center, cartesianVertices, 3, boundingSphere.center, horizonOcclusionPointScratch);
  22237. var heightRange = maximumHeight - minimumHeight;
  22238. var vertices = new Uint16Array(uBuffer.length + vBuffer.length + heightBuffer.length);
  22239. for (i = 0; i < uBuffer.length; ++i) {
  22240. vertices[i] = uBuffer[i];
  22241. }
  22242. var start = uBuffer.length;
  22243. for (i = 0; i < vBuffer.length; ++i) {
  22244. vertices[start + i] = vBuffer[i];
  22245. }
  22246. start += vBuffer.length;
  22247. for (i = 0; i < heightBuffer.length; ++i) {
  22248. vertices[start + i] = maxShort * (heightBuffer[i] - minimumHeight) / heightRange;
  22249. }
  22250. var indicesTypedArray = IndexDatatype.createTypedArray(uBuffer.length, indices);
  22251. var encodedNormals;
  22252. if (hasVertexNormals) {
  22253. var normalArray = new Uint8Array(normalBuffer);
  22254. transferableObjects.push(vertices.buffer, indicesTypedArray.buffer, normalArray.buffer);
  22255. encodedNormals = normalArray.buffer;
  22256. } else {
  22257. transferableObjects.push(vertices.buffer, indicesTypedArray.buffer);
  22258. }
  22259. return {
  22260. vertices : vertices.buffer,
  22261. encodedNormals : encodedNormals,
  22262. indices : indicesTypedArray.buffer,
  22263. minimumHeight : minimumHeight,
  22264. maximumHeight : maximumHeight,
  22265. westIndices : westIndices,
  22266. southIndices : southIndices,
  22267. eastIndices : eastIndices,
  22268. northIndices : northIndices,
  22269. boundingSphere : boundingSphere,
  22270. orientedBoundingBox : orientedBoundingBox,
  22271. horizonOcclusionPoint : horizonOcclusionPoint
  22272. };
  22273. }
  22274. function Vertex() {
  22275. this.vertexBuffer = undefined;
  22276. this.index = undefined;
  22277. this.first = undefined;
  22278. this.second = undefined;
  22279. this.ratio = undefined;
  22280. }
  22281. Vertex.prototype.clone = function(result) {
  22282. if (!defined(result)) {
  22283. result = new Vertex();
  22284. }
  22285. result.uBuffer = this.uBuffer;
  22286. result.vBuffer = this.vBuffer;
  22287. result.heightBuffer = this.heightBuffer;
  22288. result.normalBuffer = this.normalBuffer;
  22289. result.index = this.index;
  22290. result.first = this.first;
  22291. result.second = this.second;
  22292. result.ratio = this.ratio;
  22293. return result;
  22294. };
  22295. Vertex.prototype.initializeIndexed = function(uBuffer, vBuffer, heightBuffer, normalBuffer, index) {
  22296. this.uBuffer = uBuffer;
  22297. this.vBuffer = vBuffer;
  22298. this.heightBuffer = heightBuffer;
  22299. this.normalBuffer = normalBuffer;
  22300. this.index = index;
  22301. this.first = undefined;
  22302. this.second = undefined;
  22303. this.ratio = undefined;
  22304. };
  22305. Vertex.prototype.initializeFromClipResult = function(clipResult, index, vertices) {
  22306. var nextIndex = index + 1;
  22307. if (clipResult[index] !== -1) {
  22308. vertices[clipResult[index]].clone(this);
  22309. } else {
  22310. this.vertexBuffer = undefined;
  22311. this.index = undefined;
  22312. this.first = vertices[clipResult[nextIndex]];
  22313. ++nextIndex;
  22314. this.second = vertices[clipResult[nextIndex]];
  22315. ++nextIndex;
  22316. this.ratio = clipResult[nextIndex];
  22317. ++nextIndex;
  22318. }
  22319. return nextIndex;
  22320. };
  22321. Vertex.prototype.getKey = function() {
  22322. if (this.isIndexed()) {
  22323. return this.index;
  22324. }
  22325. return JSON.stringify({
  22326. first : this.first.getKey(),
  22327. second : this.second.getKey(),
  22328. ratio : this.ratio
  22329. });
  22330. };
  22331. Vertex.prototype.isIndexed = function() {
  22332. return defined(this.index);
  22333. };
  22334. Vertex.prototype.getH = function() {
  22335. if (defined(this.index)) {
  22336. return this.heightBuffer[this.index];
  22337. }
  22338. return CesiumMath.lerp(this.first.getH(), this.second.getH(), this.ratio);
  22339. };
  22340. Vertex.prototype.getU = function() {
  22341. if (defined(this.index)) {
  22342. return this.uBuffer[this.index];
  22343. }
  22344. return CesiumMath.lerp(this.first.getU(), this.second.getU(), this.ratio);
  22345. };
  22346. Vertex.prototype.getV = function() {
  22347. if (defined(this.index)) {
  22348. return this.vBuffer[this.index];
  22349. }
  22350. return CesiumMath.lerp(this.first.getV(), this.second.getV(), this.ratio);
  22351. };
  22352. var encodedScratch = new Cartesian2();
  22353. // An upsampled triangle may be clipped twice before it is assigned an index
  22354. // In this case, we need a buffer to handle the recursion of getNormalX() and getNormalY().
  22355. var depth = -1;
  22356. var cartesianScratch1 = [new Cartesian3(), new Cartesian3()];
  22357. var cartesianScratch2 = [new Cartesian3(), new Cartesian3()];
  22358. function lerpOctEncodedNormal(vertex, result) {
  22359. ++depth;
  22360. var first = cartesianScratch1[depth];
  22361. var second = cartesianScratch2[depth];
  22362. first = AttributeCompression.octDecode(vertex.first.getNormalX(), vertex.first.getNormalY(), first);
  22363. second = AttributeCompression.octDecode(vertex.second.getNormalX(), vertex.second.getNormalY(), second);
  22364. cartesian3Scratch = Cartesian3.lerp(first, second, vertex.ratio, cartesian3Scratch);
  22365. Cartesian3.normalize(cartesian3Scratch, cartesian3Scratch);
  22366. AttributeCompression.octEncode(cartesian3Scratch, result);
  22367. --depth;
  22368. return result;
  22369. }
  22370. Vertex.prototype.getNormalX = function() {
  22371. if (defined(this.index)) {
  22372. return this.normalBuffer[this.index * 2];
  22373. }
  22374. encodedScratch = lerpOctEncodedNormal(this, encodedScratch);
  22375. return encodedScratch.x;
  22376. };
  22377. Vertex.prototype.getNormalY = function() {
  22378. if (defined(this.index)) {
  22379. return this.normalBuffer[this.index * 2 + 1];
  22380. }
  22381. encodedScratch = lerpOctEncodedNormal(this, encodedScratch);
  22382. return encodedScratch.y;
  22383. };
  22384. var polygonVertices = [];
  22385. polygonVertices.push(new Vertex());
  22386. polygonVertices.push(new Vertex());
  22387. polygonVertices.push(new Vertex());
  22388. polygonVertices.push(new Vertex());
  22389. function addClippedPolygon(uBuffer, vBuffer, heightBuffer, normalBuffer, indices, vertexMap, clipped, triangleVertices, hasVertexNormals) {
  22390. if (clipped.length === 0) {
  22391. return;
  22392. }
  22393. var numVertices = 0;
  22394. var clippedIndex = 0;
  22395. while (clippedIndex < clipped.length) {
  22396. clippedIndex = polygonVertices[numVertices++].initializeFromClipResult(clipped, clippedIndex, triangleVertices);
  22397. }
  22398. for (var i = 0; i < numVertices; ++i) {
  22399. var polygonVertex = polygonVertices[i];
  22400. if (!polygonVertex.isIndexed()) {
  22401. var key = polygonVertex.getKey();
  22402. if (defined(vertexMap[key])) {
  22403. polygonVertex.newIndex = vertexMap[key];
  22404. } else {
  22405. var newIndex = uBuffer.length;
  22406. uBuffer.push(polygonVertex.getU());
  22407. vBuffer.push(polygonVertex.getV());
  22408. heightBuffer.push(polygonVertex.getH());
  22409. if (hasVertexNormals) {
  22410. normalBuffer.push(polygonVertex.getNormalX());
  22411. normalBuffer.push(polygonVertex.getNormalY());
  22412. }
  22413. polygonVertex.newIndex = newIndex;
  22414. vertexMap[key] = newIndex;
  22415. }
  22416. } else {
  22417. polygonVertex.newIndex = vertexMap[polygonVertex.index];
  22418. polygonVertex.uBuffer = uBuffer;
  22419. polygonVertex.vBuffer = vBuffer;
  22420. polygonVertex.heightBuffer = heightBuffer;
  22421. if (hasVertexNormals) {
  22422. polygonVertex.normalBuffer = normalBuffer;
  22423. }
  22424. }
  22425. }
  22426. if (numVertices === 3) {
  22427. // A triangle.
  22428. indices.push(polygonVertices[0].newIndex);
  22429. indices.push(polygonVertices[1].newIndex);
  22430. indices.push(polygonVertices[2].newIndex);
  22431. } else if (numVertices === 4) {
  22432. // A quad - two triangles.
  22433. indices.push(polygonVertices[0].newIndex);
  22434. indices.push(polygonVertices[1].newIndex);
  22435. indices.push(polygonVertices[2].newIndex);
  22436. indices.push(polygonVertices[0].newIndex);
  22437. indices.push(polygonVertices[2].newIndex);
  22438. indices.push(polygonVertices[3].newIndex);
  22439. }
  22440. }
  22441. return createTaskProcessorWorker(upsampleQuantizedTerrainMesh);
  22442. });
  22443. }());