createWallOutlineGeometry.js 932 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443
  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. /*global define*/
  171. define('Core/isArray',[
  172. './defined'
  173. ], function(
  174. defined) {
  175. 'use strict';
  176. /**
  177. * Tests an object to see if it is an array.
  178. * @exports isArray
  179. *
  180. * @param {Object} value The value to test.
  181. * @returns {Boolean} true if the value is an array, false otherwise.
  182. */
  183. var isArray = Array.isArray;
  184. if (!defined(isArray)) {
  185. isArray = function(value) {
  186. return Object.prototype.toString.call(value) === '[object Array]';
  187. };
  188. }
  189. return isArray;
  190. });
  191. /*global define*/
  192. define('Core/Check',[
  193. './defaultValue',
  194. './defined',
  195. './DeveloperError',
  196. './isArray'
  197. ], function(
  198. defaultValue,
  199. defined,
  200. DeveloperError,
  201. isArray) {
  202. 'use strict';
  203. /**
  204. * Contains functions for checking that supplied arguments are of a specified type
  205. * or meet specified conditions
  206. * @private
  207. */
  208. var Check = {};
  209. /**
  210. * Contains type checking functions, all using the typeof operator
  211. */
  212. Check.typeOf = {};
  213. /**
  214. * Contains functions for checking numeric conditions such as minimum and maximum values
  215. */
  216. Check.numeric = {};
  217. function getUndefinedErrorMessage(name) {
  218. return name + ' was required but undefined.';
  219. }
  220. function getFailedTypeErrorMessage(actual, expected, name) {
  221. return 'Expected ' + name + ' to be typeof ' + expected + ', got ' + actual;
  222. }
  223. /**
  224. * Throws if test is not defined
  225. *
  226. * @param {*} test The value that is to be checked
  227. * @param {String} name The name of the variable being tested
  228. * @exception {DeveloperError} test must be defined
  229. */
  230. Check.defined = function (test, name) {
  231. if (!defined(test)) {
  232. throw new DeveloperError(getUndefinedErrorMessage(name));
  233. }
  234. };
  235. /**
  236. * Throws if test is greater than maximum
  237. *
  238. * @param {Number} test The value to test
  239. * @param {Number} maximum The maximum allowed value
  240. * @exception {DeveloperError} test must not be greater than maximum
  241. * @exception {DeveloperError} Both test and maximum must be typeof 'number'
  242. */
  243. Check.numeric.maximum = function (test, maximum) {
  244. Check.typeOf.number(test);
  245. Check.typeOf.number(maximum);
  246. if (test > maximum) {
  247. throw new DeveloperError('Expected ' + test + ' to be at most ' + maximum);
  248. }
  249. };
  250. /**
  251. * Throws if test is less than minimum
  252. *
  253. * @param {Number} test The value to test
  254. * @param {Number} minimum The minimum allowed value
  255. * @exception {DeveloperError} test must not be less than mininum
  256. * @exception {DeveloperError} Both test and maximum must be typeof 'number'
  257. */
  258. Check.numeric.minimum = function (test, minimum) {
  259. Check.typeOf.number(test);
  260. Check.typeOf.number(minimum);
  261. if (test < minimum) {
  262. throw new DeveloperError('Expected ' + test + ' to be at least ' + minimum);
  263. }
  264. };
  265. /**
  266. * Throws if test is not typeof 'function'
  267. *
  268. * @param {*} test The value to test
  269. * @param {String} name The name of the variable being tested
  270. * @exception {DeveloperError} test must be typeof 'function'
  271. */
  272. Check.typeOf.function = function (test, name) {
  273. if (typeof test !== 'function') {
  274. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'function', name));
  275. }
  276. };
  277. /**
  278. * Throws if test is not typeof 'string'
  279. *
  280. * @param {*} test The value to test
  281. * @param {String} name The name of the variable being tested
  282. * @exception {DeveloperError} test must be typeof 'string'
  283. */
  284. Check.typeOf.string = function (test, name) {
  285. if (typeof test !== 'string') {
  286. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'string', name));
  287. }
  288. };
  289. /**
  290. * Throws if test is not typeof 'number'
  291. *
  292. * @param {*} test The value to test
  293. * @param {String} name The name of the variable being tested
  294. * @exception {DeveloperError} test must be typeof 'number'
  295. */
  296. Check.typeOf.number = function (test, name) {
  297. if (typeof test !== 'number') {
  298. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'number', name));
  299. }
  300. };
  301. /**
  302. * Throws if test is not typeof 'object'
  303. *
  304. * @param {*} test The value to test
  305. * @param {String} name The name of the variable being tested
  306. * @exception {DeveloperError} test must be typeof 'object'
  307. */
  308. Check.typeOf.object = function (test, name) {
  309. if (typeof test !== 'object') {
  310. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'object', name));
  311. }
  312. };
  313. /**
  314. * Throws if test is not typeof 'boolean'
  315. *
  316. * @param {*} test The value to test
  317. * @param {String} name The name of the variable being tested
  318. * @exception {DeveloperError} test must be typeof 'boolean'
  319. */
  320. Check.typeOf.boolean = function (test, name) {
  321. if (typeof test !== 'boolean') {
  322. throw new DeveloperError(getFailedTypeErrorMessage(typeof test, 'boolean', name));
  323. }
  324. };
  325. return Check;
  326. });
  327. /*
  328. I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
  329. so it's better encapsulated. Now you can have multiple random number generators
  330. and they won't stomp all over eachother's state.
  331. If you want to use this as a substitute for Math.random(), use the random()
  332. method like so:
  333. var m = new MersenneTwister();
  334. var randomNumber = m.random();
  335. You can also call the other genrand_{foo}() methods on the instance.
  336. If you want to use a specific seed in order to get a repeatable random
  337. sequence, pass an integer into the constructor:
  338. var m = new MersenneTwister(123);
  339. and that will always produce the same random sequence.
  340. Sean McCullough (banksean@gmail.com)
  341. */
  342. /*
  343. A C-program for MT19937, with initialization improved 2002/1/26.
  344. Coded by Takuji Nishimura and Makoto Matsumoto.
  345. Before using, initialize the state by using init_genrand(seed)
  346. or init_by_array(init_key, key_length).
  347. */
  348. /**
  349. @license
  350. mersenne-twister.js - https://gist.github.com/banksean/300494
  351. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  352. All rights reserved.
  353. Redistribution and use in source and binary forms, with or without
  354. modification, are permitted provided that the following conditions
  355. are met:
  356. 1. Redistributions of source code must retain the above copyright
  357. notice, this list of conditions and the following disclaimer.
  358. 2. Redistributions in binary form must reproduce the above copyright
  359. notice, this list of conditions and the following disclaimer in the
  360. documentation and/or other materials provided with the distribution.
  361. 3. The names of its contributors may not be used to endorse or promote
  362. products derived from this software without specific prior written
  363. permission.
  364. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  365. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  366. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  367. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  368. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  369. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  370. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  371. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  372. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  373. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  374. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  375. */
  376. /*
  377. Any feedback is very welcome.
  378. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  379. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  380. */
  381. define('ThirdParty/mersenne-twister',[],function() {
  382. var MersenneTwister = function(seed) {
  383. if (seed == undefined) {
  384. seed = new Date().getTime();
  385. }
  386. /* Period parameters */
  387. this.N = 624;
  388. this.M = 397;
  389. this.MATRIX_A = 0x9908b0df; /* constant vector a */
  390. this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
  391. this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
  392. this.mt = new Array(this.N); /* the array for the state vector */
  393. this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
  394. this.init_genrand(seed);
  395. }
  396. /* initializes mt[N] with a seed */
  397. MersenneTwister.prototype.init_genrand = function(s) {
  398. this.mt[0] = s >>> 0;
  399. for (this.mti=1; this.mti<this.N; this.mti++) {
  400. var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30);
  401. this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
  402. + this.mti;
  403. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  404. /* In the previous versions, MSBs of the seed affect */
  405. /* only MSBs of the array mt[]. */
  406. /* 2002/01/09 modified by Makoto Matsumoto */
  407. this.mt[this.mti] >>>= 0;
  408. /* for >32 bit machines */
  409. }
  410. }
  411. /* initialize by an array with array-length */
  412. /* init_key is the array for initializing keys */
  413. /* key_length is its length */
  414. /* slight change for C++, 2004/2/26 */
  415. //MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
  416. // var i, j, k;
  417. // this.init_genrand(19650218);
  418. // i=1; j=0;
  419. // k = (this.N>key_length ? this.N : key_length);
  420. // for (; k; k--) {
  421. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30)
  422. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))
  423. // + init_key[j] + j; /* non linear */
  424. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  425. // i++; j++;
  426. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  427. // if (j>=key_length) j=0;
  428. // }
  429. // for (k=this.N-1; k; k--) {
  430. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
  431. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))
  432. // - i; /* non linear */
  433. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  434. // i++;
  435. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  436. // }
  437. //
  438. // this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  439. //}
  440. /* generates a random number on [0,0xffffffff]-interval */
  441. MersenneTwister.prototype.genrand_int32 = function() {
  442. var y;
  443. var mag01 = new Array(0x0, this.MATRIX_A);
  444. /* mag01[x] = x * MATRIX_A for x=0,1 */
  445. if (this.mti >= this.N) { /* generate N words at one time */
  446. var kk;
  447. if (this.mti == this.N+1) /* if init_genrand() has not been called, */
  448. this.init_genrand(5489); /* a default initial seed is used */
  449. for (kk=0;kk<this.N-this.M;kk++) {
  450. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  451. this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
  452. }
  453. for (;kk<this.N-1;kk++) {
  454. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  455. this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  456. }
  457. y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK);
  458. this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
  459. this.mti = 0;
  460. }
  461. y = this.mt[this.mti++];
  462. /* Tempering */
  463. y ^= (y >>> 11);
  464. y ^= (y << 7) & 0x9d2c5680;
  465. y ^= (y << 15) & 0xefc60000;
  466. y ^= (y >>> 18);
  467. return y >>> 0;
  468. }
  469. /* generates a random number on [0,0x7fffffff]-interval */
  470. //MersenneTwister.prototype.genrand_int31 = function() {
  471. // return (this.genrand_int32()>>>1);
  472. //}
  473. /* generates a random number on [0,1]-real-interval */
  474. //MersenneTwister.prototype.genrand_real1 = function() {
  475. // return this.genrand_int32()*(1.0/4294967295.0);
  476. // /* divided by 2^32-1 */
  477. //}
  478. /* generates a random number on [0,1)-real-interval */
  479. MersenneTwister.prototype.random = function() {
  480. return this.genrand_int32()*(1.0/4294967296.0);
  481. /* divided by 2^32 */
  482. }
  483. /* generates a random number on (0,1)-real-interval */
  484. //MersenneTwister.prototype.genrand_real3 = function() {
  485. // return (this.genrand_int32() + 0.5)*(1.0/4294967296.0);
  486. // /* divided by 2^32 */
  487. //}
  488. /* generates a random number on [0,1) with 53-bit resolution*/
  489. //MersenneTwister.prototype.genrand_res53 = function() {
  490. // var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6;
  491. // return(a*67108864.0+b)*(1.0/9007199254740992.0);
  492. //}
  493. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  494. return MersenneTwister;
  495. });
  496. /*global define*/
  497. define('Core/Math',[
  498. '../ThirdParty/mersenne-twister',
  499. './defaultValue',
  500. './defined',
  501. './DeveloperError'
  502. ], function(
  503. MersenneTwister,
  504. defaultValue,
  505. defined,
  506. DeveloperError) {
  507. 'use strict';
  508. /**
  509. * Math functions.
  510. *
  511. * @exports CesiumMath
  512. */
  513. var CesiumMath = {};
  514. /**
  515. * 0.1
  516. * @type {Number}
  517. * @constant
  518. */
  519. CesiumMath.EPSILON1 = 0.1;
  520. /**
  521. * 0.01
  522. * @type {Number}
  523. * @constant
  524. */
  525. CesiumMath.EPSILON2 = 0.01;
  526. /**
  527. * 0.001
  528. * @type {Number}
  529. * @constant
  530. */
  531. CesiumMath.EPSILON3 = 0.001;
  532. /**
  533. * 0.0001
  534. * @type {Number}
  535. * @constant
  536. */
  537. CesiumMath.EPSILON4 = 0.0001;
  538. /**
  539. * 0.00001
  540. * @type {Number}
  541. * @constant
  542. */
  543. CesiumMath.EPSILON5 = 0.00001;
  544. /**
  545. * 0.000001
  546. * @type {Number}
  547. * @constant
  548. */
  549. CesiumMath.EPSILON6 = 0.000001;
  550. /**
  551. * 0.0000001
  552. * @type {Number}
  553. * @constant
  554. */
  555. CesiumMath.EPSILON7 = 0.0000001;
  556. /**
  557. * 0.00000001
  558. * @type {Number}
  559. * @constant
  560. */
  561. CesiumMath.EPSILON8 = 0.00000001;
  562. /**
  563. * 0.000000001
  564. * @type {Number}
  565. * @constant
  566. */
  567. CesiumMath.EPSILON9 = 0.000000001;
  568. /**
  569. * 0.0000000001
  570. * @type {Number}
  571. * @constant
  572. */
  573. CesiumMath.EPSILON10 = 0.0000000001;
  574. /**
  575. * 0.00000000001
  576. * @type {Number}
  577. * @constant
  578. */
  579. CesiumMath.EPSILON11 = 0.00000000001;
  580. /**
  581. * 0.000000000001
  582. * @type {Number}
  583. * @constant
  584. */
  585. CesiumMath.EPSILON12 = 0.000000000001;
  586. /**
  587. * 0.0000000000001
  588. * @type {Number}
  589. * @constant
  590. */
  591. CesiumMath.EPSILON13 = 0.0000000000001;
  592. /**
  593. * 0.00000000000001
  594. * @type {Number}
  595. * @constant
  596. */
  597. CesiumMath.EPSILON14 = 0.00000000000001;
  598. /**
  599. * 0.000000000000001
  600. * @type {Number}
  601. * @constant
  602. */
  603. CesiumMath.EPSILON15 = 0.000000000000001;
  604. /**
  605. * 0.0000000000000001
  606. * @type {Number}
  607. * @constant
  608. */
  609. CesiumMath.EPSILON16 = 0.0000000000000001;
  610. /**
  611. * 0.00000000000000001
  612. * @type {Number}
  613. * @constant
  614. */
  615. CesiumMath.EPSILON17 = 0.00000000000000001;
  616. /**
  617. * 0.000000000000000001
  618. * @type {Number}
  619. * @constant
  620. */
  621. CesiumMath.EPSILON18 = 0.000000000000000001;
  622. /**
  623. * 0.0000000000000000001
  624. * @type {Number}
  625. * @constant
  626. */
  627. CesiumMath.EPSILON19 = 0.0000000000000000001;
  628. /**
  629. * 0.00000000000000000001
  630. * @type {Number}
  631. * @constant
  632. */
  633. CesiumMath.EPSILON20 = 0.00000000000000000001;
  634. /**
  635. * 3.986004418e14
  636. * @type {Number}
  637. * @constant
  638. */
  639. CesiumMath.GRAVITATIONALPARAMETER = 3.986004418e14;
  640. /**
  641. * Radius of the sun in meters: 6.955e8
  642. * @type {Number}
  643. * @constant
  644. */
  645. CesiumMath.SOLAR_RADIUS = 6.955e8;
  646. /**
  647. * The mean radius of the moon, according to the "Report of the IAU/IAG Working Group on
  648. * Cartographic Coordinates and Rotational Elements of the Planets and satellites: 2000",
  649. * Celestial Mechanics 82: 83-110, 2002.
  650. * @type {Number}
  651. * @constant
  652. */
  653. CesiumMath.LUNAR_RADIUS = 1737400.0;
  654. /**
  655. * 64 * 1024
  656. * @type {Number}
  657. * @constant
  658. */
  659. CesiumMath.SIXTY_FOUR_KILOBYTES = 64 * 1024;
  660. /**
  661. * Returns the sign of the value; 1 if the value is positive, -1 if the value is
  662. * negative, or 0 if the value is 0.
  663. *
  664. * @param {Number} value The value to return the sign of.
  665. * @returns {Number} The sign of value.
  666. */
  667. CesiumMath.sign = function(value) {
  668. if (value > 0) {
  669. return 1;
  670. }
  671. if (value < 0) {
  672. return -1;
  673. }
  674. return 0;
  675. };
  676. /**
  677. * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
  678. * This is similar to {@link CesiumMath#sign} except that returns 1.0 instead of
  679. * 0.0 when the input value is 0.0.
  680. * @param {Number} value The value to return the sign of.
  681. * @returns {Number} The sign of value.
  682. */
  683. CesiumMath.signNotZero = function(value) {
  684. return value < 0.0 ? -1.0 : 1.0;
  685. };
  686. /**
  687. * Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMax]
  688. * @param {Number} value The scalar value in the range [-1.0, 1.0]
  689. * @param {Number} [rangeMax=255] The maximum value in the mapped range, 255 by default.
  690. * @returns {Number} A SNORM value, where 0 maps to -1.0 and rangeMax maps to 1.0.
  691. *
  692. * @see CesiumMath.fromSNorm
  693. */
  694. CesiumMath.toSNorm = function(value, rangeMax) {
  695. rangeMax = defaultValue(rangeMax, 255);
  696. return Math.round((CesiumMath.clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMax);
  697. };
  698. /**
  699. * Converts a SNORM value in the range [0, rangeMax] to a scalar in the range [-1.0, 1.0].
  700. * @param {Number} value SNORM value in the range [0, 255]
  701. * @param {Number} [rangeMax=255] The maximum value in the SNORM range, 255 by default.
  702. * @returns {Number} Scalar in the range [-1.0, 1.0].
  703. *
  704. * @see CesiumMath.toSNorm
  705. */
  706. CesiumMath.fromSNorm = function(value, rangeMax) {
  707. rangeMax = defaultValue(rangeMax, 255);
  708. return CesiumMath.clamp(value, 0.0, rangeMax) / rangeMax * 2.0 - 1.0;
  709. };
  710. /**
  711. * Returns the hyperbolic sine of a number.
  712. * The hyperbolic sine of <em>value</em> is defined to be
  713. * (<em>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></em>)/2.0
  714. * where <i>e</i> is Euler's number, approximately 2.71828183.
  715. *
  716. * <p>Special cases:
  717. * <ul>
  718. * <li>If the argument is NaN, then the result is NaN.</li>
  719. *
  720. * <li>If the argument is infinite, then the result is an infinity
  721. * with the same sign as the argument.</li>
  722. *
  723. * <li>If the argument is zero, then the result is a zero with the
  724. * same sign as the argument.</li>
  725. * </ul>
  726. *</p>
  727. *
  728. * @param {Number} value The number whose hyperbolic sine is to be returned.
  729. * @returns {Number} The hyperbolic sine of <code>value</code>.
  730. */
  731. CesiumMath.sinh = function(value) {
  732. var part1 = Math.pow(Math.E, value);
  733. var part2 = Math.pow(Math.E, -1.0 * value);
  734. return (part1 - part2) * 0.5;
  735. };
  736. /**
  737. * Returns the hyperbolic cosine of a number.
  738. * The hyperbolic cosine of <strong>value</strong> is defined to be
  739. * (<em>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></em>)/2.0
  740. * where <i>e</i> is Euler's number, approximately 2.71828183.
  741. *
  742. * <p>Special cases:
  743. * <ul>
  744. * <li>If the argument is NaN, then the result is NaN.</li>
  745. *
  746. * <li>If the argument is infinite, then the result is positive infinity.</li>
  747. *
  748. * <li>If the argument is zero, then the result is 1.0.</li>
  749. * </ul>
  750. *</p>
  751. *
  752. * @param {Number} value The number whose hyperbolic cosine is to be returned.
  753. * @returns {Number} The hyperbolic cosine of <code>value</code>.
  754. */
  755. CesiumMath.cosh = function(value) {
  756. var part1 = Math.pow(Math.E, value);
  757. var part2 = Math.pow(Math.E, -1.0 * value);
  758. return (part1 + part2) * 0.5;
  759. };
  760. /**
  761. * Computes the linear interpolation of two values.
  762. *
  763. * @param {Number} p The start value to interpolate.
  764. * @param {Number} q The end value to interpolate.
  765. * @param {Number} time The time of interpolation generally in the range <code>[0.0, 1.0]</code>.
  766. * @returns {Number} The linearly interpolated value.
  767. *
  768. * @example
  769. * var n = Cesium.Math.lerp(0.0, 2.0, 0.5); // returns 1.0
  770. */
  771. CesiumMath.lerp = function(p, q, time) {
  772. return ((1.0 - time) * p) + (time * q);
  773. };
  774. /**
  775. * pi
  776. *
  777. * @type {Number}
  778. * @constant
  779. */
  780. CesiumMath.PI = Math.PI;
  781. /**
  782. * 1/pi
  783. *
  784. * @type {Number}
  785. * @constant
  786. */
  787. CesiumMath.ONE_OVER_PI = 1.0 / Math.PI;
  788. /**
  789. * pi/2
  790. *
  791. * @type {Number}
  792. * @constant
  793. */
  794. CesiumMath.PI_OVER_TWO = Math.PI * 0.5;
  795. /**
  796. * pi/3
  797. *
  798. * @type {Number}
  799. * @constant
  800. */
  801. CesiumMath.PI_OVER_THREE = Math.PI / 3.0;
  802. /**
  803. * pi/4
  804. *
  805. * @type {Number}
  806. * @constant
  807. */
  808. CesiumMath.PI_OVER_FOUR = Math.PI / 4.0;
  809. /**
  810. * pi/6
  811. *
  812. * @type {Number}
  813. * @constant
  814. */
  815. CesiumMath.PI_OVER_SIX = Math.PI / 6.0;
  816. /**
  817. * 3pi/2
  818. *
  819. * @type {Number}
  820. * @constant
  821. */
  822. CesiumMath.THREE_PI_OVER_TWO = (3.0 * Math.PI) * 0.5;
  823. /**
  824. * 2pi
  825. *
  826. * @type {Number}
  827. * @constant
  828. */
  829. CesiumMath.TWO_PI = 2.0 * Math.PI;
  830. /**
  831. * 1/2pi
  832. *
  833. * @type {Number}
  834. * @constant
  835. */
  836. CesiumMath.ONE_OVER_TWO_PI = 1.0 / (2.0 * Math.PI);
  837. /**
  838. * The number of radians in a degree.
  839. *
  840. * @type {Number}
  841. * @constant
  842. * @default Math.PI / 180.0
  843. */
  844. CesiumMath.RADIANS_PER_DEGREE = Math.PI / 180.0;
  845. /**
  846. * The number of degrees in a radian.
  847. *
  848. * @type {Number}
  849. * @constant
  850. * @default 180.0 / Math.PI
  851. */
  852. CesiumMath.DEGREES_PER_RADIAN = 180.0 / Math.PI;
  853. /**
  854. * The number of radians in an arc second.
  855. *
  856. * @type {Number}
  857. * @constant
  858. * @default {@link CesiumMath.RADIANS_PER_DEGREE} / 3600.0
  859. */
  860. CesiumMath.RADIANS_PER_ARCSECOND = CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  861. /**
  862. * Converts degrees to radians.
  863. * @param {Number} degrees The angle to convert in degrees.
  864. * @returns {Number} The corresponding angle in radians.
  865. */
  866. CesiumMath.toRadians = function(degrees) {
  867. if (!defined(degrees)) {
  868. throw new DeveloperError('degrees is required.');
  869. }
  870. return degrees * CesiumMath.RADIANS_PER_DEGREE;
  871. };
  872. /**
  873. * Converts radians to degrees.
  874. * @param {Number} radians The angle to convert in radians.
  875. * @returns {Number} The corresponding angle in degrees.
  876. */
  877. CesiumMath.toDegrees = function(radians) {
  878. if (!defined(radians)) {
  879. throw new DeveloperError('radians is required.');
  880. }
  881. return radians * CesiumMath.DEGREES_PER_RADIAN;
  882. };
  883. /**
  884. * Converts a longitude value, in radians, to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  885. *
  886. * @param {Number} angle The longitude value, in radians, to convert to the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  887. * @returns {Number} The equivalent longitude value in the range [<code>-Math.PI</code>, <code>Math.PI</code>).
  888. *
  889. * @example
  890. * // Convert 270 degrees to -90 degrees longitude
  891. * var longitude = Cesium.Math.convertLongitudeRange(Cesium.Math.toRadians(270.0));
  892. */
  893. CesiumMath.convertLongitudeRange = function(angle) {
  894. if (!defined(angle)) {
  895. throw new DeveloperError('angle is required.');
  896. }
  897. var twoPi = CesiumMath.TWO_PI;
  898. var simplified = angle - Math.floor(angle / twoPi) * twoPi;
  899. if (simplified < -Math.PI) {
  900. return simplified + twoPi;
  901. }
  902. if (simplified >= Math.PI) {
  903. return simplified - twoPi;
  904. }
  905. return simplified;
  906. };
  907. /**
  908. * Convenience function that clamps a latitude value, in radians, to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  909. * Useful for sanitizing data before use in objects requiring correct range.
  910. *
  911. * @param {Number} angle The latitude value, in radians, to clamp to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  912. * @returns {Number} The latitude value clamped to the range [<code>-Math.PI/2</code>, <code>Math.PI/2</code>).
  913. *
  914. * @example
  915. * // Clamp 108 degrees latitude to 90 degrees latitude
  916. * var latitude = Cesium.Math.clampToLatitudeRange(Cesium.Math.toRadians(108.0));
  917. */
  918. CesiumMath.clampToLatitudeRange = function(angle) {
  919. if (!defined(angle)) {
  920. throw new DeveloperError('angle is required.');
  921. }
  922. return CesiumMath.clamp(angle, -1*CesiumMath.PI_OVER_TWO, CesiumMath.PI_OVER_TWO);
  923. };
  924. /**
  925. * Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
  926. *
  927. * @param {Number} angle in radians
  928. * @returns {Number} The angle in the range [<code>-CesiumMath.PI</code>, <code>CesiumMath.PI</code>].
  929. */
  930. CesiumMath.negativePiToPi = function(x) {
  931. if (!defined(x)) {
  932. throw new DeveloperError('x is required.');
  933. }
  934. return CesiumMath.zeroToTwoPi(x + CesiumMath.PI) - CesiumMath.PI;
  935. };
  936. /**
  937. * Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
  938. *
  939. * @param {Number} angle in radians
  940. * @returns {Number} The angle in the range [0, <code>CesiumMath.TWO_PI</code>].
  941. */
  942. CesiumMath.zeroToTwoPi = function(x) {
  943. if (!defined(x)) {
  944. throw new DeveloperError('x is required.');
  945. }
  946. var mod = CesiumMath.mod(x, CesiumMath.TWO_PI);
  947. if (Math.abs(mod) < CesiumMath.EPSILON14 && Math.abs(x) > CesiumMath.EPSILON14) {
  948. return CesiumMath.TWO_PI;
  949. }
  950. return mod;
  951. };
  952. /**
  953. * The modulo operation that also works for negative dividends.
  954. *
  955. * @param {Number} m The dividend.
  956. * @param {Number} n The divisor.
  957. * @returns {Number} The remainder.
  958. */
  959. CesiumMath.mod = function(m, n) {
  960. if (!defined(m)) {
  961. throw new DeveloperError('m is required.');
  962. }
  963. if (!defined(n)) {
  964. throw new DeveloperError('n is required.');
  965. }
  966. return ((m % n) + n) % n;
  967. };
  968. /**
  969. * Determines if two values are equal using an absolute or relative tolerance test. This is useful
  970. * to avoid problems due to roundoff error when comparing floating-point values directly. The values are
  971. * first compared using an absolute tolerance test. If that fails, a relative tolerance test is performed.
  972. * Use this test if you are unsure of the magnitudes of left and right.
  973. *
  974. * @param {Number} left The first value to compare.
  975. * @param {Number} right The other value to compare.
  976. * @param {Number} relativeEpsilon The maximum inclusive delta between <code>left</code> and <code>right</code> for the relative tolerance test.
  977. * @param {Number} [absoluteEpsilon=relativeEpsilon] The maximum inclusive delta between <code>left</code> and <code>right</code> for the absolute tolerance test.
  978. * @returns {Boolean} <code>true</code> if the values are equal within the epsilon; otherwise, <code>false</code>.
  979. *
  980. * @example
  981. * var a = Cesium.Math.equalsEpsilon(0.0, 0.01, Cesium.Math.EPSILON2); // true
  982. * var b = Cesium.Math.equalsEpsilon(0.0, 0.1, Cesium.Math.EPSILON2); // false
  983. * var c = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON7); // true
  984. * var d = Cesium.Math.equalsEpsilon(3699175.1634344, 3699175.2, Cesium.Math.EPSILON9); // false
  985. */
  986. CesiumMath.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  987. if (!defined(left)) {
  988. throw new DeveloperError('left is required.');
  989. }
  990. if (!defined(right)) {
  991. throw new DeveloperError('right is required.');
  992. }
  993. if (!defined(relativeEpsilon)) {
  994. throw new DeveloperError('relativeEpsilon is required.');
  995. }
  996. absoluteEpsilon = defaultValue(absoluteEpsilon, relativeEpsilon);
  997. var absDiff = Math.abs(left - right);
  998. return absDiff <= absoluteEpsilon || absDiff <= relativeEpsilon * Math.max(Math.abs(left), Math.abs(right));
  999. };
  1000. var factorials = [1];
  1001. /**
  1002. * Computes the factorial of the provided number.
  1003. *
  1004. * @param {Number} n The number whose factorial is to be computed.
  1005. * @returns {Number} The factorial of the provided number or undefined if the number is less than 0.
  1006. *
  1007. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  1008. *
  1009. *
  1010. * @example
  1011. * //Compute 7!, which is equal to 5040
  1012. * var computedFactorial = Cesium.Math.factorial(7);
  1013. *
  1014. * @see {@link http://en.wikipedia.org/wiki/Factorial|Factorial on Wikipedia}
  1015. */
  1016. CesiumMath.factorial = function(n) {
  1017. if (typeof n !== 'number' || n < 0) {
  1018. throw new DeveloperError('A number greater than or equal to 0 is required.');
  1019. }
  1020. var length = factorials.length;
  1021. if (n >= length) {
  1022. var sum = factorials[length - 1];
  1023. for (var i = length; i <= n; i++) {
  1024. factorials.push(sum * i);
  1025. }
  1026. }
  1027. return factorials[n];
  1028. };
  1029. /**
  1030. * Increments a number with a wrapping to a minimum value if the number exceeds the maximum value.
  1031. *
  1032. * @param {Number} [n] The number to be incremented.
  1033. * @param {Number} [maximumValue] The maximum incremented value before rolling over to the minimum value.
  1034. * @param {Number} [minimumValue=0.0] The number reset to after the maximum value has been exceeded.
  1035. * @returns {Number} The incremented number.
  1036. *
  1037. * @exception {DeveloperError} Maximum value must be greater than minimum value.
  1038. *
  1039. * @example
  1040. * var n = Cesium.Math.incrementWrap(5, 10, 0); // returns 6
  1041. * var n = Cesium.Math.incrementWrap(10, 10, 0); // returns 0
  1042. */
  1043. CesiumMath.incrementWrap = function(n, maximumValue, minimumValue) {
  1044. minimumValue = defaultValue(minimumValue, 0.0);
  1045. if (!defined(n)) {
  1046. throw new DeveloperError('n is required.');
  1047. }
  1048. if (maximumValue <= minimumValue) {
  1049. throw new DeveloperError('maximumValue must be greater than minimumValue.');
  1050. }
  1051. ++n;
  1052. if (n > maximumValue) {
  1053. n = minimumValue;
  1054. }
  1055. return n;
  1056. };
  1057. /**
  1058. * Determines if a positive integer is a power of two.
  1059. *
  1060. * @param {Number} n The positive integer to test.
  1061. * @returns {Boolean} <code>true</code> if the number if a power of two; otherwise, <code>false</code>.
  1062. *
  1063. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  1064. *
  1065. * @example
  1066. * var t = Cesium.Math.isPowerOfTwo(16); // true
  1067. * var f = Cesium.Math.isPowerOfTwo(20); // false
  1068. */
  1069. CesiumMath.isPowerOfTwo = function(n) {
  1070. if (typeof n !== 'number' || n < 0) {
  1071. throw new DeveloperError('A number greater than or equal to 0 is required.');
  1072. }
  1073. return (n !== 0) && ((n & (n - 1)) === 0);
  1074. };
  1075. /**
  1076. * Computes the next power-of-two integer greater than or equal to the provided positive integer.
  1077. *
  1078. * @param {Number} n The positive integer to test.
  1079. * @returns {Number} The next power-of-two integer.
  1080. *
  1081. * @exception {DeveloperError} A number greater than or equal to 0 is required.
  1082. *
  1083. * @example
  1084. * var n = Cesium.Math.nextPowerOfTwo(29); // 32
  1085. * var m = Cesium.Math.nextPowerOfTwo(32); // 32
  1086. */
  1087. CesiumMath.nextPowerOfTwo = function(n) {
  1088. if (typeof n !== 'number' || n < 0) {
  1089. throw new DeveloperError('A number greater than or equal to 0 is required.');
  1090. }
  1091. // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  1092. --n;
  1093. n |= n >> 1;
  1094. n |= n >> 2;
  1095. n |= n >> 4;
  1096. n |= n >> 8;
  1097. n |= n >> 16;
  1098. ++n;
  1099. return n;
  1100. };
  1101. /**
  1102. * Constraint a value to lie between two values.
  1103. *
  1104. * @param {Number} value The value to constrain.
  1105. * @param {Number} min The minimum value.
  1106. * @param {Number} max The maximum value.
  1107. * @returns {Number} The value clamped so that min <= value <= max.
  1108. */
  1109. CesiumMath.clamp = function(value, min, max) {
  1110. if (!defined(value)) {
  1111. throw new DeveloperError('value is required');
  1112. }
  1113. if (!defined(min)) {
  1114. throw new DeveloperError('min is required.');
  1115. }
  1116. if (!defined(max)) {
  1117. throw new DeveloperError('max is required.');
  1118. }
  1119. return value < min ? min : value > max ? max : value;
  1120. };
  1121. var randomNumberGenerator = new MersenneTwister();
  1122. /**
  1123. * Sets the seed used by the random number generator
  1124. * in {@link CesiumMath#nextRandomNumber}.
  1125. *
  1126. * @param {Number} seed An integer used as the seed.
  1127. */
  1128. CesiumMath.setRandomNumberSeed = function(seed) {
  1129. if (!defined(seed)) {
  1130. throw new DeveloperError('seed is required.');
  1131. }
  1132. randomNumberGenerator = new MersenneTwister(seed);
  1133. };
  1134. /**
  1135. * Generates a random number in the range of [0.0, 1.0)
  1136. * using a Mersenne twister.
  1137. *
  1138. * @returns {Number} A random number in the range of [0.0, 1.0).
  1139. *
  1140. * @see CesiumMath.setRandomNumberSeed
  1141. * @see {@link http://en.wikipedia.org/wiki/Mersenne_twister|Mersenne twister on Wikipedia}
  1142. */
  1143. CesiumMath.nextRandomNumber = function() {
  1144. return randomNumberGenerator.random();
  1145. };
  1146. /**
  1147. * Computes <code>Math.acos(value)</acode>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  1148. * so that the function will never return NaN.
  1149. *
  1150. * @param {Number} value The value for which to compute acos.
  1151. * @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,
  1152. * whichever is closer, if the value is outside the range.
  1153. */
  1154. CesiumMath.acosClamped = function(value) {
  1155. if (!defined(value)) {
  1156. throw new DeveloperError('value is required.');
  1157. }
  1158. return Math.acos(CesiumMath.clamp(value, -1.0, 1.0));
  1159. };
  1160. /**
  1161. * Computes <code>Math.asin(value)</acode>, but first clamps <code>value</code> to the range [-1.0, 1.0]
  1162. * so that the function will never return NaN.
  1163. *
  1164. * @param {Number} value The value for which to compute asin.
  1165. * @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,
  1166. * whichever is closer, if the value is outside the range.
  1167. */
  1168. CesiumMath.asinClamped = function(value) {
  1169. if (!defined(value)) {
  1170. throw new DeveloperError('value is required.');
  1171. }
  1172. return Math.asin(CesiumMath.clamp(value, -1.0, 1.0));
  1173. };
  1174. /**
  1175. * Finds the chord length between two points given the circle's radius and the angle between the points.
  1176. *
  1177. * @param {Number} angle The angle between the two points.
  1178. * @param {Number} radius The radius of the circle.
  1179. * @returns {Number} The chord length.
  1180. */
  1181. CesiumMath.chordLength = function(angle, radius) {
  1182. if (!defined(angle)) {
  1183. throw new DeveloperError('angle is required.');
  1184. }
  1185. if (!defined(radius)) {
  1186. throw new DeveloperError('radius is required.');
  1187. }
  1188. return 2.0 * radius * Math.sin(angle * 0.5);
  1189. };
  1190. /**
  1191. * Finds the logarithm of a number to a base.
  1192. *
  1193. * @param {Number} number The number.
  1194. * @param {Number} base The base.
  1195. * @returns {Number} The result.
  1196. */
  1197. CesiumMath.logBase = function(number, base) {
  1198. if (!defined(number)) {
  1199. throw new DeveloperError('number is required.');
  1200. }
  1201. if (!defined(base)) {
  1202. throw new DeveloperError('base is required.');
  1203. }
  1204. return Math.log(number) / Math.log(base);
  1205. };
  1206. /**
  1207. * @private
  1208. */
  1209. CesiumMath.fog = function(distanceToCamera, density) {
  1210. var scalar = distanceToCamera * density;
  1211. return 1.0 - Math.exp(-(scalar * scalar));
  1212. };
  1213. return CesiumMath;
  1214. });
  1215. /*global define*/
  1216. define('Core/Cartesian3',[
  1217. './Check',
  1218. './defaultValue',
  1219. './defined',
  1220. './DeveloperError',
  1221. './freezeObject',
  1222. './Math'
  1223. ], function(
  1224. Check,
  1225. defaultValue,
  1226. defined,
  1227. DeveloperError,
  1228. freezeObject,
  1229. CesiumMath) {
  1230. 'use strict';
  1231. /**
  1232. * A 3D Cartesian point.
  1233. * @alias Cartesian3
  1234. * @constructor
  1235. *
  1236. * @param {Number} [x=0.0] The X component.
  1237. * @param {Number} [y=0.0] The Y component.
  1238. * @param {Number} [z=0.0] The Z component.
  1239. *
  1240. * @see Cartesian2
  1241. * @see Cartesian4
  1242. * @see Packable
  1243. */
  1244. function Cartesian3(x, y, z) {
  1245. /**
  1246. * The X component.
  1247. * @type {Number}
  1248. * @default 0.0
  1249. */
  1250. this.x = defaultValue(x, 0.0);
  1251. /**
  1252. * The Y component.
  1253. * @type {Number}
  1254. * @default 0.0
  1255. */
  1256. this.y = defaultValue(y, 0.0);
  1257. /**
  1258. * The Z component.
  1259. * @type {Number}
  1260. * @default 0.0
  1261. */
  1262. this.z = defaultValue(z, 0.0);
  1263. }
  1264. /**
  1265. * Converts the provided Spherical into Cartesian3 coordinates.
  1266. *
  1267. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  1268. * @param {Cartesian3} [result] The object onto which to store the result.
  1269. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1270. */
  1271. Cartesian3.fromSpherical = function(spherical, result) {
  1272. Check.typeOf.object(spherical, 'spherical');
  1273. if (!defined(result)) {
  1274. result = new Cartesian3();
  1275. }
  1276. var clock = spherical.clock;
  1277. var cone = spherical.cone;
  1278. var magnitude = defaultValue(spherical.magnitude, 1.0);
  1279. var radial = magnitude * Math.sin(cone);
  1280. result.x = radial * Math.cos(clock);
  1281. result.y = radial * Math.sin(clock);
  1282. result.z = magnitude * Math.cos(cone);
  1283. return result;
  1284. };
  1285. /**
  1286. * Creates a Cartesian3 instance from x, y and z coordinates.
  1287. *
  1288. * @param {Number} x The x coordinate.
  1289. * @param {Number} y The y coordinate.
  1290. * @param {Number} z The z coordinate.
  1291. * @param {Cartesian3} [result] The object onto which to store the result.
  1292. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1293. */
  1294. Cartesian3.fromElements = function(x, y, z, result) {
  1295. if (!defined(result)) {
  1296. return new Cartesian3(x, y, z);
  1297. }
  1298. result.x = x;
  1299. result.y = y;
  1300. result.z = z;
  1301. return result;
  1302. };
  1303. /**
  1304. * Duplicates a Cartesian3 instance.
  1305. *
  1306. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  1307. * @param {Cartesian3} [result] The object onto which to store the result.
  1308. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  1309. */
  1310. Cartesian3.clone = function(cartesian, result) {
  1311. if (!defined(cartesian)) {
  1312. return undefined;
  1313. }
  1314. if (!defined(result)) {
  1315. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  1316. }
  1317. result.x = cartesian.x;
  1318. result.y = cartesian.y;
  1319. result.z = cartesian.z;
  1320. return result;
  1321. };
  1322. /**
  1323. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  1324. * x, y, and z properties of the Cartesian4 and drops w.
  1325. * @function
  1326. *
  1327. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  1328. * @param {Cartesian3} [result] The object onto which to store the result.
  1329. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1330. */
  1331. Cartesian3.fromCartesian4 = Cartesian3.clone;
  1332. /**
  1333. * The number of elements used to pack the object into an array.
  1334. * @type {Number}
  1335. */
  1336. Cartesian3.packedLength = 3;
  1337. /**
  1338. * Stores the provided instance into the provided array.
  1339. *
  1340. * @param {Cartesian3} value The value to pack.
  1341. * @param {Number[]} array The array to pack into.
  1342. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1343. *
  1344. * @returns {Number[]} The array that was packed into
  1345. */
  1346. Cartesian3.pack = function(value, array, startingIndex) {
  1347. Check.typeOf.object(value, 'value');
  1348. Check.defined(array, 'array');
  1349. startingIndex = defaultValue(startingIndex, 0);
  1350. array[startingIndex++] = value.x;
  1351. array[startingIndex++] = value.y;
  1352. array[startingIndex] = value.z;
  1353. return array;
  1354. };
  1355. /**
  1356. * Retrieves an instance from a packed array.
  1357. *
  1358. * @param {Number[]} array The packed array.
  1359. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1360. * @param {Cartesian3} [result] The object into which to store the result.
  1361. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1362. */
  1363. Cartesian3.unpack = function(array, startingIndex, result) {
  1364. Check.defined(array, 'array');
  1365. startingIndex = defaultValue(startingIndex, 0);
  1366. if (!defined(result)) {
  1367. result = new Cartesian3();
  1368. }
  1369. result.x = array[startingIndex++];
  1370. result.y = array[startingIndex++];
  1371. result.z = array[startingIndex];
  1372. return result;
  1373. };
  1374. /**
  1375. * Flattens an array of Cartesian3s into an array of components.
  1376. *
  1377. * @param {Cartesian3[]} array The array of cartesians to pack.
  1378. * @param {Number[]} result The array onto which to store the result.
  1379. * @returns {Number[]} The packed array.
  1380. */
  1381. Cartesian3.packArray = function(array, result) {
  1382. Check.defined(array, 'array');
  1383. var length = array.length;
  1384. if (!defined(result)) {
  1385. result = new Array(length * 3);
  1386. } else {
  1387. result.length = length * 3;
  1388. }
  1389. for (var i = 0; i < length; ++i) {
  1390. Cartesian3.pack(array[i], result, i * 3);
  1391. }
  1392. return result;
  1393. };
  1394. /**
  1395. * Unpacks an array of cartesian components into an array of Cartesian3s.
  1396. *
  1397. * @param {Number[]} array The array of components to unpack.
  1398. * @param {Cartesian3[]} result The array onto which to store the result.
  1399. * @returns {Cartesian3[]} The unpacked array.
  1400. */
  1401. Cartesian3.unpackArray = function(array, result) {
  1402. Check.defined(array, 'array');
  1403. Check.numeric.minimum(array.length, 3);
  1404. if (array.length % 3 !== 0) {
  1405. throw new DeveloperError('array length must be a multiple of 3.');
  1406. }
  1407. var length = array.length;
  1408. if (!defined(result)) {
  1409. result = new Array(length / 3);
  1410. } else {
  1411. result.length = length / 3;
  1412. }
  1413. for (var i = 0; i < length; i += 3) {
  1414. var index = i / 3;
  1415. result[index] = Cartesian3.unpack(array, i, result[index]);
  1416. }
  1417. return result;
  1418. };
  1419. /**
  1420. * Creates a Cartesian3 from three consecutive elements in an array.
  1421. * @function
  1422. *
  1423. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  1424. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  1425. * @param {Cartesian3} [result] The object onto which to store the result.
  1426. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1427. *
  1428. * @example
  1429. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  1430. * var v = [1.0, 2.0, 3.0];
  1431. * var p = Cesium.Cartesian3.fromArray(v);
  1432. *
  1433. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  1434. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  1435. * var p2 = Cesium.Cartesian3.fromArray(v2, 2);
  1436. */
  1437. Cartesian3.fromArray = Cartesian3.unpack;
  1438. /**
  1439. * Computes the value of the maximum component for the supplied Cartesian.
  1440. *
  1441. * @param {Cartesian3} cartesian The cartesian to use.
  1442. * @returns {Number} The value of the maximum component.
  1443. */
  1444. Cartesian3.maximumComponent = function(cartesian) {
  1445. Check.typeOf.object(cartesian, 'cartesian');
  1446. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  1447. };
  1448. /**
  1449. * Computes the value of the minimum component for the supplied Cartesian.
  1450. *
  1451. * @param {Cartesian3} cartesian The cartesian to use.
  1452. * @returns {Number} The value of the minimum component.
  1453. */
  1454. Cartesian3.minimumComponent = function(cartesian) {
  1455. Check.typeOf.object(cartesian, 'cartesian');
  1456. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  1457. };
  1458. /**
  1459. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  1460. *
  1461. * @param {Cartesian3} first A cartesian to compare.
  1462. * @param {Cartesian3} second A cartesian to compare.
  1463. * @param {Cartesian3} result The object into which to store the result.
  1464. * @returns {Cartesian3} A cartesian with the minimum components.
  1465. */
  1466. Cartesian3.minimumByComponent = function(first, second, result) {
  1467. Check.typeOf.object(first, 'first');
  1468. Check.typeOf.object(second, 'second');
  1469. Check.typeOf.object(result, 'result');
  1470. result.x = Math.min(first.x, second.x);
  1471. result.y = Math.min(first.y, second.y);
  1472. result.z = Math.min(first.z, second.z);
  1473. return result;
  1474. };
  1475. /**
  1476. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  1477. *
  1478. * @param {Cartesian3} first A cartesian to compare.
  1479. * @param {Cartesian3} second A cartesian to compare.
  1480. * @param {Cartesian3} result The object into which to store the result.
  1481. * @returns {Cartesian3} A cartesian with the maximum components.
  1482. */
  1483. Cartesian3.maximumByComponent = function(first, second, result) {
  1484. Check.typeOf.object(first, 'first');
  1485. Check.typeOf.object(second, 'second');
  1486. Check.typeOf.object(result, 'result');
  1487. result.x = Math.max(first.x, second.x);
  1488. result.y = Math.max(first.y, second.y);
  1489. result.z = Math.max(first.z, second.z);
  1490. return result;
  1491. };
  1492. /**
  1493. * Computes the provided Cartesian's squared magnitude.
  1494. *
  1495. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  1496. * @returns {Number} The squared magnitude.
  1497. */
  1498. Cartesian3.magnitudeSquared = function(cartesian) {
  1499. Check.typeOf.object(cartesian, 'cartesian');
  1500. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z;
  1501. };
  1502. /**
  1503. * Computes the Cartesian's magnitude (length).
  1504. *
  1505. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  1506. * @returns {Number} The magnitude.
  1507. */
  1508. Cartesian3.magnitude = function(cartesian) {
  1509. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  1510. };
  1511. var distanceScratch = new Cartesian3();
  1512. /**
  1513. * Computes the distance between two points.
  1514. *
  1515. * @param {Cartesian3} left The first point to compute the distance from.
  1516. * @param {Cartesian3} right The second point to compute the distance to.
  1517. * @returns {Number} The distance between two points.
  1518. *
  1519. * @example
  1520. * // Returns 1.0
  1521. * var d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  1522. */
  1523. Cartesian3.distance = function(left, right) {
  1524. Check.typeOf.object(left, 'left');
  1525. Check.typeOf.object(right, 'right');
  1526. Cartesian3.subtract(left, right, distanceScratch);
  1527. return Cartesian3.magnitude(distanceScratch);
  1528. };
  1529. /**
  1530. * Computes the squared distance between two points. Comparing squared distances
  1531. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  1532. *
  1533. * @param {Cartesian3} left The first point to compute the distance from.
  1534. * @param {Cartesian3} right The second point to compute the distance to.
  1535. * @returns {Number} The distance between two points.
  1536. *
  1537. * @example
  1538. * // Returns 4.0, not 2.0
  1539. * var d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  1540. */
  1541. Cartesian3.distanceSquared = function(left, right) {
  1542. Check.typeOf.object(left, 'left');
  1543. Check.typeOf.object(right, 'right');
  1544. Cartesian3.subtract(left, right, distanceScratch);
  1545. return Cartesian3.magnitudeSquared(distanceScratch);
  1546. };
  1547. /**
  1548. * Computes the normalized form of the supplied Cartesian.
  1549. *
  1550. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  1551. * @param {Cartesian3} result The object onto which to store the result.
  1552. * @returns {Cartesian3} The modified result parameter.
  1553. */
  1554. Cartesian3.normalize = function(cartesian, result) {
  1555. Check.typeOf.object(cartesian, 'cartesian');
  1556. Check.typeOf.object(result, 'result');
  1557. var magnitude = Cartesian3.magnitude(cartesian);
  1558. result.x = cartesian.x / magnitude;
  1559. result.y = cartesian.y / magnitude;
  1560. result.z = cartesian.z / magnitude;
  1561. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  1562. throw new DeveloperError('normalized result is not a number');
  1563. }
  1564. return result;
  1565. };
  1566. /**
  1567. * Computes the dot (scalar) product of two Cartesians.
  1568. *
  1569. * @param {Cartesian3} left The first Cartesian.
  1570. * @param {Cartesian3} right The second Cartesian.
  1571. * @returns {Number} The dot product.
  1572. */
  1573. Cartesian3.dot = function(left, right) {
  1574. Check.typeOf.object(left, 'left');
  1575. Check.typeOf.object(right, 'right');
  1576. return left.x * right.x + left.y * right.y + left.z * right.z;
  1577. };
  1578. /**
  1579. * Computes the componentwise product of two Cartesians.
  1580. *
  1581. * @param {Cartesian3} left The first Cartesian.
  1582. * @param {Cartesian3} right The second Cartesian.
  1583. * @param {Cartesian3} result The object onto which to store the result.
  1584. * @returns {Cartesian3} The modified result parameter.
  1585. */
  1586. Cartesian3.multiplyComponents = function(left, right, result) {
  1587. Check.typeOf.object(left, 'left');
  1588. Check.typeOf.object(right, 'right');
  1589. Check.typeOf.object(result, 'result');
  1590. result.x = left.x * right.x;
  1591. result.y = left.y * right.y;
  1592. result.z = left.z * right.z;
  1593. return result;
  1594. };
  1595. /**
  1596. * Computes the componentwise quotient of two Cartesians.
  1597. *
  1598. * @param {Cartesian3} left The first Cartesian.
  1599. * @param {Cartesian3} right The second Cartesian.
  1600. * @param {Cartesian3} result The object onto which to store the result.
  1601. * @returns {Cartesian3} The modified result parameter.
  1602. */
  1603. Cartesian3.divideComponents = function(left, right, result) {
  1604. if (!defined(left)) {
  1605. throw new DeveloperError('left is required');
  1606. }
  1607. if (!defined(right)) {
  1608. throw new DeveloperError('right is required');
  1609. }
  1610. if (!defined(result)) {
  1611. throw new DeveloperError('result is required');
  1612. }
  1613. result.x = left.x / right.x;
  1614. result.y = left.y / right.y;
  1615. result.z = left.z / right.z;
  1616. return result;
  1617. };
  1618. /**
  1619. * Computes the componentwise sum of two Cartesians.
  1620. *
  1621. * @param {Cartesian3} left The first Cartesian.
  1622. * @param {Cartesian3} right The second Cartesian.
  1623. * @param {Cartesian3} result The object onto which to store the result.
  1624. * @returns {Cartesian3} The modified result parameter.
  1625. */
  1626. Cartesian3.add = function(left, right, result) {
  1627. Check.typeOf.object(left, 'left');
  1628. Check.typeOf.object(right, 'right');
  1629. Check.typeOf.object(result, 'result');
  1630. result.x = left.x + right.x;
  1631. result.y = left.y + right.y;
  1632. result.z = left.z + right.z;
  1633. return result;
  1634. };
  1635. /**
  1636. * Computes the componentwise difference of two Cartesians.
  1637. *
  1638. * @param {Cartesian3} left The first Cartesian.
  1639. * @param {Cartesian3} right The second Cartesian.
  1640. * @param {Cartesian3} result The object onto which to store the result.
  1641. * @returns {Cartesian3} The modified result parameter.
  1642. */
  1643. Cartesian3.subtract = function(left, right, result) {
  1644. Check.typeOf.object(left, 'left');
  1645. Check.typeOf.object(right, 'right');
  1646. Check.typeOf.object(result, 'result');
  1647. result.x = left.x - right.x;
  1648. result.y = left.y - right.y;
  1649. result.z = left.z - right.z;
  1650. return result;
  1651. };
  1652. /**
  1653. * Multiplies the provided Cartesian componentwise by the provided scalar.
  1654. *
  1655. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  1656. * @param {Number} scalar The scalar to multiply with.
  1657. * @param {Cartesian3} result The object onto which to store the result.
  1658. * @returns {Cartesian3} The modified result parameter.
  1659. */
  1660. Cartesian3.multiplyByScalar = function(cartesian, scalar, result) {
  1661. Check.typeOf.object(cartesian, 'cartesian');
  1662. Check.typeOf.number(scalar, 'scalar');
  1663. Check.typeOf.object(result, 'result');
  1664. result.x = cartesian.x * scalar;
  1665. result.y = cartesian.y * scalar;
  1666. result.z = cartesian.z * scalar;
  1667. return result;
  1668. };
  1669. /**
  1670. * Divides the provided Cartesian componentwise by the provided scalar.
  1671. *
  1672. * @param {Cartesian3} cartesian The Cartesian to be divided.
  1673. * @param {Number} scalar The scalar to divide by.
  1674. * @param {Cartesian3} result The object onto which to store the result.
  1675. * @returns {Cartesian3} The modified result parameter.
  1676. */
  1677. Cartesian3.divideByScalar = function(cartesian, scalar, result) {
  1678. Check.typeOf.object(cartesian, 'cartesian');
  1679. Check.typeOf.number(scalar, 'scalar');
  1680. Check.typeOf.object(result, 'result');
  1681. result.x = cartesian.x / scalar;
  1682. result.y = cartesian.y / scalar;
  1683. result.z = cartesian.z / scalar;
  1684. return result;
  1685. };
  1686. /**
  1687. * Negates the provided Cartesian.
  1688. *
  1689. * @param {Cartesian3} cartesian The Cartesian to be negated.
  1690. * @param {Cartesian3} result The object onto which to store the result.
  1691. * @returns {Cartesian3} The modified result parameter.
  1692. */
  1693. Cartesian3.negate = function(cartesian, result) {
  1694. Check.typeOf.object(cartesian, 'cartesian');
  1695. Check.typeOf.object(result, 'result');
  1696. result.x = -cartesian.x;
  1697. result.y = -cartesian.y;
  1698. result.z = -cartesian.z;
  1699. return result;
  1700. };
  1701. /**
  1702. * Computes the absolute value of the provided Cartesian.
  1703. *
  1704. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  1705. * @param {Cartesian3} result The object onto which to store the result.
  1706. * @returns {Cartesian3} The modified result parameter.
  1707. */
  1708. Cartesian3.abs = function(cartesian, result) {
  1709. Check.typeOf.object(cartesian, 'cartesian');
  1710. Check.typeOf.object(result, 'result');
  1711. result.x = Math.abs(cartesian.x);
  1712. result.y = Math.abs(cartesian.y);
  1713. result.z = Math.abs(cartesian.z);
  1714. return result;
  1715. };
  1716. var lerpScratch = new Cartesian3();
  1717. /**
  1718. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  1719. *
  1720. * @param {Cartesian3} start The value corresponding to t at 0.0.
  1721. * @param {Cartesian3} end The value corresponding to t at 1.0.
  1722. * @param {Number} t The point along t at which to interpolate.
  1723. * @param {Cartesian3} result The object onto which to store the result.
  1724. * @returns {Cartesian3} The modified result parameter.
  1725. */
  1726. Cartesian3.lerp = function(start, end, t, result) {
  1727. Check.typeOf.object(start, 'start');
  1728. Check.typeOf.object(end, 'end');
  1729. Check.typeOf.number(t, 't');
  1730. Check.typeOf.object(result, 'result');
  1731. Cartesian3.multiplyByScalar(end, t, lerpScratch);
  1732. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  1733. return Cartesian3.add(lerpScratch, result, result);
  1734. };
  1735. var angleBetweenScratch = new Cartesian3();
  1736. var angleBetweenScratch2 = new Cartesian3();
  1737. /**
  1738. * Returns the angle, in radians, between the provided Cartesians.
  1739. *
  1740. * @param {Cartesian3} left The first Cartesian.
  1741. * @param {Cartesian3} right The second Cartesian.
  1742. * @returns {Number} The angle between the Cartesians.
  1743. */
  1744. Cartesian3.angleBetween = function(left, right) {
  1745. Check.typeOf.object(left, 'left');
  1746. Check.typeOf.object(right, 'right');
  1747. Cartesian3.normalize(left, angleBetweenScratch);
  1748. Cartesian3.normalize(right, angleBetweenScratch2);
  1749. var cosine = Cartesian3.dot(angleBetweenScratch, angleBetweenScratch2);
  1750. var sine = Cartesian3.magnitude(Cartesian3.cross(angleBetweenScratch, angleBetweenScratch2, angleBetweenScratch));
  1751. return Math.atan2(sine, cosine);
  1752. };
  1753. var mostOrthogonalAxisScratch = new Cartesian3();
  1754. /**
  1755. * Returns the axis that is most orthogonal to the provided Cartesian.
  1756. *
  1757. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  1758. * @param {Cartesian3} result The object onto which to store the result.
  1759. * @returns {Cartesian3} The most orthogonal axis.
  1760. */
  1761. Cartesian3.mostOrthogonalAxis = function(cartesian, result) {
  1762. Check.typeOf.object(cartesian, 'cartesian');
  1763. Check.typeOf.object(result, 'result');
  1764. var f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch);
  1765. Cartesian3.abs(f, f);
  1766. if (f.x <= f.y) {
  1767. if (f.x <= f.z) {
  1768. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  1769. } else {
  1770. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  1771. }
  1772. } else {
  1773. if (f.y <= f.z) {
  1774. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  1775. } else {
  1776. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  1777. }
  1778. }
  1779. return result;
  1780. };
  1781. /**
  1782. * Compares the provided Cartesians componentwise and returns
  1783. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1784. *
  1785. * @param {Cartesian3} [left] The first Cartesian.
  1786. * @param {Cartesian3} [right] The second Cartesian.
  1787. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1788. */
  1789. Cartesian3.equals = function(left, right) {
  1790. return (left === right) ||
  1791. ((defined(left)) &&
  1792. (defined(right)) &&
  1793. (left.x === right.x) &&
  1794. (left.y === right.y) &&
  1795. (left.z === right.z));
  1796. };
  1797. /**
  1798. * @private
  1799. */
  1800. Cartesian3.equalsArray = function(cartesian, array, offset) {
  1801. return cartesian.x === array[offset] &&
  1802. cartesian.y === array[offset + 1] &&
  1803. cartesian.z === array[offset + 2];
  1804. };
  1805. /**
  1806. * Compares the provided Cartesians componentwise and returns
  1807. * <code>true</code> if they pass an absolute or relative tolerance test,
  1808. * <code>false</code> otherwise.
  1809. *
  1810. * @param {Cartesian3} [left] The first Cartesian.
  1811. * @param {Cartesian3} [right] The second Cartesian.
  1812. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  1813. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1814. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1815. */
  1816. Cartesian3.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  1817. return (left === right) ||
  1818. (defined(left) &&
  1819. defined(right) &&
  1820. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  1821. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  1822. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon));
  1823. };
  1824. /**
  1825. * Computes the cross (outer) product of two Cartesians.
  1826. *
  1827. * @param {Cartesian3} left The first Cartesian.
  1828. * @param {Cartesian3} right The second Cartesian.
  1829. * @param {Cartesian3} result The object onto which to store the result.
  1830. * @returns {Cartesian3} The cross product.
  1831. */
  1832. Cartesian3.cross = function(left, right, result) {
  1833. Check.typeOf.object(left, 'left');
  1834. Check.typeOf.object(right, 'right');
  1835. Check.typeOf.object(result, 'result');
  1836. var leftX = left.x;
  1837. var leftY = left.y;
  1838. var leftZ = left.z;
  1839. var rightX = right.x;
  1840. var rightY = right.y;
  1841. var rightZ = right.z;
  1842. var x = leftY * rightZ - leftZ * rightY;
  1843. var y = leftZ * rightX - leftX * rightZ;
  1844. var z = leftX * rightY - leftY * rightX;
  1845. result.x = x;
  1846. result.y = y;
  1847. result.z = z;
  1848. return result;
  1849. };
  1850. /**
  1851. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  1852. *
  1853. * @param {Number} longitude The longitude, in degrees
  1854. * @param {Number} latitude The latitude, in degrees
  1855. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1856. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1857. * @param {Cartesian3} [result] The object onto which to store the result.
  1858. * @returns {Cartesian3} The position
  1859. *
  1860. * @example
  1861. * var position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  1862. */
  1863. Cartesian3.fromDegrees = function(longitude, latitude, height, ellipsoid, result) {
  1864. Check.typeOf.number(longitude, 'longitude');
  1865. Check.typeOf.number(latitude, 'latitude');
  1866. longitude = CesiumMath.toRadians(longitude);
  1867. latitude = CesiumMath.toRadians(latitude);
  1868. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  1869. };
  1870. var scratchN = new Cartesian3();
  1871. var scratchK = new Cartesian3();
  1872. var wgs84RadiiSquared = new Cartesian3(6378137.0 * 6378137.0, 6378137.0 * 6378137.0, 6356752.3142451793 * 6356752.3142451793);
  1873. /**
  1874. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  1875. *
  1876. * @param {Number} longitude The longitude, in radians
  1877. * @param {Number} latitude The latitude, in radians
  1878. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1879. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1880. * @param {Cartesian3} [result] The object onto which to store the result.
  1881. * @returns {Cartesian3} The position
  1882. *
  1883. * @example
  1884. * var position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  1885. */
  1886. Cartesian3.fromRadians = function(longitude, latitude, height, ellipsoid, result) {
  1887. Check.typeOf.number(longitude, 'longitude');
  1888. Check.typeOf.number(latitude, 'latitude');
  1889. height = defaultValue(height, 0.0);
  1890. var radiiSquared = defined(ellipsoid) ? ellipsoid.radiiSquared : wgs84RadiiSquared;
  1891. var cosLatitude = Math.cos(latitude);
  1892. scratchN.x = cosLatitude * Math.cos(longitude);
  1893. scratchN.y = cosLatitude * Math.sin(longitude);
  1894. scratchN.z = Math.sin(latitude);
  1895. scratchN = Cartesian3.normalize(scratchN, scratchN);
  1896. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  1897. var gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  1898. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  1899. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  1900. if (!defined(result)) {
  1901. result = new Cartesian3();
  1902. }
  1903. return Cartesian3.add(scratchK, scratchN, result);
  1904. };
  1905. /**
  1906. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  1907. *
  1908. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  1909. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  1910. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  1911. * @returns {Cartesian3[]} The array of positions.
  1912. *
  1913. * @example
  1914. * var positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  1915. */
  1916. Cartesian3.fromDegreesArray = function(coordinates, ellipsoid, result) {
  1917. Check.defined(coordinates, 'coordinates');
  1918. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  1919. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  1920. }
  1921. var length = coordinates.length;
  1922. if (!defined(result)) {
  1923. result = new Array(length / 2);
  1924. } else {
  1925. result.length = length / 2;
  1926. }
  1927. for (var i = 0; i < length; i += 2) {
  1928. var longitude = coordinates[i];
  1929. var latitude = coordinates[i + 1];
  1930. var index = i / 2;
  1931. result[index] = Cartesian3.fromDegrees(longitude, latitude, 0, ellipsoid, result[index]);
  1932. }
  1933. return result;
  1934. };
  1935. /**
  1936. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  1937. *
  1938. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  1939. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  1940. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  1941. * @returns {Cartesian3[]} The array of positions.
  1942. *
  1943. * @example
  1944. * var positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  1945. */
  1946. Cartesian3.fromRadiansArray = function(coordinates, ellipsoid, result) {
  1947. Check.defined(coordinates, 'coordinates');
  1948. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  1949. throw new DeveloperError('the number of coordinates must be a multiple of 2 and at least 2');
  1950. }
  1951. var length = coordinates.length;
  1952. if (!defined(result)) {
  1953. result = new Array(length / 2);
  1954. } else {
  1955. result.length = length / 2;
  1956. }
  1957. for (var i = 0; i < length; i += 2) {
  1958. var longitude = coordinates[i];
  1959. var latitude = coordinates[i + 1];
  1960. var index = i / 2;
  1961. result[index] = Cartesian3.fromRadians(longitude, latitude, 0, ellipsoid, result[index]);
  1962. }
  1963. return result;
  1964. };
  1965. /**
  1966. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  1967. *
  1968. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  1969. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1970. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  1971. * @returns {Cartesian3[]} The array of positions.
  1972. *
  1973. * @example
  1974. * var positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  1975. */
  1976. Cartesian3.fromDegreesArrayHeights = function(coordinates, ellipsoid, result) {
  1977. Check.defined(coordinates, 'coordinates');
  1978. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  1979. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  1980. }
  1981. var length = coordinates.length;
  1982. if (!defined(result)) {
  1983. result = new Array(length / 3);
  1984. } else {
  1985. result.length = length / 3;
  1986. }
  1987. for (var i = 0; i < length; i += 3) {
  1988. var longitude = coordinates[i];
  1989. var latitude = coordinates[i + 1];
  1990. var height = coordinates[i + 2];
  1991. var index = i / 3;
  1992. result[index] = Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid, result[index]);
  1993. }
  1994. return result;
  1995. };
  1996. /**
  1997. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  1998. *
  1999. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  2000. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2001. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  2002. * @returns {Cartesian3[]} The array of positions.
  2003. *
  2004. * @example
  2005. * var positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  2006. */
  2007. Cartesian3.fromRadiansArrayHeights = function(coordinates, ellipsoid, result) {
  2008. Check.defined(coordinates, 'coordinates');
  2009. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  2010. throw new DeveloperError('the number of coordinates must be a multiple of 3 and at least 3');
  2011. }
  2012. var length = coordinates.length;
  2013. if (!defined(result)) {
  2014. result = new Array(length / 3);
  2015. } else {
  2016. result.length = length / 3;
  2017. }
  2018. for (var i = 0; i < length; i += 3) {
  2019. var longitude = coordinates[i];
  2020. var latitude = coordinates[i + 1];
  2021. var height = coordinates[i + 2];
  2022. var index = i / 3;
  2023. result[index] = Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result[index]);
  2024. }
  2025. return result;
  2026. };
  2027. /**
  2028. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  2029. *
  2030. * @type {Cartesian3}
  2031. * @constant
  2032. */
  2033. Cartesian3.ZERO = freezeObject(new Cartesian3(0.0, 0.0, 0.0));
  2034. /**
  2035. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  2036. *
  2037. * @type {Cartesian3}
  2038. * @constant
  2039. */
  2040. Cartesian3.UNIT_X = freezeObject(new Cartesian3(1.0, 0.0, 0.0));
  2041. /**
  2042. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  2043. *
  2044. * @type {Cartesian3}
  2045. * @constant
  2046. */
  2047. Cartesian3.UNIT_Y = freezeObject(new Cartesian3(0.0, 1.0, 0.0));
  2048. /**
  2049. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  2050. *
  2051. * @type {Cartesian3}
  2052. * @constant
  2053. */
  2054. Cartesian3.UNIT_Z = freezeObject(new Cartesian3(0.0, 0.0, 1.0));
  2055. /**
  2056. * Duplicates this Cartesian3 instance.
  2057. *
  2058. * @param {Cartesian3} [result] The object onto which to store the result.
  2059. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  2060. */
  2061. Cartesian3.prototype.clone = function(result) {
  2062. return Cartesian3.clone(this, result);
  2063. };
  2064. /**
  2065. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2066. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2067. *
  2068. * @param {Cartesian3} [right] The right hand side Cartesian.
  2069. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2070. */
  2071. Cartesian3.prototype.equals = function(right) {
  2072. return Cartesian3.equals(this, right);
  2073. };
  2074. /**
  2075. * Compares this Cartesian against the provided Cartesian componentwise and returns
  2076. * <code>true</code> if they pass an absolute or relative tolerance test,
  2077. * <code>false</code> otherwise.
  2078. *
  2079. * @param {Cartesian3} [right] The right hand side Cartesian.
  2080. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  2081. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  2082. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  2083. */
  2084. Cartesian3.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  2085. return Cartesian3.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  2086. };
  2087. /**
  2088. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  2089. *
  2090. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  2091. */
  2092. Cartesian3.prototype.toString = function() {
  2093. return '(' + this.x + ', ' + this.y + ', ' + this.z + ')';
  2094. };
  2095. return Cartesian3;
  2096. });
  2097. /*global define*/
  2098. define('Core/scaleToGeodeticSurface',[
  2099. './Cartesian3',
  2100. './defined',
  2101. './DeveloperError',
  2102. './Math'
  2103. ], function(
  2104. Cartesian3,
  2105. defined,
  2106. DeveloperError,
  2107. CesiumMath) {
  2108. 'use strict';
  2109. var scaleToGeodeticSurfaceIntersection = new Cartesian3();
  2110. var scaleToGeodeticSurfaceGradient = new Cartesian3();
  2111. /**
  2112. * Scales the provided Cartesian position along the geodetic surface normal
  2113. * so that it is on the surface of this ellipsoid. If the position is
  2114. * at the center of the ellipsoid, this function returns undefined.
  2115. *
  2116. * @param {Cartesian3} cartesian The Cartesian position to scale.
  2117. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  2118. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  2119. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  2120. * @param {Cartesian3} [result] The object onto which to store the result.
  2121. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  2122. *
  2123. * @exports scaleToGeodeticSurface
  2124. *
  2125. * @private
  2126. */
  2127. function scaleToGeodeticSurface(cartesian, oneOverRadii, oneOverRadiiSquared, centerToleranceSquared, result) {
  2128. if (!defined(cartesian)) {
  2129. throw new DeveloperError('cartesian is required.');
  2130. }
  2131. if (!defined(oneOverRadii)) {
  2132. throw new DeveloperError('oneOverRadii is required.');
  2133. }
  2134. if (!defined(oneOverRadiiSquared)) {
  2135. throw new DeveloperError('oneOverRadiiSquared is required.');
  2136. }
  2137. if (!defined(centerToleranceSquared)) {
  2138. throw new DeveloperError('centerToleranceSquared is required.');
  2139. }
  2140. var positionX = cartesian.x;
  2141. var positionY = cartesian.y;
  2142. var positionZ = cartesian.z;
  2143. var oneOverRadiiX = oneOverRadii.x;
  2144. var oneOverRadiiY = oneOverRadii.y;
  2145. var oneOverRadiiZ = oneOverRadii.z;
  2146. var x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  2147. var y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  2148. var z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  2149. // Compute the squared ellipsoid norm.
  2150. var squaredNorm = x2 + y2 + z2;
  2151. var ratio = Math.sqrt(1.0 / squaredNorm);
  2152. // As an initial approximation, assume that the radial intersection is the projection point.
  2153. var intersection = Cartesian3.multiplyByScalar(cartesian, ratio, scaleToGeodeticSurfaceIntersection);
  2154. // If the position is near the center, the iteration will not converge.
  2155. if (squaredNorm < centerToleranceSquared) {
  2156. return !isFinite(ratio) ? undefined : Cartesian3.clone(intersection, result);
  2157. }
  2158. var oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  2159. var oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  2160. var oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  2161. // Use the gradient at the intersection point in place of the true unit normal.
  2162. // The difference in magnitude will be absorbed in the multiplier.
  2163. var gradient = scaleToGeodeticSurfaceGradient;
  2164. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  2165. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  2166. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  2167. // Compute the initial guess at the normal vector multiplier, lambda.
  2168. var lambda = (1.0 - ratio) * Cartesian3.magnitude(cartesian) / (0.5 * Cartesian3.magnitude(gradient));
  2169. var correction = 0.0;
  2170. var func;
  2171. var denominator;
  2172. var xMultiplier;
  2173. var yMultiplier;
  2174. var zMultiplier;
  2175. var xMultiplier2;
  2176. var yMultiplier2;
  2177. var zMultiplier2;
  2178. var xMultiplier3;
  2179. var yMultiplier3;
  2180. var zMultiplier3;
  2181. do {
  2182. lambda -= correction;
  2183. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  2184. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  2185. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  2186. xMultiplier2 = xMultiplier * xMultiplier;
  2187. yMultiplier2 = yMultiplier * yMultiplier;
  2188. zMultiplier2 = zMultiplier * zMultiplier;
  2189. xMultiplier3 = xMultiplier2 * xMultiplier;
  2190. yMultiplier3 = yMultiplier2 * yMultiplier;
  2191. zMultiplier3 = zMultiplier2 * zMultiplier;
  2192. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  2193. // "denominator" here refers to the use of this expression in the velocity and acceleration
  2194. // computations in the sections to follow.
  2195. denominator = x2 * xMultiplier3 * oneOverRadiiSquaredX + y2 * yMultiplier3 * oneOverRadiiSquaredY + z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  2196. var derivative = -2.0 * denominator;
  2197. correction = func / derivative;
  2198. } while (Math.abs(func) > CesiumMath.EPSILON12);
  2199. if (!defined(result)) {
  2200. return new Cartesian3(positionX * xMultiplier, positionY * yMultiplier, positionZ * zMultiplier);
  2201. }
  2202. result.x = positionX * xMultiplier;
  2203. result.y = positionY * yMultiplier;
  2204. result.z = positionZ * zMultiplier;
  2205. return result;
  2206. }
  2207. return scaleToGeodeticSurface;
  2208. });
  2209. /*global define*/
  2210. define('Core/Cartographic',[
  2211. './Cartesian3',
  2212. './defaultValue',
  2213. './defined',
  2214. './DeveloperError',
  2215. './freezeObject',
  2216. './Math',
  2217. './scaleToGeodeticSurface'
  2218. ], function(
  2219. Cartesian3,
  2220. defaultValue,
  2221. defined,
  2222. DeveloperError,
  2223. freezeObject,
  2224. CesiumMath,
  2225. scaleToGeodeticSurface) {
  2226. 'use strict';
  2227. /**
  2228. * A position defined by longitude, latitude, and height.
  2229. * @alias Cartographic
  2230. * @constructor
  2231. *
  2232. * @param {Number} [longitude=0.0] The longitude, in radians.
  2233. * @param {Number} [latitude=0.0] The latitude, in radians.
  2234. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2235. *
  2236. * @see Ellipsoid
  2237. */
  2238. function Cartographic(longitude, latitude, height) {
  2239. /**
  2240. * The longitude, in radians.
  2241. * @type {Number}
  2242. * @default 0.0
  2243. */
  2244. this.longitude = defaultValue(longitude, 0.0);
  2245. /**
  2246. * The latitude, in radians.
  2247. * @type {Number}
  2248. * @default 0.0
  2249. */
  2250. this.latitude = defaultValue(latitude, 0.0);
  2251. /**
  2252. * The height, in meters, above the ellipsoid.
  2253. * @type {Number}
  2254. * @default 0.0
  2255. */
  2256. this.height = defaultValue(height, 0.0);
  2257. }
  2258. /**
  2259. * Creates a new Cartographic instance from longitude and latitude
  2260. * specified in radians.
  2261. *
  2262. * @param {Number} longitude The longitude, in radians.
  2263. * @param {Number} latitude The latitude, in radians.
  2264. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2265. * @param {Cartographic} [result] The object onto which to store the result.
  2266. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  2267. */
  2268. Cartographic.fromRadians = function(longitude, latitude, height, result) {
  2269. if (!defined(longitude)) {
  2270. throw new DeveloperError('longitude is required.');
  2271. }
  2272. if (!defined(latitude)) {
  2273. throw new DeveloperError('latitude is required.');
  2274. }
  2275. height = defaultValue(height, 0.0);
  2276. if (!defined(result)) {
  2277. return new Cartographic(longitude, latitude, height);
  2278. }
  2279. result.longitude = longitude;
  2280. result.latitude = latitude;
  2281. result.height = height;
  2282. return result;
  2283. };
  2284. /**
  2285. * Creates a new Cartographic instance from longitude and latitude
  2286. * specified in degrees. The values in the resulting object will
  2287. * be in radians.
  2288. *
  2289. * @param {Number} longitude The longitude, in degrees.
  2290. * @param {Number} latitude The latitude, in degrees.
  2291. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  2292. * @param {Cartographic} [result] The object onto which to store the result.
  2293. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  2294. */
  2295. Cartographic.fromDegrees = function(longitude, latitude, height, result) {
  2296. if (!defined(longitude)) {
  2297. throw new DeveloperError('longitude is required.');
  2298. }
  2299. if (!defined(latitude)) {
  2300. throw new DeveloperError('latitude is required.');
  2301. }
  2302. longitude = CesiumMath.toRadians(longitude);
  2303. latitude = CesiumMath.toRadians(latitude);
  2304. return Cartographic.fromRadians(longitude, latitude, height, result);
  2305. };
  2306. var cartesianToCartographicN = new Cartesian3();
  2307. var cartesianToCartographicP = new Cartesian3();
  2308. var cartesianToCartographicH = new Cartesian3();
  2309. var wgs84OneOverRadii = new Cartesian3(1.0 / 6378137.0, 1.0 / 6378137.0, 1.0 / 6356752.3142451793);
  2310. var wgs84OneOverRadiiSquared = new Cartesian3(1.0 / (6378137.0 * 6378137.0), 1.0 / (6378137.0 * 6378137.0), 1.0 / (6356752.3142451793 * 6356752.3142451793));
  2311. var wgs84CenterToleranceSquared = CesiumMath.EPSILON1;
  2312. /**
  2313. * Creates a new Cartographic instance from a Cartesian position. The values in the
  2314. * resulting object will be in radians.
  2315. *
  2316. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  2317. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  2318. * @param {Cartographic} [result] The object onto which to store the result.
  2319. * @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.
  2320. */
  2321. Cartographic.fromCartesian = function(cartesian, ellipsoid, result) {
  2322. var oneOverRadii = defined(ellipsoid) ? ellipsoid.oneOverRadii : wgs84OneOverRadii;
  2323. var oneOverRadiiSquared = defined(ellipsoid) ? ellipsoid.oneOverRadiiSquared : wgs84OneOverRadiiSquared;
  2324. var centerToleranceSquared = defined(ellipsoid) ? ellipsoid._centerToleranceSquared : wgs84CenterToleranceSquared;
  2325. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  2326. var p = scaleToGeodeticSurface(cartesian, oneOverRadii, oneOverRadiiSquared, centerToleranceSquared, cartesianToCartographicP);
  2327. if (!defined(p)) {
  2328. return undefined;
  2329. }
  2330. var n = Cartesian3.multiplyComponents(p, oneOverRadiiSquared, cartesianToCartographicN);
  2331. n = Cartesian3.normalize(n, n);
  2332. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  2333. var longitude = Math.atan2(n.y, n.x);
  2334. var latitude = Math.asin(n.z);
  2335. var height = CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  2336. if (!defined(result)) {
  2337. return new Cartographic(longitude, latitude, height);
  2338. }
  2339. result.longitude = longitude;
  2340. result.latitude = latitude;
  2341. result.height = height;
  2342. return result;
  2343. };
  2344. /**
  2345. * Duplicates a Cartographic instance.
  2346. *
  2347. * @param {Cartographic} cartographic The cartographic to duplicate.
  2348. * @param {Cartographic} [result] The object onto which to store the result.
  2349. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  2350. */
  2351. Cartographic.clone = function(cartographic, result) {
  2352. if (!defined(cartographic)) {
  2353. return undefined;
  2354. }
  2355. if (!defined(result)) {
  2356. return new Cartographic(cartographic.longitude, cartographic.latitude, cartographic.height);
  2357. }
  2358. result.longitude = cartographic.longitude;
  2359. result.latitude = cartographic.latitude;
  2360. result.height = cartographic.height;
  2361. return result;
  2362. };
  2363. /**
  2364. * Compares the provided cartographics componentwise and returns
  2365. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2366. *
  2367. * @param {Cartographic} [left] The first cartographic.
  2368. * @param {Cartographic} [right] The second cartographic.
  2369. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2370. */
  2371. Cartographic.equals = function(left, right) {
  2372. return (left === right) ||
  2373. ((defined(left)) &&
  2374. (defined(right)) &&
  2375. (left.longitude === right.longitude) &&
  2376. (left.latitude === right.latitude) &&
  2377. (left.height === right.height));
  2378. };
  2379. /**
  2380. * Compares the provided cartographics componentwise and returns
  2381. * <code>true</code> if they are within the provided epsilon,
  2382. * <code>false</code> otherwise.
  2383. *
  2384. * @param {Cartographic} [left] The first cartographic.
  2385. * @param {Cartographic} [right] The second cartographic.
  2386. * @param {Number} epsilon The epsilon to use for equality testing.
  2387. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2388. */
  2389. Cartographic.equalsEpsilon = function(left, right, epsilon) {
  2390. if (typeof epsilon !== 'number') {
  2391. throw new DeveloperError('epsilon is required and must be a number.');
  2392. }
  2393. return (left === right) ||
  2394. ((defined(left)) &&
  2395. (defined(right)) &&
  2396. (Math.abs(left.longitude - right.longitude) <= epsilon) &&
  2397. (Math.abs(left.latitude - right.latitude) <= epsilon) &&
  2398. (Math.abs(left.height - right.height) <= epsilon));
  2399. };
  2400. /**
  2401. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  2402. *
  2403. * @type {Cartographic}
  2404. * @constant
  2405. */
  2406. Cartographic.ZERO = freezeObject(new Cartographic(0.0, 0.0, 0.0));
  2407. /**
  2408. * Duplicates this instance.
  2409. *
  2410. * @param {Cartographic} [result] The object onto which to store the result.
  2411. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  2412. */
  2413. Cartographic.prototype.clone = function(result) {
  2414. return Cartographic.clone(this, result);
  2415. };
  2416. /**
  2417. * Compares the provided against this cartographic componentwise and returns
  2418. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2419. *
  2420. * @param {Cartographic} [right] The second cartographic.
  2421. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  2422. */
  2423. Cartographic.prototype.equals = function(right) {
  2424. return Cartographic.equals(this, right);
  2425. };
  2426. /**
  2427. * Compares the provided against this cartographic componentwise and returns
  2428. * <code>true</code> if they are within the provided epsilon,
  2429. * <code>false</code> otherwise.
  2430. *
  2431. * @param {Cartographic} [right] The second cartographic.
  2432. * @param {Number} epsilon The epsilon to use for equality testing.
  2433. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2434. */
  2435. Cartographic.prototype.equalsEpsilon = function(right, epsilon) {
  2436. return Cartographic.equalsEpsilon(this, right, epsilon);
  2437. };
  2438. /**
  2439. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  2440. *
  2441. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  2442. */
  2443. Cartographic.prototype.toString = function() {
  2444. return '(' + this.longitude + ', ' + this.latitude + ', ' + this.height + ')';
  2445. };
  2446. return Cartographic;
  2447. });
  2448. /*global define*/
  2449. define('Core/defineProperties',[
  2450. './defined'
  2451. ], function(
  2452. defined) {
  2453. 'use strict';
  2454. var definePropertyWorks = (function() {
  2455. try {
  2456. return 'x' in Object.defineProperty({}, 'x', {});
  2457. } catch (e) {
  2458. return false;
  2459. }
  2460. })();
  2461. /**
  2462. * Defines properties on an object, using Object.defineProperties if available,
  2463. * otherwise returns the object unchanged. This function should be used in
  2464. * setup code to prevent errors from completely halting JavaScript execution
  2465. * in legacy browsers.
  2466. *
  2467. * @private
  2468. *
  2469. * @exports defineProperties
  2470. */
  2471. var defineProperties = Object.defineProperties;
  2472. if (!definePropertyWorks || !defined(defineProperties)) {
  2473. defineProperties = function(o) {
  2474. return o;
  2475. };
  2476. }
  2477. return defineProperties;
  2478. });
  2479. /*global define*/
  2480. define('Core/Ellipsoid',[
  2481. './Cartesian3',
  2482. './Cartographic',
  2483. './defaultValue',
  2484. './defined',
  2485. './defineProperties',
  2486. './DeveloperError',
  2487. './freezeObject',
  2488. './Math',
  2489. './scaleToGeodeticSurface'
  2490. ], function(
  2491. Cartesian3,
  2492. Cartographic,
  2493. defaultValue,
  2494. defined,
  2495. defineProperties,
  2496. DeveloperError,
  2497. freezeObject,
  2498. CesiumMath,
  2499. scaleToGeodeticSurface) {
  2500. 'use strict';
  2501. function initialize(ellipsoid, x, y, z) {
  2502. x = defaultValue(x, 0.0);
  2503. y = defaultValue(y, 0.0);
  2504. z = defaultValue(z, 0.0);
  2505. if (x < 0.0 || y < 0.0 || z < 0.0) {
  2506. throw new DeveloperError('All radii components must be greater than or equal to zero.');
  2507. }
  2508. ellipsoid._radii = new Cartesian3(x, y, z);
  2509. ellipsoid._radiiSquared = new Cartesian3(x * x,
  2510. y * y,
  2511. z * z);
  2512. ellipsoid._radiiToTheFourth = new Cartesian3(x * x * x * x,
  2513. y * y * y * y,
  2514. z * z * z * z);
  2515. ellipsoid._oneOverRadii = new Cartesian3(x === 0.0 ? 0.0 : 1.0 / x,
  2516. y === 0.0 ? 0.0 : 1.0 / y,
  2517. z === 0.0 ? 0.0 : 1.0 / z);
  2518. ellipsoid._oneOverRadiiSquared = new Cartesian3(x === 0.0 ? 0.0 : 1.0 / (x * x),
  2519. y === 0.0 ? 0.0 : 1.0 / (y * y),
  2520. z === 0.0 ? 0.0 : 1.0 / (z * z));
  2521. ellipsoid._minimumRadius = Math.min(x, y, z);
  2522. ellipsoid._maximumRadius = Math.max(x, y, z);
  2523. ellipsoid._centerToleranceSquared = CesiumMath.EPSILON1;
  2524. if (ellipsoid._radiiSquared.z !== 0) {
  2525. ellipsoid._sqauredXOverSquaredZ = ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  2526. }
  2527. }
  2528. /**
  2529. * A quadratic surface defined in Cartesian coordinates by the equation
  2530. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  2531. * by Cesium to represent the shape of planetary bodies.
  2532. *
  2533. * Rather than constructing this object directly, one of the provided
  2534. * constants is normally used.
  2535. * @alias Ellipsoid
  2536. * @constructor
  2537. *
  2538. * @param {Number} [x=0] The radius in the x direction.
  2539. * @param {Number} [y=0] The radius in the y direction.
  2540. * @param {Number} [z=0] The radius in the z direction.
  2541. *
  2542. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  2543. *
  2544. * @see Ellipsoid.fromCartesian3
  2545. * @see Ellipsoid.WGS84
  2546. * @see Ellipsoid.UNIT_SPHERE
  2547. */
  2548. function Ellipsoid(x, y, z) {
  2549. this._radii = undefined;
  2550. this._radiiSquared = undefined;
  2551. this._radiiToTheFourth = undefined;
  2552. this._oneOverRadii = undefined;
  2553. this._oneOverRadiiSquared = undefined;
  2554. this._minimumRadius = undefined;
  2555. this._maximumRadius = undefined;
  2556. this._centerToleranceSquared = undefined;
  2557. this._sqauredXOverSquaredZ = undefined;
  2558. initialize(this, x, y, z);
  2559. }
  2560. defineProperties(Ellipsoid.prototype, {
  2561. /**
  2562. * Gets the radii of the ellipsoid.
  2563. * @memberof Ellipsoid.prototype
  2564. * @type {Cartesian3}
  2565. * @readonly
  2566. */
  2567. radii : {
  2568. get: function() {
  2569. return this._radii;
  2570. }
  2571. },
  2572. /**
  2573. * Gets the squared radii of the ellipsoid.
  2574. * @memberof Ellipsoid.prototype
  2575. * @type {Cartesian3}
  2576. * @readonly
  2577. */
  2578. radiiSquared : {
  2579. get : function() {
  2580. return this._radiiSquared;
  2581. }
  2582. },
  2583. /**
  2584. * Gets the radii of the ellipsoid raise to the fourth power.
  2585. * @memberof Ellipsoid.prototype
  2586. * @type {Cartesian3}
  2587. * @readonly
  2588. */
  2589. radiiToTheFourth : {
  2590. get : function() {
  2591. return this._radiiToTheFourth;
  2592. }
  2593. },
  2594. /**
  2595. * Gets one over the radii of the ellipsoid.
  2596. * @memberof Ellipsoid.prototype
  2597. * @type {Cartesian3}
  2598. * @readonly
  2599. */
  2600. oneOverRadii : {
  2601. get : function() {
  2602. return this._oneOverRadii;
  2603. }
  2604. },
  2605. /**
  2606. * Gets one over the squared radii of the ellipsoid.
  2607. * @memberof Ellipsoid.prototype
  2608. * @type {Cartesian3}
  2609. * @readonly
  2610. */
  2611. oneOverRadiiSquared : {
  2612. get : function() {
  2613. return this._oneOverRadiiSquared;
  2614. }
  2615. },
  2616. /**
  2617. * Gets the minimum radius of the ellipsoid.
  2618. * @memberof Ellipsoid.prototype
  2619. * @type {Number}
  2620. * @readonly
  2621. */
  2622. minimumRadius : {
  2623. get : function() {
  2624. return this._minimumRadius;
  2625. }
  2626. },
  2627. /**
  2628. * Gets the maximum radius of the ellipsoid.
  2629. * @memberof Ellipsoid.prototype
  2630. * @type {Number}
  2631. * @readonly
  2632. */
  2633. maximumRadius : {
  2634. get : function() {
  2635. return this._maximumRadius;
  2636. }
  2637. }
  2638. });
  2639. /**
  2640. * Duplicates an Ellipsoid instance.
  2641. *
  2642. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  2643. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  2644. * instance should be created.
  2645. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  2646. */
  2647. Ellipsoid.clone = function(ellipsoid, result) {
  2648. if (!defined(ellipsoid)) {
  2649. return undefined;
  2650. }
  2651. var radii = ellipsoid._radii;
  2652. if (!defined(result)) {
  2653. return new Ellipsoid(radii.x, radii.y, radii.z);
  2654. }
  2655. Cartesian3.clone(radii, result._radii);
  2656. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  2657. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  2658. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  2659. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  2660. result._minimumRadius = ellipsoid._minimumRadius;
  2661. result._maximumRadius = ellipsoid._maximumRadius;
  2662. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  2663. return result;
  2664. };
  2665. /**
  2666. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  2667. *
  2668. * @param {Cartesian3} [radii=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  2669. * @returns {Ellipsoid} A new Ellipsoid instance.
  2670. *
  2671. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  2672. *
  2673. * @see Ellipsoid.WGS84
  2674. * @see Ellipsoid.UNIT_SPHERE
  2675. */
  2676. Ellipsoid.fromCartesian3 = function(cartesian, result) {
  2677. if (!defined(result)) {
  2678. result = new Ellipsoid();
  2679. }
  2680. if (!defined(cartesian)) {
  2681. return result;
  2682. }
  2683. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  2684. return result;
  2685. };
  2686. /**
  2687. * An Ellipsoid instance initialized to the WGS84 standard.
  2688. *
  2689. * @type {Ellipsoid}
  2690. * @constant
  2691. */
  2692. Ellipsoid.WGS84 = freezeObject(new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793));
  2693. /**
  2694. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  2695. *
  2696. * @type {Ellipsoid}
  2697. * @constant
  2698. */
  2699. Ellipsoid.UNIT_SPHERE = freezeObject(new Ellipsoid(1.0, 1.0, 1.0));
  2700. /**
  2701. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  2702. *
  2703. * @type {Ellipsoid}
  2704. * @constant
  2705. */
  2706. Ellipsoid.MOON = freezeObject(new Ellipsoid(CesiumMath.LUNAR_RADIUS, CesiumMath.LUNAR_RADIUS, CesiumMath.LUNAR_RADIUS));
  2707. /**
  2708. * Duplicates an Ellipsoid instance.
  2709. *
  2710. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  2711. * instance should be created.
  2712. * @returns {Ellipsoid} The cloned Ellipsoid.
  2713. */
  2714. Ellipsoid.prototype.clone = function(result) {
  2715. return Ellipsoid.clone(this, result);
  2716. };
  2717. /**
  2718. * The number of elements used to pack the object into an array.
  2719. * @type {Number}
  2720. */
  2721. Ellipsoid.packedLength = Cartesian3.packedLength;
  2722. /**
  2723. * Stores the provided instance into the provided array.
  2724. *
  2725. * @param {Ellipsoid} value The value to pack.
  2726. * @param {Number[]} array The array to pack into.
  2727. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2728. *
  2729. * @returns {Number[]} The array that was packed into
  2730. */
  2731. Ellipsoid.pack = function(value, array, startingIndex) {
  2732. if (!defined(value)) {
  2733. throw new DeveloperError('value is required');
  2734. }
  2735. if (!defined(array)) {
  2736. throw new DeveloperError('array is required');
  2737. }
  2738. startingIndex = defaultValue(startingIndex, 0);
  2739. Cartesian3.pack(value._radii, array, startingIndex);
  2740. return array;
  2741. };
  2742. /**
  2743. * Retrieves an instance from a packed array.
  2744. *
  2745. * @param {Number[]} array The packed array.
  2746. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2747. * @param {Ellipsoid} [result] The object into which to store the result.
  2748. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  2749. */
  2750. Ellipsoid.unpack = function(array, startingIndex, result) {
  2751. if (!defined(array)) {
  2752. throw new DeveloperError('array is required');
  2753. }
  2754. startingIndex = defaultValue(startingIndex, 0);
  2755. var radii = Cartesian3.unpack(array, startingIndex);
  2756. return Ellipsoid.fromCartesian3(radii, result);
  2757. };
  2758. /**
  2759. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  2760. * @function
  2761. *
  2762. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  2763. * @param {Cartesian3} [result] The object onto which to store the result.
  2764. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2765. */
  2766. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  2767. /**
  2768. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  2769. *
  2770. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  2771. * @param {Cartesian3} [result] The object onto which to store the result.
  2772. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2773. */
  2774. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function(cartographic, result) {
  2775. if (!defined(cartographic)) {
  2776. throw new DeveloperError('cartographic is required.');
  2777. }
  2778. var longitude = cartographic.longitude;
  2779. var latitude = cartographic.latitude;
  2780. var cosLatitude = Math.cos(latitude);
  2781. var x = cosLatitude * Math.cos(longitude);
  2782. var y = cosLatitude * Math.sin(longitude);
  2783. var z = Math.sin(latitude);
  2784. if (!defined(result)) {
  2785. result = new Cartesian3();
  2786. }
  2787. result.x = x;
  2788. result.y = y;
  2789. result.z = z;
  2790. return Cartesian3.normalize(result, result);
  2791. };
  2792. /**
  2793. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  2794. *
  2795. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  2796. * @param {Cartesian3} [result] The object onto which to store the result.
  2797. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2798. */
  2799. Ellipsoid.prototype.geodeticSurfaceNormal = function(cartesian, result) {
  2800. if (!defined(result)) {
  2801. result = new Cartesian3();
  2802. }
  2803. result = Cartesian3.multiplyComponents(cartesian, this._oneOverRadiiSquared, result);
  2804. return Cartesian3.normalize(result, result);
  2805. };
  2806. var cartographicToCartesianNormal = new Cartesian3();
  2807. var cartographicToCartesianK = new Cartesian3();
  2808. /**
  2809. * Converts the provided cartographic to Cartesian representation.
  2810. *
  2811. * @param {Cartographic} cartographic The cartographic position.
  2812. * @param {Cartesian3} [result] The object onto which to store the result.
  2813. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2814. *
  2815. * @example
  2816. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  2817. * var position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  2818. * var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  2819. */
  2820. Ellipsoid.prototype.cartographicToCartesian = function(cartographic, result) {
  2821. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  2822. var n = cartographicToCartesianNormal;
  2823. var k = cartographicToCartesianK;
  2824. this.geodeticSurfaceNormalCartographic(cartographic, n);
  2825. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  2826. var gamma = Math.sqrt(Cartesian3.dot(n, k));
  2827. Cartesian3.divideByScalar(k, gamma, k);
  2828. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  2829. if (!defined(result)) {
  2830. result = new Cartesian3();
  2831. }
  2832. return Cartesian3.add(k, n, result);
  2833. };
  2834. /**
  2835. * Converts the provided array of cartographics to an array of Cartesians.
  2836. *
  2837. * @param {Cartographic[]} cartographics An array of cartographic positions.
  2838. * @param {Cartesian3[]} [result] The object onto which to store the result.
  2839. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  2840. *
  2841. * @example
  2842. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  2843. * var positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  2844. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  2845. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  2846. * var cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  2847. */
  2848. Ellipsoid.prototype.cartographicArrayToCartesianArray = function(cartographics, result) {
  2849. if (!defined(cartographics)) {
  2850. throw new DeveloperError('cartographics is required.');
  2851. }
  2852. var length = cartographics.length;
  2853. if (!defined(result)) {
  2854. result = new Array(length);
  2855. } else {
  2856. result.length = length;
  2857. }
  2858. for ( var i = 0; i < length; i++) {
  2859. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  2860. }
  2861. return result;
  2862. };
  2863. var cartesianToCartographicN = new Cartesian3();
  2864. var cartesianToCartographicP = new Cartesian3();
  2865. var cartesianToCartographicH = new Cartesian3();
  2866. /**
  2867. * Converts the provided cartesian to cartographic representation.
  2868. * The cartesian is undefined at the center of the ellipsoid.
  2869. *
  2870. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  2871. * @param {Cartographic} [result] The object onto which to store the result.
  2872. * @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.
  2873. *
  2874. * @example
  2875. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  2876. * var position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  2877. * var cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  2878. */
  2879. Ellipsoid.prototype.cartesianToCartographic = function(cartesian, result) {
  2880. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  2881. var p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP);
  2882. if (!defined(p)) {
  2883. return undefined;
  2884. }
  2885. var n = this.geodeticSurfaceNormal(p, cartesianToCartographicN);
  2886. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  2887. var longitude = Math.atan2(n.y, n.x);
  2888. var latitude = Math.asin(n.z);
  2889. var height = CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  2890. if (!defined(result)) {
  2891. return new Cartographic(longitude, latitude, height);
  2892. }
  2893. result.longitude = longitude;
  2894. result.latitude = latitude;
  2895. result.height = height;
  2896. return result;
  2897. };
  2898. /**
  2899. * Converts the provided array of cartesians to an array of cartographics.
  2900. *
  2901. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  2902. * @param {Cartographic[]} [result] The object onto which to store the result.
  2903. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  2904. *
  2905. * @example
  2906. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  2907. * var positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  2908. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  2909. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  2910. * var cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  2911. */
  2912. Ellipsoid.prototype.cartesianArrayToCartographicArray = function(cartesians, result) {
  2913. if (!defined(cartesians)) {
  2914. throw new DeveloperError('cartesians is required.');
  2915. }
  2916. var length = cartesians.length;
  2917. if (!defined(result)) {
  2918. result = new Array(length);
  2919. } else {
  2920. result.length = length;
  2921. }
  2922. for ( var i = 0; i < length; ++i) {
  2923. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  2924. }
  2925. return result;
  2926. };
  2927. /**
  2928. * Scales the provided Cartesian position along the geodetic surface normal
  2929. * so that it is on the surface of this ellipsoid. If the position is
  2930. * at the center of the ellipsoid, this function returns undefined.
  2931. *
  2932. * @param {Cartesian3} cartesian The Cartesian position to scale.
  2933. * @param {Cartesian3} [result] The object onto which to store the result.
  2934. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  2935. */
  2936. Ellipsoid.prototype.scaleToGeodeticSurface = function(cartesian, result) {
  2937. return scaleToGeodeticSurface(cartesian, this._oneOverRadii, this._oneOverRadiiSquared, this._centerToleranceSquared, result);
  2938. };
  2939. /**
  2940. * Scales the provided Cartesian position along the geocentric surface normal
  2941. * so that it is on the surface of this ellipsoid.
  2942. *
  2943. * @param {Cartesian3} cartesian The Cartesian position to scale.
  2944. * @param {Cartesian3} [result] The object onto which to store the result.
  2945. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  2946. */
  2947. Ellipsoid.prototype.scaleToGeocentricSurface = function(cartesian, result) {
  2948. if (!defined(cartesian)) {
  2949. throw new DeveloperError('cartesian is required.');
  2950. }
  2951. if (!defined(result)) {
  2952. result = new Cartesian3();
  2953. }
  2954. var positionX = cartesian.x;
  2955. var positionY = cartesian.y;
  2956. var positionZ = cartesian.z;
  2957. var oneOverRadiiSquared = this._oneOverRadiiSquared;
  2958. var beta = 1.0 / Math.sqrt((positionX * positionX) * oneOverRadiiSquared.x +
  2959. (positionY * positionY) * oneOverRadiiSquared.y +
  2960. (positionZ * positionZ) * oneOverRadiiSquared.z);
  2961. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  2962. };
  2963. /**
  2964. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  2965. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  2966. *
  2967. * @param {Cartesian3} position The position to transform.
  2968. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2969. * return a new instance.
  2970. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  2971. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2972. */
  2973. Ellipsoid.prototype.transformPositionToScaledSpace = function(position, result) {
  2974. if (!defined(result)) {
  2975. result = new Cartesian3();
  2976. }
  2977. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  2978. };
  2979. /**
  2980. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  2981. * its components by the result of {@link Ellipsoid#radii}.
  2982. *
  2983. * @param {Cartesian3} position The position to transform.
  2984. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  2985. * return a new instance.
  2986. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2987. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2988. */
  2989. Ellipsoid.prototype.transformPositionFromScaledSpace = function(position, result) {
  2990. if (!defined(result)) {
  2991. result = new Cartesian3();
  2992. }
  2993. return Cartesian3.multiplyComponents(position, this._radii, result);
  2994. };
  2995. /**
  2996. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2997. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2998. *
  2999. * @param {Ellipsoid} [right] The other Ellipsoid.
  3000. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3001. */
  3002. Ellipsoid.prototype.equals = function(right) {
  3003. return (this === right) ||
  3004. (defined(right) &&
  3005. Cartesian3.equals(this._radii, right._radii));
  3006. };
  3007. /**
  3008. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  3009. *
  3010. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  3011. */
  3012. Ellipsoid.prototype.toString = function() {
  3013. return this._radii.toString();
  3014. };
  3015. /**
  3016. * Computes a point which is the intersection of the surface normal with the z-axis.
  3017. *
  3018. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  3019. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  3020. * 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.
  3021. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  3022. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  3023. * @param {Cartesian} [result] The cartesian to which to copy the result, or undefined to create and
  3024. * return a new instance.
  3025. * @returns {Cartesian | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  3026. *
  3027. * @exception {DeveloperError} position is required.
  3028. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  3029. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  3030. */
  3031. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function(position, buffer, result) {
  3032. if (!defined(position)) {
  3033. throw new DeveloperError('position is required.');
  3034. }
  3035. if (!CesiumMath.equalsEpsilon(this._radii.x, this._radii.y, CesiumMath.EPSILON15)) {
  3036. throw new DeveloperError('Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)');
  3037. }
  3038. if (this._radii.z === 0) {
  3039. throw new DeveloperError('Ellipsoid.radii.z must be greater than 0');
  3040. }
  3041. buffer = defaultValue(buffer, 0.0);
  3042. var sqauredXOverSquaredZ = this._sqauredXOverSquaredZ;
  3043. if (!defined(result)) {
  3044. result = new Cartesian3();
  3045. }
  3046. result.x = 0.0;
  3047. result.y = 0.0;
  3048. result.z = position.z * (1 - sqauredXOverSquaredZ);
  3049. if (Math.abs(result.z) >= this._radii.z - buffer) {
  3050. return undefined;
  3051. }
  3052. return result;
  3053. };
  3054. return Ellipsoid;
  3055. });
  3056. /*global define*/
  3057. define('Core/GeographicProjection',[
  3058. './Cartesian3',
  3059. './Cartographic',
  3060. './defaultValue',
  3061. './defined',
  3062. './defineProperties',
  3063. './DeveloperError',
  3064. './Ellipsoid'
  3065. ], function(
  3066. Cartesian3,
  3067. Cartographic,
  3068. defaultValue,
  3069. defined,
  3070. defineProperties,
  3071. DeveloperError,
  3072. Ellipsoid) {
  3073. 'use strict';
  3074. /**
  3075. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  3076. * them by the {@link Ellipsoid#maximumRadius}. This projection
  3077. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  3078. * is also known as EPSG:4326.
  3079. *
  3080. * @alias GeographicProjection
  3081. * @constructor
  3082. *
  3083. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  3084. *
  3085. * @see WebMercatorProjection
  3086. */
  3087. function GeographicProjection(ellipsoid) {
  3088. this._ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  3089. this._semimajorAxis = this._ellipsoid.maximumRadius;
  3090. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  3091. }
  3092. defineProperties(GeographicProjection.prototype, {
  3093. /**
  3094. * Gets the {@link Ellipsoid}.
  3095. *
  3096. * @memberof GeographicProjection.prototype
  3097. *
  3098. * @type {Ellipsoid}
  3099. * @readonly
  3100. */
  3101. ellipsoid : {
  3102. get : function() {
  3103. return this._ellipsoid;
  3104. }
  3105. }
  3106. });
  3107. /**
  3108. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  3109. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  3110. * ellipsoid. Z is the unmodified height.
  3111. *
  3112. * @param {Cartographic} cartographic The coordinates to project.
  3113. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  3114. * undefined, a new instance is created and returned.
  3115. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  3116. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  3117. * created and returned.
  3118. */
  3119. GeographicProjection.prototype.project = function(cartographic, result) {
  3120. // Actually this is the special case of equidistant cylindrical called the plate carree
  3121. var semimajorAxis = this._semimajorAxis;
  3122. var x = cartographic.longitude * semimajorAxis;
  3123. var y = cartographic.latitude * semimajorAxis;
  3124. var z = cartographic.height;
  3125. if (!defined(result)) {
  3126. return new Cartesian3(x, y, z);
  3127. }
  3128. result.x = x;
  3129. result.y = y;
  3130. result.z = z;
  3131. return result;
  3132. };
  3133. /**
  3134. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  3135. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  3136. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  3137. *
  3138. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  3139. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  3140. * undefined, a new instance is created and returned.
  3141. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  3142. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  3143. * created and returned.
  3144. */
  3145. GeographicProjection.prototype.unproject = function(cartesian, result) {
  3146. if (!defined(cartesian)) {
  3147. throw new DeveloperError('cartesian is required');
  3148. }
  3149. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  3150. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  3151. var latitude = cartesian.y * oneOverEarthSemimajorAxis;
  3152. var height = cartesian.z;
  3153. if (!defined(result)) {
  3154. return new Cartographic(longitude, latitude, height);
  3155. }
  3156. result.longitude = longitude;
  3157. result.latitude = latitude;
  3158. result.height = height;
  3159. return result;
  3160. };
  3161. return GeographicProjection;
  3162. });
  3163. /*global define*/
  3164. define('Core/Intersect',[
  3165. './freezeObject'
  3166. ], function(
  3167. freezeObject) {
  3168. 'use strict';
  3169. /**
  3170. * This enumerated type is used in determining where, relative to the frustum, an
  3171. * object is located. The object can either be fully contained within the frustum (INSIDE),
  3172. * partially inside the frustum and partially outside (INTERSECTING), or somwhere entirely
  3173. * outside of the frustum's 6 planes (OUTSIDE).
  3174. *
  3175. * @exports Intersect
  3176. */
  3177. var Intersect = {
  3178. /**
  3179. * Represents that an object is not contained within the frustum.
  3180. *
  3181. * @type {Number}
  3182. * @constant
  3183. */
  3184. OUTSIDE : -1,
  3185. /**
  3186. * Represents that an object intersects one of the frustum's planes.
  3187. *
  3188. * @type {Number}
  3189. * @constant
  3190. */
  3191. INTERSECTING : 0,
  3192. /**
  3193. * Represents that an object is fully within the frustum.
  3194. *
  3195. * @type {Number}
  3196. * @constant
  3197. */
  3198. INSIDE : 1
  3199. };
  3200. return freezeObject(Intersect);
  3201. });
  3202. /*global define*/
  3203. define('Core/Interval',[
  3204. './defaultValue'
  3205. ], function(
  3206. defaultValue) {
  3207. 'use strict';
  3208. /**
  3209. * Represents the closed interval [start, stop].
  3210. * @alias Interval
  3211. * @constructor
  3212. *
  3213. * @param {Number} [start=0.0] The beginning of the interval.
  3214. * @param {Number} [stop=0.0] The end of the interval.
  3215. */
  3216. function Interval(start, stop) {
  3217. /**
  3218. * The beginning of the interval.
  3219. * @type {Number}
  3220. * @default 0.0
  3221. */
  3222. this.start = defaultValue(start, 0.0);
  3223. /**
  3224. * The end of the interval.
  3225. * @type {Number}
  3226. * @default 0.0
  3227. */
  3228. this.stop = defaultValue(stop, 0.0);
  3229. }
  3230. return Interval;
  3231. });
  3232. /*global define*/
  3233. define('Core/Matrix3',[
  3234. './Cartesian3',
  3235. './defaultValue',
  3236. './defined',
  3237. './defineProperties',
  3238. './DeveloperError',
  3239. './freezeObject',
  3240. './Math'
  3241. ], function(
  3242. Cartesian3,
  3243. defaultValue,
  3244. defined,
  3245. defineProperties,
  3246. DeveloperError,
  3247. freezeObject,
  3248. CesiumMath) {
  3249. 'use strict';
  3250. /**
  3251. * A 3x3 matrix, indexable as a column-major order array.
  3252. * Constructor parameters are in row-major order for code readability.
  3253. * @alias Matrix3
  3254. * @constructor
  3255. *
  3256. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  3257. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  3258. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  3259. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  3260. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  3261. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  3262. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  3263. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  3264. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  3265. *
  3266. * @see Matrix3.fromColumnMajorArray
  3267. * @see Matrix3.fromRowMajorArray
  3268. * @see Matrix3.fromQuaternion
  3269. * @see Matrix3.fromScale
  3270. * @see Matrix3.fromUniformScale
  3271. * @see Matrix2
  3272. * @see Matrix4
  3273. */
  3274. function Matrix3(column0Row0, column1Row0, column2Row0,
  3275. column0Row1, column1Row1, column2Row1,
  3276. column0Row2, column1Row2, column2Row2) {
  3277. this[0] = defaultValue(column0Row0, 0.0);
  3278. this[1] = defaultValue(column0Row1, 0.0);
  3279. this[2] = defaultValue(column0Row2, 0.0);
  3280. this[3] = defaultValue(column1Row0, 0.0);
  3281. this[4] = defaultValue(column1Row1, 0.0);
  3282. this[5] = defaultValue(column1Row2, 0.0);
  3283. this[6] = defaultValue(column2Row0, 0.0);
  3284. this[7] = defaultValue(column2Row1, 0.0);
  3285. this[8] = defaultValue(column2Row2, 0.0);
  3286. }
  3287. /**
  3288. * The number of elements used to pack the object into an array.
  3289. * @type {Number}
  3290. */
  3291. Matrix3.packedLength = 9;
  3292. /**
  3293. * Stores the provided instance into the provided array.
  3294. *
  3295. * @param {Matrix3} value The value to pack.
  3296. * @param {Number[]} array The array to pack into.
  3297. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3298. *
  3299. * @returns {Number[]} The array that was packed into
  3300. */
  3301. Matrix3.pack = function(value, array, startingIndex) {
  3302. if (!defined(value)) {
  3303. throw new DeveloperError('value is required');
  3304. }
  3305. if (!defined(array)) {
  3306. throw new DeveloperError('array is required');
  3307. }
  3308. startingIndex = defaultValue(startingIndex, 0);
  3309. array[startingIndex++] = value[0];
  3310. array[startingIndex++] = value[1];
  3311. array[startingIndex++] = value[2];
  3312. array[startingIndex++] = value[3];
  3313. array[startingIndex++] = value[4];
  3314. array[startingIndex++] = value[5];
  3315. array[startingIndex++] = value[6];
  3316. array[startingIndex++] = value[7];
  3317. array[startingIndex++] = value[8];
  3318. return array;
  3319. };
  3320. /**
  3321. * Retrieves an instance from a packed array.
  3322. *
  3323. * @param {Number[]} array The packed array.
  3324. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3325. * @param {Matrix3} [result] The object into which to store the result.
  3326. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  3327. */
  3328. Matrix3.unpack = function(array, startingIndex, result) {
  3329. if (!defined(array)) {
  3330. throw new DeveloperError('array is required');
  3331. }
  3332. startingIndex = defaultValue(startingIndex, 0);
  3333. if (!defined(result)) {
  3334. result = new Matrix3();
  3335. }
  3336. result[0] = array[startingIndex++];
  3337. result[1] = array[startingIndex++];
  3338. result[2] = array[startingIndex++];
  3339. result[3] = array[startingIndex++];
  3340. result[4] = array[startingIndex++];
  3341. result[5] = array[startingIndex++];
  3342. result[6] = array[startingIndex++];
  3343. result[7] = array[startingIndex++];
  3344. result[8] = array[startingIndex++];
  3345. return result;
  3346. };
  3347. /**
  3348. * Duplicates a Matrix3 instance.
  3349. *
  3350. * @param {Matrix3} matrix The matrix to duplicate.
  3351. * @param {Matrix3} [result] The object onto which to store the result.
  3352. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  3353. */
  3354. Matrix3.clone = function(values, result) {
  3355. if (!defined(values)) {
  3356. return undefined;
  3357. }
  3358. if (!defined(result)) {
  3359. return new Matrix3(values[0], values[3], values[6],
  3360. values[1], values[4], values[7],
  3361. values[2], values[5], values[8]);
  3362. }
  3363. result[0] = values[0];
  3364. result[1] = values[1];
  3365. result[2] = values[2];
  3366. result[3] = values[3];
  3367. result[4] = values[4];
  3368. result[5] = values[5];
  3369. result[6] = values[6];
  3370. result[7] = values[7];
  3371. result[8] = values[8];
  3372. return result;
  3373. };
  3374. /**
  3375. * Creates a Matrix3 from 9 consecutive elements in an array.
  3376. *
  3377. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  3378. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  3379. * @param {Matrix3} [result] The object onto which to store the result.
  3380. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  3381. *
  3382. * @example
  3383. * // Create the Matrix3:
  3384. * // [1.0, 2.0, 3.0]
  3385. * // [1.0, 2.0, 3.0]
  3386. * // [1.0, 2.0, 3.0]
  3387. *
  3388. * var v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  3389. * var m = Cesium.Matrix3.fromArray(v);
  3390. *
  3391. * // Create same Matrix3 with using an offset into an array
  3392. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  3393. * var m2 = Cesium.Matrix3.fromArray(v2, 2);
  3394. */
  3395. Matrix3.fromArray = function(array, startingIndex, result) {
  3396. if (!defined(array)) {
  3397. throw new DeveloperError('array is required');
  3398. }
  3399. startingIndex = defaultValue(startingIndex, 0);
  3400. if (!defined(result)) {
  3401. result = new Matrix3();
  3402. }
  3403. result[0] = array[startingIndex];
  3404. result[1] = array[startingIndex + 1];
  3405. result[2] = array[startingIndex + 2];
  3406. result[3] = array[startingIndex + 3];
  3407. result[4] = array[startingIndex + 4];
  3408. result[5] = array[startingIndex + 5];
  3409. result[6] = array[startingIndex + 6];
  3410. result[7] = array[startingIndex + 7];
  3411. result[8] = array[startingIndex + 8];
  3412. return result;
  3413. };
  3414. /**
  3415. * Creates a Matrix3 instance from a column-major order array.
  3416. *
  3417. * @param {Number[]} values The column-major order array.
  3418. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3419. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3420. */
  3421. Matrix3.fromColumnMajorArray = function(values, result) {
  3422. if (!defined(values)) {
  3423. throw new DeveloperError('values parameter is required');
  3424. }
  3425. return Matrix3.clone(values, result);
  3426. };
  3427. /**
  3428. * Creates a Matrix3 instance from a row-major order array.
  3429. * The resulting matrix will be in column-major order.
  3430. *
  3431. * @param {Number[]} values The row-major order array.
  3432. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3433. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3434. */
  3435. Matrix3.fromRowMajorArray = function(values, result) {
  3436. if (!defined(values)) {
  3437. throw new DeveloperError('values is required.');
  3438. }
  3439. if (!defined(result)) {
  3440. return new Matrix3(values[0], values[1], values[2],
  3441. values[3], values[4], values[5],
  3442. values[6], values[7], values[8]);
  3443. }
  3444. result[0] = values[0];
  3445. result[1] = values[3];
  3446. result[2] = values[6];
  3447. result[3] = values[1];
  3448. result[4] = values[4];
  3449. result[5] = values[7];
  3450. result[6] = values[2];
  3451. result[7] = values[5];
  3452. result[8] = values[8];
  3453. return result;
  3454. };
  3455. /**
  3456. * Computes a 3x3 rotation matrix from the provided quaternion.
  3457. *
  3458. * @param {Quaternion} quaternion the quaternion to use.
  3459. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3460. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  3461. */
  3462. Matrix3.fromQuaternion = function(quaternion, result) {
  3463. if (!defined(quaternion)) {
  3464. throw new DeveloperError('quaternion is required');
  3465. }
  3466. var x2 = quaternion.x * quaternion.x;
  3467. var xy = quaternion.x * quaternion.y;
  3468. var xz = quaternion.x * quaternion.z;
  3469. var xw = quaternion.x * quaternion.w;
  3470. var y2 = quaternion.y * quaternion.y;
  3471. var yz = quaternion.y * quaternion.z;
  3472. var yw = quaternion.y * quaternion.w;
  3473. var z2 = quaternion.z * quaternion.z;
  3474. var zw = quaternion.z * quaternion.w;
  3475. var w2 = quaternion.w * quaternion.w;
  3476. var m00 = x2 - y2 - z2 + w2;
  3477. var m01 = 2.0 * (xy - zw);
  3478. var m02 = 2.0 * (xz + yw);
  3479. var m10 = 2.0 * (xy + zw);
  3480. var m11 = -x2 + y2 - z2 + w2;
  3481. var m12 = 2.0 * (yz - xw);
  3482. var m20 = 2.0 * (xz - yw);
  3483. var m21 = 2.0 * (yz + xw);
  3484. var m22 = -x2 - y2 + z2 + w2;
  3485. if (!defined(result)) {
  3486. return new Matrix3(m00, m01, m02,
  3487. m10, m11, m12,
  3488. m20, m21, m22);
  3489. }
  3490. result[0] = m00;
  3491. result[1] = m10;
  3492. result[2] = m20;
  3493. result[3] = m01;
  3494. result[4] = m11;
  3495. result[5] = m21;
  3496. result[6] = m02;
  3497. result[7] = m12;
  3498. result[8] = m22;
  3499. return result;
  3500. };
  3501. /**
  3502. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  3503. *
  3504. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  3505. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3506. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  3507. */
  3508. Matrix3.fromHeadingPitchRoll = function(headingPitchRoll, result) {
  3509. if (!defined(headingPitchRoll)) {
  3510. throw new DeveloperError('headingPitchRoll is required');
  3511. }
  3512. var cosTheta = Math.cos(-headingPitchRoll.pitch);
  3513. var cosPsi = Math.cos(-headingPitchRoll.heading);
  3514. var cosPhi = Math.cos(headingPitchRoll.roll);
  3515. var sinTheta = Math.sin(-headingPitchRoll.pitch);
  3516. var sinPsi = Math.sin(-headingPitchRoll.heading);
  3517. var sinPhi = Math.sin(headingPitchRoll.roll);
  3518. var m00 = cosTheta * cosPsi;
  3519. var m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  3520. var m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  3521. var m10 = cosTheta * sinPsi;
  3522. var m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  3523. var m12 = -sinTheta * cosPhi + cosPhi * sinTheta * sinPsi;
  3524. var m20 = -sinTheta;
  3525. var m21 = sinPhi * cosTheta;
  3526. var m22 = cosPhi * cosTheta;
  3527. if (!defined(result)) {
  3528. return new Matrix3(m00, m01, m02,
  3529. m10, m11, m12,
  3530. m20, m21, m22);
  3531. }
  3532. result[0] = m00;
  3533. result[1] = m10;
  3534. result[2] = m20;
  3535. result[3] = m01;
  3536. result[4] = m11;
  3537. result[5] = m21;
  3538. result[6] = m02;
  3539. result[7] = m12;
  3540. result[8] = m22;
  3541. return result;
  3542. };
  3543. /**
  3544. * Computes a Matrix3 instance representing a non-uniform scale.
  3545. *
  3546. * @param {Cartesian3} scale The x, y, and z scale factors.
  3547. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3548. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3549. *
  3550. * @example
  3551. * // Creates
  3552. * // [7.0, 0.0, 0.0]
  3553. * // [0.0, 8.0, 0.0]
  3554. * // [0.0, 0.0, 9.0]
  3555. * var m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  3556. */
  3557. Matrix3.fromScale = function(scale, result) {
  3558. if (!defined(scale)) {
  3559. throw new DeveloperError('scale is required.');
  3560. }
  3561. if (!defined(result)) {
  3562. return new Matrix3(
  3563. scale.x, 0.0, 0.0,
  3564. 0.0, scale.y, 0.0,
  3565. 0.0, 0.0, scale.z);
  3566. }
  3567. result[0] = scale.x;
  3568. result[1] = 0.0;
  3569. result[2] = 0.0;
  3570. result[3] = 0.0;
  3571. result[4] = scale.y;
  3572. result[5] = 0.0;
  3573. result[6] = 0.0;
  3574. result[7] = 0.0;
  3575. result[8] = scale.z;
  3576. return result;
  3577. };
  3578. /**
  3579. * Computes a Matrix3 instance representing a uniform scale.
  3580. *
  3581. * @param {Number} scale The uniform scale factor.
  3582. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3583. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3584. *
  3585. * @example
  3586. * // Creates
  3587. * // [2.0, 0.0, 0.0]
  3588. * // [0.0, 2.0, 0.0]
  3589. * // [0.0, 0.0, 2.0]
  3590. * var m = Cesium.Matrix3.fromUniformScale(2.0);
  3591. */
  3592. Matrix3.fromUniformScale = function(scale, result) {
  3593. if (typeof scale !== 'number') {
  3594. throw new DeveloperError('scale is required.');
  3595. }
  3596. if (!defined(result)) {
  3597. return new Matrix3(
  3598. scale, 0.0, 0.0,
  3599. 0.0, scale, 0.0,
  3600. 0.0, 0.0, scale);
  3601. }
  3602. result[0] = scale;
  3603. result[1] = 0.0;
  3604. result[2] = 0.0;
  3605. result[3] = 0.0;
  3606. result[4] = scale;
  3607. result[5] = 0.0;
  3608. result[6] = 0.0;
  3609. result[7] = 0.0;
  3610. result[8] = scale;
  3611. return result;
  3612. };
  3613. /**
  3614. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  3615. *
  3616. * @param {Cartesian3} the vector on the left hand side of the cross product operation.
  3617. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3618. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3619. *
  3620. * @example
  3621. * // Creates
  3622. * // [0.0, -9.0, 8.0]
  3623. * // [9.0, 0.0, -7.0]
  3624. * // [-8.0, 7.0, 0.0]
  3625. * var m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  3626. */
  3627. Matrix3.fromCrossProduct = function(vector, result) {
  3628. if (!defined(vector)) {
  3629. throw new DeveloperError('vector is required.');
  3630. }
  3631. if (!defined(result)) {
  3632. return new Matrix3(
  3633. 0.0, -vector.z, vector.y,
  3634. vector.z, 0.0, -vector.x,
  3635. -vector.y, vector.x, 0.0);
  3636. }
  3637. result[0] = 0.0;
  3638. result[1] = vector.z;
  3639. result[2] = -vector.y;
  3640. result[3] = -vector.z;
  3641. result[4] = 0.0;
  3642. result[5] = vector.x;
  3643. result[6] = vector.y;
  3644. result[7] = -vector.x;
  3645. result[8] = 0.0;
  3646. return result;
  3647. };
  3648. /**
  3649. * Creates a rotation matrix around the x-axis.
  3650. *
  3651. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  3652. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3653. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3654. *
  3655. * @example
  3656. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  3657. * var p = new Cesium.Cartesian3(5, 6, 7);
  3658. * var m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  3659. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  3660. */
  3661. Matrix3.fromRotationX = function(angle, result) {
  3662. if (!defined(angle)) {
  3663. throw new DeveloperError('angle is required.');
  3664. }
  3665. var cosAngle = Math.cos(angle);
  3666. var sinAngle = Math.sin(angle);
  3667. if (!defined(result)) {
  3668. return new Matrix3(
  3669. 1.0, 0.0, 0.0,
  3670. 0.0, cosAngle, -sinAngle,
  3671. 0.0, sinAngle, cosAngle);
  3672. }
  3673. result[0] = 1.0;
  3674. result[1] = 0.0;
  3675. result[2] = 0.0;
  3676. result[3] = 0.0;
  3677. result[4] = cosAngle;
  3678. result[5] = sinAngle;
  3679. result[6] = 0.0;
  3680. result[7] = -sinAngle;
  3681. result[8] = cosAngle;
  3682. return result;
  3683. };
  3684. /**
  3685. * Creates a rotation matrix around the y-axis.
  3686. *
  3687. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  3688. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3689. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3690. *
  3691. * @example
  3692. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  3693. * var p = new Cesium.Cartesian3(5, 6, 7);
  3694. * var m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  3695. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  3696. */
  3697. Matrix3.fromRotationY = function(angle, result) {
  3698. if (!defined(angle)) {
  3699. throw new DeveloperError('angle is required.');
  3700. }
  3701. var cosAngle = Math.cos(angle);
  3702. var sinAngle = Math.sin(angle);
  3703. if (!defined(result)) {
  3704. return new Matrix3(
  3705. cosAngle, 0.0, sinAngle,
  3706. 0.0, 1.0, 0.0,
  3707. -sinAngle, 0.0, cosAngle);
  3708. }
  3709. result[0] = cosAngle;
  3710. result[1] = 0.0;
  3711. result[2] = -sinAngle;
  3712. result[3] = 0.0;
  3713. result[4] = 1.0;
  3714. result[5] = 0.0;
  3715. result[6] = sinAngle;
  3716. result[7] = 0.0;
  3717. result[8] = cosAngle;
  3718. return result;
  3719. };
  3720. /**
  3721. * Creates a rotation matrix around the z-axis.
  3722. *
  3723. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  3724. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  3725. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  3726. *
  3727. * @example
  3728. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  3729. * var p = new Cesium.Cartesian3(5, 6, 7);
  3730. * var m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  3731. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  3732. */
  3733. Matrix3.fromRotationZ = function(angle, result) {
  3734. if (!defined(angle)) {
  3735. throw new DeveloperError('angle is required.');
  3736. }
  3737. var cosAngle = Math.cos(angle);
  3738. var sinAngle = Math.sin(angle);
  3739. if (!defined(result)) {
  3740. return new Matrix3(
  3741. cosAngle, -sinAngle, 0.0,
  3742. sinAngle, cosAngle, 0.0,
  3743. 0.0, 0.0, 1.0);
  3744. }
  3745. result[0] = cosAngle;
  3746. result[1] = sinAngle;
  3747. result[2] = 0.0;
  3748. result[3] = -sinAngle;
  3749. result[4] = cosAngle;
  3750. result[5] = 0.0;
  3751. result[6] = 0.0;
  3752. result[7] = 0.0;
  3753. result[8] = 1.0;
  3754. return result;
  3755. };
  3756. /**
  3757. * Creates an Array from the provided Matrix3 instance.
  3758. * The array will be in column-major order.
  3759. *
  3760. * @param {Matrix3} matrix The matrix to use..
  3761. * @param {Number[]} [result] The Array onto which to store the result.
  3762. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  3763. */
  3764. Matrix3.toArray = function(matrix, result) {
  3765. if (!defined(matrix)) {
  3766. throw new DeveloperError('matrix is required');
  3767. }
  3768. if (!defined(result)) {
  3769. return [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8]];
  3770. }
  3771. result[0] = matrix[0];
  3772. result[1] = matrix[1];
  3773. result[2] = matrix[2];
  3774. result[3] = matrix[3];
  3775. result[4] = matrix[4];
  3776. result[5] = matrix[5];
  3777. result[6] = matrix[6];
  3778. result[7] = matrix[7];
  3779. result[8] = matrix[8];
  3780. return result;
  3781. };
  3782. /**
  3783. * Computes the array index of the element at the provided row and column.
  3784. *
  3785. * @param {Number} row The zero-based index of the row.
  3786. * @param {Number} column The zero-based index of the column.
  3787. * @returns {Number} The index of the element at the provided row and column.
  3788. *
  3789. * @exception {DeveloperError} row must be 0, 1, or 2.
  3790. * @exception {DeveloperError} column must be 0, 1, or 2.
  3791. *
  3792. * @example
  3793. * var myMatrix = new Cesium.Matrix3();
  3794. * var column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  3795. * var column1Row0 = myMatrix[column1Row0Index]
  3796. * myMatrix[column1Row0Index] = 10.0;
  3797. */
  3798. Matrix3.getElementIndex = function(column, row) {
  3799. if (typeof row !== 'number' || row < 0 || row > 2) {
  3800. throw new DeveloperError('row must be 0, 1, or 2.');
  3801. }
  3802. if (typeof column !== 'number' || column < 0 || column > 2) {
  3803. throw new DeveloperError('column must be 0, 1, or 2.');
  3804. }
  3805. return column * 3 + row;
  3806. };
  3807. /**
  3808. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  3809. *
  3810. * @param {Matrix3} matrix The matrix to use.
  3811. * @param {Number} index The zero-based index of the column to retrieve.
  3812. * @param {Cartesian3} result The object onto which to store the result.
  3813. * @returns {Cartesian3} The modified result parameter.
  3814. *
  3815. * @exception {DeveloperError} index must be 0, 1, or 2.
  3816. */
  3817. Matrix3.getColumn = function(matrix, index, result) {
  3818. if (!defined(matrix)) {
  3819. throw new DeveloperError('matrix is required.');
  3820. }
  3821. if (typeof index !== 'number' || index < 0 || index > 2) {
  3822. throw new DeveloperError('index must be 0, 1, or 2.');
  3823. }
  3824. if (!defined(result)) {
  3825. throw new DeveloperError('result is required');
  3826. }
  3827. var startIndex = index * 3;
  3828. var x = matrix[startIndex];
  3829. var y = matrix[startIndex + 1];
  3830. var z = matrix[startIndex + 2];
  3831. result.x = x;
  3832. result.y = y;
  3833. result.z = z;
  3834. return result;
  3835. };
  3836. /**
  3837. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  3838. *
  3839. * @param {Matrix3} matrix The matrix to use.
  3840. * @param {Number} index The zero-based index of the column to set.
  3841. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  3842. * @param {Matrix3} result The object onto which to store the result.
  3843. * @returns {Matrix3} The modified result parameter.
  3844. *
  3845. * @exception {DeveloperError} index must be 0, 1, or 2.
  3846. */
  3847. Matrix3.setColumn = function(matrix, index, cartesian, result) {
  3848. if (!defined(matrix)) {
  3849. throw new DeveloperError('matrix is required');
  3850. }
  3851. if (!defined(cartesian)) {
  3852. throw new DeveloperError('cartesian is required');
  3853. }
  3854. if (typeof index !== 'number' || index < 0 || index > 2) {
  3855. throw new DeveloperError('index must be 0, 1, or 2.');
  3856. }
  3857. if (!defined(result)) {
  3858. throw new DeveloperError('result is required');
  3859. }
  3860. result = Matrix3.clone(matrix, result);
  3861. var startIndex = index * 3;
  3862. result[startIndex] = cartesian.x;
  3863. result[startIndex + 1] = cartesian.y;
  3864. result[startIndex + 2] = cartesian.z;
  3865. return result;
  3866. };
  3867. /**
  3868. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  3869. *
  3870. * @param {Matrix3} matrix The matrix to use.
  3871. * @param {Number} index The zero-based index of the row to retrieve.
  3872. * @param {Cartesian3} result The object onto which to store the result.
  3873. * @returns {Cartesian3} The modified result parameter.
  3874. *
  3875. * @exception {DeveloperError} index must be 0, 1, or 2.
  3876. */
  3877. Matrix3.getRow = function(matrix, index, result) {
  3878. if (!defined(matrix)) {
  3879. throw new DeveloperError('matrix is required.');
  3880. }
  3881. if (typeof index !== 'number' || index < 0 || index > 2) {
  3882. throw new DeveloperError('index must be 0, 1, or 2.');
  3883. }
  3884. if (!defined(result)) {
  3885. throw new DeveloperError('result is required');
  3886. }
  3887. var x = matrix[index];
  3888. var y = matrix[index + 3];
  3889. var z = matrix[index + 6];
  3890. result.x = x;
  3891. result.y = y;
  3892. result.z = z;
  3893. return result;
  3894. };
  3895. /**
  3896. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  3897. *
  3898. * @param {Matrix3} matrix The matrix to use.
  3899. * @param {Number} index The zero-based index of the row to set.
  3900. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  3901. * @param {Matrix3} result The object onto which to store the result.
  3902. * @returns {Matrix3} The modified result parameter.
  3903. *
  3904. * @exception {DeveloperError} index must be 0, 1, or 2.
  3905. */
  3906. Matrix3.setRow = function(matrix, index, cartesian, result) {
  3907. if (!defined(matrix)) {
  3908. throw new DeveloperError('matrix is required');
  3909. }
  3910. if (!defined(cartesian)) {
  3911. throw new DeveloperError('cartesian is required');
  3912. }
  3913. if (typeof index !== 'number' || index < 0 || index > 2) {
  3914. throw new DeveloperError('index must be 0, 1, or 2.');
  3915. }
  3916. if (!defined(result)) {
  3917. throw new DeveloperError('result is required');
  3918. }
  3919. result = Matrix3.clone(matrix, result);
  3920. result[index] = cartesian.x;
  3921. result[index + 3] = cartesian.y;
  3922. result[index + 6] = cartesian.z;
  3923. return result;
  3924. };
  3925. var scratchColumn = new Cartesian3();
  3926. /**
  3927. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  3928. *
  3929. * @param {Matrix3} matrix The matrix.
  3930. * @param {Cartesian3} result The object onto which to store the result.
  3931. * @returns {Cartesian3} The modified result parameter.
  3932. */
  3933. Matrix3.getScale = function(matrix, result) {
  3934. if (!defined(matrix)) {
  3935. throw new DeveloperError('matrix is required.');
  3936. }
  3937. if (!defined(result)) {
  3938. throw new DeveloperError('result is required');
  3939. }
  3940. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  3941. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn));
  3942. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn));
  3943. return result;
  3944. };
  3945. var scratchScale = new Cartesian3();
  3946. /**
  3947. * Computes the maximum scale assuming the matrix is an affine transformation.
  3948. * The maximum scale is the maximum length of the column vectors.
  3949. *
  3950. * @param {Matrix3} matrix The matrix.
  3951. * @returns {Number} The maximum scale.
  3952. */
  3953. Matrix3.getMaximumScale = function(matrix) {
  3954. Matrix3.getScale(matrix, scratchScale);
  3955. return Cartesian3.maximumComponent(scratchScale);
  3956. };
  3957. /**
  3958. * Computes the product of two matrices.
  3959. *
  3960. * @param {Matrix3} left The first matrix.
  3961. * @param {Matrix3} right The second matrix.
  3962. * @param {Matrix3} result The object onto which to store the result.
  3963. * @returns {Matrix3} The modified result parameter.
  3964. */
  3965. Matrix3.multiply = function(left, right, result) {
  3966. if (!defined(left)) {
  3967. throw new DeveloperError('left is required');
  3968. }
  3969. if (!defined(right)) {
  3970. throw new DeveloperError('right is required');
  3971. }
  3972. if (!defined(result)) {
  3973. throw new DeveloperError('result is required');
  3974. }
  3975. var column0Row0 = left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  3976. var column0Row1 = left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  3977. var column0Row2 = left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  3978. var column1Row0 = left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  3979. var column1Row1 = left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  3980. var column1Row2 = left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  3981. var column2Row0 = left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  3982. var column2Row1 = left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  3983. var column2Row2 = left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  3984. result[0] = column0Row0;
  3985. result[1] = column0Row1;
  3986. result[2] = column0Row2;
  3987. result[3] = column1Row0;
  3988. result[4] = column1Row1;
  3989. result[5] = column1Row2;
  3990. result[6] = column2Row0;
  3991. result[7] = column2Row1;
  3992. result[8] = column2Row2;
  3993. return result;
  3994. };
  3995. /**
  3996. * Computes the sum of two matrices.
  3997. *
  3998. * @param {Matrix3} left The first matrix.
  3999. * @param {Matrix3} right The second matrix.
  4000. * @param {Matrix3} result The object onto which to store the result.
  4001. * @returns {Matrix3} The modified result parameter.
  4002. */
  4003. Matrix3.add = function(left, right, result) {
  4004. if (!defined(left)) {
  4005. throw new DeveloperError('left is required');
  4006. }
  4007. if (!defined(right)) {
  4008. throw new DeveloperError('right is required');
  4009. }
  4010. if (!defined(result)) {
  4011. throw new DeveloperError('result is required');
  4012. }
  4013. result[0] = left[0] + right[0];
  4014. result[1] = left[1] + right[1];
  4015. result[2] = left[2] + right[2];
  4016. result[3] = left[3] + right[3];
  4017. result[4] = left[4] + right[4];
  4018. result[5] = left[5] + right[5];
  4019. result[6] = left[6] + right[6];
  4020. result[7] = left[7] + right[7];
  4021. result[8] = left[8] + right[8];
  4022. return result;
  4023. };
  4024. /**
  4025. * Computes the difference of two matrices.
  4026. *
  4027. * @param {Matrix3} left The first matrix.
  4028. * @param {Matrix3} right The second matrix.
  4029. * @param {Matrix3} result The object onto which to store the result.
  4030. * @returns {Matrix3} The modified result parameter.
  4031. */
  4032. Matrix3.subtract = function(left, right, result) {
  4033. if (!defined(left)) {
  4034. throw new DeveloperError('left is required');
  4035. }
  4036. if (!defined(right)) {
  4037. throw new DeveloperError('right is required');
  4038. }
  4039. if (!defined(result)) {
  4040. throw new DeveloperError('result is required');
  4041. }
  4042. result[0] = left[0] - right[0];
  4043. result[1] = left[1] - right[1];
  4044. result[2] = left[2] - right[2];
  4045. result[3] = left[3] - right[3];
  4046. result[4] = left[4] - right[4];
  4047. result[5] = left[5] - right[5];
  4048. result[6] = left[6] - right[6];
  4049. result[7] = left[7] - right[7];
  4050. result[8] = left[8] - right[8];
  4051. return result;
  4052. };
  4053. /**
  4054. * Computes the product of a matrix and a column vector.
  4055. *
  4056. * @param {Matrix3} matrix The matrix.
  4057. * @param {Cartesian3} cartesian The column.
  4058. * @param {Cartesian3} result The object onto which to store the result.
  4059. * @returns {Cartesian3} The modified result parameter.
  4060. */
  4061. Matrix3.multiplyByVector = function(matrix, cartesian, result) {
  4062. if (!defined(matrix)) {
  4063. throw new DeveloperError('matrix is required');
  4064. }
  4065. if (!defined(cartesian)) {
  4066. throw new DeveloperError('cartesian is required');
  4067. }
  4068. if (!defined(result)) {
  4069. throw new DeveloperError('result is required');
  4070. }
  4071. var vX = cartesian.x;
  4072. var vY = cartesian.y;
  4073. var vZ = cartesian.z;
  4074. var x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  4075. var y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  4076. var z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  4077. result.x = x;
  4078. result.y = y;
  4079. result.z = z;
  4080. return result;
  4081. };
  4082. /**
  4083. * Computes the product of a matrix and a scalar.
  4084. *
  4085. * @param {Matrix3} matrix The matrix.
  4086. * @param {Number} scalar The number to multiply by.
  4087. * @param {Matrix3} result The object onto which to store the result.
  4088. * @returns {Matrix3} The modified result parameter.
  4089. */
  4090. Matrix3.multiplyByScalar = function(matrix, scalar, result) {
  4091. if (!defined(matrix)) {
  4092. throw new DeveloperError('matrix is required');
  4093. }
  4094. if (typeof scalar !== 'number') {
  4095. throw new DeveloperError('scalar must be a number');
  4096. }
  4097. if (!defined(result)) {
  4098. throw new DeveloperError('result is required');
  4099. }
  4100. result[0] = matrix[0] * scalar;
  4101. result[1] = matrix[1] * scalar;
  4102. result[2] = matrix[2] * scalar;
  4103. result[3] = matrix[3] * scalar;
  4104. result[4] = matrix[4] * scalar;
  4105. result[5] = matrix[5] * scalar;
  4106. result[6] = matrix[6] * scalar;
  4107. result[7] = matrix[7] * scalar;
  4108. result[8] = matrix[8] * scalar;
  4109. return result;
  4110. };
  4111. /**
  4112. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  4113. *
  4114. * @param {Matrix3} matrix The matrix on the left-hand side.
  4115. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  4116. * @param {Matrix3} result The object onto which to store the result.
  4117. * @returns {Matrix3} The modified result parameter.
  4118. *
  4119. *
  4120. * @example
  4121. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  4122. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  4123. *
  4124. * @see Matrix3.fromScale
  4125. * @see Matrix3.multiplyByUniformScale
  4126. */
  4127. Matrix3.multiplyByScale = function(matrix, scale, result) {
  4128. if (!defined(matrix)) {
  4129. throw new DeveloperError('matrix is required');
  4130. }
  4131. if (!defined(scale)) {
  4132. throw new DeveloperError('scale is required');
  4133. }
  4134. if (!defined(result)) {
  4135. throw new DeveloperError('result is required');
  4136. }
  4137. result[0] = matrix[0] * scale.x;
  4138. result[1] = matrix[1] * scale.x;
  4139. result[2] = matrix[2] * scale.x;
  4140. result[3] = matrix[3] * scale.y;
  4141. result[4] = matrix[4] * scale.y;
  4142. result[5] = matrix[5] * scale.y;
  4143. result[6] = matrix[6] * scale.z;
  4144. result[7] = matrix[7] * scale.z;
  4145. result[8] = matrix[8] * scale.z;
  4146. return result;
  4147. };
  4148. /**
  4149. * Creates a negated copy of the provided matrix.
  4150. *
  4151. * @param {Matrix3} matrix The matrix to negate.
  4152. * @param {Matrix3} result The object onto which to store the result.
  4153. * @returns {Matrix3} The modified result parameter.
  4154. */
  4155. Matrix3.negate = function(matrix, result) {
  4156. if (!defined(matrix)) {
  4157. throw new DeveloperError('matrix is required');
  4158. }
  4159. if (!defined(result)) {
  4160. throw new DeveloperError('result is required');
  4161. }
  4162. result[0] = -matrix[0];
  4163. result[1] = -matrix[1];
  4164. result[2] = -matrix[2];
  4165. result[3] = -matrix[3];
  4166. result[4] = -matrix[4];
  4167. result[5] = -matrix[5];
  4168. result[6] = -matrix[6];
  4169. result[7] = -matrix[7];
  4170. result[8] = -matrix[8];
  4171. return result;
  4172. };
  4173. /**
  4174. * Computes the transpose of the provided matrix.
  4175. *
  4176. * @param {Matrix3} matrix The matrix to transpose.
  4177. * @param {Matrix3} result The object onto which to store the result.
  4178. * @returns {Matrix3} The modified result parameter.
  4179. */
  4180. Matrix3.transpose = function(matrix, result) {
  4181. if (!defined(matrix)) {
  4182. throw new DeveloperError('matrix is required');
  4183. }
  4184. if (!defined(result)) {
  4185. throw new DeveloperError('result is required');
  4186. }
  4187. var column0Row0 = matrix[0];
  4188. var column0Row1 = matrix[3];
  4189. var column0Row2 = matrix[6];
  4190. var column1Row0 = matrix[1];
  4191. var column1Row1 = matrix[4];
  4192. var column1Row2 = matrix[7];
  4193. var column2Row0 = matrix[2];
  4194. var column2Row1 = matrix[5];
  4195. var column2Row2 = matrix[8];
  4196. result[0] = column0Row0;
  4197. result[1] = column0Row1;
  4198. result[2] = column0Row2;
  4199. result[3] = column1Row0;
  4200. result[4] = column1Row1;
  4201. result[5] = column1Row2;
  4202. result[6] = column2Row0;
  4203. result[7] = column2Row1;
  4204. result[8] = column2Row2;
  4205. return result;
  4206. };
  4207. function computeFrobeniusNorm(matrix) {
  4208. var norm = 0.0;
  4209. for (var i = 0; i < 9; ++i) {
  4210. var temp = matrix[i];
  4211. norm += temp * temp;
  4212. }
  4213. return Math.sqrt(norm);
  4214. }
  4215. var rowVal = [1, 0, 0];
  4216. var colVal = [2, 2, 1];
  4217. function offDiagonalFrobeniusNorm(matrix) {
  4218. // Computes the "off-diagonal" Frobenius norm.
  4219. // Assumes matrix is symmetric.
  4220. var norm = 0.0;
  4221. for (var i = 0; i < 3; ++i) {
  4222. var temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  4223. norm += 2.0 * temp * temp;
  4224. }
  4225. return Math.sqrt(norm);
  4226. }
  4227. function shurDecomposition(matrix, result) {
  4228. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  4229. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  4230. //
  4231. // The routine takes a matrix, which is assumed to be symmetric, and
  4232. // finds the largest off-diagonal term, and then creates
  4233. // a matrix (result) which can be used to help reduce it
  4234. var tolerance = CesiumMath.EPSILON15;
  4235. var maxDiagonal = 0.0;
  4236. var rotAxis = 1;
  4237. // find pivot (rotAxis) based on max diagonal of matrix
  4238. for (var i = 0; i < 3; ++i) {
  4239. var temp = Math.abs(matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]);
  4240. if (temp > maxDiagonal) {
  4241. rotAxis = i;
  4242. maxDiagonal = temp;
  4243. }
  4244. }
  4245. var c = 1.0;
  4246. var s = 0.0;
  4247. var p = rowVal[rotAxis];
  4248. var q = colVal[rotAxis];
  4249. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  4250. var qq = matrix[Matrix3.getElementIndex(q, q)];
  4251. var pp = matrix[Matrix3.getElementIndex(p, p)];
  4252. var qp = matrix[Matrix3.getElementIndex(q, p)];
  4253. var tau = (qq - pp) / 2.0 / qp;
  4254. var t;
  4255. if (tau < 0.0) {
  4256. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  4257. } else {
  4258. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  4259. }
  4260. c = 1.0 / Math.sqrt(1.0 + t * t);
  4261. s = t * c;
  4262. }
  4263. result = Matrix3.clone(Matrix3.IDENTITY, result);
  4264. result[Matrix3.getElementIndex(p, p)] = result[Matrix3.getElementIndex(q, q)] = c;
  4265. result[Matrix3.getElementIndex(q, p)] = s;
  4266. result[Matrix3.getElementIndex(p, q)] = -s;
  4267. return result;
  4268. }
  4269. var jMatrix = new Matrix3();
  4270. var jMatrixTranspose = new Matrix3();
  4271. /**
  4272. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  4273. * <p>
  4274. * Returns a diagonal matrix and unitary matrix such that:
  4275. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  4276. * </p>
  4277. * <p>
  4278. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  4279. * of the unitary matrix are the corresponding eigenvectors.
  4280. * </p>
  4281. *
  4282. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  4283. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  4284. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  4285. *
  4286. * @example
  4287. * var a = //... symetric matrix
  4288. * var result = {
  4289. * unitary : new Cesium.Matrix3(),
  4290. * diagonal : new Cesium.Matrix3()
  4291. * };
  4292. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  4293. *
  4294. * var unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  4295. * var b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  4296. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  4297. *
  4298. * var lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  4299. * var v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  4300. * var c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  4301. */
  4302. Matrix3.computeEigenDecomposition = function(matrix, result) {
  4303. if (!defined(matrix)) {
  4304. throw new DeveloperError('matrix is required.');
  4305. }
  4306. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  4307. // section 8.4.3 The Classical Jacobi Algorithm
  4308. var tolerance = CesiumMath.EPSILON20;
  4309. var maxSweeps = 10;
  4310. var count = 0;
  4311. var sweep = 0;
  4312. if (!defined(result)) {
  4313. result = {};
  4314. }
  4315. var unitaryMatrix = result.unitary = Matrix3.clone(Matrix3.IDENTITY, result.unitary);
  4316. var diagMatrix = result.diagonal = Matrix3.clone(matrix, result.diagonal);
  4317. var epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  4318. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  4319. shurDecomposition(diagMatrix, jMatrix);
  4320. Matrix3.transpose(jMatrix, jMatrixTranspose);
  4321. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  4322. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  4323. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  4324. if (++count > 2) {
  4325. ++sweep;
  4326. count = 0;
  4327. }
  4328. }
  4329. return result;
  4330. };
  4331. /**
  4332. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  4333. *
  4334. * @param {Matrix3} matrix The matrix with signed elements.
  4335. * @param {Matrix3} result The object onto which to store the result.
  4336. * @returns {Matrix3} The modified result parameter.
  4337. */
  4338. Matrix3.abs = function(matrix, result) {
  4339. if (!defined(matrix)) {
  4340. throw new DeveloperError('matrix is required');
  4341. }
  4342. if (!defined(result)) {
  4343. throw new DeveloperError('result is required');
  4344. }
  4345. result[0] = Math.abs(matrix[0]);
  4346. result[1] = Math.abs(matrix[1]);
  4347. result[2] = Math.abs(matrix[2]);
  4348. result[3] = Math.abs(matrix[3]);
  4349. result[4] = Math.abs(matrix[4]);
  4350. result[5] = Math.abs(matrix[5]);
  4351. result[6] = Math.abs(matrix[6]);
  4352. result[7] = Math.abs(matrix[7]);
  4353. result[8] = Math.abs(matrix[8]);
  4354. return result;
  4355. };
  4356. /**
  4357. * Computes the determinant of the provided matrix.
  4358. *
  4359. * @param {Matrix3} matrix The matrix to use.
  4360. * @returns {Number} The value of the determinant of the matrix.
  4361. */
  4362. Matrix3.determinant = function(matrix) {
  4363. if (!defined(matrix)) {
  4364. throw new DeveloperError('matrix is required');
  4365. }
  4366. var m11 = matrix[0];
  4367. var m21 = matrix[3];
  4368. var m31 = matrix[6];
  4369. var m12 = matrix[1];
  4370. var m22 = matrix[4];
  4371. var m32 = matrix[7];
  4372. var m13 = matrix[2];
  4373. var m23 = matrix[5];
  4374. var m33 = matrix[8];
  4375. return m11 * (m22 * m33 - m23 * m32) + m12 * (m23 * m31 - m21 * m33) + m13 * (m21 * m32 - m22 * m31);
  4376. };
  4377. /**
  4378. * Computes the inverse of the provided matrix.
  4379. *
  4380. * @param {Matrix3} matrix The matrix to invert.
  4381. * @param {Matrix3} result The object onto which to store the result.
  4382. * @returns {Matrix3} The modified result parameter.
  4383. *
  4384. * @exception {DeveloperError} matrix is not invertible.
  4385. */
  4386. Matrix3.inverse = function(matrix, result) {
  4387. if (!defined(matrix)) {
  4388. throw new DeveloperError('matrix is required');
  4389. }
  4390. if (!defined(result)) {
  4391. throw new DeveloperError('result is required');
  4392. }
  4393. var m11 = matrix[0];
  4394. var m21 = matrix[1];
  4395. var m31 = matrix[2];
  4396. var m12 = matrix[3];
  4397. var m22 = matrix[4];
  4398. var m32 = matrix[5];
  4399. var m13 = matrix[6];
  4400. var m23 = matrix[7];
  4401. var m33 = matrix[8];
  4402. var determinant = Matrix3.determinant(matrix);
  4403. if (Math.abs(determinant) <= CesiumMath.EPSILON15) {
  4404. throw new DeveloperError('matrix is not invertible');
  4405. }
  4406. result[0] = m22 * m33 - m23 * m32;
  4407. result[1] = m23 * m31 - m21 * m33;
  4408. result[2] = m21 * m32 - m22 * m31;
  4409. result[3] = m13 * m32 - m12 * m33;
  4410. result[4] = m11 * m33 - m13 * m31;
  4411. result[5] = m12 * m31 - m11 * m32;
  4412. result[6] = m12 * m23 - m13 * m22;
  4413. result[7] = m13 * m21 - m11 * m23;
  4414. result[8] = m11 * m22 - m12 * m21;
  4415. var scale = 1.0 / determinant;
  4416. return Matrix3.multiplyByScalar(result, scale, result);
  4417. };
  4418. /**
  4419. * Compares the provided matrices componentwise and returns
  4420. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4421. *
  4422. * @param {Matrix3} [left] The first matrix.
  4423. * @param {Matrix3} [right] The second matrix.
  4424. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  4425. */
  4426. Matrix3.equals = function(left, right) {
  4427. return (left === right) ||
  4428. (defined(left) &&
  4429. defined(right) &&
  4430. left[0] === right[0] &&
  4431. left[1] === right[1] &&
  4432. left[2] === right[2] &&
  4433. left[3] === right[3] &&
  4434. left[4] === right[4] &&
  4435. left[5] === right[5] &&
  4436. left[6] === right[6] &&
  4437. left[7] === right[7] &&
  4438. left[8] === right[8]);
  4439. };
  4440. /**
  4441. * Compares the provided matrices componentwise and returns
  4442. * <code>true</code> if they are within the provided epsilon,
  4443. * <code>false</code> otherwise.
  4444. *
  4445. * @param {Matrix3} [left] The first matrix.
  4446. * @param {Matrix3} [right] The second matrix.
  4447. * @param {Number} epsilon The epsilon to use for equality testing.
  4448. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  4449. */
  4450. Matrix3.equalsEpsilon = function(left, right, epsilon) {
  4451. if (typeof epsilon !== 'number') {
  4452. throw new DeveloperError('epsilon must be a number');
  4453. }
  4454. return (left === right) ||
  4455. (defined(left) &&
  4456. defined(right) &&
  4457. Math.abs(left[0] - right[0]) <= epsilon &&
  4458. Math.abs(left[1] - right[1]) <= epsilon &&
  4459. Math.abs(left[2] - right[2]) <= epsilon &&
  4460. Math.abs(left[3] - right[3]) <= epsilon &&
  4461. Math.abs(left[4] - right[4]) <= epsilon &&
  4462. Math.abs(left[5] - right[5]) <= epsilon &&
  4463. Math.abs(left[6] - right[6]) <= epsilon &&
  4464. Math.abs(left[7] - right[7]) <= epsilon &&
  4465. Math.abs(left[8] - right[8]) <= epsilon);
  4466. };
  4467. /**
  4468. * An immutable Matrix3 instance initialized to the identity matrix.
  4469. *
  4470. * @type {Matrix3}
  4471. * @constant
  4472. */
  4473. Matrix3.IDENTITY = freezeObject(new Matrix3(1.0, 0.0, 0.0,
  4474. 0.0, 1.0, 0.0,
  4475. 0.0, 0.0, 1.0));
  4476. /**
  4477. * An immutable Matrix3 instance initialized to the zero matrix.
  4478. *
  4479. * @type {Matrix3}
  4480. * @constant
  4481. */
  4482. Matrix3.ZERO = freezeObject(new Matrix3(0.0, 0.0, 0.0,
  4483. 0.0, 0.0, 0.0,
  4484. 0.0, 0.0, 0.0));
  4485. /**
  4486. * The index into Matrix3 for column 0, row 0.
  4487. *
  4488. * @type {Number}
  4489. * @constant
  4490. */
  4491. Matrix3.COLUMN0ROW0 = 0;
  4492. /**
  4493. * The index into Matrix3 for column 0, row 1.
  4494. *
  4495. * @type {Number}
  4496. * @constant
  4497. */
  4498. Matrix3.COLUMN0ROW1 = 1;
  4499. /**
  4500. * The index into Matrix3 for column 0, row 2.
  4501. *
  4502. * @type {Number}
  4503. * @constant
  4504. */
  4505. Matrix3.COLUMN0ROW2 = 2;
  4506. /**
  4507. * The index into Matrix3 for column 1, row 0.
  4508. *
  4509. * @type {Number}
  4510. * @constant
  4511. */
  4512. Matrix3.COLUMN1ROW0 = 3;
  4513. /**
  4514. * The index into Matrix3 for column 1, row 1.
  4515. *
  4516. * @type {Number}
  4517. * @constant
  4518. */
  4519. Matrix3.COLUMN1ROW1 = 4;
  4520. /**
  4521. * The index into Matrix3 for column 1, row 2.
  4522. *
  4523. * @type {Number}
  4524. * @constant
  4525. */
  4526. Matrix3.COLUMN1ROW2 = 5;
  4527. /**
  4528. * The index into Matrix3 for column 2, row 0.
  4529. *
  4530. * @type {Number}
  4531. * @constant
  4532. */
  4533. Matrix3.COLUMN2ROW0 = 6;
  4534. /**
  4535. * The index into Matrix3 for column 2, row 1.
  4536. *
  4537. * @type {Number}
  4538. * @constant
  4539. */
  4540. Matrix3.COLUMN2ROW1 = 7;
  4541. /**
  4542. * The index into Matrix3 for column 2, row 2.
  4543. *
  4544. * @type {Number}
  4545. * @constant
  4546. */
  4547. Matrix3.COLUMN2ROW2 = 8;
  4548. defineProperties(Matrix3.prototype, {
  4549. /**
  4550. * Gets the number of items in the collection.
  4551. * @memberof Matrix3.prototype
  4552. *
  4553. * @type {Number}
  4554. */
  4555. length : {
  4556. get : function() {
  4557. return Matrix3.packedLength;
  4558. }
  4559. }
  4560. });
  4561. /**
  4562. * Duplicates the provided Matrix3 instance.
  4563. *
  4564. * @param {Matrix3} [result] The object onto which to store the result.
  4565. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  4566. */
  4567. Matrix3.prototype.clone = function(result) {
  4568. return Matrix3.clone(this, result);
  4569. };
  4570. /**
  4571. * Compares this matrix to the provided matrix componentwise and returns
  4572. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4573. *
  4574. * @param {Matrix3} [right] The right hand side matrix.
  4575. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  4576. */
  4577. Matrix3.prototype.equals = function(right) {
  4578. return Matrix3.equals(this, right);
  4579. };
  4580. /**
  4581. * @private
  4582. */
  4583. Matrix3.equalsArray = function(matrix, array, offset) {
  4584. return matrix[0] === array[offset] &&
  4585. matrix[1] === array[offset + 1] &&
  4586. matrix[2] === array[offset + 2] &&
  4587. matrix[3] === array[offset + 3] &&
  4588. matrix[4] === array[offset + 4] &&
  4589. matrix[5] === array[offset + 5] &&
  4590. matrix[6] === array[offset + 6] &&
  4591. matrix[7] === array[offset + 7] &&
  4592. matrix[8] === array[offset + 8];
  4593. };
  4594. /**
  4595. * Compares this matrix to the provided matrix componentwise and returns
  4596. * <code>true</code> if they are within the provided epsilon,
  4597. * <code>false</code> otherwise.
  4598. *
  4599. * @param {Matrix3} [right] The right hand side matrix.
  4600. * @param {Number} epsilon The epsilon to use for equality testing.
  4601. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  4602. */
  4603. Matrix3.prototype.equalsEpsilon = function(right, epsilon) {
  4604. return Matrix3.equalsEpsilon(this, right, epsilon);
  4605. };
  4606. /**
  4607. * Creates a string representing this Matrix with each row being
  4608. * on a separate line and in the format '(column0, column1, column2)'.
  4609. *
  4610. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  4611. */
  4612. Matrix3.prototype.toString = function() {
  4613. return '(' + this[0] + ', ' + this[3] + ', ' + this[6] + ')\n' +
  4614. '(' + this[1] + ', ' + this[4] + ', ' + this[7] + ')\n' +
  4615. '(' + this[2] + ', ' + this[5] + ', ' + this[8] + ')';
  4616. };
  4617. return Matrix3;
  4618. });
  4619. /*global define*/
  4620. define('Core/Cartesian4',[
  4621. './defaultValue',
  4622. './defined',
  4623. './DeveloperError',
  4624. './freezeObject',
  4625. './Math'
  4626. ], function(
  4627. defaultValue,
  4628. defined,
  4629. DeveloperError,
  4630. freezeObject,
  4631. CesiumMath) {
  4632. 'use strict';
  4633. /**
  4634. * A 4D Cartesian point.
  4635. * @alias Cartesian4
  4636. * @constructor
  4637. *
  4638. * @param {Number} [x=0.0] The X component.
  4639. * @param {Number} [y=0.0] The Y component.
  4640. * @param {Number} [z=0.0] The Z component.
  4641. * @param {Number} [w=0.0] The W component.
  4642. *
  4643. * @see Cartesian2
  4644. * @see Cartesian3
  4645. * @see Packable
  4646. */
  4647. function Cartesian4(x, y, z, w) {
  4648. /**
  4649. * The X component.
  4650. * @type {Number}
  4651. * @default 0.0
  4652. */
  4653. this.x = defaultValue(x, 0.0);
  4654. /**
  4655. * The Y component.
  4656. * @type {Number}
  4657. * @default 0.0
  4658. */
  4659. this.y = defaultValue(y, 0.0);
  4660. /**
  4661. * The Z component.
  4662. * @type {Number}
  4663. * @default 0.0
  4664. */
  4665. this.z = defaultValue(z, 0.0);
  4666. /**
  4667. * The W component.
  4668. * @type {Number}
  4669. * @default 0.0
  4670. */
  4671. this.w = defaultValue(w, 0.0);
  4672. }
  4673. /**
  4674. * Creates a Cartesian4 instance from x, y, z and w coordinates.
  4675. *
  4676. * @param {Number} x The x coordinate.
  4677. * @param {Number} y The y coordinate.
  4678. * @param {Number} z The z coordinate.
  4679. * @param {Number} w The w coordinate.
  4680. * @param {Cartesian4} [result] The object onto which to store the result.
  4681. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4682. */
  4683. Cartesian4.fromElements = function(x, y, z, w, result) {
  4684. if (!defined(result)) {
  4685. return new Cartesian4(x, y, z, w);
  4686. }
  4687. result.x = x;
  4688. result.y = y;
  4689. result.z = z;
  4690. result.w = w;
  4691. return result;
  4692. };
  4693. /**
  4694. * Creates a Cartesian4 instance from a {@link Color}. <code>red</code>, <code>green</code>, <code>blue</code>,
  4695. * and <code>alpha</code> map to <code>x</code>, <code>y</code>, <code>z</code>, and <code>w</code>, respectively.
  4696. *
  4697. * @param {Color} color The source color.
  4698. * @param {Cartesian4} [result] The object onto which to store the result.
  4699. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4700. */
  4701. Cartesian4.fromColor = function(color, result) {
  4702. if (!defined(color)) {
  4703. throw new DeveloperError('color is required');
  4704. }
  4705. if (!defined(result)) {
  4706. return new Cartesian4(color.red, color.green, color.blue, color.alpha);
  4707. }
  4708. result.x = color.red;
  4709. result.y = color.green;
  4710. result.z = color.blue;
  4711. result.w = color.alpha;
  4712. return result;
  4713. };
  4714. /**
  4715. * Duplicates a Cartesian4 instance.
  4716. *
  4717. * @param {Cartesian4} cartesian The Cartesian to duplicate.
  4718. * @param {Cartesian4} [result] The object onto which to store the result.
  4719. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided. (Returns undefined if cartesian is undefined)
  4720. */
  4721. Cartesian4.clone = function(cartesian, result) {
  4722. if (!defined(cartesian)) {
  4723. return undefined;
  4724. }
  4725. if (!defined(result)) {
  4726. return new Cartesian4(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4727. }
  4728. result.x = cartesian.x;
  4729. result.y = cartesian.y;
  4730. result.z = cartesian.z;
  4731. result.w = cartesian.w;
  4732. return result;
  4733. };
  4734. /**
  4735. * The number of elements used to pack the object into an array.
  4736. * @type {Number}
  4737. */
  4738. Cartesian4.packedLength = 4;
  4739. /**
  4740. * Stores the provided instance into the provided array.
  4741. *
  4742. * @param {Cartesian4} value The value to pack.
  4743. * @param {Number[]} array The array to pack into.
  4744. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4745. *
  4746. * @returns {Number[]} The array that was packed into
  4747. */
  4748. Cartesian4.pack = function(value, array, startingIndex) {
  4749. if (!defined(value)) {
  4750. throw new DeveloperError('value is required');
  4751. }
  4752. if (!defined(array)) {
  4753. throw new DeveloperError('array is required');
  4754. }
  4755. startingIndex = defaultValue(startingIndex, 0);
  4756. array[startingIndex++] = value.x;
  4757. array[startingIndex++] = value.y;
  4758. array[startingIndex++] = value.z;
  4759. array[startingIndex] = value.w;
  4760. return array;
  4761. };
  4762. /**
  4763. * Retrieves an instance from a packed array.
  4764. *
  4765. * @param {Number[]} array The packed array.
  4766. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4767. * @param {Cartesian4} [result] The object into which to store the result.
  4768. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4769. */
  4770. Cartesian4.unpack = function(array, startingIndex, result) {
  4771. if (!defined(array)) {
  4772. throw new DeveloperError('array is required');
  4773. }
  4774. startingIndex = defaultValue(startingIndex, 0);
  4775. if (!defined(result)) {
  4776. result = new Cartesian4();
  4777. }
  4778. result.x = array[startingIndex++];
  4779. result.y = array[startingIndex++];
  4780. result.z = array[startingIndex++];
  4781. result.w = array[startingIndex];
  4782. return result;
  4783. };
  4784. /**
  4785. * Flattens an array of Cartesian4s into and array of components.
  4786. *
  4787. * @param {Cartesian4[]} array The array of cartesians to pack.
  4788. * @param {Number[]} result The array onto which to store the result.
  4789. * @returns {Number[]} The packed array.
  4790. */
  4791. Cartesian4.packArray = function(array, result) {
  4792. if (!defined(array)) {
  4793. throw new DeveloperError('array is required');
  4794. }
  4795. var length = array.length;
  4796. if (!defined(result)) {
  4797. result = new Array(length * 4);
  4798. } else {
  4799. result.length = length * 4;
  4800. }
  4801. for (var i = 0; i < length; ++i) {
  4802. Cartesian4.pack(array[i], result, i * 4);
  4803. }
  4804. return result;
  4805. };
  4806. /**
  4807. * Unpacks an array of cartesian components into and array of Cartesian4s.
  4808. *
  4809. * @param {Number[]} array The array of components to unpack.
  4810. * @param {Cartesian4[]} result The array onto which to store the result.
  4811. * @returns {Cartesian4[]} The unpacked array.
  4812. */
  4813. Cartesian4.unpackArray = function(array, result) {
  4814. if (!defined(array)) {
  4815. throw new DeveloperError('array is required');
  4816. }
  4817. var length = array.length;
  4818. if (!defined(result)) {
  4819. result = new Array(length / 4);
  4820. } else {
  4821. result.length = length / 4;
  4822. }
  4823. for (var i = 0; i < length; i += 4) {
  4824. var index = i / 4;
  4825. result[index] = Cartesian4.unpack(array, i, result[index]);
  4826. }
  4827. return result;
  4828. };
  4829. /**
  4830. * Creates a Cartesian4 from four consecutive elements in an array.
  4831. * @function
  4832. *
  4833. * @param {Number[]} array The array whose four consecutive elements correspond to the x, y, z, and w components, respectively.
  4834. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  4835. * @param {Cartesian4} [result] The object onto which to store the result.
  4836. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  4837. *
  4838. * @example
  4839. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0)
  4840. * var v = [1.0, 2.0, 3.0, 4.0];
  4841. * var p = Cesium.Cartesian4.fromArray(v);
  4842. *
  4843. * // Create a Cartesian4 with (1.0, 2.0, 3.0, 4.0) using an offset into an array
  4844. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0];
  4845. * var p2 = Cesium.Cartesian4.fromArray(v2, 2);
  4846. */
  4847. Cartesian4.fromArray = Cartesian4.unpack;
  4848. /**
  4849. * Computes the value of the maximum component for the supplied Cartesian.
  4850. *
  4851. * @param {Cartesian4} cartesian The cartesian to use.
  4852. * @returns {Number} The value of the maximum component.
  4853. */
  4854. Cartesian4.maximumComponent = function(cartesian) {
  4855. if (!defined(cartesian)) {
  4856. throw new DeveloperError('cartesian is required');
  4857. }
  4858. return Math.max(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4859. };
  4860. /**
  4861. * Computes the value of the minimum component for the supplied Cartesian.
  4862. *
  4863. * @param {Cartesian4} cartesian The cartesian to use.
  4864. * @returns {Number} The value of the minimum component.
  4865. */
  4866. Cartesian4.minimumComponent = function(cartesian) {
  4867. if (!defined(cartesian)) {
  4868. throw new DeveloperError('cartesian is required');
  4869. }
  4870. return Math.min(cartesian.x, cartesian.y, cartesian.z, cartesian.w);
  4871. };
  4872. /**
  4873. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  4874. *
  4875. * @param {Cartesian4} first A cartesian to compare.
  4876. * @param {Cartesian4} second A cartesian to compare.
  4877. * @param {Cartesian4} result The object into which to store the result.
  4878. * @returns {Cartesian4} A cartesian with the minimum components.
  4879. */
  4880. Cartesian4.minimumByComponent = function(first, second, result) {
  4881. if (!defined(first)) {
  4882. throw new DeveloperError('first is required.');
  4883. }
  4884. if (!defined(second)) {
  4885. throw new DeveloperError('second is required.');
  4886. }
  4887. if (!defined(result)) {
  4888. throw new DeveloperError('result is required.');
  4889. }
  4890. result.x = Math.min(first.x, second.x);
  4891. result.y = Math.min(first.y, second.y);
  4892. result.z = Math.min(first.z, second.z);
  4893. result.w = Math.min(first.w, second.w);
  4894. return result;
  4895. };
  4896. /**
  4897. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  4898. *
  4899. * @param {Cartesian4} first A cartesian to compare.
  4900. * @param {Cartesian4} second A cartesian to compare.
  4901. * @param {Cartesian4} result The object into which to store the result.
  4902. * @returns {Cartesian4} A cartesian with the maximum components.
  4903. */
  4904. Cartesian4.maximumByComponent = function(first, second, result) {
  4905. if (!defined(first)) {
  4906. throw new DeveloperError('first is required.');
  4907. }
  4908. if (!defined(second)) {
  4909. throw new DeveloperError('second is required.');
  4910. }
  4911. if (!defined(result)) {
  4912. throw new DeveloperError('result is required.');
  4913. }
  4914. result.x = Math.max(first.x, second.x);
  4915. result.y = Math.max(first.y, second.y);
  4916. result.z = Math.max(first.z, second.z);
  4917. result.w = Math.max(first.w, second.w);
  4918. return result;
  4919. };
  4920. /**
  4921. * Computes the provided Cartesian's squared magnitude.
  4922. *
  4923. * @param {Cartesian4} cartesian The Cartesian instance whose squared magnitude is to be computed.
  4924. * @returns {Number} The squared magnitude.
  4925. */
  4926. Cartesian4.magnitudeSquared = function(cartesian) {
  4927. if (!defined(cartesian)) {
  4928. throw new DeveloperError('cartesian is required');
  4929. }
  4930. return cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z + cartesian.w * cartesian.w;
  4931. };
  4932. /**
  4933. * Computes the Cartesian's magnitude (length).
  4934. *
  4935. * @param {Cartesian4} cartesian The Cartesian instance whose magnitude is to be computed.
  4936. * @returns {Number} The magnitude.
  4937. */
  4938. Cartesian4.magnitude = function(cartesian) {
  4939. return Math.sqrt(Cartesian4.magnitudeSquared(cartesian));
  4940. };
  4941. var distanceScratch = new Cartesian4();
  4942. /**
  4943. * Computes the 4-space distance between two points.
  4944. *
  4945. * @param {Cartesian4} left The first point to compute the distance from.
  4946. * @param {Cartesian4} right The second point to compute the distance to.
  4947. * @returns {Number} The distance between two points.
  4948. *
  4949. * @example
  4950. * // Returns 1.0
  4951. * var d = Cesium.Cartesian4.distance(
  4952. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4953. * new Cesium.Cartesian4(2.0, 0.0, 0.0, 0.0));
  4954. */
  4955. Cartesian4.distance = function(left, right) {
  4956. if (!defined(left) || !defined(right)) {
  4957. throw new DeveloperError('left and right are required.');
  4958. }
  4959. Cartesian4.subtract(left, right, distanceScratch);
  4960. return Cartesian4.magnitude(distanceScratch);
  4961. };
  4962. /**
  4963. * Computes the squared distance between two points. Comparing squared distances
  4964. * using this function is more efficient than comparing distances using {@link Cartesian4#distance}.
  4965. *
  4966. * @param {Cartesian4} left The first point to compute the distance from.
  4967. * @param {Cartesian4} right The second point to compute the distance to.
  4968. * @returns {Number} The distance between two points.
  4969. *
  4970. * @example
  4971. * // Returns 4.0, not 2.0
  4972. * var d = Cesium.Cartesian4.distance(
  4973. * new Cesium.Cartesian4(1.0, 0.0, 0.0, 0.0),
  4974. * new Cesium.Cartesian4(3.0, 0.0, 0.0, 0.0));
  4975. */
  4976. Cartesian4.distanceSquared = function(left, right) {
  4977. if (!defined(left) || !defined(right)) {
  4978. throw new DeveloperError('left and right are required.');
  4979. }
  4980. Cartesian4.subtract(left, right, distanceScratch);
  4981. return Cartesian4.magnitudeSquared(distanceScratch);
  4982. };
  4983. /**
  4984. * Computes the normalized form of the supplied Cartesian.
  4985. *
  4986. * @param {Cartesian4} cartesian The Cartesian to be normalized.
  4987. * @param {Cartesian4} result The object onto which to store the result.
  4988. * @returns {Cartesian4} The modified result parameter.
  4989. */
  4990. Cartesian4.normalize = function(cartesian, result) {
  4991. if (!defined(cartesian)) {
  4992. throw new DeveloperError('cartesian is required');
  4993. }
  4994. if (!defined(result)) {
  4995. throw new DeveloperError('result is required');
  4996. }
  4997. var magnitude = Cartesian4.magnitude(cartesian);
  4998. result.x = cartesian.x / magnitude;
  4999. result.y = cartesian.y / magnitude;
  5000. result.z = cartesian.z / magnitude;
  5001. result.w = cartesian.w / magnitude;
  5002. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z) || isNaN(result.w)) {
  5003. throw new DeveloperError('normalized result is not a number');
  5004. }
  5005. return result;
  5006. };
  5007. /**
  5008. * Computes the dot (scalar) product of two Cartesians.
  5009. *
  5010. * @param {Cartesian4} left The first Cartesian.
  5011. * @param {Cartesian4} right The second Cartesian.
  5012. * @returns {Number} The dot product.
  5013. */
  5014. Cartesian4.dot = function(left, right) {
  5015. if (!defined(left)) {
  5016. throw new DeveloperError('left is required');
  5017. }
  5018. if (!defined(right)) {
  5019. throw new DeveloperError('right is required');
  5020. }
  5021. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  5022. };
  5023. /**
  5024. * Computes the componentwise product of two Cartesians.
  5025. *
  5026. * @param {Cartesian4} left The first Cartesian.
  5027. * @param {Cartesian4} right The second Cartesian.
  5028. * @param {Cartesian4} result The object onto which to store the result.
  5029. * @returns {Cartesian4} The modified result parameter.
  5030. */
  5031. Cartesian4.multiplyComponents = function(left, right, result) {
  5032. if (!defined(left)) {
  5033. throw new DeveloperError('left is required');
  5034. }
  5035. if (!defined(right)) {
  5036. throw new DeveloperError('right is required');
  5037. }
  5038. if (!defined(result)) {
  5039. throw new DeveloperError('result is required');
  5040. }
  5041. result.x = left.x * right.x;
  5042. result.y = left.y * right.y;
  5043. result.z = left.z * right.z;
  5044. result.w = left.w * right.w;
  5045. return result;
  5046. };
  5047. /**
  5048. * Computes the componentwise quotient of two Cartesians.
  5049. *
  5050. * @param {Cartesian4} left The first Cartesian.
  5051. * @param {Cartesian4} right The second Cartesian.
  5052. * @param {Cartesian4} result The object onto which to store the result.
  5053. * @returns {Cartesian4} The modified result parameter.
  5054. */
  5055. Cartesian4.divideComponents = function(left, right, result) {
  5056. if (!defined(left)) {
  5057. throw new DeveloperError('left is required');
  5058. }
  5059. if (!defined(right)) {
  5060. throw new DeveloperError('right is required');
  5061. }
  5062. if (!defined(result)) {
  5063. throw new DeveloperError('result is required');
  5064. }
  5065. result.x = left.x / right.x;
  5066. result.y = left.y / right.y;
  5067. result.z = left.z / right.z;
  5068. result.w = left.w / right.w;
  5069. return result;
  5070. };
  5071. /**
  5072. * Computes the componentwise sum of two Cartesians.
  5073. *
  5074. * @param {Cartesian4} left The first Cartesian.
  5075. * @param {Cartesian4} right The second Cartesian.
  5076. * @param {Cartesian4} result The object onto which to store the result.
  5077. * @returns {Cartesian4} The modified result parameter.
  5078. */
  5079. Cartesian4.add = function(left, right, result) {
  5080. if (!defined(left)) {
  5081. throw new DeveloperError('left is required');
  5082. }
  5083. if (!defined(right)) {
  5084. throw new DeveloperError('right is required');
  5085. }
  5086. if (!defined(result)) {
  5087. throw new DeveloperError('result is required');
  5088. }
  5089. result.x = left.x + right.x;
  5090. result.y = left.y + right.y;
  5091. result.z = left.z + right.z;
  5092. result.w = left.w + right.w;
  5093. return result;
  5094. };
  5095. /**
  5096. * Computes the componentwise difference of two Cartesians.
  5097. *
  5098. * @param {Cartesian4} left The first Cartesian.
  5099. * @param {Cartesian4} right The second Cartesian.
  5100. * @param {Cartesian4} result The object onto which to store the result.
  5101. * @returns {Cartesian4} The modified result parameter.
  5102. */
  5103. Cartesian4.subtract = function(left, right, result) {
  5104. if (!defined(left)) {
  5105. throw new DeveloperError('left is required');
  5106. }
  5107. if (!defined(right)) {
  5108. throw new DeveloperError('right is required');
  5109. }
  5110. if (!defined(result)) {
  5111. throw new DeveloperError('result is required');
  5112. }
  5113. result.x = left.x - right.x;
  5114. result.y = left.y - right.y;
  5115. result.z = left.z - right.z;
  5116. result.w = left.w - right.w;
  5117. return result;
  5118. };
  5119. /**
  5120. * Multiplies the provided Cartesian componentwise by the provided scalar.
  5121. *
  5122. * @param {Cartesian4} cartesian The Cartesian to be scaled.
  5123. * @param {Number} scalar The scalar to multiply with.
  5124. * @param {Cartesian4} result The object onto which to store the result.
  5125. * @returns {Cartesian4} The modified result parameter.
  5126. */
  5127. Cartesian4.multiplyByScalar = function(cartesian, scalar, result) {
  5128. if (!defined(cartesian)) {
  5129. throw new DeveloperError('cartesian is required');
  5130. }
  5131. if (typeof scalar !== 'number') {
  5132. throw new DeveloperError('scalar is required and must be a number.');
  5133. }
  5134. if (!defined(result)) {
  5135. throw new DeveloperError('result is required');
  5136. }
  5137. result.x = cartesian.x * scalar;
  5138. result.y = cartesian.y * scalar;
  5139. result.z = cartesian.z * scalar;
  5140. result.w = cartesian.w * scalar;
  5141. return result;
  5142. };
  5143. /**
  5144. * Divides the provided Cartesian componentwise by the provided scalar.
  5145. *
  5146. * @param {Cartesian4} cartesian The Cartesian to be divided.
  5147. * @param {Number} scalar The scalar to divide by.
  5148. * @param {Cartesian4} result The object onto which to store the result.
  5149. * @returns {Cartesian4} The modified result parameter.
  5150. */
  5151. Cartesian4.divideByScalar = function(cartesian, scalar, result) {
  5152. if (!defined(cartesian)) {
  5153. throw new DeveloperError('cartesian is required');
  5154. }
  5155. if (typeof scalar !== 'number') {
  5156. throw new DeveloperError('scalar is required and must be a number.');
  5157. }
  5158. if (!defined(result)) {
  5159. throw new DeveloperError('result is required');
  5160. }
  5161. result.x = cartesian.x / scalar;
  5162. result.y = cartesian.y / scalar;
  5163. result.z = cartesian.z / scalar;
  5164. result.w = cartesian.w / scalar;
  5165. return result;
  5166. };
  5167. /**
  5168. * Negates the provided Cartesian.
  5169. *
  5170. * @param {Cartesian4} cartesian The Cartesian to be negated.
  5171. * @param {Cartesian4} result The object onto which to store the result.
  5172. * @returns {Cartesian4} The modified result parameter.
  5173. */
  5174. Cartesian4.negate = function(cartesian, result) {
  5175. if (!defined(cartesian)) {
  5176. throw new DeveloperError('cartesian is required');
  5177. }
  5178. if (!defined(result)) {
  5179. throw new DeveloperError('result is required');
  5180. }
  5181. result.x = -cartesian.x;
  5182. result.y = -cartesian.y;
  5183. result.z = -cartesian.z;
  5184. result.w = -cartesian.w;
  5185. return result;
  5186. };
  5187. /**
  5188. * Computes the absolute value of the provided Cartesian.
  5189. *
  5190. * @param {Cartesian4} cartesian The Cartesian whose absolute value is to be computed.
  5191. * @param {Cartesian4} result The object onto which to store the result.
  5192. * @returns {Cartesian4} The modified result parameter.
  5193. */
  5194. Cartesian4.abs = function(cartesian, result) {
  5195. if (!defined(cartesian)) {
  5196. throw new DeveloperError('cartesian is required');
  5197. }
  5198. if (!defined(result)) {
  5199. throw new DeveloperError('result is required');
  5200. }
  5201. result.x = Math.abs(cartesian.x);
  5202. result.y = Math.abs(cartesian.y);
  5203. result.z = Math.abs(cartesian.z);
  5204. result.w = Math.abs(cartesian.w);
  5205. return result;
  5206. };
  5207. var lerpScratch = new Cartesian4();
  5208. /**
  5209. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  5210. *
  5211. * @param {Cartesian4} start The value corresponding to t at 0.0.
  5212. * @param {Cartesian4}end The value corresponding to t at 1.0.
  5213. * @param {Number} t The point along t at which to interpolate.
  5214. * @param {Cartesian4} result The object onto which to store the result.
  5215. * @returns {Cartesian4} The modified result parameter.
  5216. */
  5217. Cartesian4.lerp = function(start, end, t, result) {
  5218. if (!defined(start)) {
  5219. throw new DeveloperError('start is required.');
  5220. }
  5221. if (!defined(end)) {
  5222. throw new DeveloperError('end is required.');
  5223. }
  5224. if (typeof t !== 'number') {
  5225. throw new DeveloperError('t is required and must be a number.');
  5226. }
  5227. if (!defined(result)) {
  5228. throw new DeveloperError('result is required.');
  5229. }
  5230. Cartesian4.multiplyByScalar(end, t, lerpScratch);
  5231. result = Cartesian4.multiplyByScalar(start, 1.0 - t, result);
  5232. return Cartesian4.add(lerpScratch, result, result);
  5233. };
  5234. var mostOrthogonalAxisScratch = new Cartesian4();
  5235. /**
  5236. * Returns the axis that is most orthogonal to the provided Cartesian.
  5237. *
  5238. * @param {Cartesian4} cartesian The Cartesian on which to find the most orthogonal axis.
  5239. * @param {Cartesian4} result The object onto which to store the result.
  5240. * @returns {Cartesian4} The most orthogonal axis.
  5241. */
  5242. Cartesian4.mostOrthogonalAxis = function(cartesian, result) {
  5243. if (!defined(cartesian)) {
  5244. throw new DeveloperError('cartesian is required.');
  5245. }
  5246. if (!defined(result)) {
  5247. throw new DeveloperError('result is required.');
  5248. }
  5249. var f = Cartesian4.normalize(cartesian, mostOrthogonalAxisScratch);
  5250. Cartesian4.abs(f, f);
  5251. if (f.x <= f.y) {
  5252. if (f.x <= f.z) {
  5253. if (f.x <= f.w) {
  5254. result = Cartesian4.clone(Cartesian4.UNIT_X, result);
  5255. } else {
  5256. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  5257. }
  5258. } else if (f.z <= f.w) {
  5259. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  5260. } else {
  5261. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  5262. }
  5263. } else if (f.y <= f.z) {
  5264. if (f.y <= f.w) {
  5265. result = Cartesian4.clone(Cartesian4.UNIT_Y, result);
  5266. } else {
  5267. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  5268. }
  5269. } else if (f.z <= f.w) {
  5270. result = Cartesian4.clone(Cartesian4.UNIT_Z, result);
  5271. } else {
  5272. result = Cartesian4.clone(Cartesian4.UNIT_W, result);
  5273. }
  5274. return result;
  5275. };
  5276. /**
  5277. * Compares the provided Cartesians componentwise and returns
  5278. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5279. *
  5280. * @param {Cartesian4} [left] The first Cartesian.
  5281. * @param {Cartesian4} [right] The second Cartesian.
  5282. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  5283. */
  5284. Cartesian4.equals = function(left, right) {
  5285. return (left === right) ||
  5286. ((defined(left)) &&
  5287. (defined(right)) &&
  5288. (left.x === right.x) &&
  5289. (left.y === right.y) &&
  5290. (left.z === right.z) &&
  5291. (left.w === right.w));
  5292. };
  5293. /**
  5294. * @private
  5295. */
  5296. Cartesian4.equalsArray = function(cartesian, array, offset) {
  5297. return cartesian.x === array[offset] &&
  5298. cartesian.y === array[offset + 1] &&
  5299. cartesian.z === array[offset + 2] &&
  5300. cartesian.w === array[offset + 3];
  5301. };
  5302. /**
  5303. * Compares the provided Cartesians componentwise and returns
  5304. * <code>true</code> if they pass an absolute or relative tolerance test,
  5305. * <code>false</code> otherwise.
  5306. *
  5307. * @param {Cartesian4} [left] The first Cartesian.
  5308. * @param {Cartesian4} [right] The second Cartesian.
  5309. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  5310. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  5311. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  5312. */
  5313. Cartesian4.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  5314. return (left === right) ||
  5315. (defined(left) &&
  5316. defined(right) &&
  5317. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  5318. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon) &&
  5319. CesiumMath.equalsEpsilon(left.z, right.z, relativeEpsilon, absoluteEpsilon) &&
  5320. CesiumMath.equalsEpsilon(left.w, right.w, relativeEpsilon, absoluteEpsilon));
  5321. };
  5322. /**
  5323. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 0.0).
  5324. *
  5325. * @type {Cartesian4}
  5326. * @constant
  5327. */
  5328. Cartesian4.ZERO = freezeObject(new Cartesian4(0.0, 0.0, 0.0, 0.0));
  5329. /**
  5330. * An immutable Cartesian4 instance initialized to (1.0, 0.0, 0.0, 0.0).
  5331. *
  5332. * @type {Cartesian4}
  5333. * @constant
  5334. */
  5335. Cartesian4.UNIT_X = freezeObject(new Cartesian4(1.0, 0.0, 0.0, 0.0));
  5336. /**
  5337. * An immutable Cartesian4 instance initialized to (0.0, 1.0, 0.0, 0.0).
  5338. *
  5339. * @type {Cartesian4}
  5340. * @constant
  5341. */
  5342. Cartesian4.UNIT_Y = freezeObject(new Cartesian4(0.0, 1.0, 0.0, 0.0));
  5343. /**
  5344. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 1.0, 0.0).
  5345. *
  5346. * @type {Cartesian4}
  5347. * @constant
  5348. */
  5349. Cartesian4.UNIT_Z = freezeObject(new Cartesian4(0.0, 0.0, 1.0, 0.0));
  5350. /**
  5351. * An immutable Cartesian4 instance initialized to (0.0, 0.0, 0.0, 1.0).
  5352. *
  5353. * @type {Cartesian4}
  5354. * @constant
  5355. */
  5356. Cartesian4.UNIT_W = freezeObject(new Cartesian4(0.0, 0.0, 0.0, 1.0));
  5357. /**
  5358. * Duplicates this Cartesian4 instance.
  5359. *
  5360. * @param {Cartesian4} [result] The object onto which to store the result.
  5361. * @returns {Cartesian4} The modified result parameter or a new Cartesian4 instance if one was not provided.
  5362. */
  5363. Cartesian4.prototype.clone = function(result) {
  5364. return Cartesian4.clone(this, result);
  5365. };
  5366. /**
  5367. * Compares this Cartesian against the provided Cartesian componentwise and returns
  5368. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5369. *
  5370. * @param {Cartesian4} [right] The right hand side Cartesian.
  5371. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  5372. */
  5373. Cartesian4.prototype.equals = function(right) {
  5374. return Cartesian4.equals(this, right);
  5375. };
  5376. /**
  5377. * Compares this Cartesian against the provided Cartesian componentwise and returns
  5378. * <code>true</code> if they pass an absolute or relative tolerance test,
  5379. * <code>false</code> otherwise.
  5380. *
  5381. * @param {Cartesian4} [right] The right hand side Cartesian.
  5382. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  5383. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  5384. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  5385. */
  5386. Cartesian4.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  5387. return Cartesian4.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  5388. };
  5389. /**
  5390. * Creates a string representing this Cartesian in the format '(x, y)'.
  5391. *
  5392. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  5393. */
  5394. Cartesian4.prototype.toString = function() {
  5395. return '(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  5396. };
  5397. return Cartesian4;
  5398. });
  5399. /*global define*/
  5400. define('Core/RuntimeError',[
  5401. './defined'
  5402. ], function(
  5403. defined) {
  5404. 'use strict';
  5405. /**
  5406. * Constructs an exception object that is thrown due to an error that can occur at runtime, e.g.,
  5407. * out of memory, could not compile shader, etc. If a function may throw this
  5408. * exception, the calling code should be prepared to catch it.
  5409. * <br /><br />
  5410. * On the other hand, a {@link DeveloperError} indicates an exception due
  5411. * to a developer error, e.g., invalid argument, that usually indicates a bug in the
  5412. * calling code.
  5413. *
  5414. * @alias RuntimeError
  5415. * @constructor
  5416. * @extends Error
  5417. *
  5418. * @param {String} [message] The error message for this exception.
  5419. *
  5420. * @see DeveloperError
  5421. */
  5422. function RuntimeError(message) {
  5423. /**
  5424. * 'RuntimeError' indicating that this exception was thrown due to a runtime error.
  5425. * @type {String}
  5426. * @readonly
  5427. */
  5428. this.name = 'RuntimeError';
  5429. /**
  5430. * The explanation for why this exception was thrown.
  5431. * @type {String}
  5432. * @readonly
  5433. */
  5434. this.message = message;
  5435. //Browsers such as IE don't have a stack property until you actually throw the error.
  5436. var stack;
  5437. try {
  5438. throw new Error();
  5439. } catch (e) {
  5440. stack = e.stack;
  5441. }
  5442. /**
  5443. * The stack trace of this exception, if available.
  5444. * @type {String}
  5445. * @readonly
  5446. */
  5447. this.stack = stack;
  5448. }
  5449. if (defined(Object.create)) {
  5450. RuntimeError.prototype = Object.create(Error.prototype);
  5451. RuntimeError.prototype.constructor = RuntimeError;
  5452. }
  5453. RuntimeError.prototype.toString = function() {
  5454. var str = this.name + ': ' + this.message;
  5455. if (defined(this.stack)) {
  5456. str += '\n' + this.stack.toString();
  5457. }
  5458. return str;
  5459. };
  5460. return RuntimeError;
  5461. });
  5462. /*global define*/
  5463. define('Core/Matrix4',[
  5464. './Cartesian3',
  5465. './Cartesian4',
  5466. './defaultValue',
  5467. './defined',
  5468. './defineProperties',
  5469. './DeveloperError',
  5470. './freezeObject',
  5471. './Math',
  5472. './Matrix3',
  5473. './RuntimeError'
  5474. ], function(
  5475. Cartesian3,
  5476. Cartesian4,
  5477. defaultValue,
  5478. defined,
  5479. defineProperties,
  5480. DeveloperError,
  5481. freezeObject,
  5482. CesiumMath,
  5483. Matrix3,
  5484. RuntimeError) {
  5485. 'use strict';
  5486. /**
  5487. * A 4x4 matrix, indexable as a column-major order array.
  5488. * Constructor parameters are in row-major order for code readability.
  5489. * @alias Matrix4
  5490. * @constructor
  5491. *
  5492. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  5493. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  5494. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  5495. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  5496. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  5497. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  5498. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  5499. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  5500. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  5501. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  5502. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  5503. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  5504. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  5505. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  5506. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  5507. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  5508. *
  5509. * @see Matrix4.fromColumnMajorArray
  5510. * @see Matrix4.fromRowMajorArray
  5511. * @see Matrix4.fromRotationTranslation
  5512. * @see Matrix4.fromTranslationRotationScale
  5513. * @see Matrix4.fromTranslationQuaternionRotationScale
  5514. * @see Matrix4.fromTranslation
  5515. * @see Matrix4.fromScale
  5516. * @see Matrix4.fromUniformScale
  5517. * @see Matrix4.fromCamera
  5518. * @see Matrix4.computePerspectiveFieldOfView
  5519. * @see Matrix4.computeOrthographicOffCenter
  5520. * @see Matrix4.computePerspectiveOffCenter
  5521. * @see Matrix4.computeInfinitePerspectiveOffCenter
  5522. * @see Matrix4.computeViewportTransformation
  5523. * @see Matrix4.computeView
  5524. * @see Matrix2
  5525. * @see Matrix3
  5526. * @see Packable
  5527. */
  5528. function Matrix4(column0Row0, column1Row0, column2Row0, column3Row0,
  5529. column0Row1, column1Row1, column2Row1, column3Row1,
  5530. column0Row2, column1Row2, column2Row2, column3Row2,
  5531. column0Row3, column1Row3, column2Row3, column3Row3) {
  5532. this[0] = defaultValue(column0Row0, 0.0);
  5533. this[1] = defaultValue(column0Row1, 0.0);
  5534. this[2] = defaultValue(column0Row2, 0.0);
  5535. this[3] = defaultValue(column0Row3, 0.0);
  5536. this[4] = defaultValue(column1Row0, 0.0);
  5537. this[5] = defaultValue(column1Row1, 0.0);
  5538. this[6] = defaultValue(column1Row2, 0.0);
  5539. this[7] = defaultValue(column1Row3, 0.0);
  5540. this[8] = defaultValue(column2Row0, 0.0);
  5541. this[9] = defaultValue(column2Row1, 0.0);
  5542. this[10] = defaultValue(column2Row2, 0.0);
  5543. this[11] = defaultValue(column2Row3, 0.0);
  5544. this[12] = defaultValue(column3Row0, 0.0);
  5545. this[13] = defaultValue(column3Row1, 0.0);
  5546. this[14] = defaultValue(column3Row2, 0.0);
  5547. this[15] = defaultValue(column3Row3, 0.0);
  5548. }
  5549. /**
  5550. * The number of elements used to pack the object into an array.
  5551. * @type {Number}
  5552. */
  5553. Matrix4.packedLength = 16;
  5554. /**
  5555. * Stores the provided instance into the provided array.
  5556. *
  5557. * @param {Matrix4} value The value to pack.
  5558. * @param {Number[]} array The array to pack into.
  5559. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  5560. *
  5561. * @returns {Number[]} The array that was packed into
  5562. */
  5563. Matrix4.pack = function(value, array, startingIndex) {
  5564. if (!defined(value)) {
  5565. throw new DeveloperError('value is required');
  5566. }
  5567. if (!defined(array)) {
  5568. throw new DeveloperError('array is required');
  5569. }
  5570. startingIndex = defaultValue(startingIndex, 0);
  5571. array[startingIndex++] = value[0];
  5572. array[startingIndex++] = value[1];
  5573. array[startingIndex++] = value[2];
  5574. array[startingIndex++] = value[3];
  5575. array[startingIndex++] = value[4];
  5576. array[startingIndex++] = value[5];
  5577. array[startingIndex++] = value[6];
  5578. array[startingIndex++] = value[7];
  5579. array[startingIndex++] = value[8];
  5580. array[startingIndex++] = value[9];
  5581. array[startingIndex++] = value[10];
  5582. array[startingIndex++] = value[11];
  5583. array[startingIndex++] = value[12];
  5584. array[startingIndex++] = value[13];
  5585. array[startingIndex++] = value[14];
  5586. array[startingIndex] = value[15];
  5587. return array;
  5588. };
  5589. /**
  5590. * Retrieves an instance from a packed array.
  5591. *
  5592. * @param {Number[]} array The packed array.
  5593. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  5594. * @param {Matrix4} [result] The object into which to store the result.
  5595. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  5596. */
  5597. Matrix4.unpack = function(array, startingIndex, result) {
  5598. if (!defined(array)) {
  5599. throw new DeveloperError('array is required');
  5600. }
  5601. startingIndex = defaultValue(startingIndex, 0);
  5602. if (!defined(result)) {
  5603. result = new Matrix4();
  5604. }
  5605. result[0] = array[startingIndex++];
  5606. result[1] = array[startingIndex++];
  5607. result[2] = array[startingIndex++];
  5608. result[3] = array[startingIndex++];
  5609. result[4] = array[startingIndex++];
  5610. result[5] = array[startingIndex++];
  5611. result[6] = array[startingIndex++];
  5612. result[7] = array[startingIndex++];
  5613. result[8] = array[startingIndex++];
  5614. result[9] = array[startingIndex++];
  5615. result[10] = array[startingIndex++];
  5616. result[11] = array[startingIndex++];
  5617. result[12] = array[startingIndex++];
  5618. result[13] = array[startingIndex++];
  5619. result[14] = array[startingIndex++];
  5620. result[15] = array[startingIndex];
  5621. return result;
  5622. };
  5623. /**
  5624. * Duplicates a Matrix4 instance.
  5625. *
  5626. * @param {Matrix4} matrix The matrix to duplicate.
  5627. * @param {Matrix4} [result] The object onto which to store the result.
  5628. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  5629. */
  5630. Matrix4.clone = function(matrix, result) {
  5631. if (!defined(matrix)) {
  5632. return undefined;
  5633. }
  5634. if (!defined(result)) {
  5635. return new Matrix4(matrix[0], matrix[4], matrix[8], matrix[12],
  5636. matrix[1], matrix[5], matrix[9], matrix[13],
  5637. matrix[2], matrix[6], matrix[10], matrix[14],
  5638. matrix[3], matrix[7], matrix[11], matrix[15]);
  5639. }
  5640. result[0] = matrix[0];
  5641. result[1] = matrix[1];
  5642. result[2] = matrix[2];
  5643. result[3] = matrix[3];
  5644. result[4] = matrix[4];
  5645. result[5] = matrix[5];
  5646. result[6] = matrix[6];
  5647. result[7] = matrix[7];
  5648. result[8] = matrix[8];
  5649. result[9] = matrix[9];
  5650. result[10] = matrix[10];
  5651. result[11] = matrix[11];
  5652. result[12] = matrix[12];
  5653. result[13] = matrix[13];
  5654. result[14] = matrix[14];
  5655. result[15] = matrix[15];
  5656. return result;
  5657. };
  5658. /**
  5659. * Creates a Matrix4 from 16 consecutive elements in an array.
  5660. * @function
  5661. *
  5662. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  5663. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  5664. * @param {Matrix4} [result] The object onto which to store the result.
  5665. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  5666. *
  5667. * @example
  5668. * // Create the Matrix4:
  5669. * // [1.0, 2.0, 3.0, 4.0]
  5670. * // [1.0, 2.0, 3.0, 4.0]
  5671. * // [1.0, 2.0, 3.0, 4.0]
  5672. * // [1.0, 2.0, 3.0, 4.0]
  5673. *
  5674. * 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];
  5675. * var m = Cesium.Matrix4.fromArray(v);
  5676. *
  5677. * // Create same Matrix4 with using an offset into an array
  5678. * 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];
  5679. * var m2 = Cesium.Matrix4.fromArray(v2, 2);
  5680. */
  5681. Matrix4.fromArray = Matrix4.unpack;
  5682. /**
  5683. * Computes a Matrix4 instance from a column-major order array.
  5684. *
  5685. * @param {Number[]} values The column-major order array.
  5686. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5687. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5688. */
  5689. Matrix4.fromColumnMajorArray = function(values, result) {
  5690. if (!defined(values)) {
  5691. throw new DeveloperError('values is required');
  5692. }
  5693. return Matrix4.clone(values, result);
  5694. };
  5695. /**
  5696. * Computes a Matrix4 instance from a row-major order array.
  5697. * The resulting matrix will be in column-major order.
  5698. *
  5699. * @param {Number[]} values The row-major order array.
  5700. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5701. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5702. */
  5703. Matrix4.fromRowMajorArray = function(values, result) {
  5704. if (!defined(values)) {
  5705. throw new DeveloperError('values is required.');
  5706. }
  5707. if (!defined(result)) {
  5708. return new Matrix4(values[0], values[1], values[2], values[3],
  5709. values[4], values[5], values[6], values[7],
  5710. values[8], values[9], values[10], values[11],
  5711. values[12], values[13], values[14], values[15]);
  5712. }
  5713. result[0] = values[0];
  5714. result[1] = values[4];
  5715. result[2] = values[8];
  5716. result[3] = values[12];
  5717. result[4] = values[1];
  5718. result[5] = values[5];
  5719. result[6] = values[9];
  5720. result[7] = values[13];
  5721. result[8] = values[2];
  5722. result[9] = values[6];
  5723. result[10] = values[10];
  5724. result[11] = values[14];
  5725. result[12] = values[3];
  5726. result[13] = values[7];
  5727. result[14] = values[11];
  5728. result[15] = values[15];
  5729. return result;
  5730. };
  5731. /**
  5732. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  5733. * and a Cartesian3 representing the translation.
  5734. *
  5735. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  5736. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  5737. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5738. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5739. */
  5740. Matrix4.fromRotationTranslation = function(rotation, translation, result) {
  5741. if (!defined(rotation)) {
  5742. throw new DeveloperError('rotation is required.');
  5743. }
  5744. translation = defaultValue(translation, Cartesian3.ZERO);
  5745. if (!defined(result)) {
  5746. return new Matrix4(rotation[0], rotation[3], rotation[6], translation.x,
  5747. rotation[1], rotation[4], rotation[7], translation.y,
  5748. rotation[2], rotation[5], rotation[8], translation.z,
  5749. 0.0, 0.0, 0.0, 1.0);
  5750. }
  5751. result[0] = rotation[0];
  5752. result[1] = rotation[1];
  5753. result[2] = rotation[2];
  5754. result[3] = 0.0;
  5755. result[4] = rotation[3];
  5756. result[5] = rotation[4];
  5757. result[6] = rotation[5];
  5758. result[7] = 0.0;
  5759. result[8] = rotation[6];
  5760. result[9] = rotation[7];
  5761. result[10] = rotation[8];
  5762. result[11] = 0.0;
  5763. result[12] = translation.x;
  5764. result[13] = translation.y;
  5765. result[14] = translation.z;
  5766. result[15] = 1.0;
  5767. return result;
  5768. };
  5769. /**
  5770. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  5771. * representation with the rotation represented as a quaternion.
  5772. *
  5773. * @param {Cartesian3} translation The translation transformation.
  5774. * @param {Quaternion} rotation The rotation transformation.
  5775. * @param {Cartesian3} scale The non-uniform scale transformation.
  5776. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5777. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5778. *
  5779. * @example
  5780. * var result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  5781. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  5782. * Cesium.Quaternion.IDENTITY, // rotation
  5783. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  5784. * result);
  5785. */
  5786. Matrix4.fromTranslationQuaternionRotationScale = function(translation, rotation, scale, result) {
  5787. if (!defined(translation)) {
  5788. throw new DeveloperError('translation is required.');
  5789. }
  5790. if (!defined(rotation)) {
  5791. throw new DeveloperError('rotation is required.');
  5792. }
  5793. if (!defined(scale)) {
  5794. throw new DeveloperError('scale is required.');
  5795. }
  5796. if (!defined(result)) {
  5797. result = new Matrix4();
  5798. }
  5799. var scaleX = scale.x;
  5800. var scaleY = scale.y;
  5801. var scaleZ = scale.z;
  5802. var x2 = rotation.x * rotation.x;
  5803. var xy = rotation.x * rotation.y;
  5804. var xz = rotation.x * rotation.z;
  5805. var xw = rotation.x * rotation.w;
  5806. var y2 = rotation.y * rotation.y;
  5807. var yz = rotation.y * rotation.z;
  5808. var yw = rotation.y * rotation.w;
  5809. var z2 = rotation.z * rotation.z;
  5810. var zw = rotation.z * rotation.w;
  5811. var w2 = rotation.w * rotation.w;
  5812. var m00 = x2 - y2 - z2 + w2;
  5813. var m01 = 2.0 * (xy - zw);
  5814. var m02 = 2.0 * (xz + yw);
  5815. var m10 = 2.0 * (xy + zw);
  5816. var m11 = -x2 + y2 - z2 + w2;
  5817. var m12 = 2.0 * (yz - xw);
  5818. var m20 = 2.0 * (xz - yw);
  5819. var m21 = 2.0 * (yz + xw);
  5820. var m22 = -x2 - y2 + z2 + w2;
  5821. result[0] = m00 * scaleX;
  5822. result[1] = m10 * scaleX;
  5823. result[2] = m20 * scaleX;
  5824. result[3] = 0.0;
  5825. result[4] = m01 * scaleY;
  5826. result[5] = m11 * scaleY;
  5827. result[6] = m21 * scaleY;
  5828. result[7] = 0.0;
  5829. result[8] = m02 * scaleZ;
  5830. result[9] = m12 * scaleZ;
  5831. result[10] = m22 * scaleZ;
  5832. result[11] = 0.0;
  5833. result[12] = translation.x;
  5834. result[13] = translation.y;
  5835. result[14] = translation.z;
  5836. result[15] = 1.0;
  5837. return result;
  5838. };
  5839. /**
  5840. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  5841. *
  5842. * @param {TranslationRotationScale} translationRotationScale The instance.
  5843. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5844. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5845. */
  5846. Matrix4.fromTranslationRotationScale = function(translationRotationScale, result) {
  5847. if (!defined(translationRotationScale)) {
  5848. throw new DeveloperError('translationRotationScale is required.');
  5849. }
  5850. return Matrix4.fromTranslationQuaternionRotationScale(translationRotationScale.translation, translationRotationScale.rotation, translationRotationScale.scale, result);
  5851. };
  5852. /**
  5853. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  5854. *
  5855. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  5856. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5857. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5858. *
  5859. * @see Matrix4.multiplyByTranslation
  5860. */
  5861. Matrix4.fromTranslation = function(translation, result) {
  5862. if (!defined(translation)) {
  5863. throw new DeveloperError('translation is required.');
  5864. }
  5865. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  5866. };
  5867. /**
  5868. * Computes a Matrix4 instance representing a non-uniform scale.
  5869. *
  5870. * @param {Cartesian3} scale The x, y, and z scale factors.
  5871. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5872. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5873. *
  5874. * @example
  5875. * // Creates
  5876. * // [7.0, 0.0, 0.0, 0.0]
  5877. * // [0.0, 8.0, 0.0, 0.0]
  5878. * // [0.0, 0.0, 9.0, 0.0]
  5879. * // [0.0, 0.0, 0.0, 1.0]
  5880. * var m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  5881. */
  5882. Matrix4.fromScale = function(scale, result) {
  5883. if (!defined(scale)) {
  5884. throw new DeveloperError('scale is required.');
  5885. }
  5886. if (!defined(result)) {
  5887. return new Matrix4(
  5888. scale.x, 0.0, 0.0, 0.0,
  5889. 0.0, scale.y, 0.0, 0.0,
  5890. 0.0, 0.0, scale.z, 0.0,
  5891. 0.0, 0.0, 0.0, 1.0);
  5892. }
  5893. result[0] = scale.x;
  5894. result[1] = 0.0;
  5895. result[2] = 0.0;
  5896. result[3] = 0.0;
  5897. result[4] = 0.0;
  5898. result[5] = scale.y;
  5899. result[6] = 0.0;
  5900. result[7] = 0.0;
  5901. result[8] = 0.0;
  5902. result[9] = 0.0;
  5903. result[10] = scale.z;
  5904. result[11] = 0.0;
  5905. result[12] = 0.0;
  5906. result[13] = 0.0;
  5907. result[14] = 0.0;
  5908. result[15] = 1.0;
  5909. return result;
  5910. };
  5911. /**
  5912. * Computes a Matrix4 instance representing a uniform scale.
  5913. *
  5914. * @param {Number} scale The uniform scale factor.
  5915. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5916. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5917. *
  5918. * @example
  5919. * // Creates
  5920. * // [2.0, 0.0, 0.0, 0.0]
  5921. * // [0.0, 2.0, 0.0, 0.0]
  5922. * // [0.0, 0.0, 2.0, 0.0]
  5923. * // [0.0, 0.0, 0.0, 1.0]
  5924. * var m = Cesium.Matrix4.fromUniformScale(2.0);
  5925. */
  5926. Matrix4.fromUniformScale = function(scale, result) {
  5927. if (typeof scale !== 'number') {
  5928. throw new DeveloperError('scale is required.');
  5929. }
  5930. if (!defined(result)) {
  5931. return new Matrix4(scale, 0.0, 0.0, 0.0,
  5932. 0.0, scale, 0.0, 0.0,
  5933. 0.0, 0.0, scale, 0.0,
  5934. 0.0, 0.0, 0.0, 1.0);
  5935. }
  5936. result[0] = scale;
  5937. result[1] = 0.0;
  5938. result[2] = 0.0;
  5939. result[3] = 0.0;
  5940. result[4] = 0.0;
  5941. result[5] = scale;
  5942. result[6] = 0.0;
  5943. result[7] = 0.0;
  5944. result[8] = 0.0;
  5945. result[9] = 0.0;
  5946. result[10] = scale;
  5947. result[11] = 0.0;
  5948. result[12] = 0.0;
  5949. result[13] = 0.0;
  5950. result[14] = 0.0;
  5951. result[15] = 1.0;
  5952. return result;
  5953. };
  5954. var fromCameraF = new Cartesian3();
  5955. var fromCameraR = new Cartesian3();
  5956. var fromCameraU = new Cartesian3();
  5957. /**
  5958. * Computes a Matrix4 instance from a Camera.
  5959. *
  5960. * @param {Camera} camera The camera to use.
  5961. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  5962. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  5963. */
  5964. Matrix4.fromCamera = function(camera, result) {
  5965. if (!defined(camera)) {
  5966. throw new DeveloperError('camera is required.');
  5967. }
  5968. var position = camera.position;
  5969. var direction = camera.direction;
  5970. var up = camera.up;
  5971. if (!defined(position)) {
  5972. throw new DeveloperError('camera.position is required.');
  5973. }
  5974. if (!defined(direction)) {
  5975. throw new DeveloperError('camera.direction is required.');
  5976. }
  5977. if (!defined(up)) {
  5978. throw new DeveloperError('camera.up is required.');
  5979. }
  5980. Cartesian3.normalize(direction, fromCameraF);
  5981. Cartesian3.normalize(Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR);
  5982. Cartesian3.normalize(Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU);
  5983. var sX = fromCameraR.x;
  5984. var sY = fromCameraR.y;
  5985. var sZ = fromCameraR.z;
  5986. var fX = fromCameraF.x;
  5987. var fY = fromCameraF.y;
  5988. var fZ = fromCameraF.z;
  5989. var uX = fromCameraU.x;
  5990. var uY = fromCameraU.y;
  5991. var uZ = fromCameraU.z;
  5992. var positionX = position.x;
  5993. var positionY = position.y;
  5994. var positionZ = position.z;
  5995. var t0 = sX * -positionX + sY * -positionY+ sZ * -positionZ;
  5996. var t1 = uX * -positionX + uY * -positionY+ uZ * -positionZ;
  5997. var t2 = fX * positionX + fY * positionY + fZ * positionZ;
  5998. // The code below this comment is an optimized
  5999. // version of the commented lines.
  6000. // Rather that create two matrices and then multiply,
  6001. // we just bake in the multiplcation as part of creation.
  6002. // var rotation = new Matrix4(
  6003. // sX, sY, sZ, 0.0,
  6004. // uX, uY, uZ, 0.0,
  6005. // -fX, -fY, -fZ, 0.0,
  6006. // 0.0, 0.0, 0.0, 1.0);
  6007. // var translation = new Matrix4(
  6008. // 1.0, 0.0, 0.0, -position.x,
  6009. // 0.0, 1.0, 0.0, -position.y,
  6010. // 0.0, 0.0, 1.0, -position.z,
  6011. // 0.0, 0.0, 0.0, 1.0);
  6012. // return rotation.multiply(translation);
  6013. if (!defined(result)) {
  6014. return new Matrix4(
  6015. sX, sY, sZ, t0,
  6016. uX, uY, uZ, t1,
  6017. -fX, -fY, -fZ, t2,
  6018. 0.0, 0.0, 0.0, 1.0);
  6019. }
  6020. result[0] = sX;
  6021. result[1] = uX;
  6022. result[2] = -fX;
  6023. result[3] = 0.0;
  6024. result[4] = sY;
  6025. result[5] = uY;
  6026. result[6] = -fY;
  6027. result[7] = 0.0;
  6028. result[8] = sZ;
  6029. result[9] = uZ;
  6030. result[10] = -fZ;
  6031. result[11] = 0.0;
  6032. result[12] = t0;
  6033. result[13] = t1;
  6034. result[14] = t2;
  6035. result[15] = 1.0;
  6036. return result;
  6037. };
  6038. /**
  6039. * Computes a Matrix4 instance representing a perspective transformation matrix.
  6040. *
  6041. * @param {Number} fovY The field of view along the Y axis in radians.
  6042. * @param {Number} aspectRatio The aspect ratio.
  6043. * @param {Number} near The distance to the near plane in meters.
  6044. * @param {Number} far The distance to the far plane in meters.
  6045. * @param {Matrix4} result The object in which the result will be stored.
  6046. * @returns {Matrix4} The modified result parameter.
  6047. *
  6048. * @exception {DeveloperError} fovY must be in (0, PI].
  6049. * @exception {DeveloperError} aspectRatio must be greater than zero.
  6050. * @exception {DeveloperError} near must be greater than zero.
  6051. * @exception {DeveloperError} far must be greater than zero.
  6052. */
  6053. Matrix4.computePerspectiveFieldOfView = function(fovY, aspectRatio, near, far, result) {
  6054. if (fovY <= 0.0 || fovY > Math.PI) {
  6055. throw new DeveloperError('fovY must be in (0, PI].');
  6056. }
  6057. if (aspectRatio <= 0.0) {
  6058. throw new DeveloperError('aspectRatio must be greater than zero.');
  6059. }
  6060. if (near <= 0.0) {
  6061. throw new DeveloperError('near must be greater than zero.');
  6062. }
  6063. if (far <= 0.0) {
  6064. throw new DeveloperError('far must be greater than zero.');
  6065. }
  6066. if (!defined(result)) {
  6067. throw new DeveloperError('result is required');
  6068. }
  6069. var bottom = Math.tan(fovY * 0.5);
  6070. var column1Row1 = 1.0 / bottom;
  6071. var column0Row0 = column1Row1 / aspectRatio;
  6072. var column2Row2 = (far + near) / (near - far);
  6073. var column3Row2 = (2.0 * far * near) / (near - far);
  6074. result[0] = column0Row0;
  6075. result[1] = 0.0;
  6076. result[2] = 0.0;
  6077. result[3] = 0.0;
  6078. result[4] = 0.0;
  6079. result[5] = column1Row1;
  6080. result[6] = 0.0;
  6081. result[7] = 0.0;
  6082. result[8] = 0.0;
  6083. result[9] = 0.0;
  6084. result[10] = column2Row2;
  6085. result[11] = -1.0;
  6086. result[12] = 0.0;
  6087. result[13] = 0.0;
  6088. result[14] = column3Row2;
  6089. result[15] = 0.0;
  6090. return result;
  6091. };
  6092. /**
  6093. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  6094. *
  6095. * @param {Number} left The number of meters to the left of the camera that will be in view.
  6096. * @param {Number} right The number of meters to the right of the camera that will be in view.
  6097. * @param {Number} bottom The number of meters below of the camera that will be in view.
  6098. * @param {Number} top The number of meters above of the camera that will be in view.
  6099. * @param {Number} near The distance to the near plane in meters.
  6100. * @param {Number} far The distance to the far plane in meters.
  6101. * @param {Matrix4} result The object in which the result will be stored.
  6102. * @returns {Matrix4} The modified result parameter.
  6103. */
  6104. Matrix4.computeOrthographicOffCenter = function(left, right, bottom, top, near, far, result) {
  6105. if (!defined(left)) {
  6106. throw new DeveloperError('left is required.');
  6107. }
  6108. if (!defined(right)) {
  6109. throw new DeveloperError('right is required.');
  6110. }
  6111. if (!defined(bottom)) {
  6112. throw new DeveloperError('bottom is required.');
  6113. }
  6114. if (!defined(top)) {
  6115. throw new DeveloperError('top is required.');
  6116. }
  6117. if (!defined(near)) {
  6118. throw new DeveloperError('near is required.');
  6119. }
  6120. if (!defined(far)) {
  6121. throw new DeveloperError('far is required.');
  6122. }
  6123. if (!defined(result)) {
  6124. throw new DeveloperError('result is required');
  6125. }
  6126. var a = 1.0 / (right - left);
  6127. var b = 1.0 / (top - bottom);
  6128. var c = 1.0 / (far - near);
  6129. var tx = -(right + left) * a;
  6130. var ty = -(top + bottom) * b;
  6131. var tz = -(far + near) * c;
  6132. a *= 2.0;
  6133. b *= 2.0;
  6134. c *= -2.0;
  6135. result[0] = a;
  6136. result[1] = 0.0;
  6137. result[2] = 0.0;
  6138. result[3] = 0.0;
  6139. result[4] = 0.0;
  6140. result[5] = b;
  6141. result[6] = 0.0;
  6142. result[7] = 0.0;
  6143. result[8] = 0.0;
  6144. result[9] = 0.0;
  6145. result[10] = c;
  6146. result[11] = 0.0;
  6147. result[12] = tx;
  6148. result[13] = ty;
  6149. result[14] = tz;
  6150. result[15] = 1.0;
  6151. return result;
  6152. };
  6153. /**
  6154. * Computes a Matrix4 instance representing an off center perspective transformation.
  6155. *
  6156. * @param {Number} left The number of meters to the left of the camera that will be in view.
  6157. * @param {Number} right The number of meters to the right of the camera that will be in view.
  6158. * @param {Number} bottom The number of meters below of the camera that will be in view.
  6159. * @param {Number} top The number of meters above of the camera that will be in view.
  6160. * @param {Number} near The distance to the near plane in meters.
  6161. * @param {Number} far The distance to the far plane in meters.
  6162. * @param {Matrix4} result The object in which the result will be stored.
  6163. * @returns {Matrix4} The modified result parameter.
  6164. */
  6165. Matrix4.computePerspectiveOffCenter = function(left, right, bottom, top, near, far, result) {
  6166. if (!defined(left)) {
  6167. throw new DeveloperError('left is required.');
  6168. }
  6169. if (!defined(right)) {
  6170. throw new DeveloperError('right is required.');
  6171. }
  6172. if (!defined(bottom)) {
  6173. throw new DeveloperError('bottom is required.');
  6174. }
  6175. if (!defined(top)) {
  6176. throw new DeveloperError('top is required.');
  6177. }
  6178. if (!defined(near)) {
  6179. throw new DeveloperError('near is required.');
  6180. }
  6181. if (!defined(far)) {
  6182. throw new DeveloperError('far is required.');
  6183. }
  6184. if (!defined(result)) {
  6185. throw new DeveloperError('result is required');
  6186. }
  6187. var column0Row0 = 2.0 * near / (right - left);
  6188. var column1Row1 = 2.0 * near / (top - bottom);
  6189. var column2Row0 = (right + left) / (right - left);
  6190. var column2Row1 = (top + bottom) / (top - bottom);
  6191. var column2Row2 = -(far + near) / (far - near);
  6192. var column2Row3 = -1.0;
  6193. var column3Row2 = -2.0 * far * near / (far - near);
  6194. result[0] = column0Row0;
  6195. result[1] = 0.0;
  6196. result[2] = 0.0;
  6197. result[3] = 0.0;
  6198. result[4] = 0.0;
  6199. result[5] = column1Row1;
  6200. result[6] = 0.0;
  6201. result[7] = 0.0;
  6202. result[8] = column2Row0;
  6203. result[9] = column2Row1;
  6204. result[10] = column2Row2;
  6205. result[11] = column2Row3;
  6206. result[12] = 0.0;
  6207. result[13] = 0.0;
  6208. result[14] = column3Row2;
  6209. result[15] = 0.0;
  6210. return result;
  6211. };
  6212. /**
  6213. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  6214. *
  6215. * @param {Number} left The number of meters to the left of the camera that will be in view.
  6216. * @param {Number} right The number of meters to the right of the camera that will be in view.
  6217. * @param {Number} bottom The number of meters below of the camera that will be in view.
  6218. * @param {Number} top The number of meters above of the camera that will be in view.
  6219. * @param {Number} near The distance to the near plane in meters.
  6220. * @param {Matrix4} result The object in which the result will be stored.
  6221. * @returns {Matrix4} The modified result parameter.
  6222. */
  6223. Matrix4.computeInfinitePerspectiveOffCenter = function(left, right, bottom, top, near, result) {
  6224. if (!defined(left)) {
  6225. throw new DeveloperError('left is required.');
  6226. }
  6227. if (!defined(right)) {
  6228. throw new DeveloperError('right is required.');
  6229. }
  6230. if (!defined(bottom)) {
  6231. throw new DeveloperError('bottom is required.');
  6232. }
  6233. if (!defined(top)) {
  6234. throw new DeveloperError('top is required.');
  6235. }
  6236. if (!defined(near)) {
  6237. throw new DeveloperError('near is required.');
  6238. }
  6239. if (!defined(result)) {
  6240. throw new DeveloperError('result is required');
  6241. }
  6242. var column0Row0 = 2.0 * near / (right - left);
  6243. var column1Row1 = 2.0 * near / (top - bottom);
  6244. var column2Row0 = (right + left) / (right - left);
  6245. var column2Row1 = (top + bottom) / (top - bottom);
  6246. var column2Row2 = -1.0;
  6247. var column2Row3 = -1.0;
  6248. var column3Row2 = -2.0 * near;
  6249. result[0] = column0Row0;
  6250. result[1] = 0.0;
  6251. result[2] = 0.0;
  6252. result[3] = 0.0;
  6253. result[4] = 0.0;
  6254. result[5] = column1Row1;
  6255. result[6] = 0.0;
  6256. result[7] = 0.0;
  6257. result[8] = column2Row0;
  6258. result[9] = column2Row1;
  6259. result[10] = column2Row2;
  6260. result[11] = column2Row3;
  6261. result[12] = 0.0;
  6262. result[13] = 0.0;
  6263. result[14] = column3Row2;
  6264. result[15] = 0.0;
  6265. return result;
  6266. };
  6267. /**
  6268. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  6269. *
  6270. * @param {Object}[viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  6271. * @param {Number}[nearDepthRange=0.0] The near plane distance in window coordinates.
  6272. * @param {Number}[farDepthRange=1.0] The far plane distance in window coordinates.
  6273. * @param {Matrix4} result The object in which the result will be stored.
  6274. * @returns {Matrix4} The modified result parameter.
  6275. *
  6276. * @example
  6277. * // Create viewport transformation using an explicit viewport and depth range.
  6278. * var m = Cesium.Matrix4.computeViewportTransformation({
  6279. * x : 0.0,
  6280. * y : 0.0,
  6281. * width : 1024.0,
  6282. * height : 768.0
  6283. * }, 0.0, 1.0, new Cesium.Matrix4());
  6284. */
  6285. Matrix4.computeViewportTransformation = function(viewport, nearDepthRange, farDepthRange, result) {
  6286. if (!defined(result)) {
  6287. throw new DeveloperError('result is required');
  6288. }
  6289. viewport = defaultValue(viewport, defaultValue.EMPTY_OBJECT);
  6290. var x = defaultValue(viewport.x, 0.0);
  6291. var y = defaultValue(viewport.y, 0.0);
  6292. var width = defaultValue(viewport.width, 0.0);
  6293. var height = defaultValue(viewport.height, 0.0);
  6294. nearDepthRange = defaultValue(nearDepthRange, 0.0);
  6295. farDepthRange = defaultValue(farDepthRange, 1.0);
  6296. var halfWidth = width * 0.5;
  6297. var halfHeight = height * 0.5;
  6298. var halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  6299. var column0Row0 = halfWidth;
  6300. var column1Row1 = halfHeight;
  6301. var column2Row2 = halfDepth;
  6302. var column3Row0 = x + halfWidth;
  6303. var column3Row1 = y + halfHeight;
  6304. var column3Row2 = nearDepthRange + halfDepth;
  6305. var column3Row3 = 1.0;
  6306. result[0] = column0Row0;
  6307. result[1] = 0.0;
  6308. result[2] = 0.0;
  6309. result[3] = 0.0;
  6310. result[4] = 0.0;
  6311. result[5] = column1Row1;
  6312. result[6] = 0.0;
  6313. result[7] = 0.0;
  6314. result[8] = 0.0;
  6315. result[9] = 0.0;
  6316. result[10] = column2Row2;
  6317. result[11] = 0.0;
  6318. result[12] = column3Row0;
  6319. result[13] = column3Row1;
  6320. result[14] = column3Row2;
  6321. result[15] = column3Row3;
  6322. return result;
  6323. };
  6324. /**
  6325. * Computes a Matrix4 instance that transforms from world space to view space.
  6326. *
  6327. * @param {Cartesian3} position The position of the camera.
  6328. * @param {Cartesian3} direction The forward direction.
  6329. * @param {Cartesian3} up The up direction.
  6330. * @param {Cartesian3} right The right direction.
  6331. * @param {Matrix4} result The object in which the result will be stored.
  6332. * @returns {Matrix4} The modified result parameter.
  6333. */
  6334. Matrix4.computeView = function(position, direction, up, right, result) {
  6335. if (!defined(position)) {
  6336. throw new DeveloperError('position is required');
  6337. }
  6338. if (!defined(direction)) {
  6339. throw new DeveloperError('direction is required');
  6340. }
  6341. if (!defined(up)) {
  6342. throw new DeveloperError('up is required');
  6343. }
  6344. if (!defined(right)) {
  6345. throw new DeveloperError('right is required');
  6346. }
  6347. if (!defined(result)) {
  6348. throw new DeveloperError('result is required');
  6349. }
  6350. result[0] = right.x;
  6351. result[1] = up.x;
  6352. result[2] = -direction.x;
  6353. result[3] = 0.0;
  6354. result[4] = right.y;
  6355. result[5] = up.y;
  6356. result[6] = -direction.y;
  6357. result[7] = 0.0;
  6358. result[8] = right.z;
  6359. result[9] = up.z;
  6360. result[10] = -direction.z;
  6361. result[11] = 0.0;
  6362. result[12] = -Cartesian3.dot(right, position);
  6363. result[13] = -Cartesian3.dot(up, position);
  6364. result[14] = Cartesian3.dot(direction, position);
  6365. result[15] = 1.0;
  6366. return result;
  6367. };
  6368. /**
  6369. * Computes an Array from the provided Matrix4 instance.
  6370. * The array will be in column-major order.
  6371. *
  6372. * @param {Matrix4} matrix The matrix to use..
  6373. * @param {Number[]} [result] The Array onto which to store the result.
  6374. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  6375. *
  6376. * @example
  6377. * //create an array from an instance of Matrix4
  6378. * // m = [10.0, 14.0, 18.0, 22.0]
  6379. * // [11.0, 15.0, 19.0, 23.0]
  6380. * // [12.0, 16.0, 20.0, 24.0]
  6381. * // [13.0, 17.0, 21.0, 25.0]
  6382. * var a = Cesium.Matrix4.toArray(m);
  6383. *
  6384. * // m remains the same
  6385. * //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]
  6386. */
  6387. Matrix4.toArray = function(matrix, result) {
  6388. if (!defined(matrix)) {
  6389. throw new DeveloperError('matrix is required');
  6390. }
  6391. if (!defined(result)) {
  6392. return [matrix[0], matrix[1], matrix[2], matrix[3],
  6393. matrix[4], matrix[5], matrix[6], matrix[7],
  6394. matrix[8], matrix[9], matrix[10], matrix[11],
  6395. matrix[12], matrix[13], matrix[14], matrix[15]];
  6396. }
  6397. result[0] = matrix[0];
  6398. result[1] = matrix[1];
  6399. result[2] = matrix[2];
  6400. result[3] = matrix[3];
  6401. result[4] = matrix[4];
  6402. result[5] = matrix[5];
  6403. result[6] = matrix[6];
  6404. result[7] = matrix[7];
  6405. result[8] = matrix[8];
  6406. result[9] = matrix[9];
  6407. result[10] = matrix[10];
  6408. result[11] = matrix[11];
  6409. result[12] = matrix[12];
  6410. result[13] = matrix[13];
  6411. result[14] = matrix[14];
  6412. result[15] = matrix[15];
  6413. return result;
  6414. };
  6415. /**
  6416. * Computes the array index of the element at the provided row and column.
  6417. *
  6418. * @param {Number} row The zero-based index of the row.
  6419. * @param {Number} column The zero-based index of the column.
  6420. * @returns {Number} The index of the element at the provided row and column.
  6421. *
  6422. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  6423. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  6424. *
  6425. * @example
  6426. * var myMatrix = new Cesium.Matrix4();
  6427. * var column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  6428. * var column1Row0 = myMatrix[column1Row0Index];
  6429. * myMatrix[column1Row0Index] = 10.0;
  6430. */
  6431. Matrix4.getElementIndex = function(column, row) {
  6432. if (typeof row !== 'number' || row < 0 || row > 3) {
  6433. throw new DeveloperError('row must be 0, 1, 2, or 3.');
  6434. }
  6435. if (typeof column !== 'number' || column < 0 || column > 3) {
  6436. throw new DeveloperError('column must be 0, 1, 2, or 3.');
  6437. }
  6438. return column * 4 + row;
  6439. };
  6440. /**
  6441. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  6442. *
  6443. * @param {Matrix4} matrix The matrix to use.
  6444. * @param {Number} index The zero-based index of the column to retrieve.
  6445. * @param {Cartesian4} result The object onto which to store the result.
  6446. * @returns {Cartesian4} The modified result parameter.
  6447. *
  6448. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6449. *
  6450. * @example
  6451. * //returns a Cartesian4 instance with values from the specified column
  6452. * // m = [10.0, 11.0, 12.0, 13.0]
  6453. * // [14.0, 15.0, 16.0, 17.0]
  6454. * // [18.0, 19.0, 20.0, 21.0]
  6455. * // [22.0, 23.0, 24.0, 25.0]
  6456. *
  6457. * //Example 1: Creates an instance of Cartesian
  6458. * var a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  6459. *
  6460. * @example
  6461. * //Example 2: Sets values for Cartesian instance
  6462. * var a = new Cesium.Cartesian4();
  6463. * Cesium.Matrix4.getColumn(m, 2, a);
  6464. *
  6465. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  6466. */
  6467. Matrix4.getColumn = function(matrix, index, result) {
  6468. if (!defined(matrix)) {
  6469. throw new DeveloperError('matrix is required.');
  6470. }
  6471. if (typeof index !== 'number' || index < 0 || index > 3) {
  6472. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  6473. }
  6474. if (!defined(result)) {
  6475. throw new DeveloperError('result is required');
  6476. }
  6477. var startIndex = index * 4;
  6478. var x = matrix[startIndex];
  6479. var y = matrix[startIndex + 1];
  6480. var z = matrix[startIndex + 2];
  6481. var w = matrix[startIndex + 3];
  6482. result.x = x;
  6483. result.y = y;
  6484. result.z = z;
  6485. result.w = w;
  6486. return result;
  6487. };
  6488. /**
  6489. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  6490. *
  6491. * @param {Matrix4} matrix The matrix to use.
  6492. * @param {Number} index The zero-based index of the column to set.
  6493. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  6494. * @param {Matrix4} result The object onto which to store the result.
  6495. * @returns {Matrix4} The modified result parameter.
  6496. *
  6497. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6498. *
  6499. * @example
  6500. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  6501. * // m = [10.0, 11.0, 12.0, 13.0]
  6502. * // [14.0, 15.0, 16.0, 17.0]
  6503. * // [18.0, 19.0, 20.0, 21.0]
  6504. * // [22.0, 23.0, 24.0, 25.0]
  6505. *
  6506. * var a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  6507. *
  6508. * // m remains the same
  6509. * // a = [10.0, 11.0, 99.0, 13.0]
  6510. * // [14.0, 15.0, 98.0, 17.0]
  6511. * // [18.0, 19.0, 97.0, 21.0]
  6512. * // [22.0, 23.0, 96.0, 25.0]
  6513. */
  6514. Matrix4.setColumn = function(matrix, index, cartesian, result) {
  6515. if (!defined(matrix)) {
  6516. throw new DeveloperError('matrix is required');
  6517. }
  6518. if (!defined(cartesian)) {
  6519. throw new DeveloperError('cartesian is required');
  6520. }
  6521. if (typeof index !== 'number' || index < 0 || index > 3) {
  6522. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  6523. }
  6524. if (!defined(result)) {
  6525. throw new DeveloperError('result is required');
  6526. }
  6527. result = Matrix4.clone(matrix, result);
  6528. var startIndex = index * 4;
  6529. result[startIndex] = cartesian.x;
  6530. result[startIndex + 1] = cartesian.y;
  6531. result[startIndex + 2] = cartesian.z;
  6532. result[startIndex + 3] = cartesian.w;
  6533. return result;
  6534. };
  6535. /**
  6536. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  6537. * matrix with the provided translation. This assumes the matrix is an affine transformation
  6538. *
  6539. * @param {Matrix4} matrix The matrix to use.
  6540. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  6541. * @param {Cartesian4} result The object onto which to store the result.
  6542. * @returns {Matrix4} The modified result parameter.
  6543. */
  6544. Matrix4.setTranslation = function(matrix, translation, result) {
  6545. if (!defined(matrix)) {
  6546. throw new DeveloperError('matrix is required');
  6547. }
  6548. if (!defined(translation)) {
  6549. throw new DeveloperError('translation is required');
  6550. }
  6551. if (!defined(result)) {
  6552. throw new DeveloperError('result is required');
  6553. }
  6554. result[0] = matrix[0];
  6555. result[1] = matrix[1];
  6556. result[2] = matrix[2];
  6557. result[3] = matrix[3];
  6558. result[4] = matrix[4];
  6559. result[5] = matrix[5];
  6560. result[6] = matrix[6];
  6561. result[7] = matrix[7];
  6562. result[8] = matrix[8];
  6563. result[9] = matrix[9];
  6564. result[10] = matrix[10];
  6565. result[11] = matrix[11];
  6566. result[12] = translation.x;
  6567. result[13] = translation.y;
  6568. result[14] = translation.z;
  6569. result[15] = matrix[15];
  6570. return result;
  6571. };
  6572. /**
  6573. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  6574. *
  6575. * @param {Matrix4} matrix The matrix to use.
  6576. * @param {Number} index The zero-based index of the row to retrieve.
  6577. * @param {Cartesian4} result The object onto which to store the result.
  6578. * @returns {Cartesian4} The modified result parameter.
  6579. *
  6580. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6581. *
  6582. * @example
  6583. * //returns a Cartesian4 instance with values from the specified column
  6584. * // m = [10.0, 11.0, 12.0, 13.0]
  6585. * // [14.0, 15.0, 16.0, 17.0]
  6586. * // [18.0, 19.0, 20.0, 21.0]
  6587. * // [22.0, 23.0, 24.0, 25.0]
  6588. *
  6589. * //Example 1: Returns an instance of Cartesian
  6590. * var a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  6591. *
  6592. * @example
  6593. * //Example 2: Sets values for a Cartesian instance
  6594. * var a = new Cesium.Cartesian4();
  6595. * Cesium.Matrix4.getRow(m, 2, a);
  6596. *
  6597. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  6598. */
  6599. Matrix4.getRow = function(matrix, index, result) {
  6600. if (!defined(matrix)) {
  6601. throw new DeveloperError('matrix is required.');
  6602. }
  6603. if (typeof index !== 'number' || index < 0 || index > 3) {
  6604. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  6605. }
  6606. if (!defined(result)) {
  6607. throw new DeveloperError('result is required');
  6608. }
  6609. var x = matrix[index];
  6610. var y = matrix[index + 4];
  6611. var z = matrix[index + 8];
  6612. var w = matrix[index + 12];
  6613. result.x = x;
  6614. result.y = y;
  6615. result.z = z;
  6616. result.w = w;
  6617. return result;
  6618. };
  6619. /**
  6620. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  6621. *
  6622. * @param {Matrix4} matrix The matrix to use.
  6623. * @param {Number} index The zero-based index of the row to set.
  6624. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  6625. * @param {Matrix4} result The object onto which to store the result.
  6626. * @returns {Matrix4} The modified result parameter.
  6627. *
  6628. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  6629. *
  6630. * @example
  6631. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  6632. * // m = [10.0, 11.0, 12.0, 13.0]
  6633. * // [14.0, 15.0, 16.0, 17.0]
  6634. * // [18.0, 19.0, 20.0, 21.0]
  6635. * // [22.0, 23.0, 24.0, 25.0]
  6636. *
  6637. * var a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  6638. *
  6639. * // m remains the same
  6640. * // a = [10.0, 11.0, 12.0, 13.0]
  6641. * // [14.0, 15.0, 16.0, 17.0]
  6642. * // [99.0, 98.0, 97.0, 96.0]
  6643. * // [22.0, 23.0, 24.0, 25.0]
  6644. */
  6645. Matrix4.setRow = function(matrix, index, cartesian, result) {
  6646. if (!defined(matrix)) {
  6647. throw new DeveloperError('matrix is required');
  6648. }
  6649. if (!defined(cartesian)) {
  6650. throw new DeveloperError('cartesian is required');
  6651. }
  6652. if (typeof index !== 'number' || index < 0 || index > 3) {
  6653. throw new DeveloperError('index must be 0, 1, 2, or 3.');
  6654. }
  6655. if (!defined(result)) {
  6656. throw new DeveloperError('result is required');
  6657. }
  6658. result = Matrix4.clone(matrix, result);
  6659. result[index] = cartesian.x;
  6660. result[index + 4] = cartesian.y;
  6661. result[index + 8] = cartesian.z;
  6662. result[index + 12] = cartesian.w;
  6663. return result;
  6664. };
  6665. var scratchColumn = new Cartesian3();
  6666. /**
  6667. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  6668. *
  6669. * @param {Matrix4} matrix The matrix.
  6670. * @param {Cartesian3} result The object onto which to store the result.
  6671. * @returns {Cartesian3} The modified result parameter
  6672. */
  6673. Matrix4.getScale = function(matrix, result) {
  6674. if (!defined(matrix)) {
  6675. throw new DeveloperError('matrix is required.');
  6676. }
  6677. if (!defined(result)) {
  6678. throw new DeveloperError('result is required');
  6679. }
  6680. result.x = Cartesian3.magnitude(Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  6681. result.y = Cartesian3.magnitude(Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn));
  6682. result.z = Cartesian3.magnitude(Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn));
  6683. return result;
  6684. };
  6685. var scratchScale = new Cartesian3();
  6686. /**
  6687. * Computes the maximum scale assuming the matrix is an affine transformation.
  6688. * The maximum scale is the maximum length of the column vectors in the upper-left
  6689. * 3x3 matrix.
  6690. *
  6691. * @param {Matrix4} matrix The matrix.
  6692. * @returns {Number} The maximum scale.
  6693. */
  6694. Matrix4.getMaximumScale = function(matrix) {
  6695. Matrix4.getScale(matrix, scratchScale);
  6696. return Cartesian3.maximumComponent(scratchScale);
  6697. };
  6698. /**
  6699. * Computes the product of two matrices.
  6700. *
  6701. * @param {Matrix4} left The first matrix.
  6702. * @param {Matrix4} right The second matrix.
  6703. * @param {Matrix4} result The object onto which to store the result.
  6704. * @returns {Matrix4} The modified result parameter.
  6705. */
  6706. Matrix4.multiply = function(left, right, result) {
  6707. if (!defined(left)) {
  6708. throw new DeveloperError('left is required');
  6709. }
  6710. if (!defined(right)) {
  6711. throw new DeveloperError('right is required');
  6712. }
  6713. if (!defined(result)) {
  6714. throw new DeveloperError('result is required');
  6715. }
  6716. var left0 = left[0];
  6717. var left1 = left[1];
  6718. var left2 = left[2];
  6719. var left3 = left[3];
  6720. var left4 = left[4];
  6721. var left5 = left[5];
  6722. var left6 = left[6];
  6723. var left7 = left[7];
  6724. var left8 = left[8];
  6725. var left9 = left[9];
  6726. var left10 = left[10];
  6727. var left11 = left[11];
  6728. var left12 = left[12];
  6729. var left13 = left[13];
  6730. var left14 = left[14];
  6731. var left15 = left[15];
  6732. var right0 = right[0];
  6733. var right1 = right[1];
  6734. var right2 = right[2];
  6735. var right3 = right[3];
  6736. var right4 = right[4];
  6737. var right5 = right[5];
  6738. var right6 = right[6];
  6739. var right7 = right[7];
  6740. var right8 = right[8];
  6741. var right9 = right[9];
  6742. var right10 = right[10];
  6743. var right11 = right[11];
  6744. var right12 = right[12];
  6745. var right13 = right[13];
  6746. var right14 = right[14];
  6747. var right15 = right[15];
  6748. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  6749. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  6750. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  6751. var column0Row3 = left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  6752. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  6753. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  6754. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  6755. var column1Row3 = left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  6756. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  6757. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  6758. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  6759. var column2Row3 = left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  6760. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  6761. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  6762. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  6763. var column3Row3 = left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  6764. result[0] = column0Row0;
  6765. result[1] = column0Row1;
  6766. result[2] = column0Row2;
  6767. result[3] = column0Row3;
  6768. result[4] = column1Row0;
  6769. result[5] = column1Row1;
  6770. result[6] = column1Row2;
  6771. result[7] = column1Row3;
  6772. result[8] = column2Row0;
  6773. result[9] = column2Row1;
  6774. result[10] = column2Row2;
  6775. result[11] = column2Row3;
  6776. result[12] = column3Row0;
  6777. result[13] = column3Row1;
  6778. result[14] = column3Row2;
  6779. result[15] = column3Row3;
  6780. return result;
  6781. };
  6782. /**
  6783. * Computes the sum of two matrices.
  6784. *
  6785. * @param {Matrix4} left The first matrix.
  6786. * @param {Matrix4} right The second matrix.
  6787. * @param {Matrix4} result The object onto which to store the result.
  6788. * @returns {Matrix4} The modified result parameter.
  6789. */
  6790. Matrix4.add = function(left, right, result) {
  6791. if (!defined(left)) {
  6792. throw new DeveloperError('left is required');
  6793. }
  6794. if (!defined(right)) {
  6795. throw new DeveloperError('right is required');
  6796. }
  6797. if (!defined(result)) {
  6798. throw new DeveloperError('result is required');
  6799. }
  6800. result[0] = left[0] + right[0];
  6801. result[1] = left[1] + right[1];
  6802. result[2] = left[2] + right[2];
  6803. result[3] = left[3] + right[3];
  6804. result[4] = left[4] + right[4];
  6805. result[5] = left[5] + right[5];
  6806. result[6] = left[6] + right[6];
  6807. result[7] = left[7] + right[7];
  6808. result[8] = left[8] + right[8];
  6809. result[9] = left[9] + right[9];
  6810. result[10] = left[10] + right[10];
  6811. result[11] = left[11] + right[11];
  6812. result[12] = left[12] + right[12];
  6813. result[13] = left[13] + right[13];
  6814. result[14] = left[14] + right[14];
  6815. result[15] = left[15] + right[15];
  6816. return result;
  6817. };
  6818. /**
  6819. * Computes the difference of two matrices.
  6820. *
  6821. * @param {Matrix4} left The first matrix.
  6822. * @param {Matrix4} right The second matrix.
  6823. * @param {Matrix4} result The object onto which to store the result.
  6824. * @returns {Matrix4} The modified result parameter.
  6825. */
  6826. Matrix4.subtract = function(left, right, result) {
  6827. if (!defined(left)) {
  6828. throw new DeveloperError('left is required');
  6829. }
  6830. if (!defined(right)) {
  6831. throw new DeveloperError('right is required');
  6832. }
  6833. if (!defined(result)) {
  6834. throw new DeveloperError('result is required');
  6835. }
  6836. result[0] = left[0] - right[0];
  6837. result[1] = left[1] - right[1];
  6838. result[2] = left[2] - right[2];
  6839. result[3] = left[3] - right[3];
  6840. result[4] = left[4] - right[4];
  6841. result[5] = left[5] - right[5];
  6842. result[6] = left[6] - right[6];
  6843. result[7] = left[7] - right[7];
  6844. result[8] = left[8] - right[8];
  6845. result[9] = left[9] - right[9];
  6846. result[10] = left[10] - right[10];
  6847. result[11] = left[11] - right[11];
  6848. result[12] = left[12] - right[12];
  6849. result[13] = left[13] - right[13];
  6850. result[14] = left[14] - right[14];
  6851. result[15] = left[15] - right[15];
  6852. return result;
  6853. };
  6854. /**
  6855. * Computes the product of two matrices assuming the matrices are
  6856. * affine transformation matrices, where the upper left 3x3 elements
  6857. * are a rotation matrix, and the upper three elements in the fourth
  6858. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  6859. * The matrix is not verified to be in the proper form.
  6860. * This method is faster than computing the product for general 4x4
  6861. * matrices using {@link Matrix4.multiply}.
  6862. *
  6863. * @param {Matrix4} left The first matrix.
  6864. * @param {Matrix4} right The second matrix.
  6865. * @param {Matrix4} result The object onto which to store the result.
  6866. * @returns {Matrix4} The modified result parameter.
  6867. *
  6868. * @example
  6869. * 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);
  6870. * var m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  6871. * var m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  6872. */
  6873. Matrix4.multiplyTransformation = function(left, right, result) {
  6874. if (!defined(left)) {
  6875. throw new DeveloperError('left is required');
  6876. }
  6877. if (!defined(right)) {
  6878. throw new DeveloperError('right is required');
  6879. }
  6880. if (!defined(result)) {
  6881. throw new DeveloperError('result is required');
  6882. }
  6883. var left0 = left[0];
  6884. var left1 = left[1];
  6885. var left2 = left[2];
  6886. var left4 = left[4];
  6887. var left5 = left[5];
  6888. var left6 = left[6];
  6889. var left8 = left[8];
  6890. var left9 = left[9];
  6891. var left10 = left[10];
  6892. var left12 = left[12];
  6893. var left13 = left[13];
  6894. var left14 = left[14];
  6895. var right0 = right[0];
  6896. var right1 = right[1];
  6897. var right2 = right[2];
  6898. var right4 = right[4];
  6899. var right5 = right[5];
  6900. var right6 = right[6];
  6901. var right8 = right[8];
  6902. var right9 = right[9];
  6903. var right10 = right[10];
  6904. var right12 = right[12];
  6905. var right13 = right[13];
  6906. var right14 = right[14];
  6907. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6908. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6909. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6910. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6911. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6912. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6913. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6914. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6915. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6916. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12;
  6917. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13;
  6918. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14;
  6919. result[0] = column0Row0;
  6920. result[1] = column0Row1;
  6921. result[2] = column0Row2;
  6922. result[3] = 0.0;
  6923. result[4] = column1Row0;
  6924. result[5] = column1Row1;
  6925. result[6] = column1Row2;
  6926. result[7] = 0.0;
  6927. result[8] = column2Row0;
  6928. result[9] = column2Row1;
  6929. result[10] = column2Row2;
  6930. result[11] = 0.0;
  6931. result[12] = column3Row0;
  6932. result[13] = column3Row1;
  6933. result[14] = column3Row2;
  6934. result[15] = 1.0;
  6935. return result;
  6936. };
  6937. /**
  6938. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  6939. * by a 3x3 rotation matrix. This is an optimization
  6940. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  6941. *
  6942. * @param {Matrix4} matrix The matrix on the left-hand side.
  6943. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  6944. * @param {Matrix4} result The object onto which to store the result.
  6945. * @returns {Matrix4} The modified result parameter.
  6946. *
  6947. * @example
  6948. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  6949. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  6950. */
  6951. Matrix4.multiplyByMatrix3 = function(matrix, rotation, result) {
  6952. if (!defined(matrix)) {
  6953. throw new DeveloperError('matrix is required');
  6954. }
  6955. if (!defined(rotation)) {
  6956. throw new DeveloperError('rotation is required');
  6957. }
  6958. if (!defined(result)) {
  6959. throw new DeveloperError('result is required');
  6960. }
  6961. var left0 = matrix[0];
  6962. var left1 = matrix[1];
  6963. var left2 = matrix[2];
  6964. var left4 = matrix[4];
  6965. var left5 = matrix[5];
  6966. var left6 = matrix[6];
  6967. var left8 = matrix[8];
  6968. var left9 = matrix[9];
  6969. var left10 = matrix[10];
  6970. var right0 = rotation[0];
  6971. var right1 = rotation[1];
  6972. var right2 = rotation[2];
  6973. var right4 = rotation[3];
  6974. var right5 = rotation[4];
  6975. var right6 = rotation[5];
  6976. var right8 = rotation[6];
  6977. var right9 = rotation[7];
  6978. var right10 = rotation[8];
  6979. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  6980. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  6981. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  6982. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  6983. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  6984. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  6985. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  6986. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  6987. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  6988. result[0] = column0Row0;
  6989. result[1] = column0Row1;
  6990. result[2] = column0Row2;
  6991. result[3] = 0.0;
  6992. result[4] = column1Row0;
  6993. result[5] = column1Row1;
  6994. result[6] = column1Row2;
  6995. result[7] = 0.0;
  6996. result[8] = column2Row0;
  6997. result[9] = column2Row1;
  6998. result[10] = column2Row2;
  6999. result[11] = 0.0;
  7000. result[12] = matrix[12];
  7001. result[13] = matrix[13];
  7002. result[14] = matrix[14];
  7003. result[15] = matrix[15];
  7004. return result;
  7005. };
  7006. /**
  7007. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7008. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  7009. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  7010. *
  7011. * @param {Matrix4} matrix The matrix on the left-hand side.
  7012. * @param {Cartesian3} translation The translation on the right-hand side.
  7013. * @param {Matrix4} result The object onto which to store the result.
  7014. * @returns {Matrix4} The modified result parameter.
  7015. *
  7016. * @example
  7017. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  7018. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  7019. */
  7020. Matrix4.multiplyByTranslation = function(matrix, translation, result) {
  7021. if (!defined(matrix)) {
  7022. throw new DeveloperError('matrix is required');
  7023. }
  7024. if (!defined(translation)) {
  7025. throw new DeveloperError('translation is required');
  7026. }
  7027. if (!defined(result)) {
  7028. throw new DeveloperError('result is required');
  7029. }
  7030. var x = translation.x;
  7031. var y = translation.y;
  7032. var z = translation.z;
  7033. var tx = (x * matrix[0]) + (y * matrix[4]) + (z * matrix[8]) + matrix[12];
  7034. var ty = (x * matrix[1]) + (y * matrix[5]) + (z * matrix[9]) + matrix[13];
  7035. var tz = (x * matrix[2]) + (y * matrix[6]) + (z * matrix[10]) + matrix[14];
  7036. result[0] = matrix[0];
  7037. result[1] = matrix[1];
  7038. result[2] = matrix[2];
  7039. result[3] = matrix[3];
  7040. result[4] = matrix[4];
  7041. result[5] = matrix[5];
  7042. result[6] = matrix[6];
  7043. result[7] = matrix[7];
  7044. result[8] = matrix[8];
  7045. result[9] = matrix[9];
  7046. result[10] = matrix[10];
  7047. result[11] = matrix[11];
  7048. result[12] = tx;
  7049. result[13] = ty;
  7050. result[14] = tz;
  7051. result[15] = matrix[15];
  7052. return result;
  7053. };
  7054. var uniformScaleScratch = new Cartesian3();
  7055. /**
  7056. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7057. * by an implicit uniform scale matrix. This is an optimization
  7058. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  7059. * <code>m</code> must be an affine matrix.
  7060. * This function performs fewer allocations and arithmetic operations.
  7061. *
  7062. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  7063. * @param {Number} scale The uniform scale on the right-hand side.
  7064. * @param {Matrix4} result The object onto which to store the result.
  7065. * @returns {Matrix4} The modified result parameter.
  7066. *
  7067. *
  7068. * @example
  7069. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  7070. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  7071. *
  7072. * @see Matrix4.fromUniformScale
  7073. * @see Matrix4.multiplyByScale
  7074. */
  7075. Matrix4.multiplyByUniformScale = function(matrix, scale, result) {
  7076. if (!defined(matrix)) {
  7077. throw new DeveloperError('matrix is required');
  7078. }
  7079. if (typeof scale !== 'number') {
  7080. throw new DeveloperError('scale is required');
  7081. }
  7082. if (!defined(result)) {
  7083. throw new DeveloperError('result is required');
  7084. }
  7085. uniformScaleScratch.x = scale;
  7086. uniformScaleScratch.y = scale;
  7087. uniformScaleScratch.z = scale;
  7088. return Matrix4.multiplyByScale(matrix, uniformScaleScratch, result);
  7089. };
  7090. /**
  7091. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  7092. * by an implicit non-uniform scale matrix. This is an optimization
  7093. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  7094. * <code>m</code> must be an affine matrix.
  7095. * This function performs fewer allocations and arithmetic operations.
  7096. *
  7097. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  7098. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  7099. * @param {Matrix4} result The object onto which to store the result.
  7100. * @returns {Matrix4} The modified result parameter.
  7101. *
  7102. *
  7103. * @example
  7104. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  7105. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  7106. *
  7107. * @see Matrix4.fromScale
  7108. * @see Matrix4.multiplyByUniformScale
  7109. */
  7110. Matrix4.multiplyByScale = function(matrix, scale, result) {
  7111. if (!defined(matrix)) {
  7112. throw new DeveloperError('matrix is required');
  7113. }
  7114. if (!defined(scale)) {
  7115. throw new DeveloperError('scale is required');
  7116. }
  7117. if (!defined(result)) {
  7118. throw new DeveloperError('result is required');
  7119. }
  7120. var scaleX = scale.x;
  7121. var scaleY = scale.y;
  7122. var scaleZ = scale.z;
  7123. // Faster than Cartesian3.equals
  7124. if ((scaleX === 1.0) && (scaleY === 1.0) && (scaleZ === 1.0)) {
  7125. return Matrix4.clone(matrix, result);
  7126. }
  7127. result[0] = scaleX * matrix[0];
  7128. result[1] = scaleX * matrix[1];
  7129. result[2] = scaleX * matrix[2];
  7130. result[3] = 0.0;
  7131. result[4] = scaleY * matrix[4];
  7132. result[5] = scaleY * matrix[5];
  7133. result[6] = scaleY * matrix[6];
  7134. result[7] = 0.0;
  7135. result[8] = scaleZ * matrix[8];
  7136. result[9] = scaleZ * matrix[9];
  7137. result[10] = scaleZ * matrix[10];
  7138. result[11] = 0.0;
  7139. result[12] = matrix[12];
  7140. result[13] = matrix[13];
  7141. result[14] = matrix[14];
  7142. result[15] = 1.0;
  7143. return result;
  7144. };
  7145. /**
  7146. * Computes the product of a matrix and a column vector.
  7147. *
  7148. * @param {Matrix4} matrix The matrix.
  7149. * @param {Cartesian4} cartesian The vector.
  7150. * @param {Cartesian4} result The object onto which to store the result.
  7151. * @returns {Cartesian4} The modified result parameter.
  7152. */
  7153. Matrix4.multiplyByVector = function(matrix, cartesian, result) {
  7154. if (!defined(matrix)) {
  7155. throw new DeveloperError('matrix is required');
  7156. }
  7157. if (!defined(cartesian)) {
  7158. throw new DeveloperError('cartesian is required');
  7159. }
  7160. if (!defined(result)) {
  7161. throw new DeveloperError('result is required');
  7162. }
  7163. var vX = cartesian.x;
  7164. var vY = cartesian.y;
  7165. var vZ = cartesian.z;
  7166. var vW = cartesian.w;
  7167. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  7168. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  7169. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  7170. var w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  7171. result.x = x;
  7172. result.y = y;
  7173. result.z = z;
  7174. result.w = w;
  7175. return result;
  7176. };
  7177. /**
  7178. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  7179. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  7180. *
  7181. * @param {Matrix4} matrix The matrix.
  7182. * @param {Cartesian3} cartesian The point.
  7183. * @param {Cartesian3} result The object onto which to store the result.
  7184. * @returns {Cartesian3} The modified result parameter.
  7185. *
  7186. * @example
  7187. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  7188. * var result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  7189. * // A shortcut for
  7190. * // Cartesian3 p = ...
  7191. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  7192. */
  7193. Matrix4.multiplyByPointAsVector = function(matrix, cartesian, result) {
  7194. if (!defined(matrix)) {
  7195. throw new DeveloperError('matrix is required');
  7196. }
  7197. if (!defined(cartesian)) {
  7198. throw new DeveloperError('cartesian is required');
  7199. }
  7200. if (!defined(result)) {
  7201. throw new DeveloperError('result is required');
  7202. }
  7203. var vX = cartesian.x;
  7204. var vY = cartesian.y;
  7205. var vZ = cartesian.z;
  7206. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  7207. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  7208. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  7209. result.x = x;
  7210. result.y = y;
  7211. result.z = z;
  7212. return result;
  7213. };
  7214. /**
  7215. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  7216. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  7217. *
  7218. * @param {Matrix4} matrix The matrix.
  7219. * @param {Cartesian3} cartesian The point.
  7220. * @param {Cartesian3} result The object onto which to store the result.
  7221. * @returns {Cartesian3} The modified result parameter.
  7222. *
  7223. * @example
  7224. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  7225. * var result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  7226. */
  7227. Matrix4.multiplyByPoint = function(matrix, cartesian, result) {
  7228. if (!defined(matrix)) {
  7229. throw new DeveloperError('matrix is required');
  7230. }
  7231. if (!defined(cartesian)) {
  7232. throw new DeveloperError('cartesian is required');
  7233. }
  7234. if (!defined(result)) {
  7235. throw new DeveloperError('result is required');
  7236. }
  7237. var vX = cartesian.x;
  7238. var vY = cartesian.y;
  7239. var vZ = cartesian.z;
  7240. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  7241. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  7242. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  7243. result.x = x;
  7244. result.y = y;
  7245. result.z = z;
  7246. return result;
  7247. };
  7248. /**
  7249. * Computes the product of a matrix and a scalar.
  7250. *
  7251. * @param {Matrix4} matrix The matrix.
  7252. * @param {Number} scalar The number to multiply by.
  7253. * @param {Matrix4} result The object onto which to store the result.
  7254. * @returns {Matrix4} The modified result parameter.
  7255. *
  7256. * @example
  7257. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  7258. * // m = [10.0, 11.0, 12.0, 13.0]
  7259. * // [14.0, 15.0, 16.0, 17.0]
  7260. * // [18.0, 19.0, 20.0, 21.0]
  7261. * // [22.0, 23.0, 24.0, 25.0]
  7262. *
  7263. * var a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  7264. *
  7265. * // m remains the same
  7266. * // a = [-20.0, -22.0, -24.0, -26.0]
  7267. * // [-28.0, -30.0, -32.0, -34.0]
  7268. * // [-36.0, -38.0, -40.0, -42.0]
  7269. * // [-44.0, -46.0, -48.0, -50.0]
  7270. */
  7271. Matrix4.multiplyByScalar = function(matrix, scalar, result) {
  7272. if (!defined(matrix)) {
  7273. throw new DeveloperError('matrix is required');
  7274. }
  7275. if (typeof scalar !== 'number') {
  7276. throw new DeveloperError('scalar must be a number');
  7277. }
  7278. if (!defined(result)) {
  7279. throw new DeveloperError('result is required');
  7280. }
  7281. result[0] = matrix[0] * scalar;
  7282. result[1] = matrix[1] * scalar;
  7283. result[2] = matrix[2] * scalar;
  7284. result[3] = matrix[3] * scalar;
  7285. result[4] = matrix[4] * scalar;
  7286. result[5] = matrix[5] * scalar;
  7287. result[6] = matrix[6] * scalar;
  7288. result[7] = matrix[7] * scalar;
  7289. result[8] = matrix[8] * scalar;
  7290. result[9] = matrix[9] * scalar;
  7291. result[10] = matrix[10] * scalar;
  7292. result[11] = matrix[11] * scalar;
  7293. result[12] = matrix[12] * scalar;
  7294. result[13] = matrix[13] * scalar;
  7295. result[14] = matrix[14] * scalar;
  7296. result[15] = matrix[15] * scalar;
  7297. return result;
  7298. };
  7299. /**
  7300. * Computes a negated copy of the provided matrix.
  7301. *
  7302. * @param {Matrix4} matrix The matrix to negate.
  7303. * @param {Matrix4} result The object onto which to store the result.
  7304. * @returns {Matrix4} The modified result parameter.
  7305. *
  7306. * @example
  7307. * //create a new Matrix4 instance which is a negation of a Matrix4
  7308. * // m = [10.0, 11.0, 12.0, 13.0]
  7309. * // [14.0, 15.0, 16.0, 17.0]
  7310. * // [18.0, 19.0, 20.0, 21.0]
  7311. * // [22.0, 23.0, 24.0, 25.0]
  7312. *
  7313. * var a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  7314. *
  7315. * // m remains the same
  7316. * // a = [-10.0, -11.0, -12.0, -13.0]
  7317. * // [-14.0, -15.0, -16.0, -17.0]
  7318. * // [-18.0, -19.0, -20.0, -21.0]
  7319. * // [-22.0, -23.0, -24.0, -25.0]
  7320. */
  7321. Matrix4.negate = function(matrix, result) {
  7322. if (!defined(matrix)) {
  7323. throw new DeveloperError('matrix is required');
  7324. }
  7325. if (!defined(result)) {
  7326. throw new DeveloperError('result is required');
  7327. }
  7328. result[0] = -matrix[0];
  7329. result[1] = -matrix[1];
  7330. result[2] = -matrix[2];
  7331. result[3] = -matrix[3];
  7332. result[4] = -matrix[4];
  7333. result[5] = -matrix[5];
  7334. result[6] = -matrix[6];
  7335. result[7] = -matrix[7];
  7336. result[8] = -matrix[8];
  7337. result[9] = -matrix[9];
  7338. result[10] = -matrix[10];
  7339. result[11] = -matrix[11];
  7340. result[12] = -matrix[12];
  7341. result[13] = -matrix[13];
  7342. result[14] = -matrix[14];
  7343. result[15] = -matrix[15];
  7344. return result;
  7345. };
  7346. /**
  7347. * Computes the transpose of the provided matrix.
  7348. *
  7349. * @param {Matrix4} matrix The matrix to transpose.
  7350. * @param {Matrix4} result The object onto which to store the result.
  7351. * @returns {Matrix4} The modified result parameter.
  7352. *
  7353. * @example
  7354. * //returns transpose of a Matrix4
  7355. * // m = [10.0, 11.0, 12.0, 13.0]
  7356. * // [14.0, 15.0, 16.0, 17.0]
  7357. * // [18.0, 19.0, 20.0, 21.0]
  7358. * // [22.0, 23.0, 24.0, 25.0]
  7359. *
  7360. * var a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  7361. *
  7362. * // m remains the same
  7363. * // a = [10.0, 14.0, 18.0, 22.0]
  7364. * // [11.0, 15.0, 19.0, 23.0]
  7365. * // [12.0, 16.0, 20.0, 24.0]
  7366. * // [13.0, 17.0, 21.0, 25.0]
  7367. */
  7368. Matrix4.transpose = function(matrix, result) {
  7369. if (!defined(matrix)) {
  7370. throw new DeveloperError('matrix is required');
  7371. }
  7372. if (!defined(result)) {
  7373. throw new DeveloperError('result is required');
  7374. }
  7375. var matrix1 = matrix[1];
  7376. var matrix2 = matrix[2];
  7377. var matrix3 = matrix[3];
  7378. var matrix6 = matrix[6];
  7379. var matrix7 = matrix[7];
  7380. var matrix11 = matrix[11];
  7381. result[0] = matrix[0];
  7382. result[1] = matrix[4];
  7383. result[2] = matrix[8];
  7384. result[3] = matrix[12];
  7385. result[4] = matrix1;
  7386. result[5] = matrix[5];
  7387. result[6] = matrix[9];
  7388. result[7] = matrix[13];
  7389. result[8] = matrix2;
  7390. result[9] = matrix6;
  7391. result[10] = matrix[10];
  7392. result[11] = matrix[14];
  7393. result[12] = matrix3;
  7394. result[13] = matrix7;
  7395. result[14] = matrix11;
  7396. result[15] = matrix[15];
  7397. return result;
  7398. };
  7399. /**
  7400. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  7401. *
  7402. * @param {Matrix4} matrix The matrix with signed elements.
  7403. * @param {Matrix4} result The object onto which to store the result.
  7404. * @returns {Matrix4} The modified result parameter.
  7405. */
  7406. Matrix4.abs = function(matrix, result) {
  7407. if (!defined(matrix)) {
  7408. throw new DeveloperError('matrix is required');
  7409. }
  7410. if (!defined(result)) {
  7411. throw new DeveloperError('result is required');
  7412. }
  7413. result[0] = Math.abs(matrix[0]);
  7414. result[1] = Math.abs(matrix[1]);
  7415. result[2] = Math.abs(matrix[2]);
  7416. result[3] = Math.abs(matrix[3]);
  7417. result[4] = Math.abs(matrix[4]);
  7418. result[5] = Math.abs(matrix[5]);
  7419. result[6] = Math.abs(matrix[6]);
  7420. result[7] = Math.abs(matrix[7]);
  7421. result[8] = Math.abs(matrix[8]);
  7422. result[9] = Math.abs(matrix[9]);
  7423. result[10] = Math.abs(matrix[10]);
  7424. result[11] = Math.abs(matrix[11]);
  7425. result[12] = Math.abs(matrix[12]);
  7426. result[13] = Math.abs(matrix[13]);
  7427. result[14] = Math.abs(matrix[14]);
  7428. result[15] = Math.abs(matrix[15]);
  7429. return result;
  7430. };
  7431. /**
  7432. * Compares the provided matrices componentwise and returns
  7433. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7434. *
  7435. * @param {Matrix4} [left] The first matrix.
  7436. * @param {Matrix4} [right] The second matrix.
  7437. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  7438. *
  7439. * @example
  7440. * //compares two Matrix4 instances
  7441. *
  7442. * // a = [10.0, 14.0, 18.0, 22.0]
  7443. * // [11.0, 15.0, 19.0, 23.0]
  7444. * // [12.0, 16.0, 20.0, 24.0]
  7445. * // [13.0, 17.0, 21.0, 25.0]
  7446. *
  7447. * // b = [10.0, 14.0, 18.0, 22.0]
  7448. * // [11.0, 15.0, 19.0, 23.0]
  7449. * // [12.0, 16.0, 20.0, 24.0]
  7450. * // [13.0, 17.0, 21.0, 25.0]
  7451. *
  7452. * if(Cesium.Matrix4.equals(a,b)) {
  7453. * console.log("Both matrices are equal");
  7454. * } else {
  7455. * console.log("They are not equal");
  7456. * }
  7457. *
  7458. * //Prints "Both matrices are equal" on the console
  7459. */
  7460. Matrix4.equals = function(left, right) {
  7461. // Given that most matrices will be transformation matrices, the elements
  7462. // are tested in order such that the test is likely to fail as early
  7463. // as possible. I _think_ this is just as friendly to the L1 cache
  7464. // as testing in index order. It is certainty faster in practice.
  7465. return (left === right) ||
  7466. (defined(left) &&
  7467. defined(right) &&
  7468. // Translation
  7469. left[12] === right[12] &&
  7470. left[13] === right[13] &&
  7471. left[14] === right[14] &&
  7472. // Rotation/scale
  7473. left[0] === right[0] &&
  7474. left[1] === right[1] &&
  7475. left[2] === right[2] &&
  7476. left[4] === right[4] &&
  7477. left[5] === right[5] &&
  7478. left[6] === right[6] &&
  7479. left[8] === right[8] &&
  7480. left[9] === right[9] &&
  7481. left[10] === right[10] &&
  7482. // Bottom row
  7483. left[3] === right[3] &&
  7484. left[7] === right[7] &&
  7485. left[11] === right[11] &&
  7486. left[15] === right[15]);
  7487. };
  7488. /**
  7489. * Compares the provided matrices componentwise and returns
  7490. * <code>true</code> if they are within the provided epsilon,
  7491. * <code>false</code> otherwise.
  7492. *
  7493. * @param {Matrix4} [left] The first matrix.
  7494. * @param {Matrix4} [right] The second matrix.
  7495. * @param {Number} epsilon The epsilon to use for equality testing.
  7496. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  7497. *
  7498. * @example
  7499. * //compares two Matrix4 instances
  7500. *
  7501. * // a = [10.5, 14.5, 18.5, 22.5]
  7502. * // [11.5, 15.5, 19.5, 23.5]
  7503. * // [12.5, 16.5, 20.5, 24.5]
  7504. * // [13.5, 17.5, 21.5, 25.5]
  7505. *
  7506. * // b = [10.0, 14.0, 18.0, 22.0]
  7507. * // [11.0, 15.0, 19.0, 23.0]
  7508. * // [12.0, 16.0, 20.0, 24.0]
  7509. * // [13.0, 17.0, 21.0, 25.0]
  7510. *
  7511. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  7512. * console.log("Difference between both the matrices is less than 0.1");
  7513. * } else {
  7514. * console.log("Difference between both the matrices is not less than 0.1");
  7515. * }
  7516. *
  7517. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  7518. */
  7519. Matrix4.equalsEpsilon = function(left, right, epsilon) {
  7520. if (typeof epsilon !== 'number') {
  7521. throw new DeveloperError('epsilon must be a number');
  7522. }
  7523. return (left === right) ||
  7524. (defined(left) &&
  7525. defined(right) &&
  7526. Math.abs(left[0] - right[0]) <= epsilon &&
  7527. Math.abs(left[1] - right[1]) <= epsilon &&
  7528. Math.abs(left[2] - right[2]) <= epsilon &&
  7529. Math.abs(left[3] - right[3]) <= epsilon &&
  7530. Math.abs(left[4] - right[4]) <= epsilon &&
  7531. Math.abs(left[5] - right[5]) <= epsilon &&
  7532. Math.abs(left[6] - right[6]) <= epsilon &&
  7533. Math.abs(left[7] - right[7]) <= epsilon &&
  7534. Math.abs(left[8] - right[8]) <= epsilon &&
  7535. Math.abs(left[9] - right[9]) <= epsilon &&
  7536. Math.abs(left[10] - right[10]) <= epsilon &&
  7537. Math.abs(left[11] - right[11]) <= epsilon &&
  7538. Math.abs(left[12] - right[12]) <= epsilon &&
  7539. Math.abs(left[13] - right[13]) <= epsilon &&
  7540. Math.abs(left[14] - right[14]) <= epsilon &&
  7541. Math.abs(left[15] - right[15]) <= epsilon);
  7542. };
  7543. /**
  7544. * Gets the translation portion of the provided matrix, assuming the matrix is a affine transformation matrix.
  7545. *
  7546. * @param {Matrix4} matrix The matrix to use.
  7547. * @param {Cartesian3} result The object onto which to store the result.
  7548. * @returns {Cartesian3} The modified result parameter.
  7549. */
  7550. Matrix4.getTranslation = function(matrix, result) {
  7551. if (!defined(matrix)) {
  7552. throw new DeveloperError('matrix is required');
  7553. }
  7554. if (!defined(result)) {
  7555. throw new DeveloperError('result is required');
  7556. }
  7557. result.x = matrix[12];
  7558. result.y = matrix[13];
  7559. result.z = matrix[14];
  7560. return result;
  7561. };
  7562. /**
  7563. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is a affine transformation matrix.
  7564. *
  7565. * @param {Matrix4} matrix The matrix to use.
  7566. * @param {Matrix3} result The object onto which to store the result.
  7567. * @returns {Matrix3} The modified result parameter.
  7568. *
  7569. * @example
  7570. * // returns a Matrix3 instance from a Matrix4 instance
  7571. *
  7572. * // m = [10.0, 14.0, 18.0, 22.0]
  7573. * // [11.0, 15.0, 19.0, 23.0]
  7574. * // [12.0, 16.0, 20.0, 24.0]
  7575. * // [13.0, 17.0, 21.0, 25.0]
  7576. *
  7577. * var b = new Cesium.Matrix3();
  7578. * Cesium.Matrix4.getRotation(m,b);
  7579. *
  7580. * // b = [10.0, 14.0, 18.0]
  7581. * // [11.0, 15.0, 19.0]
  7582. * // [12.0, 16.0, 20.0]
  7583. */
  7584. Matrix4.getRotation = function(matrix, result) {
  7585. if (!defined(matrix)) {
  7586. throw new DeveloperError('matrix is required');
  7587. }
  7588. if (!defined(result)) {
  7589. throw new DeveloperError('result is required');
  7590. }
  7591. result[0] = matrix[0];
  7592. result[1] = matrix[1];
  7593. result[2] = matrix[2];
  7594. result[3] = matrix[4];
  7595. result[4] = matrix[5];
  7596. result[5] = matrix[6];
  7597. result[6] = matrix[8];
  7598. result[7] = matrix[9];
  7599. result[8] = matrix[10];
  7600. return result;
  7601. };
  7602. var scratchInverseRotation = new Matrix3();
  7603. var scratchMatrix3Zero = new Matrix3();
  7604. var scratchBottomRow = new Cartesian4();
  7605. var scratchExpectedBottomRow = new Cartesian4(0.0, 0.0, 0.0, 1.0);
  7606. /**
  7607. * Computes the inverse of the provided matrix using Cramers Rule.
  7608. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  7609. * If the matrix is an affine transformation matrix, it is more efficient
  7610. * to invert it with {@link Matrix4.inverseTransformation}.
  7611. *
  7612. * @param {Matrix4} matrix The matrix to invert.
  7613. * @param {Matrix4} result The object onto which to store the result.
  7614. * @returns {Matrix4} The modified result parameter.
  7615. *
  7616. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  7617. */
  7618. Matrix4.inverse = function(matrix, result) {
  7619. if (!defined(matrix)) {
  7620. throw new DeveloperError('matrix is required');
  7621. }
  7622. if (!defined(result)) {
  7623. throw new DeveloperError('result is required');
  7624. }
  7625. // Special case for a zero scale matrix that can occur, for example,
  7626. // when a model's node has a [0, 0, 0] scale.
  7627. if (Matrix3.equalsEpsilon(Matrix4.getRotation(matrix, scratchInverseRotation), scratchMatrix3Zero, CesiumMath.EPSILON7) &&
  7628. Cartesian4.equals(Matrix4.getRow(matrix, 3, scratchBottomRow), scratchExpectedBottomRow)) {
  7629. result[0] = 0.0;
  7630. result[1] = 0.0;
  7631. result[2] = 0.0;
  7632. result[3] = 0.0;
  7633. result[4] = 0.0;
  7634. result[5] = 0.0;
  7635. result[6] = 0.0;
  7636. result[7] = 0.0;
  7637. result[8] = 0.0;
  7638. result[9] = 0.0;
  7639. result[10] = 0.0;
  7640. result[11] = 0.0;
  7641. result[12] = -matrix[12];
  7642. result[13] = -matrix[13];
  7643. result[14] = -matrix[14];
  7644. result[15] = 1.0;
  7645. return result;
  7646. }
  7647. //
  7648. // Ported from:
  7649. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  7650. //
  7651. var src0 = matrix[0];
  7652. var src1 = matrix[4];
  7653. var src2 = matrix[8];
  7654. var src3 = matrix[12];
  7655. var src4 = matrix[1];
  7656. var src5 = matrix[5];
  7657. var src6 = matrix[9];
  7658. var src7 = matrix[13];
  7659. var src8 = matrix[2];
  7660. var src9 = matrix[6];
  7661. var src10 = matrix[10];
  7662. var src11 = matrix[14];
  7663. var src12 = matrix[3];
  7664. var src13 = matrix[7];
  7665. var src14 = matrix[11];
  7666. var src15 = matrix[15];
  7667. // calculate pairs for first 8 elements (cofactors)
  7668. var tmp0 = src10 * src15;
  7669. var tmp1 = src11 * src14;
  7670. var tmp2 = src9 * src15;
  7671. var tmp3 = src11 * src13;
  7672. var tmp4 = src9 * src14;
  7673. var tmp5 = src10 * src13;
  7674. var tmp6 = src8 * src15;
  7675. var tmp7 = src11 * src12;
  7676. var tmp8 = src8 * src14;
  7677. var tmp9 = src10 * src12;
  7678. var tmp10 = src8 * src13;
  7679. var tmp11 = src9 * src12;
  7680. // calculate first 8 elements (cofactors)
  7681. var dst0 = (tmp0 * src5 + tmp3 * src6 + tmp4 * src7) - (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  7682. var dst1 = (tmp1 * src4 + tmp6 * src6 + tmp9 * src7) - (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  7683. var dst2 = (tmp2 * src4 + tmp7 * src5 + tmp10 * src7) - (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  7684. var dst3 = (tmp5 * src4 + tmp8 * src5 + tmp11 * src6) - (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  7685. var dst4 = (tmp1 * src1 + tmp2 * src2 + tmp5 * src3) - (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  7686. var dst5 = (tmp0 * src0 + tmp7 * src2 + tmp8 * src3) - (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  7687. var dst6 = (tmp3 * src0 + tmp6 * src1 + tmp11 * src3) - (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  7688. var dst7 = (tmp4 * src0 + tmp9 * src1 + tmp10 * src2) - (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  7689. // calculate pairs for second 8 elements (cofactors)
  7690. tmp0 = src2 * src7;
  7691. tmp1 = src3 * src6;
  7692. tmp2 = src1 * src7;
  7693. tmp3 = src3 * src5;
  7694. tmp4 = src1 * src6;
  7695. tmp5 = src2 * src5;
  7696. tmp6 = src0 * src7;
  7697. tmp7 = src3 * src4;
  7698. tmp8 = src0 * src6;
  7699. tmp9 = src2 * src4;
  7700. tmp10 = src0 * src5;
  7701. tmp11 = src1 * src4;
  7702. // calculate second 8 elements (cofactors)
  7703. var dst8 = (tmp0 * src13 + tmp3 * src14 + tmp4 * src15) - (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  7704. var dst9 = (tmp1 * src12 + tmp6 * src14 + tmp9 * src15) - (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  7705. var dst10 = (tmp2 * src12 + tmp7 * src13 + tmp10 * src15) - (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  7706. var dst11 = (tmp5 * src12 + tmp8 * src13 + tmp11 * src14) - (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  7707. var dst12 = (tmp2 * src10 + tmp5 * src11 + tmp1 * src9) - (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  7708. var dst13 = (tmp8 * src11 + tmp0 * src8 + tmp7 * src10) - (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  7709. var dst14 = (tmp6 * src9 + tmp11 * src11 + tmp3 * src8) - (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  7710. var dst15 = (tmp10 * src10 + tmp4 * src8 + tmp9 * src9) - (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  7711. // calculate determinant
  7712. var det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  7713. if (Math.abs(det) < CesiumMath.EPSILON20) {
  7714. throw new RuntimeError('matrix is not invertible because its determinate is zero.');
  7715. }
  7716. // calculate matrix inverse
  7717. det = 1.0 / det;
  7718. result[0] = dst0 * det;
  7719. result[1] = dst1 * det;
  7720. result[2] = dst2 * det;
  7721. result[3] = dst3 * det;
  7722. result[4] = dst4 * det;
  7723. result[5] = dst5 * det;
  7724. result[6] = dst6 * det;
  7725. result[7] = dst7 * det;
  7726. result[8] = dst8 * det;
  7727. result[9] = dst9 * det;
  7728. result[10] = dst10 * det;
  7729. result[11] = dst11 * det;
  7730. result[12] = dst12 * det;
  7731. result[13] = dst13 * det;
  7732. result[14] = dst14 * det;
  7733. result[15] = dst15 * det;
  7734. return result;
  7735. };
  7736. /**
  7737. * Computes the inverse of the provided matrix assuming it is
  7738. * an affine transformation matrix, where the upper left 3x3 elements
  7739. * are a rotation matrix, and the upper three elements in the fourth
  7740. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  7741. * The matrix is not verified to be in the proper form.
  7742. * This method is faster than computing the inverse for a general 4x4
  7743. * matrix using {@link Matrix4.inverse}.
  7744. *
  7745. * @param {Matrix4} matrix The matrix to invert.
  7746. * @param {Matrix4} result The object onto which to store the result.
  7747. * @returns {Matrix4} The modified result parameter.
  7748. */
  7749. Matrix4.inverseTransformation = function(matrix, result) {
  7750. if (!defined(matrix)) {
  7751. throw new DeveloperError('matrix is required');
  7752. }
  7753. if (!defined(result)) {
  7754. throw new DeveloperError('result is required');
  7755. }
  7756. //This function is an optimized version of the below 4 lines.
  7757. //var rT = Matrix3.transpose(Matrix4.getRotation(matrix));
  7758. //var rTN = Matrix3.negate(rT);
  7759. //var rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  7760. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  7761. var matrix0 = matrix[0];
  7762. var matrix1 = matrix[1];
  7763. var matrix2 = matrix[2];
  7764. var matrix4 = matrix[4];
  7765. var matrix5 = matrix[5];
  7766. var matrix6 = matrix[6];
  7767. var matrix8 = matrix[8];
  7768. var matrix9 = matrix[9];
  7769. var matrix10 = matrix[10];
  7770. var vX = matrix[12];
  7771. var vY = matrix[13];
  7772. var vZ = matrix[14];
  7773. var x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  7774. var y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  7775. var z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  7776. result[0] = matrix0;
  7777. result[1] = matrix4;
  7778. result[2] = matrix8;
  7779. result[3] = 0.0;
  7780. result[4] = matrix1;
  7781. result[5] = matrix5;
  7782. result[6] = matrix9;
  7783. result[7] = 0.0;
  7784. result[8] = matrix2;
  7785. result[9] = matrix6;
  7786. result[10] = matrix10;
  7787. result[11] = 0.0;
  7788. result[12] = x;
  7789. result[13] = y;
  7790. result[14] = z;
  7791. result[15] = 1.0;
  7792. return result;
  7793. };
  7794. /**
  7795. * An immutable Matrix4 instance initialized to the identity matrix.
  7796. *
  7797. * @type {Matrix4}
  7798. * @constant
  7799. */
  7800. Matrix4.IDENTITY = freezeObject(new Matrix4(1.0, 0.0, 0.0, 0.0,
  7801. 0.0, 1.0, 0.0, 0.0,
  7802. 0.0, 0.0, 1.0, 0.0,
  7803. 0.0, 0.0, 0.0, 1.0));
  7804. /**
  7805. * An immutable Matrix4 instance initialized to the zero matrix.
  7806. *
  7807. * @type {Matrix4}
  7808. * @constant
  7809. */
  7810. Matrix4.ZERO = freezeObject(new Matrix4(0.0, 0.0, 0.0, 0.0,
  7811. 0.0, 0.0, 0.0, 0.0,
  7812. 0.0, 0.0, 0.0, 0.0,
  7813. 0.0, 0.0, 0.0, 0.0));
  7814. /**
  7815. * The index into Matrix4 for column 0, row 0.
  7816. *
  7817. * @type {Number}
  7818. * @constant
  7819. */
  7820. Matrix4.COLUMN0ROW0 = 0;
  7821. /**
  7822. * The index into Matrix4 for column 0, row 1.
  7823. *
  7824. * @type {Number}
  7825. * @constant
  7826. */
  7827. Matrix4.COLUMN0ROW1 = 1;
  7828. /**
  7829. * The index into Matrix4 for column 0, row 2.
  7830. *
  7831. * @type {Number}
  7832. * @constant
  7833. */
  7834. Matrix4.COLUMN0ROW2 = 2;
  7835. /**
  7836. * The index into Matrix4 for column 0, row 3.
  7837. *
  7838. * @type {Number}
  7839. * @constant
  7840. */
  7841. Matrix4.COLUMN0ROW3 = 3;
  7842. /**
  7843. * The index into Matrix4 for column 1, row 0.
  7844. *
  7845. * @type {Number}
  7846. * @constant
  7847. */
  7848. Matrix4.COLUMN1ROW0 = 4;
  7849. /**
  7850. * The index into Matrix4 for column 1, row 1.
  7851. *
  7852. * @type {Number}
  7853. * @constant
  7854. */
  7855. Matrix4.COLUMN1ROW1 = 5;
  7856. /**
  7857. * The index into Matrix4 for column 1, row 2.
  7858. *
  7859. * @type {Number}
  7860. * @constant
  7861. */
  7862. Matrix4.COLUMN1ROW2 = 6;
  7863. /**
  7864. * The index into Matrix4 for column 1, row 3.
  7865. *
  7866. * @type {Number}
  7867. * @constant
  7868. */
  7869. Matrix4.COLUMN1ROW3 = 7;
  7870. /**
  7871. * The index into Matrix4 for column 2, row 0.
  7872. *
  7873. * @type {Number}
  7874. * @constant
  7875. */
  7876. Matrix4.COLUMN2ROW0 = 8;
  7877. /**
  7878. * The index into Matrix4 for column 2, row 1.
  7879. *
  7880. * @type {Number}
  7881. * @constant
  7882. */
  7883. Matrix4.COLUMN2ROW1 = 9;
  7884. /**
  7885. * The index into Matrix4 for column 2, row 2.
  7886. *
  7887. * @type {Number}
  7888. * @constant
  7889. */
  7890. Matrix4.COLUMN2ROW2 = 10;
  7891. /**
  7892. * The index into Matrix4 for column 2, row 3.
  7893. *
  7894. * @type {Number}
  7895. * @constant
  7896. */
  7897. Matrix4.COLUMN2ROW3 = 11;
  7898. /**
  7899. * The index into Matrix4 for column 3, row 0.
  7900. *
  7901. * @type {Number}
  7902. * @constant
  7903. */
  7904. Matrix4.COLUMN3ROW0 = 12;
  7905. /**
  7906. * The index into Matrix4 for column 3, row 1.
  7907. *
  7908. * @type {Number}
  7909. * @constant
  7910. */
  7911. Matrix4.COLUMN3ROW1 = 13;
  7912. /**
  7913. * The index into Matrix4 for column 3, row 2.
  7914. *
  7915. * @type {Number}
  7916. * @constant
  7917. */
  7918. Matrix4.COLUMN3ROW2 = 14;
  7919. /**
  7920. * The index into Matrix4 for column 3, row 3.
  7921. *
  7922. * @type {Number}
  7923. * @constant
  7924. */
  7925. Matrix4.COLUMN3ROW3 = 15;
  7926. defineProperties(Matrix4.prototype, {
  7927. /**
  7928. * Gets the number of items in the collection.
  7929. * @memberof Matrix4.prototype
  7930. *
  7931. * @type {Number}
  7932. */
  7933. length : {
  7934. get : function() {
  7935. return Matrix4.packedLength;
  7936. }
  7937. }
  7938. });
  7939. /**
  7940. * Duplicates the provided Matrix4 instance.
  7941. *
  7942. * @param {Matrix4} [result] The object onto which to store the result.
  7943. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  7944. */
  7945. Matrix4.prototype.clone = function(result) {
  7946. return Matrix4.clone(this, result);
  7947. };
  7948. /**
  7949. * Compares this matrix to the provided matrix componentwise and returns
  7950. * <code>true</code> if they are equal, <code>false</code> otherwise.
  7951. *
  7952. * @param {Matrix4} [right] The right hand side matrix.
  7953. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  7954. */
  7955. Matrix4.prototype.equals = function(right) {
  7956. return Matrix4.equals(this, right);
  7957. };
  7958. /**
  7959. * @private
  7960. */
  7961. Matrix4.equalsArray = function(matrix, array, offset) {
  7962. return matrix[0] === array[offset] &&
  7963. matrix[1] === array[offset + 1] &&
  7964. matrix[2] === array[offset + 2] &&
  7965. matrix[3] === array[offset + 3] &&
  7966. matrix[4] === array[offset + 4] &&
  7967. matrix[5] === array[offset + 5] &&
  7968. matrix[6] === array[offset + 6] &&
  7969. matrix[7] === array[offset + 7] &&
  7970. matrix[8] === array[offset + 8] &&
  7971. matrix[9] === array[offset + 9] &&
  7972. matrix[10] === array[offset + 10] &&
  7973. matrix[11] === array[offset + 11] &&
  7974. matrix[12] === array[offset + 12] &&
  7975. matrix[13] === array[offset + 13] &&
  7976. matrix[14] === array[offset + 14] &&
  7977. matrix[15] === array[offset + 15];
  7978. };
  7979. /**
  7980. * Compares this matrix to the provided matrix componentwise and returns
  7981. * <code>true</code> if they are within the provided epsilon,
  7982. * <code>false</code> otherwise.
  7983. *
  7984. * @param {Matrix4} [right] The right hand side matrix.
  7985. * @param {Number} epsilon The epsilon to use for equality testing.
  7986. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  7987. */
  7988. Matrix4.prototype.equalsEpsilon = function(right, epsilon) {
  7989. return Matrix4.equalsEpsilon(this, right, epsilon);
  7990. };
  7991. /**
  7992. * Computes a string representing this Matrix with each row being
  7993. * on a separate line and in the format '(column0, column1, column2, column3)'.
  7994. *
  7995. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  7996. */
  7997. Matrix4.prototype.toString = function() {
  7998. return '(' + this[0] + ', ' + this[4] + ', ' + this[8] + ', ' + this[12] +')\n' +
  7999. '(' + this[1] + ', ' + this[5] + ', ' + this[9] + ', ' + this[13] +')\n' +
  8000. '(' + this[2] + ', ' + this[6] + ', ' + this[10] + ', ' + this[14] +')\n' +
  8001. '(' + this[3] + ', ' + this[7] + ', ' + this[11] + ', ' + this[15] +')';
  8002. };
  8003. return Matrix4;
  8004. });
  8005. /*global define*/
  8006. define('Core/Rectangle',[
  8007. './Cartographic',
  8008. './defaultValue',
  8009. './defined',
  8010. './defineProperties',
  8011. './DeveloperError',
  8012. './Ellipsoid',
  8013. './freezeObject',
  8014. './Math'
  8015. ], function(
  8016. Cartographic,
  8017. defaultValue,
  8018. defined,
  8019. defineProperties,
  8020. DeveloperError,
  8021. Ellipsoid,
  8022. freezeObject,
  8023. CesiumMath) {
  8024. 'use strict';
  8025. /**
  8026. * A two dimensional region specified as longitude and latitude coordinates.
  8027. *
  8028. * @alias Rectangle
  8029. * @constructor
  8030. *
  8031. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  8032. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  8033. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  8034. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  8035. *
  8036. * @see Packable
  8037. */
  8038. function Rectangle(west, south, east, north) {
  8039. /**
  8040. * The westernmost longitude in radians in the range [-Pi, Pi].
  8041. *
  8042. * @type {Number}
  8043. * @default 0.0
  8044. */
  8045. this.west = defaultValue(west, 0.0);
  8046. /**
  8047. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  8048. *
  8049. * @type {Number}
  8050. * @default 0.0
  8051. */
  8052. this.south = defaultValue(south, 0.0);
  8053. /**
  8054. * The easternmost longitude in radians in the range [-Pi, Pi].
  8055. *
  8056. * @type {Number}
  8057. * @default 0.0
  8058. */
  8059. this.east = defaultValue(east, 0.0);
  8060. /**
  8061. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  8062. *
  8063. * @type {Number}
  8064. * @default 0.0
  8065. */
  8066. this.north = defaultValue(north, 0.0);
  8067. }
  8068. defineProperties(Rectangle.prototype, {
  8069. /**
  8070. * Gets the width of the rectangle in radians.
  8071. * @memberof Rectangle.prototype
  8072. * @type {Number}
  8073. */
  8074. width : {
  8075. get : function() {
  8076. return Rectangle.computeWidth(this);
  8077. }
  8078. },
  8079. /**
  8080. * Gets the height of the rectangle in radians.
  8081. * @memberof Rectangle.prototype
  8082. * @type {Number}
  8083. */
  8084. height : {
  8085. get : function() {
  8086. return Rectangle.computeHeight(this);
  8087. }
  8088. }
  8089. });
  8090. /**
  8091. * The number of elements used to pack the object into an array.
  8092. * @type {Number}
  8093. */
  8094. Rectangle.packedLength = 4;
  8095. /**
  8096. * Stores the provided instance into the provided array.
  8097. *
  8098. * @param {Rectangle} value The value to pack.
  8099. * @param {Number[]} array The array to pack into.
  8100. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  8101. *
  8102. * @returns {Number[]} The array that was packed into
  8103. */
  8104. Rectangle.pack = function(value, array, startingIndex) {
  8105. if (!defined(value)) {
  8106. throw new DeveloperError('value is required');
  8107. }
  8108. if (!defined(array)) {
  8109. throw new DeveloperError('array is required');
  8110. }
  8111. startingIndex = defaultValue(startingIndex, 0);
  8112. array[startingIndex++] = value.west;
  8113. array[startingIndex++] = value.south;
  8114. array[startingIndex++] = value.east;
  8115. array[startingIndex] = value.north;
  8116. return array;
  8117. };
  8118. /**
  8119. * Retrieves an instance from a packed array.
  8120. *
  8121. * @param {Number[]} array The packed array.
  8122. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  8123. * @param {Rectangle} [result] The object into which to store the result.
  8124. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  8125. */
  8126. Rectangle.unpack = function(array, startingIndex, result) {
  8127. if (!defined(array)) {
  8128. throw new DeveloperError('array is required');
  8129. }
  8130. startingIndex = defaultValue(startingIndex, 0);
  8131. if (!defined(result)) {
  8132. result = new Rectangle();
  8133. }
  8134. result.west = array[startingIndex++];
  8135. result.south = array[startingIndex++];
  8136. result.east = array[startingIndex++];
  8137. result.north = array[startingIndex];
  8138. return result;
  8139. };
  8140. /**
  8141. * Computes the width of a rectangle in radians.
  8142. * @param {Rectangle} rectangle The rectangle to compute the width of.
  8143. * @returns {Number} The width.
  8144. */
  8145. Rectangle.computeWidth = function(rectangle) {
  8146. if (!defined(rectangle)) {
  8147. throw new DeveloperError('rectangle is required.');
  8148. }
  8149. var east = rectangle.east;
  8150. var west = rectangle.west;
  8151. if (east < west) {
  8152. east += CesiumMath.TWO_PI;
  8153. }
  8154. return east - west;
  8155. };
  8156. /**
  8157. * Computes the height of a rectangle in radians.
  8158. * @param {Rectangle} rectangle The rectangle to compute the height of.
  8159. * @returns {Number} The height.
  8160. */
  8161. Rectangle.computeHeight = function(rectangle) {
  8162. if (!defined(rectangle)) {
  8163. throw new DeveloperError('rectangle is required.');
  8164. }
  8165. return rectangle.north - rectangle.south;
  8166. };
  8167. /**
  8168. * Creates an rectangle given the boundary longitude and latitude in degrees.
  8169. *
  8170. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  8171. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  8172. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  8173. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  8174. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  8175. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8176. *
  8177. * @example
  8178. * var rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  8179. */
  8180. Rectangle.fromDegrees = function(west, south, east, north, result) {
  8181. west = CesiumMath.toRadians(defaultValue(west, 0.0));
  8182. south = CesiumMath.toRadians(defaultValue(south, 0.0));
  8183. east = CesiumMath.toRadians(defaultValue(east, 0.0));
  8184. north = CesiumMath.toRadians(defaultValue(north, 0.0));
  8185. if (!defined(result)) {
  8186. return new Rectangle(west, south, east, north);
  8187. }
  8188. result.west = west;
  8189. result.south = south;
  8190. result.east = east;
  8191. result.north = north;
  8192. return result;
  8193. };
  8194. /**
  8195. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  8196. *
  8197. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  8198. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  8199. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8200. */
  8201. Rectangle.fromCartographicArray = function(cartographics, result) {
  8202. if (!defined(cartographics)) {
  8203. throw new DeveloperError('cartographics is required.');
  8204. }
  8205. var west = Number.MAX_VALUE;
  8206. var east = -Number.MAX_VALUE;
  8207. var westOverIDL = Number.MAX_VALUE;
  8208. var eastOverIDL = -Number.MAX_VALUE;
  8209. var south = Number.MAX_VALUE;
  8210. var north = -Number.MAX_VALUE;
  8211. for ( var i = 0, len = cartographics.length; i < len; i++) {
  8212. var position = cartographics[i];
  8213. west = Math.min(west, position.longitude);
  8214. east = Math.max(east, position.longitude);
  8215. south = Math.min(south, position.latitude);
  8216. north = Math.max(north, position.latitude);
  8217. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + CesiumMath.TWO_PI;
  8218. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  8219. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  8220. }
  8221. if(east - west > eastOverIDL - westOverIDL) {
  8222. west = westOverIDL;
  8223. east = eastOverIDL;
  8224. if (east > CesiumMath.PI) {
  8225. east = east - CesiumMath.TWO_PI;
  8226. }
  8227. if (west > CesiumMath.PI) {
  8228. west = west - CesiumMath.TWO_PI;
  8229. }
  8230. }
  8231. if (!defined(result)) {
  8232. return new Rectangle(west, south, east, north);
  8233. }
  8234. result.west = west;
  8235. result.south = south;
  8236. result.east = east;
  8237. result.north = north;
  8238. return result;
  8239. };
  8240. /**
  8241. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  8242. *
  8243. * @param {Cartesian[]} cartesians The list of Cartesian instances.
  8244. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  8245. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  8246. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8247. */
  8248. Rectangle.fromCartesianArray = function(cartesians, ellipsoid, result) {
  8249. if (!defined(cartesians)) {
  8250. throw new DeveloperError('cartesians is required.');
  8251. }
  8252. var west = Number.MAX_VALUE;
  8253. var east = -Number.MAX_VALUE;
  8254. var westOverIDL = Number.MAX_VALUE;
  8255. var eastOverIDL = -Number.MAX_VALUE;
  8256. var south = Number.MAX_VALUE;
  8257. var north = -Number.MAX_VALUE;
  8258. for ( var i = 0, len = cartesians.length; i < len; i++) {
  8259. var position = ellipsoid.cartesianToCartographic(cartesians[i]);
  8260. west = Math.min(west, position.longitude);
  8261. east = Math.max(east, position.longitude);
  8262. south = Math.min(south, position.latitude);
  8263. north = Math.max(north, position.latitude);
  8264. var lonAdjusted = position.longitude >= 0 ? position.longitude : position.longitude + CesiumMath.TWO_PI;
  8265. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  8266. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  8267. }
  8268. if(east - west > eastOverIDL - westOverIDL) {
  8269. west = westOverIDL;
  8270. east = eastOverIDL;
  8271. if (east > CesiumMath.PI) {
  8272. east = east - CesiumMath.TWO_PI;
  8273. }
  8274. if (west > CesiumMath.PI) {
  8275. west = west - CesiumMath.TWO_PI;
  8276. }
  8277. }
  8278. if (!defined(result)) {
  8279. return new Rectangle(west, south, east, north);
  8280. }
  8281. result.west = west;
  8282. result.south = south;
  8283. result.east = east;
  8284. result.north = north;
  8285. return result;
  8286. };
  8287. /**
  8288. * Duplicates an Rectangle.
  8289. *
  8290. * @param {Rectangle} rectangle The rectangle to clone.
  8291. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  8292. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  8293. */
  8294. Rectangle.clone = function(rectangle, result) {
  8295. if (!defined(rectangle)) {
  8296. return undefined;
  8297. }
  8298. if (!defined(result)) {
  8299. return new Rectangle(rectangle.west, rectangle.south, rectangle.east, rectangle.north);
  8300. }
  8301. result.west = rectangle.west;
  8302. result.south = rectangle.south;
  8303. result.east = rectangle.east;
  8304. result.north = rectangle.north;
  8305. return result;
  8306. };
  8307. /**
  8308. * Duplicates this Rectangle.
  8309. *
  8310. * @param {Rectangle} [result] The object onto which to store the result.
  8311. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8312. */
  8313. Rectangle.prototype.clone = function(result) {
  8314. return Rectangle.clone(this, result);
  8315. };
  8316. /**
  8317. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8318. * <code>true</code> if they are equal, <code>false</code> otherwise.
  8319. *
  8320. * @param {Rectangle} [other] The Rectangle to compare.
  8321. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  8322. */
  8323. Rectangle.prototype.equals = function(other) {
  8324. return Rectangle.equals(this, other);
  8325. };
  8326. /**
  8327. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  8328. * <code>false</code> otherwise.
  8329. *
  8330. * @param {Rectangle} [left] The first Rectangle.
  8331. * @param {Rectangle} [right] The second Rectangle.
  8332. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  8333. */
  8334. Rectangle.equals = function(left, right) {
  8335. return (left === right) ||
  8336. ((defined(left)) &&
  8337. (defined(right)) &&
  8338. (left.west === right.west) &&
  8339. (left.south === right.south) &&
  8340. (left.east === right.east) &&
  8341. (left.north === right.north));
  8342. };
  8343. /**
  8344. * Compares the provided Rectangle with this Rectangle componentwise and returns
  8345. * <code>true</code> if they are within the provided epsilon,
  8346. * <code>false</code> otherwise.
  8347. *
  8348. * @param {Rectangle} [other] The Rectangle to compare.
  8349. * @param {Number} epsilon The epsilon to use for equality testing.
  8350. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  8351. */
  8352. Rectangle.prototype.equalsEpsilon = function(other, epsilon) {
  8353. if (typeof epsilon !== 'number') {
  8354. throw new DeveloperError('epsilon is required and must be a number.');
  8355. }
  8356. return defined(other) &&
  8357. (Math.abs(this.west - other.west) <= epsilon) &&
  8358. (Math.abs(this.south - other.south) <= epsilon) &&
  8359. (Math.abs(this.east - other.east) <= epsilon) &&
  8360. (Math.abs(this.north - other.north) <= epsilon);
  8361. };
  8362. /**
  8363. * Checks an Rectangle's properties and throws if they are not in valid ranges.
  8364. *
  8365. * @param {Rectangle} rectangle The rectangle to validate
  8366. *
  8367. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8368. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  8369. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8370. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  8371. */
  8372. Rectangle.validate = function(rectangle) {
  8373. if (!defined(rectangle)) {
  8374. throw new DeveloperError('rectangle is required');
  8375. }
  8376. var north = rectangle.north;
  8377. if (typeof north !== 'number') {
  8378. throw new DeveloperError('north is required to be a number.');
  8379. }
  8380. if (north < -CesiumMath.PI_OVER_TWO || north > CesiumMath.PI_OVER_TWO) {
  8381. throw new DeveloperError('north must be in the interval [-Pi/2, Pi/2].');
  8382. }
  8383. var south = rectangle.south;
  8384. if (typeof south !== 'number') {
  8385. throw new DeveloperError('south is required to be a number.');
  8386. }
  8387. if (south < -CesiumMath.PI_OVER_TWO || south > CesiumMath.PI_OVER_TWO) {
  8388. throw new DeveloperError('south must be in the interval [-Pi/2, Pi/2].');
  8389. }
  8390. var west = rectangle.west;
  8391. if (typeof west !== 'number') {
  8392. throw new DeveloperError('west is required to be a number.');
  8393. }
  8394. if (west < -Math.PI || west > Math.PI) {
  8395. throw new DeveloperError('west must be in the interval [-Pi, Pi].');
  8396. }
  8397. var east = rectangle.east;
  8398. if (typeof east !== 'number') {
  8399. throw new DeveloperError('east is required to be a number.');
  8400. }
  8401. if (east < -Math.PI || east > Math.PI) {
  8402. throw new DeveloperError('east must be in the interval [-Pi, Pi].');
  8403. }
  8404. };
  8405. /**
  8406. * Computes the southwest corner of an rectangle.
  8407. *
  8408. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8409. * @param {Cartographic} [result] The object onto which to store the result.
  8410. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8411. */
  8412. Rectangle.southwest = function(rectangle, result) {
  8413. if (!defined(rectangle)) {
  8414. throw new DeveloperError('rectangle is required');
  8415. }
  8416. if (!defined(result)) {
  8417. return new Cartographic(rectangle.west, rectangle.south);
  8418. }
  8419. result.longitude = rectangle.west;
  8420. result.latitude = rectangle.south;
  8421. result.height = 0.0;
  8422. return result;
  8423. };
  8424. /**
  8425. * Computes the northwest corner of an rectangle.
  8426. *
  8427. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8428. * @param {Cartographic} [result] The object onto which to store the result.
  8429. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8430. */
  8431. Rectangle.northwest = function(rectangle, result) {
  8432. if (!defined(rectangle)) {
  8433. throw new DeveloperError('rectangle is required');
  8434. }
  8435. if (!defined(result)) {
  8436. return new Cartographic(rectangle.west, rectangle.north);
  8437. }
  8438. result.longitude = rectangle.west;
  8439. result.latitude = rectangle.north;
  8440. result.height = 0.0;
  8441. return result;
  8442. };
  8443. /**
  8444. * Computes the northeast corner of an rectangle.
  8445. *
  8446. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8447. * @param {Cartographic} [result] The object onto which to store the result.
  8448. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8449. */
  8450. Rectangle.northeast = function(rectangle, result) {
  8451. if (!defined(rectangle)) {
  8452. throw new DeveloperError('rectangle is required');
  8453. }
  8454. if (!defined(result)) {
  8455. return new Cartographic(rectangle.east, rectangle.north);
  8456. }
  8457. result.longitude = rectangle.east;
  8458. result.latitude = rectangle.north;
  8459. result.height = 0.0;
  8460. return result;
  8461. };
  8462. /**
  8463. * Computes the southeast corner of an rectangle.
  8464. *
  8465. * @param {Rectangle} rectangle The rectangle for which to find the corner
  8466. * @param {Cartographic} [result] The object onto which to store the result.
  8467. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8468. */
  8469. Rectangle.southeast = function(rectangle, result) {
  8470. if (!defined(rectangle)) {
  8471. throw new DeveloperError('rectangle is required');
  8472. }
  8473. if (!defined(result)) {
  8474. return new Cartographic(rectangle.east, rectangle.south);
  8475. }
  8476. result.longitude = rectangle.east;
  8477. result.latitude = rectangle.south;
  8478. result.height = 0.0;
  8479. return result;
  8480. };
  8481. /**
  8482. * Computes the center of an rectangle.
  8483. *
  8484. * @param {Rectangle} rectangle The rectangle for which to find the center
  8485. * @param {Cartographic} [result] The object onto which to store the result.
  8486. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  8487. */
  8488. Rectangle.center = function(rectangle, result) {
  8489. if (!defined(rectangle)) {
  8490. throw new DeveloperError('rectangle is required');
  8491. }
  8492. var east = rectangle.east;
  8493. var west = rectangle.west;
  8494. if (east < west) {
  8495. east += CesiumMath.TWO_PI;
  8496. }
  8497. var longitude = CesiumMath.negativePiToPi((west + east) * 0.5);
  8498. var latitude = (rectangle.south + rectangle.north) * 0.5;
  8499. if (!defined(result)) {
  8500. return new Cartographic(longitude, latitude);
  8501. }
  8502. result.longitude = longitude;
  8503. result.latitude = latitude;
  8504. result.height = 0.0;
  8505. return result;
  8506. };
  8507. /**
  8508. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  8509. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  8510. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  8511. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  8512. * coordinates, see {@link Rectangle.simpleIntersection}.
  8513. *
  8514. * @param {Rectangle} rectangle On rectangle to find an intersection
  8515. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8516. * @param {Rectangle} [result] The object onto which to store the result.
  8517. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8518. */
  8519. Rectangle.intersection = function(rectangle, otherRectangle, result) {
  8520. if (!defined(rectangle)) {
  8521. throw new DeveloperError('rectangle is required');
  8522. }
  8523. if (!defined(otherRectangle)) {
  8524. throw new DeveloperError('otherRectangle is required.');
  8525. }
  8526. var rectangleEast = rectangle.east;
  8527. var rectangleWest = rectangle.west;
  8528. var otherRectangleEast = otherRectangle.east;
  8529. var otherRectangleWest = otherRectangle.west;
  8530. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8531. rectangleEast += CesiumMath.TWO_PI;
  8532. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8533. otherRectangleEast += CesiumMath.TWO_PI;
  8534. }
  8535. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8536. otherRectangleWest += CesiumMath.TWO_PI;
  8537. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8538. rectangleWest += CesiumMath.TWO_PI;
  8539. }
  8540. var west = CesiumMath.negativePiToPi(Math.max(rectangleWest, otherRectangleWest));
  8541. var east = CesiumMath.negativePiToPi(Math.min(rectangleEast, otherRectangleEast));
  8542. if ((rectangle.west < rectangle.east || otherRectangle.west < otherRectangle.east) && east <= west) {
  8543. return undefined;
  8544. }
  8545. var south = Math.max(rectangle.south, otherRectangle.south);
  8546. var north = Math.min(rectangle.north, otherRectangle.north);
  8547. if (south >= north) {
  8548. return undefined;
  8549. }
  8550. if (!defined(result)) {
  8551. return new Rectangle(west, south, east, north);
  8552. }
  8553. result.west = west;
  8554. result.south = south;
  8555. result.east = east;
  8556. result.north = north;
  8557. return result;
  8558. };
  8559. /**
  8560. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  8561. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  8562. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  8563. * and longitude (i.e. projected coordinates).
  8564. *
  8565. * @param {Rectangle} rectangle On rectangle to find an intersection
  8566. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  8567. * @param {Rectangle} [result] The object onto which to store the result.
  8568. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  8569. */
  8570. Rectangle.simpleIntersection = function(rectangle, otherRectangle, result) {
  8571. if (!defined(rectangle)) {
  8572. throw new DeveloperError('rectangle is required');
  8573. }
  8574. if (!defined(otherRectangle)) {
  8575. throw new DeveloperError('otherRectangle is required.');
  8576. }
  8577. var west = Math.max(rectangle.west, otherRectangle.west);
  8578. var south = Math.max(rectangle.south, otherRectangle.south);
  8579. var east = Math.min(rectangle.east, otherRectangle.east);
  8580. var north = Math.min(rectangle.north, otherRectangle.north);
  8581. if (south >= north || west >= east) {
  8582. return undefined;
  8583. }
  8584. if (!defined(result)) {
  8585. return new Rectangle(west, south, east, north);
  8586. }
  8587. result.west = west;
  8588. result.south = south;
  8589. result.east = east;
  8590. result.north = north;
  8591. return result;
  8592. };
  8593. /**
  8594. * Computes a rectangle that is the union of two rectangles.
  8595. *
  8596. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  8597. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  8598. * @param {Rectangle} [result] The object onto which to store the result.
  8599. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  8600. */
  8601. Rectangle.union = function(rectangle, otherRectangle, result) {
  8602. if (!defined(rectangle)) {
  8603. throw new DeveloperError('rectangle is required');
  8604. }
  8605. if (!defined(otherRectangle)) {
  8606. throw new DeveloperError('otherRectangle is required.');
  8607. }
  8608. if (!defined(result)) {
  8609. result = new Rectangle();
  8610. }
  8611. var rectangleEast = rectangle.east;
  8612. var rectangleWest = rectangle.west;
  8613. var otherRectangleEast = otherRectangle.east;
  8614. var otherRectangleWest = otherRectangle.west;
  8615. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  8616. rectangleEast += CesiumMath.TWO_PI;
  8617. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  8618. otherRectangleEast += CesiumMath.TWO_PI;
  8619. }
  8620. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  8621. otherRectangleWest += CesiumMath.TWO_PI;
  8622. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  8623. rectangleWest += CesiumMath.TWO_PI;
  8624. }
  8625. var west = CesiumMath.convertLongitudeRange(Math.min(rectangleWest, otherRectangleWest));
  8626. var east = CesiumMath.convertLongitudeRange(Math.max(rectangleEast, otherRectangleEast));
  8627. result.west = west;
  8628. result.south = Math.min(rectangle.south, otherRectangle.south);
  8629. result.east = east;
  8630. result.north = Math.max(rectangle.north, otherRectangle.north);
  8631. return result;
  8632. };
  8633. /**
  8634. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  8635. *
  8636. * @param {Rectangle} rectangle A rectangle to expand.
  8637. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  8638. * @param {Rectangle} [result] The object onto which to store the result.
  8639. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  8640. */
  8641. Rectangle.expand = function(rectangle, cartographic, result) {
  8642. if (!defined(rectangle)) {
  8643. throw new DeveloperError('rectangle is required.');
  8644. }
  8645. if (!defined(cartographic)) {
  8646. throw new DeveloperError('cartographic is required.');
  8647. }
  8648. if (!defined(result)) {
  8649. result = new Rectangle();
  8650. }
  8651. result.west = Math.min(rectangle.west, cartographic.longitude);
  8652. result.south = Math.min(rectangle.south, cartographic.latitude);
  8653. result.east = Math.max(rectangle.east, cartographic.longitude);
  8654. result.north = Math.max(rectangle.north, cartographic.latitude);
  8655. return result;
  8656. };
  8657. /**
  8658. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  8659. *
  8660. * @param {Rectangle} rectangle The rectangle
  8661. * @param {Cartographic} cartographic The cartographic to test.
  8662. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  8663. */
  8664. Rectangle.contains = function(rectangle, cartographic) {
  8665. if (!defined(rectangle)) {
  8666. throw new DeveloperError('rectangle is required');
  8667. }
  8668. if (!defined(cartographic)) {
  8669. throw new DeveloperError('cartographic is required.');
  8670. }
  8671. var longitude = cartographic.longitude;
  8672. var latitude = cartographic.latitude;
  8673. var west = rectangle.west;
  8674. var east = rectangle.east;
  8675. if (east < west) {
  8676. east += CesiumMath.TWO_PI;
  8677. if (longitude < 0.0) {
  8678. longitude += CesiumMath.TWO_PI;
  8679. }
  8680. }
  8681. return (longitude > west || CesiumMath.equalsEpsilon(longitude, west, CesiumMath.EPSILON14)) &&
  8682. (longitude < east || CesiumMath.equalsEpsilon(longitude, east, CesiumMath.EPSILON14)) &&
  8683. latitude >= rectangle.south &&
  8684. latitude <= rectangle.north;
  8685. };
  8686. var subsampleLlaScratch = new Cartographic();
  8687. /**
  8688. * Samples an rectangle so that it includes a list of Cartesian points suitable for passing to
  8689. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  8690. * for rectangles that cover the poles or cross the equator.
  8691. *
  8692. * @param {Rectangle} rectangle The rectangle to subsample.
  8693. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  8694. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  8695. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  8696. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  8697. */
  8698. Rectangle.subsample = function(rectangle, ellipsoid, surfaceHeight, result) {
  8699. if (!defined(rectangle)) {
  8700. throw new DeveloperError('rectangle is required');
  8701. }
  8702. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  8703. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  8704. if (!defined(result)) {
  8705. result = [];
  8706. }
  8707. var length = 0;
  8708. var north = rectangle.north;
  8709. var south = rectangle.south;
  8710. var east = rectangle.east;
  8711. var west = rectangle.west;
  8712. var lla = subsampleLlaScratch;
  8713. lla.height = surfaceHeight;
  8714. lla.longitude = west;
  8715. lla.latitude = north;
  8716. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8717. length++;
  8718. lla.longitude = east;
  8719. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8720. length++;
  8721. lla.latitude = south;
  8722. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8723. length++;
  8724. lla.longitude = west;
  8725. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8726. length++;
  8727. if (north < 0.0) {
  8728. lla.latitude = north;
  8729. } else if (south > 0.0) {
  8730. lla.latitude = south;
  8731. } else {
  8732. lla.latitude = 0.0;
  8733. }
  8734. for ( var i = 1; i < 8; ++i) {
  8735. lla.longitude = -Math.PI + i * CesiumMath.PI_OVER_TWO;
  8736. if (Rectangle.contains(rectangle, lla)) {
  8737. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8738. length++;
  8739. }
  8740. }
  8741. if (lla.latitude === 0.0) {
  8742. lla.longitude = west;
  8743. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8744. length++;
  8745. lla.longitude = east;
  8746. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  8747. length++;
  8748. }
  8749. result.length = length;
  8750. return result;
  8751. };
  8752. /**
  8753. * The largest possible rectangle.
  8754. *
  8755. * @type {Rectangle}
  8756. * @constant
  8757. */
  8758. Rectangle.MAX_VALUE = freezeObject(new Rectangle(-Math.PI, -CesiumMath.PI_OVER_TWO, Math.PI, CesiumMath.PI_OVER_TWO));
  8759. return Rectangle;
  8760. });
  8761. /*global define*/
  8762. define('Core/BoundingSphere',[
  8763. './Cartesian3',
  8764. './Cartographic',
  8765. './defaultValue',
  8766. './defined',
  8767. './DeveloperError',
  8768. './Ellipsoid',
  8769. './GeographicProjection',
  8770. './Intersect',
  8771. './Interval',
  8772. './Matrix3',
  8773. './Matrix4',
  8774. './Rectangle'
  8775. ], function(
  8776. Cartesian3,
  8777. Cartographic,
  8778. defaultValue,
  8779. defined,
  8780. DeveloperError,
  8781. Ellipsoid,
  8782. GeographicProjection,
  8783. Intersect,
  8784. Interval,
  8785. Matrix3,
  8786. Matrix4,
  8787. Rectangle) {
  8788. 'use strict';
  8789. /**
  8790. * A bounding sphere with a center and a radius.
  8791. * @alias BoundingSphere
  8792. * @constructor
  8793. *
  8794. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  8795. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  8796. *
  8797. * @see AxisAlignedBoundingBox
  8798. * @see BoundingRectangle
  8799. * @see Packable
  8800. */
  8801. function BoundingSphere(center, radius) {
  8802. /**
  8803. * The center point of the sphere.
  8804. * @type {Cartesian3}
  8805. * @default {@link Cartesian3.ZERO}
  8806. */
  8807. this.center = Cartesian3.clone(defaultValue(center, Cartesian3.ZERO));
  8808. /**
  8809. * The radius of the sphere.
  8810. * @type {Number}
  8811. * @default 0.0
  8812. */
  8813. this.radius = defaultValue(radius, 0.0);
  8814. }
  8815. var fromPointsXMin = new Cartesian3();
  8816. var fromPointsYMin = new Cartesian3();
  8817. var fromPointsZMin = new Cartesian3();
  8818. var fromPointsXMax = new Cartesian3();
  8819. var fromPointsYMax = new Cartesian3();
  8820. var fromPointsZMax = new Cartesian3();
  8821. var fromPointsCurrentPos = new Cartesian3();
  8822. var fromPointsScratch = new Cartesian3();
  8823. var fromPointsRitterCenter = new Cartesian3();
  8824. var fromPointsMinBoxPt = new Cartesian3();
  8825. var fromPointsMaxBoxPt = new Cartesian3();
  8826. var fromPointsNaiveCenterScratch = new Cartesian3();
  8827. /**
  8828. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  8829. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  8830. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  8831. *
  8832. * @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.
  8833. * @param {BoundingSphere} [result] The object onto which to store the result.
  8834. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  8835. *
  8836. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  8837. */
  8838. BoundingSphere.fromPoints = function(positions, result) {
  8839. if (!defined(result)) {
  8840. result = new BoundingSphere();
  8841. }
  8842. if (!defined(positions) || positions.length === 0) {
  8843. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  8844. result.radius = 0.0;
  8845. return result;
  8846. }
  8847. var currentPos = Cartesian3.clone(positions[0], fromPointsCurrentPos);
  8848. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  8849. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  8850. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  8851. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  8852. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  8853. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  8854. var numPositions = positions.length;
  8855. for (var i = 1; i < numPositions; i++) {
  8856. Cartesian3.clone(positions[i], currentPos);
  8857. var x = currentPos.x;
  8858. var y = currentPos.y;
  8859. var z = currentPos.z;
  8860. // Store points containing the the smallest and largest components
  8861. if (x < xMin.x) {
  8862. Cartesian3.clone(currentPos, xMin);
  8863. }
  8864. if (x > xMax.x) {
  8865. Cartesian3.clone(currentPos, xMax);
  8866. }
  8867. if (y < yMin.y) {
  8868. Cartesian3.clone(currentPos, yMin);
  8869. }
  8870. if (y > yMax.y) {
  8871. Cartesian3.clone(currentPos, yMax);
  8872. }
  8873. if (z < zMin.z) {
  8874. Cartesian3.clone(currentPos, zMin);
  8875. }
  8876. if (z > zMax.z) {
  8877. Cartesian3.clone(currentPos, zMax);
  8878. }
  8879. }
  8880. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  8881. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  8882. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  8883. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  8884. // Set the diameter endpoints to the largest span.
  8885. var diameter1 = xMin;
  8886. var diameter2 = xMax;
  8887. var maxSpan = xSpan;
  8888. if (ySpan > maxSpan) {
  8889. maxSpan = ySpan;
  8890. diameter1 = yMin;
  8891. diameter2 = yMax;
  8892. }
  8893. if (zSpan > maxSpan) {
  8894. maxSpan = zSpan;
  8895. diameter1 = zMin;
  8896. diameter2 = zMax;
  8897. }
  8898. // Calculate the center of the initial sphere found by Ritter's algorithm
  8899. var ritterCenter = fromPointsRitterCenter;
  8900. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  8901. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  8902. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  8903. // Calculate the radius of the initial sphere found by Ritter's algorithm
  8904. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  8905. var ritterRadius = Math.sqrt(radiusSquared);
  8906. // Find the center of the sphere found using the Naive method.
  8907. var minBoxPt = fromPointsMinBoxPt;
  8908. minBoxPt.x = xMin.x;
  8909. minBoxPt.y = yMin.y;
  8910. minBoxPt.z = zMin.z;
  8911. var maxBoxPt = fromPointsMaxBoxPt;
  8912. maxBoxPt.x = xMax.x;
  8913. maxBoxPt.y = yMax.y;
  8914. maxBoxPt.z = zMax.z;
  8915. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  8916. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  8917. var naiveRadius = 0;
  8918. for (i = 0; i < numPositions; i++) {
  8919. Cartesian3.clone(positions[i], currentPos);
  8920. // Find the furthest point from the naive center to calculate the naive radius.
  8921. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  8922. if (r > naiveRadius) {
  8923. naiveRadius = r;
  8924. }
  8925. // Make adjustments to the Ritter Sphere to include all points.
  8926. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  8927. if (oldCenterToPointSquared > radiusSquared) {
  8928. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  8929. // Calculate new radius to include the point that lies outside
  8930. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  8931. radiusSquared = ritterRadius * ritterRadius;
  8932. // Calculate center of new Ritter sphere
  8933. var oldToNew = oldCenterToPoint - ritterRadius;
  8934. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  8935. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  8936. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  8937. }
  8938. }
  8939. if (ritterRadius < naiveRadius) {
  8940. Cartesian3.clone(ritterCenter, result.center);
  8941. result.radius = ritterRadius;
  8942. } else {
  8943. Cartesian3.clone(naiveCenter, result.center);
  8944. result.radius = naiveRadius;
  8945. }
  8946. return result;
  8947. };
  8948. var defaultProjection = new GeographicProjection();
  8949. var fromRectangle2DLowerLeft = new Cartesian3();
  8950. var fromRectangle2DUpperRight = new Cartesian3();
  8951. var fromRectangle2DSouthwest = new Cartographic();
  8952. var fromRectangle2DNortheast = new Cartographic();
  8953. /**
  8954. * Computes a bounding sphere from an rectangle projected in 2D.
  8955. *
  8956. * @param {Rectangle} rectangle The rectangle around which to create a bounding sphere.
  8957. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  8958. * @param {BoundingSphere} [result] The object onto which to store the result.
  8959. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  8960. */
  8961. BoundingSphere.fromRectangle2D = function(rectangle, projection, result) {
  8962. return BoundingSphere.fromRectangleWithHeights2D(rectangle, projection, 0.0, 0.0, result);
  8963. };
  8964. /**
  8965. * Computes a bounding sphere from an rectangle projected in 2D. The bounding sphere accounts for the
  8966. * object's minimum and maximum heights over the rectangle.
  8967. *
  8968. * @param {Rectangle} rectangle The rectangle around which to create a bounding sphere.
  8969. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  8970. * @param {Number} [minimumHeight=0.0] The minimum height over the rectangle.
  8971. * @param {Number} [maximumHeight=0.0] The maximum height over the rectangle.
  8972. * @param {BoundingSphere} [result] The object onto which to store the result.
  8973. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  8974. */
  8975. BoundingSphere.fromRectangleWithHeights2D = function(rectangle, projection, minimumHeight, maximumHeight, result) {
  8976. if (!defined(result)) {
  8977. result = new BoundingSphere();
  8978. }
  8979. if (!defined(rectangle)) {
  8980. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  8981. result.radius = 0.0;
  8982. return result;
  8983. }
  8984. projection = defaultValue(projection, defaultProjection);
  8985. Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  8986. fromRectangle2DSouthwest.height = minimumHeight;
  8987. Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  8988. fromRectangle2DNortheast.height = maximumHeight;
  8989. var lowerLeft = projection.project(fromRectangle2DSouthwest, fromRectangle2DLowerLeft);
  8990. var upperRight = projection.project(fromRectangle2DNortheast, fromRectangle2DUpperRight);
  8991. var width = upperRight.x - lowerLeft.x;
  8992. var height = upperRight.y - lowerLeft.y;
  8993. var elevation = upperRight.z - lowerLeft.z;
  8994. result.radius = Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  8995. var center = result.center;
  8996. center.x = lowerLeft.x + width * 0.5;
  8997. center.y = lowerLeft.y + height * 0.5;
  8998. center.z = lowerLeft.z + elevation * 0.5;
  8999. return result;
  9000. };
  9001. var fromRectangle3DScratch = [];
  9002. /**
  9003. * Computes a bounding sphere from an rectangle in 3D. The bounding sphere is created using a subsample of points
  9004. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  9005. *
  9006. * @param {Rectangle} rectangle The valid rectangle used to create a bounding sphere.
  9007. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  9008. * @param {Number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  9009. * @param {BoundingSphere} [result] The object onto which to store the result.
  9010. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9011. */
  9012. BoundingSphere.fromRectangle3D = function(rectangle, ellipsoid, surfaceHeight, result) {
  9013. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  9014. surfaceHeight = defaultValue(surfaceHeight, 0.0);
  9015. var positions;
  9016. if (defined(rectangle)) {
  9017. positions = Rectangle.subsample(rectangle, ellipsoid, surfaceHeight, fromRectangle3DScratch);
  9018. }
  9019. return BoundingSphere.fromPoints(positions, result);
  9020. };
  9021. /**
  9022. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  9023. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  9024. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  9025. * ensure a tight fit.
  9026. *
  9027. * @param {Number[]} positions An array of points that the bounding sphere will enclose. Each point
  9028. * is formed from three elements in the array in the order X, Y, Z.
  9029. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  9030. * origin of the coordinate system. This is useful when the positions are to be used for
  9031. * relative-to-center (RTC) rendering.
  9032. * @param {Number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  9033. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  9034. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  9035. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  9036. * the stride is 5, however, two array elements are skipped and the next position begins at array
  9037. * index 5.
  9038. * @param {BoundingSphere} [result] The object onto which to store the result.
  9039. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9040. *
  9041. * @example
  9042. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  9043. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  9044. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  9045. * var center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  9046. * var points = [1.0, 2.0, 3.0, 0.1, 0.2,
  9047. * 4.0, 5.0, 6.0, 0.1, 0.2,
  9048. * 7.0, 8.0, 9.0, 0.1, 0.2];
  9049. * var sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  9050. *
  9051. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  9052. */
  9053. BoundingSphere.fromVertices = function(positions, center, stride, result) {
  9054. if (!defined(result)) {
  9055. result = new BoundingSphere();
  9056. }
  9057. if (!defined(positions) || positions.length === 0) {
  9058. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9059. result.radius = 0.0;
  9060. return result;
  9061. }
  9062. center = defaultValue(center, Cartesian3.ZERO);
  9063. stride = defaultValue(stride, 3);
  9064. if (stride < 3) {
  9065. throw new DeveloperError('stride must be 3 or greater.');
  9066. }
  9067. var currentPos = fromPointsCurrentPos;
  9068. currentPos.x = positions[0] + center.x;
  9069. currentPos.y = positions[1] + center.y;
  9070. currentPos.z = positions[2] + center.z;
  9071. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  9072. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  9073. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  9074. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  9075. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  9076. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  9077. var numElements = positions.length;
  9078. for (var i = 0; i < numElements; i += stride) {
  9079. var x = positions[i] + center.x;
  9080. var y = positions[i + 1] + center.y;
  9081. var z = positions[i + 2] + center.z;
  9082. currentPos.x = x;
  9083. currentPos.y = y;
  9084. currentPos.z = z;
  9085. // Store points containing the the smallest and largest components
  9086. if (x < xMin.x) {
  9087. Cartesian3.clone(currentPos, xMin);
  9088. }
  9089. if (x > xMax.x) {
  9090. Cartesian3.clone(currentPos, xMax);
  9091. }
  9092. if (y < yMin.y) {
  9093. Cartesian3.clone(currentPos, yMin);
  9094. }
  9095. if (y > yMax.y) {
  9096. Cartesian3.clone(currentPos, yMax);
  9097. }
  9098. if (z < zMin.z) {
  9099. Cartesian3.clone(currentPos, zMin);
  9100. }
  9101. if (z > zMax.z) {
  9102. Cartesian3.clone(currentPos, zMax);
  9103. }
  9104. }
  9105. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  9106. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  9107. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  9108. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  9109. // Set the diameter endpoints to the largest span.
  9110. var diameter1 = xMin;
  9111. var diameter2 = xMax;
  9112. var maxSpan = xSpan;
  9113. if (ySpan > maxSpan) {
  9114. maxSpan = ySpan;
  9115. diameter1 = yMin;
  9116. diameter2 = yMax;
  9117. }
  9118. if (zSpan > maxSpan) {
  9119. maxSpan = zSpan;
  9120. diameter1 = zMin;
  9121. diameter2 = zMax;
  9122. }
  9123. // Calculate the center of the initial sphere found by Ritter's algorithm
  9124. var ritterCenter = fromPointsRitterCenter;
  9125. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  9126. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  9127. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  9128. // Calculate the radius of the initial sphere found by Ritter's algorithm
  9129. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  9130. var ritterRadius = Math.sqrt(radiusSquared);
  9131. // Find the center of the sphere found using the Naive method.
  9132. var minBoxPt = fromPointsMinBoxPt;
  9133. minBoxPt.x = xMin.x;
  9134. minBoxPt.y = yMin.y;
  9135. minBoxPt.z = zMin.z;
  9136. var maxBoxPt = fromPointsMaxBoxPt;
  9137. maxBoxPt.x = xMax.x;
  9138. maxBoxPt.y = yMax.y;
  9139. maxBoxPt.z = zMax.z;
  9140. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  9141. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  9142. var naiveRadius = 0;
  9143. for (i = 0; i < numElements; i += stride) {
  9144. currentPos.x = positions[i] + center.x;
  9145. currentPos.y = positions[i + 1] + center.y;
  9146. currentPos.z = positions[i + 2] + center.z;
  9147. // Find the furthest point from the naive center to calculate the naive radius.
  9148. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  9149. if (r > naiveRadius) {
  9150. naiveRadius = r;
  9151. }
  9152. // Make adjustments to the Ritter Sphere to include all points.
  9153. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  9154. if (oldCenterToPointSquared > radiusSquared) {
  9155. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  9156. // Calculate new radius to include the point that lies outside
  9157. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  9158. radiusSquared = ritterRadius * ritterRadius;
  9159. // Calculate center of new Ritter sphere
  9160. var oldToNew = oldCenterToPoint - ritterRadius;
  9161. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  9162. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  9163. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  9164. }
  9165. }
  9166. if (ritterRadius < naiveRadius) {
  9167. Cartesian3.clone(ritterCenter, result.center);
  9168. result.radius = ritterRadius;
  9169. } else {
  9170. Cartesian3.clone(naiveCenter, result.center);
  9171. result.radius = naiveRadius;
  9172. }
  9173. return result;
  9174. };
  9175. /**
  9176. * Computes a tight-fitting bounding sphere enclosing a list of {@link EncodedCartesian3}s, where the points are
  9177. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  9178. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  9179. * ensure a tight fit.
  9180. *
  9181. * @param {Number[]} positionsHigh An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  9182. * is formed from three elements in the array in the order X, Y, Z.
  9183. * @param {Number[]} positionsLow An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  9184. * is formed from three elements in the array in the order X, Y, Z.
  9185. * @param {BoundingSphere} [result] The object onto which to store the result.
  9186. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9187. *
  9188. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  9189. */
  9190. BoundingSphere.fromEncodedCartesianVertices = function(positionsHigh, positionsLow, result) {
  9191. if (!defined(result)) {
  9192. result = new BoundingSphere();
  9193. }
  9194. if (!defined(positionsHigh) || !defined(positionsLow) || positionsHigh.length !== positionsLow.length || positionsHigh.length === 0) {
  9195. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9196. result.radius = 0.0;
  9197. return result;
  9198. }
  9199. var currentPos = fromPointsCurrentPos;
  9200. currentPos.x = positionsHigh[0] + positionsLow[0];
  9201. currentPos.y = positionsHigh[1] + positionsLow[1];
  9202. currentPos.z = positionsHigh[2] + positionsLow[2];
  9203. var xMin = Cartesian3.clone(currentPos, fromPointsXMin);
  9204. var yMin = Cartesian3.clone(currentPos, fromPointsYMin);
  9205. var zMin = Cartesian3.clone(currentPos, fromPointsZMin);
  9206. var xMax = Cartesian3.clone(currentPos, fromPointsXMax);
  9207. var yMax = Cartesian3.clone(currentPos, fromPointsYMax);
  9208. var zMax = Cartesian3.clone(currentPos, fromPointsZMax);
  9209. var numElements = positionsHigh.length;
  9210. for (var i = 0; i < numElements; i += 3) {
  9211. var x = positionsHigh[i] + positionsLow[i];
  9212. var y = positionsHigh[i + 1] + positionsLow[i + 1];
  9213. var z = positionsHigh[i + 2] + positionsLow[i + 2];
  9214. currentPos.x = x;
  9215. currentPos.y = y;
  9216. currentPos.z = z;
  9217. // Store points containing the the smallest and largest components
  9218. if (x < xMin.x) {
  9219. Cartesian3.clone(currentPos, xMin);
  9220. }
  9221. if (x > xMax.x) {
  9222. Cartesian3.clone(currentPos, xMax);
  9223. }
  9224. if (y < yMin.y) {
  9225. Cartesian3.clone(currentPos, yMin);
  9226. }
  9227. if (y > yMax.y) {
  9228. Cartesian3.clone(currentPos, yMax);
  9229. }
  9230. if (z < zMin.z) {
  9231. Cartesian3.clone(currentPos, zMin);
  9232. }
  9233. if (z > zMax.z) {
  9234. Cartesian3.clone(currentPos, zMax);
  9235. }
  9236. }
  9237. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  9238. var xSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  9239. var ySpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  9240. var zSpan = Cartesian3.magnitudeSquared(Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  9241. // Set the diameter endpoints to the largest span.
  9242. var diameter1 = xMin;
  9243. var diameter2 = xMax;
  9244. var maxSpan = xSpan;
  9245. if (ySpan > maxSpan) {
  9246. maxSpan = ySpan;
  9247. diameter1 = yMin;
  9248. diameter2 = yMax;
  9249. }
  9250. if (zSpan > maxSpan) {
  9251. maxSpan = zSpan;
  9252. diameter1 = zMin;
  9253. diameter2 = zMax;
  9254. }
  9255. // Calculate the center of the initial sphere found by Ritter's algorithm
  9256. var ritterCenter = fromPointsRitterCenter;
  9257. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  9258. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  9259. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  9260. // Calculate the radius of the initial sphere found by Ritter's algorithm
  9261. var radiusSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  9262. var ritterRadius = Math.sqrt(radiusSquared);
  9263. // Find the center of the sphere found using the Naive method.
  9264. var minBoxPt = fromPointsMinBoxPt;
  9265. minBoxPt.x = xMin.x;
  9266. minBoxPt.y = yMin.y;
  9267. minBoxPt.z = zMin.z;
  9268. var maxBoxPt = fromPointsMaxBoxPt;
  9269. maxBoxPt.x = xMax.x;
  9270. maxBoxPt.y = yMax.y;
  9271. maxBoxPt.z = zMax.z;
  9272. var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
  9273. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  9274. var naiveRadius = 0;
  9275. for (i = 0; i < numElements; i += 3) {
  9276. currentPos.x = positionsHigh[i] + positionsLow[i];
  9277. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  9278. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  9279. // Find the furthest point from the naive center to calculate the naive radius.
  9280. var r = Cartesian3.magnitude(Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  9281. if (r > naiveRadius) {
  9282. naiveRadius = r;
  9283. }
  9284. // Make adjustments to the Ritter Sphere to include all points.
  9285. var oldCenterToPointSquared = Cartesian3.magnitudeSquared(Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  9286. if (oldCenterToPointSquared > radiusSquared) {
  9287. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  9288. // Calculate new radius to include the point that lies outside
  9289. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  9290. radiusSquared = ritterRadius * ritterRadius;
  9291. // Calculate center of new Ritter sphere
  9292. var oldToNew = oldCenterToPoint - ritterRadius;
  9293. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  9294. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  9295. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  9296. }
  9297. }
  9298. if (ritterRadius < naiveRadius) {
  9299. Cartesian3.clone(ritterCenter, result.center);
  9300. result.radius = ritterRadius;
  9301. } else {
  9302. Cartesian3.clone(naiveCenter, result.center);
  9303. result.radius = naiveRadius;
  9304. }
  9305. return result;
  9306. };
  9307. /**
  9308. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  9309. * tighly and fully encompases the box.
  9310. *
  9311. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  9312. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  9313. * @param {BoundingSphere} [result] The object onto which to store the result.
  9314. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9315. *
  9316. * @example
  9317. * // Create a bounding sphere around the unit cube
  9318. * var sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  9319. */
  9320. BoundingSphere.fromCornerPoints = function(corner, oppositeCorner, result) {
  9321. if (!defined(corner) || !defined(oppositeCorner)) {
  9322. throw new DeveloperError('corner and oppositeCorner are required.');
  9323. }
  9324. if (!defined(result)) {
  9325. result = new BoundingSphere();
  9326. }
  9327. var center = result.center;
  9328. Cartesian3.add(corner, oppositeCorner, center);
  9329. Cartesian3.multiplyByScalar(center, 0.5, center);
  9330. result.radius = Cartesian3.distance(center, oppositeCorner);
  9331. return result;
  9332. };
  9333. /**
  9334. * Creates a bounding sphere encompassing an ellipsoid.
  9335. *
  9336. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  9337. * @param {BoundingSphere} [result] The object onto which to store the result.
  9338. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9339. *
  9340. * @example
  9341. * var boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  9342. */
  9343. BoundingSphere.fromEllipsoid = function(ellipsoid, result) {
  9344. if (!defined(ellipsoid)) {
  9345. throw new DeveloperError('ellipsoid is required.');
  9346. }
  9347. if (!defined(result)) {
  9348. result = new BoundingSphere();
  9349. }
  9350. Cartesian3.clone(Cartesian3.ZERO, result.center);
  9351. result.radius = ellipsoid.maximumRadius;
  9352. return result;
  9353. };
  9354. var fromBoundingSpheresScratch = new Cartesian3();
  9355. /**
  9356. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  9357. *
  9358. * @param {BoundingSphere[]} boundingSpheres The array of bounding spheres.
  9359. * @param {BoundingSphere} [result] The object onto which to store the result.
  9360. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9361. */
  9362. BoundingSphere.fromBoundingSpheres = function(boundingSpheres, result) {
  9363. if (!defined(result)) {
  9364. result = new BoundingSphere();
  9365. }
  9366. if (!defined(boundingSpheres) || boundingSpheres.length === 0) {
  9367. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  9368. result.radius = 0.0;
  9369. return result;
  9370. }
  9371. var length = boundingSpheres.length;
  9372. if (length === 1) {
  9373. return BoundingSphere.clone(boundingSpheres[0], result);
  9374. }
  9375. if (length === 2) {
  9376. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  9377. }
  9378. var positions = [];
  9379. for (var i = 0; i < length; i++) {
  9380. positions.push(boundingSpheres[i].center);
  9381. }
  9382. result = BoundingSphere.fromPoints(positions, result);
  9383. var center = result.center;
  9384. var radius = result.radius;
  9385. for (i = 0; i < length; i++) {
  9386. var tmp = boundingSpheres[i];
  9387. radius = Math.max(radius, Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) + tmp.radius);
  9388. }
  9389. result.radius = radius;
  9390. return result;
  9391. };
  9392. var fromOrientedBoundingBoxScratchU = new Cartesian3();
  9393. var fromOrientedBoundingBoxScratchV = new Cartesian3();
  9394. var fromOrientedBoundingBoxScratchW = new Cartesian3();
  9395. /**
  9396. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  9397. *
  9398. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  9399. * @param {BoundingSphere} [result] The object onto which to store the result.
  9400. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9401. */
  9402. BoundingSphere.fromOrientedBoundingBox = function(orientedBoundingBox, result) {
  9403. if (!defined(result)) {
  9404. result = new BoundingSphere();
  9405. }
  9406. var halfAxes = orientedBoundingBox.halfAxes;
  9407. var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  9408. var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  9409. var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  9410. var uHalf = Cartesian3.magnitude(u);
  9411. var vHalf = Cartesian3.magnitude(v);
  9412. var wHalf = Cartesian3.magnitude(w);
  9413. result.center = Cartesian3.clone(orientedBoundingBox.center, result.center);
  9414. result.radius = Math.max(uHalf, vHalf, wHalf);
  9415. return result;
  9416. };
  9417. /**
  9418. * Duplicates a BoundingSphere instance.
  9419. *
  9420. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  9421. * @param {BoundingSphere} [result] The object onto which to store the result.
  9422. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  9423. */
  9424. BoundingSphere.clone = function(sphere, result) {
  9425. if (!defined(sphere)) {
  9426. return undefined;
  9427. }
  9428. if (!defined(result)) {
  9429. return new BoundingSphere(sphere.center, sphere.radius);
  9430. }
  9431. result.center = Cartesian3.clone(sphere.center, result.center);
  9432. result.radius = sphere.radius;
  9433. return result;
  9434. };
  9435. /**
  9436. * The number of elements used to pack the object into an array.
  9437. * @type {Number}
  9438. */
  9439. BoundingSphere.packedLength = 4;
  9440. /**
  9441. * Stores the provided instance into the provided array.
  9442. *
  9443. * @param {BoundingSphere} value The value to pack.
  9444. * @param {Number[]} array The array to pack into.
  9445. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  9446. *
  9447. * @returns {Number[]} The array that was packed into
  9448. */
  9449. BoundingSphere.pack = function(value, array, startingIndex) {
  9450. if (!defined(value)) {
  9451. throw new DeveloperError('value is required');
  9452. }
  9453. if (!defined(array)) {
  9454. throw new DeveloperError('array is required');
  9455. }
  9456. startingIndex = defaultValue(startingIndex, 0);
  9457. var center = value.center;
  9458. array[startingIndex++] = center.x;
  9459. array[startingIndex++] = center.y;
  9460. array[startingIndex++] = center.z;
  9461. array[startingIndex] = value.radius;
  9462. return array;
  9463. };
  9464. /**
  9465. * Retrieves an instance from a packed array.
  9466. *
  9467. * @param {Number[]} array The packed array.
  9468. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  9469. * @param {BoundingSphere} [result] The object into which to store the result.
  9470. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  9471. */
  9472. BoundingSphere.unpack = function(array, startingIndex, result) {
  9473. if (!defined(array)) {
  9474. throw new DeveloperError('array is required');
  9475. }
  9476. startingIndex = defaultValue(startingIndex, 0);
  9477. if (!defined(result)) {
  9478. result = new BoundingSphere();
  9479. }
  9480. var center = result.center;
  9481. center.x = array[startingIndex++];
  9482. center.y = array[startingIndex++];
  9483. center.z = array[startingIndex++];
  9484. result.radius = array[startingIndex];
  9485. return result;
  9486. };
  9487. var unionScratch = new Cartesian3();
  9488. var unionScratchCenter = new Cartesian3();
  9489. /**
  9490. * Computes a bounding sphere that contains both the left and right bounding spheres.
  9491. *
  9492. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  9493. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  9494. * @param {BoundingSphere} [result] The object onto which to store the result.
  9495. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9496. */
  9497. BoundingSphere.union = function(left, right, result) {
  9498. if (!defined(left)) {
  9499. throw new DeveloperError('left is required.');
  9500. }
  9501. if (!defined(right)) {
  9502. throw new DeveloperError('right is required.');
  9503. }
  9504. if (!defined(result)) {
  9505. result = new BoundingSphere();
  9506. }
  9507. var leftCenter = left.center;
  9508. var leftRadius = left.radius;
  9509. var rightCenter = right.center;
  9510. var rightRadius = right.radius;
  9511. var toRightCenter = Cartesian3.subtract(rightCenter, leftCenter, unionScratch);
  9512. var centerSeparation = Cartesian3.magnitude(toRightCenter);
  9513. if (leftRadius >= (centerSeparation + rightRadius)) {
  9514. // Left sphere wins.
  9515. left.clone(result);
  9516. return result;
  9517. }
  9518. if (rightRadius >= (centerSeparation + leftRadius)) {
  9519. // Right sphere wins.
  9520. right.clone(result);
  9521. return result;
  9522. }
  9523. // There are two tangent points, one on far side of each sphere.
  9524. var halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
  9525. // Compute the center point halfway between the two tangent points.
  9526. var center = Cartesian3.multiplyByScalar(toRightCenter,
  9527. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation, unionScratchCenter);
  9528. Cartesian3.add(center, leftCenter, center);
  9529. Cartesian3.clone(center, result.center);
  9530. result.radius = halfDistanceBetweenTangentPoints;
  9531. return result;
  9532. };
  9533. var expandScratch = new Cartesian3();
  9534. /**
  9535. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  9536. *
  9537. * @param {BoundingSphere} sphere A sphere to expand.
  9538. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  9539. * @param {BoundingSphere} [result] The object onto which to store the result.
  9540. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9541. */
  9542. BoundingSphere.expand = function(sphere, point, result) {
  9543. if (!defined(sphere)) {
  9544. throw new DeveloperError('sphere is required.');
  9545. }
  9546. if (!defined(point)) {
  9547. throw new DeveloperError('point is required.');
  9548. }
  9549. result = BoundingSphere.clone(sphere, result);
  9550. var radius = Cartesian3.magnitude(Cartesian3.subtract(point, result.center, expandScratch));
  9551. if (radius > result.radius) {
  9552. result.radius = radius;
  9553. }
  9554. return result;
  9555. };
  9556. /**
  9557. * Determines which side of a plane a sphere is located.
  9558. *
  9559. * @param {BoundingSphere} sphere The bounding sphere to test.
  9560. * @param {Plane} plane The plane to test against.
  9561. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  9562. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  9563. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  9564. * intersects the plane.
  9565. */
  9566. BoundingSphere.intersectPlane = function(sphere, plane) {
  9567. if (!defined(sphere)) {
  9568. throw new DeveloperError('sphere is required.');
  9569. }
  9570. if (!defined(plane)) {
  9571. throw new DeveloperError('plane is required.');
  9572. }
  9573. var center = sphere.center;
  9574. var radius = sphere.radius;
  9575. var normal = plane.normal;
  9576. var distanceToPlane = Cartesian3.dot(normal, center) + plane.distance;
  9577. if (distanceToPlane < -radius) {
  9578. // The center point is negative side of the plane normal
  9579. return Intersect.OUTSIDE;
  9580. } else if (distanceToPlane < radius) {
  9581. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  9582. return Intersect.INTERSECTING;
  9583. }
  9584. return Intersect.INSIDE;
  9585. };
  9586. /**
  9587. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  9588. *
  9589. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  9590. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  9591. * @param {BoundingSphere} [result] The object onto which to store the result.
  9592. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9593. */
  9594. BoundingSphere.transform = function(sphere, transform, result) {
  9595. if (!defined(sphere)) {
  9596. throw new DeveloperError('sphere is required.');
  9597. }
  9598. if (!defined(transform)) {
  9599. throw new DeveloperError('transform is required.');
  9600. }
  9601. if (!defined(result)) {
  9602. result = new BoundingSphere();
  9603. }
  9604. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  9605. result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
  9606. return result;
  9607. };
  9608. var distanceSquaredToScratch = new Cartesian3();
  9609. /**
  9610. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  9611. *
  9612. * @param {BoundingSphere} sphere The sphere.
  9613. * @param {Cartesian3} cartesian The point
  9614. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  9615. *
  9616. * @example
  9617. * // Sort bounding spheres from back to front
  9618. * spheres.sort(function(a, b) {
  9619. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  9620. * });
  9621. */
  9622. BoundingSphere.distanceSquaredTo = function(sphere, cartesian) {
  9623. if (!defined(sphere)) {
  9624. throw new DeveloperError('sphere is required.');
  9625. }
  9626. if (!defined(cartesian)) {
  9627. throw new DeveloperError('cartesian is required.');
  9628. }
  9629. var diff = Cartesian3.subtract(sphere.center, cartesian, distanceSquaredToScratch);
  9630. return Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
  9631. };
  9632. /**
  9633. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  9634. * The transformation matrix is not verified to have a uniform scale of 1.
  9635. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  9636. *
  9637. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  9638. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  9639. * @param {BoundingSphere} [result] The object onto which to store the result.
  9640. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9641. *
  9642. * @example
  9643. * var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  9644. * var boundingSphere = new Cesium.BoundingSphere();
  9645. * var newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  9646. */
  9647. BoundingSphere.transformWithoutScale = function(sphere, transform, result) {
  9648. if (!defined(sphere)) {
  9649. throw new DeveloperError('sphere is required.');
  9650. }
  9651. if (!defined(transform)) {
  9652. throw new DeveloperError('transform is required.');
  9653. }
  9654. if (!defined(result)) {
  9655. result = new BoundingSphere();
  9656. }
  9657. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  9658. result.radius = sphere.radius;
  9659. return result;
  9660. };
  9661. var scratchCartesian3 = new Cartesian3();
  9662. /**
  9663. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  9664. * plus/minus the radius of the bounding sphere.
  9665. * <br>
  9666. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  9667. * closest and farthest planes from position that intersect the bounding sphere.
  9668. *
  9669. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  9670. * @param {Cartesian3} position The position to calculate the distance from.
  9671. * @param {Cartesian3} direction The direction from position.
  9672. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  9673. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  9674. */
  9675. BoundingSphere.computePlaneDistances = function(sphere, position, direction, result) {
  9676. if (!defined(sphere)) {
  9677. throw new DeveloperError('sphere is required.');
  9678. }
  9679. if (!defined(position)) {
  9680. throw new DeveloperError('position is required.');
  9681. }
  9682. if (!defined(direction)) {
  9683. throw new DeveloperError('direction is required.');
  9684. }
  9685. if (!defined(result)) {
  9686. result = new Interval();
  9687. }
  9688. var toCenter = Cartesian3.subtract(sphere.center, position, scratchCartesian3);
  9689. var mag = Cartesian3.dot(direction, toCenter);
  9690. result.start = mag - sphere.radius;
  9691. result.stop = mag + sphere.radius;
  9692. return result;
  9693. };
  9694. var projectTo2DNormalScratch = new Cartesian3();
  9695. var projectTo2DEastScratch = new Cartesian3();
  9696. var projectTo2DNorthScratch = new Cartesian3();
  9697. var projectTo2DWestScratch = new Cartesian3();
  9698. var projectTo2DSouthScratch = new Cartesian3();
  9699. var projectTo2DCartographicScratch = new Cartographic();
  9700. var projectTo2DPositionsScratch = new Array(8);
  9701. for (var n = 0; n < 8; ++n) {
  9702. projectTo2DPositionsScratch[n] = new Cartesian3();
  9703. }
  9704. var projectTo2DProjection = new GeographicProjection();
  9705. /**
  9706. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  9707. *
  9708. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  9709. * @param {Object} [projection=GeographicProjection] The projection to 2D.
  9710. * @param {BoundingSphere} [result] The object onto which to store the result.
  9711. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9712. */
  9713. BoundingSphere.projectTo2D = function(sphere, projection, result) {
  9714. if (!defined(sphere)) {
  9715. throw new DeveloperError('sphere is required.');
  9716. }
  9717. projection = defaultValue(projection, projectTo2DProjection);
  9718. var ellipsoid = projection.ellipsoid;
  9719. var center = sphere.center;
  9720. var radius = sphere.radius;
  9721. var normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  9722. var east = Cartesian3.cross(Cartesian3.UNIT_Z, normal, projectTo2DEastScratch);
  9723. Cartesian3.normalize(east, east);
  9724. var north = Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  9725. Cartesian3.normalize(north, north);
  9726. Cartesian3.multiplyByScalar(normal, radius, normal);
  9727. Cartesian3.multiplyByScalar(north, radius, north);
  9728. Cartesian3.multiplyByScalar(east, radius, east);
  9729. var south = Cartesian3.negate(north, projectTo2DSouthScratch);
  9730. var west = Cartesian3.negate(east, projectTo2DWestScratch);
  9731. var positions = projectTo2DPositionsScratch;
  9732. // top NE corner
  9733. var corner = positions[0];
  9734. Cartesian3.add(normal, north, corner);
  9735. Cartesian3.add(corner, east, corner);
  9736. // top NW corner
  9737. corner = positions[1];
  9738. Cartesian3.add(normal, north, corner);
  9739. Cartesian3.add(corner, west, corner);
  9740. // top SW corner
  9741. corner = positions[2];
  9742. Cartesian3.add(normal, south, corner);
  9743. Cartesian3.add(corner, west, corner);
  9744. // top SE corner
  9745. corner = positions[3];
  9746. Cartesian3.add(normal, south, corner);
  9747. Cartesian3.add(corner, east, corner);
  9748. Cartesian3.negate(normal, normal);
  9749. // bottom NE corner
  9750. corner = positions[4];
  9751. Cartesian3.add(normal, north, corner);
  9752. Cartesian3.add(corner, east, corner);
  9753. // bottom NW corner
  9754. corner = positions[5];
  9755. Cartesian3.add(normal, north, corner);
  9756. Cartesian3.add(corner, west, corner);
  9757. // bottom SW corner
  9758. corner = positions[6];
  9759. Cartesian3.add(normal, south, corner);
  9760. Cartesian3.add(corner, west, corner);
  9761. // bottom SE corner
  9762. corner = positions[7];
  9763. Cartesian3.add(normal, south, corner);
  9764. Cartesian3.add(corner, east, corner);
  9765. var length = positions.length;
  9766. for (var i = 0; i < length; ++i) {
  9767. var position = positions[i];
  9768. Cartesian3.add(center, position, position);
  9769. var cartographic = ellipsoid.cartesianToCartographic(position, projectTo2DCartographicScratch);
  9770. projection.project(cartographic, position);
  9771. }
  9772. result = BoundingSphere.fromPoints(positions, result);
  9773. // swizzle center components
  9774. center = result.center;
  9775. var x = center.x;
  9776. var y = center.y;
  9777. var z = center.z;
  9778. center.x = z;
  9779. center.y = x;
  9780. center.z = y;
  9781. return result;
  9782. };
  9783. /**
  9784. * Determines whether or not a sphere is hidden from view by the occluder.
  9785. *
  9786. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  9787. * @param {Occluder} occluder The occluder.
  9788. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  9789. */
  9790. BoundingSphere.isOccluded = function(sphere, occluder) {
  9791. if (!defined(sphere)) {
  9792. throw new DeveloperError('sphere is required.');
  9793. }
  9794. if (!defined(occluder)) {
  9795. throw new DeveloperError('occluder is required.');
  9796. }
  9797. return !occluder.isBoundingSphereVisible(sphere);
  9798. };
  9799. /**
  9800. * Compares the provided BoundingSphere componentwise and returns
  9801. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9802. *
  9803. * @param {BoundingSphere} [left] The first BoundingSphere.
  9804. * @param {BoundingSphere} [right] The second BoundingSphere.
  9805. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  9806. */
  9807. BoundingSphere.equals = function(left, right) {
  9808. return (left === right) ||
  9809. ((defined(left)) &&
  9810. (defined(right)) &&
  9811. Cartesian3.equals(left.center, right.center) &&
  9812. left.radius === right.radius);
  9813. };
  9814. /**
  9815. * Determines which side of a plane the sphere is located.
  9816. *
  9817. * @param {Plane} plane The plane to test against.
  9818. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  9819. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  9820. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  9821. * intersects the plane.
  9822. */
  9823. BoundingSphere.prototype.intersectPlane = function(plane) {
  9824. return BoundingSphere.intersectPlane(this, plane);
  9825. };
  9826. /**
  9827. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  9828. *
  9829. * @param {Cartesian3} cartesian The point
  9830. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  9831. *
  9832. * @example
  9833. * // Sort bounding spheres from back to front
  9834. * spheres.sort(function(a, b) {
  9835. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  9836. * });
  9837. */
  9838. BoundingSphere.prototype.distanceSquaredTo = function(cartesian) {
  9839. return BoundingSphere.distanceSquaredTo(this, cartesian);
  9840. };
  9841. /**
  9842. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  9843. * plus/minus the radius of the bounding sphere.
  9844. * <br>
  9845. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  9846. * closest and farthest planes from position that intersect the bounding sphere.
  9847. *
  9848. * @param {Cartesian3} position The position to calculate the distance from.
  9849. * @param {Cartesian3} direction The direction from position.
  9850. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  9851. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  9852. */
  9853. BoundingSphere.prototype.computePlaneDistances = function(position, direction, result) {
  9854. return BoundingSphere.computePlaneDistances(this, position, direction, result);
  9855. };
  9856. /**
  9857. * Determines whether or not a sphere is hidden from view by the occluder.
  9858. *
  9859. * @param {Occluder} occluder The occluder.
  9860. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  9861. */
  9862. BoundingSphere.prototype.isOccluded = function(occluder) {
  9863. return BoundingSphere.isOccluded(this, occluder);
  9864. };
  9865. /**
  9866. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  9867. * <code>true</code> if they are equal, <code>false</code> otherwise.
  9868. *
  9869. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  9870. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  9871. */
  9872. BoundingSphere.prototype.equals = function(right) {
  9873. return BoundingSphere.equals(this, right);
  9874. };
  9875. /**
  9876. * Duplicates this BoundingSphere instance.
  9877. *
  9878. * @param {BoundingSphere} [result] The object onto which to store the result.
  9879. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  9880. */
  9881. BoundingSphere.prototype.clone = function(result) {
  9882. return BoundingSphere.clone(this, result);
  9883. };
  9884. return BoundingSphere;
  9885. });
  9886. /*global define*/
  9887. define('Core/Fullscreen',[
  9888. './defined',
  9889. './defineProperties'
  9890. ], function(
  9891. defined,
  9892. defineProperties) {
  9893. 'use strict';
  9894. var _supportsFullscreen;
  9895. var _names = {
  9896. requestFullscreen : undefined,
  9897. exitFullscreen : undefined,
  9898. fullscreenEnabled : undefined,
  9899. fullscreenElement : undefined,
  9900. fullscreenchange : undefined,
  9901. fullscreenerror : undefined
  9902. };
  9903. /**
  9904. * Browser-independent functions for working with the standard fullscreen API.
  9905. *
  9906. * @exports Fullscreen
  9907. *
  9908. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  9909. */
  9910. var Fullscreen = {};
  9911. defineProperties(Fullscreen, {
  9912. /**
  9913. * The element that is currently fullscreen, if any. To simply check if the
  9914. * browser is in fullscreen mode or not, use {@link Fullscreen#fullscreen}.
  9915. * @memberof Fullscreen
  9916. * @type {Object}
  9917. * @readonly
  9918. */
  9919. element : {
  9920. get : function() {
  9921. if (!Fullscreen.supportsFullscreen()) {
  9922. return undefined;
  9923. }
  9924. return document[_names.fullscreenElement];
  9925. }
  9926. },
  9927. /**
  9928. * The name of the event on the document that is fired when fullscreen is
  9929. * entered or exited. This event name is intended for use with addEventListener.
  9930. * In your event handler, to determine if the browser is in fullscreen mode or not,
  9931. * use {@link Fullscreen#fullscreen}.
  9932. * @memberof Fullscreen
  9933. * @type {String}
  9934. * @readonly
  9935. */
  9936. changeEventName : {
  9937. get : function() {
  9938. if (!Fullscreen.supportsFullscreen()) {
  9939. return undefined;
  9940. }
  9941. return _names.fullscreenchange;
  9942. }
  9943. },
  9944. /**
  9945. * The name of the event that is fired when a fullscreen error
  9946. * occurs. This event name is intended for use with addEventListener.
  9947. * @memberof Fullscreen
  9948. * @type {String}
  9949. * @readonly
  9950. */
  9951. errorEventName : {
  9952. get : function() {
  9953. if (!Fullscreen.supportsFullscreen()) {
  9954. return undefined;
  9955. }
  9956. return _names.fullscreenerror;
  9957. }
  9958. },
  9959. /**
  9960. * Determine whether the browser will allow an element to be made fullscreen, or not.
  9961. * For example, by default, iframes cannot go fullscreen unless the containing page
  9962. * adds an "allowfullscreen" attribute (or prefixed equivalent).
  9963. * @memberof Fullscreen
  9964. * @type {Boolean}
  9965. * @readonly
  9966. */
  9967. enabled : {
  9968. get : function() {
  9969. if (!Fullscreen.supportsFullscreen()) {
  9970. return undefined;
  9971. }
  9972. return document[_names.fullscreenEnabled];
  9973. }
  9974. },
  9975. /**
  9976. * Determines if the browser is currently in fullscreen mode.
  9977. * @memberof Fullscreen
  9978. * @type {Boolean}
  9979. * @readonly
  9980. */
  9981. fullscreen : {
  9982. get : function() {
  9983. if (!Fullscreen.supportsFullscreen()) {
  9984. return undefined;
  9985. }
  9986. return Fullscreen.element !== null;
  9987. }
  9988. }
  9989. });
  9990. /**
  9991. * Detects whether the browser supports the standard fullscreen API.
  9992. *
  9993. * @returns {Boolean} <code>true</code> if the browser supports the standard fullscreen API,
  9994. * <code>false</code> otherwise.
  9995. */
  9996. Fullscreen.supportsFullscreen = function() {
  9997. if (defined(_supportsFullscreen)) {
  9998. return _supportsFullscreen;
  9999. }
  10000. _supportsFullscreen = false;
  10001. var body = document.body;
  10002. if (typeof body.requestFullscreen === 'function') {
  10003. // go with the unprefixed, standard set of names
  10004. _names.requestFullscreen = 'requestFullscreen';
  10005. _names.exitFullscreen = 'exitFullscreen';
  10006. _names.fullscreenEnabled = 'fullscreenEnabled';
  10007. _names.fullscreenElement = 'fullscreenElement';
  10008. _names.fullscreenchange = 'fullscreenchange';
  10009. _names.fullscreenerror = 'fullscreenerror';
  10010. _supportsFullscreen = true;
  10011. return _supportsFullscreen;
  10012. }
  10013. //check for the correct combination of prefix plus the various names that browsers use
  10014. var prefixes = ['webkit', 'moz', 'o', 'ms', 'khtml'];
  10015. var name;
  10016. for (var i = 0, len = prefixes.length; i < len; ++i) {
  10017. var prefix = prefixes[i];
  10018. // casing of Fullscreen differs across browsers
  10019. name = prefix + 'RequestFullscreen';
  10020. if (typeof body[name] === 'function') {
  10021. _names.requestFullscreen = name;
  10022. _supportsFullscreen = true;
  10023. } else {
  10024. name = prefix + 'RequestFullScreen';
  10025. if (typeof body[name] === 'function') {
  10026. _names.requestFullscreen = name;
  10027. _supportsFullscreen = true;
  10028. }
  10029. }
  10030. // disagreement about whether it's "exit" as per spec, or "cancel"
  10031. name = prefix + 'ExitFullscreen';
  10032. if (typeof document[name] === 'function') {
  10033. _names.exitFullscreen = name;
  10034. } else {
  10035. name = prefix + 'CancelFullScreen';
  10036. if (typeof document[name] === 'function') {
  10037. _names.exitFullscreen = name;
  10038. }
  10039. }
  10040. // casing of Fullscreen differs across browsers
  10041. name = prefix + 'FullscreenEnabled';
  10042. if (document[name] !== undefined) {
  10043. _names.fullscreenEnabled = name;
  10044. } else {
  10045. name = prefix + 'FullScreenEnabled';
  10046. if (document[name] !== undefined) {
  10047. _names.fullscreenEnabled = name;
  10048. }
  10049. }
  10050. // casing of Fullscreen differs across browsers
  10051. name = prefix + 'FullscreenElement';
  10052. if (document[name] !== undefined) {
  10053. _names.fullscreenElement = name;
  10054. } else {
  10055. name = prefix + 'FullScreenElement';
  10056. if (document[name] !== undefined) {
  10057. _names.fullscreenElement = name;
  10058. }
  10059. }
  10060. // thankfully, event names are all lowercase per spec
  10061. name = prefix + 'fullscreenchange';
  10062. // event names do not have 'on' in the front, but the property on the document does
  10063. if (document['on' + name] !== undefined) {
  10064. //except on IE
  10065. if (prefix === 'ms') {
  10066. name = 'MSFullscreenChange';
  10067. }
  10068. _names.fullscreenchange = name;
  10069. }
  10070. name = prefix + 'fullscreenerror';
  10071. if (document['on' + name] !== undefined) {
  10072. //except on IE
  10073. if (prefix === 'ms') {
  10074. name = 'MSFullscreenError';
  10075. }
  10076. _names.fullscreenerror = name;
  10077. }
  10078. }
  10079. return _supportsFullscreen;
  10080. };
  10081. /**
  10082. * Asynchronously requests the browser to enter fullscreen mode on the given element.
  10083. * If fullscreen mode is not supported by the browser, does nothing.
  10084. *
  10085. * @param {Object} element The HTML element which will be placed into fullscreen mode.
  10086. * @param {HMDVRDevice} [vrDevice] The VR device.
  10087. *
  10088. * @example
  10089. * // Put the entire page into fullscreen.
  10090. * Cesium.Fullscreen.requestFullscreen(document.body)
  10091. *
  10092. * // Place only the Cesium canvas into fullscreen.
  10093. * Cesium.Fullscreen.requestFullscreen(scene.canvas)
  10094. */
  10095. Fullscreen.requestFullscreen = function(element, vrDevice) {
  10096. if (!Fullscreen.supportsFullscreen()) {
  10097. return;
  10098. }
  10099. element[_names.requestFullscreen]({ vrDisplay: vrDevice });
  10100. };
  10101. /**
  10102. * Asynchronously exits fullscreen mode. If the browser is not currently
  10103. * in fullscreen, or if fullscreen mode is not supported by the browser, does nothing.
  10104. */
  10105. Fullscreen.exitFullscreen = function() {
  10106. if (!Fullscreen.supportsFullscreen()) {
  10107. return;
  10108. }
  10109. document[_names.exitFullscreen]();
  10110. };
  10111. return Fullscreen;
  10112. });
  10113. /*global define*/
  10114. define('Core/FeatureDetection',[
  10115. './defaultValue',
  10116. './defined',
  10117. './Fullscreen'
  10118. ], function(
  10119. defaultValue,
  10120. defined,
  10121. Fullscreen) {
  10122. 'use strict';
  10123. var theNavigator;
  10124. if (typeof navigator !== 'undefined') {
  10125. theNavigator = navigator;
  10126. } else {
  10127. theNavigator = {};
  10128. }
  10129. function extractVersion(versionString) {
  10130. var parts = versionString.split('.');
  10131. for (var i = 0, len = parts.length; i < len; ++i) {
  10132. parts[i] = parseInt(parts[i], 10);
  10133. }
  10134. return parts;
  10135. }
  10136. var isChromeResult;
  10137. var chromeVersionResult;
  10138. function isChrome() {
  10139. if (!defined(isChromeResult)) {
  10140. isChromeResult = false;
  10141. // Edge contains Chrome in the user agent too
  10142. if (!isEdge()) {
  10143. var fields = (/ Chrome\/([\.0-9]+)/).exec(theNavigator.userAgent);
  10144. if (fields !== null) {
  10145. isChromeResult = true;
  10146. chromeVersionResult = extractVersion(fields[1]);
  10147. }
  10148. }
  10149. }
  10150. return isChromeResult;
  10151. }
  10152. function chromeVersion() {
  10153. return isChrome() && chromeVersionResult;
  10154. }
  10155. var isSafariResult;
  10156. var safariVersionResult;
  10157. function isSafari() {
  10158. if (!defined(isSafariResult)) {
  10159. isSafariResult = false;
  10160. // Chrome and Edge contain Safari in the user agent too
  10161. if (!isChrome() && !isEdge() && (/ Safari\/[\.0-9]+/).test(theNavigator.userAgent)) {
  10162. var fields = (/ Version\/([\.0-9]+)/).exec(theNavigator.userAgent);
  10163. if (fields !== null) {
  10164. isSafariResult = true;
  10165. safariVersionResult = extractVersion(fields[1]);
  10166. }
  10167. }
  10168. }
  10169. return isSafariResult;
  10170. }
  10171. function safariVersion() {
  10172. return isSafari() && safariVersionResult;
  10173. }
  10174. var isWebkitResult;
  10175. var webkitVersionResult;
  10176. function isWebkit() {
  10177. if (!defined(isWebkitResult)) {
  10178. isWebkitResult = false;
  10179. var fields = (/ AppleWebKit\/([\.0-9]+)(\+?)/).exec(theNavigator.userAgent);
  10180. if (fields !== null) {
  10181. isWebkitResult = true;
  10182. webkitVersionResult = extractVersion(fields[1]);
  10183. webkitVersionResult.isNightly = !!fields[2];
  10184. }
  10185. }
  10186. return isWebkitResult;
  10187. }
  10188. function webkitVersion() {
  10189. return isWebkit() && webkitVersionResult;
  10190. }
  10191. var isInternetExplorerResult;
  10192. var internetExplorerVersionResult;
  10193. function isInternetExplorer() {
  10194. if (!defined(isInternetExplorerResult)) {
  10195. isInternetExplorerResult = false;
  10196. var fields;
  10197. if (theNavigator.appName === 'Microsoft Internet Explorer') {
  10198. fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  10199. if (fields !== null) {
  10200. isInternetExplorerResult = true;
  10201. internetExplorerVersionResult = extractVersion(fields[1]);
  10202. }
  10203. } else if (theNavigator.appName === 'Netscape') {
  10204. fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
  10205. if (fields !== null) {
  10206. isInternetExplorerResult = true;
  10207. internetExplorerVersionResult = extractVersion(fields[1]);
  10208. }
  10209. }
  10210. }
  10211. return isInternetExplorerResult;
  10212. }
  10213. function internetExplorerVersion() {
  10214. return isInternetExplorer() && internetExplorerVersionResult;
  10215. }
  10216. var isEdgeResult;
  10217. var edgeVersionResult;
  10218. function isEdge() {
  10219. if (!defined(isEdgeResult)) {
  10220. isEdgeResult = false;
  10221. var fields = (/ Edge\/([\.0-9]+)/).exec(theNavigator.userAgent);
  10222. if (fields !== null) {
  10223. isEdgeResult = true;
  10224. edgeVersionResult = extractVersion(fields[1]);
  10225. }
  10226. }
  10227. return isEdgeResult;
  10228. }
  10229. function edgeVersion() {
  10230. return isEdge() && edgeVersionResult;
  10231. }
  10232. var isFirefoxResult;
  10233. var firefoxVersionResult;
  10234. function isFirefox() {
  10235. if (!defined(isFirefoxResult)) {
  10236. isFirefoxResult = false;
  10237. var fields = /Firefox\/([\.0-9]+)/.exec(theNavigator.userAgent);
  10238. if (fields !== null) {
  10239. isFirefoxResult = true;
  10240. firefoxVersionResult = extractVersion(fields[1]);
  10241. }
  10242. }
  10243. return isFirefoxResult;
  10244. }
  10245. var isWindowsResult;
  10246. function isWindows() {
  10247. if (!defined(isWindowsResult)) {
  10248. isWindowsResult = /Windows/i.test(theNavigator.appVersion);
  10249. }
  10250. return isWindowsResult;
  10251. }
  10252. function firefoxVersion() {
  10253. return isFirefox() && firefoxVersionResult;
  10254. }
  10255. var hasPointerEvents;
  10256. function supportsPointerEvents() {
  10257. if (!defined(hasPointerEvents)) {
  10258. //While navigator.pointerEnabled is deprecated in the W3C specification
  10259. //we still need to use it if it exists in order to support browsers
  10260. //that rely on it, such as the Windows WebBrowser control which defines
  10261. //PointerEvent but sets navigator.pointerEnabled to false.
  10262. hasPointerEvents = typeof PointerEvent !== 'undefined' && (!defined(theNavigator.pointerEnabled) || theNavigator.pointerEnabled);
  10263. }
  10264. return hasPointerEvents;
  10265. }
  10266. var imageRenderingValueResult;
  10267. var supportsImageRenderingPixelatedResult;
  10268. function supportsImageRenderingPixelated() {
  10269. if (!defined(supportsImageRenderingPixelatedResult)) {
  10270. var canvas = document.createElement('canvas');
  10271. canvas.setAttribute('style',
  10272. 'image-rendering: -moz-crisp-edges;' +
  10273. 'image-rendering: pixelated;');
  10274. //canvas.style.imageRendering will be undefined, null or an empty string on unsupported browsers.
  10275. var tmp = canvas.style.imageRendering;
  10276. supportsImageRenderingPixelatedResult = defined(tmp) && tmp !== '';
  10277. if (supportsImageRenderingPixelatedResult) {
  10278. imageRenderingValueResult = tmp;
  10279. }
  10280. }
  10281. return supportsImageRenderingPixelatedResult;
  10282. }
  10283. function imageRenderingValue() {
  10284. return supportsImageRenderingPixelated() ? imageRenderingValueResult : undefined;
  10285. }
  10286. /**
  10287. * A set of functions to detect whether the current browser supports
  10288. * various features.
  10289. *
  10290. * @exports FeatureDetection
  10291. */
  10292. var FeatureDetection = {
  10293. isChrome : isChrome,
  10294. chromeVersion : chromeVersion,
  10295. isSafari : isSafari,
  10296. safariVersion : safariVersion,
  10297. isWebkit : isWebkit,
  10298. webkitVersion : webkitVersion,
  10299. isInternetExplorer : isInternetExplorer,
  10300. internetExplorerVersion : internetExplorerVersion,
  10301. isEdge : isEdge,
  10302. edgeVersion : edgeVersion,
  10303. isFirefox : isFirefox,
  10304. firefoxVersion : firefoxVersion,
  10305. isWindows : isWindows,
  10306. hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
  10307. supportsPointerEvents : supportsPointerEvents,
  10308. supportsImageRenderingPixelated: supportsImageRenderingPixelated,
  10309. imageRenderingValue: imageRenderingValue
  10310. };
  10311. /**
  10312. * Detects whether the current browser supports the full screen standard.
  10313. *
  10314. * @returns {Boolean} true if the browser supports the full screen standard, false if not.
  10315. *
  10316. * @see Fullscreen
  10317. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  10318. */
  10319. FeatureDetection.supportsFullscreen = function() {
  10320. return Fullscreen.supportsFullscreen();
  10321. };
  10322. /**
  10323. * Detects whether the current browser supports typed arrays.
  10324. *
  10325. * @returns {Boolean} true if the browser supports typed arrays, false if not.
  10326. *
  10327. * @see {@link http://www.khronos.org/registry/typedarray/specs/latest/|Typed Array Specification}
  10328. */
  10329. FeatureDetection.supportsTypedArrays = function() {
  10330. return typeof ArrayBuffer !== 'undefined';
  10331. };
  10332. /**
  10333. * Detects whether the current browser supports Web Workers.
  10334. *
  10335. * @returns {Boolean} true if the browsers supports Web Workers, false if not.
  10336. *
  10337. * @see {@link http://www.w3.org/TR/workers/}
  10338. */
  10339. FeatureDetection.supportsWebWorkers = function() {
  10340. return typeof Worker !== 'undefined';
  10341. };
  10342. return FeatureDetection;
  10343. });
  10344. /*global define*/
  10345. define('Core/WebGLConstants',[
  10346. './freezeObject'
  10347. ], function(
  10348. freezeObject) {
  10349. 'use strict';
  10350. /**
  10351. * Enum containing WebGL Constant values by name.
  10352. * for use without an active WebGL context, or in cases where certain constants are unavailable using the WebGL context
  10353. * (For example, in [Safari 9]{@link https://github.com/AnalyticalGraphicsInc/cesium/issues/2989}).
  10354. *
  10355. * These match the constants from the [WebGL 1.0]{@link https://www.khronos.org/registry/webgl/specs/latest/1.0/}
  10356. * and [WebGL 2.0]{@link https://www.khronos.org/registry/webgl/specs/latest/2.0/}
  10357. * specifications.
  10358. *
  10359. * @exports WebGLConstants
  10360. */
  10361. var WebGLConstants = {
  10362. DEPTH_BUFFER_BIT : 0x00000100,
  10363. STENCIL_BUFFER_BIT : 0x00000400,
  10364. COLOR_BUFFER_BIT : 0x00004000,
  10365. POINTS : 0x0000,
  10366. LINES : 0x0001,
  10367. LINE_LOOP : 0x0002,
  10368. LINE_STRIP : 0x0003,
  10369. TRIANGLES : 0x0004,
  10370. TRIANGLE_STRIP : 0x0005,
  10371. TRIANGLE_FAN : 0x0006,
  10372. ZERO : 0,
  10373. ONE : 1,
  10374. SRC_COLOR : 0x0300,
  10375. ONE_MINUS_SRC_COLOR : 0x0301,
  10376. SRC_ALPHA : 0x0302,
  10377. ONE_MINUS_SRC_ALPHA : 0x0303,
  10378. DST_ALPHA : 0x0304,
  10379. ONE_MINUS_DST_ALPHA : 0x0305,
  10380. DST_COLOR : 0x0306,
  10381. ONE_MINUS_DST_COLOR : 0x0307,
  10382. SRC_ALPHA_SATURATE : 0x0308,
  10383. FUNC_ADD : 0x8006,
  10384. BLEND_EQUATION : 0x8009,
  10385. BLEND_EQUATION_RGB : 0x8009, // same as BLEND_EQUATION
  10386. BLEND_EQUATION_ALPHA : 0x883D,
  10387. FUNC_SUBTRACT : 0x800A,
  10388. FUNC_REVERSE_SUBTRACT : 0x800B,
  10389. BLEND_DST_RGB : 0x80C8,
  10390. BLEND_SRC_RGB : 0x80C9,
  10391. BLEND_DST_ALPHA : 0x80CA,
  10392. BLEND_SRC_ALPHA : 0x80CB,
  10393. CONSTANT_COLOR : 0x8001,
  10394. ONE_MINUS_CONSTANT_COLOR : 0x8002,
  10395. CONSTANT_ALPHA : 0x8003,
  10396. ONE_MINUS_CONSTANT_ALPHA : 0x8004,
  10397. BLEND_COLOR : 0x8005,
  10398. ARRAY_BUFFER : 0x8892,
  10399. ELEMENT_ARRAY_BUFFER : 0x8893,
  10400. ARRAY_BUFFER_BINDING : 0x8894,
  10401. ELEMENT_ARRAY_BUFFER_BINDING : 0x8895,
  10402. STREAM_DRAW : 0x88E0,
  10403. STATIC_DRAW : 0x88E4,
  10404. DYNAMIC_DRAW : 0x88E8,
  10405. BUFFER_SIZE : 0x8764,
  10406. BUFFER_USAGE : 0x8765,
  10407. CURRENT_VERTEX_ATTRIB : 0x8626,
  10408. FRONT : 0x0404,
  10409. BACK : 0x0405,
  10410. FRONT_AND_BACK : 0x0408,
  10411. CULL_FACE : 0x0B44,
  10412. BLEND : 0x0BE2,
  10413. DITHER : 0x0BD0,
  10414. STENCIL_TEST : 0x0B90,
  10415. DEPTH_TEST : 0x0B71,
  10416. SCISSOR_TEST : 0x0C11,
  10417. POLYGON_OFFSET_FILL : 0x8037,
  10418. SAMPLE_ALPHA_TO_COVERAGE : 0x809E,
  10419. SAMPLE_COVERAGE : 0x80A0,
  10420. NO_ERROR : 0,
  10421. INVALID_ENUM : 0x0500,
  10422. INVALID_VALUE : 0x0501,
  10423. INVALID_OPERATION : 0x0502,
  10424. OUT_OF_MEMORY : 0x0505,
  10425. CW : 0x0900,
  10426. CCW : 0x0901,
  10427. LINE_WIDTH : 0x0B21,
  10428. ALIASED_POINT_SIZE_RANGE : 0x846D,
  10429. ALIASED_LINE_WIDTH_RANGE : 0x846E,
  10430. CULL_FACE_MODE : 0x0B45,
  10431. FRONT_FACE : 0x0B46,
  10432. DEPTH_RANGE : 0x0B70,
  10433. DEPTH_WRITEMASK : 0x0B72,
  10434. DEPTH_CLEAR_VALUE : 0x0B73,
  10435. DEPTH_FUNC : 0x0B74,
  10436. STENCIL_CLEAR_VALUE : 0x0B91,
  10437. STENCIL_FUNC : 0x0B92,
  10438. STENCIL_FAIL : 0x0B94,
  10439. STENCIL_PASS_DEPTH_FAIL : 0x0B95,
  10440. STENCIL_PASS_DEPTH_PASS : 0x0B96,
  10441. STENCIL_REF : 0x0B97,
  10442. STENCIL_VALUE_MASK : 0x0B93,
  10443. STENCIL_WRITEMASK : 0x0B98,
  10444. STENCIL_BACK_FUNC : 0x8800,
  10445. STENCIL_BACK_FAIL : 0x8801,
  10446. STENCIL_BACK_PASS_DEPTH_FAIL : 0x8802,
  10447. STENCIL_BACK_PASS_DEPTH_PASS : 0x8803,
  10448. STENCIL_BACK_REF : 0x8CA3,
  10449. STENCIL_BACK_VALUE_MASK : 0x8CA4,
  10450. STENCIL_BACK_WRITEMASK : 0x8CA5,
  10451. VIEWPORT : 0x0BA2,
  10452. SCISSOR_BOX : 0x0C10,
  10453. COLOR_CLEAR_VALUE : 0x0C22,
  10454. COLOR_WRITEMASK : 0x0C23,
  10455. UNPACK_ALIGNMENT : 0x0CF5,
  10456. PACK_ALIGNMENT : 0x0D05,
  10457. MAX_TEXTURE_SIZE : 0x0D33,
  10458. MAX_VIEWPORT_DIMS : 0x0D3A,
  10459. SUBPIXEL_BITS : 0x0D50,
  10460. RED_BITS : 0x0D52,
  10461. GREEN_BITS : 0x0D53,
  10462. BLUE_BITS : 0x0D54,
  10463. ALPHA_BITS : 0x0D55,
  10464. DEPTH_BITS : 0x0D56,
  10465. STENCIL_BITS : 0x0D57,
  10466. POLYGON_OFFSET_UNITS : 0x2A00,
  10467. POLYGON_OFFSET_FACTOR : 0x8038,
  10468. TEXTURE_BINDING_2D : 0x8069,
  10469. SAMPLE_BUFFERS : 0x80A8,
  10470. SAMPLES : 0x80A9,
  10471. SAMPLE_COVERAGE_VALUE : 0x80AA,
  10472. SAMPLE_COVERAGE_INVERT : 0x80AB,
  10473. COMPRESSED_TEXTURE_FORMATS : 0x86A3,
  10474. DONT_CARE : 0x1100,
  10475. FASTEST : 0x1101,
  10476. NICEST : 0x1102,
  10477. GENERATE_MIPMAP_HINT : 0x8192,
  10478. BYTE : 0x1400,
  10479. UNSIGNED_BYTE : 0x1401,
  10480. SHORT : 0x1402,
  10481. UNSIGNED_SHORT : 0x1403,
  10482. INT : 0x1404,
  10483. UNSIGNED_INT : 0x1405,
  10484. FLOAT : 0x1406,
  10485. DEPTH_COMPONENT : 0x1902,
  10486. ALPHA : 0x1906,
  10487. RGB : 0x1907,
  10488. RGBA : 0x1908,
  10489. LUMINANCE : 0x1909,
  10490. LUMINANCE_ALPHA : 0x190A,
  10491. UNSIGNED_SHORT_4_4_4_4 : 0x8033,
  10492. UNSIGNED_SHORT_5_5_5_1 : 0x8034,
  10493. UNSIGNED_SHORT_5_6_5 : 0x8363,
  10494. FRAGMENT_SHADER : 0x8B30,
  10495. VERTEX_SHADER : 0x8B31,
  10496. MAX_VERTEX_ATTRIBS : 0x8869,
  10497. MAX_VERTEX_UNIFORM_VECTORS : 0x8DFB,
  10498. MAX_VARYING_VECTORS : 0x8DFC,
  10499. MAX_COMBINED_TEXTURE_IMAGE_UNITS : 0x8B4D,
  10500. MAX_VERTEX_TEXTURE_IMAGE_UNITS : 0x8B4C,
  10501. MAX_TEXTURE_IMAGE_UNITS : 0x8872,
  10502. MAX_FRAGMENT_UNIFORM_VECTORS : 0x8DFD,
  10503. SHADER_TYPE : 0x8B4F,
  10504. DELETE_STATUS : 0x8B80,
  10505. LINK_STATUS : 0x8B82,
  10506. VALIDATE_STATUS : 0x8B83,
  10507. ATTACHED_SHADERS : 0x8B85,
  10508. ACTIVE_UNIFORMS : 0x8B86,
  10509. ACTIVE_ATTRIBUTES : 0x8B89,
  10510. SHADING_LANGUAGE_VERSION : 0x8B8C,
  10511. CURRENT_PROGRAM : 0x8B8D,
  10512. NEVER : 0x0200,
  10513. LESS : 0x0201,
  10514. EQUAL : 0x0202,
  10515. LEQUAL : 0x0203,
  10516. GREATER : 0x0204,
  10517. NOTEQUAL : 0x0205,
  10518. GEQUAL : 0x0206,
  10519. ALWAYS : 0x0207,
  10520. KEEP : 0x1E00,
  10521. REPLACE : 0x1E01,
  10522. INCR : 0x1E02,
  10523. DECR : 0x1E03,
  10524. INVERT : 0x150A,
  10525. INCR_WRAP : 0x8507,
  10526. DECR_WRAP : 0x8508,
  10527. VENDOR : 0x1F00,
  10528. RENDERER : 0x1F01,
  10529. VERSION : 0x1F02,
  10530. NEAREST : 0x2600,
  10531. LINEAR : 0x2601,
  10532. NEAREST_MIPMAP_NEAREST : 0x2700,
  10533. LINEAR_MIPMAP_NEAREST : 0x2701,
  10534. NEAREST_MIPMAP_LINEAR : 0x2702,
  10535. LINEAR_MIPMAP_LINEAR : 0x2703,
  10536. TEXTURE_MAG_FILTER : 0x2800,
  10537. TEXTURE_MIN_FILTER : 0x2801,
  10538. TEXTURE_WRAP_S : 0x2802,
  10539. TEXTURE_WRAP_T : 0x2803,
  10540. TEXTURE_2D : 0x0DE1,
  10541. TEXTURE : 0x1702,
  10542. TEXTURE_CUBE_MAP : 0x8513,
  10543. TEXTURE_BINDING_CUBE_MAP : 0x8514,
  10544. TEXTURE_CUBE_MAP_POSITIVE_X : 0x8515,
  10545. TEXTURE_CUBE_MAP_NEGATIVE_X : 0x8516,
  10546. TEXTURE_CUBE_MAP_POSITIVE_Y : 0x8517,
  10547. TEXTURE_CUBE_MAP_NEGATIVE_Y : 0x8518,
  10548. TEXTURE_CUBE_MAP_POSITIVE_Z : 0x8519,
  10549. TEXTURE_CUBE_MAP_NEGATIVE_Z : 0x851A,
  10550. MAX_CUBE_MAP_TEXTURE_SIZE : 0x851C,
  10551. TEXTURE0 : 0x84C0,
  10552. TEXTURE1 : 0x84C1,
  10553. TEXTURE2 : 0x84C2,
  10554. TEXTURE3 : 0x84C3,
  10555. TEXTURE4 : 0x84C4,
  10556. TEXTURE5 : 0x84C5,
  10557. TEXTURE6 : 0x84C6,
  10558. TEXTURE7 : 0x84C7,
  10559. TEXTURE8 : 0x84C8,
  10560. TEXTURE9 : 0x84C9,
  10561. TEXTURE10 : 0x84CA,
  10562. TEXTURE11 : 0x84CB,
  10563. TEXTURE12 : 0x84CC,
  10564. TEXTURE13 : 0x84CD,
  10565. TEXTURE14 : 0x84CE,
  10566. TEXTURE15 : 0x84CF,
  10567. TEXTURE16 : 0x84D0,
  10568. TEXTURE17 : 0x84D1,
  10569. TEXTURE18 : 0x84D2,
  10570. TEXTURE19 : 0x84D3,
  10571. TEXTURE20 : 0x84D4,
  10572. TEXTURE21 : 0x84D5,
  10573. TEXTURE22 : 0x84D6,
  10574. TEXTURE23 : 0x84D7,
  10575. TEXTURE24 : 0x84D8,
  10576. TEXTURE25 : 0x84D9,
  10577. TEXTURE26 : 0x84DA,
  10578. TEXTURE27 : 0x84DB,
  10579. TEXTURE28 : 0x84DC,
  10580. TEXTURE29 : 0x84DD,
  10581. TEXTURE30 : 0x84DE,
  10582. TEXTURE31 : 0x84DF,
  10583. ACTIVE_TEXTURE : 0x84E0,
  10584. REPEAT : 0x2901,
  10585. CLAMP_TO_EDGE : 0x812F,
  10586. MIRRORED_REPEAT : 0x8370,
  10587. FLOAT_VEC2 : 0x8B50,
  10588. FLOAT_VEC3 : 0x8B51,
  10589. FLOAT_VEC4 : 0x8B52,
  10590. INT_VEC2 : 0x8B53,
  10591. INT_VEC3 : 0x8B54,
  10592. INT_VEC4 : 0x8B55,
  10593. BOOL : 0x8B56,
  10594. BOOL_VEC2 : 0x8B57,
  10595. BOOL_VEC3 : 0x8B58,
  10596. BOOL_VEC4 : 0x8B59,
  10597. FLOAT_MAT2 : 0x8B5A,
  10598. FLOAT_MAT3 : 0x8B5B,
  10599. FLOAT_MAT4 : 0x8B5C,
  10600. SAMPLER_2D : 0x8B5E,
  10601. SAMPLER_CUBE : 0x8B60,
  10602. VERTEX_ATTRIB_ARRAY_ENABLED : 0x8622,
  10603. VERTEX_ATTRIB_ARRAY_SIZE : 0x8623,
  10604. VERTEX_ATTRIB_ARRAY_STRIDE : 0x8624,
  10605. VERTEX_ATTRIB_ARRAY_TYPE : 0x8625,
  10606. VERTEX_ATTRIB_ARRAY_NORMALIZED : 0x886A,
  10607. VERTEX_ATTRIB_ARRAY_POINTER : 0x8645,
  10608. VERTEX_ATTRIB_ARRAY_BUFFER_BINDING : 0x889F,
  10609. IMPLEMENTATION_COLOR_READ_TYPE : 0x8B9A,
  10610. IMPLEMENTATION_COLOR_READ_FORMAT : 0x8B9B,
  10611. COMPILE_STATUS : 0x8B81,
  10612. LOW_FLOAT : 0x8DF0,
  10613. MEDIUM_FLOAT : 0x8DF1,
  10614. HIGH_FLOAT : 0x8DF2,
  10615. LOW_INT : 0x8DF3,
  10616. MEDIUM_INT : 0x8DF4,
  10617. HIGH_INT : 0x8DF5,
  10618. FRAMEBUFFER : 0x8D40,
  10619. RENDERBUFFER : 0x8D41,
  10620. RGBA4 : 0x8056,
  10621. RGB5_A1 : 0x8057,
  10622. RGB565 : 0x8D62,
  10623. DEPTH_COMPONENT16 : 0x81A5,
  10624. STENCIL_INDEX : 0x1901,
  10625. STENCIL_INDEX8 : 0x8D48,
  10626. DEPTH_STENCIL : 0x84F9,
  10627. RENDERBUFFER_WIDTH : 0x8D42,
  10628. RENDERBUFFER_HEIGHT : 0x8D43,
  10629. RENDERBUFFER_INTERNAL_FORMAT : 0x8D44,
  10630. RENDERBUFFER_RED_SIZE : 0x8D50,
  10631. RENDERBUFFER_GREEN_SIZE : 0x8D51,
  10632. RENDERBUFFER_BLUE_SIZE : 0x8D52,
  10633. RENDERBUFFER_ALPHA_SIZE : 0x8D53,
  10634. RENDERBUFFER_DEPTH_SIZE : 0x8D54,
  10635. RENDERBUFFER_STENCIL_SIZE : 0x8D55,
  10636. FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE : 0x8CD0,
  10637. FRAMEBUFFER_ATTACHMENT_OBJECT_NAME : 0x8CD1,
  10638. FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL : 0x8CD2,
  10639. FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE : 0x8CD3,
  10640. COLOR_ATTACHMENT0 : 0x8CE0,
  10641. DEPTH_ATTACHMENT : 0x8D00,
  10642. STENCIL_ATTACHMENT : 0x8D20,
  10643. DEPTH_STENCIL_ATTACHMENT : 0x821A,
  10644. NONE : 0,
  10645. FRAMEBUFFER_COMPLETE : 0x8CD5,
  10646. FRAMEBUFFER_INCOMPLETE_ATTACHMENT : 0x8CD6,
  10647. FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : 0x8CD7,
  10648. FRAMEBUFFER_INCOMPLETE_DIMENSIONS : 0x8CD9,
  10649. FRAMEBUFFER_UNSUPPORTED : 0x8CDD,
  10650. FRAMEBUFFER_BINDING : 0x8CA6,
  10651. RENDERBUFFER_BINDING : 0x8CA7,
  10652. MAX_RENDERBUFFER_SIZE : 0x84E8,
  10653. INVALID_FRAMEBUFFER_OPERATION : 0x0506,
  10654. UNPACK_FLIP_Y_WEBGL : 0x9240,
  10655. UNPACK_PREMULTIPLY_ALPHA_WEBGL : 0x9241,
  10656. CONTEXT_LOST_WEBGL : 0x9242,
  10657. UNPACK_COLORSPACE_CONVERSION_WEBGL : 0x9243,
  10658. BROWSER_DEFAULT_WEBGL : 0x9244,
  10659. // Desktop OpenGL
  10660. DOUBLE : 0x140A,
  10661. // WebGL 2
  10662. READ_BUFFER : 0x0C02,
  10663. UNPACK_ROW_LENGTH : 0x0CF2,
  10664. UNPACK_SKIP_ROWS : 0x0CF3,
  10665. UNPACK_SKIP_PIXELS : 0x0CF4,
  10666. PACK_ROW_LENGTH : 0x0D02,
  10667. PACK_SKIP_ROWS : 0x0D03,
  10668. PACK_SKIP_PIXELS : 0x0D04,
  10669. COLOR : 0x1800,
  10670. DEPTH : 0x1801,
  10671. STENCIL : 0x1802,
  10672. RED : 0x1903,
  10673. RGB8 : 0x8051,
  10674. RGBA8 : 0x8058,
  10675. RGB10_A2 : 0x8059,
  10676. TEXTURE_BINDING_3D : 0x806A,
  10677. UNPACK_SKIP_IMAGES : 0x806D,
  10678. UNPACK_IMAGE_HEIGHT : 0x806E,
  10679. TEXTURE_3D : 0x806F,
  10680. TEXTURE_WRAP_R : 0x8072,
  10681. MAX_3D_TEXTURE_SIZE : 0x8073,
  10682. UNSIGNED_INT_2_10_10_10_REV : 0x8368,
  10683. MAX_ELEMENTS_VERTICES : 0x80E8,
  10684. MAX_ELEMENTS_INDICES : 0x80E9,
  10685. TEXTURE_MIN_LOD : 0x813A,
  10686. TEXTURE_MAX_LOD : 0x813B,
  10687. TEXTURE_BASE_LEVEL : 0x813C,
  10688. TEXTURE_MAX_LEVEL : 0x813D,
  10689. MIN : 0x8007,
  10690. MAX : 0x8008,
  10691. DEPTH_COMPONENT24 : 0x81A6,
  10692. MAX_TEXTURE_LOD_BIAS : 0x84FD,
  10693. TEXTURE_COMPARE_MODE : 0x884C,
  10694. TEXTURE_COMPARE_FUNC : 0x884D,
  10695. CURRENT_QUERY : 0x8865,
  10696. QUERY_RESULT : 0x8866,
  10697. QUERY_RESULT_AVAILABLE : 0x8867,
  10698. STREAM_READ : 0x88E1,
  10699. STREAM_COPY : 0x88E2,
  10700. STATIC_READ : 0x88E5,
  10701. STATIC_COPY : 0x88E6,
  10702. DYNAMIC_READ : 0x88E9,
  10703. DYNAMIC_COPY : 0x88EA,
  10704. MAX_DRAW_BUFFERS : 0x8824,
  10705. DRAW_BUFFER0 : 0x8825,
  10706. DRAW_BUFFER1 : 0x8826,
  10707. DRAW_BUFFER2 : 0x8827,
  10708. DRAW_BUFFER3 : 0x8828,
  10709. DRAW_BUFFER4 : 0x8829,
  10710. DRAW_BUFFER5 : 0x882A,
  10711. DRAW_BUFFER6 : 0x882B,
  10712. DRAW_BUFFER7 : 0x882C,
  10713. DRAW_BUFFER8 : 0x882D,
  10714. DRAW_BUFFER9 : 0x882E,
  10715. DRAW_BUFFER10 : 0x882F,
  10716. DRAW_BUFFER11 : 0x8830,
  10717. DRAW_BUFFER12 : 0x8831,
  10718. DRAW_BUFFER13 : 0x8832,
  10719. DRAW_BUFFER14 : 0x8833,
  10720. DRAW_BUFFER15 : 0x8834,
  10721. MAX_FRAGMENT_UNIFORM_COMPONENTS : 0x8B49,
  10722. MAX_VERTEX_UNIFORM_COMPONENTS : 0x8B4A,
  10723. SAMPLER_3D : 0x8B5F,
  10724. SAMPLER_2D_SHADOW : 0x8B62,
  10725. FRAGMENT_SHADER_DERIVATIVE_HINT : 0x8B8B,
  10726. PIXEL_PACK_BUFFER : 0x88EB,
  10727. PIXEL_UNPACK_BUFFER : 0x88EC,
  10728. PIXEL_PACK_BUFFER_BINDING : 0x88ED,
  10729. PIXEL_UNPACK_BUFFER_BINDING : 0x88EF,
  10730. FLOAT_MAT2x3 : 0x8B65,
  10731. FLOAT_MAT2x4 : 0x8B66,
  10732. FLOAT_MAT3x2 : 0x8B67,
  10733. FLOAT_MAT3x4 : 0x8B68,
  10734. FLOAT_MAT4x2 : 0x8B69,
  10735. FLOAT_MAT4x3 : 0x8B6A,
  10736. SRGB : 0x8C40,
  10737. SRGB8 : 0x8C41,
  10738. SRGB8_ALPHA8 : 0x8C43,
  10739. COMPARE_REF_TO_TEXTURE : 0x884E,
  10740. RGBA32F : 0x8814,
  10741. RGB32F : 0x8815,
  10742. RGBA16F : 0x881A,
  10743. RGB16F : 0x881B,
  10744. VERTEX_ATTRIB_ARRAY_INTEGER : 0x88FD,
  10745. MAX_ARRAY_TEXTURE_LAYERS : 0x88FF,
  10746. MIN_PROGRAM_TEXEL_OFFSET : 0x8904,
  10747. MAX_PROGRAM_TEXEL_OFFSET : 0x8905,
  10748. MAX_VARYING_COMPONENTS : 0x8B4B,
  10749. TEXTURE_2D_ARRAY : 0x8C1A,
  10750. TEXTURE_BINDING_2D_ARRAY : 0x8C1D,
  10751. R11F_G11F_B10F : 0x8C3A,
  10752. UNSIGNED_INT_10F_11F_11F_REV : 0x8C3B,
  10753. RGB9_E5 : 0x8C3D,
  10754. UNSIGNED_INT_5_9_9_9_REV : 0x8C3E,
  10755. TRANSFORM_FEEDBACK_BUFFER_MODE : 0x8C7F,
  10756. MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS : 0x8C80,
  10757. TRANSFORM_FEEDBACK_VARYINGS : 0x8C83,
  10758. TRANSFORM_FEEDBACK_BUFFER_START : 0x8C84,
  10759. TRANSFORM_FEEDBACK_BUFFER_SIZE : 0x8C85,
  10760. TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN : 0x8C88,
  10761. RASTERIZER_DISCARD : 0x8C89,
  10762. MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS : 0x8C8A,
  10763. MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS : 0x8C8B,
  10764. INTERLEAVED_ATTRIBS : 0x8C8C,
  10765. SEPARATE_ATTRIBS : 0x8C8D,
  10766. TRANSFORM_FEEDBACK_BUFFER : 0x8C8E,
  10767. TRANSFORM_FEEDBACK_BUFFER_BINDING : 0x8C8F,
  10768. RGBA32UI : 0x8D70,
  10769. RGB32UI : 0x8D71,
  10770. RGBA16UI : 0x8D76,
  10771. RGB16UI : 0x8D77,
  10772. RGBA8UI : 0x8D7C,
  10773. RGB8UI : 0x8D7D,
  10774. RGBA32I : 0x8D82,
  10775. RGB32I : 0x8D83,
  10776. RGBA16I : 0x8D88,
  10777. RGB16I : 0x8D89,
  10778. RGBA8I : 0x8D8E,
  10779. RGB8I : 0x8D8F,
  10780. RED_INTEGER : 0x8D94,
  10781. RGB_INTEGER : 0x8D98,
  10782. RGBA_INTEGER : 0x8D99,
  10783. SAMPLER_2D_ARRAY : 0x8DC1,
  10784. SAMPLER_2D_ARRAY_SHADOW : 0x8DC4,
  10785. SAMPLER_CUBE_SHADOW : 0x8DC5,
  10786. UNSIGNED_INT_VEC2 : 0x8DC6,
  10787. UNSIGNED_INT_VEC3 : 0x8DC7,
  10788. UNSIGNED_INT_VEC4 : 0x8DC8,
  10789. INT_SAMPLER_2D : 0x8DCA,
  10790. INT_SAMPLER_3D : 0x8DCB,
  10791. INT_SAMPLER_CUBE : 0x8DCC,
  10792. INT_SAMPLER_2D_ARRAY : 0x8DCF,
  10793. UNSIGNED_INT_SAMPLER_2D : 0x8DD2,
  10794. UNSIGNED_INT_SAMPLER_3D : 0x8DD3,
  10795. UNSIGNED_INT_SAMPLER_CUBE : 0x8DD4,
  10796. UNSIGNED_INT_SAMPLER_2D_ARRAY : 0x8DD7,
  10797. DEPTH_COMPONENT32F : 0x8CAC,
  10798. DEPTH32F_STENCIL8 : 0x8CAD,
  10799. FLOAT_32_UNSIGNED_INT_24_8_REV : 0x8DAD,
  10800. FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING : 0x8210,
  10801. FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE : 0x8211,
  10802. FRAMEBUFFER_ATTACHMENT_RED_SIZE : 0x8212,
  10803. FRAMEBUFFER_ATTACHMENT_GREEN_SIZE : 0x8213,
  10804. FRAMEBUFFER_ATTACHMENT_BLUE_SIZE : 0x8214,
  10805. FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE : 0x8215,
  10806. FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE : 0x8216,
  10807. FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE : 0x8217,
  10808. FRAMEBUFFER_DEFAULT : 0x8218,
  10809. UNSIGNED_INT_24_8 : 0x84FA,
  10810. DEPTH24_STENCIL8 : 0x88F0,
  10811. UNSIGNED_NORMALIZED : 0x8C17,
  10812. DRAW_FRAMEBUFFER_BINDING : 0x8CA6, // Same as FRAMEBUFFER_BINDING
  10813. READ_FRAMEBUFFER : 0x8CA8,
  10814. DRAW_FRAMEBUFFER : 0x8CA9,
  10815. READ_FRAMEBUFFER_BINDING : 0x8CAA,
  10816. RENDERBUFFER_SAMPLES : 0x8CAB,
  10817. FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER : 0x8CD4,
  10818. MAX_COLOR_ATTACHMENTS : 0x8CDF,
  10819. COLOR_ATTACHMENT1 : 0x8CE1,
  10820. COLOR_ATTACHMENT2 : 0x8CE2,
  10821. COLOR_ATTACHMENT3 : 0x8CE3,
  10822. COLOR_ATTACHMENT4 : 0x8CE4,
  10823. COLOR_ATTACHMENT5 : 0x8CE5,
  10824. COLOR_ATTACHMENT6 : 0x8CE6,
  10825. COLOR_ATTACHMENT7 : 0x8CE7,
  10826. COLOR_ATTACHMENT8 : 0x8CE8,
  10827. COLOR_ATTACHMENT9 : 0x8CE9,
  10828. COLOR_ATTACHMENT10 : 0x8CEA,
  10829. COLOR_ATTACHMENT11 : 0x8CEB,
  10830. COLOR_ATTACHMENT12 : 0x8CEC,
  10831. COLOR_ATTACHMENT13 : 0x8CED,
  10832. COLOR_ATTACHMENT14 : 0x8CEE,
  10833. COLOR_ATTACHMENT15 : 0x8CEF,
  10834. FRAMEBUFFER_INCOMPLETE_MULTISAMPLE : 0x8D56,
  10835. MAX_SAMPLES : 0x8D57,
  10836. HALF_FLOAT : 0x140B,
  10837. RG : 0x8227,
  10838. RG_INTEGER : 0x8228,
  10839. R8 : 0x8229,
  10840. RG8 : 0x822B,
  10841. R16F : 0x822D,
  10842. R32F : 0x822E,
  10843. RG16F : 0x822F,
  10844. RG32F : 0x8230,
  10845. R8I : 0x8231,
  10846. R8UI : 0x8232,
  10847. R16I : 0x8233,
  10848. R16UI : 0x8234,
  10849. R32I : 0x8235,
  10850. R32UI : 0x8236,
  10851. RG8I : 0x8237,
  10852. RG8UI : 0x8238,
  10853. RG16I : 0x8239,
  10854. RG16UI : 0x823A,
  10855. RG32I : 0x823B,
  10856. RG32UI : 0x823C,
  10857. VERTEX_ARRAY_BINDING : 0x85B5,
  10858. R8_SNORM : 0x8F94,
  10859. RG8_SNORM : 0x8F95,
  10860. RGB8_SNORM : 0x8F96,
  10861. RGBA8_SNORM : 0x8F97,
  10862. SIGNED_NORMALIZED : 0x8F9C,
  10863. COPY_READ_BUFFER : 0x8F36,
  10864. COPY_WRITE_BUFFER : 0x8F37,
  10865. COPY_READ_BUFFER_BINDING : 0x8F36, // Same as COPY_READ_BUFFER
  10866. COPY_WRITE_BUFFER_BINDING : 0x8F37, // Same as COPY_WRITE_BUFFER
  10867. UNIFORM_BUFFER : 0x8A11,
  10868. UNIFORM_BUFFER_BINDING : 0x8A28,
  10869. UNIFORM_BUFFER_START : 0x8A29,
  10870. UNIFORM_BUFFER_SIZE : 0x8A2A,
  10871. MAX_VERTEX_UNIFORM_BLOCKS : 0x8A2B,
  10872. MAX_FRAGMENT_UNIFORM_BLOCKS : 0x8A2D,
  10873. MAX_COMBINED_UNIFORM_BLOCKS : 0x8A2E,
  10874. MAX_UNIFORM_BUFFER_BINDINGS : 0x8A2F,
  10875. MAX_UNIFORM_BLOCK_SIZE : 0x8A30,
  10876. MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS : 0x8A31,
  10877. MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS : 0x8A33,
  10878. UNIFORM_BUFFER_OFFSET_ALIGNMENT : 0x8A34,
  10879. ACTIVE_UNIFORM_BLOCKS : 0x8A36,
  10880. UNIFORM_TYPE : 0x8A37,
  10881. UNIFORM_SIZE : 0x8A38,
  10882. UNIFORM_BLOCK_INDEX : 0x8A3A,
  10883. UNIFORM_OFFSET : 0x8A3B,
  10884. UNIFORM_ARRAY_STRIDE : 0x8A3C,
  10885. UNIFORM_MATRIX_STRIDE : 0x8A3D,
  10886. UNIFORM_IS_ROW_MAJOR : 0x8A3E,
  10887. UNIFORM_BLOCK_BINDING : 0x8A3F,
  10888. UNIFORM_BLOCK_DATA_SIZE : 0x8A40,
  10889. UNIFORM_BLOCK_ACTIVE_UNIFORMS : 0x8A42,
  10890. UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES : 0x8A43,
  10891. UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER : 0x8A44,
  10892. UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER : 0x8A46,
  10893. INVALID_INDEX : 0xFFFFFFFF,
  10894. MAX_VERTEX_OUTPUT_COMPONENTS : 0x9122,
  10895. MAX_FRAGMENT_INPUT_COMPONENTS : 0x9125,
  10896. MAX_SERVER_WAIT_TIMEOUT : 0x9111,
  10897. OBJECT_TYPE : 0x9112,
  10898. SYNC_CONDITION : 0x9113,
  10899. SYNC_STATUS : 0x9114,
  10900. SYNC_FLAGS : 0x9115,
  10901. SYNC_FENCE : 0x9116,
  10902. SYNC_GPU_COMMANDS_COMPLETE : 0x9117,
  10903. UNSIGNALED : 0x9118,
  10904. SIGNALED : 0x9119,
  10905. ALREADY_SIGNALED : 0x911A,
  10906. TIMEOUT_EXPIRED : 0x911B,
  10907. CONDITION_SATISFIED : 0x911C,
  10908. WAIT_FAILED : 0x911D,
  10909. SYNC_FLUSH_COMMANDS_BIT : 0x00000001,
  10910. VERTEX_ATTRIB_ARRAY_DIVISOR : 0x88FE,
  10911. ANY_SAMPLES_PASSED : 0x8C2F,
  10912. ANY_SAMPLES_PASSED_CONSERVATIVE : 0x8D6A,
  10913. SAMPLER_BINDING : 0x8919,
  10914. RGB10_A2UI : 0x906F,
  10915. INT_2_10_10_10_REV : 0x8D9F,
  10916. TRANSFORM_FEEDBACK : 0x8E22,
  10917. TRANSFORM_FEEDBACK_PAUSED : 0x8E23,
  10918. TRANSFORM_FEEDBACK_ACTIVE : 0x8E24,
  10919. TRANSFORM_FEEDBACK_BINDING : 0x8E25,
  10920. COMPRESSED_R11_EAC : 0x9270,
  10921. COMPRESSED_SIGNED_R11_EAC : 0x9271,
  10922. COMPRESSED_RG11_EAC : 0x9272,
  10923. COMPRESSED_SIGNED_RG11_EAC : 0x9273,
  10924. COMPRESSED_RGB8_ETC2 : 0x9274,
  10925. COMPRESSED_SRGB8_ETC2 : 0x9275,
  10926. COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 : 0x9276,
  10927. COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : 0x9277,
  10928. COMPRESSED_RGBA8_ETC2_EAC : 0x9278,
  10929. COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : 0x9279,
  10930. TEXTURE_IMMUTABLE_FORMAT : 0x912F,
  10931. MAX_ELEMENT_INDEX : 0x8D6B,
  10932. TEXTURE_IMMUTABLE_LEVELS : 0x82DF
  10933. };
  10934. return freezeObject(WebGLConstants);
  10935. });
  10936. /*global define*/
  10937. define('Core/ComponentDatatype',[
  10938. './defaultValue',
  10939. './defined',
  10940. './DeveloperError',
  10941. './FeatureDetection',
  10942. './freezeObject',
  10943. './WebGLConstants'
  10944. ], function(
  10945. defaultValue,
  10946. defined,
  10947. DeveloperError,
  10948. FeatureDetection,
  10949. freezeObject,
  10950. WebGLConstants) {
  10951. 'use strict';
  10952. // Bail out if the browser doesn't support typed arrays, to prevent the setup function
  10953. // from failing, since we won't be able to create a WebGL context anyway.
  10954. if (!FeatureDetection.supportsTypedArrays()) {
  10955. return {};
  10956. }
  10957. /**
  10958. * WebGL component datatypes. Components are intrinsics,
  10959. * which form attributes, which form vertices.
  10960. *
  10961. * @exports ComponentDatatype
  10962. */
  10963. var ComponentDatatype = {
  10964. /**
  10965. * 8-bit signed byte corresponding to <code>gl.BYTE</code> and the type
  10966. * of an element in <code>Int8Array</code>.
  10967. *
  10968. * @type {Number}
  10969. * @constant
  10970. */
  10971. BYTE : WebGLConstants.BYTE,
  10972. /**
  10973. * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
  10974. * of an element in <code>Uint8Array</code>.
  10975. *
  10976. * @type {Number}
  10977. * @constant
  10978. */
  10979. UNSIGNED_BYTE : WebGLConstants.UNSIGNED_BYTE,
  10980. /**
  10981. * 16-bit signed short corresponding to <code>SHORT</code> and the type
  10982. * of an element in <code>Int16Array</code>.
  10983. *
  10984. * @type {Number}
  10985. * @constant
  10986. */
  10987. SHORT : WebGLConstants.SHORT,
  10988. /**
  10989. * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
  10990. * of an element in <code>Uint16Array</code>.
  10991. *
  10992. * @type {Number}
  10993. * @constant
  10994. */
  10995. UNSIGNED_SHORT : WebGLConstants.UNSIGNED_SHORT,
  10996. /**
  10997. * 32-bit signed int corresponding to <code>INT</code> and the type
  10998. * of an element in <code>Int32Array</code>.
  10999. *
  11000. * @memberOf ComponentDatatype
  11001. *
  11002. * @type {Number}
  11003. * @constant
  11004. */
  11005. INT : WebGLConstants.INT,
  11006. /**
  11007. * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
  11008. * of an element in <code>Uint32Array</code>.
  11009. *
  11010. * @memberOf ComponentDatatype
  11011. *
  11012. * @type {Number}
  11013. * @constant
  11014. */
  11015. UNSIGNED_INT : WebGLConstants.UNSIGNED_INT,
  11016. /**
  11017. * 32-bit floating-point corresponding to <code>FLOAT</code> and the type
  11018. * of an element in <code>Float32Array</code>.
  11019. *
  11020. * @type {Number}
  11021. * @constant
  11022. */
  11023. FLOAT : WebGLConstants.FLOAT,
  11024. /**
  11025. * 64-bit floating-point corresponding to <code>gl.DOUBLE</code> (in Desktop OpenGL;
  11026. * this is not supported in WebGL, and is emulated in Cesium via {@link GeometryPipeline.encodeAttribute})
  11027. * and the type of an element in <code>Float64Array</code>.
  11028. *
  11029. * @memberOf ComponentDatatype
  11030. *
  11031. * @type {Number}
  11032. * @constant
  11033. * @default 0x140A
  11034. */
  11035. DOUBLE : WebGLConstants.DOUBLE
  11036. };
  11037. /**
  11038. * Returns the size, in bytes, of the corresponding datatype.
  11039. *
  11040. * @param {ComponentDatatype} componentDatatype The component datatype to get the size of.
  11041. * @returns {Number} The size in bytes.
  11042. *
  11043. * @exception {DeveloperError} componentDatatype is not a valid value.
  11044. *
  11045. * @example
  11046. * // Returns Int8Array.BYTES_PER_ELEMENT
  11047. * var size = Cesium.ComponentDatatype.getSizeInBytes(Cesium.ComponentDatatype.BYTE);
  11048. */
  11049. ComponentDatatype.getSizeInBytes = function(componentDatatype){
  11050. if (!defined(componentDatatype)) {
  11051. throw new DeveloperError('value is required.');
  11052. }
  11053. switch (componentDatatype) {
  11054. case ComponentDatatype.BYTE:
  11055. return Int8Array.BYTES_PER_ELEMENT;
  11056. case ComponentDatatype.UNSIGNED_BYTE:
  11057. return Uint8Array.BYTES_PER_ELEMENT;
  11058. case ComponentDatatype.SHORT:
  11059. return Int16Array.BYTES_PER_ELEMENT;
  11060. case ComponentDatatype.UNSIGNED_SHORT:
  11061. return Uint16Array.BYTES_PER_ELEMENT;
  11062. case ComponentDatatype.INT:
  11063. return Int32Array.BYTES_PER_ELEMENT;
  11064. case ComponentDatatype.UNSIGNED_INT:
  11065. return Uint32Array.BYTES_PER_ELEMENT;
  11066. case ComponentDatatype.FLOAT:
  11067. return Float32Array.BYTES_PER_ELEMENT;
  11068. case ComponentDatatype.DOUBLE:
  11069. return Float64Array.BYTES_PER_ELEMENT;
  11070. default:
  11071. throw new DeveloperError('componentDatatype is not a valid value.');
  11072. }
  11073. };
  11074. /**
  11075. * Gets the {@link ComponentDatatype} for the provided TypedArray instance.
  11076. *
  11077. * @param {TypedArray} array The typed array.
  11078. * @returns {ComponentDatatype} The ComponentDatatype for the provided array, or undefined if the array is not a TypedArray.
  11079. */
  11080. ComponentDatatype.fromTypedArray = function(array) {
  11081. if (array instanceof Int8Array) {
  11082. return ComponentDatatype.BYTE;
  11083. }
  11084. if (array instanceof Uint8Array) {
  11085. return ComponentDatatype.UNSIGNED_BYTE;
  11086. }
  11087. if (array instanceof Int16Array) {
  11088. return ComponentDatatype.SHORT;
  11089. }
  11090. if (array instanceof Uint16Array) {
  11091. return ComponentDatatype.UNSIGNED_SHORT;
  11092. }
  11093. if (array instanceof Int32Array) {
  11094. return ComponentDatatype.INT;
  11095. }
  11096. if (array instanceof Uint32Array) {
  11097. return ComponentDatatype.UNSIGNED_INT;
  11098. }
  11099. if (array instanceof Float32Array) {
  11100. return ComponentDatatype.FLOAT;
  11101. }
  11102. if (array instanceof Float64Array) {
  11103. return ComponentDatatype.DOUBLE;
  11104. }
  11105. };
  11106. /**
  11107. * Validates that the provided component datatype is a valid {@link ComponentDatatype}
  11108. *
  11109. * @param {ComponentDatatype} componentDatatype The component datatype to validate.
  11110. * @returns {Boolean} <code>true</code> if the provided component datatype is a valid value; otherwise, <code>false</code>.
  11111. *
  11112. * @example
  11113. * if (!Cesium.ComponentDatatype.validate(componentDatatype)) {
  11114. * throw new Cesium.DeveloperError('componentDatatype must be a valid value.');
  11115. * }
  11116. */
  11117. ComponentDatatype.validate = function(componentDatatype) {
  11118. return defined(componentDatatype) &&
  11119. (componentDatatype === ComponentDatatype.BYTE ||
  11120. componentDatatype === ComponentDatatype.UNSIGNED_BYTE ||
  11121. componentDatatype === ComponentDatatype.SHORT ||
  11122. componentDatatype === ComponentDatatype.UNSIGNED_SHORT ||
  11123. componentDatatype === ComponentDatatype.INT ||
  11124. componentDatatype === ComponentDatatype.UNSIGNED_INT ||
  11125. componentDatatype === ComponentDatatype.FLOAT ||
  11126. componentDatatype === ComponentDatatype.DOUBLE);
  11127. };
  11128. /**
  11129. * Creates a typed array corresponding to component data type.
  11130. *
  11131. * @param {ComponentDatatype} componentDatatype The component data type.
  11132. * @param {Number|Array} valuesOrLength The length of the array to create or an array.
  11133. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array.
  11134. *
  11135. * @exception {DeveloperError} componentDatatype is not a valid value.
  11136. *
  11137. * @example
  11138. * // creates a Float32Array with length of 100
  11139. * var typedArray = Cesium.ComponentDatatype.createTypedArray(Cesium.ComponentDatatype.FLOAT, 100);
  11140. */
  11141. ComponentDatatype.createTypedArray = function(componentDatatype, valuesOrLength) {
  11142. if (!defined(componentDatatype)) {
  11143. throw new DeveloperError('componentDatatype is required.');
  11144. }
  11145. if (!defined(valuesOrLength)) {
  11146. throw new DeveloperError('valuesOrLength is required.');
  11147. }
  11148. switch (componentDatatype) {
  11149. case ComponentDatatype.BYTE:
  11150. return new Int8Array(valuesOrLength);
  11151. case ComponentDatatype.UNSIGNED_BYTE:
  11152. return new Uint8Array(valuesOrLength);
  11153. case ComponentDatatype.SHORT:
  11154. return new Int16Array(valuesOrLength);
  11155. case ComponentDatatype.UNSIGNED_SHORT:
  11156. return new Uint16Array(valuesOrLength);
  11157. case ComponentDatatype.INT:
  11158. return new Int32Array(valuesOrLength);
  11159. case ComponentDatatype.UNSIGNED_INT:
  11160. return new Uint32Array(valuesOrLength);
  11161. case ComponentDatatype.FLOAT:
  11162. return new Float32Array(valuesOrLength);
  11163. case ComponentDatatype.DOUBLE:
  11164. return new Float64Array(valuesOrLength);
  11165. default:
  11166. throw new DeveloperError('componentDatatype is not a valid value.');
  11167. }
  11168. };
  11169. /**
  11170. * Creates a typed view of an array of bytes.
  11171. *
  11172. * @param {ComponentDatatype} componentDatatype The type of the view to create.
  11173. * @param {ArrayBuffer} buffer The buffer storage to use for the view.
  11174. * @param {Number} [byteOffset] The offset, in bytes, to the first element in the view.
  11175. * @param {Number} [length] The number of elements in the view.
  11176. * @returns {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} A typed array view of the buffer.
  11177. *
  11178. * @exception {DeveloperError} componentDatatype is not a valid value.
  11179. */
  11180. ComponentDatatype.createArrayBufferView = function(componentDatatype, buffer, byteOffset, length) {
  11181. if (!defined(componentDatatype)) {
  11182. throw new DeveloperError('componentDatatype is required.');
  11183. }
  11184. if (!defined(buffer)) {
  11185. throw new DeveloperError('buffer is required.');
  11186. }
  11187. byteOffset = defaultValue(byteOffset, 0);
  11188. length = defaultValue(length, (buffer.byteLength - byteOffset) / ComponentDatatype.getSizeInBytes(componentDatatype));
  11189. switch (componentDatatype) {
  11190. case ComponentDatatype.BYTE:
  11191. return new Int8Array(buffer, byteOffset, length);
  11192. case ComponentDatatype.UNSIGNED_BYTE:
  11193. return new Uint8Array(buffer, byteOffset, length);
  11194. case ComponentDatatype.SHORT:
  11195. return new Int16Array(buffer, byteOffset, length);
  11196. case ComponentDatatype.UNSIGNED_SHORT:
  11197. return new Uint16Array(buffer, byteOffset, length);
  11198. case ComponentDatatype.INT:
  11199. return new Int32Array(buffer, byteOffset, length);
  11200. case ComponentDatatype.UNSIGNED_INT:
  11201. return new Uint32Array(buffer, byteOffset, length);
  11202. case ComponentDatatype.FLOAT:
  11203. return new Float32Array(buffer, byteOffset, length);
  11204. case ComponentDatatype.DOUBLE:
  11205. return new Float64Array(buffer, byteOffset, length);
  11206. default:
  11207. throw new DeveloperError('componentDatatype is not a valid value.');
  11208. }
  11209. };
  11210. /**
  11211. * Get the ComponentDatatype from its name.
  11212. *
  11213. * @param {String} name The name of the ComponentDatatype.
  11214. * @returns {ComponentDatatype} The ComponentDatatype.
  11215. *
  11216. * @exception {DeveloperError} name is not a valid value.
  11217. */
  11218. ComponentDatatype.fromName = function(name) {
  11219. switch (name) {
  11220. case 'BYTE':
  11221. return ComponentDatatype.BYTE;
  11222. case 'UNSIGNED_BYTE':
  11223. return ComponentDatatype.UNSIGNED_BYTE;
  11224. case 'SHORT':
  11225. return ComponentDatatype.SHORT;
  11226. case 'UNSIGNED_SHORT':
  11227. return ComponentDatatype.UNSIGNED_SHORT;
  11228. case 'INT':
  11229. return ComponentDatatype.INT;
  11230. case 'UNSIGNED_INT':
  11231. return ComponentDatatype.UNSIGNED_INT;
  11232. case 'FLOAT':
  11233. return ComponentDatatype.FLOAT;
  11234. case 'DOUBLE':
  11235. return ComponentDatatype.DOUBLE;
  11236. default:
  11237. throw new DeveloperError('name is not a valid value.');
  11238. }
  11239. };
  11240. return freezeObject(ComponentDatatype);
  11241. });
  11242. /*global define*/
  11243. define('Core/GeometryType',[
  11244. './freezeObject'
  11245. ], function(
  11246. freezeObject) {
  11247. 'use strict';
  11248. /**
  11249. * @private
  11250. */
  11251. var GeometryType = {
  11252. NONE : 0,
  11253. TRIANGLES : 1,
  11254. LINES : 2,
  11255. POLYLINES : 3
  11256. };
  11257. return freezeObject(GeometryType);
  11258. });
  11259. /*global define*/
  11260. define('Core/PrimitiveType',[
  11261. './freezeObject',
  11262. './WebGLConstants'
  11263. ], function(
  11264. freezeObject,
  11265. WebGLConstants) {
  11266. 'use strict';
  11267. /**
  11268. * The type of a geometric primitive, i.e., points, lines, and triangles.
  11269. *
  11270. * @exports PrimitiveType
  11271. */
  11272. var PrimitiveType = {
  11273. /**
  11274. * Points primitive where each vertex (or index) is a separate point.
  11275. *
  11276. * @type {Number}
  11277. * @constant
  11278. */
  11279. POINTS : WebGLConstants.POINTS,
  11280. /**
  11281. * Lines primitive where each two vertices (or indices) is a line segment. Line segments are not necessarily connected.
  11282. *
  11283. * @type {Number}
  11284. * @constant
  11285. */
  11286. LINES : WebGLConstants.LINES,
  11287. /**
  11288. * Line loop primitive where each vertex (or index) after the first connects a line to
  11289. * the previous vertex, and the last vertex implicitly connects to the first.
  11290. *
  11291. * @type {Number}
  11292. * @constant
  11293. */
  11294. LINE_LOOP : WebGLConstants.LINE_LOOP,
  11295. /**
  11296. * Line strip primitive where each vertex (or index) after the first connects a line to the previous vertex.
  11297. *
  11298. * @type {Number}
  11299. * @constant
  11300. */
  11301. LINE_STRIP : WebGLConstants.LINE_STRIP,
  11302. /**
  11303. * Triangles primitive where each three vertices (or indices) is a triangle. Triangles do not necessarily share edges.
  11304. *
  11305. * @type {Number}
  11306. * @constant
  11307. */
  11308. TRIANGLES : WebGLConstants.TRIANGLES,
  11309. /**
  11310. * Triangle strip primitive where each vertex (or index) after the first two connect to
  11311. * the previous two vertices forming a triangle. For example, this can be used to model a wall.
  11312. *
  11313. * @type {Number}
  11314. * @constant
  11315. */
  11316. TRIANGLE_STRIP : WebGLConstants.TRIANGLE_STRIP,
  11317. /**
  11318. * Triangle fan primitive where each vertex (or index) after the first two connect to
  11319. * the previous vertex and the first vertex forming a triangle. For example, this can be used
  11320. * to model a cone or circle.
  11321. *
  11322. * @type {Number}
  11323. * @constant
  11324. */
  11325. TRIANGLE_FAN : WebGLConstants.TRIANGLE_FAN,
  11326. /**
  11327. * @private
  11328. */
  11329. validate : function(primitiveType) {
  11330. return primitiveType === PrimitiveType.POINTS ||
  11331. primitiveType === PrimitiveType.LINES ||
  11332. primitiveType === PrimitiveType.LINE_LOOP ||
  11333. primitiveType === PrimitiveType.LINE_STRIP ||
  11334. primitiveType === PrimitiveType.TRIANGLES ||
  11335. primitiveType === PrimitiveType.TRIANGLE_STRIP ||
  11336. primitiveType === PrimitiveType.TRIANGLE_FAN;
  11337. }
  11338. };
  11339. return freezeObject(PrimitiveType);
  11340. });
  11341. /*global define*/
  11342. define('Core/Geometry',[
  11343. './defaultValue',
  11344. './defined',
  11345. './DeveloperError',
  11346. './GeometryType',
  11347. './PrimitiveType'
  11348. ], function(
  11349. defaultValue,
  11350. defined,
  11351. DeveloperError,
  11352. GeometryType,
  11353. PrimitiveType) {
  11354. 'use strict';
  11355. /**
  11356. * A geometry representation with attributes forming vertices and optional index data
  11357. * defining primitives. Geometries and an {@link Appearance}, which describes the shading,
  11358. * can be assigned to a {@link Primitive} for visualization. A <code>Primitive</code> can
  11359. * be created from many heterogeneous - in many cases - geometries for performance.
  11360. * <p>
  11361. * Geometries can be transformed and optimized using functions in {@link GeometryPipeline}.
  11362. * </p>
  11363. *
  11364. * @alias Geometry
  11365. * @constructor
  11366. *
  11367. * @param {Object} options Object with the following properties:
  11368. * @param {GeometryAttributes} options.attributes Attributes, which make up the geometry's vertices.
  11369. * @param {PrimitiveType} [options.primitiveType=PrimitiveType.TRIANGLES] The type of primitives in the geometry.
  11370. * @param {Uint16Array|Uint32Array} [options.indices] Optional index data that determines the primitives in the geometry.
  11371. * @param {BoundingSphere} [options.boundingSphere] An optional bounding sphere that fully enclosed the geometry.
  11372. *
  11373. * @see PolygonGeometry
  11374. * @see RectangleGeometry
  11375. * @see EllipseGeometry
  11376. * @see CircleGeometry
  11377. * @see WallGeometry
  11378. * @see SimplePolylineGeometry
  11379. * @see BoxGeometry
  11380. * @see EllipsoidGeometry
  11381. *
  11382. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Geometry%20and%20Appearances.html|Geometry and Appearances Demo}
  11383. *
  11384. * @example
  11385. * // Create geometry with a position attribute and indexed lines.
  11386. * var positions = new Float64Array([
  11387. * 0.0, 0.0, 0.0,
  11388. * 7500000.0, 0.0, 0.0,
  11389. * 0.0, 7500000.0, 0.0
  11390. * ]);
  11391. *
  11392. * var geometry = new Cesium.Geometry({
  11393. * attributes : {
  11394. * position : new Cesium.GeometryAttribute({
  11395. * componentDatatype : Cesium.ComponentDatatype.DOUBLE,
  11396. * componentsPerAttribute : 3,
  11397. * values : positions
  11398. * })
  11399. * },
  11400. * indices : new Uint16Array([0, 1, 1, 2, 2, 0]),
  11401. * primitiveType : Cesium.PrimitiveType.LINES,
  11402. * boundingSphere : Cesium.BoundingSphere.fromVertices(positions)
  11403. * });
  11404. */
  11405. function Geometry(options) {
  11406. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  11407. if (!defined(options.attributes)) {
  11408. throw new DeveloperError('options.attributes is required.');
  11409. }
  11410. /**
  11411. * Attributes, which make up the geometry's vertices. Each property in this object corresponds to a
  11412. * {@link GeometryAttribute} containing the attribute's data.
  11413. * <p>
  11414. * Attributes are always stored non-interleaved in a Geometry.
  11415. * </p>
  11416. * <p>
  11417. * There are reserved attribute names with well-known semantics. The following attributes
  11418. * are created by a Geometry (depending on the provided {@link VertexFormat}.
  11419. * <ul>
  11420. * <li><code>position</code> - 3D vertex position. 64-bit floating-point (for precision). 3 components per attribute. See {@link VertexFormat#position}.</li>
  11421. * <li><code>normal</code> - Normal (normalized), commonly used for lighting. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#normal}.</li>
  11422. * <li><code>st</code> - 2D texture coordinate. 32-bit floating-point. 2 components per attribute. See {@link VertexFormat#st}.</li>
  11423. * <li><code>binormal</code> - Binormal (normalized), used for tangent-space effects like bump mapping. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#binormal}.</li>
  11424. * <li><code>tangent</code> - Tangent (normalized), used for tangent-space effects like bump mapping. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#tangent}.</li>
  11425. * </ul>
  11426. * </p>
  11427. * <p>
  11428. * The following attribute names are generally not created by a Geometry, but are added
  11429. * to a Geometry by a {@link Primitive} or {@link GeometryPipeline} functions to prepare
  11430. * the geometry for rendering.
  11431. * <ul>
  11432. * <li><code>position3DHigh</code> - High 32 bits for encoded 64-bit position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  11433. * <li><code>position3DLow</code> - Low 32 bits for encoded 64-bit position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  11434. * <li><code>position3DHigh</code> - High 32 bits for encoded 64-bit 2D (Columbus view) position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  11435. * <li><code>position2DLow</code> - Low 32 bits for encoded 64-bit 2D (Columbus view) position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  11436. * <li><code>color</code> - RGBA color (normalized) usually from {@link GeometryInstance#color}. 32-bit floating-point. 4 components per attribute.</li>
  11437. * <li><code>pickColor</code> - RGBA color used for picking. 32-bit floating-point. 4 components per attribute.</li>
  11438. * </ul>
  11439. * </p>
  11440. *
  11441. * @type GeometryAttributes
  11442. *
  11443. * @default undefined
  11444. *
  11445. *
  11446. * @example
  11447. * geometry.attributes.position = new Cesium.GeometryAttribute({
  11448. * componentDatatype : Cesium.ComponentDatatype.FLOAT,
  11449. * componentsPerAttribute : 3,
  11450. * values : new Float32Array(0)
  11451. * });
  11452. *
  11453. * @see GeometryAttribute
  11454. * @see VertexFormat
  11455. */
  11456. this.attributes = options.attributes;
  11457. /**
  11458. * Optional index data that - along with {@link Geometry#primitiveType} -
  11459. * determines the primitives in the geometry.
  11460. *
  11461. * @type Array
  11462. *
  11463. * @default undefined
  11464. */
  11465. this.indices = options.indices;
  11466. /**
  11467. * The type of primitives in the geometry. This is most often {@link PrimitiveType.TRIANGLES},
  11468. * but can varying based on the specific geometry.
  11469. *
  11470. * @type PrimitiveType
  11471. *
  11472. * @default undefined
  11473. */
  11474. this.primitiveType = defaultValue(options.primitiveType, PrimitiveType.TRIANGLES);
  11475. /**
  11476. * An optional bounding sphere that fully encloses the geometry. This is
  11477. * commonly used for culling.
  11478. *
  11479. * @type BoundingSphere
  11480. *
  11481. * @default undefined
  11482. */
  11483. this.boundingSphere = options.boundingSphere;
  11484. /**
  11485. * @private
  11486. */
  11487. this.geometryType = defaultValue(options.geometryType, GeometryType.NONE);
  11488. /**
  11489. * @private
  11490. */
  11491. this.boundingSphereCV = options.boundingSphereCV;
  11492. }
  11493. /**
  11494. * Computes the number of vertices in a geometry. The runtime is linear with
  11495. * respect to the number of attributes in a vertex, not the number of vertices.
  11496. *
  11497. * @param {Geometry} geometry The geometry.
  11498. * @returns {Number} The number of vertices in the geometry.
  11499. *
  11500. * @example
  11501. * var numVertices = Cesium.Geometry.computeNumberOfVertices(geometry);
  11502. */
  11503. Geometry.computeNumberOfVertices = function(geometry) {
  11504. if (!defined(geometry)) {
  11505. throw new DeveloperError('geometry is required.');
  11506. }
  11507. var numberOfVertices = -1;
  11508. for ( var property in geometry.attributes) {
  11509. if (geometry.attributes.hasOwnProperty(property) &&
  11510. defined(geometry.attributes[property]) &&
  11511. defined(geometry.attributes[property].values)) {
  11512. var attribute = geometry.attributes[property];
  11513. var num = attribute.values.length / attribute.componentsPerAttribute;
  11514. if ((numberOfVertices !== num) && (numberOfVertices !== -1)) {
  11515. throw new DeveloperError('All attribute lists must have the same number of attributes.');
  11516. }
  11517. numberOfVertices = num;
  11518. }
  11519. }
  11520. return numberOfVertices;
  11521. };
  11522. return Geometry;
  11523. });
  11524. /*global define*/
  11525. define('Core/GeometryAttribute',[
  11526. './defaultValue',
  11527. './defined',
  11528. './DeveloperError'
  11529. ], function(
  11530. defaultValue,
  11531. defined,
  11532. DeveloperError) {
  11533. 'use strict';
  11534. /**
  11535. * Values and type information for geometry attributes. A {@link Geometry}
  11536. * generally contains one or more attributes. All attributes together form
  11537. * the geometry's vertices.
  11538. *
  11539. * @alias GeometryAttribute
  11540. * @constructor
  11541. *
  11542. * @param {Object} [options] Object with the following properties:
  11543. * @param {ComponentDatatype} [options.componentDatatype] The datatype of each component in the attribute, e.g., individual elements in values.
  11544. * @param {Number} [options.componentsPerAttribute] A number between 1 and 4 that defines the number of components in an attributes.
  11545. * @param {Boolean} [options.normalize=false] When <code>true</code> and <code>componentDatatype</code> is an integer format, indicate that the components should be mapped to the range [0, 1] (unsigned) or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  11546. * @param {TypedArray} [options.values] The values for the attributes stored in a typed array.
  11547. *
  11548. * @exception {DeveloperError} options.componentsPerAttribute must be between 1 and 4.
  11549. *
  11550. *
  11551. * @example
  11552. * var geometry = new Cesium.Geometry({
  11553. * attributes : {
  11554. * position : new Cesium.GeometryAttribute({
  11555. * componentDatatype : Cesium.ComponentDatatype.FLOAT,
  11556. * componentsPerAttribute : 3,
  11557. * values : new Float32Array([
  11558. * 0.0, 0.0, 0.0,
  11559. * 7500000.0, 0.0, 0.0,
  11560. * 0.0, 7500000.0, 0.0
  11561. * ])
  11562. * })
  11563. * },
  11564. * primitiveType : Cesium.PrimitiveType.LINE_LOOP
  11565. * });
  11566. *
  11567. * @see Geometry
  11568. */
  11569. function GeometryAttribute(options) {
  11570. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  11571. if (!defined(options.componentDatatype)) {
  11572. throw new DeveloperError('options.componentDatatype is required.');
  11573. }
  11574. if (!defined(options.componentsPerAttribute)) {
  11575. throw new DeveloperError('options.componentsPerAttribute is required.');
  11576. }
  11577. if (options.componentsPerAttribute < 1 || options.componentsPerAttribute > 4) {
  11578. throw new DeveloperError('options.componentsPerAttribute must be between 1 and 4.');
  11579. }
  11580. if (!defined(options.values)) {
  11581. throw new DeveloperError('options.values is required.');
  11582. }
  11583. /**
  11584. * The datatype of each component in the attribute, e.g., individual elements in
  11585. * {@link GeometryAttribute#values}.
  11586. *
  11587. * @type ComponentDatatype
  11588. *
  11589. * @default undefined
  11590. */
  11591. this.componentDatatype = options.componentDatatype;
  11592. /**
  11593. * A number between 1 and 4 that defines the number of components in an attributes.
  11594. * For example, a position attribute with x, y, and z components would have 3 as
  11595. * shown in the code example.
  11596. *
  11597. * @type Number
  11598. *
  11599. * @default undefined
  11600. *
  11601. * @example
  11602. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  11603. * attribute.componentsPerAttribute = 3;
  11604. * attribute.values = new Float32Array([
  11605. * 0.0, 0.0, 0.0,
  11606. * 7500000.0, 0.0, 0.0,
  11607. * 0.0, 7500000.0, 0.0
  11608. * ]);
  11609. */
  11610. this.componentsPerAttribute = options.componentsPerAttribute;
  11611. /**
  11612. * When <code>true</code> and <code>componentDatatype</code> is an integer format,
  11613. * indicate that the components should be mapped to the range [0, 1] (unsigned)
  11614. * or [-1, 1] (signed) when they are accessed as floating-point for rendering.
  11615. * <p>
  11616. * This is commonly used when storing colors using {@link ComponentDatatype.UNSIGNED_BYTE}.
  11617. * </p>
  11618. *
  11619. * @type Boolean
  11620. *
  11621. * @default false
  11622. *
  11623. * @example
  11624. * attribute.componentDatatype = Cesium.ComponentDatatype.UNSIGNED_BYTE;
  11625. * attribute.componentsPerAttribute = 4;
  11626. * attribute.normalize = true;
  11627. * attribute.values = new Uint8Array([
  11628. * Cesium.Color.floatToByte(color.red),
  11629. * Cesium.Color.floatToByte(color.green),
  11630. * Cesium.Color.floatToByte(color.blue),
  11631. * Cesium.Color.floatToByte(color.alpha)
  11632. * ]);
  11633. */
  11634. this.normalize = defaultValue(options.normalize, false);
  11635. /**
  11636. * The values for the attributes stored in a typed array. In the code example,
  11637. * every three elements in <code>values</code> defines one attributes since
  11638. * <code>componentsPerAttribute</code> is 3.
  11639. *
  11640. * @type TypedArray
  11641. *
  11642. * @default undefined
  11643. *
  11644. * @example
  11645. * attribute.componentDatatype = Cesium.ComponentDatatype.FLOAT;
  11646. * attribute.componentsPerAttribute = 3;
  11647. * attribute.values = new Float32Array([
  11648. * 0.0, 0.0, 0.0,
  11649. * 7500000.0, 0.0, 0.0,
  11650. * 0.0, 7500000.0, 0.0
  11651. * ]);
  11652. */
  11653. this.values = options.values;
  11654. }
  11655. return GeometryAttribute;
  11656. });
  11657. /*global define*/
  11658. define('Core/GeometryAttributes',[
  11659. './defaultValue'
  11660. ], function(
  11661. defaultValue) {
  11662. 'use strict';
  11663. /**
  11664. * Attributes, which make up a geometry's vertices. Each property in this object corresponds to a
  11665. * {@link GeometryAttribute} containing the attribute's data.
  11666. * <p>
  11667. * Attributes are always stored non-interleaved in a Geometry.
  11668. * </p>
  11669. *
  11670. * @alias GeometryAttributes
  11671. * @constructor
  11672. */
  11673. function GeometryAttributes(options) {
  11674. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  11675. /**
  11676. * The 3D position attribute.
  11677. * <p>
  11678. * 64-bit floating-point (for precision). 3 components per attribute.
  11679. * </p>
  11680. *
  11681. * @type GeometryAttribute
  11682. *
  11683. * @default undefined
  11684. */
  11685. this.position = options.position;
  11686. /**
  11687. * The normal attribute (normalized), which is commonly used for lighting.
  11688. * <p>
  11689. * 32-bit floating-point. 3 components per attribute.
  11690. * </p>
  11691. *
  11692. * @type GeometryAttribute
  11693. *
  11694. * @default undefined
  11695. */
  11696. this.normal = options.normal;
  11697. /**
  11698. * The 2D texture coordinate attribute.
  11699. * <p>
  11700. * 32-bit floating-point. 2 components per attribute
  11701. * </p>
  11702. *
  11703. * @type GeometryAttribute
  11704. *
  11705. * @default undefined
  11706. */
  11707. this.st = options.st;
  11708. /**
  11709. * The binormal attribute (normalized), which is used for tangent-space effects like bump mapping.
  11710. * <p>
  11711. * 32-bit floating-point. 3 components per attribute.
  11712. * </p>
  11713. *
  11714. * @type GeometryAttribute
  11715. *
  11716. * @default undefined
  11717. */
  11718. this.binormal = options.binormal;
  11719. /**
  11720. * The tangent attribute (normalized), which is used for tangent-space effects like bump mapping.
  11721. * <p>
  11722. * 32-bit floating-point. 3 components per attribute.
  11723. * </p>
  11724. *
  11725. * @type GeometryAttribute
  11726. *
  11727. * @default undefined
  11728. */
  11729. this.tangent = options.tangent;
  11730. /**
  11731. * The color attribute.
  11732. * <p>
  11733. * 8-bit unsigned integer. 4 components per attribute.
  11734. * </p>
  11735. *
  11736. * @type GeometryAttribute
  11737. *
  11738. * @default undefined
  11739. */
  11740. this.color = options.color;
  11741. }
  11742. return GeometryAttributes;
  11743. });
  11744. /*global define*/
  11745. define('Core/IndexDatatype',[
  11746. './defined',
  11747. './DeveloperError',
  11748. './freezeObject',
  11749. './Math',
  11750. './WebGLConstants'
  11751. ], function(
  11752. defined,
  11753. DeveloperError,
  11754. freezeObject,
  11755. CesiumMath,
  11756. WebGLConstants) {
  11757. 'use strict';
  11758. /**
  11759. * Constants for WebGL index datatypes. These corresponds to the
  11760. * <code>type</code> parameter of {@link http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawElements.xml|drawElements}.
  11761. *
  11762. * @exports IndexDatatype
  11763. */
  11764. var IndexDatatype = {
  11765. /**
  11766. * 8-bit unsigned byte corresponding to <code>UNSIGNED_BYTE</code> and the type
  11767. * of an element in <code>Uint8Array</code>.
  11768. *
  11769. * @type {Number}
  11770. * @constant
  11771. */
  11772. UNSIGNED_BYTE : WebGLConstants.UNSIGNED_BYTE,
  11773. /**
  11774. * 16-bit unsigned short corresponding to <code>UNSIGNED_SHORT</code> and the type
  11775. * of an element in <code>Uint16Array</code>.
  11776. *
  11777. * @type {Number}
  11778. * @constant
  11779. */
  11780. UNSIGNED_SHORT : WebGLConstants.UNSIGNED_SHORT,
  11781. /**
  11782. * 32-bit unsigned int corresponding to <code>UNSIGNED_INT</code> and the type
  11783. * of an element in <code>Uint32Array</code>.
  11784. *
  11785. * @type {Number}
  11786. * @constant
  11787. */
  11788. UNSIGNED_INT : WebGLConstants.UNSIGNED_INT
  11789. };
  11790. /**
  11791. * Returns the size, in bytes, of the corresponding datatype.
  11792. *
  11793. * @param {IndexDatatype} indexDatatype The index datatype to get the size of.
  11794. * @returns {Number} The size in bytes.
  11795. *
  11796. * @example
  11797. * // Returns 2
  11798. * var size = Cesium.IndexDatatype.getSizeInBytes(Cesium.IndexDatatype.UNSIGNED_SHORT);
  11799. */
  11800. IndexDatatype.getSizeInBytes = function(indexDatatype) {
  11801. switch(indexDatatype) {
  11802. case IndexDatatype.UNSIGNED_BYTE:
  11803. return Uint8Array.BYTES_PER_ELEMENT;
  11804. case IndexDatatype.UNSIGNED_SHORT:
  11805. return Uint16Array.BYTES_PER_ELEMENT;
  11806. case IndexDatatype.UNSIGNED_INT:
  11807. return Uint32Array.BYTES_PER_ELEMENT;
  11808. }
  11809. throw new DeveloperError('indexDatatype is required and must be a valid IndexDatatype constant.');
  11810. };
  11811. /**
  11812. * Validates that the provided index datatype is a valid {@link IndexDatatype}.
  11813. *
  11814. * @param {IndexDatatype} indexDatatype The index datatype to validate.
  11815. * @returns {Boolean} <code>true</code> if the provided index datatype is a valid value; otherwise, <code>false</code>.
  11816. *
  11817. * @example
  11818. * if (!Cesium.IndexDatatype.validate(indexDatatype)) {
  11819. * throw new Cesium.DeveloperError('indexDatatype must be a valid value.');
  11820. * }
  11821. */
  11822. IndexDatatype.validate = function(indexDatatype) {
  11823. return defined(indexDatatype) &&
  11824. (indexDatatype === IndexDatatype.UNSIGNED_BYTE ||
  11825. indexDatatype === IndexDatatype.UNSIGNED_SHORT ||
  11826. indexDatatype === IndexDatatype.UNSIGNED_INT);
  11827. };
  11828. /**
  11829. * Creates a typed array that will store indices, using either <code><Uint16Array</code>
  11830. * or <code>Uint32Array</code> depending on the number of vertices.
  11831. *
  11832. * @param {Number} numberOfVertices Number of vertices that the indices will reference.
  11833. * @param {Any} indicesLengthOrArray Passed through to the typed array constructor.
  11834. * @returns {Uint16Array|Uint32Array} A <code>Uint16Array</code> or <code>Uint32Array</code> constructed with <code>indicesLengthOrArray</code>.
  11835. *
  11836. * @example
  11837. * this.indices = Cesium.IndexDatatype.createTypedArray(positions.length / 3, numberOfIndices);
  11838. */
  11839. IndexDatatype.createTypedArray = function(numberOfVertices, indicesLengthOrArray) {
  11840. if (!defined(numberOfVertices)) {
  11841. throw new DeveloperError('numberOfVertices is required.');
  11842. }
  11843. if (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  11844. return new Uint32Array(indicesLengthOrArray);
  11845. }
  11846. return new Uint16Array(indicesLengthOrArray);
  11847. };
  11848. /**
  11849. * Creates a typed array from a source array buffer. The resulting typed array will store indices, using either <code><Uint16Array</code>
  11850. * or <code>Uint32Array</code> depending on the number of vertices.
  11851. *
  11852. * @param {Number} numberOfVertices Number of vertices that the indices will reference.
  11853. * @param {ArrayBuffer} sourceArray Passed through to the typed array constructor.
  11854. * @param {Number} byteOffset Passed through to the typed array constructor.
  11855. * @param {Number} length Passed through to the typed array constructor.
  11856. * @returns {Uint16Array|Uint32Array} A <code>Uint16Array</code> or <code>Uint32Array</code> constructed with <code>sourceArray</code>, <code>byteOffset</code>, and <code>length</code>.
  11857. *
  11858. */
  11859. IndexDatatype.createTypedArrayFromArrayBuffer = function(numberOfVertices, sourceArray, byteOffset, length) {
  11860. if (!defined(numberOfVertices)) {
  11861. throw new DeveloperError('numberOfVertices is required.');
  11862. }
  11863. if (!defined(sourceArray)) {
  11864. throw new DeveloperError('sourceArray is required.');
  11865. }
  11866. if (!defined(byteOffset)) {
  11867. throw new DeveloperError('byteOffset is required.');
  11868. }
  11869. if (numberOfVertices >= CesiumMath.SIXTY_FOUR_KILOBYTES) {
  11870. return new Uint32Array(sourceArray, byteOffset, length);
  11871. }
  11872. return new Uint16Array(sourceArray, byteOffset, length);
  11873. };
  11874. return freezeObject(IndexDatatype);
  11875. });
  11876. /*global define*/
  11877. define('Core/AxisAlignedBoundingBox',[
  11878. './Cartesian3',
  11879. './defaultValue',
  11880. './defined',
  11881. './DeveloperError',
  11882. './Intersect'
  11883. ], function(
  11884. Cartesian3,
  11885. defaultValue,
  11886. defined,
  11887. DeveloperError,
  11888. Intersect) {
  11889. 'use strict';
  11890. /**
  11891. * Creates an instance of an AxisAlignedBoundingBox from the minimum and maximum points along the x, y, and z axes.
  11892. * @alias AxisAlignedBoundingBox
  11893. * @constructor
  11894. *
  11895. * @param {Cartesian3} [minimum=Cartesian3.ZERO] The minimum point along the x, y, and z axes.
  11896. * @param {Cartesian3} [maximum=Cartesian3.ZERO] The maximum point along the x, y, and z axes.
  11897. * @param {Cartesian3} [center] The center of the box; automatically computed if not supplied.
  11898. *
  11899. * @see BoundingSphere
  11900. * @see BoundingRectangle
  11901. */
  11902. function AxisAlignedBoundingBox(minimum, maximum, center) {
  11903. /**
  11904. * The minimum point defining the bounding box.
  11905. * @type {Cartesian3}
  11906. * @default {@link Cartesian3.ZERO}
  11907. */
  11908. this.minimum = Cartesian3.clone(defaultValue(minimum, Cartesian3.ZERO));
  11909. /**
  11910. * The maximum point defining the bounding box.
  11911. * @type {Cartesian3}
  11912. * @default {@link Cartesian3.ZERO}
  11913. */
  11914. this.maximum = Cartesian3.clone(defaultValue(maximum, Cartesian3.ZERO));
  11915. //If center was not defined, compute it.
  11916. if (!defined(center)) {
  11917. center = Cartesian3.add(this.minimum, this.maximum, new Cartesian3());
  11918. Cartesian3.multiplyByScalar(center, 0.5, center);
  11919. } else {
  11920. center = Cartesian3.clone(center);
  11921. }
  11922. /**
  11923. * The center point of the bounding box.
  11924. * @type {Cartesian3}
  11925. */
  11926. this.center = center;
  11927. }
  11928. /**
  11929. * Computes an instance of an AxisAlignedBoundingBox. The box is determined by
  11930. * finding the points spaced the farthest apart on the x, y, and z axes.
  11931. *
  11932. * @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.
  11933. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  11934. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  11935. *
  11936. * @example
  11937. * // Compute an axis aligned bounding box enclosing two points.
  11938. * var box = Cesium.AxisAlignedBoundingBox.fromPoints([new Cesium.Cartesian3(2, 0, 0), new Cesium.Cartesian3(-2, 0, 0)]);
  11939. */
  11940. AxisAlignedBoundingBox.fromPoints = function(positions, result) {
  11941. if (!defined(result)) {
  11942. result = new AxisAlignedBoundingBox();
  11943. }
  11944. if (!defined(positions) || positions.length === 0) {
  11945. result.minimum = Cartesian3.clone(Cartesian3.ZERO, result.minimum);
  11946. result.maximum = Cartesian3.clone(Cartesian3.ZERO, result.maximum);
  11947. result.center = Cartesian3.clone(Cartesian3.ZERO, result.center);
  11948. return result;
  11949. }
  11950. var minimumX = positions[0].x;
  11951. var minimumY = positions[0].y;
  11952. var minimumZ = positions[0].z;
  11953. var maximumX = positions[0].x;
  11954. var maximumY = positions[0].y;
  11955. var maximumZ = positions[0].z;
  11956. var length = positions.length;
  11957. for ( var i = 1; i < length; i++) {
  11958. var p = positions[i];
  11959. var x = p.x;
  11960. var y = p.y;
  11961. var z = p.z;
  11962. minimumX = Math.min(x, minimumX);
  11963. maximumX = Math.max(x, maximumX);
  11964. minimumY = Math.min(y, minimumY);
  11965. maximumY = Math.max(y, maximumY);
  11966. minimumZ = Math.min(z, minimumZ);
  11967. maximumZ = Math.max(z, maximumZ);
  11968. }
  11969. var minimum = result.minimum;
  11970. minimum.x = minimumX;
  11971. minimum.y = minimumY;
  11972. minimum.z = minimumZ;
  11973. var maximum = result.maximum;
  11974. maximum.x = maximumX;
  11975. maximum.y = maximumY;
  11976. maximum.z = maximumZ;
  11977. var center = Cartesian3.add(minimum, maximum, result.center);
  11978. Cartesian3.multiplyByScalar(center, 0.5, center);
  11979. return result;
  11980. };
  11981. /**
  11982. * Duplicates a AxisAlignedBoundingBox instance.
  11983. *
  11984. * @param {AxisAlignedBoundingBox} box The bounding box to duplicate.
  11985. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  11986. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if none was provided. (Returns undefined if box is undefined)
  11987. */
  11988. AxisAlignedBoundingBox.clone = function(box, result) {
  11989. if (!defined(box)) {
  11990. return undefined;
  11991. }
  11992. if (!defined(result)) {
  11993. return new AxisAlignedBoundingBox(box.minimum, box.maximum);
  11994. }
  11995. result.minimum = Cartesian3.clone(box.minimum, result.minimum);
  11996. result.maximum = Cartesian3.clone(box.maximum, result.maximum);
  11997. result.center = Cartesian3.clone(box.center, result.center);
  11998. return result;
  11999. };
  12000. /**
  12001. * Compares the provided AxisAlignedBoundingBox componentwise and returns
  12002. * <code>true</code> if they are equal, <code>false</code> otherwise.
  12003. *
  12004. * @param {AxisAlignedBoundingBox} [left] The first AxisAlignedBoundingBox.
  12005. * @param {AxisAlignedBoundingBox} [right] The second AxisAlignedBoundingBox.
  12006. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  12007. */
  12008. AxisAlignedBoundingBox.equals = function(left, right) {
  12009. return (left === right) ||
  12010. ((defined(left)) &&
  12011. (defined(right)) &&
  12012. Cartesian3.equals(left.center, right.center) &&
  12013. Cartesian3.equals(left.minimum, right.minimum) &&
  12014. Cartesian3.equals(left.maximum, right.maximum));
  12015. };
  12016. var intersectScratch = new Cartesian3();
  12017. /**
  12018. * Determines which side of a plane a box is located.
  12019. *
  12020. * @param {AxisAlignedBoundingBox} box The bounding box to test.
  12021. * @param {Plane} plane The plane to test against.
  12022. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  12023. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  12024. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  12025. * intersects the plane.
  12026. */
  12027. AxisAlignedBoundingBox.intersectPlane = function(box, plane) {
  12028. if (!defined(box)) {
  12029. throw new DeveloperError('box is required.');
  12030. }
  12031. if (!defined(plane)) {
  12032. throw new DeveloperError('plane is required.');
  12033. }
  12034. intersectScratch = Cartesian3.subtract(box.maximum, box.minimum, intersectScratch);
  12035. var h = Cartesian3.multiplyByScalar(intersectScratch, 0.5, intersectScratch); //The positive half diagonal
  12036. var normal = plane.normal;
  12037. var e = h.x * Math.abs(normal.x) + h.y * Math.abs(normal.y) + h.z * Math.abs(normal.z);
  12038. var s = Cartesian3.dot(box.center, normal) + plane.distance; //signed distance from center
  12039. if (s - e > 0) {
  12040. return Intersect.INSIDE;
  12041. }
  12042. if (s + e < 0) {
  12043. //Not in front because normals point inward
  12044. return Intersect.OUTSIDE;
  12045. }
  12046. return Intersect.INTERSECTING;
  12047. };
  12048. /**
  12049. * Duplicates this AxisAlignedBoundingBox instance.
  12050. *
  12051. * @param {AxisAlignedBoundingBox} [result] The object onto which to store the result.
  12052. * @returns {AxisAlignedBoundingBox} The modified result parameter or a new AxisAlignedBoundingBox instance if one was not provided.
  12053. */
  12054. AxisAlignedBoundingBox.prototype.clone = function(result) {
  12055. return AxisAlignedBoundingBox.clone(this, result);
  12056. };
  12057. /**
  12058. * Determines which side of a plane this box is located.
  12059. *
  12060. * @param {Plane} plane The plane to test against.
  12061. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  12062. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  12063. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  12064. * intersects the plane.
  12065. */
  12066. AxisAlignedBoundingBox.prototype.intersectPlane = function(plane) {
  12067. return AxisAlignedBoundingBox.intersectPlane(this, plane);
  12068. };
  12069. /**
  12070. * Compares this AxisAlignedBoundingBox against the provided AxisAlignedBoundingBox componentwise and returns
  12071. * <code>true</code> if they are equal, <code>false</code> otherwise.
  12072. *
  12073. * @param {AxisAlignedBoundingBox} [right] The right hand side AxisAlignedBoundingBox.
  12074. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  12075. */
  12076. AxisAlignedBoundingBox.prototype.equals = function(right) {
  12077. return AxisAlignedBoundingBox.equals(this, right);
  12078. };
  12079. return AxisAlignedBoundingBox;
  12080. });
  12081. /*global define*/
  12082. define('Core/Cartesian2',[
  12083. './defaultValue',
  12084. './defined',
  12085. './DeveloperError',
  12086. './freezeObject',
  12087. './Math'
  12088. ], function(
  12089. defaultValue,
  12090. defined,
  12091. DeveloperError,
  12092. freezeObject,
  12093. CesiumMath) {
  12094. 'use strict';
  12095. /**
  12096. * A 2D Cartesian point.
  12097. * @alias Cartesian2
  12098. * @constructor
  12099. *
  12100. * @param {Number} [x=0.0] The X component.
  12101. * @param {Number} [y=0.0] The Y component.
  12102. *
  12103. * @see Cartesian3
  12104. * @see Cartesian4
  12105. * @see Packable
  12106. */
  12107. function Cartesian2(x, y) {
  12108. /**
  12109. * The X component.
  12110. * @type {Number}
  12111. * @default 0.0
  12112. */
  12113. this.x = defaultValue(x, 0.0);
  12114. /**
  12115. * The Y component.
  12116. * @type {Number}
  12117. * @default 0.0
  12118. */
  12119. this.y = defaultValue(y, 0.0);
  12120. }
  12121. /**
  12122. * Creates a Cartesian2 instance from x and y coordinates.
  12123. *
  12124. * @param {Number} x The x coordinate.
  12125. * @param {Number} y The y coordinate.
  12126. * @param {Cartesian2} [result] The object onto which to store the result.
  12127. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  12128. */
  12129. Cartesian2.fromElements = function(x, y, result) {
  12130. if (!defined(result)) {
  12131. return new Cartesian2(x, y);
  12132. }
  12133. result.x = x;
  12134. result.y = y;
  12135. return result;
  12136. };
  12137. /**
  12138. * Duplicates a Cartesian2 instance.
  12139. *
  12140. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  12141. * @param {Cartesian2} [result] The object onto which to store the result.
  12142. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  12143. */
  12144. Cartesian2.clone = function(cartesian, result) {
  12145. if (!defined(cartesian)) {
  12146. return undefined;
  12147. }
  12148. if (!defined(result)) {
  12149. return new Cartesian2(cartesian.x, cartesian.y);
  12150. }
  12151. result.x = cartesian.x;
  12152. result.y = cartesian.y;
  12153. return result;
  12154. };
  12155. /**
  12156. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  12157. * x and y properties of the Cartesian3 and drops z.
  12158. * @function
  12159. *
  12160. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  12161. * @param {Cartesian2} [result] The object onto which to store the result.
  12162. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  12163. */
  12164. Cartesian2.fromCartesian3 = Cartesian2.clone;
  12165. /**
  12166. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  12167. * x and y properties of the Cartesian4 and drops z and w.
  12168. * @function
  12169. *
  12170. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  12171. * @param {Cartesian2} [result] The object onto which to store the result.
  12172. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  12173. */
  12174. Cartesian2.fromCartesian4 = Cartesian2.clone;
  12175. /**
  12176. * The number of elements used to pack the object into an array.
  12177. * @type {Number}
  12178. */
  12179. Cartesian2.packedLength = 2;
  12180. /**
  12181. * Stores the provided instance into the provided array.
  12182. *
  12183. * @param {Cartesian2} value The value to pack.
  12184. * @param {Number[]} array The array to pack into.
  12185. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  12186. *
  12187. * @returns {Number[]} The array that was packed into
  12188. */
  12189. Cartesian2.pack = function(value, array, startingIndex) {
  12190. if (!defined(value)) {
  12191. throw new DeveloperError('value is required');
  12192. }
  12193. if (!defined(array)) {
  12194. throw new DeveloperError('array is required');
  12195. }
  12196. startingIndex = defaultValue(startingIndex, 0);
  12197. array[startingIndex++] = value.x;
  12198. array[startingIndex] = value.y;
  12199. return array;
  12200. };
  12201. /**
  12202. * Retrieves an instance from a packed array.
  12203. *
  12204. * @param {Number[]} array The packed array.
  12205. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  12206. * @param {Cartesian2} [result] The object into which to store the result.
  12207. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  12208. */
  12209. Cartesian2.unpack = function(array, startingIndex, result) {
  12210. if (!defined(array)) {
  12211. throw new DeveloperError('array is required');
  12212. }
  12213. startingIndex = defaultValue(startingIndex, 0);
  12214. if (!defined(result)) {
  12215. result = new Cartesian2();
  12216. }
  12217. result.x = array[startingIndex++];
  12218. result.y = array[startingIndex];
  12219. return result;
  12220. };
  12221. /**
  12222. * Flattens an array of Cartesian2s into and array of components.
  12223. *
  12224. * @param {Cartesian2[]} array The array of cartesians to pack.
  12225. * @param {Number[]} result The array onto which to store the result.
  12226. * @returns {Number[]} The packed array.
  12227. */
  12228. Cartesian2.packArray = function(array, result) {
  12229. if (!defined(array)) {
  12230. throw new DeveloperError('array is required');
  12231. }
  12232. var length = array.length;
  12233. if (!defined(result)) {
  12234. result = new Array(length * 2);
  12235. } else {
  12236. result.length = length * 2;
  12237. }
  12238. for (var i = 0; i < length; ++i) {
  12239. Cartesian2.pack(array[i], result, i * 2);
  12240. }
  12241. return result;
  12242. };
  12243. /**
  12244. * Unpacks an array of cartesian components into and array of Cartesian2s.
  12245. *
  12246. * @param {Number[]} array The array of components to unpack.
  12247. * @param {Cartesian2[]} result The array onto which to store the result.
  12248. * @returns {Cartesian2[]} The unpacked array.
  12249. */
  12250. Cartesian2.unpackArray = function(array, result) {
  12251. if (!defined(array)) {
  12252. throw new DeveloperError('array is required');
  12253. }
  12254. var length = array.length;
  12255. if (!defined(result)) {
  12256. result = new Array(length / 2);
  12257. } else {
  12258. result.length = length / 2;
  12259. }
  12260. for (var i = 0; i < length; i += 2) {
  12261. var index = i / 2;
  12262. result[index] = Cartesian2.unpack(array, i, result[index]);
  12263. }
  12264. return result;
  12265. };
  12266. /**
  12267. * Creates a Cartesian2 from two consecutive elements in an array.
  12268. * @function
  12269. *
  12270. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  12271. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  12272. * @param {Cartesian2} [result] The object onto which to store the result.
  12273. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  12274. *
  12275. * @example
  12276. * // Create a Cartesian2 with (1.0, 2.0)
  12277. * var v = [1.0, 2.0];
  12278. * var p = Cesium.Cartesian2.fromArray(v);
  12279. *
  12280. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  12281. * var v2 = [0.0, 0.0, 1.0, 2.0];
  12282. * var p2 = Cesium.Cartesian2.fromArray(v2, 2);
  12283. */
  12284. Cartesian2.fromArray = Cartesian2.unpack;
  12285. /**
  12286. * Computes the value of the maximum component for the supplied Cartesian.
  12287. *
  12288. * @param {Cartesian2} cartesian The cartesian to use.
  12289. * @returns {Number} The value of the maximum component.
  12290. */
  12291. Cartesian2.maximumComponent = function(cartesian) {
  12292. if (!defined(cartesian)) {
  12293. throw new DeveloperError('cartesian is required');
  12294. }
  12295. return Math.max(cartesian.x, cartesian.y);
  12296. };
  12297. /**
  12298. * Computes the value of the minimum component for the supplied Cartesian.
  12299. *
  12300. * @param {Cartesian2} cartesian The cartesian to use.
  12301. * @returns {Number} The value of the minimum component.
  12302. */
  12303. Cartesian2.minimumComponent = function(cartesian) {
  12304. if (!defined(cartesian)) {
  12305. throw new DeveloperError('cartesian is required');
  12306. }
  12307. return Math.min(cartesian.x, cartesian.y);
  12308. };
  12309. /**
  12310. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  12311. *
  12312. * @param {Cartesian2} first A cartesian to compare.
  12313. * @param {Cartesian2} second A cartesian to compare.
  12314. * @param {Cartesian2} result The object into which to store the result.
  12315. * @returns {Cartesian2} A cartesian with the minimum components.
  12316. */
  12317. Cartesian2.minimumByComponent = function(first, second, result) {
  12318. if (!defined(first)) {
  12319. throw new DeveloperError('first is required.');
  12320. }
  12321. if (!defined(second)) {
  12322. throw new DeveloperError('second is required.');
  12323. }
  12324. if (!defined(result)) {
  12325. throw new DeveloperError('result is required.');
  12326. }
  12327. result.x = Math.min(first.x, second.x);
  12328. result.y = Math.min(first.y, second.y);
  12329. return result;
  12330. };
  12331. /**
  12332. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  12333. *
  12334. * @param {Cartesian2} first A cartesian to compare.
  12335. * @param {Cartesian2} second A cartesian to compare.
  12336. * @param {Cartesian2} result The object into which to store the result.
  12337. * @returns {Cartesian2} A cartesian with the maximum components.
  12338. */
  12339. Cartesian2.maximumByComponent = function(first, second, result) {
  12340. if (!defined(first)) {
  12341. throw new DeveloperError('first is required.');
  12342. }
  12343. if (!defined(second)) {
  12344. throw new DeveloperError('second is required.');
  12345. }
  12346. if (!defined(result)) {
  12347. throw new DeveloperError('result is required.');
  12348. }
  12349. result.x = Math.max(first.x, second.x);
  12350. result.y = Math.max(first.y, second.y);
  12351. return result;
  12352. };
  12353. /**
  12354. * Computes the provided Cartesian's squared magnitude.
  12355. *
  12356. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  12357. * @returns {Number} The squared magnitude.
  12358. */
  12359. Cartesian2.magnitudeSquared = function(cartesian) {
  12360. if (!defined(cartesian)) {
  12361. throw new DeveloperError('cartesian is required');
  12362. }
  12363. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  12364. };
  12365. /**
  12366. * Computes the Cartesian's magnitude (length).
  12367. *
  12368. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  12369. * @returns {Number} The magnitude.
  12370. */
  12371. Cartesian2.magnitude = function(cartesian) {
  12372. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  12373. };
  12374. var distanceScratch = new Cartesian2();
  12375. /**
  12376. * Computes the distance between two points.
  12377. *
  12378. * @param {Cartesian2} left The first point to compute the distance from.
  12379. * @param {Cartesian2} right The second point to compute the distance to.
  12380. * @returns {Number} The distance between two points.
  12381. *
  12382. * @example
  12383. * // Returns 1.0
  12384. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  12385. */
  12386. Cartesian2.distance = function(left, right) {
  12387. if (!defined(left) || !defined(right)) {
  12388. throw new DeveloperError('left and right are required.');
  12389. }
  12390. Cartesian2.subtract(left, right, distanceScratch);
  12391. return Cartesian2.magnitude(distanceScratch);
  12392. };
  12393. /**
  12394. * Computes the squared distance between two points. Comparing squared distances
  12395. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  12396. *
  12397. * @param {Cartesian2} left The first point to compute the distance from.
  12398. * @param {Cartesian2} right The second point to compute the distance to.
  12399. * @returns {Number} The distance between two points.
  12400. *
  12401. * @example
  12402. * // Returns 4.0, not 2.0
  12403. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  12404. */
  12405. Cartesian2.distanceSquared = function(left, right) {
  12406. if (!defined(left) || !defined(right)) {
  12407. throw new DeveloperError('left and right are required.');
  12408. }
  12409. Cartesian2.subtract(left, right, distanceScratch);
  12410. return Cartesian2.magnitudeSquared(distanceScratch);
  12411. };
  12412. /**
  12413. * Computes the normalized form of the supplied Cartesian.
  12414. *
  12415. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  12416. * @param {Cartesian2} result The object onto which to store the result.
  12417. * @returns {Cartesian2} The modified result parameter.
  12418. */
  12419. Cartesian2.normalize = function(cartesian, result) {
  12420. if (!defined(cartesian)) {
  12421. throw new DeveloperError('cartesian is required');
  12422. }
  12423. if (!defined(result)) {
  12424. throw new DeveloperError('result is required');
  12425. }
  12426. var magnitude = Cartesian2.magnitude(cartesian);
  12427. result.x = cartesian.x / magnitude;
  12428. result.y = cartesian.y / magnitude;
  12429. if (isNaN(result.x) || isNaN(result.y)) {
  12430. throw new DeveloperError('normalized result is not a number');
  12431. }
  12432. return result;
  12433. };
  12434. /**
  12435. * Computes the dot (scalar) product of two Cartesians.
  12436. *
  12437. * @param {Cartesian2} left The first Cartesian.
  12438. * @param {Cartesian2} right The second Cartesian.
  12439. * @returns {Number} The dot product.
  12440. */
  12441. Cartesian2.dot = function(left, right) {
  12442. if (!defined(left)) {
  12443. throw new DeveloperError('left is required');
  12444. }
  12445. if (!defined(right)) {
  12446. throw new DeveloperError('right is required');
  12447. }
  12448. return left.x * right.x + left.y * right.y;
  12449. };
  12450. /**
  12451. * Computes the componentwise product of two Cartesians.
  12452. *
  12453. * @param {Cartesian2} left The first Cartesian.
  12454. * @param {Cartesian2} right The second Cartesian.
  12455. * @param {Cartesian2} result The object onto which to store the result.
  12456. * @returns {Cartesian2} The modified result parameter.
  12457. */
  12458. Cartesian2.multiplyComponents = function(left, right, result) {
  12459. if (!defined(left)) {
  12460. throw new DeveloperError('left is required');
  12461. }
  12462. if (!defined(right)) {
  12463. throw new DeveloperError('right is required');
  12464. }
  12465. if (!defined(result)) {
  12466. throw new DeveloperError('result is required');
  12467. }
  12468. result.x = left.x * right.x;
  12469. result.y = left.y * right.y;
  12470. return result;
  12471. };
  12472. /**
  12473. * Computes the componentwise quotient of two Cartesians.
  12474. *
  12475. * @param {Cartesian2} left The first Cartesian.
  12476. * @param {Cartesian2} right The second Cartesian.
  12477. * @param {Cartesian2} result The object onto which to store the result.
  12478. * @returns {Cartesian2} The modified result parameter.
  12479. */
  12480. Cartesian2.divideComponents = function(left, right, result) {
  12481. if (!defined(left)) {
  12482. throw new DeveloperError('left is required');
  12483. }
  12484. if (!defined(right)) {
  12485. throw new DeveloperError('right is required');
  12486. }
  12487. if (!defined(result)) {
  12488. throw new DeveloperError('result is required');
  12489. }
  12490. result.x = left.x / right.x;
  12491. result.y = left.y / right.y;
  12492. return result;
  12493. };
  12494. /**
  12495. * Computes the componentwise sum of two Cartesians.
  12496. *
  12497. * @param {Cartesian2} left The first Cartesian.
  12498. * @param {Cartesian2} right The second Cartesian.
  12499. * @param {Cartesian2} result The object onto which to store the result.
  12500. * @returns {Cartesian2} The modified result parameter.
  12501. */
  12502. Cartesian2.add = function(left, right, result) {
  12503. if (!defined(left)) {
  12504. throw new DeveloperError('left is required');
  12505. }
  12506. if (!defined(right)) {
  12507. throw new DeveloperError('right is required');
  12508. }
  12509. if (!defined(result)) {
  12510. throw new DeveloperError('result is required');
  12511. }
  12512. result.x = left.x + right.x;
  12513. result.y = left.y + right.y;
  12514. return result;
  12515. };
  12516. /**
  12517. * Computes the componentwise difference of two Cartesians.
  12518. *
  12519. * @param {Cartesian2} left The first Cartesian.
  12520. * @param {Cartesian2} right The second Cartesian.
  12521. * @param {Cartesian2} result The object onto which to store the result.
  12522. * @returns {Cartesian2} The modified result parameter.
  12523. */
  12524. Cartesian2.subtract = function(left, right, result) {
  12525. if (!defined(left)) {
  12526. throw new DeveloperError('left is required');
  12527. }
  12528. if (!defined(right)) {
  12529. throw new DeveloperError('right is required');
  12530. }
  12531. if (!defined(result)) {
  12532. throw new DeveloperError('result is required');
  12533. }
  12534. result.x = left.x - right.x;
  12535. result.y = left.y - right.y;
  12536. return result;
  12537. };
  12538. /**
  12539. * Multiplies the provided Cartesian componentwise by the provided scalar.
  12540. *
  12541. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  12542. * @param {Number} scalar The scalar to multiply with.
  12543. * @param {Cartesian2} result The object onto which to store the result.
  12544. * @returns {Cartesian2} The modified result parameter.
  12545. */
  12546. Cartesian2.multiplyByScalar = function(cartesian, scalar, result) {
  12547. if (!defined(cartesian)) {
  12548. throw new DeveloperError('cartesian is required');
  12549. }
  12550. if (typeof scalar !== 'number') {
  12551. throw new DeveloperError('scalar is required and must be a number.');
  12552. }
  12553. if (!defined(result)) {
  12554. throw new DeveloperError('result is required');
  12555. }
  12556. result.x = cartesian.x * scalar;
  12557. result.y = cartesian.y * scalar;
  12558. return result;
  12559. };
  12560. /**
  12561. * Divides the provided Cartesian componentwise by the provided scalar.
  12562. *
  12563. * @param {Cartesian2} cartesian The Cartesian to be divided.
  12564. * @param {Number} scalar The scalar to divide by.
  12565. * @param {Cartesian2} result The object onto which to store the result.
  12566. * @returns {Cartesian2} The modified result parameter.
  12567. */
  12568. Cartesian2.divideByScalar = function(cartesian, scalar, result) {
  12569. if (!defined(cartesian)) {
  12570. throw new DeveloperError('cartesian is required');
  12571. }
  12572. if (typeof scalar !== 'number') {
  12573. throw new DeveloperError('scalar is required and must be a number.');
  12574. }
  12575. if (!defined(result)) {
  12576. throw new DeveloperError('result is required');
  12577. }
  12578. result.x = cartesian.x / scalar;
  12579. result.y = cartesian.y / scalar;
  12580. return result;
  12581. };
  12582. /**
  12583. * Negates the provided Cartesian.
  12584. *
  12585. * @param {Cartesian2} cartesian The Cartesian to be negated.
  12586. * @param {Cartesian2} result The object onto which to store the result.
  12587. * @returns {Cartesian2} The modified result parameter.
  12588. */
  12589. Cartesian2.negate = function(cartesian, result) {
  12590. if (!defined(cartesian)) {
  12591. throw new DeveloperError('cartesian is required');
  12592. }
  12593. if (!defined(result)) {
  12594. throw new DeveloperError('result is required');
  12595. }
  12596. result.x = -cartesian.x;
  12597. result.y = -cartesian.y;
  12598. return result;
  12599. };
  12600. /**
  12601. * Computes the absolute value of the provided Cartesian.
  12602. *
  12603. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  12604. * @param {Cartesian2} result The object onto which to store the result.
  12605. * @returns {Cartesian2} The modified result parameter.
  12606. */
  12607. Cartesian2.abs = function(cartesian, result) {
  12608. if (!defined(cartesian)) {
  12609. throw new DeveloperError('cartesian is required');
  12610. }
  12611. if (!defined(result)) {
  12612. throw new DeveloperError('result is required');
  12613. }
  12614. result.x = Math.abs(cartesian.x);
  12615. result.y = Math.abs(cartesian.y);
  12616. return result;
  12617. };
  12618. var lerpScratch = new Cartesian2();
  12619. /**
  12620. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  12621. *
  12622. * @param {Cartesian2} start The value corresponding to t at 0.0.
  12623. * @param {Cartesian2} end The value corresponding to t at 1.0.
  12624. * @param {Number} t The point along t at which to interpolate.
  12625. * @param {Cartesian2} result The object onto which to store the result.
  12626. * @returns {Cartesian2} The modified result parameter.
  12627. */
  12628. Cartesian2.lerp = function(start, end, t, result) {
  12629. if (!defined(start)) {
  12630. throw new DeveloperError('start is required.');
  12631. }
  12632. if (!defined(end)) {
  12633. throw new DeveloperError('end is required.');
  12634. }
  12635. if (typeof t !== 'number') {
  12636. throw new DeveloperError('t is required and must be a number.');
  12637. }
  12638. if (!defined(result)) {
  12639. throw new DeveloperError('result is required.');
  12640. }
  12641. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  12642. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  12643. return Cartesian2.add(lerpScratch, result, result);
  12644. };
  12645. var angleBetweenScratch = new Cartesian2();
  12646. var angleBetweenScratch2 = new Cartesian2();
  12647. /**
  12648. * Returns the angle, in radians, between the provided Cartesians.
  12649. *
  12650. * @param {Cartesian2} left The first Cartesian.
  12651. * @param {Cartesian2} right The second Cartesian.
  12652. * @returns {Number} The angle between the Cartesians.
  12653. */
  12654. Cartesian2.angleBetween = function(left, right) {
  12655. if (!defined(left)) {
  12656. throw new DeveloperError('left is required');
  12657. }
  12658. if (!defined(right)) {
  12659. throw new DeveloperError('right is required');
  12660. }
  12661. Cartesian2.normalize(left, angleBetweenScratch);
  12662. Cartesian2.normalize(right, angleBetweenScratch2);
  12663. return CesiumMath.acosClamped(Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2));
  12664. };
  12665. var mostOrthogonalAxisScratch = new Cartesian2();
  12666. /**
  12667. * Returns the axis that is most orthogonal to the provided Cartesian.
  12668. *
  12669. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  12670. * @param {Cartesian2} result The object onto which to store the result.
  12671. * @returns {Cartesian2} The most orthogonal axis.
  12672. */
  12673. Cartesian2.mostOrthogonalAxis = function(cartesian, result) {
  12674. if (!defined(cartesian)) {
  12675. throw new DeveloperError('cartesian is required.');
  12676. }
  12677. if (!defined(result)) {
  12678. throw new DeveloperError('result is required.');
  12679. }
  12680. var f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  12681. Cartesian2.abs(f, f);
  12682. if (f.x <= f.y) {
  12683. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  12684. } else {
  12685. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  12686. }
  12687. return result;
  12688. };
  12689. /**
  12690. * Compares the provided Cartesians componentwise and returns
  12691. * <code>true</code> if they are equal, <code>false</code> otherwise.
  12692. *
  12693. * @param {Cartesian2} [left] The first Cartesian.
  12694. * @param {Cartesian2} [right] The second Cartesian.
  12695. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  12696. */
  12697. Cartesian2.equals = function(left, right) {
  12698. return (left === right) ||
  12699. ((defined(left)) &&
  12700. (defined(right)) &&
  12701. (left.x === right.x) &&
  12702. (left.y === right.y));
  12703. };
  12704. /**
  12705. * @private
  12706. */
  12707. Cartesian2.equalsArray = function(cartesian, array, offset) {
  12708. return cartesian.x === array[offset] &&
  12709. cartesian.y === array[offset + 1];
  12710. };
  12711. /**
  12712. * Compares the provided Cartesians componentwise and returns
  12713. * <code>true</code> if they pass an absolute or relative tolerance test,
  12714. * <code>false</code> otherwise.
  12715. *
  12716. * @param {Cartesian2} [left] The first Cartesian.
  12717. * @param {Cartesian2} [right] The second Cartesian.
  12718. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  12719. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  12720. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  12721. */
  12722. Cartesian2.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  12723. return (left === right) ||
  12724. (defined(left) &&
  12725. defined(right) &&
  12726. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  12727. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon));
  12728. };
  12729. /**
  12730. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  12731. *
  12732. * @type {Cartesian2}
  12733. * @constant
  12734. */
  12735. Cartesian2.ZERO = freezeObject(new Cartesian2(0.0, 0.0));
  12736. /**
  12737. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  12738. *
  12739. * @type {Cartesian2}
  12740. * @constant
  12741. */
  12742. Cartesian2.UNIT_X = freezeObject(new Cartesian2(1.0, 0.0));
  12743. /**
  12744. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  12745. *
  12746. * @type {Cartesian2}
  12747. * @constant
  12748. */
  12749. Cartesian2.UNIT_Y = freezeObject(new Cartesian2(0.0, 1.0));
  12750. /**
  12751. * Duplicates this Cartesian2 instance.
  12752. *
  12753. * @param {Cartesian2} [result] The object onto which to store the result.
  12754. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  12755. */
  12756. Cartesian2.prototype.clone = function(result) {
  12757. return Cartesian2.clone(this, result);
  12758. };
  12759. /**
  12760. * Compares this Cartesian against the provided Cartesian componentwise and returns
  12761. * <code>true</code> if they are equal, <code>false</code> otherwise.
  12762. *
  12763. * @param {Cartesian2} [right] The right hand side Cartesian.
  12764. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  12765. */
  12766. Cartesian2.prototype.equals = function(right) {
  12767. return Cartesian2.equals(this, right);
  12768. };
  12769. /**
  12770. * Compares this Cartesian against the provided Cartesian componentwise and returns
  12771. * <code>true</code> if they pass an absolute or relative tolerance test,
  12772. * <code>false</code> otherwise.
  12773. *
  12774. * @param {Cartesian2} [right] The right hand side Cartesian.
  12775. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  12776. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  12777. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  12778. */
  12779. Cartesian2.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  12780. return Cartesian2.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  12781. };
  12782. /**
  12783. * Creates a string representing this Cartesian in the format '(x, y)'.
  12784. *
  12785. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  12786. */
  12787. Cartesian2.prototype.toString = function() {
  12788. return '(' + this.x + ', ' + this.y + ')';
  12789. };
  12790. return Cartesian2;
  12791. });
  12792. /*global define*/
  12793. define('Core/QuadraticRealPolynomial',[
  12794. './DeveloperError',
  12795. './Math'
  12796. ], function(
  12797. DeveloperError,
  12798. CesiumMath) {
  12799. 'use strict';
  12800. /**
  12801. * Defines functions for 2nd order polynomial functions of one variable with only real coefficients.
  12802. *
  12803. * @exports QuadraticRealPolynomial
  12804. */
  12805. var QuadraticRealPolynomial = {};
  12806. /**
  12807. * Provides the discriminant of the quadratic equation from the supplied coefficients.
  12808. *
  12809. * @param {Number} a The coefficient of the 2nd order monomial.
  12810. * @param {Number} b The coefficient of the 1st order monomial.
  12811. * @param {Number} c The coefficient of the 0th order monomial.
  12812. * @returns {Number} The value of the discriminant.
  12813. */
  12814. QuadraticRealPolynomial.computeDiscriminant = function(a, b, c) {
  12815. if (typeof a !== 'number') {
  12816. throw new DeveloperError('a is a required number.');
  12817. }
  12818. if (typeof b !== 'number') {
  12819. throw new DeveloperError('b is a required number.');
  12820. }
  12821. if (typeof c !== 'number') {
  12822. throw new DeveloperError('c is a required number.');
  12823. }
  12824. var discriminant = b * b - 4.0 * a * c;
  12825. return discriminant;
  12826. };
  12827. function addWithCancellationCheck(left, right, tolerance) {
  12828. var difference = left + right;
  12829. if ((CesiumMath.sign(left) !== CesiumMath.sign(right)) &&
  12830. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance) {
  12831. return 0.0;
  12832. }
  12833. return difference;
  12834. }
  12835. /**
  12836. * Provides the real valued roots of the quadratic polynomial with the provided coefficients.
  12837. *
  12838. * @param {Number} a The coefficient of the 2nd order monomial.
  12839. * @param {Number} b The coefficient of the 1st order monomial.
  12840. * @param {Number} c The coefficient of the 0th order monomial.
  12841. * @returns {Number[]} The real valued roots.
  12842. */
  12843. QuadraticRealPolynomial.computeRealRoots = function(a, b, c) {
  12844. if (typeof a !== 'number') {
  12845. throw new DeveloperError('a is a required number.');
  12846. }
  12847. if (typeof b !== 'number') {
  12848. throw new DeveloperError('b is a required number.');
  12849. }
  12850. if (typeof c !== 'number') {
  12851. throw new DeveloperError('c is a required number.');
  12852. }
  12853. var ratio;
  12854. if (a === 0.0) {
  12855. if (b === 0.0) {
  12856. // Constant function: c = 0.
  12857. return [];
  12858. }
  12859. // Linear function: b * x + c = 0.
  12860. return [-c / b];
  12861. } else if (b === 0.0) {
  12862. if (c === 0.0) {
  12863. // 2nd order monomial: a * x^2 = 0.
  12864. return [0.0, 0.0];
  12865. }
  12866. var cMagnitude = Math.abs(c);
  12867. var aMagnitude = Math.abs(a);
  12868. if ((cMagnitude < aMagnitude) && (cMagnitude / aMagnitude < CesiumMath.EPSILON14)) { // c ~= 0.0.
  12869. // 2nd order monomial: a * x^2 = 0.
  12870. return [0.0, 0.0];
  12871. } else if ((cMagnitude > aMagnitude) && (aMagnitude / cMagnitude < CesiumMath.EPSILON14)) { // a ~= 0.0.
  12872. // Constant function: c = 0.
  12873. return [];
  12874. }
  12875. // a * x^2 + c = 0
  12876. ratio = -c / a;
  12877. if (ratio < 0.0) {
  12878. // Both roots are complex.
  12879. return [];
  12880. }
  12881. // Both roots are real.
  12882. var root = Math.sqrt(ratio);
  12883. return [-root, root];
  12884. } else if (c === 0.0) {
  12885. // a * x^2 + b * x = 0
  12886. ratio = -b / a;
  12887. if (ratio < 0.0) {
  12888. return [ratio, 0.0];
  12889. }
  12890. return [0.0, ratio];
  12891. }
  12892. // a * x^2 + b * x + c = 0
  12893. var b2 = b * b;
  12894. var four_ac = 4.0 * a * c;
  12895. var radicand = addWithCancellationCheck(b2, -four_ac, CesiumMath.EPSILON14);
  12896. if (radicand < 0.0) {
  12897. // Both roots are complex.
  12898. return [];
  12899. }
  12900. var q = -0.5 * addWithCancellationCheck(b, CesiumMath.sign(b) * Math.sqrt(radicand), CesiumMath.EPSILON14);
  12901. if (b > 0.0) {
  12902. return [q / a, c / q];
  12903. }
  12904. return [c / q, q / a];
  12905. };
  12906. return QuadraticRealPolynomial;
  12907. });
  12908. /*global define*/
  12909. define('Core/CubicRealPolynomial',[
  12910. './DeveloperError',
  12911. './QuadraticRealPolynomial'
  12912. ], function(
  12913. DeveloperError,
  12914. QuadraticRealPolynomial) {
  12915. 'use strict';
  12916. /**
  12917. * Defines functions for 3rd order polynomial functions of one variable with only real coefficients.
  12918. *
  12919. * @exports CubicRealPolynomial
  12920. */
  12921. var CubicRealPolynomial = {};
  12922. /**
  12923. * Provides the discriminant of the cubic equation from the supplied coefficients.
  12924. *
  12925. * @param {Number} a The coefficient of the 3rd order monomial.
  12926. * @param {Number} b The coefficient of the 2nd order monomial.
  12927. * @param {Number} c The coefficient of the 1st order monomial.
  12928. * @param {Number} d The coefficient of the 0th order monomial.
  12929. * @returns {Number} The value of the discriminant.
  12930. */
  12931. CubicRealPolynomial.computeDiscriminant = function(a, b, c, d) {
  12932. if (typeof a !== 'number') {
  12933. throw new DeveloperError('a is a required number.');
  12934. }
  12935. if (typeof b !== 'number') {
  12936. throw new DeveloperError('b is a required number.');
  12937. }
  12938. if (typeof c !== 'number') {
  12939. throw new DeveloperError('c is a required number.');
  12940. }
  12941. if (typeof d !== 'number') {
  12942. throw new DeveloperError('d is a required number.');
  12943. }
  12944. var a2 = a * a;
  12945. var b2 = b * b;
  12946. var c2 = c * c;
  12947. var d2 = d * d;
  12948. var discriminant = 18.0 * a * b * c * d + b2 * c2 - 27.0 * a2 * d2 - 4.0 * (a * c2 * c + b2 * b * d);
  12949. return discriminant;
  12950. };
  12951. function computeRealRoots(a, b, c, d) {
  12952. var A = a;
  12953. var B = b / 3.0;
  12954. var C = c / 3.0;
  12955. var D = d;
  12956. var AC = A * C;
  12957. var BD = B * D;
  12958. var B2 = B * B;
  12959. var C2 = C * C;
  12960. var delta1 = A * C - B2;
  12961. var delta2 = A * D - B * C;
  12962. var delta3 = B * D - C2;
  12963. var discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  12964. var temp;
  12965. var temp1;
  12966. if (discriminant < 0.0) {
  12967. var ABar;
  12968. var CBar;
  12969. var DBar;
  12970. if (B2 * BD >= AC * C2) {
  12971. ABar = A;
  12972. CBar = delta1;
  12973. DBar = -2.0 * B * delta1 + A * delta2;
  12974. } else {
  12975. ABar = D;
  12976. CBar = delta3;
  12977. DBar = -D * delta2 + 2.0 * C * delta3;
  12978. }
  12979. var s = (DBar < 0.0) ? -1.0 : 1.0; // This is not Math.Sign()!
  12980. var temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  12981. temp1 = -DBar + temp0;
  12982. var x = temp1 / 2.0;
  12983. var p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  12984. var q = (temp1 === temp0) ? -p : -CBar / p;
  12985. temp = (CBar <= 0.0) ? p + q : -DBar / (p * p + q * q + CBar);
  12986. if (B2 * BD >= AC * C2) {
  12987. return [(temp - B) / A];
  12988. }
  12989. return [-D / (temp + C)];
  12990. }
  12991. var CBarA = delta1;
  12992. var DBarA = -2.0 * B * delta1 + A * delta2;
  12993. var CBarD = delta3;
  12994. var DBarD = -D * delta2 + 2.0 * C * delta3;
  12995. var squareRootOfDiscriminant = Math.sqrt(discriminant);
  12996. var halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  12997. var theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  12998. temp = 2.0 * Math.sqrt(-CBarA);
  12999. var cosine = Math.cos(theta);
  13000. temp1 = temp * cosine;
  13001. var temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  13002. var numeratorLarge = (temp1 + temp3 > 2.0 * B) ? temp1 - B : temp3 - B;
  13003. var denominatorLarge = A;
  13004. var root1 = numeratorLarge / denominatorLarge;
  13005. theta = Math.abs(Math.atan2(D * squareRootOfDiscriminant, -DBarD) / 3.0);
  13006. temp = 2.0 * Math.sqrt(-CBarD);
  13007. cosine = Math.cos(theta);
  13008. temp1 = temp * cosine;
  13009. temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  13010. var numeratorSmall = -D;
  13011. var denominatorSmall = (temp1 + temp3 < 2.0 * C) ? temp1 + C : temp3 + C;
  13012. var root3 = numeratorSmall / denominatorSmall;
  13013. var E = denominatorLarge * denominatorSmall;
  13014. var F = -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  13015. var G = numeratorLarge * numeratorSmall;
  13016. var root2 = (C * F - B * G) / (-B * F + C * E);
  13017. if (root1 <= root2) {
  13018. if (root1 <= root3) {
  13019. if (root2 <= root3) {
  13020. return [root1, root2, root3];
  13021. }
  13022. return [root1, root3, root2];
  13023. }
  13024. return [root3, root1, root2];
  13025. }
  13026. if (root1 <= root3) {
  13027. return [root2, root1, root3];
  13028. }
  13029. if (root2 <= root3) {
  13030. return [root2, root3, root1];
  13031. }
  13032. return [root3, root2, root1];
  13033. }
  13034. /**
  13035. * Provides the real valued roots of the cubic polynomial with the provided coefficients.
  13036. *
  13037. * @param {Number} a The coefficient of the 3rd order monomial.
  13038. * @param {Number} b The coefficient of the 2nd order monomial.
  13039. * @param {Number} c The coefficient of the 1st order monomial.
  13040. * @param {Number} d The coefficient of the 0th order monomial.
  13041. * @returns {Number[]} The real valued roots.
  13042. */
  13043. CubicRealPolynomial.computeRealRoots = function(a, b, c, d) {
  13044. if (typeof a !== 'number') {
  13045. throw new DeveloperError('a is a required number.');
  13046. }
  13047. if (typeof b !== 'number') {
  13048. throw new DeveloperError('b is a required number.');
  13049. }
  13050. if (typeof c !== 'number') {
  13051. throw new DeveloperError('c is a required number.');
  13052. }
  13053. if (typeof d !== 'number') {
  13054. throw new DeveloperError('d is a required number.');
  13055. }
  13056. var roots;
  13057. var ratio;
  13058. if (a === 0.0) {
  13059. // Quadratic function: b * x^2 + c * x + d = 0.
  13060. return QuadraticRealPolynomial.computeRealRoots(b, c, d);
  13061. } else if (b === 0.0) {
  13062. if (c === 0.0) {
  13063. if (d === 0.0) {
  13064. // 3rd order monomial: a * x^3 = 0.
  13065. return [0.0, 0.0, 0.0];
  13066. }
  13067. // a * x^3 + d = 0
  13068. ratio = -d / a;
  13069. var root = (ratio < 0.0) ? -Math.pow(-ratio, 1.0 / 3.0) : Math.pow(ratio, 1.0 / 3.0);
  13070. return [root, root, root];
  13071. } else if (d === 0.0) {
  13072. // x * (a * x^2 + c) = 0.
  13073. roots = QuadraticRealPolynomial.computeRealRoots(a, 0, c);
  13074. // Return the roots in ascending order.
  13075. if (roots.Length === 0) {
  13076. return [0.0];
  13077. }
  13078. return [roots[0], 0.0, roots[1]];
  13079. }
  13080. // Deflated cubic polynomial: a * x^3 + c * x + d= 0.
  13081. return computeRealRoots(a, 0, c, d);
  13082. } else if (c === 0.0) {
  13083. if (d === 0.0) {
  13084. // x^2 * (a * x + b) = 0.
  13085. ratio = -b / a;
  13086. if (ratio < 0.0) {
  13087. return [ratio, 0.0, 0.0];
  13088. }
  13089. return [0.0, 0.0, ratio];
  13090. }
  13091. // a * x^3 + b * x^2 + d = 0.
  13092. return computeRealRoots(a, b, 0, d);
  13093. } else if (d === 0.0) {
  13094. // x * (a * x^2 + b * x + c) = 0
  13095. roots = QuadraticRealPolynomial.computeRealRoots(a, b, c);
  13096. // Return the roots in ascending order.
  13097. if (roots.length === 0) {
  13098. return [0.0];
  13099. } else if (roots[1] <= 0.0) {
  13100. return [roots[0], roots[1], 0.0];
  13101. } else if (roots[0] >= 0.0) {
  13102. return [0.0, roots[0], roots[1]];
  13103. }
  13104. return [roots[0], 0.0, roots[1]];
  13105. }
  13106. return computeRealRoots(a, b, c, d);
  13107. };
  13108. return CubicRealPolynomial;
  13109. });
  13110. /*global define*/
  13111. define('Core/QuarticRealPolynomial',[
  13112. './CubicRealPolynomial',
  13113. './DeveloperError',
  13114. './Math',
  13115. './QuadraticRealPolynomial'
  13116. ], function(
  13117. CubicRealPolynomial,
  13118. DeveloperError,
  13119. CesiumMath,
  13120. QuadraticRealPolynomial) {
  13121. 'use strict';
  13122. /**
  13123. * Defines functions for 4th order polynomial functions of one variable with only real coefficients.
  13124. *
  13125. * @exports QuarticRealPolynomial
  13126. */
  13127. var QuarticRealPolynomial = {};
  13128. /**
  13129. * Provides the discriminant of the quartic equation from the supplied coefficients.
  13130. *
  13131. * @param {Number} a The coefficient of the 4th order monomial.
  13132. * @param {Number} b The coefficient of the 3rd order monomial.
  13133. * @param {Number} c The coefficient of the 2nd order monomial.
  13134. * @param {Number} d The coefficient of the 1st order monomial.
  13135. * @param {Number} e The coefficient of the 0th order monomial.
  13136. * @returns {Number} The value of the discriminant.
  13137. */
  13138. QuarticRealPolynomial.computeDiscriminant = function(a, b, c, d, e) {
  13139. if (typeof a !== 'number') {
  13140. throw new DeveloperError('a is a required number.');
  13141. }
  13142. if (typeof b !== 'number') {
  13143. throw new DeveloperError('b is a required number.');
  13144. }
  13145. if (typeof c !== 'number') {
  13146. throw new DeveloperError('c is a required number.');
  13147. }
  13148. if (typeof d !== 'number') {
  13149. throw new DeveloperError('d is a required number.');
  13150. }
  13151. if (typeof e !== 'number') {
  13152. throw new DeveloperError('e is a required number.');
  13153. }
  13154. var a2 = a * a;
  13155. var a3 = a2 * a;
  13156. var b2 = b * b;
  13157. var b3 = b2 * b;
  13158. var c2 = c * c;
  13159. var c3 = c2 * c;
  13160. var d2 = d * d;
  13161. var d3 = d2 * d;
  13162. var e2 = e * e;
  13163. var e3 = e2 * e;
  13164. 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) +
  13165. 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) +
  13166. e2 * (144.0 * a * b2 * c - 27.0 * b2 * b2 - 128.0 * a2 * c2 - 192.0 * a2 * b * d);
  13167. return discriminant;
  13168. };
  13169. function original(a3, a2, a1, a0) {
  13170. var a3Squared = a3 * a3;
  13171. var p = a2 - 3.0 * a3Squared / 8.0;
  13172. var q = a1 - a2 * a3 / 2.0 + a3Squared * a3 / 8.0;
  13173. var r = a0 - a1 * a3 / 4.0 + a2 * a3Squared / 16.0 - 3.0 * a3Squared * a3Squared / 256.0;
  13174. // Find the roots of the cubic equations: h^6 + 2 p h^4 + (p^2 - 4 r) h^2 - q^2 = 0.
  13175. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, 2.0 * p, p * p - 4.0 * r, -q * q);
  13176. if (cubicRoots.length > 0) {
  13177. var temp = -a3 / 4.0;
  13178. // Use the largest positive root.
  13179. var hSquared = cubicRoots[cubicRoots.length - 1];
  13180. if (Math.abs(hSquared) < CesiumMath.EPSILON14) {
  13181. // y^4 + p y^2 + r = 0.
  13182. var roots = QuadraticRealPolynomial.computeRealRoots(1.0, p, r);
  13183. if (roots.length === 2) {
  13184. var root0 = roots[0];
  13185. var root1 = roots[1];
  13186. var y;
  13187. if (root0 >= 0.0 && root1 >= 0.0) {
  13188. var y0 = Math.sqrt(root0);
  13189. var y1 = Math.sqrt(root1);
  13190. return [temp - y1, temp - y0, temp + y0, temp + y1];
  13191. } else if (root0 >= 0.0 && root1 < 0.0) {
  13192. y = Math.sqrt(root0);
  13193. return [temp - y, temp + y];
  13194. } else if (root0 < 0.0 && root1 >= 0.0) {
  13195. y = Math.sqrt(root1);
  13196. return [temp - y, temp + y];
  13197. }
  13198. }
  13199. return [];
  13200. } else if (hSquared > 0.0) {
  13201. var h = Math.sqrt(hSquared);
  13202. var m = (p + hSquared - q / h) / 2.0;
  13203. var n = (p + hSquared + q / h) / 2.0;
  13204. // Now solve the two quadratic factors: (y^2 + h y + m)(y^2 - h y + n);
  13205. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, h, m);
  13206. var roots2 = QuadraticRealPolynomial.computeRealRoots(1.0, -h, n);
  13207. if (roots1.length !== 0) {
  13208. roots1[0] += temp;
  13209. roots1[1] += temp;
  13210. if (roots2.length !== 0) {
  13211. roots2[0] += temp;
  13212. roots2[1] += temp;
  13213. if (roots1[1] <= roots2[0]) {
  13214. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  13215. } else if (roots2[1] <= roots1[0]) {
  13216. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  13217. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  13218. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  13219. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  13220. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  13221. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  13222. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  13223. }
  13224. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  13225. }
  13226. return roots1;
  13227. }
  13228. if (roots2.length !== 0) {
  13229. roots2[0] += temp;
  13230. roots2[1] += temp;
  13231. return roots2;
  13232. }
  13233. return [];
  13234. }
  13235. }
  13236. return [];
  13237. }
  13238. function neumark(a3, a2, a1, a0) {
  13239. var a1Squared = a1 * a1;
  13240. var a2Squared = a2 * a2;
  13241. var a3Squared = a3 * a3;
  13242. var p = -2.0 * a2;
  13243. var q = a1 * a3 + a2Squared - 4.0 * a0;
  13244. var r = a3Squared * a0 - a1 * a2 * a3 + a1Squared;
  13245. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, p, q, r);
  13246. if (cubicRoots.length > 0) {
  13247. // Use the most positive root
  13248. var y = cubicRoots[0];
  13249. var temp = (a2 - y);
  13250. var tempSquared = temp * temp;
  13251. var g1 = a3 / 2.0;
  13252. var h1 = temp / 2.0;
  13253. var m = tempSquared - 4.0 * a0;
  13254. var mError = tempSquared + 4.0 * Math.abs(a0);
  13255. var n = a3Squared - 4.0 * y;
  13256. var nError = a3Squared + 4.0 * Math.abs(y);
  13257. var g2;
  13258. var h2;
  13259. if (y < 0.0 || (m * nError < n * mError)) {
  13260. var squareRootOfN = Math.sqrt(n);
  13261. g2 = squareRootOfN / 2.0;
  13262. h2 = squareRootOfN === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfN;
  13263. } else {
  13264. var squareRootOfM = Math.sqrt(m);
  13265. g2 = squareRootOfM === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfM;
  13266. h2 = squareRootOfM / 2.0;
  13267. }
  13268. var G;
  13269. var g;
  13270. if (g1 === 0.0 && g2 === 0.0) {
  13271. G = 0.0;
  13272. g = 0.0;
  13273. } else if (CesiumMath.sign(g1) === CesiumMath.sign(g2)) {
  13274. G = g1 + g2;
  13275. g = y / G;
  13276. } else {
  13277. g = g1 - g2;
  13278. G = y / g;
  13279. }
  13280. var H;
  13281. var h;
  13282. if (h1 === 0.0 && h2 === 0.0) {
  13283. H = 0.0;
  13284. h = 0.0;
  13285. } else if (CesiumMath.sign(h1) === CesiumMath.sign(h2)) {
  13286. H = h1 + h2;
  13287. h = a0 / H;
  13288. } else {
  13289. h = h1 - h2;
  13290. H = a0 / h;
  13291. }
  13292. // Now solve the two quadratic factors: (y^2 + G y + H)(y^2 + g y + h);
  13293. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, G, H);
  13294. var roots2 = QuadraticRealPolynomial.computeRealRoots(1.0, g, h);
  13295. if (roots1.length !== 0) {
  13296. if (roots2.length !== 0) {
  13297. if (roots1[1] <= roots2[0]) {
  13298. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  13299. } else if (roots2[1] <= roots1[0]) {
  13300. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  13301. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  13302. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  13303. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  13304. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  13305. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  13306. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  13307. } else {
  13308. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  13309. }
  13310. }
  13311. return roots1;
  13312. }
  13313. if (roots2.length !== 0) {
  13314. return roots2;
  13315. }
  13316. }
  13317. return [];
  13318. }
  13319. /**
  13320. * Provides the real valued roots of the quartic polynomial with the provided coefficients.
  13321. *
  13322. * @param {Number} a The coefficient of the 4th order monomial.
  13323. * @param {Number} b The coefficient of the 3rd order monomial.
  13324. * @param {Number} c The coefficient of the 2nd order monomial.
  13325. * @param {Number} d The coefficient of the 1st order monomial.
  13326. * @param {Number} e The coefficient of the 0th order monomial.
  13327. * @returns {Number[]} The real valued roots.
  13328. */
  13329. QuarticRealPolynomial.computeRealRoots = function(a, b, c, d, e) {
  13330. if (typeof a !== 'number') {
  13331. throw new DeveloperError('a is a required number.');
  13332. }
  13333. if (typeof b !== 'number') {
  13334. throw new DeveloperError('b is a required number.');
  13335. }
  13336. if (typeof c !== 'number') {
  13337. throw new DeveloperError('c is a required number.');
  13338. }
  13339. if (typeof d !== 'number') {
  13340. throw new DeveloperError('d is a required number.');
  13341. }
  13342. if (typeof e !== 'number') {
  13343. throw new DeveloperError('e is a required number.');
  13344. }
  13345. if (Math.abs(a) < CesiumMath.EPSILON15) {
  13346. return CubicRealPolynomial.computeRealRoots(b, c, d, e);
  13347. }
  13348. var a3 = b / a;
  13349. var a2 = c / a;
  13350. var a1 = d / a;
  13351. var a0 = e / a;
  13352. var k = (a3 < 0.0) ? 1 : 0;
  13353. k += (a2 < 0.0) ? k + 1 : k;
  13354. k += (a1 < 0.0) ? k + 1 : k;
  13355. k += (a0 < 0.0) ? k + 1 : k;
  13356. switch (k) {
  13357. case 0:
  13358. return original(a3, a2, a1, a0);
  13359. case 1:
  13360. return neumark(a3, a2, a1, a0);
  13361. case 2:
  13362. return neumark(a3, a2, a1, a0);
  13363. case 3:
  13364. return original(a3, a2, a1, a0);
  13365. case 4:
  13366. return original(a3, a2, a1, a0);
  13367. case 5:
  13368. return neumark(a3, a2, a1, a0);
  13369. case 6:
  13370. return original(a3, a2, a1, a0);
  13371. case 7:
  13372. return original(a3, a2, a1, a0);
  13373. case 8:
  13374. return neumark(a3, a2, a1, a0);
  13375. case 9:
  13376. return original(a3, a2, a1, a0);
  13377. case 10:
  13378. return original(a3, a2, a1, a0);
  13379. case 11:
  13380. return neumark(a3, a2, a1, a0);
  13381. case 12:
  13382. return original(a3, a2, a1, a0);
  13383. case 13:
  13384. return original(a3, a2, a1, a0);
  13385. case 14:
  13386. return original(a3, a2, a1, a0);
  13387. case 15:
  13388. return original(a3, a2, a1, a0);
  13389. default:
  13390. return undefined;
  13391. }
  13392. };
  13393. return QuarticRealPolynomial;
  13394. });
  13395. /*global define*/
  13396. define('Core/Ray',[
  13397. './Cartesian3',
  13398. './defaultValue',
  13399. './defined',
  13400. './DeveloperError'
  13401. ], function(
  13402. Cartesian3,
  13403. defaultValue,
  13404. defined,
  13405. DeveloperError) {
  13406. 'use strict';
  13407. /**
  13408. * Represents a ray that extends infinitely from the provided origin in the provided direction.
  13409. * @alias Ray
  13410. * @constructor
  13411. *
  13412. * @param {Cartesian3} [origin=Cartesian3.ZERO] The origin of the ray.
  13413. * @param {Cartesian3} [direction=Cartesian3.ZERO] The direction of the ray.
  13414. */
  13415. function Ray(origin, direction) {
  13416. direction = Cartesian3.clone(defaultValue(direction, Cartesian3.ZERO));
  13417. if (!Cartesian3.equals(direction, Cartesian3.ZERO)) {
  13418. Cartesian3.normalize(direction, direction);
  13419. }
  13420. /**
  13421. * The origin of the ray.
  13422. * @type {Cartesian3}
  13423. * @default {@link Cartesian3.ZERO}
  13424. */
  13425. this.origin = Cartesian3.clone(defaultValue(origin, Cartesian3.ZERO));
  13426. /**
  13427. * The direction of the ray.
  13428. * @type {Cartesian3}
  13429. */
  13430. this.direction = direction;
  13431. }
  13432. /**
  13433. * Computes the point along the ray given by r(t) = o + t*d,
  13434. * where o is the origin of the ray and d is the direction.
  13435. *
  13436. * @param {Ray} ray The ray.
  13437. * @param {Number} t A scalar value.
  13438. * @param {Cartesian3} [result] The object in which the result will be stored.
  13439. * @returns {Cartesian3} The modified result parameter, or a new instance if none was provided.
  13440. *
  13441. * @example
  13442. * //Get the first intersection point of a ray and an ellipsoid.
  13443. * var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  13444. * var point = Cesium.Ray.getPoint(ray, intersection.start);
  13445. */
  13446. Ray.getPoint = function(ray, t, result) {
  13447. if (!defined(ray)){
  13448. throw new DeveloperError('ray is requred');
  13449. }
  13450. if (typeof t !== 'number') {
  13451. throw new DeveloperError('t is a required number');
  13452. }
  13453. if (!defined(result)) {
  13454. result = new Cartesian3();
  13455. }
  13456. result = Cartesian3.multiplyByScalar(ray.direction, t, result);
  13457. return Cartesian3.add(ray.origin, result, result);
  13458. };
  13459. return Ray;
  13460. });
  13461. /*global define*/
  13462. define('Core/IntersectionTests',[
  13463. './Cartesian3',
  13464. './Cartographic',
  13465. './defaultValue',
  13466. './defined',
  13467. './DeveloperError',
  13468. './Math',
  13469. './Matrix3',
  13470. './QuadraticRealPolynomial',
  13471. './QuarticRealPolynomial',
  13472. './Ray'
  13473. ], function(
  13474. Cartesian3,
  13475. Cartographic,
  13476. defaultValue,
  13477. defined,
  13478. DeveloperError,
  13479. CesiumMath,
  13480. Matrix3,
  13481. QuadraticRealPolynomial,
  13482. QuarticRealPolynomial,
  13483. Ray) {
  13484. 'use strict';
  13485. /**
  13486. * Functions for computing the intersection between geometries such as rays, planes, triangles, and ellipsoids.
  13487. *
  13488. * @exports IntersectionTests
  13489. */
  13490. var IntersectionTests = {};
  13491. /**
  13492. * Computes the intersection of a ray and a plane.
  13493. *
  13494. * @param {Ray} ray The ray.
  13495. * @param {Plane} plane The plane.
  13496. * @param {Cartesian3} [result] The object onto which to store the result.
  13497. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13498. */
  13499. IntersectionTests.rayPlane = function(ray, plane, result) {
  13500. if (!defined(ray)) {
  13501. throw new DeveloperError('ray is required.');
  13502. }
  13503. if (!defined(plane)) {
  13504. throw new DeveloperError('plane is required.');
  13505. }
  13506. if (!defined(result)) {
  13507. result = new Cartesian3();
  13508. }
  13509. var origin = ray.origin;
  13510. var direction = ray.direction;
  13511. var normal = plane.normal;
  13512. var denominator = Cartesian3.dot(normal, direction);
  13513. if (Math.abs(denominator) < CesiumMath.EPSILON15) {
  13514. // Ray is parallel to plane. The ray may be in the polygon's plane.
  13515. return undefined;
  13516. }
  13517. var t = (-plane.distance - Cartesian3.dot(normal, origin)) / denominator;
  13518. if (t < 0) {
  13519. return undefined;
  13520. }
  13521. result = Cartesian3.multiplyByScalar(direction, t, result);
  13522. return Cartesian3.add(origin, result, result);
  13523. };
  13524. var scratchEdge0 = new Cartesian3();
  13525. var scratchEdge1 = new Cartesian3();
  13526. var scratchPVec = new Cartesian3();
  13527. var scratchTVec = new Cartesian3();
  13528. var scratchQVec = new Cartesian3();
  13529. /**
  13530. * Computes the intersection of a ray and a triangle as a parametric distance along the input ray.
  13531. *
  13532. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  13533. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  13534. *
  13535. * @memberof IntersectionTests
  13536. *
  13537. * @param {Ray} ray The ray.
  13538. * @param {Cartesian3} p0 The first vertex of the triangle.
  13539. * @param {Cartesian3} p1 The second vertex of the triangle.
  13540. * @param {Cartesian3} p2 The third vertex of the triangle.
  13541. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13542. * and return undefined for intersections with the back face.
  13543. * @returns {Number} The intersection as a parametric distance along the ray, or undefined if there is no intersection.
  13544. */
  13545. IntersectionTests.rayTriangleParametric = function(ray, p0, p1, p2, cullBackFaces) {
  13546. if (!defined(ray)) {
  13547. throw new DeveloperError('ray is required.');
  13548. }
  13549. if (!defined(p0)) {
  13550. throw new DeveloperError('p0 is required.');
  13551. }
  13552. if (!defined(p1)) {
  13553. throw new DeveloperError('p1 is required.');
  13554. }
  13555. if (!defined(p2)) {
  13556. throw new DeveloperError('p2 is required.');
  13557. }
  13558. cullBackFaces = defaultValue(cullBackFaces, false);
  13559. var origin = ray.origin;
  13560. var direction = ray.direction;
  13561. var edge0 = Cartesian3.subtract(p1, p0, scratchEdge0);
  13562. var edge1 = Cartesian3.subtract(p2, p0, scratchEdge1);
  13563. var p = Cartesian3.cross(direction, edge1, scratchPVec);
  13564. var det = Cartesian3.dot(edge0, p);
  13565. var tvec;
  13566. var q;
  13567. var u;
  13568. var v;
  13569. var t;
  13570. if (cullBackFaces) {
  13571. if (det < CesiumMath.EPSILON6) {
  13572. return undefined;
  13573. }
  13574. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  13575. u = Cartesian3.dot(tvec, p);
  13576. if (u < 0.0 || u > det) {
  13577. return undefined;
  13578. }
  13579. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  13580. v = Cartesian3.dot(direction, q);
  13581. if (v < 0.0 || u + v > det) {
  13582. return undefined;
  13583. }
  13584. t = Cartesian3.dot(edge1, q) / det;
  13585. } else {
  13586. if (Math.abs(det) < CesiumMath.EPSILON6) {
  13587. return undefined;
  13588. }
  13589. var invDet = 1.0 / det;
  13590. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  13591. u = Cartesian3.dot(tvec, p) * invDet;
  13592. if (u < 0.0 || u > 1.0) {
  13593. return undefined;
  13594. }
  13595. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  13596. v = Cartesian3.dot(direction, q) * invDet;
  13597. if (v < 0.0 || u + v > 1.0) {
  13598. return undefined;
  13599. }
  13600. t = Cartesian3.dot(edge1, q) * invDet;
  13601. }
  13602. return t;
  13603. };
  13604. /**
  13605. * Computes the intersection of a ray and a triangle as a Cartesian3 coordinate.
  13606. *
  13607. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  13608. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  13609. *
  13610. * @memberof IntersectionTests
  13611. *
  13612. * @param {Ray} ray The ray.
  13613. * @param {Cartesian3} p0 The first vertex of the triangle.
  13614. * @param {Cartesian3} p1 The second vertex of the triangle.
  13615. * @param {Cartesian3} p2 The third vertex of the triangle.
  13616. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13617. * and return undefined for intersections with the back face.
  13618. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  13619. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13620. */
  13621. IntersectionTests.rayTriangle = function(ray, p0, p1, p2, cullBackFaces, result) {
  13622. var t = IntersectionTests.rayTriangleParametric(ray, p0, p1, p2, cullBackFaces);
  13623. if (!defined(t) || t < 0.0) {
  13624. return undefined;
  13625. }
  13626. if (!defined(result)) {
  13627. result = new Cartesian3();
  13628. }
  13629. Cartesian3.multiplyByScalar(ray.direction, t, result);
  13630. return Cartesian3.add(ray.origin, result, result);
  13631. };
  13632. var scratchLineSegmentTriangleRay = new Ray();
  13633. /**
  13634. * Computes the intersection of a line segment and a triangle.
  13635. * @memberof IntersectionTests
  13636. *
  13637. * @param {Cartesian3} v0 The an end point of the line segment.
  13638. * @param {Cartesian3} v1 The other end point of the line segment.
  13639. * @param {Cartesian3} p0 The first vertex of the triangle.
  13640. * @param {Cartesian3} p1 The second vertex of the triangle.
  13641. * @param {Cartesian3} p2 The third vertex of the triangle.
  13642. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  13643. * and return undefined for intersections with the back face.
  13644. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  13645. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  13646. */
  13647. IntersectionTests.lineSegmentTriangle = function(v0, v1, p0, p1, p2, cullBackFaces, result) {
  13648. if (!defined(v0)) {
  13649. throw new DeveloperError('v0 is required.');
  13650. }
  13651. if (!defined(v1)) {
  13652. throw new DeveloperError('v1 is required.');
  13653. }
  13654. if (!defined(p0)) {
  13655. throw new DeveloperError('p0 is required.');
  13656. }
  13657. if (!defined(p1)) {
  13658. throw new DeveloperError('p1 is required.');
  13659. }
  13660. if (!defined(p2)) {
  13661. throw new DeveloperError('p2 is required.');
  13662. }
  13663. var ray = scratchLineSegmentTriangleRay;
  13664. Cartesian3.clone(v0, ray.origin);
  13665. Cartesian3.subtract(v1, v0, ray.direction);
  13666. Cartesian3.normalize(ray.direction, ray.direction);
  13667. var t = IntersectionTests.rayTriangleParametric(ray, p0, p1, p2, cullBackFaces);
  13668. if (!defined(t) || t < 0.0 || t > Cartesian3.distance(v0, v1)) {
  13669. return undefined;
  13670. }
  13671. if (!defined(result)) {
  13672. result = new Cartesian3();
  13673. }
  13674. Cartesian3.multiplyByScalar(ray.direction, t, result);
  13675. return Cartesian3.add(ray.origin, result, result);
  13676. };
  13677. function solveQuadratic(a, b, c, result) {
  13678. var det = b * b - 4.0 * a * c;
  13679. if (det < 0.0) {
  13680. return undefined;
  13681. } else if (det > 0.0) {
  13682. var denom = 1.0 / (2.0 * a);
  13683. var disc = Math.sqrt(det);
  13684. var root0 = (-b + disc) * denom;
  13685. var root1 = (-b - disc) * denom;
  13686. if (root0 < root1) {
  13687. result.root0 = root0;
  13688. result.root1 = root1;
  13689. } else {
  13690. result.root0 = root1;
  13691. result.root1 = root0;
  13692. }
  13693. return result;
  13694. }
  13695. var root = -b / (2.0 * a);
  13696. if (root === 0.0) {
  13697. return undefined;
  13698. }
  13699. result.root0 = result.root1 = root;
  13700. return result;
  13701. }
  13702. var raySphereRoots = {
  13703. root0 : 0.0,
  13704. root1 : 0.0
  13705. };
  13706. function raySphere(ray, sphere, result) {
  13707. if (!defined(result)) {
  13708. result = {};
  13709. }
  13710. var origin = ray.origin;
  13711. var direction = ray.direction;
  13712. var center = sphere.center;
  13713. var radiusSquared = sphere.radius * sphere.radius;
  13714. var diff = Cartesian3.subtract(origin, center, scratchPVec);
  13715. var a = Cartesian3.dot(direction, direction);
  13716. var b = 2.0 * Cartesian3.dot(direction, diff);
  13717. var c = Cartesian3.magnitudeSquared(diff) - radiusSquared;
  13718. var roots = solveQuadratic(a, b, c, raySphereRoots);
  13719. if (!defined(roots)) {
  13720. return undefined;
  13721. }
  13722. result.start = roots.root0;
  13723. result.stop = roots.root1;
  13724. return result;
  13725. }
  13726. /**
  13727. * Computes the intersection points of a ray with a sphere.
  13728. * @memberof IntersectionTests
  13729. *
  13730. * @param {Ray} ray The ray.
  13731. * @param {BoundingSphere} sphere The sphere.
  13732. * @param {Object} [result] The result onto which to store the result.
  13733. * @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.
  13734. */
  13735. IntersectionTests.raySphere = function(ray, sphere, result) {
  13736. if (!defined(ray)) {
  13737. throw new DeveloperError('ray is required.');
  13738. }
  13739. if (!defined(sphere)) {
  13740. throw new DeveloperError('sphere is required.');
  13741. }
  13742. result = raySphere(ray, sphere, result);
  13743. if (!defined(result) || result.stop < 0.0) {
  13744. return undefined;
  13745. }
  13746. result.start = Math.max(result.start, 0.0);
  13747. return result;
  13748. };
  13749. var scratchLineSegmentRay = new Ray();
  13750. /**
  13751. * Computes the intersection points of a line segment with a sphere.
  13752. * @memberof IntersectionTests
  13753. *
  13754. * @param {Cartesian3} p0 An end point of the line segment.
  13755. * @param {Cartesian3} p1 The other end point of the line segment.
  13756. * @param {BoundingSphere} sphere The sphere.
  13757. * @param {Object} [result] The result onto which to store the result.
  13758. * @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.
  13759. */
  13760. IntersectionTests.lineSegmentSphere = function(p0, p1, sphere, result) {
  13761. if (!defined(p0)) {
  13762. throw new DeveloperError('p0 is required.');
  13763. }
  13764. if (!defined(p1)) {
  13765. throw new DeveloperError('p1 is required.');
  13766. }
  13767. if (!defined(sphere)) {
  13768. throw new DeveloperError('sphere is required.');
  13769. }
  13770. var ray = scratchLineSegmentRay;
  13771. Cartesian3.clone(p0, ray.origin);
  13772. var direction = Cartesian3.subtract(p1, p0, ray.direction);
  13773. var maxT = Cartesian3.magnitude(direction);
  13774. Cartesian3.normalize(direction, direction);
  13775. result = raySphere(ray, sphere, result);
  13776. if (!defined(result) || result.stop < 0.0 || result.start > maxT) {
  13777. return undefined;
  13778. }
  13779. result.start = Math.max(result.start, 0.0);
  13780. result.stop = Math.min(result.stop, maxT);
  13781. return result;
  13782. };
  13783. var scratchQ = new Cartesian3();
  13784. var scratchW = new Cartesian3();
  13785. /**
  13786. * Computes the intersection points of a ray with an ellipsoid.
  13787. *
  13788. * @param {Ray} ray The ray.
  13789. * @param {Ellipsoid} ellipsoid The ellipsoid.
  13790. * @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.
  13791. */
  13792. IntersectionTests.rayEllipsoid = function(ray, ellipsoid) {
  13793. if (!defined(ray)) {
  13794. throw new DeveloperError('ray is required.');
  13795. }
  13796. if (!defined(ellipsoid)) {
  13797. throw new DeveloperError('ellipsoid is required.');
  13798. }
  13799. var inverseRadii = ellipsoid.oneOverRadii;
  13800. var q = Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  13801. var w = Cartesian3.multiplyComponents(inverseRadii, ray.direction, scratchW);
  13802. var q2 = Cartesian3.magnitudeSquared(q);
  13803. var qw = Cartesian3.dot(q, w);
  13804. var difference, w2, product, discriminant, temp;
  13805. if (q2 > 1.0) {
  13806. // Outside ellipsoid.
  13807. if (qw >= 0.0) {
  13808. // Looking outward or tangent (0 intersections).
  13809. return undefined;
  13810. }
  13811. // qw < 0.0.
  13812. var qw2 = qw * qw;
  13813. difference = q2 - 1.0; // Positively valued.
  13814. w2 = Cartesian3.magnitudeSquared(w);
  13815. product = w2 * difference;
  13816. if (qw2 < product) {
  13817. // Imaginary roots (0 intersections).
  13818. return undefined;
  13819. } else if (qw2 > product) {
  13820. // Distinct roots (2 intersections).
  13821. discriminant = qw * qw - product;
  13822. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  13823. var root0 = temp / w2;
  13824. var root1 = difference / temp;
  13825. if (root0 < root1) {
  13826. return {
  13827. start : root0,
  13828. stop : root1
  13829. };
  13830. }
  13831. return {
  13832. start : root1,
  13833. stop : root0
  13834. };
  13835. } else {
  13836. // qw2 == product. Repeated roots (2 intersections).
  13837. var root = Math.sqrt(difference / w2);
  13838. return {
  13839. start : root,
  13840. stop : root
  13841. };
  13842. }
  13843. } else if (q2 < 1.0) {
  13844. // Inside ellipsoid (2 intersections).
  13845. difference = q2 - 1.0; // Negatively valued.
  13846. w2 = Cartesian3.magnitudeSquared(w);
  13847. product = w2 * difference; // Negatively valued.
  13848. discriminant = qw * qw - product;
  13849. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  13850. return {
  13851. start : 0.0,
  13852. stop : temp / w2
  13853. };
  13854. } else {
  13855. // q2 == 1.0. On ellipsoid.
  13856. if (qw < 0.0) {
  13857. // Looking inward.
  13858. w2 = Cartesian3.magnitudeSquared(w);
  13859. return {
  13860. start : 0.0,
  13861. stop : -qw / w2
  13862. };
  13863. }
  13864. // qw >= 0.0. Looking outward or tangent.
  13865. return undefined;
  13866. }
  13867. };
  13868. function addWithCancellationCheck(left, right, tolerance) {
  13869. var difference = left + right;
  13870. if ((CesiumMath.sign(left) !== CesiumMath.sign(right)) &&
  13871. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance) {
  13872. return 0.0;
  13873. }
  13874. return difference;
  13875. }
  13876. function quadraticVectorExpression(A, b, c, x, w) {
  13877. var xSquared = x * x;
  13878. var wSquared = w * w;
  13879. var l2 = (A[Matrix3.COLUMN1ROW1] - A[Matrix3.COLUMN2ROW2]) * wSquared;
  13880. var l1 = w * (x * addWithCancellationCheck(A[Matrix3.COLUMN1ROW0], A[Matrix3.COLUMN0ROW1], CesiumMath.EPSILON15) + b.y);
  13881. var l0 = (A[Matrix3.COLUMN0ROW0] * xSquared + A[Matrix3.COLUMN2ROW2] * wSquared) + x * b.x + c;
  13882. var r1 = wSquared * addWithCancellationCheck(A[Matrix3.COLUMN2ROW1], A[Matrix3.COLUMN1ROW2], CesiumMath.EPSILON15);
  13883. var r0 = w * (x * addWithCancellationCheck(A[Matrix3.COLUMN2ROW0], A[Matrix3.COLUMN0ROW2]) + b.z);
  13884. var cosines;
  13885. var solutions = [];
  13886. if (r0 === 0.0 && r1 === 0.0) {
  13887. cosines = QuadraticRealPolynomial.computeRealRoots(l2, l1, l0);
  13888. if (cosines.length === 0) {
  13889. return solutions;
  13890. }
  13891. var cosine0 = cosines[0];
  13892. var sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  13893. solutions.push(new Cartesian3(x, w * cosine0, w * -sine0));
  13894. solutions.push(new Cartesian3(x, w * cosine0, w * sine0));
  13895. if (cosines.length === 2) {
  13896. var cosine1 = cosines[1];
  13897. var sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  13898. solutions.push(new Cartesian3(x, w * cosine1, w * -sine1));
  13899. solutions.push(new Cartesian3(x, w * cosine1, w * sine1));
  13900. }
  13901. return solutions;
  13902. }
  13903. var r0Squared = r0 * r0;
  13904. var r1Squared = r1 * r1;
  13905. var l2Squared = l2 * l2;
  13906. var r0r1 = r0 * r1;
  13907. var c4 = l2Squared + r1Squared;
  13908. var c3 = 2.0 * (l1 * l2 + r0r1);
  13909. var c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  13910. var c1 = 2.0 * (l0 * l1 - r0r1);
  13911. var c0 = l0 * l0 - r0Squared;
  13912. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  13913. return solutions;
  13914. }
  13915. cosines = QuarticRealPolynomial.computeRealRoots(c4, c3, c2, c1, c0);
  13916. var length = cosines.length;
  13917. if (length === 0) {
  13918. return solutions;
  13919. }
  13920. for ( var i = 0; i < length; ++i) {
  13921. var cosine = cosines[i];
  13922. var cosineSquared = cosine * cosine;
  13923. var sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  13924. var sine = Math.sqrt(sineSquared);
  13925. //var left = l2 * cosineSquared + l1 * cosine + l0;
  13926. var left;
  13927. if (CesiumMath.sign(l2) === CesiumMath.sign(l0)) {
  13928. left = addWithCancellationCheck(l2 * cosineSquared + l0, l1 * cosine, CesiumMath.EPSILON12);
  13929. } else if (CesiumMath.sign(l0) === CesiumMath.sign(l1 * cosine)) {
  13930. left = addWithCancellationCheck(l2 * cosineSquared, l1 * cosine + l0, CesiumMath.EPSILON12);
  13931. } else {
  13932. left = addWithCancellationCheck(l2 * cosineSquared + l1 * cosine, l0, CesiumMath.EPSILON12);
  13933. }
  13934. var right = addWithCancellationCheck(r1 * cosine, r0, CesiumMath.EPSILON15);
  13935. var product = left * right;
  13936. if (product < 0.0) {
  13937. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  13938. } else if (product > 0.0) {
  13939. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  13940. } else if (sine !== 0.0) {
  13941. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  13942. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  13943. ++i;
  13944. } else {
  13945. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  13946. }
  13947. }
  13948. return solutions;
  13949. }
  13950. var firstAxisScratch = new Cartesian3();
  13951. var secondAxisScratch = new Cartesian3();
  13952. var thirdAxisScratch = new Cartesian3();
  13953. var referenceScratch = new Cartesian3();
  13954. var bCart = new Cartesian3();
  13955. var bScratch = new Matrix3();
  13956. var btScratch = new Matrix3();
  13957. var diScratch = new Matrix3();
  13958. var dScratch = new Matrix3();
  13959. var cScratch = new Matrix3();
  13960. var tempMatrix = new Matrix3();
  13961. var aScratch = new Matrix3();
  13962. var sScratch = new Cartesian3();
  13963. var closestScratch = new Cartesian3();
  13964. var surfPointScratch = new Cartographic();
  13965. /**
  13966. * Provides the point along the ray which is nearest to the ellipsoid.
  13967. *
  13968. * @param {Ray} ray The ray.
  13969. * @param {Ellipsoid} ellipsoid The ellipsoid.
  13970. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  13971. */
  13972. IntersectionTests.grazingAltitudeLocation = function(ray, ellipsoid) {
  13973. if (!defined(ray)) {
  13974. throw new DeveloperError('ray is required.');
  13975. }
  13976. if (!defined(ellipsoid)) {
  13977. throw new DeveloperError('ellipsoid is required.');
  13978. }
  13979. var position = ray.origin;
  13980. var direction = ray.direction;
  13981. if (!Cartesian3.equals(position, Cartesian3.ZERO)) {
  13982. var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  13983. if (Cartesian3.dot(direction, normal) >= 0.0) { // The location provided is the closest point in altitude
  13984. return position;
  13985. }
  13986. }
  13987. var intersects = defined(this.rayEllipsoid(ray, ellipsoid));
  13988. // Compute the scaled direction vector.
  13989. var f = ellipsoid.transformPositionToScaledSpace(direction, firstAxisScratch);
  13990. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  13991. var firstAxis = Cartesian3.normalize(f, f);
  13992. var reference = Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  13993. var secondAxis = Cartesian3.normalize(Cartesian3.cross(reference, firstAxis, secondAxisScratch), secondAxisScratch);
  13994. var thirdAxis = Cartesian3.normalize(Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch), thirdAxisScratch);
  13995. var B = bScratch;
  13996. B[0] = firstAxis.x;
  13997. B[1] = firstAxis.y;
  13998. B[2] = firstAxis.z;
  13999. B[3] = secondAxis.x;
  14000. B[4] = secondAxis.y;
  14001. B[5] = secondAxis.z;
  14002. B[6] = thirdAxis.x;
  14003. B[7] = thirdAxis.y;
  14004. B[8] = thirdAxis.z;
  14005. var B_T = Matrix3.transpose(B, btScratch);
  14006. // Get the scaling matrix and its inverse.
  14007. var D_I = Matrix3.fromScale(ellipsoid.radii, diScratch);
  14008. var D = Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  14009. var C = cScratch;
  14010. C[0] = 0.0;
  14011. C[1] = -direction.z;
  14012. C[2] = direction.y;
  14013. C[3] = direction.z;
  14014. C[4] = 0.0;
  14015. C[5] = -direction.x;
  14016. C[6] = -direction.y;
  14017. C[7] = direction.x;
  14018. C[8] = 0.0;
  14019. var temp = Matrix3.multiply(Matrix3.multiply(B_T, D, tempMatrix), C, tempMatrix);
  14020. var A = Matrix3.multiply(Matrix3.multiply(temp, D_I, aScratch), B, aScratch);
  14021. var b = Matrix3.multiplyByVector(temp, position, bCart);
  14022. // Solve for the solutions to the expression in standard form:
  14023. var solutions = quadraticVectorExpression(A, Cartesian3.negate(b, firstAxisScratch), 0.0, 0.0, 1.0);
  14024. var s;
  14025. var altitude;
  14026. var length = solutions.length;
  14027. if (length > 0) {
  14028. var closest = Cartesian3.clone(Cartesian3.ZERO, closestScratch);
  14029. var maximumValue = Number.NEGATIVE_INFINITY;
  14030. for ( var i = 0; i < length; ++i) {
  14031. s = Matrix3.multiplyByVector(D_I, Matrix3.multiplyByVector(B, solutions[i], sScratch), sScratch);
  14032. var v = Cartesian3.normalize(Cartesian3.subtract(s, position, referenceScratch), referenceScratch);
  14033. var dotProduct = Cartesian3.dot(v, direction);
  14034. if (dotProduct > maximumValue) {
  14035. maximumValue = dotProduct;
  14036. closest = Cartesian3.clone(s, closest);
  14037. }
  14038. }
  14039. var surfacePoint = ellipsoid.cartesianToCartographic(closest, surfPointScratch);
  14040. maximumValue = CesiumMath.clamp(maximumValue, 0.0, 1.0);
  14041. altitude = Cartesian3.magnitude(Cartesian3.subtract(closest, position, referenceScratch)) * Math.sqrt(1.0 - maximumValue * maximumValue);
  14042. altitude = intersects ? -altitude : altitude;
  14043. surfacePoint.height = altitude;
  14044. return ellipsoid.cartographicToCartesian(surfacePoint, new Cartesian3());
  14045. }
  14046. return undefined;
  14047. };
  14048. var lineSegmentPlaneDifference = new Cartesian3();
  14049. /**
  14050. * Computes the intersection of a line segment and a plane.
  14051. *
  14052. * @param {Cartesian3} endPoint0 An end point of the line segment.
  14053. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  14054. * @param {Plane} plane The plane.
  14055. * @param {Cartesian3} [result] The object onto which to store the result.
  14056. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  14057. *
  14058. * @example
  14059. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  14060. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  14061. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  14062. *
  14063. * var p0 = new Cesium.Cartesian3(...);
  14064. * var p1 = new Cesium.Cartesian3(...);
  14065. *
  14066. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  14067. * var intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  14068. */
  14069. IntersectionTests.lineSegmentPlane = function(endPoint0, endPoint1, plane, result) {
  14070. if (!defined(endPoint0)) {
  14071. throw new DeveloperError('endPoint0 is required.');
  14072. }
  14073. if (!defined(endPoint1)) {
  14074. throw new DeveloperError('endPoint1 is required.');
  14075. }
  14076. if (!defined(plane)) {
  14077. throw new DeveloperError('plane is required.');
  14078. }
  14079. if (!defined(result)) {
  14080. result = new Cartesian3();
  14081. }
  14082. var difference = Cartesian3.subtract(endPoint1, endPoint0, lineSegmentPlaneDifference);
  14083. var normal = plane.normal;
  14084. var nDotDiff = Cartesian3.dot(normal, difference);
  14085. // check if the segment and plane are parallel
  14086. if (Math.abs(nDotDiff) < CesiumMath.EPSILON6) {
  14087. return undefined;
  14088. }
  14089. var nDotP0 = Cartesian3.dot(normal, endPoint0);
  14090. var t = -(plane.distance + nDotP0) / nDotDiff;
  14091. // intersection only if t is in [0, 1]
  14092. if (t < 0.0 || t > 1.0) {
  14093. return undefined;
  14094. }
  14095. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  14096. Cartesian3.multiplyByScalar(difference, t, result);
  14097. Cartesian3.add(endPoint0, result, result);
  14098. return result;
  14099. };
  14100. /**
  14101. * Computes the intersection of a triangle and a plane
  14102. *
  14103. * @param {Cartesian3} p0 First point of the triangle
  14104. * @param {Cartesian3} p1 Second point of the triangle
  14105. * @param {Cartesian3} p2 Third point of the triangle
  14106. * @param {Plane} plane Intersection plane
  14107. * @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)
  14108. *
  14109. * @example
  14110. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  14111. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  14112. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  14113. *
  14114. * var p0 = new Cesium.Cartesian3(...);
  14115. * var p1 = new Cesium.Cartesian3(...);
  14116. * var p2 = new Cesium.Cartesian3(...);
  14117. *
  14118. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  14119. * var triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  14120. */
  14121. IntersectionTests.trianglePlaneIntersection = function(p0, p1, p2, plane) {
  14122. if ((!defined(p0)) ||
  14123. (!defined(p1)) ||
  14124. (!defined(p2)) ||
  14125. (!defined(plane))) {
  14126. throw new DeveloperError('p0, p1, p2, and plane are required.');
  14127. }
  14128. var planeNormal = plane.normal;
  14129. var planeD = plane.distance;
  14130. var p0Behind = (Cartesian3.dot(planeNormal, p0) + planeD) < 0.0;
  14131. var p1Behind = (Cartesian3.dot(planeNormal, p1) + planeD) < 0.0;
  14132. var p2Behind = (Cartesian3.dot(planeNormal, p2) + planeD) < 0.0;
  14133. // Given these dots products, the calls to lineSegmentPlaneIntersection
  14134. // always have defined results.
  14135. var numBehind = 0;
  14136. numBehind += p0Behind ? 1 : 0;
  14137. numBehind += p1Behind ? 1 : 0;
  14138. numBehind += p2Behind ? 1 : 0;
  14139. var u1, u2;
  14140. if (numBehind === 1 || numBehind === 2) {
  14141. u1 = new Cartesian3();
  14142. u2 = new Cartesian3();
  14143. }
  14144. if (numBehind === 1) {
  14145. if (p0Behind) {
  14146. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  14147. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  14148. return {
  14149. positions : [p0, p1, p2, u1, u2 ],
  14150. indices : [
  14151. // Behind
  14152. 0, 3, 4,
  14153. // In front
  14154. 1, 2, 4,
  14155. 1, 4, 3
  14156. ]
  14157. };
  14158. } else if (p1Behind) {
  14159. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  14160. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  14161. return {
  14162. positions : [p0, p1, p2, u1, u2 ],
  14163. indices : [
  14164. // Behind
  14165. 1, 3, 4,
  14166. // In front
  14167. 2, 0, 4,
  14168. 2, 4, 3
  14169. ]
  14170. };
  14171. } else if (p2Behind) {
  14172. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  14173. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  14174. return {
  14175. positions : [p0, p1, p2, u1, u2 ],
  14176. indices : [
  14177. // Behind
  14178. 2, 3, 4,
  14179. // In front
  14180. 0, 1, 4,
  14181. 0, 4, 3
  14182. ]
  14183. };
  14184. }
  14185. } else if (numBehind === 2) {
  14186. if (!p0Behind) {
  14187. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  14188. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  14189. return {
  14190. positions : [p0, p1, p2, u1, u2 ],
  14191. indices : [
  14192. // Behind
  14193. 1, 2, 4,
  14194. 1, 4, 3,
  14195. // In front
  14196. 0, 3, 4
  14197. ]
  14198. };
  14199. } else if (!p1Behind) {
  14200. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  14201. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  14202. return {
  14203. positions : [p0, p1, p2, u1, u2 ],
  14204. indices : [
  14205. // Behind
  14206. 2, 0, 4,
  14207. 2, 4, 3,
  14208. // In front
  14209. 1, 3, 4
  14210. ]
  14211. };
  14212. } else if (!p2Behind) {
  14213. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  14214. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  14215. return {
  14216. positions : [p0, p1, p2, u1, u2 ],
  14217. indices : [
  14218. // Behind
  14219. 0, 1, 4,
  14220. 0, 4, 3,
  14221. // In front
  14222. 2, 3, 4
  14223. ]
  14224. };
  14225. }
  14226. }
  14227. // if numBehind is 3, the triangle is completely behind the plane;
  14228. // otherwise, it is completely in front (numBehind is 0).
  14229. return undefined;
  14230. };
  14231. return IntersectionTests;
  14232. });
  14233. /*global define*/
  14234. define('Core/Plane',[
  14235. './Cartesian3',
  14236. './defined',
  14237. './DeveloperError',
  14238. './freezeObject'
  14239. ], function(
  14240. Cartesian3,
  14241. defined,
  14242. DeveloperError,
  14243. freezeObject) {
  14244. 'use strict';
  14245. /**
  14246. * A plane in Hessian Normal Form defined by
  14247. * <pre>
  14248. * ax + by + cz + d = 0
  14249. * </pre>
  14250. * where (a, b, c) is the plane's <code>normal</code>, d is the signed
  14251. * <code>distance</code> to the plane, and (x, y, z) is any point on
  14252. * the plane.
  14253. *
  14254. * @alias Plane
  14255. * @constructor
  14256. *
  14257. * @param {Cartesian3} normal The plane's normal (normalized).
  14258. * @param {Number} distance The shortest distance from the origin to the plane. The sign of
  14259. * <code>distance</code> determines which side of the plane the origin
  14260. * is on. If <code>distance</code> is positive, the origin is in the half-space
  14261. * in the direction of the normal; if negative, the origin is in the half-space
  14262. * opposite to the normal; if zero, the plane passes through the origin.
  14263. *
  14264. * @example
  14265. * // The plane x=0
  14266. * var plane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X, 0.0);
  14267. */
  14268. function Plane(normal, distance) {
  14269. if (!defined(normal)) {
  14270. throw new DeveloperError('normal is required.');
  14271. }
  14272. if (!defined(distance)) {
  14273. throw new DeveloperError('distance is required.');
  14274. }
  14275. /**
  14276. * The plane's normal.
  14277. *
  14278. * @type {Cartesian3}
  14279. */
  14280. this.normal = Cartesian3.clone(normal);
  14281. /**
  14282. * The shortest distance from the origin to the plane. The sign of
  14283. * <code>distance</code> determines which side of the plane the origin
  14284. * is on. If <code>distance</code> is positive, the origin is in the half-space
  14285. * in the direction of the normal; if negative, the origin is in the half-space
  14286. * opposite to the normal; if zero, the plane passes through the origin.
  14287. *
  14288. * @type {Number}
  14289. */
  14290. this.distance = distance;
  14291. }
  14292. /**
  14293. * Creates a plane from a normal and a point on the plane.
  14294. *
  14295. * @param {Cartesian3} point The point on the plane.
  14296. * @param {Cartesian3} normal The plane's normal (normalized).
  14297. * @param {Plane} [result] The object onto which to store the result.
  14298. * @returns {Plane} A new plane instance or the modified result parameter.
  14299. *
  14300. * @example
  14301. * var point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  14302. * var normal = ellipsoid.geodeticSurfaceNormal(point);
  14303. * var tangentPlane = Cesium.Plane.fromPointNormal(point, normal);
  14304. */
  14305. Plane.fromPointNormal = function(point, normal, result) {
  14306. if (!defined(point)) {
  14307. throw new DeveloperError('point is required.');
  14308. }
  14309. if (!defined(normal)) {
  14310. throw new DeveloperError('normal is required.');
  14311. }
  14312. var distance = -Cartesian3.dot(normal, point);
  14313. if (!defined(result)) {
  14314. return new Plane(normal, distance);
  14315. }
  14316. Cartesian3.clone(normal, result.normal);
  14317. result.distance = distance;
  14318. return result;
  14319. };
  14320. var scratchNormal = new Cartesian3();
  14321. /**
  14322. * Creates a plane from the general equation
  14323. *
  14324. * @param {Cartesian4} coefficients The plane's normal (normalized).
  14325. * @param {Plane} [result] The object onto which to store the result.
  14326. * @returns {Plane} A new plane instance or the modified result parameter.
  14327. */
  14328. Plane.fromCartesian4 = function(coefficients, result) {
  14329. if (!defined(coefficients)) {
  14330. throw new DeveloperError('coefficients is required.');
  14331. }
  14332. var normal = Cartesian3.fromCartesian4(coefficients, scratchNormal);
  14333. var distance = coefficients.w;
  14334. if (!defined(result)) {
  14335. return new Plane(normal, distance);
  14336. } else {
  14337. Cartesian3.clone(normal, result.normal);
  14338. result.distance = distance;
  14339. return result;
  14340. }
  14341. };
  14342. /**
  14343. * Computes the signed shortest distance of a point to a plane.
  14344. * The sign of the distance determines which side of the plane the point
  14345. * is on. If the distance is positive, the point is in the half-space
  14346. * in the direction of the normal; if negative, the point is in the half-space
  14347. * opposite to the normal; if zero, the plane passes through the point.
  14348. *
  14349. * @param {Plane} plane The plane.
  14350. * @param {Cartesian3} point The point.
  14351. * @returns {Number} The signed shortest distance of the point to the plane.
  14352. */
  14353. Plane.getPointDistance = function(plane, point) {
  14354. if (!defined(plane)) {
  14355. throw new DeveloperError('plane is required.');
  14356. }
  14357. if (!defined(point)) {
  14358. throw new DeveloperError('point is required.');
  14359. }
  14360. return Cartesian3.dot(plane.normal, point) + plane.distance;
  14361. };
  14362. /**
  14363. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  14364. *
  14365. * @type {Plane}
  14366. * @constant
  14367. */
  14368. Plane.ORIGIN_XY_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Z, 0.0));
  14369. /**
  14370. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  14371. *
  14372. * @type {Plane}
  14373. * @constant
  14374. */
  14375. Plane.ORIGIN_YZ_PLANE = freezeObject(new Plane(Cartesian3.UNIT_X, 0.0));
  14376. /**
  14377. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  14378. *
  14379. * @type {Plane}
  14380. * @constant
  14381. */
  14382. Plane.ORIGIN_ZX_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Y, 0.0));
  14383. return Plane;
  14384. });
  14385. /**
  14386. @license
  14387. when.js - https://github.com/cujojs/when
  14388. MIT License (c) copyright B Cavalier & J Hann
  14389. * A lightweight CommonJS Promises/A and when() implementation
  14390. * when is part of the cujo.js family of libraries (http://cujojs.com/)
  14391. *
  14392. * Licensed under the MIT License at:
  14393. * http://www.opensource.org/licenses/mit-license.php
  14394. *
  14395. * @version 1.7.1
  14396. */
  14397. (function(define) { 'use strict';
  14398. define('ThirdParty/when',[],function () {
  14399. var reduceArray, slice, undef;
  14400. //
  14401. // Public API
  14402. //
  14403. when.defer = defer; // Create a deferred
  14404. when.resolve = resolve; // Create a resolved promise
  14405. when.reject = reject; // Create a rejected promise
  14406. when.join = join; // Join 2 or more promises
  14407. when.all = all; // Resolve a list of promises
  14408. when.map = map; // Array.map() for promises
  14409. when.reduce = reduce; // Array.reduce() for promises
  14410. when.any = any; // One-winner race
  14411. when.some = some; // Multi-winner race
  14412. when.chain = chain; // Make a promise trigger another resolver
  14413. when.isPromise = isPromise; // Determine if a thing is a promise
  14414. /**
  14415. * Register an observer for a promise or immediate value.
  14416. *
  14417. * @param {*} promiseOrValue
  14418. * @param {function?} [onFulfilled] callback to be called when promiseOrValue is
  14419. * successfully fulfilled. If promiseOrValue is an immediate value, callback
  14420. * will be invoked immediately.
  14421. * @param {function?} [onRejected] callback to be called when promiseOrValue is
  14422. * rejected.
  14423. * @param {function?} [onProgress] callback to be called when progress updates
  14424. * are issued for promiseOrValue.
  14425. * @returns {Promise} a new {@link Promise} that will complete with the return
  14426. * value of callback or errback or the completion value of promiseOrValue if
  14427. * callback and/or errback is not supplied.
  14428. */
  14429. function when(promiseOrValue, onFulfilled, onRejected, onProgress) {
  14430. // Get a trusted promise for the input promiseOrValue, and then
  14431. // register promise handlers
  14432. return resolve(promiseOrValue).then(onFulfilled, onRejected, onProgress);
  14433. }
  14434. /**
  14435. * Returns promiseOrValue if promiseOrValue is a {@link Promise}, a new Promise if
  14436. * promiseOrValue is a foreign promise, or a new, already-fulfilled {@link Promise}
  14437. * whose value is promiseOrValue if promiseOrValue is an immediate value.
  14438. *
  14439. * @param {*} promiseOrValue
  14440. * @returns Guaranteed to return a trusted Promise. If promiseOrValue is a when.js {@link Promise}
  14441. * returns promiseOrValue, otherwise, returns a new, already-resolved, when.js {@link Promise}
  14442. * whose resolution value is:
  14443. * * the resolution value of promiseOrValue if it's a foreign promise, or
  14444. * * promiseOrValue if it's a value
  14445. */
  14446. function resolve(promiseOrValue) {
  14447. var promise, deferred;
  14448. if(promiseOrValue instanceof Promise) {
  14449. // It's a when.js promise, so we trust it
  14450. promise = promiseOrValue;
  14451. } else {
  14452. // It's not a when.js promise. See if it's a foreign promise or a value.
  14453. if(isPromise(promiseOrValue)) {
  14454. // It's a thenable, but we don't know where it came from, so don't trust
  14455. // its implementation entirely. Introduce a trusted middleman when.js promise
  14456. deferred = defer();
  14457. // IMPORTANT: This is the only place when.js should ever call .then() on an
  14458. // untrusted promise. Don't expose the return value to the untrusted promise
  14459. promiseOrValue.then(
  14460. function(value) { deferred.resolve(value); },
  14461. function(reason) { deferred.reject(reason); },
  14462. function(update) { deferred.progress(update); }
  14463. );
  14464. promise = deferred.promise;
  14465. } else {
  14466. // It's a value, not a promise. Create a resolved promise for it.
  14467. promise = fulfilled(promiseOrValue);
  14468. }
  14469. }
  14470. return promise;
  14471. }
  14472. /**
  14473. * Returns a rejected promise for the supplied promiseOrValue. The returned
  14474. * promise will be rejected with:
  14475. * - promiseOrValue, if it is a value, or
  14476. * - if promiseOrValue is a promise
  14477. * - promiseOrValue's value after it is fulfilled
  14478. * - promiseOrValue's reason after it is rejected
  14479. * @param {*} promiseOrValue the rejected value of the returned {@link Promise}
  14480. * @returns {Promise} rejected {@link Promise}
  14481. */
  14482. function reject(promiseOrValue) {
  14483. return when(promiseOrValue, rejected);
  14484. }
  14485. /**
  14486. * Trusted Promise constructor. A Promise created from this constructor is
  14487. * a trusted when.js promise. Any other duck-typed promise is considered
  14488. * untrusted.
  14489. * @constructor
  14490. * @name Promise
  14491. */
  14492. function Promise(then) {
  14493. this.then = then;
  14494. }
  14495. Promise.prototype = {
  14496. /**
  14497. * Register a callback that will be called when a promise is
  14498. * fulfilled or rejected. Optionally also register a progress handler.
  14499. * Shortcut for .then(onFulfilledOrRejected, onFulfilledOrRejected, onProgress)
  14500. * @param {function?} [onFulfilledOrRejected]
  14501. * @param {function?} [onProgress]
  14502. * @returns {Promise}
  14503. */
  14504. always: function(onFulfilledOrRejected, onProgress) {
  14505. return this.then(onFulfilledOrRejected, onFulfilledOrRejected, onProgress);
  14506. },
  14507. /**
  14508. * Register a rejection handler. Shortcut for .then(undefined, onRejected)
  14509. * @param {function?} onRejected
  14510. * @returns {Promise}
  14511. */
  14512. otherwise: function(onRejected) {
  14513. return this.then(undef, onRejected);
  14514. },
  14515. /**
  14516. * Shortcut for .then(function() { return value; })
  14517. * @param {*} value
  14518. * @returns {Promise} a promise that:
  14519. * - is fulfilled if value is not a promise, or
  14520. * - if value is a promise, will fulfill with its value, or reject
  14521. * with its reason.
  14522. */
  14523. yield: function(value) {
  14524. return this.then(function() {
  14525. return value;
  14526. });
  14527. },
  14528. /**
  14529. * Assumes that this promise will fulfill with an array, and arranges
  14530. * for the onFulfilled to be called with the array as its argument list
  14531. * i.e. onFulfilled.spread(undefined, array).
  14532. * @param {function} onFulfilled function to receive spread arguments
  14533. * @returns {Promise}
  14534. */
  14535. spread: function(onFulfilled) {
  14536. return this.then(function(array) {
  14537. // array may contain promises, so resolve its contents.
  14538. return all(array, function(array) {
  14539. return onFulfilled.apply(undef, array);
  14540. });
  14541. });
  14542. }
  14543. };
  14544. /**
  14545. * Create an already-resolved promise for the supplied value
  14546. * @private
  14547. *
  14548. * @param {*} value
  14549. * @returns {Promise} fulfilled promise
  14550. */
  14551. function fulfilled(value) {
  14552. var p = new Promise(function(onFulfilled) {
  14553. // TODO: Promises/A+ check typeof onFulfilled
  14554. try {
  14555. return resolve(onFulfilled ? onFulfilled(value) : value);
  14556. } catch(e) {
  14557. return rejected(e);
  14558. }
  14559. });
  14560. return p;
  14561. }
  14562. /**
  14563. * Create an already-rejected {@link Promise} with the supplied
  14564. * rejection reason.
  14565. * @private
  14566. *
  14567. * @param {*} reason
  14568. * @returns {Promise} rejected promise
  14569. */
  14570. function rejected(reason) {
  14571. var p = new Promise(function(_, onRejected) {
  14572. // TODO: Promises/A+ check typeof onRejected
  14573. try {
  14574. return onRejected ? resolve(onRejected(reason)) : rejected(reason);
  14575. } catch(e) {
  14576. return rejected(e);
  14577. }
  14578. });
  14579. return p;
  14580. }
  14581. /**
  14582. * Creates a new, Deferred with fully isolated resolver and promise parts,
  14583. * either or both of which may be given out safely to consumers.
  14584. * The Deferred itself has the full API: resolve, reject, progress, and
  14585. * then. The resolver has resolve, reject, and progress. The promise
  14586. * only has then.
  14587. *
  14588. * @returns {Deferred}
  14589. */
  14590. function defer() {
  14591. var deferred, promise, handlers, progressHandlers,
  14592. _then, _progress, _resolve;
  14593. /**
  14594. * The promise for the new deferred
  14595. * @type {Promise}
  14596. */
  14597. promise = new Promise(then);
  14598. /**
  14599. * The full Deferred object, with {@link Promise} and {@link Resolver} parts
  14600. * @class Deferred
  14601. * @name Deferred
  14602. */
  14603. deferred = {
  14604. then: then, // DEPRECATED: use deferred.promise.then
  14605. resolve: promiseResolve,
  14606. reject: promiseReject,
  14607. // TODO: Consider renaming progress() to notify()
  14608. progress: promiseProgress,
  14609. promise: promise,
  14610. resolver: {
  14611. resolve: promiseResolve,
  14612. reject: promiseReject,
  14613. progress: promiseProgress
  14614. }
  14615. };
  14616. handlers = [];
  14617. progressHandlers = [];
  14618. /**
  14619. * Pre-resolution then() that adds the supplied callback, errback, and progback
  14620. * functions to the registered listeners
  14621. * @private
  14622. *
  14623. * @param {function?} [onFulfilled] resolution handler
  14624. * @param {function?} [onRejected] rejection handler
  14625. * @param {function?} [onProgress] progress handler
  14626. */
  14627. _then = function(onFulfilled, onRejected, onProgress) {
  14628. // TODO: Promises/A+ check typeof onFulfilled, onRejected, onProgress
  14629. var deferred, progressHandler;
  14630. deferred = defer();
  14631. progressHandler = typeof onProgress === 'function'
  14632. ? function(update) {
  14633. try {
  14634. // Allow progress handler to transform progress event
  14635. deferred.progress(onProgress(update));
  14636. } catch(e) {
  14637. // Use caught value as progress
  14638. deferred.progress(e);
  14639. }
  14640. }
  14641. : function(update) { deferred.progress(update); };
  14642. handlers.push(function(promise) {
  14643. promise.then(onFulfilled, onRejected)
  14644. .then(deferred.resolve, deferred.reject, progressHandler);
  14645. });
  14646. progressHandlers.push(progressHandler);
  14647. return deferred.promise;
  14648. };
  14649. /**
  14650. * Issue a progress event, notifying all progress listeners
  14651. * @private
  14652. * @param {*} update progress event payload to pass to all listeners
  14653. */
  14654. _progress = function(update) {
  14655. processQueue(progressHandlers, update);
  14656. return update;
  14657. };
  14658. /**
  14659. * Transition from pre-resolution state to post-resolution state, notifying
  14660. * all listeners of the resolution or rejection
  14661. * @private
  14662. * @param {*} value the value of this deferred
  14663. */
  14664. _resolve = function(value) {
  14665. value = resolve(value);
  14666. // Replace _then with one that directly notifies with the result.
  14667. _then = value.then;
  14668. // Replace _resolve so that this Deferred can only be resolved once
  14669. _resolve = resolve;
  14670. // Make _progress a noop, to disallow progress for the resolved promise.
  14671. _progress = noop;
  14672. // Notify handlers
  14673. processQueue(handlers, value);
  14674. // Free progressHandlers array since we'll never issue progress events
  14675. progressHandlers = handlers = undef;
  14676. return value;
  14677. };
  14678. return deferred;
  14679. /**
  14680. * Wrapper to allow _then to be replaced safely
  14681. * @param {function?} [onFulfilled] resolution handler
  14682. * @param {function?} [onRejected] rejection handler
  14683. * @param {function?} [onProgress] progress handler
  14684. * @returns {Promise} new promise
  14685. */
  14686. function then(onFulfilled, onRejected, onProgress) {
  14687. // TODO: Promises/A+ check typeof onFulfilled, onRejected, onProgress
  14688. return _then(onFulfilled, onRejected, onProgress);
  14689. }
  14690. /**
  14691. * Wrapper to allow _resolve to be replaced
  14692. */
  14693. function promiseResolve(val) {
  14694. return _resolve(val);
  14695. }
  14696. /**
  14697. * Wrapper to allow _reject to be replaced
  14698. */
  14699. function promiseReject(err) {
  14700. return _resolve(rejected(err));
  14701. }
  14702. /**
  14703. * Wrapper to allow _progress to be replaced
  14704. */
  14705. function promiseProgress(update) {
  14706. return _progress(update);
  14707. }
  14708. }
  14709. /**
  14710. * Determines if promiseOrValue is a promise or not. Uses the feature
  14711. * test from http://wiki.commonjs.org/wiki/Promises/A to determine if
  14712. * promiseOrValue is a promise.
  14713. *
  14714. * @param {*} promiseOrValue anything
  14715. * @returns {boolean} true if promiseOrValue is a {@link Promise}
  14716. */
  14717. function isPromise(promiseOrValue) {
  14718. return promiseOrValue && typeof promiseOrValue.then === 'function';
  14719. }
  14720. /**
  14721. * Initiates a competitive race, returning a promise that will resolve when
  14722. * howMany of the supplied promisesOrValues have resolved, or will reject when
  14723. * it becomes impossible for howMany to resolve, for example, when
  14724. * (promisesOrValues.length - howMany) + 1 input promises reject.
  14725. *
  14726. * @param {Array} promisesOrValues array of anything, may contain a mix
  14727. * of promises and values
  14728. * @param howMany {number} number of promisesOrValues to resolve
  14729. * @param {function?} [onFulfilled] resolution handler
  14730. * @param {function?} [onRejected] rejection handler
  14731. * @param {function?} [onProgress] progress handler
  14732. * @returns {Promise} promise that will resolve to an array of howMany values that
  14733. * resolved first, or will reject with an array of (promisesOrValues.length - howMany) + 1
  14734. * rejection reasons.
  14735. */
  14736. function some(promisesOrValues, howMany, onFulfilled, onRejected, onProgress) {
  14737. checkCallbacks(2, arguments);
  14738. return when(promisesOrValues, function(promisesOrValues) {
  14739. var toResolve, toReject, values, reasons, deferred, fulfillOne, rejectOne, progress, len, i;
  14740. len = promisesOrValues.length >>> 0;
  14741. toResolve = Math.max(0, Math.min(howMany, len));
  14742. values = [];
  14743. toReject = (len - toResolve) + 1;
  14744. reasons = [];
  14745. deferred = defer();
  14746. // No items in the input, resolve immediately
  14747. if (!toResolve) {
  14748. deferred.resolve(values);
  14749. } else {
  14750. progress = deferred.progress;
  14751. rejectOne = function(reason) {
  14752. reasons.push(reason);
  14753. if(!--toReject) {
  14754. fulfillOne = rejectOne = noop;
  14755. deferred.reject(reasons);
  14756. }
  14757. };
  14758. fulfillOne = function(val) {
  14759. // This orders the values based on promise resolution order
  14760. // Another strategy would be to use the original position of
  14761. // the corresponding promise.
  14762. values.push(val);
  14763. if (!--toResolve) {
  14764. fulfillOne = rejectOne = noop;
  14765. deferred.resolve(values);
  14766. }
  14767. };
  14768. for(i = 0; i < len; ++i) {
  14769. if(i in promisesOrValues) {
  14770. when(promisesOrValues[i], fulfiller, rejecter, progress);
  14771. }
  14772. }
  14773. }
  14774. return deferred.then(onFulfilled, onRejected, onProgress);
  14775. function rejecter(reason) {
  14776. rejectOne(reason);
  14777. }
  14778. function fulfiller(val) {
  14779. fulfillOne(val);
  14780. }
  14781. });
  14782. }
  14783. /**
  14784. * Initiates a competitive race, returning a promise that will resolve when
  14785. * any one of the supplied promisesOrValues has resolved or will reject when
  14786. * *all* promisesOrValues have rejected.
  14787. *
  14788. * @param {Array|Promise} promisesOrValues array of anything, may contain a mix
  14789. * of {@link Promise}s and values
  14790. * @param {function?} [onFulfilled] resolution handler
  14791. * @param {function?} [onRejected] rejection handler
  14792. * @param {function?} [onProgress] progress handler
  14793. * @returns {Promise} promise that will resolve to the value that resolved first, or
  14794. * will reject with an array of all rejected inputs.
  14795. */
  14796. function any(promisesOrValues, onFulfilled, onRejected, onProgress) {
  14797. function unwrapSingleResult(val) {
  14798. return onFulfilled ? onFulfilled(val[0]) : val[0];
  14799. }
  14800. return some(promisesOrValues, 1, unwrapSingleResult, onRejected, onProgress);
  14801. }
  14802. /**
  14803. * Return a promise that will resolve only once all the supplied promisesOrValues
  14804. * have resolved. The resolution value of the returned promise will be an array
  14805. * containing the resolution values of each of the promisesOrValues.
  14806. * @memberOf when
  14807. *
  14808. * @param {Array|Promise} promisesOrValues array of anything, may contain a mix
  14809. * of {@link Promise}s and values
  14810. * @param {function?} [onFulfilled] resolution handler
  14811. * @param {function?} [onRejected] rejection handler
  14812. * @param {function?} [onProgress] progress handler
  14813. * @returns {Promise}
  14814. */
  14815. function all(promisesOrValues, onFulfilled, onRejected, onProgress) {
  14816. checkCallbacks(1, arguments);
  14817. return map(promisesOrValues, identity).then(onFulfilled, onRejected, onProgress);
  14818. }
  14819. /**
  14820. * Joins multiple promises into a single returned promise.
  14821. * @returns {Promise} a promise that will fulfill when *all* the input promises
  14822. * have fulfilled, or will reject when *any one* of the input promises rejects.
  14823. */
  14824. function join(/* ...promises */) {
  14825. return map(arguments, identity);
  14826. }
  14827. /**
  14828. * Traditional map function, similar to `Array.prototype.map()`, but allows
  14829. * input to contain {@link Promise}s and/or values, and mapFunc may return
  14830. * either a value or a {@link Promise}
  14831. *
  14832. * @param {Array|Promise} promise array of anything, may contain a mix
  14833. * of {@link Promise}s and values
  14834. * @param {function} mapFunc mapping function mapFunc(value) which may return
  14835. * either a {@link Promise} or value
  14836. * @returns {Promise} a {@link Promise} that will resolve to an array containing
  14837. * the mapped output values.
  14838. */
  14839. function map(promise, mapFunc) {
  14840. return when(promise, function(array) {
  14841. var results, len, toResolve, resolve, i, d;
  14842. // Since we know the resulting length, we can preallocate the results
  14843. // array to avoid array expansions.
  14844. toResolve = len = array.length >>> 0;
  14845. results = [];
  14846. d = defer();
  14847. if(!toResolve) {
  14848. d.resolve(results);
  14849. } else {
  14850. resolve = function resolveOne(item, i) {
  14851. when(item, mapFunc).then(function(mapped) {
  14852. results[i] = mapped;
  14853. if(!--toResolve) {
  14854. d.resolve(results);
  14855. }
  14856. }, d.reject);
  14857. };
  14858. // Since mapFunc may be async, get all invocations of it into flight
  14859. for(i = 0; i < len; i++) {
  14860. if(i in array) {
  14861. resolve(array[i], i);
  14862. } else {
  14863. --toResolve;
  14864. }
  14865. }
  14866. }
  14867. return d.promise;
  14868. });
  14869. }
  14870. /**
  14871. * Traditional reduce function, similar to `Array.prototype.reduce()`, but
  14872. * input may contain promises and/or values, and reduceFunc
  14873. * may return either a value or a promise, *and* initialValue may
  14874. * be a promise for the starting value.
  14875. *
  14876. * @param {Array|Promise} promise array or promise for an array of anything,
  14877. * may contain a mix of promises and values.
  14878. * @param {function} reduceFunc reduce function reduce(currentValue, nextValue, index, total),
  14879. * where total is the total number of items being reduced, and will be the same
  14880. * in each call to reduceFunc.
  14881. * @returns {Promise} that will resolve to the final reduced value
  14882. */
  14883. function reduce(promise, reduceFunc /*, initialValue */) {
  14884. var args = slice.call(arguments, 1);
  14885. return when(promise, function(array) {
  14886. var total;
  14887. total = array.length;
  14888. // Wrap the supplied reduceFunc with one that handles promises and then
  14889. // delegates to the supplied.
  14890. args[0] = function (current, val, i) {
  14891. return when(current, function (c) {
  14892. return when(val, function (value) {
  14893. return reduceFunc(c, value, i, total);
  14894. });
  14895. });
  14896. };
  14897. return reduceArray.apply(array, args);
  14898. });
  14899. }
  14900. /**
  14901. * Ensure that resolution of promiseOrValue will trigger resolver with the
  14902. * value or reason of promiseOrValue, or instead with resolveValue if it is provided.
  14903. *
  14904. * @param promiseOrValue
  14905. * @param {Object} resolver
  14906. * @param {function} resolver.resolve
  14907. * @param {function} resolver.reject
  14908. * @param {*} [resolveValue]
  14909. * @returns {Promise}
  14910. */
  14911. function chain(promiseOrValue, resolver, resolveValue) {
  14912. var useResolveValue = arguments.length > 2;
  14913. return when(promiseOrValue,
  14914. function(val) {
  14915. val = useResolveValue ? resolveValue : val;
  14916. resolver.resolve(val);
  14917. return val;
  14918. },
  14919. function(reason) {
  14920. resolver.reject(reason);
  14921. return rejected(reason);
  14922. },
  14923. resolver.progress
  14924. );
  14925. }
  14926. //
  14927. // Utility functions
  14928. //
  14929. /**
  14930. * Apply all functions in queue to value
  14931. * @param {Array} queue array of functions to execute
  14932. * @param {*} value argument passed to each function
  14933. */
  14934. function processQueue(queue, value) {
  14935. var handler, i = 0;
  14936. while (handler = queue[i++]) {
  14937. handler(value);
  14938. }
  14939. }
  14940. /**
  14941. * Helper that checks arrayOfCallbacks to ensure that each element is either
  14942. * a function, or null or undefined.
  14943. * @private
  14944. * @param {number} start index at which to start checking items in arrayOfCallbacks
  14945. * @param {Array} arrayOfCallbacks array to check
  14946. * @throws {Error} if any element of arrayOfCallbacks is something other than
  14947. * a functions, null, or undefined.
  14948. */
  14949. function checkCallbacks(start, arrayOfCallbacks) {
  14950. // TODO: Promises/A+ update type checking and docs
  14951. var arg, i = arrayOfCallbacks.length;
  14952. while(i > start) {
  14953. arg = arrayOfCallbacks[--i];
  14954. if (arg != null && typeof arg != 'function') {
  14955. throw new Error('arg '+i+' must be a function');
  14956. }
  14957. }
  14958. }
  14959. /**
  14960. * No-Op function used in method replacement
  14961. * @private
  14962. */
  14963. function noop() {}
  14964. slice = [].slice;
  14965. // ES5 reduce implementation if native not available
  14966. // See: http://es5.github.com/#x15.4.4.21 as there are many
  14967. // specifics and edge cases.
  14968. reduceArray = [].reduce ||
  14969. function(reduceFunc /*, initialValue */) {
  14970. /*jshint maxcomplexity: 7*/
  14971. // ES5 dictates that reduce.length === 1
  14972. // This implementation deviates from ES5 spec in the following ways:
  14973. // 1. It does not check if reduceFunc is a Callable
  14974. var arr, args, reduced, len, i;
  14975. i = 0;
  14976. // This generates a jshint warning, despite being valid
  14977. // "Missing 'new' prefix when invoking a constructor."
  14978. // See https://github.com/jshint/jshint/issues/392
  14979. arr = Object(this);
  14980. len = arr.length >>> 0;
  14981. args = arguments;
  14982. // If no initialValue, use first item of array (we know length !== 0 here)
  14983. // and adjust i to start at second item
  14984. if(args.length <= 1) {
  14985. // Skip to the first real element in the array
  14986. for(;;) {
  14987. if(i in arr) {
  14988. reduced = arr[i++];
  14989. break;
  14990. }
  14991. // If we reached the end of the array without finding any real
  14992. // elements, it's a TypeError
  14993. if(++i >= len) {
  14994. throw new TypeError();
  14995. }
  14996. }
  14997. } else {
  14998. // If initialValue provided, use it
  14999. reduced = args[1];
  15000. }
  15001. // Do the actual reduce
  15002. for(;i < len; ++i) {
  15003. // Skip holes
  15004. if(i in arr) {
  15005. reduced = reduceFunc(reduced, arr[i], i, arr);
  15006. }
  15007. }
  15008. return reduced;
  15009. };
  15010. function identity(x) {
  15011. return x;
  15012. }
  15013. return when;
  15014. });
  15015. })(typeof define == 'function' && define.amd
  15016. ? define
  15017. : function (factory) { typeof exports === 'object'
  15018. ? (module.exports = factory())
  15019. : (this.when = factory());
  15020. }
  15021. // Boilerplate for AMD, Node, and browser global
  15022. );
  15023. /*global define*/
  15024. define('Core/oneTimeWarning',[
  15025. './defaultValue',
  15026. './defined',
  15027. './DeveloperError'
  15028. ], function(
  15029. defaultValue,
  15030. defined,
  15031. DeveloperError) {
  15032. "use strict";
  15033. var warnings = {};
  15034. /**
  15035. * Logs a one time message to the console. Use this function instead of
  15036. * <code>console.log</code> directly since this does not log duplicate messages
  15037. * unless it is called from multiple workers.
  15038. *
  15039. * @exports oneTimeWarning
  15040. *
  15041. * @param {String} identifier The unique identifier for this warning.
  15042. * @param {String} [message=identifier] The message to log to the console.
  15043. *
  15044. * @example
  15045. * for(var i=0;i<foo.length;++i) {
  15046. * if (!defined(foo[i].bar)) {
  15047. * // Something that can be recovered from but may happen a lot
  15048. * oneTimeWarning('foo.bar undefined', 'foo.bar is undefined. Setting to 0.');
  15049. * foo[i].bar = 0;
  15050. * // ...
  15051. * }
  15052. * }
  15053. *
  15054. * @private
  15055. */
  15056. function oneTimeWarning(identifier, message) {
  15057. if (!defined(identifier)) {
  15058. throw new DeveloperError('identifier is required.');
  15059. }
  15060. if (!defined(warnings[identifier])) {
  15061. warnings[identifier] = true;
  15062. console.log(defaultValue(message, identifier));
  15063. }
  15064. }
  15065. 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.';
  15066. return oneTimeWarning;
  15067. });
  15068. /*global define*/
  15069. define('Core/deprecationWarning',[
  15070. './defined',
  15071. './DeveloperError',
  15072. './oneTimeWarning'
  15073. ], function(
  15074. defined,
  15075. DeveloperError,
  15076. oneTimeWarning) {
  15077. 'use strict';
  15078. /**
  15079. * Logs a deprecation message to the console. Use this function instead of
  15080. * <code>console.log</code> directly since this does not log duplicate messages
  15081. * unless it is called from multiple workers.
  15082. *
  15083. * @exports deprecationWarning
  15084. *
  15085. * @param {String} identifier The unique identifier for this deprecated API.
  15086. * @param {String} message The message to log to the console.
  15087. *
  15088. * @example
  15089. * // Deprecated function or class
  15090. * function Foo() {
  15091. * deprecationWarning('Foo', 'Foo was deprecated in Cesium 1.01. It will be removed in 1.03. Use newFoo instead.');
  15092. * // ...
  15093. * }
  15094. *
  15095. * // Deprecated function
  15096. * Bar.prototype.func = function() {
  15097. * deprecationWarning('Bar.func', 'Bar.func() was deprecated in Cesium 1.01. It will be removed in 1.03. Use Bar.newFunc() instead.');
  15098. * // ...
  15099. * };
  15100. *
  15101. * // Deprecated property
  15102. * defineProperties(Bar.prototype, {
  15103. * prop : {
  15104. * get : function() {
  15105. * deprecationWarning('Bar.prop', 'Bar.prop was deprecated in Cesium 1.01. It will be removed in 1.03. Use Bar.newProp instead.');
  15106. * // ...
  15107. * },
  15108. * set : function(value) {
  15109. * deprecationWarning('Bar.prop', 'Bar.prop was deprecated in Cesium 1.01. It will be removed in 1.03. Use Bar.newProp instead.');
  15110. * // ...
  15111. * }
  15112. * }
  15113. * });
  15114. *
  15115. * @private
  15116. */
  15117. function deprecationWarning(identifier, message) {
  15118. if (!defined(identifier) || !defined(message)) {
  15119. throw new DeveloperError('identifier and message are required.');
  15120. }
  15121. oneTimeWarning(identifier, message);
  15122. }
  15123. return deprecationWarning;
  15124. });
  15125. /*global define*/
  15126. define('Core/binarySearch',[
  15127. './defined',
  15128. './DeveloperError'
  15129. ], function(
  15130. defined,
  15131. DeveloperError) {
  15132. 'use strict';
  15133. /**
  15134. * Finds an item in a sorted array.
  15135. *
  15136. * @exports binarySearch
  15137. *
  15138. * @param {Array} array The sorted array to search.
  15139. * @param {Object} itemToFind The item to find in the array.
  15140. * @param {binarySearch~Comparator} comparator The function to use to compare the item to
  15141. * elements in the array.
  15142. * @returns {Number} The index of <code>itemToFind</code> in the array, if it exists. If <code>itemToFind</code>
  15143. * does not exist, the return value is a negative number which is the bitwise complement (~)
  15144. * of the index before which the itemToFind should be inserted in order to maintain the
  15145. * sorted order of the array.
  15146. *
  15147. * @example
  15148. * // Create a comparator function to search through an array of numbers.
  15149. * function comparator(a, b) {
  15150. * return a - b;
  15151. * };
  15152. * var numbers = [0, 2, 4, 6, 8];
  15153. * var index = Cesium.binarySearch(numbers, 6, comparator); // 3
  15154. */
  15155. function binarySearch(array, itemToFind, comparator) {
  15156. if (!defined(array)) {
  15157. throw new DeveloperError('array is required.');
  15158. }
  15159. if (!defined(itemToFind)) {
  15160. throw new DeveloperError('itemToFind is required.');
  15161. }
  15162. if (!defined(comparator)) {
  15163. throw new DeveloperError('comparator is required.');
  15164. }
  15165. var low = 0;
  15166. var high = array.length - 1;
  15167. var i;
  15168. var comparison;
  15169. while (low <= high) {
  15170. i = ~~((low + high) / 2);
  15171. comparison = comparator(array[i], itemToFind);
  15172. if (comparison < 0) {
  15173. low = i + 1;
  15174. continue;
  15175. }
  15176. if (comparison > 0) {
  15177. high = i - 1;
  15178. continue;
  15179. }
  15180. return i;
  15181. }
  15182. return ~(high + 1);
  15183. }
  15184. /**
  15185. * A function used to compare two items while performing a binary search.
  15186. * @callback binarySearch~Comparator
  15187. *
  15188. * @param {Object} a An item in the array.
  15189. * @param {Object} b The item being searched for.
  15190. * @returns {Number} Returns a negative value if <code>a</code> is less than <code>b</code>,
  15191. * a positive value if <code>a</code> is greater than <code>b</code>, or
  15192. * 0 if <code>a</code> is equal to <code>b</code>.
  15193. *
  15194. * @example
  15195. * function compareNumbers(a, b) {
  15196. * return a - b;
  15197. * }
  15198. */
  15199. return binarySearch;
  15200. });
  15201. /*global define*/
  15202. define('Core/EarthOrientationParametersSample',[],function() {
  15203. 'use strict';
  15204. /**
  15205. * A set of Earth Orientation Parameters (EOP) sampled at a time.
  15206. *
  15207. * @alias EarthOrientationParametersSample
  15208. * @constructor
  15209. *
  15210. * @param {Number} xPoleWander The pole wander about the X axis, in radians.
  15211. * @param {Number} yPoleWander The pole wander about the Y axis, in radians.
  15212. * @param {Number} xPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  15213. * @param {Number} yPoleOffset The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  15214. * @param {Number} ut1MinusUtc The difference in time standards, UT1 - UTC, in seconds.
  15215. *
  15216. * @private
  15217. */
  15218. function EarthOrientationParametersSample(xPoleWander, yPoleWander, xPoleOffset, yPoleOffset, ut1MinusUtc) {
  15219. /**
  15220. * The pole wander about the X axis, in radians.
  15221. * @type {Number}
  15222. */
  15223. this.xPoleWander = xPoleWander;
  15224. /**
  15225. * The pole wander about the Y axis, in radians.
  15226. * @type {Number}
  15227. */
  15228. this.yPoleWander = yPoleWander;
  15229. /**
  15230. * The offset to the Celestial Intermediate Pole (CIP) about the X axis, in radians.
  15231. * @type {Number}
  15232. */
  15233. this.xPoleOffset = xPoleOffset;
  15234. /**
  15235. * The offset to the Celestial Intermediate Pole (CIP) about the Y axis, in radians.
  15236. * @type {Number}
  15237. */
  15238. this.yPoleOffset = yPoleOffset;
  15239. /**
  15240. * The difference in time standards, UT1 - UTC, in seconds.
  15241. * @type {Number}
  15242. */
  15243. this.ut1MinusUtc = ut1MinusUtc;
  15244. }
  15245. return EarthOrientationParametersSample;
  15246. });
  15247. /**
  15248. @license
  15249. sprintf.js from the php.js project - https://github.com/kvz/phpjs
  15250. Directly from https://github.com/kvz/phpjs/blob/master/functions/strings/sprintf.js
  15251. php.js is copyright 2012 Kevin van Zonneveld.
  15252. Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
  15253. (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
  15254. (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jack, Jonas
  15255. Raoni Soares Silva (http://www.jsfromhell.com), Philip Peterson, Legaev
  15256. Andrey, Ates Goral (http://magnetiq.com), Alex, Ratheous, Martijn Wieringa,
  15257. Rafa? Kukawski (http://blog.kukawski.pl), lmeyrick
  15258. (https://sourceforge.net/projects/bcmath-js/), Nate, Philippe Baumann,
  15259. Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/), Carlos R.
  15260. L. Rodrigues (http://www.jsfromhell.com), Ash Searle
  15261. (http://hexmen.com/blog/), Jani Hartikainen, travc, Ole Vrijenhoek,
  15262. Erkekjetter, Michael Grier, Rafa? Kukawski (http://kukawski.pl), Johnny
  15263. Mast (http://www.phpvrouwen.nl), T.Wild, d3x,
  15264. http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
  15265. Rafa? Kukawski (http://blog.kukawski.pl/), stag019, pilus, WebDevHobo
  15266. (http://webdevhobo.blogspot.com/), marrtins, GeekFG
  15267. (http://geekfg.blogspot.com), Andrea Giammarchi
  15268. (http://webreflection.blogspot.com), Arpad Ray (mailto:arpad@php.net),
  15269. gorthaur, Paul Smith, Tim de Koning (http://www.kingsquare.nl), Joris, Oleg
  15270. Eremeev, Steve Hilder, majak, gettimeofday, KELAN, Josh Fraser
  15271. (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
  15272. Marc Palau, Martin
  15273. (http://www.erlenwiese.de/), Breaking Par Consulting Inc
  15274. (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
  15275. Chris, Mirek Slugen, saulius, Alfonso Jimenez
  15276. (http://www.alfonsojimenez.com), Diplom@t (http://difane.com/), felix,
  15277. Mailfaker (http://www.weedem.fr/), Tyler Akins (http://rumkin.com), Caio
  15278. Ariede (http://caioariede.com), Robin, Kankrelune
  15279. (http://www.webfaktory.info/), Karol Kowalski, Imgen Tata
  15280. (http://www.myipdf.com/), mdsjack (http://www.mdsjack.bo.it), Dreamer,
  15281. Felix Geisendoerfer (http://www.debuggable.com/felix), Lars Fischer, AJ,
  15282. David, Aman Gupta, Michael White, Public Domain
  15283. (http://www.json.org/json2.js), Steven Levithan
  15284. (http://blog.stevenlevithan.com), Sakimori, Pellentesque Malesuada,
  15285. Thunder.m, Dj (http://phpjs.org/functions/htmlentities:425#comment_134018),
  15286. Steve Clay, David James, Francois, class_exists, nobbler, T. Wild, Itsacon
  15287. (http://www.itsacon.net/), date, Ole Vrijenhoek (http://www.nervous.nl/),
  15288. Fox, Raphael (Ao RUDLER), Marco, noname, Mateusz "loonquawl" Zalega, Frank
  15289. Forte, Arno, ger, mktime, john (http://www.jd-tech.net), Nick Kolosov
  15290. (http://sammy.ru), marc andreu, Scott Cariss, Douglas Crockford
  15291. (http://javascript.crockford.com), madipta, Slawomir Kaniecki,
  15292. ReverseSyntax, Nathan, Alex Wilson, kenneth, Bayron Guevara, Adam Wallner
  15293. (http://web2.bitbaro.hu/), paulo kuong, jmweb, Lincoln Ramsay, djmix,
  15294. Pyerre, Jon Hohle, Thiago Mata (http://thiagomata.blog.com), lmeyrick
  15295. (https://sourceforge.net/projects/bcmath-js/this.), Linuxworld, duncan,
  15296. Gilbert, Sanjoy Roy, Shingo, sankai, Oskar Larsson H?gfeldt
  15297. (http://oskar-lh.name/), Denny Wardhana, 0m3r, Everlasto, Subhasis Deb,
  15298. josh, jd, Pier Paolo Ramon (http://www.mastersoup.com/), P, merabi, Soren
  15299. Hansen, Eugene Bulkin (http://doubleaw.com/), Der Simon
  15300. (http://innerdom.sourceforge.net/), echo is bad, Ozh, XoraX
  15301. (http://www.xorax.info), EdorFaus, JB, J A R, Marc Jansen, Francesco, LH,
  15302. Stoyan Kyosev (http://www.svest.org/), nord_ua, omid
  15303. (http://phpjs.org/functions/380:380#comment_137122), Brad Touesnard, MeEtc
  15304. (http://yass.meetcweb.com), Peter-Paul Koch
  15305. (http://www.quirksmode.org/js/beat.html), Olivier Louvignes
  15306. (http://mg-crea.com/), T0bsn, Tim Wiel, Bryan Elliott, Jalal Berrami,
  15307. Martin, JT, David Randall, Thomas Beaucourt (http://www.webapp.fr), taith,
  15308. vlado houba, Pierre-Luc Paour, Kristof Coomans (SCK-CEN Belgian Nucleair
  15309. Research Centre), Martin Pool, Kirk Strobeck, Rick Waldron, Brant Messenger
  15310. (http://www.brantmessenger.com/), Devan Penner-Woelk, Saulo Vallory, Wagner
  15311. B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
  15312. (http://carrot.org/), Christoph, Daniel Esteban, strftime, Mick@el, rezna,
  15313. Simon Willison (http://simonwillison.net), Anton Ongson, Gabriel Paderni,
  15314. Marco van Oort, penutbutterjelly, Philipp Lenssen, Bjorn Roesbeke
  15315. (http://www.bjornroesbeke.be/), Bug?, Eric Nagel, Tomasz Wesolowski,
  15316. Evertjan Garretsen, Bobby Drake, Blues (http://tech.bluesmoon.info/), Luke
  15317. Godfrey, Pul, uestla, Alan C, Ulrich, Rafal Kukawski, Yves Sucaet,
  15318. sowberry, Norman "zEh" Fuchs, hitwork, Zahlii, johnrembo, Nick Callen,
  15319. Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Brian Tafoya
  15320. (http://www.premasolutions.com/), Philippe Jausions
  15321. (http://pear.php.net/user/jausions), Aidan Lister
  15322. (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
  15323. strcmp, Taras Bogach, jpfle, Alexander Ermolaev
  15324. (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
  15325. dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
  15326. (http://www.pedrotainha.com), James, Arnout Kazemier
  15327. (http://www.3rd-Eden.com), Chris McMacken, gabriel paderni, Yannoo,
  15328. FGFEmperor, baris ozdil, Tod Gentille, Greg Frazier, jakes, 3D-GRAF, Allan
  15329. Jensen (http://www.winternet.no), Howard Yeend, Benjamin Lupton, davook,
  15330. daniel airton wermann (http://wermann.com.br), Atli T¨®r, Maximusya, Ryan
  15331. W Tenney (http://ryan.10e.us), Alexander M Beedie, fearphage
  15332. (http://http/my.opera.com/fearphage/), Nathan Sepulveda, Victor, Matteo,
  15333. Billy, stensi, Cord, Manish, T.J. Leahy, Riddler
  15334. (http://www.frontierwebdev.com/), Rafa? Kukawski, FremyCompany, Matt
  15335. Bradley, Tim de Koning, Luis Salazar (http://www.freaky-media.com/), Diogo
  15336. Resende, Rival, Andrej Pavlovic, Garagoth, Le Torbi
  15337. (http://www.letorbi.de/), Dino, Josep Sanz (http://www.ws3.es/), rem,
  15338. Russell Walker (http://www.nbill.co.uk/), Jamie Beck
  15339. (http://www.terabit.ca/), setcookie, Michael, YUI Library:
  15340. http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
  15341. http://hacks.bluesmoon.info/strftime/strftime.js, Ben
  15342. (http://benblume.co.uk/), DtTvB
  15343. (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Andreas, William,
  15344. meo, incidence, Cagri Ekin, Amirouche, Amir Habibi
  15345. (http://www.residence-mixte.com/), Luke Smith (http://lucassmith.name),
  15346. Kheang Hok Chin (http://www.distantia.ca/), Jay Klehr, Lorenzo Pisani,
  15347. Tony, Yen-Wei Liu, Greenseed, mk.keck, Leslie Hoare, dude, booeyOH, Ben
  15348. Bryan
  15349. Licensed under the MIT (MIT-LICENSE.txt) license.
  15350. Permission is hereby granted, free of charge, to any person obtaining a
  15351. copy of this software and associated documentation files (the
  15352. "Software"), to deal in the Software without restriction, including
  15353. without limitation the rights to use, copy, modify, merge, publish,
  15354. distribute, sublicense, and/or sell copies of the Software, and to
  15355. permit persons to whom the Software is furnished to do so, subject to
  15356. the following conditions:
  15357. The above copyright notice and this permission notice shall be included
  15358. in all copies or substantial portions of the Software.
  15359. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15360. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15361. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15362. IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
  15363. OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15364. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  15365. OTHER DEALINGS IN THE SOFTWARE.
  15366. */
  15367. /*global define*/
  15368. define('ThirdParty/sprintf',[],function() {
  15369. function sprintf () {
  15370. // http://kevin.vanzonneveld.net
  15371. // + original by: Ash Searle (http://hexmen.com/blog/)
  15372. // + namespaced by: Michael White (http://getsprink.com)
  15373. // + tweaked by: Jack
  15374. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  15375. // + input by: Paulo Freitas
  15376. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  15377. // + input by: Brett Zamir (http://brett-zamir.me)
  15378. // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  15379. // + improved by: Dj
  15380. // + improved by: Allidylls
  15381. // * example 1: sprintf("%01.2f", 123.1);
  15382. // * returns 1: 123.10
  15383. // * example 2: sprintf("[%10s]", 'monkey');
  15384. // * returns 2: '[ monkey]'
  15385. // * example 3: sprintf("[%'#10s]", 'monkey');
  15386. // * returns 3: '[####monkey]'
  15387. // * example 4: sprintf("%d", 123456789012345);
  15388. // * returns 4: '123456789012345'
  15389. var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuideEfFgG])/g;
  15390. var a = arguments,
  15391. i = 0,
  15392. format = a[i++];
  15393. // pad()
  15394. var pad = function (str, len, chr, leftJustify) {
  15395. if (!chr) {
  15396. chr = ' ';
  15397. }
  15398. var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
  15399. return leftJustify ? str + padding : padding + str;
  15400. };
  15401. // justify()
  15402. var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
  15403. var diff = minWidth - value.length;
  15404. if (diff > 0) {
  15405. if (leftJustify || !zeroPad) {
  15406. value = pad(value, minWidth, customPadChar, leftJustify);
  15407. } else {
  15408. value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
  15409. }
  15410. }
  15411. return value;
  15412. };
  15413. // formatBaseX()
  15414. var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
  15415. // Note: casts negative numbers to positive ones
  15416. var number = value >>> 0;
  15417. prefix = prefix && number && {
  15418. '2': '0b',
  15419. '8': '0',
  15420. '16': '0x'
  15421. }[base] || '';
  15422. value = prefix + pad(number.toString(base), precision || 0, '0', false);
  15423. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  15424. };
  15425. // formatString()
  15426. var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
  15427. if (precision != null) {
  15428. value = value.slice(0, precision);
  15429. }
  15430. return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
  15431. };
  15432. // doFormat()
  15433. var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
  15434. var number;
  15435. var prefix;
  15436. var method;
  15437. var textTransform;
  15438. var value;
  15439. if (substring == '%%') {
  15440. return '%';
  15441. }
  15442. // parse flags
  15443. var leftJustify = false,
  15444. positivePrefix = '',
  15445. zeroPad = false,
  15446. prefixBaseX = false,
  15447. customPadChar = ' ';
  15448. var flagsl = flags.length;
  15449. for (var j = 0; flags && j < flagsl; j++) {
  15450. switch (flags.charAt(j)) {
  15451. case ' ':
  15452. positivePrefix = ' ';
  15453. break;
  15454. case '+':
  15455. positivePrefix = '+';
  15456. break;
  15457. case '-':
  15458. leftJustify = true;
  15459. break;
  15460. case "'":
  15461. customPadChar = flags.charAt(j + 1);
  15462. break;
  15463. case '0':
  15464. zeroPad = true;
  15465. break;
  15466. case '#':
  15467. prefixBaseX = true;
  15468. break;
  15469. }
  15470. }
  15471. // parameters may be null, undefined, empty-string or real valued
  15472. // we want to ignore null, undefined and empty-string values
  15473. if (!minWidth) {
  15474. minWidth = 0;
  15475. } else if (minWidth == '*') {
  15476. minWidth = +a[i++];
  15477. } else if (minWidth.charAt(0) == '*') {
  15478. minWidth = +a[minWidth.slice(1, -1)];
  15479. } else {
  15480. minWidth = +minWidth;
  15481. }
  15482. // Note: undocumented perl feature:
  15483. if (minWidth < 0) {
  15484. minWidth = -minWidth;
  15485. leftJustify = true;
  15486. }
  15487. if (!isFinite(minWidth)) {
  15488. throw new Error('sprintf: (minimum-)width must be finite');
  15489. }
  15490. if (!precision) {
  15491. precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
  15492. } else if (precision == '*') {
  15493. precision = +a[i++];
  15494. } else if (precision.charAt(0) == '*') {
  15495. precision = +a[precision.slice(1, -1)];
  15496. } else {
  15497. precision = +precision;
  15498. }
  15499. // grab value using valueIndex if required?
  15500. value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
  15501. switch (type) {
  15502. case 's':
  15503. return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
  15504. case 'c':
  15505. return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
  15506. case 'b':
  15507. return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15508. case 'o':
  15509. return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15510. case 'x':
  15511. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15512. case 'X':
  15513. return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
  15514. case 'u':
  15515. return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
  15516. case 'i':
  15517. case 'd':
  15518. number = +value || 0;
  15519. number = Math.round(number - number % 1); // Plain Math.round doesn't just truncate
  15520. prefix = number < 0 ? '-' : positivePrefix;
  15521. value = prefix + pad(String(Math.abs(number)), precision, '0', false);
  15522. return justify(value, prefix, leftJustify, minWidth, zeroPad);
  15523. case 'e':
  15524. case 'E':
  15525. case 'f': // Should handle locales (as per setlocale)
  15526. case 'F':
  15527. case 'g':
  15528. case 'G':
  15529. number = +value;
  15530. prefix = number < 0 ? '-' : positivePrefix;
  15531. method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
  15532. textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
  15533. value = prefix + Math.abs(number)[method](precision);
  15534. return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
  15535. default:
  15536. return substring;
  15537. }
  15538. };
  15539. return format.replace(regex, doFormat);
  15540. }
  15541. return sprintf;
  15542. });
  15543. /*global define*/
  15544. define('Core/GregorianDate',[],function() {
  15545. 'use strict';
  15546. /**
  15547. * Represents a Gregorian date in a more precise format than the JavaScript Date object.
  15548. * In addition to submillisecond precision, this object can also represent leap seconds.
  15549. * @alias GregorianDate
  15550. * @constructor
  15551. *
  15552. * @see JulianDate#toGregorianDate
  15553. */
  15554. function GregorianDate(year, month, day, hour, minute, second, millisecond, isLeapSecond) {
  15555. /**
  15556. * Gets or sets the year as a whole number.
  15557. * @type {Number}
  15558. */
  15559. this.year = year;
  15560. /**
  15561. * Gets or sets the month as a whole number with range [1, 12].
  15562. * @type {Number}
  15563. */
  15564. this.month = month;
  15565. /**
  15566. * Gets or sets the day of the month as a whole number starting at 1.
  15567. * @type {Number}
  15568. */
  15569. this.day = day;
  15570. /**
  15571. * Gets or sets the hour as a whole number with range [0, 23].
  15572. * @type {Number}
  15573. */
  15574. this.hour = hour;
  15575. /**
  15576. * Gets or sets the minute of the hour as a whole number with range [0, 59].
  15577. * @type {Number}
  15578. */
  15579. this.minute = minute;
  15580. /**
  15581. * Gets or sets the second of the minute as a whole number with range [0, 60], with 60 representing a leap second.
  15582. * @type {Number}
  15583. */
  15584. this.second = second;
  15585. /**
  15586. * Gets or sets the millisecond of the second as a floating point number with range [0.0, 1000.0).
  15587. * @type {Number}
  15588. */
  15589. this.millisecond = millisecond;
  15590. /**
  15591. * Gets or sets whether this time is during a leap second.
  15592. * @type {Boolean}
  15593. */
  15594. this.isLeapSecond = isLeapSecond;
  15595. }
  15596. return GregorianDate;
  15597. });
  15598. /*global define*/
  15599. define('Core/isLeapYear',[
  15600. './DeveloperError'
  15601. ], function(
  15602. DeveloperError) {
  15603. 'use strict';
  15604. /**
  15605. * Determines if a given date is a leap year.
  15606. *
  15607. * @exports isLeapYear
  15608. *
  15609. * @param {Number} year The year to be tested.
  15610. * @returns {Boolean} True if <code>year</code> is a leap year.
  15611. *
  15612. * @example
  15613. * var leapYear = Cesium.isLeapYear(2000); // true
  15614. */
  15615. function isLeapYear(year) {
  15616. if (year === null || isNaN(year)) {
  15617. throw new DeveloperError('year is required and must be a number.');
  15618. }
  15619. return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
  15620. }
  15621. return isLeapYear;
  15622. });
  15623. /*global define*/
  15624. define('Core/LeapSecond',[],function() {
  15625. 'use strict';
  15626. /**
  15627. * Describes a single leap second, which is constructed from a {@link JulianDate} and a
  15628. * numerical offset representing the number of seconds TAI is ahead of the UTC time standard.
  15629. * @alias LeapSecond
  15630. * @constructor
  15631. *
  15632. * @param {JulianDate} [date] A Julian date representing the time of the leap second.
  15633. * @param {Number} [offset] The cumulative number of seconds that TAI is ahead of UTC at the provided date.
  15634. */
  15635. function LeapSecond(date, offset) {
  15636. /**
  15637. * Gets or sets the date at which this leap second occurs.
  15638. * @type {JulianDate}
  15639. */
  15640. this.julianDate = date;
  15641. /**
  15642. * Gets or sets the cumulative number of seconds between the UTC and TAI time standards at the time
  15643. * of this leap second.
  15644. * @type {Number}
  15645. */
  15646. this.offset = offset;
  15647. }
  15648. return LeapSecond;
  15649. });
  15650. /*global define*/
  15651. define('Core/TimeConstants',[
  15652. './freezeObject'
  15653. ], function(
  15654. freezeObject) {
  15655. 'use strict';
  15656. /**
  15657. * Constants for time conversions like those done by {@link JulianDate}.
  15658. *
  15659. * @exports TimeConstants
  15660. *
  15661. * @see JulianDate
  15662. *
  15663. * @private
  15664. */
  15665. var TimeConstants = {
  15666. /**
  15667. * The number of seconds in one millisecond: <code>0.001</code>
  15668. * @type {Number}
  15669. * @constant
  15670. */
  15671. SECONDS_PER_MILLISECOND : 0.001,
  15672. /**
  15673. * The number of seconds in one minute: <code>60</code>.
  15674. * @type {Number}
  15675. * @constant
  15676. */
  15677. SECONDS_PER_MINUTE : 60.0,
  15678. /**
  15679. * The number of minutes in one hour: <code>60</code>.
  15680. * @type {Number}
  15681. * @constant
  15682. */
  15683. MINUTES_PER_HOUR : 60.0,
  15684. /**
  15685. * The number of hours in one day: <code>24</code>.
  15686. * @type {Number}
  15687. * @constant
  15688. */
  15689. HOURS_PER_DAY : 24.0,
  15690. /**
  15691. * The number of seconds in one hour: <code>3600</code>.
  15692. * @type {Number}
  15693. * @constant
  15694. */
  15695. SECONDS_PER_HOUR : 3600.0,
  15696. /**
  15697. * The number of minutes in one day: <code>1440</code>.
  15698. * @type {Number}
  15699. * @constant
  15700. */
  15701. MINUTES_PER_DAY : 1440.0,
  15702. /**
  15703. * The number of seconds in one day, ignoring leap seconds: <code>86400</code>.
  15704. * @type {Number}
  15705. * @constant
  15706. */
  15707. SECONDS_PER_DAY : 86400.0,
  15708. /**
  15709. * The number of days in one Julian century: <code>36525</code>.
  15710. * @type {Number}
  15711. * @constant
  15712. */
  15713. DAYS_PER_JULIAN_CENTURY : 36525.0,
  15714. /**
  15715. * One trillionth of a second.
  15716. * @type {Number}
  15717. * @constant
  15718. */
  15719. PICOSECOND : 0.000000001,
  15720. /**
  15721. * The number of days to subtract from a Julian date to determine the
  15722. * modified Julian date, which gives the number of days since midnight
  15723. * on November 17, 1858.
  15724. * @type {Number}
  15725. * @constant
  15726. */
  15727. MODIFIED_JULIAN_DATE_DIFFERENCE : 2400000.5
  15728. };
  15729. return freezeObject(TimeConstants);
  15730. });
  15731. /*global define*/
  15732. define('Core/TimeStandard',[
  15733. './freezeObject'
  15734. ], function(
  15735. freezeObject) {
  15736. 'use strict';
  15737. /**
  15738. * Provides the type of time standards which JulianDate can take as input.
  15739. *
  15740. * @exports TimeStandard
  15741. *
  15742. * @see JulianDate
  15743. */
  15744. var TimeStandard = {
  15745. /**
  15746. * Represents the coordinated Universal Time (UTC) time standard.
  15747. *
  15748. * UTC is related to TAI according to the relationship
  15749. * <code>UTC = TAI - deltaT</code> where <code>deltaT</code> is the number of leap
  15750. * seconds which have been introduced as of the time in TAI.
  15751. *
  15752. */
  15753. UTC : 0,
  15754. /**
  15755. * Represents the International Atomic Time (TAI) time standard.
  15756. * TAI is the principal time standard to which the other time standards are related.
  15757. */
  15758. TAI : 1
  15759. };
  15760. return freezeObject(TimeStandard);
  15761. });
  15762. /*global define*/
  15763. define('Core/JulianDate',[
  15764. '../ThirdParty/sprintf',
  15765. './binarySearch',
  15766. './defaultValue',
  15767. './defined',
  15768. './DeveloperError',
  15769. './GregorianDate',
  15770. './isLeapYear',
  15771. './LeapSecond',
  15772. './TimeConstants',
  15773. './TimeStandard'
  15774. ], function(
  15775. sprintf,
  15776. binarySearch,
  15777. defaultValue,
  15778. defined,
  15779. DeveloperError,
  15780. GregorianDate,
  15781. isLeapYear,
  15782. LeapSecond,
  15783. TimeConstants,
  15784. TimeStandard) {
  15785. 'use strict';
  15786. var gregorianDateScratch = new GregorianDate();
  15787. var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  15788. var daysInLeapFeburary = 29;
  15789. function compareLeapSecondDates(leapSecond, dateToFind) {
  15790. return JulianDate.compare(leapSecond.julianDate, dateToFind.julianDate);
  15791. }
  15792. // we don't really need a leap second instance, anything with a julianDate property will do
  15793. var binarySearchScratchLeapSecond = new LeapSecond();
  15794. function convertUtcToTai(julianDate) {
  15795. //Even though julianDate is in UTC, we'll treat it as TAI and
  15796. //search the leap second table for it.
  15797. binarySearchScratchLeapSecond.julianDate = julianDate;
  15798. var leapSeconds = JulianDate.leapSeconds;
  15799. var index = binarySearch(leapSeconds, binarySearchScratchLeapSecond, compareLeapSecondDates);
  15800. if (index < 0) {
  15801. index = ~index;
  15802. }
  15803. if (index >= leapSeconds.length) {
  15804. index = leapSeconds.length - 1;
  15805. }
  15806. var offset = leapSeconds[index].offset;
  15807. if (index > 0) {
  15808. //Now we have the index of the closest leap second that comes on or after our UTC time.
  15809. //However, if the difference between the UTC date being converted and the TAI
  15810. //defined leap second is greater than the offset, we are off by one and need to use
  15811. //the previous leap second.
  15812. var difference = JulianDate.secondsDifference(leapSeconds[index].julianDate, julianDate);
  15813. if (difference > offset) {
  15814. index--;
  15815. offset = leapSeconds[index].offset;
  15816. }
  15817. }
  15818. JulianDate.addSeconds(julianDate, offset, julianDate);
  15819. }
  15820. function convertTaiToUtc(julianDate, result) {
  15821. binarySearchScratchLeapSecond.julianDate = julianDate;
  15822. var leapSeconds = JulianDate.leapSeconds;
  15823. var index = binarySearch(leapSeconds, binarySearchScratchLeapSecond, compareLeapSecondDates);
  15824. if (index < 0) {
  15825. index = ~index;
  15826. }
  15827. //All times before our first leap second get the first offset.
  15828. if (index === 0) {
  15829. return JulianDate.addSeconds(julianDate, -leapSeconds[0].offset, result);
  15830. }
  15831. //All times after our leap second get the last offset.
  15832. if (index >= leapSeconds.length) {
  15833. return JulianDate.addSeconds(julianDate, -leapSeconds[index - 1].offset, result);
  15834. }
  15835. //Compute the difference between the found leap second and the time we are converting.
  15836. var difference = JulianDate.secondsDifference(leapSeconds[index].julianDate, julianDate);
  15837. if (difference === 0) {
  15838. //The date is in our leap second table.
  15839. return JulianDate.addSeconds(julianDate, -leapSeconds[index].offset, result);
  15840. }
  15841. if (difference <= 1.0) {
  15842. //The requested date is during the moment of a leap second, then we cannot convert to UTC
  15843. return undefined;
  15844. }
  15845. //The time is in between two leap seconds, index is the leap second after the date
  15846. //we're converting, so we subtract one to get the correct LeapSecond instance.
  15847. return JulianDate.addSeconds(julianDate, -leapSeconds[--index].offset, result);
  15848. }
  15849. function setComponents(wholeDays, secondsOfDay, julianDate) {
  15850. var extraDays = (secondsOfDay / TimeConstants.SECONDS_PER_DAY) | 0;
  15851. wholeDays += extraDays;
  15852. secondsOfDay -= TimeConstants.SECONDS_PER_DAY * extraDays;
  15853. if (secondsOfDay < 0) {
  15854. wholeDays--;
  15855. secondsOfDay += TimeConstants.SECONDS_PER_DAY;
  15856. }
  15857. julianDate.dayNumber = wholeDays;
  15858. julianDate.secondsOfDay = secondsOfDay;
  15859. return julianDate;
  15860. }
  15861. function computeJulianDateComponents(year, month, day, hour, minute, second, millisecond) {
  15862. // Algorithm from page 604 of the Explanatory Supplement to the
  15863. // Astronomical Almanac (Seidelmann 1992).
  15864. var a = ((month - 14) / 12) | 0;
  15865. var b = year + 4800 + a;
  15866. var dayNumber = (((1461 * b) / 4) | 0) + (((367 * (month - 2 - 12 * a)) / 12) | 0) - (((3 * (((b + 100) / 100) | 0)) / 4) | 0) + day - 32075;
  15867. // JulianDates are noon-based
  15868. hour = hour - 12;
  15869. if (hour < 0) {
  15870. hour += 24;
  15871. }
  15872. var secondsOfDay = second + ((hour * TimeConstants.SECONDS_PER_HOUR) + (minute * TimeConstants.SECONDS_PER_MINUTE) + (millisecond * TimeConstants.SECONDS_PER_MILLISECOND));
  15873. if (secondsOfDay >= 43200.0) {
  15874. dayNumber -= 1;
  15875. }
  15876. return [dayNumber, secondsOfDay];
  15877. }
  15878. //Regular expressions used for ISO8601 date parsing.
  15879. //YYYY
  15880. var matchCalendarYear = /^(\d{4})$/;
  15881. //YYYY-MM (YYYYMM is invalid)
  15882. var matchCalendarMonth = /^(\d{4})-(\d{2})$/;
  15883. //YYYY-DDD or YYYYDDD
  15884. var matchOrdinalDate = /^(\d{4})-?(\d{3})$/;
  15885. //YYYY-Www or YYYYWww or YYYY-Www-D or YYYYWwwD
  15886. var matchWeekDate = /^(\d{4})-?W(\d{2})-?(\d{1})?$/;
  15887. //YYYY-MM-DD or YYYYMMDD
  15888. var matchCalendarDate = /^(\d{4})-?(\d{2})-?(\d{2})$/;
  15889. // Match utc offset
  15890. var utcOffset = /([Z+\-])?(\d{2})?:?(\d{2})?$/;
  15891. // Match hours HH or HH.xxxxx
  15892. var matchHours = /^(\d{2})(\.\d+)?/.source + utcOffset.source;
  15893. // Match hours/minutes HH:MM HHMM.xxxxx
  15894. var matchHoursMinutes = /^(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  15895. // Match hours/minutes HH:MM:SS HHMMSS.xxxxx
  15896. var matchHoursMinutesSeconds = /^(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?/.source + utcOffset.source;
  15897. var iso8601ErrorMessage = 'Invalid ISO 8601 date.';
  15898. /**
  15899. * Represents an astronomical Julian date, which is the number of days since noon on January 1, -4712 (4713 BC).
  15900. * For increased precision, this class stores the whole number part of the date and the seconds
  15901. * part of the date in separate components. In order to be safe for arithmetic and represent
  15902. * leap seconds, the date is always stored in the International Atomic Time standard
  15903. * {@link TimeStandard.TAI}.
  15904. * @alias JulianDate
  15905. * @constructor
  15906. *
  15907. * @param {Number} [julianDayNumber=0.0] The Julian Day Number representing the number of whole days. Fractional days will also be handled correctly.
  15908. * @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.
  15909. * @param {TimeStandard} [timeStandard=TimeStandard.UTC] The time standard in which the first two parameters are defined.
  15910. */
  15911. function JulianDate(julianDayNumber, secondsOfDay, timeStandard) {
  15912. /**
  15913. * Gets or sets the number of whole days.
  15914. * @type {Number}
  15915. */
  15916. this.dayNumber = undefined;
  15917. /**
  15918. * Gets or sets the number of seconds into the current day.
  15919. * @type {Number}
  15920. */
  15921. this.secondsOfDay = undefined;
  15922. julianDayNumber = defaultValue(julianDayNumber, 0.0);
  15923. secondsOfDay = defaultValue(secondsOfDay, 0.0);
  15924. timeStandard = defaultValue(timeStandard, TimeStandard.UTC);
  15925. //If julianDayNumber is fractional, make it an integer and add the number of seconds the fraction represented.
  15926. var wholeDays = julianDayNumber | 0;
  15927. secondsOfDay = secondsOfDay + (julianDayNumber - wholeDays) * TimeConstants.SECONDS_PER_DAY;
  15928. setComponents(wholeDays, secondsOfDay, this);
  15929. if (timeStandard === TimeStandard.UTC) {
  15930. convertUtcToTai(this);
  15931. }
  15932. }
  15933. /**
  15934. * Creates a new instance from a JavaScript Date.
  15935. *
  15936. * @param {Date} date A JavaScript Date.
  15937. * @param {JulianDate} [result] An existing instance to use for the result.
  15938. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  15939. *
  15940. * @exception {DeveloperError} date must be a valid JavaScript Date.
  15941. */
  15942. JulianDate.fromDate = function(date, result) {
  15943. if (!(date instanceof Date) || isNaN(date.getTime())) {
  15944. throw new DeveloperError('date must be a valid JavaScript Date.');
  15945. }
  15946. var components = computeJulianDateComponents(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
  15947. if (!defined(result)) {
  15948. return new JulianDate(components[0], components[1], TimeStandard.UTC);
  15949. }
  15950. setComponents(components[0], components[1], result);
  15951. convertUtcToTai(result);
  15952. return result;
  15953. };
  15954. /**
  15955. * Creates a new instance from a from an {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} date.
  15956. * This method is superior to <code>Date.parse</code> because it will handle all valid formats defined by the ISO 8601
  15957. * specification, including leap seconds and sub-millisecond times, which discarded by most JavaScript implementations.
  15958. *
  15959. * @param {String} iso8601String An ISO 8601 date.
  15960. * @param {JulianDate} [result] An existing instance to use for the result.
  15961. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  15962. *
  15963. * @exception {DeveloperError} Invalid ISO 8601 date.
  15964. */
  15965. JulianDate.fromIso8601 = function(iso8601String, result) {
  15966. if (typeof iso8601String !== 'string') {
  15967. throw new DeveloperError(iso8601ErrorMessage);
  15968. }
  15969. //Comma and decimal point both indicate a fractional number according to ISO 8601,
  15970. //start out by blanket replacing , with . which is the only valid such symbol in JS.
  15971. iso8601String = iso8601String.replace(',', '.');
  15972. //Split the string into its date and time components, denoted by a mandatory T
  15973. var tokens = iso8601String.split('T');
  15974. var year;
  15975. var month = 1;
  15976. var day = 1;
  15977. var hour = 0;
  15978. var minute = 0;
  15979. var second = 0;
  15980. var millisecond = 0;
  15981. //Lacking a time is okay, but a missing date is illegal.
  15982. var date = tokens[0];
  15983. var time = tokens[1];
  15984. var tmp;
  15985. var inLeapYear;
  15986. if (!defined(date)) {
  15987. throw new DeveloperError(iso8601ErrorMessage);
  15988. }
  15989. var dashCount;
  15990. //First match the date against possible regular expressions.
  15991. tokens = date.match(matchCalendarDate);
  15992. if (tokens !== null) {
  15993. dashCount = date.split('-').length - 1;
  15994. if (dashCount > 0 && dashCount !== 2) {
  15995. throw new DeveloperError(iso8601ErrorMessage);
  15996. }
  15997. year = +tokens[1];
  15998. month = +tokens[2];
  15999. day = +tokens[3];
  16000. } else {
  16001. tokens = date.match(matchCalendarMonth);
  16002. if (tokens !== null) {
  16003. year = +tokens[1];
  16004. month = +tokens[2];
  16005. } else {
  16006. tokens = date.match(matchCalendarYear);
  16007. if (tokens !== null) {
  16008. year = +tokens[1];
  16009. } else {
  16010. //Not a year/month/day so it must be an ordinal date.
  16011. var dayOfYear;
  16012. tokens = date.match(matchOrdinalDate);
  16013. if (tokens !== null) {
  16014. year = +tokens[1];
  16015. dayOfYear = +tokens[2];
  16016. inLeapYear = isLeapYear(year);
  16017. //This validation is only applicable for this format.
  16018. if (dayOfYear < 1 || (inLeapYear && dayOfYear > 366) || (!inLeapYear && dayOfYear > 365)) {
  16019. throw new DeveloperError(iso8601ErrorMessage);
  16020. }
  16021. } else {
  16022. tokens = date.match(matchWeekDate);
  16023. if (tokens !== null) {
  16024. //ISO week date to ordinal date from
  16025. //http://en.wikipedia.org/w/index.php?title=ISO_week_date&oldid=474176775
  16026. year = +tokens[1];
  16027. var weekNumber = +tokens[2];
  16028. var dayOfWeek = +tokens[3] || 0;
  16029. dashCount = date.split('-').length - 1;
  16030. if (dashCount > 0 &&
  16031. ((!defined(tokens[3]) && dashCount !== 1) ||
  16032. (defined(tokens[3]) && dashCount !== 2))) {
  16033. throw new DeveloperError(iso8601ErrorMessage);
  16034. }
  16035. var january4 = new Date(Date.UTC(year, 0, 4));
  16036. dayOfYear = (weekNumber * 7) + dayOfWeek - january4.getUTCDay() - 3;
  16037. } else {
  16038. //None of our regular expressions succeeded in parsing the date properly.
  16039. throw new DeveloperError(iso8601ErrorMessage);
  16040. }
  16041. }
  16042. //Split an ordinal date into month/day.
  16043. tmp = new Date(Date.UTC(year, 0, 1));
  16044. tmp.setUTCDate(dayOfYear);
  16045. month = tmp.getUTCMonth() + 1;
  16046. day = tmp.getUTCDate();
  16047. }
  16048. }
  16049. }
  16050. //Now that we have all of the date components, validate them to make sure nothing is out of range.
  16051. inLeapYear = isLeapYear(year);
  16052. if (month < 1 || month > 12 || day < 1 || ((month !== 2 || !inLeapYear) && day > daysInMonth[month - 1]) || (inLeapYear && month === 2 && day > daysInLeapFeburary)) {
  16053. throw new DeveloperError(iso8601ErrorMessage);
  16054. }
  16055. //Not move onto the time string, which is much simpler.
  16056. var offsetIndex;
  16057. if (defined(time)) {
  16058. tokens = time.match(matchHoursMinutesSeconds);
  16059. if (tokens !== null) {
  16060. dashCount = time.split(':').length - 1;
  16061. if (dashCount > 0 && dashCount !== 2 && dashCount !== 3) {
  16062. throw new DeveloperError(iso8601ErrorMessage);
  16063. }
  16064. hour = +tokens[1];
  16065. minute = +tokens[2];
  16066. second = +tokens[3];
  16067. millisecond = +(tokens[4] || 0) * 1000.0;
  16068. offsetIndex = 5;
  16069. } else {
  16070. tokens = time.match(matchHoursMinutes);
  16071. if (tokens !== null) {
  16072. dashCount = time.split(':').length - 1;
  16073. if (dashCount > 2) {
  16074. throw new DeveloperError(iso8601ErrorMessage);
  16075. }
  16076. hour = +tokens[1];
  16077. minute = +tokens[2];
  16078. second = +(tokens[3] || 0) * 60.0;
  16079. offsetIndex = 4;
  16080. } else {
  16081. tokens = time.match(matchHours);
  16082. if (tokens !== null) {
  16083. hour = +tokens[1];
  16084. minute = +(tokens[2] || 0) * 60.0;
  16085. offsetIndex = 3;
  16086. } else {
  16087. throw new DeveloperError(iso8601ErrorMessage);
  16088. }
  16089. }
  16090. }
  16091. //Validate that all values are in proper range. Minutes and hours have special cases at 60 and 24.
  16092. if (minute >= 60 || second >= 61 || hour > 24 || (hour === 24 && (minute > 0 || second > 0 || millisecond > 0))) {
  16093. throw new DeveloperError(iso8601ErrorMessage);
  16094. }
  16095. //Check the UTC offset value, if no value exists, use local time
  16096. //a Z indicates UTC, + or - are offsets.
  16097. var offset = tokens[offsetIndex];
  16098. var offsetHours = +(tokens[offsetIndex + 1]);
  16099. var offsetMinutes = +(tokens[offsetIndex + 2] || 0);
  16100. switch (offset) {
  16101. case '+':
  16102. hour = hour - offsetHours;
  16103. minute = minute - offsetMinutes;
  16104. break;
  16105. case '-':
  16106. hour = hour + offsetHours;
  16107. minute = minute + offsetMinutes;
  16108. break;
  16109. case 'Z':
  16110. break;
  16111. default:
  16112. minute = minute + new Date(Date.UTC(year, month - 1, day, hour, minute)).getTimezoneOffset();
  16113. break;
  16114. }
  16115. } else {
  16116. //If no time is specified, it is considered the beginning of the day, local time.
  16117. minute = minute + new Date(year, month - 1, day).getTimezoneOffset();
  16118. }
  16119. //ISO8601 denotes a leap second by any time having a seconds component of 60 seconds.
  16120. //If that's the case, we need to temporarily subtract a second in order to build a UTC date.
  16121. //Then we add it back in after converting to TAI.
  16122. var isLeapSecond = second === 60;
  16123. if (isLeapSecond) {
  16124. second--;
  16125. }
  16126. //Even if we successfully parsed the string into its components, after applying UTC offset or
  16127. //special cases like 24:00:00 denoting midnight, we need to normalize the data appropriately.
  16128. //milliseconds can never be greater than 1000, and seconds can't be above 60, so we start with minutes
  16129. while (minute >= 60) {
  16130. minute -= 60;
  16131. hour++;
  16132. }
  16133. while (hour >= 24) {
  16134. hour -= 24;
  16135. day++;
  16136. }
  16137. tmp = (inLeapYear && month === 2) ? daysInLeapFeburary : daysInMonth[month - 1];
  16138. while (day > tmp) {
  16139. day -= tmp;
  16140. month++;
  16141. if (month > 12) {
  16142. month -= 12;
  16143. year++;
  16144. }
  16145. tmp = (inLeapYear && month === 2) ? daysInLeapFeburary : daysInMonth[month - 1];
  16146. }
  16147. //If UTC offset is at the beginning/end of the day, minutes can be negative.
  16148. while (minute < 0) {
  16149. minute += 60;
  16150. hour--;
  16151. }
  16152. while (hour < 0) {
  16153. hour += 24;
  16154. day--;
  16155. }
  16156. while (day < 1) {
  16157. month--;
  16158. if (month < 1) {
  16159. month += 12;
  16160. year--;
  16161. }
  16162. tmp = (inLeapYear && month === 2) ? daysInLeapFeburary : daysInMonth[month - 1];
  16163. day += tmp;
  16164. }
  16165. //Now create the JulianDate components from the Gregorian date and actually create our instance.
  16166. var components = computeJulianDateComponents(year, month, day, hour, minute, second, millisecond);
  16167. if (!defined(result)) {
  16168. result = new JulianDate(components[0], components[1], TimeStandard.UTC);
  16169. } else {
  16170. setComponents(components[0], components[1], result);
  16171. convertUtcToTai(result);
  16172. }
  16173. //If we were on a leap second, add it back.
  16174. if (isLeapSecond) {
  16175. JulianDate.addSeconds(result, 1, result);
  16176. }
  16177. return result;
  16178. };
  16179. /**
  16180. * Creates a new instance that represents the current system time.
  16181. * This is equivalent to calling <code>JulianDate.fromDate(new Date());</code>.
  16182. *
  16183. * @param {JulianDate} [result] An existing instance to use for the result.
  16184. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  16185. */
  16186. JulianDate.now = function(result) {
  16187. return JulianDate.fromDate(new Date(), result);
  16188. };
  16189. var toGregorianDateScratch = new JulianDate(0, 0, TimeStandard.TAI);
  16190. /**
  16191. * Creates a {@link GregorianDate} from the provided instance.
  16192. *
  16193. * @param {JulianDate} julianDate The date to be converted.
  16194. * @param {GregorianDate} [result] An existing instance to use for the result.
  16195. * @returns {GregorianDate} The modified result parameter or a new instance if none was provided.
  16196. */
  16197. JulianDate.toGregorianDate = function(julianDate, result) {
  16198. if (!defined(julianDate)) {
  16199. throw new DeveloperError('julianDate is required.');
  16200. }
  16201. var isLeapSecond = false;
  16202. var thisUtc = convertTaiToUtc(julianDate, toGregorianDateScratch);
  16203. if (!defined(thisUtc)) {
  16204. //Conversion to UTC will fail if we are during a leap second.
  16205. //If that's the case, subtract a second and convert again.
  16206. //JavaScript doesn't support leap seconds, so this results in second 59 being repeated twice.
  16207. JulianDate.addSeconds(julianDate, -1, toGregorianDateScratch);
  16208. thisUtc = convertTaiToUtc(toGregorianDateScratch, toGregorianDateScratch);
  16209. isLeapSecond = true;
  16210. }
  16211. var julianDayNumber = thisUtc.dayNumber;
  16212. var secondsOfDay = thisUtc.secondsOfDay;
  16213. if (secondsOfDay >= 43200.0) {
  16214. julianDayNumber += 1;
  16215. }
  16216. // Algorithm from page 604 of the Explanatory Supplement to the
  16217. // Astronomical Almanac (Seidelmann 1992).
  16218. var L = (julianDayNumber + 68569) | 0;
  16219. var N = (4 * L / 146097) | 0;
  16220. L = (L - (((146097 * N + 3) / 4) | 0)) | 0;
  16221. var I = ((4000 * (L + 1)) / 1461001) | 0;
  16222. L = (L - (((1461 * I) / 4) | 0) + 31) | 0;
  16223. var J = ((80 * L) / 2447) | 0;
  16224. var day = (L - (((2447 * J) / 80) | 0)) | 0;
  16225. L = (J / 11) | 0;
  16226. var month = (J + 2 - 12 * L) | 0;
  16227. var year = (100 * (N - 49) + I + L) | 0;
  16228. var hour = (secondsOfDay / TimeConstants.SECONDS_PER_HOUR) | 0;
  16229. var remainingSeconds = secondsOfDay - (hour * TimeConstants.SECONDS_PER_HOUR);
  16230. var minute = (remainingSeconds / TimeConstants.SECONDS_PER_MINUTE) | 0;
  16231. remainingSeconds = remainingSeconds - (minute * TimeConstants.SECONDS_PER_MINUTE);
  16232. var second = remainingSeconds | 0;
  16233. var millisecond = ((remainingSeconds - second) / TimeConstants.SECONDS_PER_MILLISECOND);
  16234. // JulianDates are noon-based
  16235. hour += 12;
  16236. if (hour > 23) {
  16237. hour -= 24;
  16238. }
  16239. //If we were on a leap second, add it back.
  16240. if (isLeapSecond) {
  16241. second += 1;
  16242. }
  16243. if (!defined(result)) {
  16244. return new GregorianDate(year, month, day, hour, minute, second, millisecond, isLeapSecond);
  16245. }
  16246. result.year = year;
  16247. result.month = month;
  16248. result.day = day;
  16249. result.hour = hour;
  16250. result.minute = minute;
  16251. result.second = second;
  16252. result.millisecond = millisecond;
  16253. result.isLeapSecond = isLeapSecond;
  16254. return result;
  16255. };
  16256. /**
  16257. * Creates a JavaScript Date from the provided instance.
  16258. * Since JavaScript dates are only accurate to the nearest millisecond and
  16259. * cannot represent a leap second, consider using {@link JulianDate.toGregorianDate} instead.
  16260. * If the provided JulianDate is during a leap second, the previous second is used.
  16261. *
  16262. * @param {JulianDate} julianDate The date to be converted.
  16263. * @returns {Date} A new instance representing the provided date.
  16264. */
  16265. JulianDate.toDate = function(julianDate) {
  16266. if (!defined(julianDate)) {
  16267. throw new DeveloperError('julianDate is required.');
  16268. }
  16269. var gDate = JulianDate.toGregorianDate(julianDate, gregorianDateScratch);
  16270. var second = gDate.second;
  16271. if (gDate.isLeapSecond) {
  16272. second -= 1;
  16273. }
  16274. return new Date(Date.UTC(gDate.year, gDate.month - 1, gDate.day, gDate.hour, gDate.minute, second, gDate.millisecond));
  16275. };
  16276. /**
  16277. * Creates an ISO8601 representation of the provided date.
  16278. *
  16279. * @param {JulianDate} julianDate The date to be converted.
  16280. * @param {Number} [precision] The number of fractional digits used to represent the seconds component. By default, the most precise representation is used.
  16281. * @returns {String} The ISO8601 representation of the provided date.
  16282. */
  16283. JulianDate.toIso8601 = function(julianDate, precision) {
  16284. if (!defined(julianDate)) {
  16285. throw new DeveloperError('julianDate is required.');
  16286. }
  16287. var gDate = JulianDate.toGregorianDate(julianDate, gDate);
  16288. var millisecondStr;
  16289. if (!defined(precision) && gDate.millisecond !== 0) {
  16290. //Forces milliseconds into a number with at least 3 digits to whatever the default toString() precision is.
  16291. millisecondStr = (gDate.millisecond * 0.01).toString().replace('.', '');
  16292. return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%sZ", gDate.year, gDate.month, gDate.day, gDate.hour, gDate.minute, gDate.second, millisecondStr);
  16293. }
  16294. //Precision is either 0 or milliseconds is 0 with undefined precision, in either case, leave off milliseconds entirely
  16295. if (!defined(precision) || precision === 0) {
  16296. return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", gDate.year, gDate.month, gDate.day, gDate.hour, gDate.minute, gDate.second);
  16297. }
  16298. //Forces milliseconds into a number with at least 3 digits to whatever the specified precision is.
  16299. millisecondStr = (gDate.millisecond * 0.01).toFixed(precision).replace('.', '').slice(0, precision);
  16300. return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%sZ", gDate.year, gDate.month, gDate.day, gDate.hour, gDate.minute, gDate.second, millisecondStr);
  16301. };
  16302. /**
  16303. * Duplicates a JulianDate instance.
  16304. *
  16305. * @param {JulianDate} julianDate The date to duplicate.
  16306. * @param {JulianDate} [result] An existing instance to use for the result.
  16307. * @returns {JulianDate} The modified result parameter or a new instance if none was provided. Returns undefined if julianDate is undefined.
  16308. */
  16309. JulianDate.clone = function(julianDate, result) {
  16310. if (!defined(julianDate)) {
  16311. return undefined;
  16312. }
  16313. if (!defined(result)) {
  16314. return new JulianDate(julianDate.dayNumber, julianDate.secondsOfDay, TimeStandard.TAI);
  16315. }
  16316. result.dayNumber = julianDate.dayNumber;
  16317. result.secondsOfDay = julianDate.secondsOfDay;
  16318. return result;
  16319. };
  16320. /**
  16321. * Compares two instances.
  16322. *
  16323. * @param {JulianDate} left The first instance.
  16324. * @param {JulianDate} right The second instance.
  16325. * @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.
  16326. */
  16327. JulianDate.compare = function(left, right) {
  16328. if (!defined(left)) {
  16329. throw new DeveloperError('left is required.');
  16330. }
  16331. if (!defined(right)) {
  16332. throw new DeveloperError('right is required.');
  16333. }
  16334. var julianDayNumberDifference = left.dayNumber - right.dayNumber;
  16335. if (julianDayNumberDifference !== 0) {
  16336. return julianDayNumberDifference;
  16337. }
  16338. return left.secondsOfDay - right.secondsOfDay;
  16339. };
  16340. /**
  16341. * Compares two instances and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  16342. *
  16343. * @param {JulianDate} [left] The first instance.
  16344. * @param {JulianDate} [right] The second instance.
  16345. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  16346. */
  16347. JulianDate.equals = function(left, right) {
  16348. return (left === right) ||
  16349. (defined(left) &&
  16350. defined(right) &&
  16351. left.dayNumber === right.dayNumber &&
  16352. left.secondsOfDay === right.secondsOfDay);
  16353. };
  16354. /**
  16355. * Compares two instances and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  16356. * each other. That is, in order for the dates to be considered equal (and for
  16357. * this function to return <code>true</code>), the absolute value of the difference between them, in
  16358. * seconds, must be less than <code>epsilon</code>.
  16359. *
  16360. * @param {JulianDate} [left] The first instance.
  16361. * @param {JulianDate} [right] The second instance.
  16362. * @param {Number} epsilon The maximum number of seconds that should separate the two instances.
  16363. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  16364. */
  16365. JulianDate.equalsEpsilon = function(left, right, epsilon) {
  16366. if (!defined(epsilon)) {
  16367. throw new DeveloperError('epsilon is required.');
  16368. }
  16369. return (left === right) ||
  16370. (defined(left) &&
  16371. defined(right) &&
  16372. Math.abs(JulianDate.secondsDifference(left, right)) <= epsilon);
  16373. };
  16374. /**
  16375. * Computes the total number of whole and fractional days represented by the provided instance.
  16376. *
  16377. * @param {JulianDate} julianDate The date.
  16378. * @returns {Number} The Julian date as single floating point number.
  16379. */
  16380. JulianDate.totalDays = function(julianDate) {
  16381. if (!defined(julianDate)) {
  16382. throw new DeveloperError('julianDate is required.');
  16383. }
  16384. return julianDate.dayNumber + (julianDate.secondsOfDay / TimeConstants.SECONDS_PER_DAY);
  16385. };
  16386. /**
  16387. * Computes the difference in seconds between the provided instance.
  16388. *
  16389. * @param {JulianDate} left The first instance.
  16390. * @param {JulianDate} right The second instance.
  16391. * @returns {Number} The difference, in seconds, when subtracting <code>right</code> from <code>left</code>.
  16392. */
  16393. JulianDate.secondsDifference = function(left, right) {
  16394. if (!defined(left)) {
  16395. throw new DeveloperError('left is required.');
  16396. }
  16397. if (!defined(right)) {
  16398. throw new DeveloperError('right is required.');
  16399. }
  16400. var dayDifference = (left.dayNumber - right.dayNumber) * TimeConstants.SECONDS_PER_DAY;
  16401. return (dayDifference + (left.secondsOfDay - right.secondsOfDay));
  16402. };
  16403. /**
  16404. * Computes the difference in days between the provided instance.
  16405. *
  16406. * @param {JulianDate} left The first instance.
  16407. * @param {JulianDate} right The second instance.
  16408. * @returns {Number} The difference, in days, when subtracting <code>right</code> from <code>left</code>.
  16409. */
  16410. JulianDate.daysDifference = function(left, right) {
  16411. if (!defined(left)) {
  16412. throw new DeveloperError('left is required.');
  16413. }
  16414. if (!defined(right)) {
  16415. throw new DeveloperError('right is required.');
  16416. }
  16417. var dayDifference = (left.dayNumber - right.dayNumber);
  16418. var secondDifference = (left.secondsOfDay - right.secondsOfDay) / TimeConstants.SECONDS_PER_DAY;
  16419. return dayDifference + secondDifference;
  16420. };
  16421. /**
  16422. * Computes the number of seconds the provided instance is ahead of UTC.
  16423. *
  16424. * @param {JulianDate} julianDate The date.
  16425. * @returns {Number} The number of seconds the provided instance is ahead of UTC
  16426. */
  16427. JulianDate.computeTaiMinusUtc = function(julianDate) {
  16428. binarySearchScratchLeapSecond.julianDate = julianDate;
  16429. var leapSeconds = JulianDate.leapSeconds;
  16430. var index = binarySearch(leapSeconds, binarySearchScratchLeapSecond, compareLeapSecondDates);
  16431. if (index < 0) {
  16432. index = ~index;
  16433. --index;
  16434. if (index < 0) {
  16435. index = 0;
  16436. }
  16437. }
  16438. return leapSeconds[index].offset;
  16439. };
  16440. /**
  16441. * Adds the provided number of seconds to the provided date instance.
  16442. *
  16443. * @param {JulianDate} julianDate The date.
  16444. * @param {Number} seconds The number of seconds to add or subtract.
  16445. * @param {JulianDate} result An existing instance to use for the result.
  16446. * @returns {JulianDate} The modified result parameter.
  16447. */
  16448. JulianDate.addSeconds = function(julianDate, seconds, result) {
  16449. if (!defined(julianDate)) {
  16450. throw new DeveloperError('julianDate is required.');
  16451. }
  16452. if (!defined(seconds)) {
  16453. throw new DeveloperError('seconds is required.');
  16454. }
  16455. if (!defined(result)) {
  16456. throw new DeveloperError('result is required.');
  16457. }
  16458. return setComponents(julianDate.dayNumber, julianDate.secondsOfDay + seconds, result);
  16459. };
  16460. /**
  16461. * Adds the provided number of minutes to the provided date instance.
  16462. *
  16463. * @param {JulianDate} julianDate The date.
  16464. * @param {Number} minutes The number of minutes to add or subtract.
  16465. * @param {JulianDate} result An existing instance to use for the result.
  16466. * @returns {JulianDate} The modified result parameter.
  16467. */
  16468. JulianDate.addMinutes = function(julianDate, minutes, result) {
  16469. if (!defined(julianDate)) {
  16470. throw new DeveloperError('julianDate is required.');
  16471. }
  16472. if (!defined(minutes)) {
  16473. throw new DeveloperError('minutes is required.');
  16474. }
  16475. if (!defined(result)) {
  16476. throw new DeveloperError('result is required.');
  16477. }
  16478. var newSecondsOfDay = julianDate.secondsOfDay + (minutes * TimeConstants.SECONDS_PER_MINUTE);
  16479. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  16480. };
  16481. /**
  16482. * Adds the provided number of hours to the provided date instance.
  16483. *
  16484. * @param {JulianDate} julianDate The date.
  16485. * @param {Number} hours The number of hours to add or subtract.
  16486. * @param {JulianDate} result An existing instance to use for the result.
  16487. * @returns {JulianDate} The modified result parameter.
  16488. */
  16489. JulianDate.addHours = function(julianDate, hours, result) {
  16490. if (!defined(julianDate)) {
  16491. throw new DeveloperError('julianDate is required.');
  16492. }
  16493. if (!defined(hours)) {
  16494. throw new DeveloperError('hours is required.');
  16495. }
  16496. if (!defined(result)) {
  16497. throw new DeveloperError('result is required.');
  16498. }
  16499. var newSecondsOfDay = julianDate.secondsOfDay + (hours * TimeConstants.SECONDS_PER_HOUR);
  16500. return setComponents(julianDate.dayNumber, newSecondsOfDay, result);
  16501. };
  16502. /**
  16503. * Adds the provided number of days to the provided date instance.
  16504. *
  16505. * @param {JulianDate} julianDate The date.
  16506. * @param {Number} days The number of days to add or subtract.
  16507. * @param {JulianDate} result An existing instance to use for the result.
  16508. * @returns {JulianDate} The modified result parameter.
  16509. */
  16510. JulianDate.addDays = function(julianDate, days, result) {
  16511. if (!defined(julianDate)) {
  16512. throw new DeveloperError('julianDate is required.');
  16513. }
  16514. if (!defined(days)) {
  16515. throw new DeveloperError('days is required.');
  16516. }
  16517. if (!defined(result)) {
  16518. throw new DeveloperError('result is required.');
  16519. }
  16520. var newJulianDayNumber = julianDate.dayNumber + days;
  16521. return setComponents(newJulianDayNumber, julianDate.secondsOfDay, result);
  16522. };
  16523. /**
  16524. * Compares the provided instances and returns <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  16525. *
  16526. * @param {JulianDate} left The first instance.
  16527. * @param {JulianDate} right The second instance.
  16528. * @returns {Boolean} <code>true</code> if <code>left</code> is earlier than <code>right</code>, <code>false</code> otherwise.
  16529. */
  16530. JulianDate.lessThan = function(left, right) {
  16531. return JulianDate.compare(left, right) < 0;
  16532. };
  16533. /**
  16534. * 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.
  16535. *
  16536. * @param {JulianDate} left The first instance.
  16537. * @param {JulianDate} right The second instance.
  16538. * @returns {Boolean} <code>true</code> if <code>left</code> is earlier than or equal to <code>right</code>, <code>false</code> otherwise.
  16539. */
  16540. JulianDate.lessThanOrEquals = function(left, right) {
  16541. return JulianDate.compare(left, right) <= 0;
  16542. };
  16543. /**
  16544. * Compares the provided instances and returns <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  16545. *
  16546. * @param {JulianDate} left The first instance.
  16547. * @param {JulianDate} right The second instance.
  16548. * @returns {Boolean} <code>true</code> if <code>left</code> is later than <code>right</code>, <code>false</code> otherwise.
  16549. */
  16550. JulianDate.greaterThan = function(left, right) {
  16551. return JulianDate.compare(left, right) > 0;
  16552. };
  16553. /**
  16554. * 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.
  16555. *
  16556. * @param {JulianDate} left The first instance.
  16557. * @param {JulianDate} right The second instance.
  16558. * @returns {Boolean} <code>true</code> if <code>left</code> is later than or equal to <code>right</code>, <code>false</code> otherwise.
  16559. */
  16560. JulianDate.greaterThanOrEquals = function(left, right) {
  16561. return JulianDate.compare(left, right) >= 0;
  16562. };
  16563. /**
  16564. * Duplicates this instance.
  16565. *
  16566. * @param {JulianDate} [result] An existing instance to use for the result.
  16567. * @returns {JulianDate} The modified result parameter or a new instance if none was provided.
  16568. */
  16569. JulianDate.prototype.clone = function(result) {
  16570. return JulianDate.clone(this, result);
  16571. };
  16572. /**
  16573. * Compares this and the provided instance and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  16574. *
  16575. * @param {JulianDate} [right] The second instance.
  16576. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  16577. */
  16578. JulianDate.prototype.equals = function(right) {
  16579. return JulianDate.equals(this, right);
  16580. };
  16581. /**
  16582. * Compares this and the provided instance and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  16583. * each other. That is, in order for the dates to be considered equal (and for
  16584. * this function to return <code>true</code>), the absolute value of the difference between them, in
  16585. * seconds, must be less than <code>epsilon</code>.
  16586. *
  16587. * @param {JulianDate} [right] The second instance.
  16588. * @param {Number} epsilon The maximum number of seconds that should separate the two instances.
  16589. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  16590. */
  16591. JulianDate.prototype.equalsEpsilon = function(right, epsilon) {
  16592. return JulianDate.equalsEpsilon(this, right, epsilon);
  16593. };
  16594. /**
  16595. * Creates a string representing this date in ISO8601 format.
  16596. *
  16597. * @returns {String} A string representing this date in ISO8601 format.
  16598. */
  16599. JulianDate.prototype.toString = function() {
  16600. return JulianDate.toIso8601(this);
  16601. };
  16602. /**
  16603. * Gets or sets the list of leap seconds used throughout Cesium.
  16604. * @memberof JulianDate
  16605. * @type {LeapSecond[]}
  16606. */
  16607. JulianDate.leapSeconds = [
  16608. new LeapSecond(new JulianDate(2441317, 43210.0, TimeStandard.TAI), 10), // January 1, 1972 00:00:00 UTC
  16609. new LeapSecond(new JulianDate(2441499, 43211.0, TimeStandard.TAI), 11), // July 1, 1972 00:00:00 UTC
  16610. new LeapSecond(new JulianDate(2441683, 43212.0, TimeStandard.TAI), 12), // January 1, 1973 00:00:00 UTC
  16611. new LeapSecond(new JulianDate(2442048, 43213.0, TimeStandard.TAI), 13), // January 1, 1974 00:00:00 UTC
  16612. new LeapSecond(new JulianDate(2442413, 43214.0, TimeStandard.TAI), 14), // January 1, 1975 00:00:00 UTC
  16613. new LeapSecond(new JulianDate(2442778, 43215.0, TimeStandard.TAI), 15), // January 1, 1976 00:00:00 UTC
  16614. new LeapSecond(new JulianDate(2443144, 43216.0, TimeStandard.TAI), 16), // January 1, 1977 00:00:00 UTC
  16615. new LeapSecond(new JulianDate(2443509, 43217.0, TimeStandard.TAI), 17), // January 1, 1978 00:00:00 UTC
  16616. new LeapSecond(new JulianDate(2443874, 43218.0, TimeStandard.TAI), 18), // January 1, 1979 00:00:00 UTC
  16617. new LeapSecond(new JulianDate(2444239, 43219.0, TimeStandard.TAI), 19), // January 1, 1980 00:00:00 UTC
  16618. new LeapSecond(new JulianDate(2444786, 43220.0, TimeStandard.TAI), 20), // July 1, 1981 00:00:00 UTC
  16619. new LeapSecond(new JulianDate(2445151, 43221.0, TimeStandard.TAI), 21), // July 1, 1982 00:00:00 UTC
  16620. new LeapSecond(new JulianDate(2445516, 43222.0, TimeStandard.TAI), 22), // July 1, 1983 00:00:00 UTC
  16621. new LeapSecond(new JulianDate(2446247, 43223.0, TimeStandard.TAI), 23), // July 1, 1985 00:00:00 UTC
  16622. new LeapSecond(new JulianDate(2447161, 43224.0, TimeStandard.TAI), 24), // January 1, 1988 00:00:00 UTC
  16623. new LeapSecond(new JulianDate(2447892, 43225.0, TimeStandard.TAI), 25), // January 1, 1990 00:00:00 UTC
  16624. new LeapSecond(new JulianDate(2448257, 43226.0, TimeStandard.TAI), 26), // January 1, 1991 00:00:00 UTC
  16625. new LeapSecond(new JulianDate(2448804, 43227.0, TimeStandard.TAI), 27), // July 1, 1992 00:00:00 UTC
  16626. new LeapSecond(new JulianDate(2449169, 43228.0, TimeStandard.TAI), 28), // July 1, 1993 00:00:00 UTC
  16627. new LeapSecond(new JulianDate(2449534, 43229.0, TimeStandard.TAI), 29), // July 1, 1994 00:00:00 UTC
  16628. new LeapSecond(new JulianDate(2450083, 43230.0, TimeStandard.TAI), 30), // January 1, 1996 00:00:00 UTC
  16629. new LeapSecond(new JulianDate(2450630, 43231.0, TimeStandard.TAI), 31), // July 1, 1997 00:00:00 UTC
  16630. new LeapSecond(new JulianDate(2451179, 43232.0, TimeStandard.TAI), 32), // January 1, 1999 00:00:00 UTC
  16631. new LeapSecond(new JulianDate(2453736, 43233.0, TimeStandard.TAI), 33), // January 1, 2006 00:00:00 UTC
  16632. new LeapSecond(new JulianDate(2454832, 43234.0, TimeStandard.TAI), 34), // January 1, 2009 00:00:00 UTC
  16633. new LeapSecond(new JulianDate(2456109, 43235.0, TimeStandard.TAI), 35), // July 1, 2012 00:00:00 UTC
  16634. new LeapSecond(new JulianDate(2457204, 43236.0, TimeStandard.TAI), 36), // July 1, 2015 00:00:00 UTC
  16635. new LeapSecond(new JulianDate(2457754, 43237.0, TimeStandard.TAI), 37) // January 1, 2017 00:00:00 UTC
  16636. ];
  16637. return JulianDate;
  16638. });
  16639. /*global define*/
  16640. define('Core/clone',[
  16641. './defaultValue'
  16642. ], function(
  16643. defaultValue) {
  16644. 'use strict';
  16645. /**
  16646. * Clones an object, returning a new object containing the same properties.
  16647. *
  16648. * @exports clone
  16649. *
  16650. * @param {Object} object The object to clone.
  16651. * @param {Boolean} [deep=false] If true, all properties will be deep cloned recursively.
  16652. * @returns {Object} The cloned object.
  16653. */
  16654. function clone(object, deep) {
  16655. if (object === null || typeof object !== 'object') {
  16656. return object;
  16657. }
  16658. deep = defaultValue(deep, false);
  16659. var result = new object.constructor();
  16660. for ( var propertyName in object) {
  16661. if (object.hasOwnProperty(propertyName)) {
  16662. var value = object[propertyName];
  16663. if (deep) {
  16664. value = clone(value, deep);
  16665. }
  16666. result[propertyName] = value;
  16667. }
  16668. }
  16669. return result;
  16670. }
  16671. return clone;
  16672. });
  16673. /*global define*/
  16674. define('Core/parseResponseHeaders',[], function() {
  16675. 'use strict';
  16676. /**
  16677. * Parses the result of XMLHttpRequest's getAllResponseHeaders() method into
  16678. * a dictionary.
  16679. *
  16680. * @exports parseResponseHeaders
  16681. *
  16682. * @param {String} headerString The header string returned by getAllResponseHeaders(). The format is
  16683. * described here: http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders()-method
  16684. * @returns {Object} A dictionary of key/value pairs, where each key is the name of a header and the corresponding value
  16685. * is that header's value.
  16686. *
  16687. * @private
  16688. */
  16689. function parseResponseHeaders(headerString) {
  16690. var headers = {};
  16691. if (!headerString) {
  16692. return headers;
  16693. }
  16694. var headerPairs = headerString.split('\u000d\u000a');
  16695. for (var i = 0; i < headerPairs.length; ++i) {
  16696. var headerPair = headerPairs[i];
  16697. // Can't use split() here because it does the wrong thing
  16698. // if the header value has the string ": " in it.
  16699. var index = headerPair.indexOf('\u003a\u0020');
  16700. if (index > 0) {
  16701. var key = headerPair.substring(0, index);
  16702. var val = headerPair.substring(index + 2);
  16703. headers[key] = val;
  16704. }
  16705. }
  16706. return headers;
  16707. }
  16708. return parseResponseHeaders;
  16709. });
  16710. /*global define*/
  16711. define('Core/RequestErrorEvent',[
  16712. './defined',
  16713. './parseResponseHeaders'
  16714. ], function(
  16715. defined,
  16716. parseResponseHeaders) {
  16717. 'use strict';
  16718. /**
  16719. * An event that is raised when a request encounters an error.
  16720. *
  16721. * @constructor
  16722. * @alias RequestErrorEvent
  16723. *
  16724. * @param {Number} [statusCode] The HTTP error status code, such as 404.
  16725. * @param {Object} [response] The response included along with the error.
  16726. * @param {String|Object} [responseHeaders] The response headers, represented either as an object literal or as a
  16727. * string in the format returned by XMLHttpRequest's getAllResponseHeaders() function.
  16728. */
  16729. function RequestErrorEvent(statusCode, response, responseHeaders) {
  16730. /**
  16731. * The HTTP error status code, such as 404. If the error does not have a particular
  16732. * HTTP code, this property will be undefined.
  16733. *
  16734. * @type {Number}
  16735. */
  16736. this.statusCode = statusCode;
  16737. /**
  16738. * The response included along with the error. If the error does not include a response,
  16739. * this property will be undefined.
  16740. *
  16741. * @type {Object}
  16742. */
  16743. this.response = response;
  16744. /**
  16745. * The headers included in the response, represented as an object literal of key/value pairs.
  16746. * If the error does not include any headers, this property will be undefined.
  16747. *
  16748. * @type {Object}
  16749. */
  16750. this.responseHeaders = responseHeaders;
  16751. if (typeof this.responseHeaders === 'string') {
  16752. this.responseHeaders = parseResponseHeaders(this.responseHeaders);
  16753. }
  16754. }
  16755. /**
  16756. * Creates a string representing this RequestErrorEvent.
  16757. * @memberof RequestErrorEvent
  16758. *
  16759. * @returns {String} A string representing the provided RequestErrorEvent.
  16760. */
  16761. RequestErrorEvent.prototype.toString = function() {
  16762. var str = 'Request has failed.';
  16763. if (defined(this.statusCode)) {
  16764. str += ' Status Code: ' + this.statusCode;
  16765. }
  16766. return str;
  16767. };
  16768. return RequestErrorEvent;
  16769. });
  16770. /**
  16771. * @license
  16772. *
  16773. * Grauw URI utilities
  16774. *
  16775. * See: http://hg.grauw.nl/grauw-lib/file/tip/src/uri.js
  16776. *
  16777. * @author Laurens Holst (http://www.grauw.nl/)
  16778. *
  16779. * Copyright 2012 Laurens Holst
  16780. *
  16781. * Licensed under the Apache License, Version 2.0 (the "License");
  16782. * you may not use this file except in compliance with the License.
  16783. * You may obtain a copy of the License at
  16784. *
  16785. * http://www.apache.org/licenses/LICENSE-2.0
  16786. *
  16787. * Unless required by applicable law or agreed to in writing, software
  16788. * distributed under the License is distributed on an "AS IS" BASIS,
  16789. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16790. * See the License for the specific language governing permissions and
  16791. * limitations under the License.
  16792. *
  16793. */
  16794. /*global define*/
  16795. define('ThirdParty/Uri',[],function() {
  16796. /**
  16797. * Constructs a URI object.
  16798. * @constructor
  16799. * @class Implementation of URI parsing and base URI resolving algorithm in RFC 3986.
  16800. * @param {string|URI} uri A string or URI object to create the object from.
  16801. */
  16802. function URI(uri) {
  16803. if (uri instanceof URI) { // copy constructor
  16804. this.scheme = uri.scheme;
  16805. this.authority = uri.authority;
  16806. this.path = uri.path;
  16807. this.query = uri.query;
  16808. this.fragment = uri.fragment;
  16809. } else if (uri) { // uri is URI string or cast to string
  16810. var c = parseRegex.exec(uri);
  16811. this.scheme = c[1];
  16812. this.authority = c[2];
  16813. this.path = c[3];
  16814. this.query = c[4];
  16815. this.fragment = c[5];
  16816. }
  16817. }
  16818. // Initial values on the prototype
  16819. URI.prototype.scheme = null;
  16820. URI.prototype.authority = null;
  16821. URI.prototype.path = '';
  16822. URI.prototype.query = null;
  16823. URI.prototype.fragment = null;
  16824. // Regular expression from RFC 3986 appendix B
  16825. var parseRegex = new RegExp('^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?$');
  16826. /**
  16827. * Returns the scheme part of the URI.
  16828. * In "http://example.com:80/a/b?x#y" this is "http".
  16829. */
  16830. URI.prototype.getScheme = function() {
  16831. return this.scheme;
  16832. };
  16833. /**
  16834. * Returns the authority part of the URI.
  16835. * In "http://example.com:80/a/b?x#y" this is "example.com:80".
  16836. */
  16837. URI.prototype.getAuthority = function() {
  16838. return this.authority;
  16839. };
  16840. /**
  16841. * Returns the path part of the URI.
  16842. * In "http://example.com:80/a/b?x#y" this is "/a/b".
  16843. * In "mailto:mike@example.com" this is "mike@example.com".
  16844. */
  16845. URI.prototype.getPath = function() {
  16846. return this.path;
  16847. };
  16848. /**
  16849. * Returns the query part of the URI.
  16850. * In "http://example.com:80/a/b?x#y" this is "x".
  16851. */
  16852. URI.prototype.getQuery = function() {
  16853. return this.query;
  16854. };
  16855. /**
  16856. * Returns the fragment part of the URI.
  16857. * In "http://example.com:80/a/b?x#y" this is "y".
  16858. */
  16859. URI.prototype.getFragment = function() {
  16860. return this.fragment;
  16861. };
  16862. /**
  16863. * Tests whether the URI is an absolute URI.
  16864. * See RFC 3986 section 4.3.
  16865. */
  16866. URI.prototype.isAbsolute = function() {
  16867. return !!this.scheme && !this.fragment;
  16868. };
  16869. ///**
  16870. //* Extensive validation of the URI against the ABNF in RFC 3986
  16871. //*/
  16872. //URI.prototype.validate
  16873. /**
  16874. * Tests whether the URI is a same-document reference.
  16875. * See RFC 3986 section 4.4.
  16876. *
  16877. * To perform more thorough comparison, you can normalise the URI objects.
  16878. */
  16879. URI.prototype.isSameDocumentAs = function(uri) {
  16880. return uri.scheme == this.scheme &&
  16881. uri.authority == this.authority &&
  16882. uri.path == this.path &&
  16883. uri.query == this.query;
  16884. };
  16885. /**
  16886. * Simple String Comparison of two URIs.
  16887. * See RFC 3986 section 6.2.1.
  16888. *
  16889. * To perform more thorough comparison, you can normalise the URI objects.
  16890. */
  16891. URI.prototype.equals = function(uri) {
  16892. return this.isSameDocumentAs(uri) && uri.fragment == this.fragment;
  16893. };
  16894. /**
  16895. * Normalizes the URI using syntax-based normalization.
  16896. * This includes case normalization, percent-encoding normalization and path segment normalization.
  16897. * XXX: Percent-encoding normalization does not escape characters that need to be escaped.
  16898. * (Although that would not be a valid URI in the first place. See validate().)
  16899. * See RFC 3986 section 6.2.2.
  16900. */
  16901. URI.prototype.normalize = function() {
  16902. this.removeDotSegments();
  16903. if (this.scheme)
  16904. this.scheme = this.scheme.toLowerCase();
  16905. if (this.authority)
  16906. this.authority = this.authority.replace(authorityRegex, replaceAuthority).
  16907. replace(caseRegex, replaceCase);
  16908. if (this.path)
  16909. this.path = this.path.replace(caseRegex, replaceCase);
  16910. if (this.query)
  16911. this.query = this.query.replace(caseRegex, replaceCase);
  16912. if (this.fragment)
  16913. this.fragment = this.fragment.replace(caseRegex, replaceCase);
  16914. };
  16915. var caseRegex = /%[0-9a-z]{2}/gi;
  16916. var percentRegex = /[a-zA-Z0-9\-\._~]/;
  16917. var authorityRegex = /(.*@)?([^@:]*)(:.*)?/;
  16918. function replaceCase(str) {
  16919. var dec = unescape(str);
  16920. return percentRegex.test(dec) ? dec : str.toUpperCase();
  16921. }
  16922. function replaceAuthority(str, p1, p2, p3) {
  16923. return (p1 || '') + p2.toLowerCase() + (p3 || '');
  16924. }
  16925. /**
  16926. * Resolve a relative URI (this) against a base URI.
  16927. * The base URI must be an absolute URI.
  16928. * See RFC 3986 section 5.2
  16929. */
  16930. URI.prototype.resolve = function(baseURI) {
  16931. var uri = new URI();
  16932. if (this.scheme) {
  16933. uri.scheme = this.scheme;
  16934. uri.authority = this.authority;
  16935. uri.path = this.path;
  16936. uri.query = this.query;
  16937. } else {
  16938. uri.scheme = baseURI.scheme;
  16939. if (this.authority) {
  16940. uri.authority = this.authority;
  16941. uri.path = this.path;
  16942. uri.query = this.query;
  16943. } else {
  16944. uri.authority = baseURI.authority;
  16945. if (this.path == '') {
  16946. uri.path = baseURI.path;
  16947. uri.query = this.query || baseURI.query;
  16948. } else {
  16949. if (this.path.charAt(0) == '/') {
  16950. uri.path = this.path;
  16951. uri.removeDotSegments();
  16952. } else {
  16953. if (baseURI.authority && baseURI.path == '') {
  16954. uri.path = '/' + this.path;
  16955. } else {
  16956. uri.path = baseURI.path.substring(0, baseURI.path.lastIndexOf('/') + 1) + this.path;
  16957. }
  16958. uri.removeDotSegments();
  16959. }
  16960. uri.query = this.query;
  16961. }
  16962. }
  16963. }
  16964. uri.fragment = this.fragment;
  16965. return uri;
  16966. };
  16967. /**
  16968. * Remove dot segments from path.
  16969. * See RFC 3986 section 5.2.4
  16970. * @private
  16971. */
  16972. URI.prototype.removeDotSegments = function() {
  16973. var input = this.path.split('/'),
  16974. output = [],
  16975. segment,
  16976. absPath = input[0] == '';
  16977. if (absPath)
  16978. input.shift();
  16979. var sFirst = input[0] == '' ? input.shift() : null;
  16980. while (input.length) {
  16981. segment = input.shift();
  16982. if (segment == '..') {
  16983. output.pop();
  16984. } else if (segment != '.') {
  16985. output.push(segment);
  16986. }
  16987. }
  16988. if (segment == '.' || segment == '..')
  16989. output.push('');
  16990. if (absPath)
  16991. output.unshift('');
  16992. this.path = output.join('/');
  16993. };
  16994. // We don't like this function because it builds up a cache that is never cleared.
  16995. // /**
  16996. // * Resolves a relative URI against an absolute base URI.
  16997. // * Convenience method.
  16998. // * @param {String} uri the relative URI to resolve
  16999. // * @param {String} baseURI the base URI (must be absolute) to resolve against
  17000. // */
  17001. // URI.resolve = function(sURI, sBaseURI) {
  17002. // var uri = cache[sURI] || (cache[sURI] = new URI(sURI));
  17003. // var baseURI = cache[sBaseURI] || (cache[sBaseURI] = new URI(sBaseURI));
  17004. // return uri.resolve(baseURI).toString();
  17005. // };
  17006. // var cache = {};
  17007. /**
  17008. * Serialises the URI to a string.
  17009. */
  17010. URI.prototype.toString = function() {
  17011. var result = '';
  17012. if (this.scheme)
  17013. result += this.scheme + ':';
  17014. if (this.authority)
  17015. result += '//' + this.authority;
  17016. result += this.path;
  17017. if (this.query)
  17018. result += '?' + this.query;
  17019. if (this.fragment)
  17020. result += '#' + this.fragment;
  17021. return result;
  17022. };
  17023. return URI;
  17024. });
  17025. /*global define*/
  17026. define('Core/TrustedServers',[
  17027. '../ThirdParty/Uri',
  17028. './defined',
  17029. './DeveloperError'
  17030. ], function(
  17031. Uri,
  17032. defined,
  17033. DeveloperError) {
  17034. 'use strict';
  17035. /**
  17036. * A singleton that contains all of the servers that are trusted. Credentials will be sent with
  17037. * any requests to these servers.
  17038. *
  17039. * @exports TrustedServers
  17040. *
  17041. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  17042. */
  17043. var TrustedServers = {};
  17044. var _servers = {};
  17045. /**
  17046. * Adds a trusted server to the registry
  17047. *
  17048. * @param {String} host The host to be added.
  17049. * @param {Number} port The port used to access the host.
  17050. *
  17051. * @example
  17052. * // Add a trusted server
  17053. * TrustedServers.add('my.server.com', 80);
  17054. */
  17055. TrustedServers.add = function(host, port) {
  17056. if (!defined(host)) {
  17057. throw new DeveloperError('host is required.');
  17058. }
  17059. if (!defined(port) || port <= 0) {
  17060. throw new DeveloperError('port is required to be greater than 0.');
  17061. }
  17062. var authority = host.toLowerCase() + ':' + port;
  17063. if (!defined(_servers[authority])) {
  17064. _servers[authority] = true;
  17065. }
  17066. };
  17067. /**
  17068. * Removes a trusted server from the registry
  17069. *
  17070. * @param {String} host The host to be removed.
  17071. * @param {Number} port The port used to access the host.
  17072. *
  17073. * @example
  17074. * // Remove a trusted server
  17075. * TrustedServers.remove('my.server.com', 80);
  17076. */
  17077. TrustedServers.remove = function(host, port) {
  17078. if (!defined(host)) {
  17079. throw new DeveloperError('host is required.');
  17080. }
  17081. if (!defined(port) || port <= 0) {
  17082. throw new DeveloperError('port is required to be greater than 0.');
  17083. }
  17084. var authority = host.toLowerCase() + ':' + port;
  17085. if (defined(_servers[authority])) {
  17086. delete _servers[authority];
  17087. }
  17088. };
  17089. function getAuthority(url) {
  17090. var uri = new Uri(url);
  17091. uri.normalize();
  17092. // Removes username:password@ so we just have host[:port]
  17093. var authority = uri.getAuthority();
  17094. if (!defined(authority)) {
  17095. return undefined; // Relative URL
  17096. }
  17097. if (authority.indexOf('@') !== -1) {
  17098. var parts = authority.split('@');
  17099. authority = parts[1];
  17100. }
  17101. // If the port is missing add one based on the scheme
  17102. if (authority.indexOf(':') === -1) {
  17103. var scheme = uri.getScheme();
  17104. if (!defined(scheme)) {
  17105. scheme = window.location.protocol;
  17106. scheme = scheme.substring(0, scheme.length-1);
  17107. }
  17108. if (scheme === 'http') {
  17109. authority += ':80';
  17110. } else if (scheme === 'https') {
  17111. authority += ':443';
  17112. } else {
  17113. return undefined;
  17114. }
  17115. }
  17116. return authority;
  17117. }
  17118. /**
  17119. * Tests whether a server is trusted or not. The server must have been added with the port if it is included in the url.
  17120. *
  17121. * @param {String} url The url to be tested against the trusted list
  17122. *
  17123. * @returns {boolean} Returns true if url is trusted, false otherwise.
  17124. *
  17125. * @example
  17126. * // Add server
  17127. * TrustedServers.add('my.server.com', 81);
  17128. *
  17129. * // Check if server is trusted
  17130. * if (TrustedServers.contains('https://my.server.com:81/path/to/file.png')) {
  17131. * // my.server.com:81 is trusted
  17132. * }
  17133. * if (TrustedServers.contains('https://my.server.com/path/to/file.png')) {
  17134. * // my.server.com isn't trusted
  17135. * }
  17136. */
  17137. TrustedServers.contains = function(url) {
  17138. if (!defined(url)) {
  17139. throw new DeveloperError('url is required.');
  17140. }
  17141. var authority = getAuthority(url);
  17142. if (defined(authority) && defined(_servers[authority])) {
  17143. return true;
  17144. }
  17145. return false;
  17146. };
  17147. /**
  17148. * Clears the registry
  17149. *
  17150. * @example
  17151. * // Remove a trusted server
  17152. * TrustedServers.clear();
  17153. */
  17154. TrustedServers.clear = function() {
  17155. _servers = {};
  17156. };
  17157. return TrustedServers;
  17158. });
  17159. /*global define*/
  17160. define('Core/loadWithXhr',[
  17161. '../ThirdParty/when',
  17162. './defaultValue',
  17163. './defined',
  17164. './DeveloperError',
  17165. './RequestErrorEvent',
  17166. './RuntimeError',
  17167. './TrustedServers'
  17168. ], function(
  17169. when,
  17170. defaultValue,
  17171. defined,
  17172. DeveloperError,
  17173. RequestErrorEvent,
  17174. RuntimeError,
  17175. TrustedServers) {
  17176. 'use strict';
  17177. /**
  17178. * Asynchronously loads the given URL. Returns a promise that will resolve to
  17179. * the result once loaded, or reject if the URL failed to load. The data is loaded
  17180. * using XMLHttpRequest, which means that in order to make requests to another origin,
  17181. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  17182. *
  17183. * @exports loadWithXhr
  17184. *
  17185. * @param {Object} options Object with the following properties:
  17186. * @param {String|Promise.<String>} options.url The URL of the data, or a promise for the URL.
  17187. * @param {String} [options.responseType] The type of response. This controls the type of item returned.
  17188. * @param {String} [options.method='GET'] The HTTP method to use.
  17189. * @param {String} [options.data] The data to send with the request, if any.
  17190. * @param {Object} [options.headers] HTTP headers to send with the request, if any.
  17191. * @param {String} [options.overrideMimeType] Overrides the MIME type returned by the server.
  17192. * @returns {Promise.<Object>} a promise that will resolve to the requested data when loaded.
  17193. *
  17194. *
  17195. * @example
  17196. * // Load a single URL asynchronously. In real code, you should use loadBlob instead.
  17197. * Cesium.loadWithXhr({
  17198. * url : 'some/url',
  17199. * responseType : 'blob'
  17200. * }).then(function(blob) {
  17201. * // use the data
  17202. * }).otherwise(function(error) {
  17203. * // an error occurred
  17204. * });
  17205. *
  17206. * @see loadArrayBuffer
  17207. * @see loadBlob
  17208. * @see loadJson
  17209. * @see loadText
  17210. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  17211. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  17212. */
  17213. function loadWithXhr(options) {
  17214. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  17215. if (!defined(options.url)) {
  17216. throw new DeveloperError('options.url is required.');
  17217. }
  17218. var responseType = options.responseType;
  17219. var method = defaultValue(options.method, 'GET');
  17220. var data = options.data;
  17221. var headers = options.headers;
  17222. var overrideMimeType = options.overrideMimeType;
  17223. return when(options.url, function(url) {
  17224. var deferred = when.defer();
  17225. loadWithXhr.load(url, responseType, method, data, headers, deferred, overrideMimeType);
  17226. return deferred.promise;
  17227. });
  17228. }
  17229. var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
  17230. function decodeDataUriText(isBase64, data) {
  17231. var result = decodeURIComponent(data);
  17232. if (isBase64) {
  17233. return atob(result);
  17234. }
  17235. return result;
  17236. }
  17237. function decodeDataUriArrayBuffer(isBase64, data) {
  17238. var byteString = decodeDataUriText(isBase64, data);
  17239. var buffer = new ArrayBuffer(byteString.length);
  17240. var view = new Uint8Array(buffer);
  17241. for (var i = 0; i < byteString.length; i++) {
  17242. view[i] = byteString.charCodeAt(i);
  17243. }
  17244. return buffer;
  17245. }
  17246. function decodeDataUri(dataUriRegexResult, responseType) {
  17247. responseType = defaultValue(responseType, '');
  17248. var mimeType = dataUriRegexResult[1];
  17249. var isBase64 = !!dataUriRegexResult[2];
  17250. var data = dataUriRegexResult[3];
  17251. switch (responseType) {
  17252. case '':
  17253. case 'text':
  17254. return decodeDataUriText(isBase64, data);
  17255. case 'arraybuffer':
  17256. return decodeDataUriArrayBuffer(isBase64, data);
  17257. case 'blob':
  17258. var buffer = decodeDataUriArrayBuffer(isBase64, data);
  17259. return new Blob([buffer], {
  17260. type : mimeType
  17261. });
  17262. case 'document':
  17263. var parser = new DOMParser();
  17264. return parser.parseFromString(decodeDataUriText(isBase64, data), mimeType);
  17265. case 'json':
  17266. return JSON.parse(decodeDataUriText(isBase64, data));
  17267. default:
  17268. throw new DeveloperError('Unhandled responseType: ' + responseType);
  17269. }
  17270. }
  17271. // This is broken out into a separate function so that it can be mocked for testing purposes.
  17272. loadWithXhr.load = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
  17273. var dataUriRegexResult = dataUriRegex.exec(url);
  17274. if (dataUriRegexResult !== null) {
  17275. deferred.resolve(decodeDataUri(dataUriRegexResult, responseType));
  17276. return;
  17277. }
  17278. var xhr = new XMLHttpRequest();
  17279. if (TrustedServers.contains(url)) {
  17280. xhr.withCredentials = true;
  17281. }
  17282. if (defined(overrideMimeType) && defined(xhr.overrideMimeType)) {
  17283. xhr.overrideMimeType(overrideMimeType);
  17284. }
  17285. xhr.open(method, url, true);
  17286. if (defined(headers)) {
  17287. for (var key in headers) {
  17288. if (headers.hasOwnProperty(key)) {
  17289. xhr.setRequestHeader(key, headers[key]);
  17290. }
  17291. }
  17292. }
  17293. if (defined(responseType)) {
  17294. xhr.responseType = responseType;
  17295. }
  17296. xhr.onload = function() {
  17297. if (xhr.status < 200 || xhr.status >= 300) {
  17298. deferred.reject(new RequestErrorEvent(xhr.status, xhr.response, xhr.getAllResponseHeaders()));
  17299. return;
  17300. }
  17301. var response = xhr.response;
  17302. var browserResponseType = xhr.responseType;
  17303. //All modern browsers will go into either the first if block or last else block.
  17304. //Other code paths support older browsers that either do not support the supplied responseType
  17305. //or do not support the xhr.response property.
  17306. if (defined(response) && (!defined(responseType) || (browserResponseType === responseType))) {
  17307. deferred.resolve(response);
  17308. } else if ((responseType === 'json') && typeof response === 'string') {
  17309. try {
  17310. deferred.resolve(JSON.parse(response));
  17311. } catch (e) {
  17312. deferred.reject(e);
  17313. }
  17314. } else if ((browserResponseType === '' || browserResponseType === 'document') && defined(xhr.responseXML) && xhr.responseXML.hasChildNodes()) {
  17315. deferred.resolve(xhr.responseXML);
  17316. } else if ((browserResponseType === '' || browserResponseType === 'text') && defined(xhr.responseText)) {
  17317. deferred.resolve(xhr.responseText);
  17318. } else {
  17319. deferred.reject(new RuntimeError('Invalid XMLHttpRequest response type.'));
  17320. }
  17321. };
  17322. xhr.onerror = function(e) {
  17323. deferred.reject(new RequestErrorEvent());
  17324. };
  17325. xhr.send(data);
  17326. };
  17327. loadWithXhr.defaultLoad = loadWithXhr.load;
  17328. return loadWithXhr;
  17329. });
  17330. /*global define*/
  17331. define('Core/loadText',[
  17332. './loadWithXhr'
  17333. ], function(
  17334. loadWithXhr) {
  17335. 'use strict';
  17336. /**
  17337. * Asynchronously loads the given URL as text. Returns a promise that will resolve to
  17338. * a String once loaded, or reject if the URL failed to load. The data is loaded
  17339. * using XMLHttpRequest, which means that in order to make requests to another origin,
  17340. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  17341. *
  17342. * @exports loadText
  17343. *
  17344. * @param {String|Promise.<String>} url The URL to request, or a promise for the URL.
  17345. * @param {Object} [headers] HTTP headers to send with the request.
  17346. * @returns {Promise.<String>} a promise that will resolve to the requested data when loaded.
  17347. *
  17348. *
  17349. * @example
  17350. * // load text from a URL, setting a custom header
  17351. * Cesium.loadText('http://someUrl.com/someJson.txt', {
  17352. * 'X-Custom-Header' : 'some value'
  17353. * }).then(function(text) {
  17354. * // Do something with the text
  17355. * }).otherwise(function(error) {
  17356. * // an error occurred
  17357. * });
  17358. *
  17359. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
  17360. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  17361. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  17362. */
  17363. function loadText(url, headers) {
  17364. return loadWithXhr({
  17365. url : url,
  17366. headers : headers
  17367. });
  17368. }
  17369. return loadText;
  17370. });
  17371. /*global define*/
  17372. define('Core/loadJson',[
  17373. './clone',
  17374. './defined',
  17375. './DeveloperError',
  17376. './loadText'
  17377. ], function(
  17378. clone,
  17379. defined,
  17380. DeveloperError,
  17381. loadText) {
  17382. 'use strict';
  17383. var defaultHeaders = {
  17384. Accept : 'application/json,*/*;q=0.01'
  17385. };
  17386. // note: &#42;&#47;&#42; below is */* but that ends the comment block early
  17387. /**
  17388. * Asynchronously loads the given URL as JSON. Returns a promise that will resolve to
  17389. * a JSON object once loaded, or reject if the URL failed to load. The data is loaded
  17390. * using XMLHttpRequest, which means that in order to make requests to another origin,
  17391. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled. This function
  17392. * adds 'Accept: application/json,&#42;&#47;&#42;;q=0.01' to the request headers, if not
  17393. * already specified.
  17394. *
  17395. * @exports loadJson
  17396. *
  17397. * @param {String|Promise.<String>} url The URL to request, or a promise for the URL.
  17398. * @param {Object} [headers] HTTP headers to send with the request.
  17399. * 'Accept: application/json,&#42;&#47;&#42;;q=0.01' is added to the request headers automatically
  17400. * if not specified.
  17401. * @returns {Promise.<Object>} a promise that will resolve to the requested data when loaded.
  17402. *
  17403. *
  17404. * @example
  17405. * Cesium.loadJson('http://someUrl.com/someJson.txt').then(function(jsonData) {
  17406. * // Do something with the JSON object
  17407. * }).otherwise(function(error) {
  17408. * // an error occurred
  17409. * });
  17410. *
  17411. * @see loadText
  17412. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  17413. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  17414. */
  17415. function loadJson(url, headers) {
  17416. if (!defined(url)) {
  17417. throw new DeveloperError('url is required.');
  17418. }
  17419. if (!defined(headers)) {
  17420. headers = defaultHeaders;
  17421. } else if (!defined(headers.Accept)) {
  17422. // clone before adding the Accept header
  17423. headers = clone(headers);
  17424. headers.Accept = defaultHeaders.Accept;
  17425. }
  17426. return loadText(url, headers).then(function(value) {
  17427. return JSON.parse(value);
  17428. });
  17429. }
  17430. return loadJson;
  17431. });
  17432. /*global define*/
  17433. define('Core/EarthOrientationParameters',[
  17434. '../ThirdParty/when',
  17435. './binarySearch',
  17436. './defaultValue',
  17437. './defined',
  17438. './EarthOrientationParametersSample',
  17439. './freezeObject',
  17440. './JulianDate',
  17441. './LeapSecond',
  17442. './loadJson',
  17443. './RuntimeError',
  17444. './TimeConstants',
  17445. './TimeStandard'
  17446. ], function(
  17447. when,
  17448. binarySearch,
  17449. defaultValue,
  17450. defined,
  17451. EarthOrientationParametersSample,
  17452. freezeObject,
  17453. JulianDate,
  17454. LeapSecond,
  17455. loadJson,
  17456. RuntimeError,
  17457. TimeConstants,
  17458. TimeStandard) {
  17459. 'use strict';
  17460. /**
  17461. * Specifies Earth polar motion coordinates and the difference between UT1 and UTC.
  17462. * These Earth Orientation Parameters (EOP) are primarily used in the transformation from
  17463. * the International Celestial Reference Frame (ICRF) to the International Terrestrial
  17464. * Reference Frame (ITRF).
  17465. *
  17466. * @alias EarthOrientationParameters
  17467. * @constructor
  17468. *
  17469. * @param {Object} [options] Object with the following properties:
  17470. * @param {String} [options.url] The URL from which to obtain EOP data. If neither this
  17471. * parameter nor options.data is specified, all EOP values are assumed
  17472. * to be 0.0. If options.data is specified, this parameter is
  17473. * ignored.
  17474. * @param {Object} [options.data] The actual EOP data. If neither this
  17475. * parameter nor options.data is specified, all EOP values are assumed
  17476. * to be 0.0.
  17477. * @param {Boolean} [options.addNewLeapSeconds=true] True if leap seconds that
  17478. * are specified in the EOP data but not in {@link JulianDate.leapSeconds}
  17479. * should be added to {@link JulianDate.leapSeconds}. False if
  17480. * new leap seconds should be handled correctly in the context
  17481. * of the EOP data but otherwise ignored.
  17482. *
  17483. * @example
  17484. * // An example EOP data file, EOP.json:
  17485. * {
  17486. * "columnNames" : ["dateIso8601","modifiedJulianDateUtc","xPoleWanderRadians","yPoleWanderRadians","ut1MinusUtcSeconds","lengthOfDayCorrectionSeconds","xCelestialPoleOffsetRadians","yCelestialPoleOffsetRadians","taiMinusUtcSeconds"],
  17487. * "samples" : [
  17488. * "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,
  17489. * "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,
  17490. * "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
  17491. * ]
  17492. * }
  17493. *
  17494. * @example
  17495. * // Loading the EOP data
  17496. * var eop = new Cesium.EarthOrientationParameters({ url : 'Data/EOP.json' });
  17497. * Cesium.Transforms.earthOrientationParameters = eop;
  17498. *
  17499. * @private
  17500. */
  17501. function EarthOrientationParameters(options) {
  17502. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  17503. this._dates = undefined;
  17504. this._samples = undefined;
  17505. this._dateColumn = -1;
  17506. this._xPoleWanderRadiansColumn = -1;
  17507. this._yPoleWanderRadiansColumn = -1;
  17508. this._ut1MinusUtcSecondsColumn = -1;
  17509. this._xCelestialPoleOffsetRadiansColumn = -1;
  17510. this._yCelestialPoleOffsetRadiansColumn = -1;
  17511. this._taiMinusUtcSecondsColumn = -1;
  17512. this._columnCount = 0;
  17513. this._lastIndex = -1;
  17514. this._downloadPromise = undefined;
  17515. this._dataError = undefined;
  17516. this._addNewLeapSeconds = defaultValue(options.addNewLeapSeconds, true);
  17517. if (defined(options.data)) {
  17518. // Use supplied EOP data.
  17519. onDataReady(this, options.data);
  17520. } else if (defined(options.url)) {
  17521. // Download EOP data.
  17522. var that = this;
  17523. this._downloadPromise = when(loadJson(options.url), function(eopData) {
  17524. onDataReady(that, eopData);
  17525. }, function() {
  17526. that._dataError = 'An error occurred while retrieving the EOP data from the URL ' + options.url + '.';
  17527. });
  17528. } else {
  17529. // Use all zeros for EOP data.
  17530. onDataReady(this, {
  17531. 'columnNames' : ['dateIso8601', 'modifiedJulianDateUtc', 'xPoleWanderRadians', 'yPoleWanderRadians', 'ut1MinusUtcSeconds', 'lengthOfDayCorrectionSeconds', 'xCelestialPoleOffsetRadians', 'yCelestialPoleOffsetRadians', 'taiMinusUtcSeconds'],
  17532. 'samples' : []
  17533. });
  17534. }
  17535. }
  17536. /**
  17537. * A default {@link EarthOrientationParameters} instance that returns zero for all EOP values.
  17538. */
  17539. EarthOrientationParameters.NONE = freezeObject({
  17540. getPromiseToLoad : function() {
  17541. return when();
  17542. },
  17543. compute : function(date, result) {
  17544. if (!defined(result)) {
  17545. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  17546. } else {
  17547. result.xPoleWander = 0.0;
  17548. result.yPoleWander = 0.0;
  17549. result.xPoleOffset = 0.0;
  17550. result.yPoleOffset = 0.0;
  17551. result.ut1MinusUtc = 0.0;
  17552. }
  17553. return result;
  17554. }
  17555. });
  17556. /**
  17557. * Gets a promise that, when resolved, indicates that the EOP data has been loaded and is
  17558. * ready to use.
  17559. *
  17560. * @returns {Promise.<undefined>} The promise.
  17561. *
  17562. * @see when
  17563. */
  17564. EarthOrientationParameters.prototype.getPromiseToLoad = function() {
  17565. return when(this._downloadPromise);
  17566. };
  17567. /**
  17568. * Computes the Earth Orientation Parameters (EOP) for a given date by interpolating.
  17569. * If the EOP data has not yet been download, this method returns undefined.
  17570. *
  17571. * @param {JulianDate} date The date for each to evaluate the EOP.
  17572. * @param {EarthOrientationParametersSample} [result] The instance to which to copy the result.
  17573. * If this parameter is undefined, a new instance is created and returned.
  17574. * @returns {EarthOrientationParametersSample} The EOP evaluated at the given date, or
  17575. * undefined if the data necessary to evaluate EOP at the date has not yet been
  17576. * downloaded.
  17577. *
  17578. * @exception {RuntimeError} The loaded EOP data has an error and cannot be used.
  17579. *
  17580. * @see EarthOrientationParameters#getPromiseToLoad
  17581. */
  17582. EarthOrientationParameters.prototype.compute = function(date, result) {
  17583. // We cannot compute until the samples are available.
  17584. if (!defined(this._samples)) {
  17585. if (defined(this._dataError)) {
  17586. throw new RuntimeError(this._dataError);
  17587. }
  17588. return undefined;
  17589. }
  17590. if (!defined(result)) {
  17591. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  17592. }
  17593. if (this._samples.length === 0) {
  17594. result.xPoleWander = 0.0;
  17595. result.yPoleWander = 0.0;
  17596. result.xPoleOffset = 0.0;
  17597. result.yPoleOffset = 0.0;
  17598. result.ut1MinusUtc = 0.0;
  17599. return result;
  17600. }
  17601. var dates = this._dates;
  17602. var lastIndex = this._lastIndex;
  17603. var before = 0;
  17604. var after = 0;
  17605. if (defined(lastIndex)) {
  17606. var previousIndexDate = dates[lastIndex];
  17607. var nextIndexDate = dates[lastIndex + 1];
  17608. var isAfterPrevious = JulianDate.lessThanOrEquals(previousIndexDate, date);
  17609. var isAfterLastSample = !defined(nextIndexDate);
  17610. var isBeforeNext = isAfterLastSample || JulianDate.greaterThanOrEquals(nextIndexDate, date);
  17611. if (isAfterPrevious && isBeforeNext) {
  17612. before = lastIndex;
  17613. if (!isAfterLastSample && nextIndexDate.equals(date)) {
  17614. ++before;
  17615. }
  17616. after = before + 1;
  17617. interpolate(this, dates, this._samples, date, before, after, result);
  17618. return result;
  17619. }
  17620. }
  17621. var index = binarySearch(dates, date, JulianDate.compare, this._dateColumn);
  17622. if (index >= 0) {
  17623. // If the next entry is the same date, use the later entry. This way, if two entries
  17624. // describe the same moment, one before a leap second and the other after, then we will use
  17625. // the post-leap second data.
  17626. if (index < dates.length - 1 && dates[index + 1].equals(date)) {
  17627. ++index;
  17628. }
  17629. before = index;
  17630. after = index;
  17631. } else {
  17632. after = ~index;
  17633. before = after - 1;
  17634. // Use the first entry if the date requested is before the beginning of the data.
  17635. if (before < 0) {
  17636. before = 0;
  17637. }
  17638. }
  17639. this._lastIndex = before;
  17640. interpolate(this, dates, this._samples, date, before, after, result);
  17641. return result;
  17642. };
  17643. function compareLeapSecondDates(leapSecond, dateToFind) {
  17644. return JulianDate.compare(leapSecond.julianDate, dateToFind);
  17645. }
  17646. function onDataReady(eop, eopData) {
  17647. if (!defined(eopData.columnNames)) {
  17648. eop._dataError = 'Error in loaded EOP data: The columnNames property is required.';
  17649. return;
  17650. }
  17651. if (!defined(eopData.samples)) {
  17652. eop._dataError = 'Error in loaded EOP data: The samples property is required.';
  17653. return;
  17654. }
  17655. var dateColumn = eopData.columnNames.indexOf('modifiedJulianDateUtc');
  17656. var xPoleWanderRadiansColumn = eopData.columnNames.indexOf('xPoleWanderRadians');
  17657. var yPoleWanderRadiansColumn = eopData.columnNames.indexOf('yPoleWanderRadians');
  17658. var ut1MinusUtcSecondsColumn = eopData.columnNames.indexOf('ut1MinusUtcSeconds');
  17659. var xCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf('xCelestialPoleOffsetRadians');
  17660. var yCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf('yCelestialPoleOffsetRadians');
  17661. var taiMinusUtcSecondsColumn = eopData.columnNames.indexOf('taiMinusUtcSeconds');
  17662. if (dateColumn < 0 || xPoleWanderRadiansColumn < 0 || yPoleWanderRadiansColumn < 0 || ut1MinusUtcSecondsColumn < 0 || xCelestialPoleOffsetRadiansColumn < 0 || yCelestialPoleOffsetRadiansColumn < 0 || taiMinusUtcSecondsColumn < 0) {
  17663. eop._dataError = 'Error in loaded EOP data: The columnNames property must include modifiedJulianDateUtc, xPoleWanderRadians, yPoleWanderRadians, ut1MinusUtcSeconds, xCelestialPoleOffsetRadians, yCelestialPoleOffsetRadians, and taiMinusUtcSeconds columns';
  17664. return;
  17665. }
  17666. var samples = eop._samples = eopData.samples;
  17667. var dates = eop._dates = [];
  17668. eop._dateColumn = dateColumn;
  17669. eop._xPoleWanderRadiansColumn = xPoleWanderRadiansColumn;
  17670. eop._yPoleWanderRadiansColumn = yPoleWanderRadiansColumn;
  17671. eop._ut1MinusUtcSecondsColumn = ut1MinusUtcSecondsColumn;
  17672. eop._xCelestialPoleOffsetRadiansColumn = xCelestialPoleOffsetRadiansColumn;
  17673. eop._yCelestialPoleOffsetRadiansColumn = yCelestialPoleOffsetRadiansColumn;
  17674. eop._taiMinusUtcSecondsColumn = taiMinusUtcSecondsColumn;
  17675. eop._columnCount = eopData.columnNames.length;
  17676. eop._lastIndex = undefined;
  17677. var lastTaiMinusUtc;
  17678. var addNewLeapSeconds = eop._addNewLeapSeconds;
  17679. // Convert the ISO8601 dates to JulianDates.
  17680. for (var i = 0, len = samples.length; i < len; i += eop._columnCount) {
  17681. var mjd = samples[i + dateColumn];
  17682. var taiMinusUtc = samples[i + taiMinusUtcSecondsColumn];
  17683. var day = mjd + TimeConstants.MODIFIED_JULIAN_DATE_DIFFERENCE;
  17684. var date = new JulianDate(day, taiMinusUtc, TimeStandard.TAI);
  17685. dates.push(date);
  17686. if (addNewLeapSeconds) {
  17687. if (taiMinusUtc !== lastTaiMinusUtc && defined(lastTaiMinusUtc)) {
  17688. // We crossed a leap second boundary, so add the leap second
  17689. // if it does not already exist.
  17690. var leapSeconds = JulianDate.leapSeconds;
  17691. var leapSecondIndex = binarySearch(leapSeconds, date, compareLeapSecondDates);
  17692. if (leapSecondIndex < 0) {
  17693. var leapSecond = new LeapSecond(date, taiMinusUtc);
  17694. leapSeconds.splice(~leapSecondIndex, 0, leapSecond);
  17695. }
  17696. }
  17697. lastTaiMinusUtc = taiMinusUtc;
  17698. }
  17699. }
  17700. }
  17701. function fillResultFromIndex(eop, samples, index, columnCount, result) {
  17702. var start = index * columnCount;
  17703. result.xPoleWander = samples[start + eop._xPoleWanderRadiansColumn];
  17704. result.yPoleWander = samples[start + eop._yPoleWanderRadiansColumn];
  17705. result.xPoleOffset = samples[start + eop._xCelestialPoleOffsetRadiansColumn];
  17706. result.yPoleOffset = samples[start + eop._yCelestialPoleOffsetRadiansColumn];
  17707. result.ut1MinusUtc = samples[start + eop._ut1MinusUtcSecondsColumn];
  17708. }
  17709. function linearInterp(dx, y1, y2) {
  17710. return y1 + dx * (y2 - y1);
  17711. }
  17712. function interpolate(eop, dates, samples, date, before, after, result) {
  17713. var columnCount = eop._columnCount;
  17714. // First check the bounds on the EOP data
  17715. // If we are after the bounds of the data, return zeros.
  17716. // The 'before' index should never be less than zero.
  17717. if (after > dates.length - 1) {
  17718. result.xPoleWander = 0;
  17719. result.yPoleWander = 0;
  17720. result.xPoleOffset = 0;
  17721. result.yPoleOffset = 0;
  17722. result.ut1MinusUtc = 0;
  17723. return result;
  17724. }
  17725. var beforeDate = dates[before];
  17726. var afterDate = dates[after];
  17727. if (beforeDate.equals(afterDate) || date.equals(beforeDate)) {
  17728. fillResultFromIndex(eop, samples, before, columnCount, result);
  17729. return result;
  17730. } else if (date.equals(afterDate)) {
  17731. fillResultFromIndex(eop, samples, after, columnCount, result);
  17732. return result;
  17733. }
  17734. var factor = JulianDate.secondsDifference(date, beforeDate) / JulianDate.secondsDifference(afterDate, beforeDate);
  17735. var startBefore = before * columnCount;
  17736. var startAfter = after * columnCount;
  17737. // Handle UT1 leap second edge case
  17738. var beforeUt1MinusUtc = samples[startBefore + eop._ut1MinusUtcSecondsColumn];
  17739. var afterUt1MinusUtc = samples[startAfter + eop._ut1MinusUtcSecondsColumn];
  17740. var offsetDifference = afterUt1MinusUtc - beforeUt1MinusUtc;
  17741. if (offsetDifference > 0.5 || offsetDifference < -0.5) {
  17742. // The absolute difference between the values is more than 0.5, so we may have
  17743. // crossed a leap second. Check if this is the case and, if so, adjust the
  17744. // afterValue to account for the leap second. This way, our interpolation will
  17745. // produce reasonable results.
  17746. var beforeTaiMinusUtc = samples[startBefore + eop._taiMinusUtcSecondsColumn];
  17747. var afterTaiMinusUtc = samples[startAfter + eop._taiMinusUtcSecondsColumn];
  17748. if (beforeTaiMinusUtc !== afterTaiMinusUtc) {
  17749. if (afterDate.equals(date)) {
  17750. // If we are at the end of the leap second interval, take the second value
  17751. // Otherwise, the interpolation below will yield the wrong side of the
  17752. // discontinuity
  17753. // At the end of the leap second, we need to start accounting for the jump
  17754. beforeUt1MinusUtc = afterUt1MinusUtc;
  17755. } else {
  17756. // Otherwise, remove the leap second so that the interpolation is correct
  17757. afterUt1MinusUtc -= afterTaiMinusUtc - beforeTaiMinusUtc;
  17758. }
  17759. }
  17760. }
  17761. result.xPoleWander = linearInterp(factor, samples[startBefore + eop._xPoleWanderRadiansColumn], samples[startAfter + eop._xPoleWanderRadiansColumn]);
  17762. result.yPoleWander = linearInterp(factor, samples[startBefore + eop._yPoleWanderRadiansColumn], samples[startAfter + eop._yPoleWanderRadiansColumn]);
  17763. result.xPoleOffset = linearInterp(factor, samples[startBefore + eop._xCelestialPoleOffsetRadiansColumn], samples[startAfter + eop._xCelestialPoleOffsetRadiansColumn]);
  17764. result.yPoleOffset = linearInterp(factor, samples[startBefore + eop._yCelestialPoleOffsetRadiansColumn], samples[startAfter + eop._yCelestialPoleOffsetRadiansColumn]);
  17765. result.ut1MinusUtc = linearInterp(factor, beforeUt1MinusUtc, afterUt1MinusUtc);
  17766. return result;
  17767. }
  17768. return EarthOrientationParameters;
  17769. });
  17770. /*global define*/
  17771. define('Core/HeadingPitchRoll',[
  17772. './defaultValue',
  17773. './defined',
  17774. './DeveloperError',
  17775. './Math'
  17776. ], function(
  17777. defaultValue,
  17778. defined,
  17779. DeveloperError,
  17780. CesiumMath) {
  17781. "use strict";
  17782. /**
  17783. * A rotation expressed as a heading, pitch, and roll. Heading is the rotation about the
  17784. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  17785. * the positive x axis.
  17786. * @alias HeadingPitchRoll
  17787. * @constructor
  17788. *
  17789. * @param {Number} [heading=0.0] The heading component in radians.
  17790. * @param {Number} [pitch=0.0] The pitch component in radians.
  17791. * @param {Number} [roll=0.0] The roll component in radians.
  17792. */
  17793. function HeadingPitchRoll(heading, pitch, roll) {
  17794. this.heading = defaultValue(heading, 0.0);
  17795. this.pitch = defaultValue(pitch, 0.0);
  17796. this.roll = defaultValue(roll, 0.0);
  17797. }
  17798. /**
  17799. * Computes the heading, pitch and roll from a quaternion (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  17800. *
  17801. * @param {Quaternion} quaternion The quaternion from which to retrieve heading, pitch, and roll, all expressed in radians.
  17802. * @param {Quaternion} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  17803. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  17804. */
  17805. HeadingPitchRoll.fromQuaternion = function(quaternion, result) {
  17806. if (!defined(quaternion)) {
  17807. throw new DeveloperError('quaternion is required');
  17808. }
  17809. if (!defined(result)) {
  17810. result = new HeadingPitchRoll();
  17811. }
  17812. var test = 2 * (quaternion.w * quaternion.y - quaternion.z * quaternion.x);
  17813. var denominatorRoll = 1 - 2 * (quaternion.x * quaternion.x + quaternion.y * quaternion.y);
  17814. var numeratorRoll = 2 * (quaternion.w * quaternion.x + quaternion.y * quaternion.z);
  17815. var denominatorHeading = 1 - 2 * (quaternion.y * quaternion.y + quaternion.z * quaternion.z);
  17816. var numeratorHeading = 2 * (quaternion.w * quaternion.z + quaternion.x * quaternion.y);
  17817. result.heading = -Math.atan2(numeratorHeading, denominatorHeading);
  17818. result.roll = Math.atan2(numeratorRoll, denominatorRoll);
  17819. result.pitch = -Math.asin(test);
  17820. return result;
  17821. };
  17822. /**
  17823. * Returns a new HeadingPitchRoll instance from angles given in degrees.
  17824. *
  17825. * @param {Number} heading the heading in degrees
  17826. * @param {Number} pitch the pitch in degrees
  17827. * @param {Number} roll the heading in degrees
  17828. * @param {HeadingPitchRoll} [result] The object in which to store the result. If not provided, a new instance is created and returned.
  17829. * @returns {HeadingPitchRoll} A new HeadingPitchRoll instance
  17830. */
  17831. HeadingPitchRoll.fromDegrees = function(heading, pitch, roll, result) {
  17832. if (!defined(heading)) {
  17833. throw new DeveloperError('heading is required');
  17834. }
  17835. if (!defined(pitch)) {
  17836. throw new DeveloperError('pitch is required');
  17837. }
  17838. if (!defined(roll)) {
  17839. throw new DeveloperError('roll is required');
  17840. }
  17841. if (!defined(result)) {
  17842. result = new HeadingPitchRoll();
  17843. }
  17844. result.heading = heading * CesiumMath.RADIANS_PER_DEGREE;
  17845. result.pitch = pitch * CesiumMath.RADIANS_PER_DEGREE;
  17846. result.roll = roll * CesiumMath.RADIANS_PER_DEGREE;
  17847. return result;
  17848. };
  17849. /**
  17850. * Duplicates a HeadingPitchRoll instance.
  17851. *
  17852. * @param {HeadingPitchRoll} headingPitchRoll The HeadingPitchRoll to duplicate.
  17853. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  17854. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided. (Returns undefined if headingPitchRoll is undefined)
  17855. */
  17856. HeadingPitchRoll.clone = function(headingPitchRoll, result) {
  17857. if (!defined(headingPitchRoll)) {
  17858. return undefined;
  17859. }
  17860. if (!defined(result)) {
  17861. return new HeadingPitchRoll(headingPitchRoll.heading, headingPitchRoll.pitch, headingPitchRoll.roll);
  17862. }
  17863. result.heading = headingPitchRoll.heading;
  17864. result.pitch = headingPitchRoll.pitch;
  17865. result.roll = headingPitchRoll.roll;
  17866. return result;
  17867. };
  17868. /**
  17869. * Compares the provided HeadingPitchRolls componentwise and returns
  17870. * <code>true</code> if they are equal, <code>false</code> otherwise.
  17871. *
  17872. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  17873. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  17874. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  17875. */
  17876. HeadingPitchRoll.equals = function(left, right) {
  17877. return (left === right) ||
  17878. ((defined(left)) &&
  17879. (defined(right)) &&
  17880. (left.heading === right.heading) &&
  17881. (left.pitch === right.pitch) &&
  17882. (left.roll === right.roll));
  17883. };
  17884. /**
  17885. * Compares the provided HeadingPitchRolls componentwise and returns
  17886. * <code>true</code> if they pass an absolute or relative tolerance test,
  17887. * <code>false</code> otherwise.
  17888. *
  17889. * @param {HeadingPitchRoll} [left] The first HeadingPitchRoll.
  17890. * @param {HeadingPitchRoll} [right] The second HeadingPitchRoll.
  17891. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  17892. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  17893. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  17894. */
  17895. HeadingPitchRoll.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  17896. return (left === right) ||
  17897. (defined(left) &&
  17898. defined(right) &&
  17899. CesiumMath.equalsEpsilon(left.heading, right.heading, relativeEpsilon, absoluteEpsilon) &&
  17900. CesiumMath.equalsEpsilon(left.pitch, right.pitch, relativeEpsilon, absoluteEpsilon) &&
  17901. CesiumMath.equalsEpsilon(left.roll, right.roll, relativeEpsilon, absoluteEpsilon));
  17902. };
  17903. /**
  17904. * Duplicates this HeadingPitchRoll instance.
  17905. *
  17906. * @param {HeadingPitchRoll} [result] The object onto which to store the result.
  17907. * @returns {HeadingPitchRoll} The modified result parameter or a new HeadingPitchRoll instance if one was not provided.
  17908. */
  17909. HeadingPitchRoll.prototype.clone = function(result) {
  17910. return HeadingPitchRoll.clone(this, result);
  17911. };
  17912. /**
  17913. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  17914. * <code>true</code> if they are equal, <code>false</code> otherwise.
  17915. *
  17916. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  17917. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  17918. */
  17919. HeadingPitchRoll.prototype.equals = function(right) {
  17920. return HeadingPitchRoll.equals(this, right);
  17921. };
  17922. /**
  17923. * Compares this HeadingPitchRoll against the provided HeadingPitchRoll componentwise and returns
  17924. * <code>true</code> if they pass an absolute or relative tolerance test,
  17925. * <code>false</code> otherwise.
  17926. *
  17927. * @param {HeadingPitchRoll} [right] The right hand side HeadingPitchRoll.
  17928. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  17929. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  17930. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  17931. */
  17932. HeadingPitchRoll.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  17933. return HeadingPitchRoll.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  17934. };
  17935. /**
  17936. * Creates a string representing this HeadingPitchRoll in the format '(heading, pitch, roll)' in radians.
  17937. *
  17938. * @returns {String} A string representing the provided HeadingPitchRoll in the format '(heading, pitch, roll)'.
  17939. */
  17940. HeadingPitchRoll.prototype.toString = function() {
  17941. return '(' + this.heading + ', ' + this.pitch + ', ' + this.roll + ')';
  17942. };
  17943. return HeadingPitchRoll;
  17944. });
  17945. /*global define*/
  17946. define('Core/getAbsoluteUri',[
  17947. '../ThirdParty/Uri',
  17948. './defaultValue',
  17949. './defined',
  17950. './DeveloperError'
  17951. ], function(
  17952. Uri,
  17953. defaultValue,
  17954. defined,
  17955. DeveloperError) {
  17956. 'use strict';
  17957. /**
  17958. * Given a relative Uri and a base Uri, returns the absolute Uri of the relative Uri.
  17959. * @exports getAbsoluteUri
  17960. *
  17961. * @param {String} relative The relative Uri.
  17962. * @param {String} [base] The base Uri.
  17963. * @returns {String} The absolute Uri of the given relative Uri.
  17964. *
  17965. * @example
  17966. * //absolute Uri will be "https://test.com/awesome.png";
  17967. * var absoluteUri = Cesium.getAbsoluteUri('awesome.png', 'https://test.com');
  17968. */
  17969. function getAbsoluteUri(relative, base) {
  17970. if (!defined(relative)) {
  17971. throw new DeveloperError('relative uri is required.');
  17972. }
  17973. base = defaultValue(base, document.location.href);
  17974. var baseUri = new Uri(base);
  17975. var relativeUri = new Uri(relative);
  17976. return relativeUri.resolve(baseUri).toString();
  17977. }
  17978. return getAbsoluteUri;
  17979. });
  17980. /*global define*/
  17981. define('Core/joinUrls',[
  17982. '../ThirdParty/Uri',
  17983. './defaultValue',
  17984. './defined',
  17985. './DeveloperError'
  17986. ], function(
  17987. Uri,
  17988. defaultValue,
  17989. defined,
  17990. DeveloperError) {
  17991. 'use strict';
  17992. /**
  17993. * Function for joining URLs in a manner that is aware of query strings and fragments.
  17994. * This is useful when the base URL has a query string that needs to be maintained
  17995. * (e.g. a presigned base URL).
  17996. * @param {String|Uri} first The base URL.
  17997. * @param {String|Uri} second The URL path to join to the base URL. If this URL is absolute, it is returned unmodified.
  17998. * @param {Boolean} [appendSlash=true] The boolean determining whether there should be a forward slash between first and second.
  17999. * @private
  18000. */
  18001. function joinUrls(first, second, appendSlash) {
  18002. if (!defined(first)) {
  18003. throw new DeveloperError('first is required');
  18004. }
  18005. if (!defined(second)) {
  18006. throw new DeveloperError('second is required');
  18007. }
  18008. appendSlash = defaultValue(appendSlash, true);
  18009. if (!(first instanceof Uri)) {
  18010. first = new Uri(first);
  18011. }
  18012. if (!(second instanceof Uri)) {
  18013. second = new Uri(second);
  18014. }
  18015. // Uri.isAbsolute returns false for a URL like '//foo.com'. So if we have an authority but
  18016. // not a scheme, add a scheme matching the page's scheme.
  18017. if (defined(second.authority) && !defined(second.scheme)) {
  18018. if (typeof document !== 'undefined' && defined(document.location) && defined(document.location.href)) {
  18019. second.scheme = new Uri(document.location.href).scheme;
  18020. } else {
  18021. // Not in a browser? Use the first URL's scheme instead.
  18022. second.scheme = first.scheme;
  18023. }
  18024. }
  18025. // If the second URL is absolute, use it for the scheme, authority, and path.
  18026. var baseUri = first;
  18027. if (second.isAbsolute()) {
  18028. baseUri = second;
  18029. }
  18030. var url = '';
  18031. if (defined(baseUri.scheme)) {
  18032. url += baseUri.scheme + ':';
  18033. }
  18034. if (defined(baseUri.authority)) {
  18035. url += '//' + baseUri.authority;
  18036. if (baseUri.path !== '' && baseUri.path !== '/') {
  18037. url = url.replace(/\/?$/, '/');
  18038. baseUri.path = baseUri.path.replace(/^\/?/g, '');
  18039. }
  18040. }
  18041. // Combine the paths (only if second is relative).
  18042. if (baseUri === first) {
  18043. if (appendSlash) {
  18044. url += first.path.replace(/\/?$/, '/') + second.path.replace(/^\/?/g, '');
  18045. } else {
  18046. url += first.path + second.path;
  18047. }
  18048. } else {
  18049. url += second.path;
  18050. }
  18051. // Combine the queries and fragments.
  18052. var hasFirstQuery = defined(first.query);
  18053. var hasSecondQuery = defined(second.query);
  18054. if (hasFirstQuery && hasSecondQuery) {
  18055. url += '?' + first.query + '&' + second.query;
  18056. } else if (hasFirstQuery && !hasSecondQuery) {
  18057. url += '?' + first.query;
  18058. } else if (!hasFirstQuery && hasSecondQuery) {
  18059. url += '?' + second.query;
  18060. }
  18061. var hasSecondFragment = defined(second.fragment);
  18062. if (defined(first.fragment) && !hasSecondFragment) {
  18063. url += '#' + first.fragment;
  18064. } else if (hasSecondFragment) {
  18065. url += '#' + second.fragment;
  18066. }
  18067. return url;
  18068. }
  18069. return joinUrls;
  18070. });
  18071. /*global define*/
  18072. define('Core/buildModuleUrl',[
  18073. '../ThirdParty/Uri',
  18074. './defined',
  18075. './DeveloperError',
  18076. './getAbsoluteUri',
  18077. './joinUrls',
  18078. 'require'
  18079. ], function(
  18080. Uri,
  18081. defined,
  18082. DeveloperError,
  18083. getAbsoluteUri,
  18084. joinUrls,
  18085. require) {
  18086. 'use strict';
  18087. /*global CESIUM_BASE_URL*/
  18088. var cesiumScriptRegex = /((?:.*\/)|^)cesium[\w-]*\.js(?:\W|$)/i;
  18089. function getBaseUrlFromCesiumScript() {
  18090. var scripts = document.getElementsByTagName('script');
  18091. for ( var i = 0, len = scripts.length; i < len; ++i) {
  18092. var src = scripts[i].getAttribute('src');
  18093. var result = cesiumScriptRegex.exec(src);
  18094. if (result !== null) {
  18095. return result[1];
  18096. }
  18097. }
  18098. return undefined;
  18099. }
  18100. var baseUrl;
  18101. function getCesiumBaseUrl() {
  18102. if (defined(baseUrl)) {
  18103. return baseUrl;
  18104. }
  18105. var baseUrlString;
  18106. if (typeof CESIUM_BASE_URL !== 'undefined') {
  18107. baseUrlString = CESIUM_BASE_URL;
  18108. } else {
  18109. baseUrlString = getBaseUrlFromCesiumScript();
  18110. }
  18111. if (!defined(baseUrlString)) {
  18112. throw new DeveloperError('Unable to determine Cesium base URL automatically, try defining a global variable called CESIUM_BASE_URL.');
  18113. }
  18114. baseUrl = new Uri(getAbsoluteUri(baseUrlString));
  18115. return baseUrl;
  18116. }
  18117. function buildModuleUrlFromRequireToUrl(moduleID) {
  18118. //moduleID will be non-relative, so require it relative to this module, in Core.
  18119. return require.toUrl('../' + moduleID);
  18120. }
  18121. function buildModuleUrlFromBaseUrl(moduleID) {
  18122. return joinUrls(getCesiumBaseUrl(), moduleID);
  18123. }
  18124. var implementation;
  18125. var a;
  18126. /**
  18127. * Given a non-relative moduleID, returns an absolute URL to the file represented by that module ID,
  18128. * using, in order of preference, require.toUrl, the value of a global CESIUM_BASE_URL, or
  18129. * the base URL of the Cesium.js script.
  18130. *
  18131. * @private
  18132. */
  18133. function buildModuleUrl(moduleID) {
  18134. if (!defined(implementation)) {
  18135. //select implementation
  18136. if (defined(require.toUrl)) {
  18137. implementation = buildModuleUrlFromRequireToUrl;
  18138. } else {
  18139. implementation = buildModuleUrlFromBaseUrl;
  18140. }
  18141. }
  18142. if (!defined(a)) {
  18143. a = document.createElement('a');
  18144. }
  18145. var url = implementation(moduleID);
  18146. a.href = url;
  18147. a.href = a.href; // IE only absolutizes href on get, not set
  18148. return a.href;
  18149. }
  18150. // exposed for testing
  18151. buildModuleUrl._cesiumScriptRegex = cesiumScriptRegex;
  18152. /**
  18153. * Sets the base URL for resolving modules.
  18154. * @param {String} value The new base URL.
  18155. */
  18156. buildModuleUrl.setBaseUrl = function(value) {
  18157. baseUrl = new Uri(value).resolve(new Uri(document.location.href));
  18158. };
  18159. return buildModuleUrl;
  18160. });
  18161. /*global define*/
  18162. define('Core/Iau2006XysSample',[],function() {
  18163. 'use strict';
  18164. /**
  18165. * An IAU 2006 XYS value sampled at a particular time.
  18166. *
  18167. * @alias Iau2006XysSample
  18168. * @constructor
  18169. *
  18170. * @param {Number} x The X value.
  18171. * @param {Number} y The Y value.
  18172. * @param {Number} s The S value.
  18173. *
  18174. * @private
  18175. */
  18176. function Iau2006XysSample(x, y, s) {
  18177. /**
  18178. * The X value.
  18179. * @type {Number}
  18180. */
  18181. this.x = x;
  18182. /**
  18183. * The Y value.
  18184. * @type {Number}
  18185. */
  18186. this.y = y;
  18187. /**
  18188. * The S value.
  18189. * @type {Number}
  18190. */
  18191. this.s = s;
  18192. }
  18193. return Iau2006XysSample;
  18194. });
  18195. /*global define*/
  18196. define('Core/Iau2006XysData',[
  18197. '../ThirdParty/when',
  18198. './buildModuleUrl',
  18199. './defaultValue',
  18200. './defined',
  18201. './Iau2006XysSample',
  18202. './JulianDate',
  18203. './loadJson',
  18204. './TimeStandard'
  18205. ], function(
  18206. when,
  18207. buildModuleUrl,
  18208. defaultValue,
  18209. defined,
  18210. Iau2006XysSample,
  18211. JulianDate,
  18212. loadJson,
  18213. TimeStandard) {
  18214. 'use strict';
  18215. /**
  18216. * A set of IAU2006 XYS data that is used to evaluate the transformation between the International
  18217. * Celestial Reference Frame (ICRF) and the International Terrestrial Reference Frame (ITRF).
  18218. *
  18219. * @alias Iau2006XysData
  18220. * @constructor
  18221. *
  18222. * @param {Object} [options] Object with the following properties:
  18223. * @param {String} [options.xysFileUrlTemplate='Assets/IAU2006_XYS/IAU2006_XYS_{0}.json'] A template URL for obtaining the XYS data. In the template,
  18224. * `{0}` will be replaced with the file index.
  18225. * @param {Number} [options.interpolationOrder=9] The order of interpolation to perform on the XYS data.
  18226. * @param {Number} [options.sampleZeroJulianEphemerisDate=2442396.5] The Julian ephemeris date (JED) of the
  18227. * first XYS sample.
  18228. * @param {Number} [options.stepSizeDays=1.0] The step size, in days, between successive XYS samples.
  18229. * @param {Number} [options.samplesPerXysFile=1000] The number of samples in each XYS file.
  18230. * @param {Number} [options.totalSamples=27426] The total number of samples in all XYS files.
  18231. *
  18232. * @private
  18233. */
  18234. function Iau2006XysData(options) {
  18235. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  18236. this._xysFileUrlTemplate = options.xysFileUrlTemplate;
  18237. this._interpolationOrder = defaultValue(options.interpolationOrder, 9);
  18238. this._sampleZeroJulianEphemerisDate = defaultValue(options.sampleZeroJulianEphemerisDate, 2442396.5);
  18239. this._sampleZeroDateTT = new JulianDate(this._sampleZeroJulianEphemerisDate, 0.0, TimeStandard.TAI);
  18240. this._stepSizeDays = defaultValue(options.stepSizeDays, 1.0);
  18241. this._samplesPerXysFile = defaultValue(options.samplesPerXysFile, 1000);
  18242. this._totalSamples = defaultValue(options.totalSamples, 27426);
  18243. this._samples = new Array(this._totalSamples * 3);
  18244. this._chunkDownloadsInProgress = [];
  18245. var order = this._interpolationOrder;
  18246. // Compute denominators and X values for interpolation.
  18247. var denom = this._denominators = new Array(order + 1);
  18248. var xTable = this._xTable = new Array(order + 1);
  18249. var stepN = Math.pow(this._stepSizeDays, order);
  18250. for ( var i = 0; i <= order; ++i) {
  18251. denom[i] = stepN;
  18252. xTable[i] = i * this._stepSizeDays;
  18253. for ( var j = 0; j <= order; ++j) {
  18254. if (j !== i) {
  18255. denom[i] *= (i - j);
  18256. }
  18257. }
  18258. denom[i] = 1.0 / denom[i];
  18259. }
  18260. // Allocate scratch arrays for interpolation.
  18261. this._work = new Array(order + 1);
  18262. this._coef = new Array(order + 1);
  18263. }
  18264. var julianDateScratch = new JulianDate(0, 0.0, TimeStandard.TAI);
  18265. function getDaysSinceEpoch(xys, dayTT, secondTT) {
  18266. var dateTT = julianDateScratch;
  18267. dateTT.dayNumber = dayTT;
  18268. dateTT.secondsOfDay = secondTT;
  18269. return JulianDate.daysDifference(dateTT, xys._sampleZeroDateTT);
  18270. }
  18271. /**
  18272. * Preloads XYS data for a specified date range.
  18273. *
  18274. * @param {Number} startDayTT The Julian day number of the beginning of the interval to preload, expressed in
  18275. * the Terrestrial Time (TT) time standard.
  18276. * @param {Number} startSecondTT The seconds past noon of the beginning of the interval to preload, expressed in
  18277. * the Terrestrial Time (TT) time standard.
  18278. * @param {Number} stopDayTT The Julian day number of the end of the interval to preload, expressed in
  18279. * the Terrestrial Time (TT) time standard.
  18280. * @param {Number} stopSecondTT The seconds past noon of the end of the interval to preload, expressed in
  18281. * the Terrestrial Time (TT) time standard.
  18282. * @returns {Promise.<undefined>} A promise that, when resolved, indicates that the requested interval has been
  18283. * preloaded.
  18284. */
  18285. Iau2006XysData.prototype.preload = function(startDayTT, startSecondTT, stopDayTT, stopSecondTT) {
  18286. var startDaysSinceEpoch = getDaysSinceEpoch(this, startDayTT, startSecondTT);
  18287. var stopDaysSinceEpoch = getDaysSinceEpoch(this, stopDayTT, stopSecondTT);
  18288. var startIndex = (startDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) | 0;
  18289. if (startIndex < 0) {
  18290. startIndex = 0;
  18291. }
  18292. var stopIndex = (stopDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) | 0 + this._interpolationOrder;
  18293. if (stopIndex >= this._totalSamples) {
  18294. stopIndex = this._totalSamples - 1;
  18295. }
  18296. var startChunk = (startIndex / this._samplesPerXysFile) | 0;
  18297. var stopChunk = (stopIndex / this._samplesPerXysFile) | 0;
  18298. var promises = [];
  18299. for ( var i = startChunk; i <= stopChunk; ++i) {
  18300. promises.push(requestXysChunk(this, i));
  18301. }
  18302. return when.all(promises);
  18303. };
  18304. /**
  18305. * Computes the XYS values for a given date by interpolating. If the required data is not yet downloaded,
  18306. * this method will return undefined.
  18307. *
  18308. * @param {Number} dayTT The Julian day number for which to compute the XYS value, expressed in
  18309. * the Terrestrial Time (TT) time standard.
  18310. * @param {Number} secondTT The seconds past noon of the date for which to compute the XYS value, expressed in
  18311. * the Terrestrial Time (TT) time standard.
  18312. * @param {Iau2006XysSample} [result] The instance to which to copy the interpolated result. If this parameter
  18313. * is undefined, a new instance is allocated and returned.
  18314. * @returns {Iau2006XysSample} The interpolated XYS values, or undefined if the required data for this
  18315. * computation has not yet been downloaded.
  18316. *
  18317. * @see Iau2006XysData#preload
  18318. */
  18319. Iau2006XysData.prototype.computeXysRadians = function(dayTT, secondTT, result) {
  18320. var daysSinceEpoch = getDaysSinceEpoch(this, dayTT, secondTT);
  18321. if (daysSinceEpoch < 0.0) {
  18322. // Can't evaluate prior to the epoch of the data.
  18323. return undefined;
  18324. }
  18325. var centerIndex = (daysSinceEpoch / this._stepSizeDays) | 0;
  18326. if (centerIndex >= this._totalSamples) {
  18327. // Can't evaluate after the last sample in the data.
  18328. return undefined;
  18329. }
  18330. var degree = this._interpolationOrder;
  18331. var firstIndex = centerIndex - ((degree / 2) | 0);
  18332. if (firstIndex < 0) {
  18333. firstIndex = 0;
  18334. }
  18335. var lastIndex = firstIndex + degree;
  18336. if (lastIndex >= this._totalSamples) {
  18337. lastIndex = this._totalSamples - 1;
  18338. firstIndex = lastIndex - degree;
  18339. if (firstIndex < 0) {
  18340. firstIndex = 0;
  18341. }
  18342. }
  18343. // Are all the samples we need present?
  18344. // We can assume so if the first and last are present
  18345. var isDataMissing = false;
  18346. var samples = this._samples;
  18347. if (!defined(samples[firstIndex * 3])) {
  18348. requestXysChunk(this, (firstIndex / this._samplesPerXysFile) | 0);
  18349. isDataMissing = true;
  18350. }
  18351. if (!defined(samples[lastIndex * 3])) {
  18352. requestXysChunk(this, (lastIndex / this._samplesPerXysFile) | 0);
  18353. isDataMissing = true;
  18354. }
  18355. if (isDataMissing) {
  18356. return undefined;
  18357. }
  18358. if (!defined(result)) {
  18359. result = new Iau2006XysSample(0.0, 0.0, 0.0);
  18360. } else {
  18361. result.x = 0.0;
  18362. result.y = 0.0;
  18363. result.s = 0.0;
  18364. }
  18365. var x = daysSinceEpoch - firstIndex * this._stepSizeDays;
  18366. var work = this._work;
  18367. var denom = this._denominators;
  18368. var coef = this._coef;
  18369. var xTable = this._xTable;
  18370. var i, j;
  18371. for (i = 0; i <= degree; ++i) {
  18372. work[i] = x - xTable[i];
  18373. }
  18374. for (i = 0; i <= degree; ++i) {
  18375. coef[i] = 1.0;
  18376. for (j = 0; j <= degree; ++j) {
  18377. if (j !== i) {
  18378. coef[i] *= work[j];
  18379. }
  18380. }
  18381. coef[i] *= denom[i];
  18382. var sampleIndex = (firstIndex + i) * 3;
  18383. result.x += coef[i] * samples[sampleIndex++];
  18384. result.y += coef[i] * samples[sampleIndex++];
  18385. result.s += coef[i] * samples[sampleIndex];
  18386. }
  18387. return result;
  18388. };
  18389. function requestXysChunk(xysData, chunkIndex) {
  18390. if (xysData._chunkDownloadsInProgress[chunkIndex]) {
  18391. // Chunk has already been requested.
  18392. return xysData._chunkDownloadsInProgress[chunkIndex];
  18393. }
  18394. var deferred = when.defer();
  18395. xysData._chunkDownloadsInProgress[chunkIndex] = deferred;
  18396. var chunkUrl;
  18397. var xysFileUrlTemplate = xysData._xysFileUrlTemplate;
  18398. if (defined(xysFileUrlTemplate)) {
  18399. chunkUrl = xysFileUrlTemplate.replace('{0}', chunkIndex);
  18400. } else {
  18401. chunkUrl = buildModuleUrl('Assets/IAU2006_XYS/IAU2006_XYS_' + chunkIndex + '.json');
  18402. }
  18403. when(loadJson(chunkUrl), function(chunk) {
  18404. xysData._chunkDownloadsInProgress[chunkIndex] = false;
  18405. var samples = xysData._samples;
  18406. var newSamples = chunk.samples;
  18407. var startIndex = chunkIndex * xysData._samplesPerXysFile * 3;
  18408. for ( var i = 0, len = newSamples.length; i < len; ++i) {
  18409. samples[startIndex + i] = newSamples[i];
  18410. }
  18411. deferred.resolve();
  18412. });
  18413. return deferred.promise;
  18414. }
  18415. return Iau2006XysData;
  18416. });
  18417. /*global define*/
  18418. define('Core/Quaternion',[
  18419. './Cartesian3',
  18420. './defaultValue',
  18421. './defined',
  18422. './DeveloperError',
  18423. './FeatureDetection',
  18424. './freezeObject',
  18425. './Math',
  18426. './Matrix3'
  18427. ], function(
  18428. Cartesian3,
  18429. defaultValue,
  18430. defined,
  18431. DeveloperError,
  18432. FeatureDetection,
  18433. freezeObject,
  18434. CesiumMath,
  18435. Matrix3) {
  18436. 'use strict';
  18437. /**
  18438. * A set of 4-dimensional coordinates used to represent rotation in 3-dimensional space.
  18439. * @alias Quaternion
  18440. * @constructor
  18441. *
  18442. * @param {Number} [x=0.0] The X component.
  18443. * @param {Number} [y=0.0] The Y component.
  18444. * @param {Number} [z=0.0] The Z component.
  18445. * @param {Number} [w=0.0] The W component.
  18446. *
  18447. * @see PackableForInterpolation
  18448. */
  18449. function Quaternion(x, y, z, w) {
  18450. /**
  18451. * The X component.
  18452. * @type {Number}
  18453. * @default 0.0
  18454. */
  18455. this.x = defaultValue(x, 0.0);
  18456. /**
  18457. * The Y component.
  18458. * @type {Number}
  18459. * @default 0.0
  18460. */
  18461. this.y = defaultValue(y, 0.0);
  18462. /**
  18463. * The Z component.
  18464. * @type {Number}
  18465. * @default 0.0
  18466. */
  18467. this.z = defaultValue(z, 0.0);
  18468. /**
  18469. * The W component.
  18470. * @type {Number}
  18471. * @default 0.0
  18472. */
  18473. this.w = defaultValue(w, 0.0);
  18474. }
  18475. var fromAxisAngleScratch = new Cartesian3();
  18476. /**
  18477. * Computes a quaternion representing a rotation around an axis.
  18478. *
  18479. * @param {Cartesian3} axis The axis of rotation.
  18480. * @param {Number} angle The angle in radians to rotate around the axis.
  18481. * @param {Quaternion} [result] The object onto which to store the result.
  18482. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18483. */
  18484. Quaternion.fromAxisAngle = function(axis, angle, result) {
  18485. if (!defined(axis)) {
  18486. throw new DeveloperError('axis is required.');
  18487. }
  18488. if (typeof angle !== 'number') {
  18489. throw new DeveloperError('angle is required and must be a number.');
  18490. }
  18491. var halfAngle = angle / 2.0;
  18492. var s = Math.sin(halfAngle);
  18493. fromAxisAngleScratch = Cartesian3.normalize(axis, fromAxisAngleScratch);
  18494. var x = fromAxisAngleScratch.x * s;
  18495. var y = fromAxisAngleScratch.y * s;
  18496. var z = fromAxisAngleScratch.z * s;
  18497. var w = Math.cos(halfAngle);
  18498. if (!defined(result)) {
  18499. return new Quaternion(x, y, z, w);
  18500. }
  18501. result.x = x;
  18502. result.y = y;
  18503. result.z = z;
  18504. result.w = w;
  18505. return result;
  18506. };
  18507. var fromRotationMatrixNext = [1, 2, 0];
  18508. var fromRotationMatrixQuat = new Array(3);
  18509. /**
  18510. * Computes a Quaternion from the provided Matrix3 instance.
  18511. *
  18512. * @param {Matrix3} matrix The rotation matrix.
  18513. * @param {Quaternion} [result] The object onto which to store the result.
  18514. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18515. *
  18516. * @see Matrix3.fromQuaternion
  18517. */
  18518. Quaternion.fromRotationMatrix = function(matrix, result) {
  18519. if (!defined(matrix)) {
  18520. throw new DeveloperError('matrix is required.');
  18521. }
  18522. var root;
  18523. var x;
  18524. var y;
  18525. var z;
  18526. var w;
  18527. var m00 = matrix[Matrix3.COLUMN0ROW0];
  18528. var m11 = matrix[Matrix3.COLUMN1ROW1];
  18529. var m22 = matrix[Matrix3.COLUMN2ROW2];
  18530. var trace = m00 + m11 + m22;
  18531. if (trace > 0.0) {
  18532. // |w| > 1/2, may as well choose w > 1/2
  18533. root = Math.sqrt(trace + 1.0); // 2w
  18534. w = 0.5 * root;
  18535. root = 0.5 / root; // 1/(4w)
  18536. x = (matrix[Matrix3.COLUMN1ROW2] - matrix[Matrix3.COLUMN2ROW1]) * root;
  18537. y = (matrix[Matrix3.COLUMN2ROW0] - matrix[Matrix3.COLUMN0ROW2]) * root;
  18538. z = (matrix[Matrix3.COLUMN0ROW1] - matrix[Matrix3.COLUMN1ROW0]) * root;
  18539. } else {
  18540. // |w| <= 1/2
  18541. var next = fromRotationMatrixNext;
  18542. var i = 0;
  18543. if (m11 > m00) {
  18544. i = 1;
  18545. }
  18546. if (m22 > m00 && m22 > m11) {
  18547. i = 2;
  18548. }
  18549. var j = next[i];
  18550. var k = next[j];
  18551. root = Math.sqrt(matrix[Matrix3.getElementIndex(i, i)] - matrix[Matrix3.getElementIndex(j, j)] - matrix[Matrix3.getElementIndex(k, k)] + 1.0);
  18552. var quat = fromRotationMatrixQuat;
  18553. quat[i] = 0.5 * root;
  18554. root = 0.5 / root;
  18555. w = (matrix[Matrix3.getElementIndex(k, j)] - matrix[Matrix3.getElementIndex(j, k)]) * root;
  18556. quat[j] = (matrix[Matrix3.getElementIndex(j, i)] + matrix[Matrix3.getElementIndex(i, j)]) * root;
  18557. quat[k] = (matrix[Matrix3.getElementIndex(k, i)] + matrix[Matrix3.getElementIndex(i, k)]) * root;
  18558. x = -quat[0];
  18559. y = -quat[1];
  18560. z = -quat[2];
  18561. }
  18562. if (!defined(result)) {
  18563. return new Quaternion(x, y, z, w);
  18564. }
  18565. result.x = x;
  18566. result.y = y;
  18567. result.z = z;
  18568. result.w = w;
  18569. return result;
  18570. };
  18571. var scratchHPRQuaternion = new Quaternion();
  18572. /**
  18573. * Computes a rotation from the given heading, pitch and roll angles. Heading is the rotation about the
  18574. * negative z axis. Pitch is the rotation about the negative y axis. Roll is the rotation about
  18575. * the positive x axis.
  18576. *
  18577. * @param {Number} heading The heading angle in radians.
  18578. * @param {Number} pitch The pitch angle in radians.
  18579. * @param {Number} roll The roll angle in radians.
  18580. * @param {Quaternion} [result] The object onto which to store the result.
  18581. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  18582. */
  18583. Quaternion.fromHeadingPitchRoll = function(heading, pitch, roll, result) {
  18584. if (!defined(heading)) {
  18585. throw new DeveloperError('heading is required.');
  18586. }
  18587. if (!defined(pitch)) {
  18588. throw new DeveloperError('pitch is required.');
  18589. }
  18590. if (!defined(roll)) {
  18591. throw new DeveloperError('roll is required.');
  18592. }
  18593. var rollQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_X, roll, scratchHPRQuaternion);
  18594. var pitchQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_Y, -pitch, result);
  18595. result = Quaternion.multiply(pitchQuaternion, rollQuaternion, pitchQuaternion);
  18596. var headingQuaternion = Quaternion.fromAxisAngle(Cartesian3.UNIT_Z, -heading, scratchHPRQuaternion);
  18597. return Quaternion.multiply(headingQuaternion, result, result);
  18598. };
  18599. var sampledQuaternionAxis = new Cartesian3();
  18600. var sampledQuaternionRotation = new Cartesian3();
  18601. var sampledQuaternionTempQuaternion = new Quaternion();
  18602. var sampledQuaternionQuaternion0 = new Quaternion();
  18603. var sampledQuaternionQuaternion0Conjugate = new Quaternion();
  18604. /**
  18605. * The number of elements used to pack the object into an array.
  18606. * @type {Number}
  18607. */
  18608. Quaternion.packedLength = 4;
  18609. /**
  18610. * Stores the provided instance into the provided array.
  18611. *
  18612. * @param {Quaternion} value The value to pack.
  18613. * @param {Number[]} array The array to pack into.
  18614. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  18615. *
  18616. * @returns {Number[]} The array that was packed into
  18617. */
  18618. Quaternion.pack = function(value, array, startingIndex) {
  18619. if (!defined(value)) {
  18620. throw new DeveloperError('value is required');
  18621. }
  18622. if (!defined(array)) {
  18623. throw new DeveloperError('array is required');
  18624. }
  18625. startingIndex = defaultValue(startingIndex, 0);
  18626. array[startingIndex++] = value.x;
  18627. array[startingIndex++] = value.y;
  18628. array[startingIndex++] = value.z;
  18629. array[startingIndex] = value.w;
  18630. return array;
  18631. };
  18632. /**
  18633. * Retrieves an instance from a packed array.
  18634. *
  18635. * @param {Number[]} array The packed array.
  18636. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  18637. * @param {Quaternion} [result] The object into which to store the result.
  18638. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18639. */
  18640. Quaternion.unpack = function(array, startingIndex, result) {
  18641. if (!defined(array)) {
  18642. throw new DeveloperError('array is required');
  18643. }
  18644. startingIndex = defaultValue(startingIndex, 0);
  18645. if (!defined(result)) {
  18646. result = new Quaternion();
  18647. }
  18648. result.x = array[startingIndex];
  18649. result.y = array[startingIndex + 1];
  18650. result.z = array[startingIndex + 2];
  18651. result.w = array[startingIndex + 3];
  18652. return result;
  18653. };
  18654. /**
  18655. * The number of elements used to store the object into an array in its interpolatable form.
  18656. * @type {Number}
  18657. */
  18658. Quaternion.packedInterpolationLength = 3;
  18659. /**
  18660. * Converts a packed array into a form suitable for interpolation.
  18661. *
  18662. * @param {Number[]} packedArray The packed array.
  18663. * @param {Number} [startingIndex=0] The index of the first element to be converted.
  18664. * @param {Number} [lastIndex=packedArray.length] The index of the last element to be converted.
  18665. * @param {Number[]} result The object into which to store the result.
  18666. */
  18667. Quaternion.convertPackedArrayForInterpolation = function(packedArray, startingIndex, lastIndex, result) {
  18668. Quaternion.unpack(packedArray, lastIndex * 4, sampledQuaternionQuaternion0Conjugate);
  18669. Quaternion.conjugate(sampledQuaternionQuaternion0Conjugate, sampledQuaternionQuaternion0Conjugate);
  18670. for (var i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  18671. var offset = i * 3;
  18672. Quaternion.unpack(packedArray, (startingIndex + i) * 4, sampledQuaternionTempQuaternion);
  18673. Quaternion.multiply(sampledQuaternionTempQuaternion, sampledQuaternionQuaternion0Conjugate, sampledQuaternionTempQuaternion);
  18674. if (sampledQuaternionTempQuaternion.w < 0) {
  18675. Quaternion.negate(sampledQuaternionTempQuaternion, sampledQuaternionTempQuaternion);
  18676. }
  18677. Quaternion.computeAxis(sampledQuaternionTempQuaternion, sampledQuaternionAxis);
  18678. var angle = Quaternion.computeAngle(sampledQuaternionTempQuaternion);
  18679. result[offset] = sampledQuaternionAxis.x * angle;
  18680. result[offset + 1] = sampledQuaternionAxis.y * angle;
  18681. result[offset + 2] = sampledQuaternionAxis.z * angle;
  18682. }
  18683. };
  18684. /**
  18685. * Retrieves an instance from a packed array converted with {@link convertPackedArrayForInterpolation}.
  18686. *
  18687. * @param {Number[]} array The array previously packed for interpolation.
  18688. * @param {Number[]} sourceArray The original packed array.
  18689. * @param {Number} [startingIndex=0] The startingIndex used to convert the array.
  18690. * @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  18691. * @param {Quaternion} [result] The object into which to store the result.
  18692. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  18693. */
  18694. Quaternion.unpackInterpolationResult = function(array, sourceArray, firstIndex, lastIndex, result) {
  18695. if (!defined(result)) {
  18696. result = new Quaternion();
  18697. }
  18698. Cartesian3.fromArray(array, 0, sampledQuaternionRotation);
  18699. var magnitude = Cartesian3.magnitude(sampledQuaternionRotation);
  18700. Quaternion.unpack(sourceArray, lastIndex * 4, sampledQuaternionQuaternion0);
  18701. if (magnitude === 0) {
  18702. Quaternion.clone(Quaternion.IDENTITY, sampledQuaternionTempQuaternion);
  18703. } else {
  18704. Quaternion.fromAxisAngle(sampledQuaternionRotation, magnitude, sampledQuaternionTempQuaternion);
  18705. }
  18706. return Quaternion.multiply(sampledQuaternionTempQuaternion, sampledQuaternionQuaternion0, result);
  18707. };
  18708. /**
  18709. * Duplicates a Quaternion instance.
  18710. *
  18711. * @param {Quaternion} quaternion The quaternion to duplicate.
  18712. * @param {Quaternion} [result] The object onto which to store the result.
  18713. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided. (Returns undefined if quaternion is undefined)
  18714. */
  18715. Quaternion.clone = function(quaternion, result) {
  18716. if (!defined(quaternion)) {
  18717. return undefined;
  18718. }
  18719. if (!defined(result)) {
  18720. return new Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
  18721. }
  18722. result.x = quaternion.x;
  18723. result.y = quaternion.y;
  18724. result.z = quaternion.z;
  18725. result.w = quaternion.w;
  18726. return result;
  18727. };
  18728. /**
  18729. * Computes the conjugate of the provided quaternion.
  18730. *
  18731. * @param {Quaternion} quaternion The quaternion to conjugate.
  18732. * @param {Quaternion} result The object onto which to store the result.
  18733. * @returns {Quaternion} The modified result parameter.
  18734. */
  18735. Quaternion.conjugate = function(quaternion, result) {
  18736. if (!defined(quaternion)) {
  18737. throw new DeveloperError('quaternion is required');
  18738. }
  18739. if (!defined(result)) {
  18740. throw new DeveloperError('result is required');
  18741. }
  18742. result.x = -quaternion.x;
  18743. result.y = -quaternion.y;
  18744. result.z = -quaternion.z;
  18745. result.w = quaternion.w;
  18746. return result;
  18747. };
  18748. /**
  18749. * Computes magnitude squared for the provided quaternion.
  18750. *
  18751. * @param {Quaternion} quaternion The quaternion to conjugate.
  18752. * @returns {Number} The magnitude squared.
  18753. */
  18754. Quaternion.magnitudeSquared = function(quaternion) {
  18755. if (!defined(quaternion)) {
  18756. throw new DeveloperError('quaternion is required');
  18757. }
  18758. return quaternion.x * quaternion.x + quaternion.y * quaternion.y + quaternion.z * quaternion.z + quaternion.w * quaternion.w;
  18759. };
  18760. /**
  18761. * Computes magnitude for the provided quaternion.
  18762. *
  18763. * @param {Quaternion} quaternion The quaternion to conjugate.
  18764. * @returns {Number} The magnitude.
  18765. */
  18766. Quaternion.magnitude = function(quaternion) {
  18767. return Math.sqrt(Quaternion.magnitudeSquared(quaternion));
  18768. };
  18769. /**
  18770. * Computes the normalized form of the provided quaternion.
  18771. *
  18772. * @param {Quaternion} quaternion The quaternion to normalize.
  18773. * @param {Quaternion} result The object onto which to store the result.
  18774. * @returns {Quaternion} The modified result parameter.
  18775. */
  18776. Quaternion.normalize = function(quaternion, result) {
  18777. if (!defined(result)) {
  18778. throw new DeveloperError('result is required');
  18779. }
  18780. var inverseMagnitude = 1.0 / Quaternion.magnitude(quaternion);
  18781. var x = quaternion.x * inverseMagnitude;
  18782. var y = quaternion.y * inverseMagnitude;
  18783. var z = quaternion.z * inverseMagnitude;
  18784. var w = quaternion.w * inverseMagnitude;
  18785. result.x = x;
  18786. result.y = y;
  18787. result.z = z;
  18788. result.w = w;
  18789. return result;
  18790. };
  18791. /**
  18792. * Computes the inverse of the provided quaternion.
  18793. *
  18794. * @param {Quaternion} quaternion The quaternion to normalize.
  18795. * @param {Quaternion} result The object onto which to store the result.
  18796. * @returns {Quaternion} The modified result parameter.
  18797. */
  18798. Quaternion.inverse = function(quaternion, result) {
  18799. if (!defined(result)) {
  18800. throw new DeveloperError('result is required');
  18801. }
  18802. var magnitudeSquared = Quaternion.magnitudeSquared(quaternion);
  18803. result = Quaternion.conjugate(quaternion, result);
  18804. return Quaternion.multiplyByScalar(result, 1.0 / magnitudeSquared, result);
  18805. };
  18806. /**
  18807. * Computes the componentwise sum of two quaternions.
  18808. *
  18809. * @param {Quaternion} left The first quaternion.
  18810. * @param {Quaternion} right The second quaternion.
  18811. * @param {Quaternion} result The object onto which to store the result.
  18812. * @returns {Quaternion} The modified result parameter.
  18813. */
  18814. Quaternion.add = function(left, right, result) {
  18815. if (!defined(left)) {
  18816. throw new DeveloperError('left is required');
  18817. }
  18818. if (!defined(right)) {
  18819. throw new DeveloperError('right is required');
  18820. }
  18821. if (!defined(result)) {
  18822. throw new DeveloperError('result is required');
  18823. }
  18824. result.x = left.x + right.x;
  18825. result.y = left.y + right.y;
  18826. result.z = left.z + right.z;
  18827. result.w = left.w + right.w;
  18828. return result;
  18829. };
  18830. /**
  18831. * Computes the componentwise difference of two quaternions.
  18832. *
  18833. * @param {Quaternion} left The first quaternion.
  18834. * @param {Quaternion} right The second quaternion.
  18835. * @param {Quaternion} result The object onto which to store the result.
  18836. * @returns {Quaternion} The modified result parameter.
  18837. */
  18838. Quaternion.subtract = function(left, right, result) {
  18839. if (!defined(left)) {
  18840. throw new DeveloperError('left is required');
  18841. }
  18842. if (!defined(right)) {
  18843. throw new DeveloperError('right is required');
  18844. }
  18845. if (!defined(result)) {
  18846. throw new DeveloperError('result is required');
  18847. }
  18848. result.x = left.x - right.x;
  18849. result.y = left.y - right.y;
  18850. result.z = left.z - right.z;
  18851. result.w = left.w - right.w;
  18852. return result;
  18853. };
  18854. /**
  18855. * Negates the provided quaternion.
  18856. *
  18857. * @param {Quaternion} quaternion The quaternion to be negated.
  18858. * @param {Quaternion} result The object onto which to store the result.
  18859. * @returns {Quaternion} The modified result parameter.
  18860. */
  18861. Quaternion.negate = function(quaternion, result) {
  18862. if (!defined(quaternion)) {
  18863. throw new DeveloperError('quaternion is required');
  18864. }
  18865. if (!defined(result)) {
  18866. throw new DeveloperError('result is required');
  18867. }
  18868. result.x = -quaternion.x;
  18869. result.y = -quaternion.y;
  18870. result.z = -quaternion.z;
  18871. result.w = -quaternion.w;
  18872. return result;
  18873. };
  18874. /**
  18875. * Computes the dot (scalar) product of two quaternions.
  18876. *
  18877. * @param {Quaternion} left The first quaternion.
  18878. * @param {Quaternion} right The second quaternion.
  18879. * @returns {Number} The dot product.
  18880. */
  18881. Quaternion.dot = function(left, right) {
  18882. if (!defined(left)) {
  18883. throw new DeveloperError('left is required');
  18884. }
  18885. if (!defined(right)) {
  18886. throw new DeveloperError('right is required');
  18887. }
  18888. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  18889. };
  18890. /**
  18891. * Computes the product of two quaternions.
  18892. *
  18893. * @param {Quaternion} left The first quaternion.
  18894. * @param {Quaternion} right The second quaternion.
  18895. * @param {Quaternion} result The object onto which to store the result.
  18896. * @returns {Quaternion} The modified result parameter.
  18897. */
  18898. Quaternion.multiply = function(left, right, result) {
  18899. if (!defined(left)) {
  18900. throw new DeveloperError('left is required');
  18901. }
  18902. if (!defined(right)) {
  18903. throw new DeveloperError('right is required');
  18904. }
  18905. if (!defined(result)) {
  18906. throw new DeveloperError('result is required');
  18907. }
  18908. var leftX = left.x;
  18909. var leftY = left.y;
  18910. var leftZ = left.z;
  18911. var leftW = left.w;
  18912. var rightX = right.x;
  18913. var rightY = right.y;
  18914. var rightZ = right.z;
  18915. var rightW = right.w;
  18916. var x = leftW * rightX + leftX * rightW + leftY * rightZ - leftZ * rightY;
  18917. var y = leftW * rightY - leftX * rightZ + leftY * rightW + leftZ * rightX;
  18918. var z = leftW * rightZ + leftX * rightY - leftY * rightX + leftZ * rightW;
  18919. var w = leftW * rightW - leftX * rightX - leftY * rightY - leftZ * rightZ;
  18920. result.x = x;
  18921. result.y = y;
  18922. result.z = z;
  18923. result.w = w;
  18924. return result;
  18925. };
  18926. /**
  18927. * Multiplies the provided quaternion componentwise by the provided scalar.
  18928. *
  18929. * @param {Quaternion} quaternion The quaternion to be scaled.
  18930. * @param {Number} scalar The scalar to multiply with.
  18931. * @param {Quaternion} result The object onto which to store the result.
  18932. * @returns {Quaternion} The modified result parameter.
  18933. */
  18934. Quaternion.multiplyByScalar = function(quaternion, scalar, result) {
  18935. if (!defined(quaternion)) {
  18936. throw new DeveloperError('quaternion is required');
  18937. }
  18938. if (typeof scalar !== 'number') {
  18939. throw new DeveloperError('scalar is required and must be a number.');
  18940. }
  18941. if (!defined(result)) {
  18942. throw new DeveloperError('result is required');
  18943. }
  18944. result.x = quaternion.x * scalar;
  18945. result.y = quaternion.y * scalar;
  18946. result.z = quaternion.z * scalar;
  18947. result.w = quaternion.w * scalar;
  18948. return result;
  18949. };
  18950. /**
  18951. * Divides the provided quaternion componentwise by the provided scalar.
  18952. *
  18953. * @param {Quaternion} quaternion The quaternion to be divided.
  18954. * @param {Number} scalar The scalar to divide by.
  18955. * @param {Quaternion} result The object onto which to store the result.
  18956. * @returns {Quaternion} The modified result parameter.
  18957. */
  18958. Quaternion.divideByScalar = function(quaternion, scalar, result) {
  18959. if (!defined(quaternion)) {
  18960. throw new DeveloperError('quaternion is required');
  18961. }
  18962. if (typeof scalar !== 'number') {
  18963. throw new DeveloperError('scalar is required and must be a number.');
  18964. }
  18965. if (!defined(result)) {
  18966. throw new DeveloperError('result is required');
  18967. }
  18968. result.x = quaternion.x / scalar;
  18969. result.y = quaternion.y / scalar;
  18970. result.z = quaternion.z / scalar;
  18971. result.w = quaternion.w / scalar;
  18972. return result;
  18973. };
  18974. /**
  18975. * Computes the axis of rotation of the provided quaternion.
  18976. *
  18977. * @param {Quaternion} quaternion The quaternion to use.
  18978. * @param {Cartesian3} result The object onto which to store the result.
  18979. * @returns {Cartesian3} The modified result parameter.
  18980. */
  18981. Quaternion.computeAxis = function(quaternion, result) {
  18982. if (!defined(quaternion)) {
  18983. throw new DeveloperError('quaternion is required');
  18984. }
  18985. if (!defined(result)) {
  18986. throw new DeveloperError('result is required');
  18987. }
  18988. var w = quaternion.w;
  18989. if (Math.abs(w - 1.0) < CesiumMath.EPSILON6) {
  18990. result.x = result.y = result.z = 0;
  18991. return result;
  18992. }
  18993. var scalar = 1.0 / Math.sqrt(1.0 - (w * w));
  18994. result.x = quaternion.x * scalar;
  18995. result.y = quaternion.y * scalar;
  18996. result.z = quaternion.z * scalar;
  18997. return result;
  18998. };
  18999. /**
  19000. * Computes the angle of rotation of the provided quaternion.
  19001. *
  19002. * @param {Quaternion} quaternion The quaternion to use.
  19003. * @returns {Number} The angle of rotation.
  19004. */
  19005. Quaternion.computeAngle = function(quaternion) {
  19006. if (!defined(quaternion)) {
  19007. throw new DeveloperError('quaternion is required');
  19008. }
  19009. if (Math.abs(quaternion.w - 1.0) < CesiumMath.EPSILON6) {
  19010. return 0.0;
  19011. }
  19012. return 2.0 * Math.acos(quaternion.w);
  19013. };
  19014. var lerpScratch = new Quaternion();
  19015. /**
  19016. * Computes the linear interpolation or extrapolation at t using the provided quaternions.
  19017. *
  19018. * @param {Quaternion} start The value corresponding to t at 0.0.
  19019. * @param {Quaternion} end The value corresponding to t at 1.0.
  19020. * @param {Number} t The point along t at which to interpolate.
  19021. * @param {Quaternion} result The object onto which to store the result.
  19022. * @returns {Quaternion} The modified result parameter.
  19023. */
  19024. Quaternion.lerp = function(start, end, t, result) {
  19025. if (!defined(start)) {
  19026. throw new DeveloperError('start is required.');
  19027. }
  19028. if (!defined(end)) {
  19029. throw new DeveloperError('end is required.');
  19030. }
  19031. if (typeof t !== 'number') {
  19032. throw new DeveloperError('t is required and must be a number.');
  19033. }
  19034. if (!defined(result)) {
  19035. throw new DeveloperError('result is required');
  19036. }
  19037. lerpScratch = Quaternion.multiplyByScalar(end, t, lerpScratch);
  19038. result = Quaternion.multiplyByScalar(start, 1.0 - t, result);
  19039. return Quaternion.add(lerpScratch, result, result);
  19040. };
  19041. var slerpEndNegated = new Quaternion();
  19042. var slerpScaledP = new Quaternion();
  19043. var slerpScaledR = new Quaternion();
  19044. /**
  19045. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  19046. *
  19047. * @param {Quaternion} start The value corresponding to t at 0.0.
  19048. * @param {Quaternion} end The value corresponding to t at 1.0.
  19049. * @param {Number} t The point along t at which to interpolate.
  19050. * @param {Quaternion} result The object onto which to store the result.
  19051. * @returns {Quaternion} The modified result parameter.
  19052. *
  19053. * @see Quaternion#fastSlerp
  19054. */
  19055. Quaternion.slerp = function(start, end, t, result) {
  19056. if (!defined(start)) {
  19057. throw new DeveloperError('start is required.');
  19058. }
  19059. if (!defined(end)) {
  19060. throw new DeveloperError('end is required.');
  19061. }
  19062. if (typeof t !== 'number') {
  19063. throw new DeveloperError('t is required and must be a number.');
  19064. }
  19065. if (!defined(result)) {
  19066. throw new DeveloperError('result is required');
  19067. }
  19068. var dot = Quaternion.dot(start, end);
  19069. // The angle between start must be acute. Since q and -q represent
  19070. // the same rotation, negate q to get the acute angle.
  19071. var r = end;
  19072. if (dot < 0.0) {
  19073. dot = -dot;
  19074. r = slerpEndNegated = Quaternion.negate(end, slerpEndNegated);
  19075. }
  19076. // dot > 0, as the dot product approaches 1, the angle between the
  19077. // quaternions vanishes. use linear interpolation.
  19078. if (1.0 - dot < CesiumMath.EPSILON6) {
  19079. return Quaternion.lerp(start, r, t, result);
  19080. }
  19081. var theta = Math.acos(dot);
  19082. slerpScaledP = Quaternion.multiplyByScalar(start, Math.sin((1 - t) * theta), slerpScaledP);
  19083. slerpScaledR = Quaternion.multiplyByScalar(r, Math.sin(t * theta), slerpScaledR);
  19084. result = Quaternion.add(slerpScaledP, slerpScaledR, result);
  19085. return Quaternion.multiplyByScalar(result, 1.0 / Math.sin(theta), result);
  19086. };
  19087. /**
  19088. * The logarithmic quaternion function.
  19089. *
  19090. * @param {Quaternion} quaternion The unit quaternion.
  19091. * @param {Cartesian3} result The object onto which to store the result.
  19092. * @returns {Cartesian3} The modified result parameter.
  19093. */
  19094. Quaternion.log = function(quaternion, result) {
  19095. if (!defined(quaternion)) {
  19096. throw new DeveloperError('quaternion is required.');
  19097. }
  19098. if (!defined(result)) {
  19099. throw new DeveloperError('result is required');
  19100. }
  19101. var theta = CesiumMath.acosClamped(quaternion.w);
  19102. var thetaOverSinTheta = 0.0;
  19103. if (theta !== 0.0) {
  19104. thetaOverSinTheta = theta / Math.sin(theta);
  19105. }
  19106. return Cartesian3.multiplyByScalar(quaternion, thetaOverSinTheta, result);
  19107. };
  19108. /**
  19109. * The exponential quaternion function.
  19110. *
  19111. * @param {Cartesian3} cartesian The cartesian.
  19112. * @param {Quaternion} result The object onto which to store the result.
  19113. * @returns {Quaternion} The modified result parameter.
  19114. */
  19115. Quaternion.exp = function(cartesian, result) {
  19116. if (!defined(cartesian)) {
  19117. throw new DeveloperError('cartesian is required.');
  19118. }
  19119. if (!defined(result)) {
  19120. throw new DeveloperError('result is required');
  19121. }
  19122. var theta = Cartesian3.magnitude(cartesian);
  19123. var sinThetaOverTheta = 0.0;
  19124. if (theta !== 0.0) {
  19125. sinThetaOverTheta = Math.sin(theta) / theta;
  19126. }
  19127. result.x = cartesian.x * sinThetaOverTheta;
  19128. result.y = cartesian.y * sinThetaOverTheta;
  19129. result.z = cartesian.z * sinThetaOverTheta;
  19130. result.w = Math.cos(theta);
  19131. return result;
  19132. };
  19133. var squadScratchCartesian0 = new Cartesian3();
  19134. var squadScratchCartesian1 = new Cartesian3();
  19135. var squadScratchQuaternion0 = new Quaternion();
  19136. var squadScratchQuaternion1 = new Quaternion();
  19137. /**
  19138. * Computes an inner quadrangle point.
  19139. * <p>This will compute quaternions that ensure a squad curve is C<sup>1</sup>.</p>
  19140. *
  19141. * @param {Quaternion} q0 The first quaternion.
  19142. * @param {Quaternion} q1 The second quaternion.
  19143. * @param {Quaternion} q2 The third quaternion.
  19144. * @param {Quaternion} result The object onto which to store the result.
  19145. * @returns {Quaternion} The modified result parameter.
  19146. *
  19147. * @see Quaternion#squad
  19148. */
  19149. Quaternion.computeInnerQuadrangle = function(q0, q1, q2, result) {
  19150. if (!defined(q0) || !defined(q1) || !defined(q2)) {
  19151. throw new DeveloperError('q0, q1, and q2 are required.');
  19152. }
  19153. if (!defined(result)) {
  19154. throw new DeveloperError('result is required');
  19155. }
  19156. var qInv = Quaternion.conjugate(q1, squadScratchQuaternion0);
  19157. Quaternion.multiply(qInv, q2, squadScratchQuaternion1);
  19158. var cart0 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian0);
  19159. Quaternion.multiply(qInv, q0, squadScratchQuaternion1);
  19160. var cart1 = Quaternion.log(squadScratchQuaternion1, squadScratchCartesian1);
  19161. Cartesian3.add(cart0, cart1, cart0);
  19162. Cartesian3.multiplyByScalar(cart0, 0.25, cart0);
  19163. Cartesian3.negate(cart0, cart0);
  19164. Quaternion.exp(cart0, squadScratchQuaternion0);
  19165. return Quaternion.multiply(q1, squadScratchQuaternion0, result);
  19166. };
  19167. /**
  19168. * Computes the spherical quadrangle interpolation between quaternions.
  19169. *
  19170. * @param {Quaternion} q0 The first quaternion.
  19171. * @param {Quaternion} q1 The second quaternion.
  19172. * @param {Quaternion} s0 The first inner quadrangle.
  19173. * @param {Quaternion} s1 The second inner quadrangle.
  19174. * @param {Number} t The time in [0,1] used to interpolate.
  19175. * @param {Quaternion} result The object onto which to store the result.
  19176. * @returns {Quaternion} The modified result parameter.
  19177. *
  19178. *
  19179. * @example
  19180. * // 1. compute the squad interpolation between two quaternions on a curve
  19181. * var s0 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i - 1], quaternions[i], quaternions[i + 1], new Cesium.Quaternion());
  19182. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[i], quaternions[i + 1], quaternions[i + 2], new Cesium.Quaternion());
  19183. * var q = Cesium.Quaternion.squad(quaternions[i], quaternions[i + 1], s0, s1, t, new Cesium.Quaternion());
  19184. *
  19185. * // 2. compute the squad interpolation as above but where the first quaternion is a end point.
  19186. * var s1 = Cesium.Quaternion.computeInnerQuadrangle(quaternions[0], quaternions[1], quaternions[2], new Cesium.Quaternion());
  19187. * var q = Cesium.Quaternion.squad(quaternions[0], quaternions[1], quaternions[0], s1, t, new Cesium.Quaternion());
  19188. *
  19189. * @see Quaternion#computeInnerQuadrangle
  19190. */
  19191. Quaternion.squad = function(q0, q1, s0, s1, t, result) {
  19192. if (!defined(q0) || !defined(q1) || !defined(s0) || !defined(s1)) {
  19193. throw new DeveloperError('q0, q1, s0, and s1 are required.');
  19194. }
  19195. if (typeof t !== 'number') {
  19196. throw new DeveloperError('t is required and must be a number.');
  19197. }
  19198. if (!defined(result)) {
  19199. throw new DeveloperError('result is required');
  19200. }
  19201. var slerp0 = Quaternion.slerp(q0, q1, t, squadScratchQuaternion0);
  19202. var slerp1 = Quaternion.slerp(s0, s1, t, squadScratchQuaternion1);
  19203. return Quaternion.slerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  19204. };
  19205. var fastSlerpScratchQuaternion = new Quaternion();
  19206. var opmu = 1.90110745351730037;
  19207. var u = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19208. var v = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19209. var bT = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19210. var bD = FeatureDetection.supportsTypedArrays() ? new Float32Array(8) : [];
  19211. for (var i = 0; i < 7; ++i) {
  19212. var s = i + 1.0;
  19213. var t = 2.0 * s + 1.0;
  19214. u[i] = 1.0 / (s * t);
  19215. v[i] = s / t;
  19216. }
  19217. u[7] = opmu / (8.0 * 17.0);
  19218. v[7] = opmu * 8.0 / 17.0;
  19219. /**
  19220. * Computes the spherical linear interpolation or extrapolation at t using the provided quaternions.
  19221. * This implementation is faster than {@link Quaternion#slerp}, but is only accurate up to 10<sup>-6</sup>.
  19222. *
  19223. * @param {Quaternion} start The value corresponding to t at 0.0.
  19224. * @param {Quaternion} end The value corresponding to t at 1.0.
  19225. * @param {Number} t The point along t at which to interpolate.
  19226. * @param {Quaternion} result The object onto which to store the result.
  19227. * @returns {Quaternion} The modified result parameter.
  19228. *
  19229. * @see Quaternion#slerp
  19230. */
  19231. Quaternion.fastSlerp = function(start, end, t, result) {
  19232. if (!defined(start)) {
  19233. throw new DeveloperError('start is required.');
  19234. }
  19235. if (!defined(end)) {
  19236. throw new DeveloperError('end is required.');
  19237. }
  19238. if (typeof t !== 'number') {
  19239. throw new DeveloperError('t is required and must be a number.');
  19240. }
  19241. if (!defined(result)) {
  19242. throw new DeveloperError('result is required');
  19243. }
  19244. var x = Quaternion.dot(start, end);
  19245. var sign;
  19246. if (x >= 0) {
  19247. sign = 1.0;
  19248. } else {
  19249. sign = -1.0;
  19250. x = -x;
  19251. }
  19252. var xm1 = x - 1.0;
  19253. var d = 1.0 - t;
  19254. var sqrT = t * t;
  19255. var sqrD = d * d;
  19256. for (var i = 7; i >= 0; --i) {
  19257. bT[i] = (u[i] * sqrT - v[i]) * xm1;
  19258. bD[i] = (u[i] * sqrD - v[i]) * xm1;
  19259. }
  19260. var cT = sign * t * (
  19261. 1.0 + bT[0] * (1.0 + bT[1] * (1.0 + bT[2] * (1.0 + bT[3] * (
  19262. 1.0 + bT[4] * (1.0 + bT[5] * (1.0 + bT[6] * (1.0 + bT[7]))))))));
  19263. var cD = d * (
  19264. 1.0 + bD[0] * (1.0 + bD[1] * (1.0 + bD[2] * (1.0 + bD[3] * (
  19265. 1.0 + bD[4] * (1.0 + bD[5] * (1.0 + bD[6] * (1.0 + bD[7]))))))));
  19266. var temp = Quaternion.multiplyByScalar(start, cD, fastSlerpScratchQuaternion);
  19267. Quaternion.multiplyByScalar(end, cT, result);
  19268. return Quaternion.add(temp, result, result);
  19269. };
  19270. /**
  19271. * Computes the spherical quadrangle interpolation between quaternions.
  19272. * An implementation that is faster than {@link Quaternion#squad}, but less accurate.
  19273. *
  19274. * @param {Quaternion} q0 The first quaternion.
  19275. * @param {Quaternion} q1 The second quaternion.
  19276. * @param {Quaternion} s0 The first inner quadrangle.
  19277. * @param {Quaternion} s1 The second inner quadrangle.
  19278. * @param {Number} t The time in [0,1] used to interpolate.
  19279. * @param {Quaternion} result The object onto which to store the result.
  19280. * @returns {Quaternion} The modified result parameter or a new instance if none was provided.
  19281. *
  19282. * @see Quaternion#squad
  19283. */
  19284. Quaternion.fastSquad = function(q0, q1, s0, s1, t, result) {
  19285. if (!defined(q0) || !defined(q1) || !defined(s0) || !defined(s1)) {
  19286. throw new DeveloperError('q0, q1, s0, and s1 are required.');
  19287. }
  19288. if (typeof t !== 'number') {
  19289. throw new DeveloperError('t is required and must be a number.');
  19290. }
  19291. if (!defined(result)) {
  19292. throw new DeveloperError('result is required');
  19293. }
  19294. var slerp0 = Quaternion.fastSlerp(q0, q1, t, squadScratchQuaternion0);
  19295. var slerp1 = Quaternion.fastSlerp(s0, s1, t, squadScratchQuaternion1);
  19296. return Quaternion.fastSlerp(slerp0, slerp1, 2.0 * t * (1.0 - t), result);
  19297. };
  19298. /**
  19299. * Compares the provided quaternions componentwise and returns
  19300. * <code>true</code> if they are equal, <code>false</code> otherwise.
  19301. *
  19302. * @param {Quaternion} [left] The first quaternion.
  19303. * @param {Quaternion} [right] The second quaternion.
  19304. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  19305. */
  19306. Quaternion.equals = function(left, right) {
  19307. return (left === right) ||
  19308. ((defined(left)) &&
  19309. (defined(right)) &&
  19310. (left.x === right.x) &&
  19311. (left.y === right.y) &&
  19312. (left.z === right.z) &&
  19313. (left.w === right.w));
  19314. };
  19315. /**
  19316. * Compares the provided quaternions componentwise and returns
  19317. * <code>true</code> if they are within the provided epsilon,
  19318. * <code>false</code> otherwise.
  19319. *
  19320. * @param {Quaternion} [left] The first quaternion.
  19321. * @param {Quaternion} [right] The second quaternion.
  19322. * @param {Number} epsilon The epsilon to use for equality testing.
  19323. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  19324. */
  19325. Quaternion.equalsEpsilon = function(left, right, epsilon) {
  19326. if (typeof epsilon !== 'number') {
  19327. throw new DeveloperError('epsilon is required and must be a number.');
  19328. }
  19329. return (left === right) ||
  19330. ((defined(left)) &&
  19331. (defined(right)) &&
  19332. (Math.abs(left.x - right.x) <= epsilon) &&
  19333. (Math.abs(left.y - right.y) <= epsilon) &&
  19334. (Math.abs(left.z - right.z) <= epsilon) &&
  19335. (Math.abs(left.w - right.w) <= epsilon));
  19336. };
  19337. /**
  19338. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 0.0).
  19339. *
  19340. * @type {Quaternion}
  19341. * @constant
  19342. */
  19343. Quaternion.ZERO = freezeObject(new Quaternion(0.0, 0.0, 0.0, 0.0));
  19344. /**
  19345. * An immutable Quaternion instance initialized to (0.0, 0.0, 0.0, 1.0).
  19346. *
  19347. * @type {Quaternion}
  19348. * @constant
  19349. */
  19350. Quaternion.IDENTITY = freezeObject(new Quaternion(0.0, 0.0, 0.0, 1.0));
  19351. /**
  19352. * Duplicates this Quaternion instance.
  19353. *
  19354. * @param {Quaternion} [result] The object onto which to store the result.
  19355. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if one was not provided.
  19356. */
  19357. Quaternion.prototype.clone = function(result) {
  19358. return Quaternion.clone(this, result);
  19359. };
  19360. /**
  19361. * Compares this and the provided quaternion componentwise and returns
  19362. * <code>true</code> if they are equal, <code>false</code> otherwise.
  19363. *
  19364. * @param {Quaternion} [right] The right hand side quaternion.
  19365. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  19366. */
  19367. Quaternion.prototype.equals = function(right) {
  19368. return Quaternion.equals(this, right);
  19369. };
  19370. /**
  19371. * Compares this and the provided quaternion componentwise and returns
  19372. * <code>true</code> if they are within the provided epsilon,
  19373. * <code>false</code> otherwise.
  19374. *
  19375. * @param {Quaternion} [right] The right hand side quaternion.
  19376. * @param {Number} epsilon The epsilon to use for equality testing.
  19377. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  19378. */
  19379. Quaternion.prototype.equalsEpsilon = function(right, epsilon) {
  19380. return Quaternion.equalsEpsilon(this, right, epsilon);
  19381. };
  19382. /**
  19383. * Returns a string representing this quaternion in the format (x, y, z, w).
  19384. *
  19385. * @returns {String} A string representing this Quaternion.
  19386. */
  19387. Quaternion.prototype.toString = function() {
  19388. return '(' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ')';
  19389. };
  19390. return Quaternion;
  19391. });
  19392. /*global define*/
  19393. define('Core/Transforms',[
  19394. '../ThirdParty/when',
  19395. './Cartesian2',
  19396. './Cartesian3',
  19397. './Cartesian4',
  19398. './Cartographic',
  19399. './defaultValue',
  19400. './defined',
  19401. './deprecationWarning',
  19402. './DeveloperError',
  19403. './EarthOrientationParameters',
  19404. './EarthOrientationParametersSample',
  19405. './Ellipsoid',
  19406. './HeadingPitchRoll',
  19407. './Iau2006XysData',
  19408. './Iau2006XysSample',
  19409. './JulianDate',
  19410. './Math',
  19411. './Matrix3',
  19412. './Matrix4',
  19413. './Quaternion',
  19414. './TimeConstants'
  19415. ], function(
  19416. when,
  19417. Cartesian2,
  19418. Cartesian3,
  19419. Cartesian4,
  19420. Cartographic,
  19421. defaultValue,
  19422. defined,
  19423. deprecationWarning,
  19424. DeveloperError,
  19425. EarthOrientationParameters,
  19426. EarthOrientationParametersSample,
  19427. Ellipsoid,
  19428. HeadingPitchRoll,
  19429. Iau2006XysData,
  19430. Iau2006XysSample,
  19431. JulianDate,
  19432. CesiumMath,
  19433. Matrix3,
  19434. Matrix4,
  19435. Quaternion,
  19436. TimeConstants) {
  19437. 'use strict';
  19438. /**
  19439. * Contains functions for transforming positions to various reference frames.
  19440. *
  19441. * @exports Transforms
  19442. */
  19443. var Transforms = {};
  19444. var eastNorthUpToFixedFrameNormal = new Cartesian3();
  19445. var eastNorthUpToFixedFrameTangent = new Cartesian3();
  19446. var eastNorthUpToFixedFrameBitangent = new Cartesian3();
  19447. /**
  19448. * Computes a 4x4 transformation matrix from a reference frame with an east-north-up axes
  19449. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19450. * The local axes are defined as:
  19451. * <ul>
  19452. * <li>The <code>x</code> axis points in the local east direction.</li>
  19453. * <li>The <code>y</code> axis points in the local north direction.</li>
  19454. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  19455. * </ul>
  19456. *
  19457. * @param {Cartesian3} origin The center point of the local reference frame.
  19458. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19459. * @param {Matrix4} [result] The object onto which to store the result.
  19460. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19461. *
  19462. * @example
  19463. * // Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame.
  19464. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19465. * var transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);
  19466. */
  19467. Transforms.eastNorthUpToFixedFrame = function(origin, ellipsoid, result) {
  19468. if (!defined(origin)) {
  19469. throw new DeveloperError('origin is required.');
  19470. }
  19471. // If x and y are zero, assume origin is at a pole, which is a special case.
  19472. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19473. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19474. var sign = CesiumMath.sign(origin.z);
  19475. if (!defined(result)) {
  19476. return new Matrix4(
  19477. 0.0, -sign, 0.0, origin.x,
  19478. 1.0, 0.0, 0.0, origin.y,
  19479. 0.0, 0.0, sign, origin.z,
  19480. 0.0, 0.0, 0.0, 1.0);
  19481. }
  19482. result[0] = 0.0;
  19483. result[1] = 1.0;
  19484. result[2] = 0.0;
  19485. result[3] = 0.0;
  19486. result[4] = -sign;
  19487. result[5] = 0.0;
  19488. result[6] = 0.0;
  19489. result[7] = 0.0;
  19490. result[8] = 0.0;
  19491. result[9] = 0.0;
  19492. result[10] = sign;
  19493. result[11] = 0.0;
  19494. result[12] = origin.x;
  19495. result[13] = origin.y;
  19496. result[14] = origin.z;
  19497. result[15] = 1.0;
  19498. return result;
  19499. }
  19500. var normal = eastNorthUpToFixedFrameNormal;
  19501. var tangent = eastNorthUpToFixedFrameTangent;
  19502. var bitangent = eastNorthUpToFixedFrameBitangent;
  19503. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19504. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19505. tangent.x = -origin.y;
  19506. tangent.y = origin.x;
  19507. tangent.z = 0.0;
  19508. Cartesian3.normalize(tangent, tangent);
  19509. Cartesian3.cross(normal, tangent, bitangent);
  19510. if (!defined(result)) {
  19511. return new Matrix4(
  19512. tangent.x, bitangent.x, normal.x, origin.x,
  19513. tangent.y, bitangent.y, normal.y, origin.y,
  19514. tangent.z, bitangent.z, normal.z, origin.z,
  19515. 0.0, 0.0, 0.0, 1.0);
  19516. }
  19517. result[0] = tangent.x;
  19518. result[1] = tangent.y;
  19519. result[2] = tangent.z;
  19520. result[3] = 0.0;
  19521. result[4] = bitangent.x;
  19522. result[5] = bitangent.y;
  19523. result[6] = bitangent.z;
  19524. result[7] = 0.0;
  19525. result[8] = normal.x;
  19526. result[9] = normal.y;
  19527. result[10] = normal.z;
  19528. result[11] = 0.0;
  19529. result[12] = origin.x;
  19530. result[13] = origin.y;
  19531. result[14] = origin.z;
  19532. result[15] = 1.0;
  19533. return result;
  19534. };
  19535. var northEastDownToFixedFrameNormal = new Cartesian3();
  19536. var northEastDownToFixedFrameTangent = new Cartesian3();
  19537. var northEastDownToFixedFrameBitangent = new Cartesian3();
  19538. /**
  19539. * Computes a 4x4 transformation matrix from a reference frame with an north-east-down axes
  19540. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19541. * The local axes are defined as:
  19542. * <ul>
  19543. * <li>The <code>x</code> axis points in the local north direction.</li>
  19544. * <li>The <code>y</code> axis points in the local east direction.</li>
  19545. * <li>The <code>z</code> axis points in the opposite direction of the ellipsoid surface normal which passes through the position.</li>
  19546. * </ul>
  19547. *
  19548. * @param {Cartesian3} origin The center point of the local reference frame.
  19549. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19550. * @param {Matrix4} [result] The object onto which to store the result.
  19551. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19552. *
  19553. * @example
  19554. * // Get the transform from local north-east-down at cartographic (0.0, 0.0) to Earth's fixed frame.
  19555. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19556. * var transform = Cesium.Transforms.northEastDownToFixedFrame(center);
  19557. */
  19558. Transforms.northEastDownToFixedFrame = function(origin, ellipsoid, result) {
  19559. if (!defined(origin)) {
  19560. throw new DeveloperError('origin is required.');
  19561. }
  19562. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19563. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19564. // The poles are special cases. If x and y are zero, assume origin is at a pole.
  19565. var sign = CesiumMath.sign(origin.z);
  19566. if (!defined(result)) {
  19567. return new Matrix4(
  19568. -sign, 0.0, 0.0, origin.x,
  19569. 0.0, 1.0, 0.0, origin.y,
  19570. 0.0, 0.0, -sign, origin.z,
  19571. 0.0, 0.0, 0.0, 1.0);
  19572. }
  19573. result[0] = -sign;
  19574. result[1] = 0.0;
  19575. result[2] = 0.0;
  19576. result[3] = 0.0;
  19577. result[4] = 0.0;
  19578. result[5] = 1.0;
  19579. result[6] = 0.0;
  19580. result[7] = 0.0;
  19581. result[8] = 0.0;
  19582. result[9] = 0.0;
  19583. result[10] = -sign;
  19584. result[11] = 0.0;
  19585. result[12] = origin.x;
  19586. result[13] = origin.y;
  19587. result[14] = origin.z;
  19588. result[15] = 1.0;
  19589. return result;
  19590. }
  19591. var normal = northEastDownToFixedFrameNormal;
  19592. var tangent = northEastDownToFixedFrameTangent;
  19593. var bitangent = northEastDownToFixedFrameBitangent;
  19594. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19595. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19596. tangent.x = -origin.y;
  19597. tangent.y = origin.x;
  19598. tangent.z = 0.0;
  19599. Cartesian3.normalize(tangent, tangent);
  19600. Cartesian3.cross(normal, tangent, bitangent);
  19601. if (!defined(result)) {
  19602. return new Matrix4(
  19603. bitangent.x, tangent.x, -normal.x, origin.x,
  19604. bitangent.y, tangent.y, -normal.y, origin.y,
  19605. bitangent.z, tangent.z, -normal.z, origin.z,
  19606. 0.0, 0.0, 0.0, 1.0);
  19607. }
  19608. result[0] = bitangent.x;
  19609. result[1] = bitangent.y;
  19610. result[2] = bitangent.z;
  19611. result[3] = 0.0;
  19612. result[4] = tangent.x;
  19613. result[5] = tangent.y;
  19614. result[6] = tangent.z;
  19615. result[7] = 0.0;
  19616. result[8] = -normal.x;
  19617. result[9] = -normal.y;
  19618. result[10] = -normal.z;
  19619. result[11] = 0.0;
  19620. result[12] = origin.x;
  19621. result[13] = origin.y;
  19622. result[14] = origin.z;
  19623. result[15] = 1.0;
  19624. return result;
  19625. };
  19626. /**
  19627. * Computes a 4x4 transformation matrix from a reference frame with an north-up-east axes
  19628. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19629. * The local axes are defined as:
  19630. * <ul>
  19631. * <li>The <code>x</code> axis points in the local north direction.</li>
  19632. * <li>The <code>y</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  19633. * <li>The <code>z</code> axis points in the local east direction.</li>
  19634. * </ul>
  19635. *
  19636. * @param {Cartesian3} origin The center point of the local reference frame.
  19637. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19638. * @param {Matrix4} [result] The object onto which to store the result.
  19639. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19640. *
  19641. * @example
  19642. * // Get the transform from local north-up-east at cartographic (0.0, 0.0) to Earth's fixed frame.
  19643. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19644. * var transform = Cesium.Transforms.northUpEastToFixedFrame(center);
  19645. */
  19646. Transforms.northUpEastToFixedFrame = function(origin, ellipsoid, result) {
  19647. if (!defined(origin)) {
  19648. throw new DeveloperError('origin is required.');
  19649. }
  19650. // If x and y are zero, assume origin is at a pole, which is a special case.
  19651. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19652. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19653. var sign = CesiumMath.sign(origin.z);
  19654. if (!defined(result)) {
  19655. return new Matrix4(
  19656. -sign, 0.0, 0.0, origin.x,
  19657. 0.0, 0.0, 1.0, origin.y,
  19658. 0.0, sign, 0.0, origin.z,
  19659. 0.0, 0.0, 0.0, 1.0);
  19660. }
  19661. result[0] = -sign;
  19662. result[1] = 0.0;
  19663. result[2] = 0.0;
  19664. result[3] = 0.0;
  19665. result[4] = 0.0;
  19666. result[5] = 0.0;
  19667. result[6] = sign;
  19668. result[7] = 0.0;
  19669. result[8] = 0.0;
  19670. result[9] = 1.0;
  19671. result[10] = 0.0;
  19672. result[11] = 0.0;
  19673. result[12] = origin.x;
  19674. result[13] = origin.y;
  19675. result[14] = origin.z;
  19676. result[15] = 1.0;
  19677. return result;
  19678. }
  19679. var normal = eastNorthUpToFixedFrameNormal;
  19680. var tangent = eastNorthUpToFixedFrameTangent;
  19681. var bitangent = eastNorthUpToFixedFrameBitangent;
  19682. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19683. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19684. tangent.x = -origin.y;
  19685. tangent.y = origin.x;
  19686. tangent.z = 0.0;
  19687. Cartesian3.normalize(tangent, tangent);
  19688. Cartesian3.cross(normal, tangent, bitangent);
  19689. if (!defined(result)) {
  19690. return new Matrix4(
  19691. bitangent.x, normal.x, tangent.x, origin.x,
  19692. bitangent.y, normal.y, tangent.y, origin.y,
  19693. bitangent.z, normal.z, tangent.z, origin.z,
  19694. 0.0, 0.0, 0.0, 1.0);
  19695. }
  19696. result[0] = bitangent.x;
  19697. result[1] = bitangent.y;
  19698. result[2] = bitangent.z;
  19699. result[3] = 0.0;
  19700. result[4] = normal.x;
  19701. result[5] = normal.y;
  19702. result[6] = normal.z;
  19703. result[7] = 0.0;
  19704. result[8] = tangent.x;
  19705. result[9] = tangent.y;
  19706. result[10] = tangent.z;
  19707. result[11] = 0.0;
  19708. result[12] = origin.x;
  19709. result[13] = origin.y;
  19710. result[14] = origin.z;
  19711. result[15] = 1.0;
  19712. return result;
  19713. };
  19714. /**
  19715. * Computes a 4x4 transformation matrix from a reference frame with an north-west-up axes
  19716. * centered at the provided origin to the provided ellipsoid's fixed reference frame.
  19717. * The local axes are defined as:
  19718. * <ul>
  19719. * <li>The <code>x</code> axis points in the local north direction.</li>
  19720. * <li>The <code>y</code> axis points in the local west direction.</li>
  19721. * <li>The <code>z</code> axis points in the direction of the ellipsoid surface normal which passes through the position.</li>
  19722. * </ul>
  19723. *
  19724. * @param {Cartesian3} origin The center point of the local reference frame.
  19725. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19726. * @param {Matrix4} [result] The object onto which to store the result.
  19727. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19728. *
  19729. * @example
  19730. * // Get the transform from local north-West-Up at cartographic (0.0, 0.0) to Earth's fixed frame.
  19731. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19732. * var transform = Cesium.Transforms.northWestUpToFixedFrame(center);
  19733. */
  19734. Transforms.northWestUpToFixedFrame = function(origin, ellipsoid, result) {
  19735. if (!defined(origin)) {
  19736. throw new DeveloperError('origin is required.');
  19737. }
  19738. // If x and y are zero, assume origin is at a pole, which is a special case.
  19739. if (CesiumMath.equalsEpsilon(origin.x, 0.0, CesiumMath.EPSILON14) &&
  19740. CesiumMath.equalsEpsilon(origin.y, 0.0, CesiumMath.EPSILON14)) {
  19741. var sign = CesiumMath.sign(origin.z);
  19742. if (!defined(result)) {
  19743. return new Matrix4(
  19744. -sign, 0.0, 0.0, origin.x,
  19745. 0.0, -1.0, 0.0, origin.y,
  19746. 0.0, 0.0, sign, origin.z,
  19747. 0.0, 0.0, 0.0, 1.0);
  19748. }
  19749. result[0] = -sign;
  19750. result[1] = 0.0;
  19751. result[2] = 0.0;
  19752. result[3] = 0.0;
  19753. result[4] = 0.0;
  19754. result[5] = -1.0;
  19755. result[6] = 0.0;
  19756. result[7] = 0.0;
  19757. result[8] = 0.0;
  19758. result[9] = 0.0;
  19759. result[10] = sign;
  19760. result[11] = 0.0;
  19761. result[12] = origin.x;
  19762. result[13] = origin.y;
  19763. result[14] = origin.z;
  19764. result[15] = 1.0;
  19765. return result;
  19766. }
  19767. var normal = eastNorthUpToFixedFrameNormal;//Up
  19768. var tangent = eastNorthUpToFixedFrameTangent;//East
  19769. var bitangent = eastNorthUpToFixedFrameBitangent;//North
  19770. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  19771. ellipsoid.geodeticSurfaceNormal(origin, normal);
  19772. tangent.x = -origin.y;
  19773. tangent.y = origin.x;
  19774. tangent.z = 0.0;
  19775. Cartesian3.normalize(tangent, tangent);
  19776. Cartesian3.cross(normal, tangent, bitangent);
  19777. if (!defined(result)) {
  19778. return new Matrix4(
  19779. bitangent.x, -tangent.x, normal.x, origin.x,
  19780. bitangent.y, -tangent.y, normal.y, origin.y,
  19781. bitangent.z, -tangent.z, normal.z, origin.z,
  19782. 0.0, 0.0, 0.0, 1.0);
  19783. }
  19784. result[0] = bitangent.x;
  19785. result[1] = bitangent.y;
  19786. result[2] = bitangent.z;
  19787. result[3] = 0.0;
  19788. result[4] = -tangent.x;
  19789. result[5] = -tangent.y;
  19790. result[6] = -tangent.z;
  19791. result[7] = 0.0;
  19792. result[8] = normal.x;
  19793. result[9] = normal.y;
  19794. result[10] = normal.z;
  19795. result[11] = 0.0;
  19796. result[12] = origin.x;
  19797. result[13] = origin.y;
  19798. result[14] = origin.z;
  19799. result[15] = 1.0;
  19800. return result;
  19801. };
  19802. var scratchHPRQuaternion = new Quaternion();
  19803. var scratchScale = new Cartesian3(1.0, 1.0, 1.0);
  19804. var scratchHPRMatrix4 = new Matrix4();
  19805. /**
  19806. * Computes a 4x4 transformation matrix from a reference frame with axes computed from the heading-pitch-roll angles
  19807. * centered at the provided origin to the provided ellipsoid's fixed reference frame. Heading is the rotation from the local north
  19808. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  19809. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  19810. *
  19811. * @param {Cartesian3} origin The center point of the local reference frame.
  19812. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  19813. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19814. * @param {Matrix4} [result] The object onto which to store the result.
  19815. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if none was provided.
  19816. *
  19817. * @example
  19818. * // Get the transform from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  19819. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19820. * var heading = -Cesium.Math.PI_OVER_TWO;
  19821. * var pitch = Cesium.Math.PI_OVER_FOUR;
  19822. * var roll = 0.0;
  19823. * var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  19824. * var transform = Cesium.Transforms.headingPitchRollToFixedFrame(center, hpr);
  19825. */
  19826. Transforms.headingPitchRollToFixedFrame = function(origin, headingPitchRoll, pitch, roll, ellipsoid, result) {
  19827. var heading;
  19828. if (typeof headingPitchRoll === 'object') {
  19829. // Shift arguments using assignments to encourage JIT optimization.
  19830. ellipsoid = pitch;
  19831. result = roll;
  19832. heading = headingPitchRoll.heading;
  19833. pitch = headingPitchRoll.pitch;
  19834. roll = headingPitchRoll.roll;
  19835. } else {
  19836. 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.');
  19837. heading = headingPitchRoll;
  19838. }
  19839. // checks for required parameters happen in the called functions
  19840. var hprQuaternion = Quaternion.fromHeadingPitchRoll(heading, pitch, roll, scratchHPRQuaternion);
  19841. var hprMatrix = Matrix4.fromTranslationQuaternionRotationScale(Cartesian3.ZERO, hprQuaternion, scratchScale, scratchHPRMatrix4);
  19842. result = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result);
  19843. return Matrix4.multiply(result, hprMatrix, result);
  19844. };
  19845. var scratchHPR = new HeadingPitchRoll();
  19846. var scratchENUMatrix4 = new Matrix4();
  19847. var scratchHPRMatrix3 = new Matrix3();
  19848. /**
  19849. * Computes a quaternion from a reference frame with axes computed from the heading-pitch-roll angles
  19850. * centered at the provided origin. Heading is the rotation from the local north
  19851. * direction where a positive angle is increasing eastward. Pitch is the rotation from the local east-north plane. Positive pitch angles
  19852. * are above the plane. Negative pitch angles are below the plane. Roll is the first rotation applied about the local east axis.
  19853. *
  19854. * @param {Cartesian3} origin The center point of the local reference frame.
  19855. * @param {HeadingPitchRoll} headingPitchRoll The heading, pitch, and roll.
  19856. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid whose fixed frame is used in the transformation.
  19857. * @param {Quaternion} [result] The object onto which to store the result.
  19858. * @returns {Quaternion} The modified result parameter or a new Quaternion instance if none was provided.
  19859. *
  19860. * @example
  19861. * // Get the quaternion from local heading-pitch-roll at cartographic (0.0, 0.0) to Earth's fixed frame.
  19862. * var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  19863. * var heading = -Cesium.Math.PI_OVER_TWO;
  19864. * var pitch = Cesium.Math.PI_OVER_FOUR;
  19865. * var roll = 0.0;
  19866. * var hpr = new HeadingPitchRoll(heading, pitch, roll);
  19867. * var quaternion = Cesium.Transforms.headingPitchRollQuaternion(center, hpr);
  19868. */
  19869. Transforms.headingPitchRollQuaternion = function(origin, headingPitchRoll, pitch, roll, ellipsoid, result) {
  19870. var hpr;
  19871. if (typeof headingPitchRoll === 'object') {
  19872. // Shift arguments using assignment to encourage JIT optimization.
  19873. hpr = headingPitchRoll;
  19874. ellipsoid = pitch;
  19875. result = roll;
  19876. } else {
  19877. 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.');
  19878. scratchHPR.heading = headingPitchRoll;
  19879. scratchHPR.pitch = pitch;
  19880. scratchHPR.roll = roll;
  19881. hpr = scratchHPR;
  19882. }
  19883. // checks for required parameters happen in the called functions
  19884. var transform = Transforms.headingPitchRollToFixedFrame(origin, hpr, ellipsoid, scratchENUMatrix4);
  19885. var rotation = Matrix4.getRotation(transform, scratchHPRMatrix3);
  19886. return Quaternion.fromRotationMatrix(rotation, result);
  19887. };
  19888. var gmstConstant0 = 6 * 3600 + 41 * 60 + 50.54841;
  19889. var gmstConstant1 = 8640184.812866;
  19890. var gmstConstant2 = 0.093104;
  19891. var gmstConstant3 = -6.2E-6;
  19892. var rateCoef = 1.1772758384668e-19;
  19893. var wgs84WRPrecessing = 7.2921158553E-5;
  19894. var twoPiOverSecondsInDay = CesiumMath.TWO_PI / 86400.0;
  19895. var dateInUtc = new JulianDate();
  19896. /**
  19897. * Computes a rotation matrix to transform a point or vector from True Equator Mean Equinox (TEME) axes to the
  19898. * pseudo-fixed axes at a given time. This method treats the UT1 time standard as equivalent to UTC.
  19899. *
  19900. * @param {JulianDate} date The time at which to compute the rotation matrix.
  19901. * @param {Matrix3} [result] The object onto which to store the result.
  19902. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if none was provided.
  19903. *
  19904. * @example
  19905. * //Set the view to in the inertial frame.
  19906. * scene.preRender.addEventListener(function(scene, time) {
  19907. * var now = Cesium.JulianDate.now();
  19908. * var offset = Cesium.Matrix4.multiplyByPoint(camera.transform, camera.position, new Cesium.Cartesian3());
  19909. * var transform = Cesium.Matrix4.fromRotationTranslation(Cesium.Transforms.computeTemeToPseudoFixedMatrix(now));
  19910. * var inverseTransform = Cesium.Matrix4.inverseTransformation(transform, new Cesium.Matrix4());
  19911. * Cesium.Matrix4.multiplyByPoint(inverseTransform, offset, offset);
  19912. * camera.lookAtTransform(transform, offset);
  19913. * });
  19914. */
  19915. Transforms.computeTemeToPseudoFixedMatrix = function (date, result) {
  19916. if (!defined(date)) {
  19917. throw new DeveloperError('date is required.');
  19918. }
  19919. // GMST is actually computed using UT1. We're using UTC as an approximation of UT1.
  19920. // We do not want to use the function like convertTaiToUtc in JulianDate because
  19921. // we explicitly do not want to fail when inside the leap second.
  19922. dateInUtc = JulianDate.addSeconds(date, -JulianDate.computeTaiMinusUtc(date), dateInUtc);
  19923. var utcDayNumber = dateInUtc.dayNumber;
  19924. var utcSecondsIntoDay = dateInUtc.secondsOfDay;
  19925. var t;
  19926. var diffDays = utcDayNumber - 2451545;
  19927. if (utcSecondsIntoDay >= 43200.0) {
  19928. t = (diffDays + 0.5) / TimeConstants.DAYS_PER_JULIAN_CENTURY;
  19929. } else {
  19930. t = (diffDays - 0.5) / TimeConstants.DAYS_PER_JULIAN_CENTURY;
  19931. }
  19932. var gmst0 = gmstConstant0 + t * (gmstConstant1 + t * (gmstConstant2 + t * gmstConstant3));
  19933. var angle = (gmst0 * twoPiOverSecondsInDay) % CesiumMath.TWO_PI;
  19934. var ratio = wgs84WRPrecessing + rateCoef * (utcDayNumber - 2451545.5);
  19935. var secondsSinceMidnight = (utcSecondsIntoDay + TimeConstants.SECONDS_PER_DAY * 0.5) % TimeConstants.SECONDS_PER_DAY;
  19936. var gha = angle + (ratio * secondsSinceMidnight);
  19937. var cosGha = Math.cos(gha);
  19938. var sinGha = Math.sin(gha);
  19939. if (!defined(result)) {
  19940. return new Matrix3(cosGha, sinGha, 0.0,
  19941. -sinGha, cosGha, 0.0,
  19942. 0.0, 0.0, 1.0);
  19943. }
  19944. result[0] = cosGha;
  19945. result[1] = -sinGha;
  19946. result[2] = 0.0;
  19947. result[3] = sinGha;
  19948. result[4] = cosGha;
  19949. result[5] = 0.0;
  19950. result[6] = 0.0;
  19951. result[7] = 0.0;
  19952. result[8] = 1.0;
  19953. return result;
  19954. };
  19955. /**
  19956. * The source of IAU 2006 XYS data, used for computing the transformation between the
  19957. * Fixed and ICRF axes.
  19958. * @type {Iau2006XysData}
  19959. *
  19960. * @see Transforms.computeIcrfToFixedMatrix
  19961. * @see Transforms.computeFixedToIcrfMatrix
  19962. *
  19963. * @private
  19964. */
  19965. Transforms.iau2006XysData = new Iau2006XysData();
  19966. /**
  19967. * The source of Earth Orientation Parameters (EOP) data, used for computing the transformation
  19968. * between the Fixed and ICRF axes. By default, zero values are used for all EOP values,
  19969. * yielding a reasonable but not completely accurate representation of the ICRF axes.
  19970. * @type {EarthOrientationParameters}
  19971. *
  19972. * @see Transforms.computeIcrfToFixedMatrix
  19973. * @see Transforms.computeFixedToIcrfMatrix
  19974. *
  19975. * @private
  19976. */
  19977. Transforms.earthOrientationParameters = EarthOrientationParameters.NONE;
  19978. var ttMinusTai = 32.184;
  19979. var j2000ttDays = 2451545.0;
  19980. /**
  19981. * Preloads the data necessary to transform between the ICRF and Fixed axes, in either
  19982. * direction, over a given interval. This function returns a promise that, when resolved,
  19983. * indicates that the preload has completed.
  19984. *
  19985. * @param {TimeInterval} timeInterval The interval to preload.
  19986. * @returns {Promise.<undefined>} A promise that, when resolved, indicates that the preload has completed
  19987. * and evaluation of the transformation between the fixed and ICRF axes will
  19988. * no longer return undefined for a time inside the interval.
  19989. *
  19990. *
  19991. * @example
  19992. * var interval = new Cesium.TimeInterval(...);
  19993. * when(Cesium.Transforms.preloadIcrfFixed(interval), function() {
  19994. * // the data is now loaded
  19995. * });
  19996. *
  19997. * @see Transforms.computeIcrfToFixedMatrix
  19998. * @see Transforms.computeFixedToIcrfMatrix
  19999. * @see when
  20000. */
  20001. Transforms.preloadIcrfFixed = function(timeInterval) {
  20002. var startDayTT = timeInterval.start.dayNumber;
  20003. var startSecondTT = timeInterval.start.secondsOfDay + ttMinusTai;
  20004. var stopDayTT = timeInterval.stop.dayNumber;
  20005. var stopSecondTT = timeInterval.stop.secondsOfDay + ttMinusTai;
  20006. var xysPromise = Transforms.iau2006XysData.preload(startDayTT, startSecondTT, stopDayTT, stopSecondTT);
  20007. var eopPromise = Transforms.earthOrientationParameters.getPromiseToLoad();
  20008. return when.all([xysPromise, eopPromise]);
  20009. };
  20010. /**
  20011. * Computes a rotation matrix to transform a point or vector from the International Celestial
  20012. * Reference Frame (GCRF/ICRF) inertial frame axes to the Earth-Fixed frame axes (ITRF)
  20013. * at a given time. This function may return undefined if the data necessary to
  20014. * do the transformation is not yet loaded.
  20015. *
  20016. * @param {JulianDate} date The time at which to compute the rotation matrix.
  20017. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  20018. * not specified, a new instance is created and returned.
  20019. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  20020. * transformation is not yet loaded.
  20021. *
  20022. *
  20023. * @example
  20024. * scene.preRender.addEventListener(function(scene, time) {
  20025. * var icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(time);
  20026. * if (Cesium.defined(icrfToFixed)) {
  20027. * var offset = Cesium.Matrix4.multiplyByPoint(camera.transform, camera.position, new Cesium.Cartesian3());
  20028. * var transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed)
  20029. * var inverseTransform = Cesium.Matrix4.inverseTransformation(transform, new Cesium.Matrix4());
  20030. * Cesium.Matrix4.multiplyByPoint(inverseTransform, offset, offset);
  20031. * camera.lookAtTransform(transform, offset);
  20032. * }
  20033. * });
  20034. *
  20035. * @see Transforms.preloadIcrfFixed
  20036. */
  20037. Transforms.computeIcrfToFixedMatrix = function(date, result) {
  20038. if (!defined(date)) {
  20039. throw new DeveloperError('date is required.');
  20040. }
  20041. if (!defined(result)) {
  20042. result = new Matrix3();
  20043. }
  20044. var fixedToIcrfMtx = Transforms.computeFixedToIcrfMatrix(date, result);
  20045. if (!defined(fixedToIcrfMtx)) {
  20046. return undefined;
  20047. }
  20048. return Matrix3.transpose(fixedToIcrfMtx, result);
  20049. };
  20050. var xysScratch = new Iau2006XysSample(0.0, 0.0, 0.0);
  20051. var eopScratch = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  20052. var rotation1Scratch = new Matrix3();
  20053. var rotation2Scratch = new Matrix3();
  20054. /**
  20055. * Computes a rotation matrix to transform a point or vector from the Earth-Fixed frame axes (ITRF)
  20056. * to the International Celestial Reference Frame (GCRF/ICRF) inertial frame axes
  20057. * at a given time. This function may return undefined if the data necessary to
  20058. * do the transformation is not yet loaded.
  20059. *
  20060. * @param {JulianDate} date The time at which to compute the rotation matrix.
  20061. * @param {Matrix3} [result] The object onto which to store the result. If this parameter is
  20062. * not specified, a new instance is created and returned.
  20063. * @returns {Matrix3} The rotation matrix, or undefined if the data necessary to do the
  20064. * transformation is not yet loaded.
  20065. *
  20066. *
  20067. * @example
  20068. * // Transform a point from the ICRF axes to the Fixed axes.
  20069. * var now = Cesium.JulianDate.now();
  20070. * var pointInFixed = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  20071. * var fixedToIcrf = Cesium.Transforms.computeIcrfToFixedMatrix(now);
  20072. * var pointInInertial = new Cesium.Cartesian3();
  20073. * if (Cesium.defined(fixedToIcrf)) {
  20074. * pointInInertial = Cesium.Matrix3.multiplyByVector(fixedToIcrf, pointInFixed, pointInInertial);
  20075. * }
  20076. *
  20077. * @see Transforms.preloadIcrfFixed
  20078. */
  20079. Transforms.computeFixedToIcrfMatrix = function(date, result) {
  20080. if (!defined(date)) {
  20081. throw new DeveloperError('date is required.');
  20082. }
  20083. if (!defined(result)) {
  20084. result = new Matrix3();
  20085. }
  20086. // Compute pole wander
  20087. var eop = Transforms.earthOrientationParameters.compute(date, eopScratch);
  20088. if (!defined(eop)) {
  20089. return undefined;
  20090. }
  20091. // There is no external conversion to Terrestrial Time (TT).
  20092. // So use International Atomic Time (TAI) and convert using offsets.
  20093. // Here we are assuming that dayTT and secondTT are positive
  20094. var dayTT = date.dayNumber;
  20095. // It's possible here that secondTT could roll over 86400
  20096. // This does not seem to affect the precision (unit tests check for this)
  20097. var secondTT = date.secondsOfDay + ttMinusTai;
  20098. var xys = Transforms.iau2006XysData.computeXysRadians(dayTT, secondTT, xysScratch);
  20099. if (!defined(xys)) {
  20100. return undefined;
  20101. }
  20102. var x = xys.x + eop.xPoleOffset;
  20103. var y = xys.y + eop.yPoleOffset;
  20104. // Compute XYS rotation
  20105. var a = 1.0 / (1.0 + Math.sqrt(1.0 - x * x - y * y));
  20106. var rotation1 = rotation1Scratch;
  20107. rotation1[0] = 1.0 - a * x * x;
  20108. rotation1[3] = -a * x * y;
  20109. rotation1[6] = x;
  20110. rotation1[1] = -a * x * y;
  20111. rotation1[4] = 1 - a * y * y;
  20112. rotation1[7] = y;
  20113. rotation1[2] = -x;
  20114. rotation1[5] = -y;
  20115. rotation1[8] = 1 - a * (x * x + y * y);
  20116. var rotation2 = Matrix3.fromRotationZ(-xys.s, rotation2Scratch);
  20117. var matrixQ = Matrix3.multiply(rotation1, rotation2, rotation1Scratch);
  20118. // Similar to TT conversions above
  20119. // It's possible here that secondTT could roll over 86400
  20120. // This does not seem to affect the precision (unit tests check for this)
  20121. var dateUt1day = date.dayNumber;
  20122. var dateUt1sec = date.secondsOfDay - JulianDate.computeTaiMinusUtc(date) + eop.ut1MinusUtc;
  20123. // Compute Earth rotation angle
  20124. // The IERS standard for era is
  20125. // era = 0.7790572732640 + 1.00273781191135448 * Tu
  20126. // where
  20127. // Tu = JulianDateInUt1 - 2451545.0
  20128. // However, you get much more precision if you make the following simplification
  20129. // era = a + (1 + b) * (JulianDayNumber + FractionOfDay - 2451545)
  20130. // era = a + (JulianDayNumber - 2451545) + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  20131. // era = a + FractionOfDay + b (JulianDayNumber - 2451545 + FractionOfDay)
  20132. // since (JulianDayNumber - 2451545) represents an integer number of revolutions which will be discarded anyway.
  20133. var daysSinceJ2000 = dateUt1day - 2451545;
  20134. var fractionOfDay = dateUt1sec / TimeConstants.SECONDS_PER_DAY;
  20135. var era = 0.7790572732640 + fractionOfDay + 0.00273781191135448 * (daysSinceJ2000 + fractionOfDay);
  20136. era = (era % 1.0) * CesiumMath.TWO_PI;
  20137. var earthRotation = Matrix3.fromRotationZ(era, rotation2Scratch);
  20138. // pseudoFixed to ICRF
  20139. var pfToIcrf = Matrix3.multiply(matrixQ, earthRotation, rotation1Scratch);
  20140. // Compute pole wander matrix
  20141. var cosxp = Math.cos(eop.xPoleWander);
  20142. var cosyp = Math.cos(eop.yPoleWander);
  20143. var sinxp = Math.sin(eop.xPoleWander);
  20144. var sinyp = Math.sin(eop.yPoleWander);
  20145. var ttt = (dayTT - j2000ttDays) + secondTT / TimeConstants.SECONDS_PER_DAY;
  20146. ttt /= 36525.0;
  20147. // approximate sp value in rad
  20148. var sp = -47.0e-6 * ttt * CesiumMath.RADIANS_PER_DEGREE / 3600.0;
  20149. var cossp = Math.cos(sp);
  20150. var sinsp = Math.sin(sp);
  20151. var fToPfMtx = rotation2Scratch;
  20152. fToPfMtx[0] = cosxp * cossp;
  20153. fToPfMtx[1] = cosxp * sinsp;
  20154. fToPfMtx[2] = sinxp;
  20155. fToPfMtx[3] = -cosyp * sinsp + sinyp * sinxp * cossp;
  20156. fToPfMtx[4] = cosyp * cossp + sinyp * sinxp * sinsp;
  20157. fToPfMtx[5] = -sinyp * cosxp;
  20158. fToPfMtx[6] = -sinyp * sinsp - cosyp * sinxp * cossp;
  20159. fToPfMtx[7] = sinyp * cossp - cosyp * sinxp * sinsp;
  20160. fToPfMtx[8] = cosyp * cosxp;
  20161. return Matrix3.multiply(pfToIcrf, fToPfMtx, result);
  20162. };
  20163. var pointToWindowCoordinatesTemp = new Cartesian4();
  20164. /**
  20165. * Transform a point from model coordinates to window coordinates.
  20166. *
  20167. * @param {Matrix4} modelViewProjectionMatrix The 4x4 model-view-projection matrix.
  20168. * @param {Matrix4} viewportTransformation The 4x4 viewport transformation.
  20169. * @param {Cartesian3} point The point to transform.
  20170. * @param {Cartesian2} [result] The object onto which to store the result.
  20171. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  20172. */
  20173. Transforms.pointToWindowCoordinates = function (modelViewProjectionMatrix, viewportTransformation, point, result) {
  20174. result = Transforms.pointToGLWindowCoordinates(modelViewProjectionMatrix, viewportTransformation, point, result);
  20175. result.y = 2.0 * viewportTransformation[5] - result.y;
  20176. return result;
  20177. };
  20178. /**
  20179. * @private
  20180. */
  20181. Transforms.pointToGLWindowCoordinates = function(modelViewProjectionMatrix, viewportTransformation, point, result) {
  20182. if (!defined(modelViewProjectionMatrix)) {
  20183. throw new DeveloperError('modelViewProjectionMatrix is required.');
  20184. }
  20185. if (!defined(viewportTransformation)) {
  20186. throw new DeveloperError('viewportTransformation is required.');
  20187. }
  20188. if (!defined(point)) {
  20189. throw new DeveloperError('point is required.');
  20190. }
  20191. if (!defined(result)) {
  20192. result = new Cartesian2();
  20193. }
  20194. var tmp = pointToWindowCoordinatesTemp;
  20195. Matrix4.multiplyByVector(modelViewProjectionMatrix, Cartesian4.fromElements(point.x, point.y, point.z, 1, tmp), tmp);
  20196. Cartesian4.multiplyByScalar(tmp, 1.0 / tmp.w, tmp);
  20197. Matrix4.multiplyByVector(viewportTransformation, tmp, tmp);
  20198. return Cartesian2.fromCartesian4(tmp, result);
  20199. };
  20200. var normalScratch = new Cartesian3();
  20201. var rightScratch = new Cartesian3();
  20202. var upScratch = new Cartesian3();
  20203. /**
  20204. * @private
  20205. */
  20206. Transforms.rotationMatrixFromPositionVelocity = function(position, velocity, ellipsoid, result) {
  20207. if (!defined(position)) {
  20208. throw new DeveloperError('position is required.');
  20209. }
  20210. if (!defined(velocity)) {
  20211. throw new DeveloperError('velocity is required.');
  20212. }
  20213. var normal = defaultValue(ellipsoid, Ellipsoid.WGS84).geodeticSurfaceNormal(position, normalScratch);
  20214. var right = Cartesian3.cross(velocity, normal, rightScratch);
  20215. if (Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)) {
  20216. right = Cartesian3.clone(Cartesian3.UNIT_X, right);
  20217. }
  20218. var up = Cartesian3.cross(right, velocity, upScratch);
  20219. Cartesian3.cross(velocity, up, right);
  20220. Cartesian3.negate(right, right);
  20221. if (!defined(result)) {
  20222. result = new Matrix3();
  20223. }
  20224. result[0] = velocity.x;
  20225. result[1] = velocity.y;
  20226. result[2] = velocity.z;
  20227. result[3] = right.x;
  20228. result[4] = right.y;
  20229. result[5] = right.z;
  20230. result[6] = up.x;
  20231. result[7] = up.y;
  20232. result[8] = up.z;
  20233. return result;
  20234. };
  20235. var scratchCartographic = new Cartographic();
  20236. var scratchCartesian3Projection = new Cartesian3();
  20237. var scratchCartesian3 = new Cartesian3();
  20238. var scratchCartesian4Origin = new Cartesian4();
  20239. var scratchCartesian4NewOrigin = new Cartesian4();
  20240. var scratchCartesian4NewXAxis = new Cartesian4();
  20241. var scratchCartesian4NewYAxis = new Cartesian4();
  20242. var scratchCartesian4NewZAxis = new Cartesian4();
  20243. var scratchFromENU = new Matrix4();
  20244. var scratchToENU = new Matrix4();
  20245. /**
  20246. * @private
  20247. */
  20248. Transforms.basisTo2D = function(projection, matrix, result) {
  20249. if (!defined(projection)) {
  20250. throw new DeveloperError('projection is required.');
  20251. }
  20252. if (!defined(matrix)) {
  20253. throw new DeveloperError('matrix is required.');
  20254. }
  20255. if (!defined(result)) {
  20256. throw new DeveloperError('result is required.');
  20257. }
  20258. var ellipsoid = projection.ellipsoid;
  20259. var origin = Matrix4.getColumn(matrix, 3, scratchCartesian4Origin);
  20260. var cartographic = ellipsoid.cartesianToCartographic(origin, scratchCartographic);
  20261. var fromENU = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, scratchFromENU);
  20262. var toENU = Matrix4.inverseTransformation(fromENU, scratchToENU);
  20263. var projectedPosition = projection.project(cartographic, scratchCartesian3Projection);
  20264. var newOrigin = scratchCartesian4NewOrigin;
  20265. newOrigin.x = projectedPosition.z;
  20266. newOrigin.y = projectedPosition.x;
  20267. newOrigin.z = projectedPosition.y;
  20268. newOrigin.w = 1.0;
  20269. var xAxis = Matrix4.getColumn(matrix, 0, scratchCartesian3);
  20270. var xScale = Cartesian3.magnitude(xAxis);
  20271. var newXAxis = Matrix4.multiplyByVector(toENU, xAxis, scratchCartesian4NewXAxis);
  20272. Cartesian4.fromElements(newXAxis.z, newXAxis.x, newXAxis.y, 0.0, newXAxis);
  20273. var yAxis = Matrix4.getColumn(matrix, 1, scratchCartesian3);
  20274. var yScale = Cartesian3.magnitude(yAxis);
  20275. var newYAxis = Matrix4.multiplyByVector(toENU, yAxis, scratchCartesian4NewYAxis);
  20276. Cartesian4.fromElements(newYAxis.z, newYAxis.x, newYAxis.y, 0.0, newYAxis);
  20277. var zAxis = Matrix4.getColumn(matrix, 2, scratchCartesian3);
  20278. var zScale = Cartesian3.magnitude(zAxis);
  20279. var newZAxis = scratchCartesian4NewZAxis;
  20280. Cartesian3.cross(newXAxis, newYAxis, newZAxis);
  20281. Cartesian3.normalize(newZAxis, newZAxis);
  20282. Cartesian3.cross(newYAxis, newZAxis, newXAxis);
  20283. Cartesian3.normalize(newXAxis, newXAxis);
  20284. Cartesian3.cross(newZAxis, newXAxis, newYAxis);
  20285. Cartesian3.normalize(newYAxis, newYAxis);
  20286. Cartesian3.multiplyByScalar(newXAxis, xScale, newXAxis);
  20287. Cartesian3.multiplyByScalar(newYAxis, yScale, newYAxis);
  20288. Cartesian3.multiplyByScalar(newZAxis, zScale, newZAxis);
  20289. Matrix4.setColumn(result, 0, newXAxis, result);
  20290. Matrix4.setColumn(result, 1, newYAxis, result);
  20291. Matrix4.setColumn(result, 2, newZAxis, result);
  20292. Matrix4.setColumn(result, 3, newOrigin, result);
  20293. return result;
  20294. };
  20295. return Transforms;
  20296. });
  20297. /*global define*/
  20298. define('Core/EllipsoidTangentPlane',[
  20299. './AxisAlignedBoundingBox',
  20300. './Cartesian2',
  20301. './Cartesian3',
  20302. './Cartesian4',
  20303. './defaultValue',
  20304. './defined',
  20305. './defineProperties',
  20306. './DeveloperError',
  20307. './Ellipsoid',
  20308. './IntersectionTests',
  20309. './Matrix4',
  20310. './Plane',
  20311. './Ray',
  20312. './Transforms'
  20313. ], function(
  20314. AxisAlignedBoundingBox,
  20315. Cartesian2,
  20316. Cartesian3,
  20317. Cartesian4,
  20318. defaultValue,
  20319. defined,
  20320. defineProperties,
  20321. DeveloperError,
  20322. Ellipsoid,
  20323. IntersectionTests,
  20324. Matrix4,
  20325. Plane,
  20326. Ray,
  20327. Transforms) {
  20328. 'use strict';
  20329. var scratchCart4 = new Cartesian4();
  20330. /**
  20331. * A plane tangent to the provided ellipsoid at the provided origin.
  20332. * If origin is not on the surface of the ellipsoid, it's surface projection will be used.
  20333. * If origin is at the center of the ellipsoid, an exception will be thrown.
  20334. * @alias EllipsoidTangentPlane
  20335. * @constructor
  20336. *
  20337. * @param {Cartesian3} origin The point on the surface of the ellipsoid where the tangent plane touches.
  20338. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  20339. *
  20340. * @exception {DeveloperError} origin must not be at the center of the ellipsoid.
  20341. */
  20342. function EllipsoidTangentPlane(origin, ellipsoid) {
  20343. if (!defined(origin)) {
  20344. throw new DeveloperError('origin is required.');
  20345. }
  20346. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  20347. origin = ellipsoid.scaleToGeodeticSurface(origin);
  20348. if (!defined(origin)) {
  20349. throw new DeveloperError('origin must not be at the center of the ellipsoid.');
  20350. }
  20351. var eastNorthUp = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid);
  20352. this._ellipsoid = ellipsoid;
  20353. this._origin = origin;
  20354. this._xAxis = Cartesian3.fromCartesian4(Matrix4.getColumn(eastNorthUp, 0, scratchCart4));
  20355. this._yAxis = Cartesian3.fromCartesian4(Matrix4.getColumn(eastNorthUp, 1, scratchCart4));
  20356. var normal = Cartesian3.fromCartesian4(Matrix4.getColumn(eastNorthUp, 2, scratchCart4));
  20357. this._plane = Plane.fromPointNormal(origin, normal);
  20358. }
  20359. defineProperties(EllipsoidTangentPlane.prototype, {
  20360. /**
  20361. * Gets the ellipsoid.
  20362. * @memberof EllipsoidTangentPlane.prototype
  20363. * @type {Ellipsoid}
  20364. */
  20365. ellipsoid : {
  20366. get : function() {
  20367. return this._ellipsoid;
  20368. }
  20369. },
  20370. /**
  20371. * Gets the origin.
  20372. * @memberof EllipsoidTangentPlane.prototype
  20373. * @type {Cartesian3}
  20374. */
  20375. origin : {
  20376. get : function() {
  20377. return this._origin;
  20378. }
  20379. },
  20380. /**
  20381. * Gets the plane which is tangent to the ellipsoid.
  20382. * @memberof EllipsoidTangentPlane.prototype
  20383. * @readonly
  20384. * @type {Plane}
  20385. */
  20386. plane : {
  20387. get : function() {
  20388. return this._plane;
  20389. }
  20390. },
  20391. /**
  20392. * Gets the local X-axis (east) of the tangent plane.
  20393. * @memberof EllipsoidTangentPlane.prototype
  20394. * @readonly
  20395. * @type {Cartesian3}
  20396. */
  20397. xAxis : {
  20398. get : function() {
  20399. return this._xAxis;
  20400. }
  20401. },
  20402. /**
  20403. * Gets the local Y-axis (north) of the tangent plane.
  20404. * @memberof EllipsoidTangentPlane.prototype
  20405. * @readonly
  20406. * @type {Cartesian3}
  20407. */
  20408. yAxis : {
  20409. get : function() {
  20410. return this._yAxis;
  20411. }
  20412. },
  20413. /**
  20414. * Gets the local Z-axis (up) of the tangent plane.
  20415. * @member EllipsoidTangentPlane.prototype
  20416. * @readonly
  20417. * @type {Cartesian3}
  20418. */
  20419. zAxis : {
  20420. get : function() {
  20421. return this._plane.normal;
  20422. }
  20423. }
  20424. });
  20425. var tmp = new AxisAlignedBoundingBox();
  20426. /**
  20427. * Creates a new instance from the provided ellipsoid and the center
  20428. * point of the provided Cartesians.
  20429. *
  20430. * @param {Ellipsoid} ellipsoid The ellipsoid to use.
  20431. * @param {Cartesian3} cartesians The list of positions surrounding the center point.
  20432. */
  20433. EllipsoidTangentPlane.fromPoints = function(cartesians, ellipsoid) {
  20434. if (!defined(cartesians)) {
  20435. throw new DeveloperError('cartesians is required.');
  20436. }
  20437. var box = AxisAlignedBoundingBox.fromPoints(cartesians, tmp);
  20438. return new EllipsoidTangentPlane(box.center, ellipsoid);
  20439. };
  20440. var scratchProjectPointOntoPlaneRay = new Ray();
  20441. var scratchProjectPointOntoPlaneCartesian3 = new Cartesian3();
  20442. /**
  20443. * Computes the projection of the provided 3D position onto the 2D plane, radially outward from the {@link EllipsoidTangentPlane.ellipsoid} coordinate system origin.
  20444. *
  20445. * @param {Cartesian3} cartesian The point to project.
  20446. * @param {Cartesian2} [result] The object onto which to store the result.
  20447. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided. Undefined if there is no intersection point
  20448. */
  20449. EllipsoidTangentPlane.prototype.projectPointOntoPlane = function(cartesian, result) {
  20450. if (!defined(cartesian)) {
  20451. throw new DeveloperError('cartesian is required.');
  20452. }
  20453. var ray = scratchProjectPointOntoPlaneRay;
  20454. ray.origin = cartesian;
  20455. Cartesian3.normalize(cartesian, ray.direction);
  20456. var intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20457. if (!defined(intersectionPoint)) {
  20458. Cartesian3.negate(ray.direction, ray.direction);
  20459. intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20460. }
  20461. if (defined(intersectionPoint)) {
  20462. var v = Cartesian3.subtract(intersectionPoint, this._origin, intersectionPoint);
  20463. var x = Cartesian3.dot(this._xAxis, v);
  20464. var y = Cartesian3.dot(this._yAxis, v);
  20465. if (!defined(result)) {
  20466. return new Cartesian2(x, y);
  20467. }
  20468. result.x = x;
  20469. result.y = y;
  20470. return result;
  20471. }
  20472. return undefined;
  20473. };
  20474. /**
  20475. * Computes the projection of the provided 3D positions onto the 2D plane (where possible), radially outward from the global origin.
  20476. * The resulting array may be shorter than the input array - if a single projection is impossible it will not be included.
  20477. *
  20478. * @see EllipsoidTangentPlane.projectPointOntoPlane
  20479. *
  20480. * @param {Cartesian3[]} cartesians The array of points to project.
  20481. * @param {Cartesian2[]} [result] The array of Cartesian2 instances onto which to store results.
  20482. * @returns {Cartesian2[]} The modified result parameter or a new array of Cartesian2 instances if none was provided.
  20483. */
  20484. EllipsoidTangentPlane.prototype.projectPointsOntoPlane = function(cartesians, result) {
  20485. if (!defined(cartesians)) {
  20486. throw new DeveloperError('cartesians is required.');
  20487. }
  20488. if (!defined(result)) {
  20489. result = [];
  20490. }
  20491. var count = 0;
  20492. var length = cartesians.length;
  20493. for ( var i = 0; i < length; i++) {
  20494. var p = this.projectPointOntoPlane(cartesians[i], result[count]);
  20495. if (defined(p)) {
  20496. result[count] = p;
  20497. count++;
  20498. }
  20499. }
  20500. result.length = count;
  20501. return result;
  20502. };
  20503. /**
  20504. * Computes the projection of the provided 3D position onto the 2D plane, along the plane normal.
  20505. *
  20506. * @param {Cartesian3} cartesian The point to project.
  20507. * @param {Cartesian2} [result] The object onto which to store the result.
  20508. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  20509. */
  20510. EllipsoidTangentPlane.prototype.projectPointToNearestOnPlane = function(cartesian, result) {
  20511. if (!defined(cartesian)) {
  20512. throw new DeveloperError('cartesian is required.');
  20513. }
  20514. if (!defined(result)) {
  20515. result = new Cartesian2();
  20516. }
  20517. var ray = scratchProjectPointOntoPlaneRay;
  20518. ray.origin = cartesian;
  20519. Cartesian3.clone(this._plane.normal, ray.direction);
  20520. var intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20521. if (!defined(intersectionPoint)) {
  20522. Cartesian3.negate(ray.direction, ray.direction);
  20523. intersectionPoint = IntersectionTests.rayPlane(ray, this._plane, scratchProjectPointOntoPlaneCartesian3);
  20524. }
  20525. var v = Cartesian3.subtract(intersectionPoint, this._origin, intersectionPoint);
  20526. var x = Cartesian3.dot(this._xAxis, v);
  20527. var y = Cartesian3.dot(this._yAxis, v);
  20528. result.x = x;
  20529. result.y = y;
  20530. return result;
  20531. };
  20532. /**
  20533. * Computes the projection of the provided 3D positions onto the 2D plane, along the plane normal.
  20534. *
  20535. * @see EllipsoidTangentPlane.projectPointToNearestOnPlane
  20536. *
  20537. * @param {Cartesian3[]} cartesians The array of points to project.
  20538. * @param {Cartesian2[]} [result] The array of Cartesian2 instances onto which to store results.
  20539. * @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>.
  20540. */
  20541. EllipsoidTangentPlane.prototype.projectPointsToNearestOnPlane = function(cartesians, result) {
  20542. if (!defined(cartesians)) {
  20543. throw new DeveloperError('cartesians is required.');
  20544. }
  20545. if (!defined(result)) {
  20546. result = [];
  20547. }
  20548. var length = cartesians.length;
  20549. result.length = length;
  20550. for (var i = 0; i < length; i++) {
  20551. result[i] = this.projectPointToNearestOnPlane(cartesians[i], result[i]);
  20552. }
  20553. return result;
  20554. };
  20555. var projectPointsOntoEllipsoidScratch = new Cartesian3();
  20556. /**
  20557. * Computes the projection of the provided 2D positions onto the 3D ellipsoid.
  20558. *
  20559. * @param {Cartesian2[]} cartesians The array of points to project.
  20560. * @param {Cartesian3[]} [result] The array of Cartesian3 instances onto which to store results.
  20561. * @returns {Cartesian3[]} The modified result parameter or a new array of Cartesian3 instances if none was provided.
  20562. */
  20563. EllipsoidTangentPlane.prototype.projectPointsOntoEllipsoid = function(cartesians, result) {
  20564. if (!defined(cartesians)) {
  20565. throw new DeveloperError('cartesians is required.');
  20566. }
  20567. var length = cartesians.length;
  20568. if (!defined(result)) {
  20569. result = new Array(length);
  20570. } else {
  20571. result.length = length;
  20572. }
  20573. var ellipsoid = this._ellipsoid;
  20574. var origin = this._origin;
  20575. var xAxis = this._xAxis;
  20576. var yAxis = this._yAxis;
  20577. var tmp = projectPointsOntoEllipsoidScratch;
  20578. for ( var i = 0; i < length; ++i) {
  20579. var position = cartesians[i];
  20580. Cartesian3.multiplyByScalar(xAxis, position.x, tmp);
  20581. if (!defined(result[i])) {
  20582. result[i] = new Cartesian3();
  20583. }
  20584. var point = Cartesian3.add(origin, tmp, result[i]);
  20585. Cartesian3.multiplyByScalar(yAxis, position.y, tmp);
  20586. Cartesian3.add(point, tmp, point);
  20587. ellipsoid.scaleToGeocentricSurface(point, point);
  20588. }
  20589. return result;
  20590. };
  20591. return EllipsoidTangentPlane;
  20592. });
  20593. /*global define*/
  20594. define('ThirdParty/earcut-2.1.1',[], function() {
  20595. 'use strict';
  20596. function earcut(data, holeIndices, dim) {
  20597. dim = dim || 2;
  20598. var hasHoles = holeIndices && holeIndices.length,
  20599. outerLen = hasHoles ? holeIndices[0] * dim : data.length,
  20600. outerNode = linkedList(data, 0, outerLen, dim, true),
  20601. triangles = [];
  20602. if (!outerNode) return triangles;
  20603. var minX, minY, maxX, maxY, x, y, size;
  20604. if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
  20605. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  20606. if (data.length > 80 * dim) {
  20607. minX = maxX = data[0];
  20608. minY = maxY = data[1];
  20609. for (var i = dim; i < outerLen; i += dim) {
  20610. x = data[i];
  20611. y = data[i + 1];
  20612. if (x < minX) minX = x;
  20613. if (y < minY) minY = y;
  20614. if (x > maxX) maxX = x;
  20615. if (y > maxY) maxY = y;
  20616. }
  20617. // minX, minY and size are later used to transform coords into integers for z-order calculation
  20618. size = Math.max(maxX - minX, maxY - minY);
  20619. }
  20620. earcutLinked(outerNode, triangles, dim, minX, minY, size);
  20621. return triangles;
  20622. }
  20623. // create a circular doubly linked list from polygon points in the specified winding order
  20624. function linkedList(data, start, end, dim, clockwise) {
  20625. var i, last;
  20626. if (clockwise === (signedArea(data, start, end, dim) > 0)) {
  20627. for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
  20628. } else {
  20629. for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
  20630. }
  20631. if (last && equals(last, last.next)) {
  20632. removeNode(last);
  20633. last = last.next;
  20634. }
  20635. return last;
  20636. }
  20637. // eliminate colinear or duplicate points
  20638. function filterPoints(start, end) {
  20639. if (!start) return start;
  20640. if (!end) end = start;
  20641. var p = start,
  20642. again;
  20643. do {
  20644. again = false;
  20645. if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
  20646. removeNode(p);
  20647. p = end = p.prev;
  20648. if (p === p.next) return null;
  20649. again = true;
  20650. } else {
  20651. p = p.next;
  20652. }
  20653. } while (again || p !== end);
  20654. return end;
  20655. }
  20656. // main ear slicing loop which triangulates a polygon (given as a linked list)
  20657. function earcutLinked(ear, triangles, dim, minX, minY, size, pass) {
  20658. if (!ear) return;
  20659. // interlink polygon nodes in z-order
  20660. if (!pass && size) indexCurve(ear, minX, minY, size);
  20661. var stop = ear,
  20662. prev, next;
  20663. // iterate through ears, slicing them one by one
  20664. while (ear.prev !== ear.next) {
  20665. prev = ear.prev;
  20666. next = ear.next;
  20667. if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) {
  20668. // cut off the triangle
  20669. triangles.push(prev.i / dim);
  20670. triangles.push(ear.i / dim);
  20671. triangles.push(next.i / dim);
  20672. removeNode(ear);
  20673. // skipping the next vertice leads to less sliver triangles
  20674. ear = next.next;
  20675. stop = next.next;
  20676. continue;
  20677. }
  20678. ear = next;
  20679. // if we looped through the whole remaining polygon and can't find any more ears
  20680. if (ear === stop) {
  20681. // try filtering points and slicing again
  20682. if (!pass) {
  20683. earcutLinked(filterPoints(ear), triangles, dim, minX, minY, size, 1);
  20684. // if this didn't work, try curing all small self-intersections locally
  20685. } else if (pass === 1) {
  20686. ear = cureLocalIntersections(ear, triangles, dim);
  20687. earcutLinked(ear, triangles, dim, minX, minY, size, 2);
  20688. // as a last resort, try splitting the remaining polygon into two
  20689. } else if (pass === 2) {
  20690. splitEarcut(ear, triangles, dim, minX, minY, size);
  20691. }
  20692. break;
  20693. }
  20694. }
  20695. }
  20696. // check whether a polygon node forms a valid ear with adjacent nodes
  20697. function isEar(ear) {
  20698. var a = ear.prev,
  20699. b = ear,
  20700. c = ear.next;
  20701. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  20702. // now make sure we don't have other points inside the potential ear
  20703. var p = ear.next.next;
  20704. while (p !== ear.prev) {
  20705. if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  20706. area(p.prev, p, p.next) >= 0) return false;
  20707. p = p.next;
  20708. }
  20709. return true;
  20710. }
  20711. function isEarHashed(ear, minX, minY, size) {
  20712. var a = ear.prev,
  20713. b = ear,
  20714. c = ear.next;
  20715. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  20716. // triangle bbox; min & max are calculated like this for speed
  20717. var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x),
  20718. minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y),
  20719. maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x),
  20720. maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y);
  20721. // z-order range for the current triangle bbox;
  20722. var minZ = zOrder(minTX, minTY, minX, minY, size),
  20723. maxZ = zOrder(maxTX, maxTY, minX, minY, size);
  20724. // first look for points inside the triangle in increasing z-order
  20725. var p = ear.nextZ;
  20726. while (p && p.z <= maxZ) {
  20727. if (p !== ear.prev && p !== ear.next &&
  20728. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  20729. area(p.prev, p, p.next) >= 0) return false;
  20730. p = p.nextZ;
  20731. }
  20732. // then look for points in decreasing z-order
  20733. p = ear.prevZ;
  20734. while (p && p.z >= minZ) {
  20735. if (p !== ear.prev && p !== ear.next &&
  20736. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  20737. area(p.prev, p, p.next) >= 0) return false;
  20738. p = p.prevZ;
  20739. }
  20740. return true;
  20741. }
  20742. // go through all polygon nodes and cure small local self-intersections
  20743. function cureLocalIntersections(start, triangles, dim) {
  20744. var p = start;
  20745. do {
  20746. var a = p.prev,
  20747. b = p.next.next;
  20748. if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
  20749. triangles.push(a.i / dim);
  20750. triangles.push(p.i / dim);
  20751. triangles.push(b.i / dim);
  20752. // remove two nodes involved
  20753. removeNode(p);
  20754. removeNode(p.next);
  20755. p = start = b;
  20756. }
  20757. p = p.next;
  20758. } while (p !== start);
  20759. return p;
  20760. }
  20761. // try splitting polygon into two and triangulate them independently
  20762. function splitEarcut(start, triangles, dim, minX, minY, size) {
  20763. // look for a valid diagonal that divides the polygon into two
  20764. var a = start;
  20765. do {
  20766. var b = a.next.next;
  20767. while (b !== a.prev) {
  20768. if (a.i !== b.i && isValidDiagonal(a, b)) {
  20769. // split the polygon in two by the diagonal
  20770. var c = splitPolygon(a, b);
  20771. // filter colinear points around the cuts
  20772. a = filterPoints(a, a.next);
  20773. c = filterPoints(c, c.next);
  20774. // run earcut on each half
  20775. earcutLinked(a, triangles, dim, minX, minY, size);
  20776. earcutLinked(c, triangles, dim, minX, minY, size);
  20777. return;
  20778. }
  20779. b = b.next;
  20780. }
  20781. a = a.next;
  20782. } while (a !== start);
  20783. }
  20784. // link every hole into the outer loop, producing a single-ring polygon without holes
  20785. function eliminateHoles(data, holeIndices, outerNode, dim) {
  20786. var queue = [],
  20787. i, len, start, end, list;
  20788. for (i = 0, len = holeIndices.length; i < len; i++) {
  20789. start = holeIndices[i] * dim;
  20790. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  20791. list = linkedList(data, start, end, dim, false);
  20792. if (list === list.next) list.steiner = true;
  20793. queue.push(getLeftmost(list));
  20794. }
  20795. queue.sort(compareX);
  20796. // process holes from left to right
  20797. for (i = 0; i < queue.length; i++) {
  20798. eliminateHole(queue[i], outerNode);
  20799. outerNode = filterPoints(outerNode, outerNode.next);
  20800. }
  20801. return outerNode;
  20802. }
  20803. function compareX(a, b) {
  20804. return a.x - b.x;
  20805. }
  20806. // find a bridge between vertices that connects hole with an outer ring and and link it
  20807. function eliminateHole(hole, outerNode) {
  20808. outerNode = findHoleBridge(hole, outerNode);
  20809. if (outerNode) {
  20810. var b = splitPolygon(outerNode, hole);
  20811. filterPoints(b, b.next);
  20812. }
  20813. }
  20814. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  20815. function findHoleBridge(hole, outerNode) {
  20816. var p = outerNode,
  20817. hx = hole.x,
  20818. hy = hole.y,
  20819. qx = -Infinity,
  20820. m;
  20821. // find a segment intersected by a ray from the hole's leftmost point to the left;
  20822. // segment's endpoint with lesser x will be potential connection point
  20823. do {
  20824. if (hy <= p.y && hy >= p.next.y) {
  20825. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  20826. if (x <= hx && x > qx) {
  20827. qx = x;
  20828. if (x === hx) {
  20829. if (hy === p.y) return p;
  20830. if (hy === p.next.y) return p.next;
  20831. }
  20832. m = p.x < p.next.x ? p : p.next;
  20833. }
  20834. }
  20835. p = p.next;
  20836. } while (p !== outerNode);
  20837. if (!m) return null;
  20838. if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint
  20839. // look for points inside the triangle of hole point, segment intersection and endpoint;
  20840. // if there are no points found, we have a valid connection;
  20841. // otherwise choose the point of the minimum angle with the ray as connection point
  20842. var stop = m,
  20843. mx = m.x,
  20844. my = m.y,
  20845. tanMin = Infinity,
  20846. tan;
  20847. p = m.next;
  20848. while (p !== stop) {
  20849. if (hx >= p.x && p.x >= mx &&
  20850. pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  20851. tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
  20852. if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) {
  20853. m = p;
  20854. tanMin = tan;
  20855. }
  20856. }
  20857. p = p.next;
  20858. }
  20859. return m;
  20860. }
  20861. // interlink polygon nodes in z-order
  20862. function indexCurve(start, minX, minY, size) {
  20863. var p = start;
  20864. do {
  20865. if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, size);
  20866. p.prevZ = p.prev;
  20867. p.nextZ = p.next;
  20868. p = p.next;
  20869. } while (p !== start);
  20870. p.prevZ.nextZ = null;
  20871. p.prevZ = null;
  20872. sortLinked(p);
  20873. }
  20874. // Simon Tatham's linked list merge sort algorithm
  20875. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  20876. function sortLinked(list) {
  20877. var i, p, q, e, tail, numMerges, pSize, qSize,
  20878. inSize = 1;
  20879. do {
  20880. p = list;
  20881. list = null;
  20882. tail = null;
  20883. numMerges = 0;
  20884. while (p) {
  20885. numMerges++;
  20886. q = p;
  20887. pSize = 0;
  20888. for (i = 0; i < inSize; i++) {
  20889. pSize++;
  20890. q = q.nextZ;
  20891. if (!q) break;
  20892. }
  20893. qSize = inSize;
  20894. while (pSize > 0 || (qSize > 0 && q)) {
  20895. if (pSize === 0) {
  20896. e = q;
  20897. q = q.nextZ;
  20898. qSize--;
  20899. } else if (qSize === 0 || !q) {
  20900. e = p;
  20901. p = p.nextZ;
  20902. pSize--;
  20903. } else if (p.z <= q.z) {
  20904. e = p;
  20905. p = p.nextZ;
  20906. pSize--;
  20907. } else {
  20908. e = q;
  20909. q = q.nextZ;
  20910. qSize--;
  20911. }
  20912. if (tail) tail.nextZ = e;
  20913. else list = e;
  20914. e.prevZ = tail;
  20915. tail = e;
  20916. }
  20917. p = q;
  20918. }
  20919. tail.nextZ = null;
  20920. inSize *= 2;
  20921. } while (numMerges > 1);
  20922. return list;
  20923. }
  20924. // z-order of a point given coords and size of the data bounding box
  20925. function zOrder(x, y, minX, minY, size) {
  20926. // coords are transformed into non-negative 15-bit integer range
  20927. x = 32767 * (x - minX) / size;
  20928. y = 32767 * (y - minY) / size;
  20929. x = (x | (x << 8)) & 0x00FF00FF;
  20930. x = (x | (x << 4)) & 0x0F0F0F0F;
  20931. x = (x | (x << 2)) & 0x33333333;
  20932. x = (x | (x << 1)) & 0x55555555;
  20933. y = (y | (y << 8)) & 0x00FF00FF;
  20934. y = (y | (y << 4)) & 0x0F0F0F0F;
  20935. y = (y | (y << 2)) & 0x33333333;
  20936. y = (y | (y << 1)) & 0x55555555;
  20937. return x | (y << 1);
  20938. }
  20939. // find the leftmost node of a polygon ring
  20940. function getLeftmost(start) {
  20941. var p = start,
  20942. leftmost = start;
  20943. do {
  20944. if (p.x < leftmost.x) leftmost = p;
  20945. p = p.next;
  20946. } while (p !== start);
  20947. return leftmost;
  20948. }
  20949. // check if a point lies within a convex triangle
  20950. function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  20951. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  20952. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  20953. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  20954. }
  20955. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  20956. function isValidDiagonal(a, b) {
  20957. return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
  20958. locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
  20959. }
  20960. // signed area of a triangle
  20961. function area(p, q, r) {
  20962. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  20963. }
  20964. // check if two points are equal
  20965. function equals(p1, p2) {
  20966. return p1.x === p2.x && p1.y === p2.y;
  20967. }
  20968. // check if two segments intersect
  20969. function intersects(p1, q1, p2, q2) {
  20970. if ((equals(p1, q1) && equals(p2, q2)) ||
  20971. (equals(p1, q2) && equals(p2, q1))) return true;
  20972. return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 &&
  20973. area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0;
  20974. }
  20975. // check if a polygon diagonal intersects any polygon segments
  20976. function intersectsPolygon(a, b) {
  20977. var p = a;
  20978. do {
  20979. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  20980. intersects(p, p.next, a, b)) return true;
  20981. p = p.next;
  20982. } while (p !== a);
  20983. return false;
  20984. }
  20985. // check if a polygon diagonal is locally inside the polygon
  20986. function locallyInside(a, b) {
  20987. return area(a.prev, a, a.next) < 0 ?
  20988. area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
  20989. area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
  20990. }
  20991. // check if the middle point of a polygon diagonal is inside the polygon
  20992. function middleInside(a, b) {
  20993. var p = a,
  20994. inside = false,
  20995. px = (a.x + b.x) / 2,
  20996. py = (a.y + b.y) / 2;
  20997. do {
  20998. if (((p.y > py) !== (p.next.y > py)) && (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  20999. inside = !inside;
  21000. p = p.next;
  21001. } while (p !== a);
  21002. return inside;
  21003. }
  21004. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  21005. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  21006. function splitPolygon(a, b) {
  21007. var a2 = new Node(a.i, a.x, a.y),
  21008. b2 = new Node(b.i, b.x, b.y),
  21009. an = a.next,
  21010. bp = b.prev;
  21011. a.next = b;
  21012. b.prev = a;
  21013. a2.next = an;
  21014. an.prev = a2;
  21015. b2.next = a2;
  21016. a2.prev = b2;
  21017. bp.next = b2;
  21018. b2.prev = bp;
  21019. return b2;
  21020. }
  21021. // create a node and optionally link it with previous one (in a circular doubly linked list)
  21022. function insertNode(i, x, y, last) {
  21023. var p = new Node(i, x, y);
  21024. if (!last) {
  21025. p.prev = p;
  21026. p.next = p;
  21027. } else {
  21028. p.next = last.next;
  21029. p.prev = last;
  21030. last.next.prev = p;
  21031. last.next = p;
  21032. }
  21033. return p;
  21034. }
  21035. function removeNode(p) {
  21036. p.next.prev = p.prev;
  21037. p.prev.next = p.next;
  21038. if (p.prevZ) p.prevZ.nextZ = p.nextZ;
  21039. if (p.nextZ) p.nextZ.prevZ = p.prevZ;
  21040. }
  21041. function Node(i, x, y) {
  21042. // vertice index in coordinates array
  21043. this.i = i;
  21044. // vertex coordinates
  21045. this.x = x;
  21046. this.y = y;
  21047. // previous and next vertice nodes in a polygon ring
  21048. this.prev = null;
  21049. this.next = null;
  21050. // z-order curve value
  21051. this.z = null;
  21052. // previous and next nodes in z-order
  21053. this.prevZ = null;
  21054. this.nextZ = null;
  21055. // indicates whether this is a steiner point
  21056. this.steiner = false;
  21057. }
  21058. // return a percentage difference between the polygon area and its triangulation area;
  21059. // used to verify correctness of triangulation
  21060. earcut.deviation = function (data, holeIndices, dim, triangles) {
  21061. var hasHoles = holeIndices && holeIndices.length;
  21062. var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  21063. var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
  21064. if (hasHoles) {
  21065. for (var i = 0, len = holeIndices.length; i < len; i++) {
  21066. var start = holeIndices[i] * dim;
  21067. var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  21068. polygonArea -= Math.abs(signedArea(data, start, end, dim));
  21069. }
  21070. }
  21071. var trianglesArea = 0;
  21072. for (i = 0; i < triangles.length; i += 3) {
  21073. var a = triangles[i] * dim;
  21074. var b = triangles[i + 1] * dim;
  21075. var c = triangles[i + 2] * dim;
  21076. trianglesArea += Math.abs(
  21077. (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
  21078. (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
  21079. }
  21080. return polygonArea === 0 && trianglesArea === 0 ? 0 :
  21081. Math.abs((trianglesArea - polygonArea) / polygonArea);
  21082. };
  21083. function signedArea(data, start, end, dim) {
  21084. var sum = 0;
  21085. for (var i = start, j = end - dim; i < end; i += dim) {
  21086. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  21087. j = i;
  21088. }
  21089. return sum;
  21090. }
  21091. // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
  21092. earcut.flatten = function (data) {
  21093. var dim = data[0][0].length,
  21094. result = {vertices: [], holes: [], dimensions: dim},
  21095. holeIndex = 0;
  21096. for (var i = 0; i < data.length; i++) {
  21097. for (var j = 0; j < data[i].length; j++) {
  21098. for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
  21099. }
  21100. if (i > 0) {
  21101. holeIndex += data[i - 1].length;
  21102. result.holes.push(holeIndex);
  21103. }
  21104. }
  21105. return result;
  21106. };
  21107. return earcut;
  21108. });
  21109. /*global define*/
  21110. define('Core/WindingOrder',[
  21111. './freezeObject',
  21112. './WebGLConstants'
  21113. ], function(
  21114. freezeObject,
  21115. WebGLConstants) {
  21116. 'use strict';
  21117. /**
  21118. * Winding order defines the order of vertices for a triangle to be considered front-facing.
  21119. *
  21120. * @exports WindingOrder
  21121. */
  21122. var WindingOrder = {
  21123. /**
  21124. * Vertices are in clockwise order.
  21125. *
  21126. * @type {Number}
  21127. * @constant
  21128. */
  21129. CLOCKWISE : WebGLConstants.CW,
  21130. /**
  21131. * Vertices are in counter-clockwise order.
  21132. *
  21133. * @type {Number}
  21134. * @constant
  21135. */
  21136. COUNTER_CLOCKWISE : WebGLConstants.CCW,
  21137. /**
  21138. * @private
  21139. */
  21140. validate : function(windingOrder) {
  21141. return windingOrder === WindingOrder.CLOCKWISE ||
  21142. windingOrder === WindingOrder.COUNTER_CLOCKWISE;
  21143. }
  21144. };
  21145. return freezeObject(WindingOrder);
  21146. });
  21147. /*global define*/
  21148. define('Core/PolygonPipeline',[
  21149. '../ThirdParty/earcut-2.1.1',
  21150. './Cartesian2',
  21151. './Cartesian3',
  21152. './ComponentDatatype',
  21153. './defaultValue',
  21154. './defined',
  21155. './DeveloperError',
  21156. './Ellipsoid',
  21157. './Geometry',
  21158. './GeometryAttribute',
  21159. './Math',
  21160. './PrimitiveType',
  21161. './WindingOrder'
  21162. ], function(
  21163. earcut,
  21164. Cartesian2,
  21165. Cartesian3,
  21166. ComponentDatatype,
  21167. defaultValue,
  21168. defined,
  21169. DeveloperError,
  21170. Ellipsoid,
  21171. Geometry,
  21172. GeometryAttribute,
  21173. CesiumMath,
  21174. PrimitiveType,
  21175. WindingOrder) {
  21176. 'use strict';
  21177. var scaleToGeodeticHeightN = new Cartesian3();
  21178. var scaleToGeodeticHeightP = new Cartesian3();
  21179. /**
  21180. * @private
  21181. */
  21182. var PolygonPipeline = {};
  21183. /**
  21184. * @exception {DeveloperError} At least three positions are required.
  21185. */
  21186. PolygonPipeline.computeArea2D = function(positions) {
  21187. if (!defined(positions)) {
  21188. throw new DeveloperError('positions is required.');
  21189. }
  21190. if (positions.length < 3) {
  21191. throw new DeveloperError('At least three positions are required.');
  21192. }
  21193. var length = positions.length;
  21194. var area = 0.0;
  21195. for ( var i0 = length - 1, i1 = 0; i1 < length; i0 = i1++) {
  21196. var v0 = positions[i0];
  21197. var v1 = positions[i1];
  21198. area += (v0.x * v1.y) - (v1.x * v0.y);
  21199. }
  21200. return area * 0.5;
  21201. };
  21202. /**
  21203. * @returns {WindingOrder} The winding order.
  21204. *
  21205. * @exception {DeveloperError} At least three positions are required.
  21206. */
  21207. PolygonPipeline.computeWindingOrder2D = function(positions) {
  21208. var area = PolygonPipeline.computeArea2D(positions);
  21209. return (area > 0.0) ? WindingOrder.COUNTER_CLOCKWISE : WindingOrder.CLOCKWISE;
  21210. };
  21211. /**
  21212. * Triangulate a polygon.
  21213. *
  21214. * @param {Cartesian2[]} positions Cartesian2 array containing the vertices of the polygon
  21215. * @param {Number[]} [holes] An array of the staring indices of the holes.
  21216. * @returns {Number[]} Index array representing triangles that fill the polygon
  21217. */
  21218. PolygonPipeline.triangulate = function(positions, holes) {
  21219. if (!defined(positions)) {
  21220. throw new DeveloperError('positions is required.');
  21221. }
  21222. var flattenedPositions = Cartesian2.packArray(positions);
  21223. return earcut(flattenedPositions, holes, 2);
  21224. };
  21225. var subdivisionV0Scratch = new Cartesian3();
  21226. var subdivisionV1Scratch = new Cartesian3();
  21227. var subdivisionV2Scratch = new Cartesian3();
  21228. var subdivisionS0Scratch = new Cartesian3();
  21229. var subdivisionS1Scratch = new Cartesian3();
  21230. var subdivisionS2Scratch = new Cartesian3();
  21231. var subdivisionMidScratch = new Cartesian3();
  21232. /**
  21233. * Subdivides positions and raises points to the surface of the ellipsoid.
  21234. *
  21235. * @param {Ellipsoid} ellipsoid The ellipsoid the polygon in on.
  21236. * @param {Cartesian3[]} positions An array of {@link Cartesian3} positions of the polygon.
  21237. * @param {Number[]} indices An array of indices that determines the triangles in the polygon.
  21238. * @param {Number} [granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  21239. *
  21240. * @exception {DeveloperError} At least three indices are required.
  21241. * @exception {DeveloperError} The number of indices must be divisable by three.
  21242. * @exception {DeveloperError} Granularity must be greater than zero.
  21243. */
  21244. PolygonPipeline.computeSubdivision = function(ellipsoid, positions, indices, granularity) {
  21245. granularity = defaultValue(granularity, CesiumMath.RADIANS_PER_DEGREE);
  21246. if (!defined(ellipsoid)) {
  21247. throw new DeveloperError('ellipsoid is required.');
  21248. }
  21249. if (!defined(positions)) {
  21250. throw new DeveloperError('positions is required.');
  21251. }
  21252. if (!defined(indices)) {
  21253. throw new DeveloperError('indices is required.');
  21254. }
  21255. if (indices.length < 3) {
  21256. throw new DeveloperError('At least three indices are required.');
  21257. }
  21258. if (indices.length % 3 !== 0) {
  21259. throw new DeveloperError('The number of indices must be divisable by three.');
  21260. }
  21261. if (granularity <= 0.0) {
  21262. throw new DeveloperError('granularity must be greater than zero.');
  21263. }
  21264. // triangles that need (or might need) to be subdivided.
  21265. var triangles = indices.slice(0);
  21266. // New positions due to edge splits are appended to the positions list.
  21267. var i;
  21268. var length = positions.length;
  21269. var subdividedPositions = new Array(length * 3);
  21270. var q = 0;
  21271. for (i = 0; i < length; i++) {
  21272. var item = positions[i];
  21273. subdividedPositions[q++] = item.x;
  21274. subdividedPositions[q++] = item.y;
  21275. subdividedPositions[q++] = item.z;
  21276. }
  21277. var subdividedIndices = [];
  21278. // Used to make sure shared edges are not split more than once.
  21279. var edges = {};
  21280. var radius = ellipsoid.maximumRadius;
  21281. var minDistance = CesiumMath.chordLength(granularity, radius);
  21282. var minDistanceSqrd = minDistance * minDistance;
  21283. while (triangles.length > 0) {
  21284. var i2 = triangles.pop();
  21285. var i1 = triangles.pop();
  21286. var i0 = triangles.pop();
  21287. var v0 = Cartesian3.fromArray(subdividedPositions, i0 * 3, subdivisionV0Scratch);
  21288. var v1 = Cartesian3.fromArray(subdividedPositions, i1 * 3, subdivisionV1Scratch);
  21289. var v2 = Cartesian3.fromArray(subdividedPositions, i2 * 3, subdivisionV2Scratch);
  21290. var s0 = Cartesian3.multiplyByScalar(Cartesian3.normalize(v0, subdivisionS0Scratch), radius, subdivisionS0Scratch);
  21291. var s1 = Cartesian3.multiplyByScalar(Cartesian3.normalize(v1, subdivisionS1Scratch), radius, subdivisionS1Scratch);
  21292. var s2 = Cartesian3.multiplyByScalar(Cartesian3.normalize(v2, subdivisionS2Scratch), radius, subdivisionS2Scratch);
  21293. var g0 = Cartesian3.magnitudeSquared(Cartesian3.subtract(s0, s1, subdivisionMidScratch));
  21294. var g1 = Cartesian3.magnitudeSquared(Cartesian3.subtract(s1, s2, subdivisionMidScratch));
  21295. var g2 = Cartesian3.magnitudeSquared(Cartesian3.subtract(s2, s0, subdivisionMidScratch));
  21296. var max = Math.max(g0, g1, g2);
  21297. var edge;
  21298. var mid;
  21299. // if the max length squared of a triangle edge is greater than the chord length of squared
  21300. // of the granularity, subdivide the triangle
  21301. if (max > minDistanceSqrd) {
  21302. if (g0 === max) {
  21303. edge = Math.min(i0, i1) + ' ' + Math.max(i0, i1);
  21304. i = edges[edge];
  21305. if (!defined(i)) {
  21306. mid = Cartesian3.add(v0, v1, subdivisionMidScratch);
  21307. Cartesian3.multiplyByScalar(mid, 0.5, mid);
  21308. subdividedPositions.push(mid.x, mid.y, mid.z);
  21309. i = subdividedPositions.length / 3 - 1;
  21310. edges[edge] = i;
  21311. }
  21312. triangles.push(i0, i, i2);
  21313. triangles.push(i, i1, i2);
  21314. } else if (g1 === max) {
  21315. edge = Math.min(i1, i2) + ' ' + Math.max(i1, i2);
  21316. i = edges[edge];
  21317. if (!defined(i)) {
  21318. mid = Cartesian3.add(v1, v2, subdivisionMidScratch);
  21319. Cartesian3.multiplyByScalar(mid, 0.5, mid);
  21320. subdividedPositions.push(mid.x, mid.y, mid.z);
  21321. i = subdividedPositions.length / 3 - 1;
  21322. edges[edge] = i;
  21323. }
  21324. triangles.push(i1, i, i0);
  21325. triangles.push(i, i2, i0);
  21326. } else if (g2 === max) {
  21327. edge = Math.min(i2, i0) + ' ' + Math.max(i2, i0);
  21328. i = edges[edge];
  21329. if (!defined(i)) {
  21330. mid = Cartesian3.add(v2, v0, subdivisionMidScratch);
  21331. Cartesian3.multiplyByScalar(mid, 0.5, mid);
  21332. subdividedPositions.push(mid.x, mid.y, mid.z);
  21333. i = subdividedPositions.length / 3 - 1;
  21334. edges[edge] = i;
  21335. }
  21336. triangles.push(i2, i, i1);
  21337. triangles.push(i, i0, i1);
  21338. }
  21339. } else {
  21340. subdividedIndices.push(i0);
  21341. subdividedIndices.push(i1);
  21342. subdividedIndices.push(i2);
  21343. }
  21344. }
  21345. return new Geometry({
  21346. attributes : {
  21347. position : new GeometryAttribute({
  21348. componentDatatype : ComponentDatatype.DOUBLE,
  21349. componentsPerAttribute : 3,
  21350. values : subdividedPositions
  21351. })
  21352. },
  21353. indices : subdividedIndices,
  21354. primitiveType : PrimitiveType.TRIANGLES
  21355. });
  21356. };
  21357. /**
  21358. * Scales each position of a geometry's position attribute to a height, in place.
  21359. *
  21360. * @param {Number[]} positions The array of numbers representing the positions to be scaled
  21361. * @param {Number} [height=0.0] The desired height to add to the positions
  21362. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the positions lie.
  21363. * @param {Boolean} [scaleToSurface=true] <code>true</code> if the positions need to be scaled to the surface before the height is added.
  21364. * @returns {Number[]} The input array of positions, scaled to height
  21365. */
  21366. PolygonPipeline.scaleToGeodeticHeight = function(positions, height, ellipsoid, scaleToSurface) {
  21367. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  21368. var n = scaleToGeodeticHeightN;
  21369. var p = scaleToGeodeticHeightP;
  21370. height = defaultValue(height, 0.0);
  21371. scaleToSurface = defaultValue(scaleToSurface, true);
  21372. if (defined(positions)) {
  21373. var length = positions.length;
  21374. for ( var i = 0; i < length; i += 3) {
  21375. Cartesian3.fromArray(positions, i, p);
  21376. if (scaleToSurface) {
  21377. p = ellipsoid.scaleToGeodeticSurface(p, p);
  21378. }
  21379. if (height !== 0) {
  21380. n = ellipsoid.geodeticSurfaceNormal(p, n);
  21381. Cartesian3.multiplyByScalar(n, height, n);
  21382. Cartesian3.add(p, n, p);
  21383. }
  21384. positions[i] = p.x;
  21385. positions[i + 1] = p.y;
  21386. positions[i + 2] = p.z;
  21387. }
  21388. }
  21389. return positions;
  21390. };
  21391. return PolygonPipeline;
  21392. });
  21393. /*global define*/
  21394. define('Core/EllipsoidGeodesic',[
  21395. './Cartesian3',
  21396. './Cartographic',
  21397. './defaultValue',
  21398. './defined',
  21399. './defineProperties',
  21400. './DeveloperError',
  21401. './Ellipsoid',
  21402. './Math'
  21403. ], function(
  21404. Cartesian3,
  21405. Cartographic,
  21406. defaultValue,
  21407. defined,
  21408. defineProperties,
  21409. DeveloperError,
  21410. Ellipsoid,
  21411. CesiumMath) {
  21412. 'use strict';
  21413. function setConstants(ellipsoidGeodesic) {
  21414. var uSquared = ellipsoidGeodesic._uSquared;
  21415. var a = ellipsoidGeodesic._ellipsoid.maximumRadius;
  21416. var b = ellipsoidGeodesic._ellipsoid.minimumRadius;
  21417. var f = (a - b) / a;
  21418. var cosineHeading = Math.cos(ellipsoidGeodesic._startHeading);
  21419. var sineHeading = Math.sin(ellipsoidGeodesic._startHeading);
  21420. var tanU = (1 - f) * Math.tan(ellipsoidGeodesic._start.latitude);
  21421. var cosineU = 1.0 / Math.sqrt(1.0 + tanU * tanU);
  21422. var sineU = cosineU * tanU;
  21423. var sigma = Math.atan2(tanU, cosineHeading);
  21424. var sineAlpha = cosineU * sineHeading;
  21425. var sineSquaredAlpha = sineAlpha * sineAlpha;
  21426. var cosineSquaredAlpha = 1.0 - sineSquaredAlpha;
  21427. var cosineAlpha = Math.sqrt(cosineSquaredAlpha);
  21428. var u2Over4 = uSquared / 4.0;
  21429. var u4Over16 = u2Over4 * u2Over4;
  21430. var u6Over64 = u4Over16 * u2Over4;
  21431. var u8Over256 = u4Over16 * u4Over16;
  21432. var a0 = (1.0 + u2Over4 - 3.0 * u4Over16 / 4.0 + 5.0 * u6Over64 / 4.0 - 175.0 * u8Over256 / 64.0);
  21433. var a1 = (1.0 - u2Over4 + 15.0 * u4Over16 / 8.0 - 35.0 * u6Over64 / 8.0);
  21434. var a2 = (1.0 - 3.0 * u2Over4 + 35.0 * u4Over16 / 4.0);
  21435. var a3 = (1.0 - 5.0 * u2Over4);
  21436. var distanceRatio = a0 * sigma - a1 * Math.sin(2.0 * sigma) * u2Over4 / 2.0 - a2 * Math.sin(4.0 * sigma) * u4Over16 / 16.0 -
  21437. a3 * Math.sin(6.0 * sigma) * u6Over64 / 48.0 - Math.sin(8.0 * sigma) * 5.0 * u8Over256 / 512;
  21438. var constants = ellipsoidGeodesic._constants;
  21439. constants.a = a;
  21440. constants.b = b;
  21441. constants.f = f;
  21442. constants.cosineHeading = cosineHeading;
  21443. constants.sineHeading = sineHeading;
  21444. constants.tanU = tanU;
  21445. constants.cosineU = cosineU;
  21446. constants.sineU = sineU;
  21447. constants.sigma = sigma;
  21448. constants.sineAlpha = sineAlpha;
  21449. constants.sineSquaredAlpha = sineSquaredAlpha;
  21450. constants.cosineSquaredAlpha = cosineSquaredAlpha;
  21451. constants.cosineAlpha = cosineAlpha;
  21452. constants.u2Over4 = u2Over4;
  21453. constants.u4Over16 = u4Over16;
  21454. constants.u6Over64 = u6Over64;
  21455. constants.u8Over256 = u8Over256;
  21456. constants.a0 = a0;
  21457. constants.a1 = a1;
  21458. constants.a2 = a2;
  21459. constants.a3 = a3;
  21460. constants.distanceRatio = distanceRatio;
  21461. }
  21462. function computeC(f, cosineSquaredAlpha) {
  21463. return f * cosineSquaredAlpha * (4.0 + f * (4.0 - 3.0 * cosineSquaredAlpha)) / 16.0;
  21464. }
  21465. function computeDeltaLambda(f, sineAlpha, cosineSquaredAlpha, sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint) {
  21466. var C = computeC(f, cosineSquaredAlpha);
  21467. return (1.0 - C) * f * sineAlpha * (sigma + C * sineSigma * (cosineTwiceSigmaMidpoint +
  21468. C * cosineSigma * (2.0 * cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint - 1.0)));
  21469. }
  21470. function vincentyInverseFormula(ellipsoidGeodesic, major, minor, firstLongitude, firstLatitude, secondLongitude, secondLatitude) {
  21471. var eff = (major - minor) / major;
  21472. var l = secondLongitude - firstLongitude;
  21473. var u1 = Math.atan((1 - eff) * Math.tan(firstLatitude));
  21474. var u2 = Math.atan((1 - eff) * Math.tan(secondLatitude));
  21475. var cosineU1 = Math.cos(u1);
  21476. var sineU1 = Math.sin(u1);
  21477. var cosineU2 = Math.cos(u2);
  21478. var sineU2 = Math.sin(u2);
  21479. var cc = cosineU1 * cosineU2;
  21480. var cs = cosineU1 * sineU2;
  21481. var ss = sineU1 * sineU2;
  21482. var sc = sineU1 * cosineU2;
  21483. var lambda = l;
  21484. var lambdaDot = CesiumMath.TWO_PI;
  21485. var cosineLambda = Math.cos(lambda);
  21486. var sineLambda = Math.sin(lambda);
  21487. var sigma;
  21488. var cosineSigma;
  21489. var sineSigma;
  21490. var cosineSquaredAlpha;
  21491. var cosineTwiceSigmaMidpoint;
  21492. do {
  21493. cosineLambda = Math.cos(lambda);
  21494. sineLambda = Math.sin(lambda);
  21495. var temp = cs - sc * cosineLambda;
  21496. sineSigma = Math.sqrt(cosineU2 * cosineU2 * sineLambda * sineLambda + temp * temp);
  21497. cosineSigma = ss + cc * cosineLambda;
  21498. sigma = Math.atan2(sineSigma, cosineSigma);
  21499. var sineAlpha;
  21500. if (sineSigma === 0.0) {
  21501. sineAlpha = 0.0;
  21502. cosineSquaredAlpha = 1.0;
  21503. } else {
  21504. sineAlpha = cc * sineLambda / sineSigma;
  21505. cosineSquaredAlpha = 1.0 - sineAlpha * sineAlpha;
  21506. }
  21507. lambdaDot = lambda;
  21508. cosineTwiceSigmaMidpoint = cosineSigma - 2.0 * ss / cosineSquaredAlpha;
  21509. if (isNaN(cosineTwiceSigmaMidpoint)) {
  21510. cosineTwiceSigmaMidpoint = 0.0;
  21511. }
  21512. lambda = l + computeDeltaLambda(eff, sineAlpha, cosineSquaredAlpha,
  21513. sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint);
  21514. } while (Math.abs(lambda - lambdaDot) > CesiumMath.EPSILON12);
  21515. var uSquared = cosineSquaredAlpha * (major * major - minor * minor) / (minor * minor);
  21516. var A = 1.0 + uSquared * (4096.0 + uSquared * (uSquared * (320.0 - 175.0 * uSquared) - 768.0)) / 16384.0;
  21517. var B = uSquared * (256.0 + uSquared * (uSquared * (74.0 - 47.0 * uSquared) - 128.0)) / 1024.0;
  21518. var cosineSquaredTwiceSigmaMidpoint = cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint;
  21519. var deltaSigma = B * sineSigma * (cosineTwiceSigmaMidpoint + B * (cosineSigma *
  21520. (2.0 * cosineSquaredTwiceSigmaMidpoint - 1.0) - B * cosineTwiceSigmaMidpoint *
  21521. (4.0 * sineSigma * sineSigma - 3.0) * (4.0 * cosineSquaredTwiceSigmaMidpoint - 3.0) / 6.0) / 4.0);
  21522. var distance = minor * A * (sigma - deltaSigma);
  21523. var startHeading = Math.atan2(cosineU2 * sineLambda, cs - sc * cosineLambda);
  21524. var endHeading = Math.atan2(cosineU1 * sineLambda, cs * cosineLambda - sc);
  21525. ellipsoidGeodesic._distance = distance;
  21526. ellipsoidGeodesic._startHeading = startHeading;
  21527. ellipsoidGeodesic._endHeading = endHeading;
  21528. ellipsoidGeodesic._uSquared = uSquared;
  21529. }
  21530. function computeProperties(ellipsoidGeodesic, start, end, ellipsoid) {
  21531. var firstCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(start, scratchCart2), scratchCart1);
  21532. var lastCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(end, scratchCart2), scratchCart2);
  21533. if (Math.abs(Math.abs(Cartesian3.angleBetween(firstCartesian, lastCartesian)) - Math.PI) < 0.0125) {
  21534. throw new DeveloperError('geodesic position is not unique');
  21535. }
  21536. vincentyInverseFormula(ellipsoidGeodesic, ellipsoid.maximumRadius, ellipsoid.minimumRadius,
  21537. start.longitude, start.latitude, end.longitude, end.latitude);
  21538. ellipsoidGeodesic._start = Cartographic.clone(start, ellipsoidGeodesic._start);
  21539. ellipsoidGeodesic._end = Cartographic.clone(end, ellipsoidGeodesic._end);
  21540. ellipsoidGeodesic._start.height = 0;
  21541. ellipsoidGeodesic._end.height = 0;
  21542. setConstants(ellipsoidGeodesic);
  21543. }
  21544. var scratchCart1 = new Cartesian3();
  21545. var scratchCart2 = new Cartesian3();
  21546. /**
  21547. * Initializes a geodesic on the ellipsoid connecting the two provided planetodetic points.
  21548. *
  21549. * @alias EllipsoidGeodesic
  21550. * @constructor
  21551. *
  21552. * @param {Cartographic} [start] The initial planetodetic point on the path.
  21553. * @param {Cartographic} [end] The final planetodetic point on the path.
  21554. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the geodesic lies.
  21555. */
  21556. function EllipsoidGeodesic(start, end, ellipsoid) {
  21557. var e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  21558. this._ellipsoid = e;
  21559. this._start = new Cartographic();
  21560. this._end = new Cartographic();
  21561. this._constants = {};
  21562. this._startHeading = undefined;
  21563. this._endHeading = undefined;
  21564. this._distance = undefined;
  21565. this._uSquared = undefined;
  21566. if (defined(start) && defined(end)) {
  21567. computeProperties(this, start, end, e);
  21568. }
  21569. }
  21570. defineProperties(EllipsoidGeodesic.prototype, {
  21571. /**
  21572. * Gets the ellipsoid.
  21573. * @memberof EllipsoidGeodesic.prototype
  21574. * @type {Ellipsoid}
  21575. * @readonly
  21576. */
  21577. ellipsoid : {
  21578. get : function() {
  21579. return this._ellipsoid;
  21580. }
  21581. },
  21582. /**
  21583. * Gets the surface distance between the start and end point
  21584. * @memberof EllipsoidGeodesic.prototype
  21585. * @type {Number}
  21586. * @readonly
  21587. */
  21588. surfaceDistance : {
  21589. get : function() {
  21590. if (!defined(this._distance)) {
  21591. throw new DeveloperError('set end positions before getting surfaceDistance');
  21592. }
  21593. return this._distance;
  21594. }
  21595. },
  21596. /**
  21597. * Gets the initial planetodetic point on the path.
  21598. * @memberof EllipsoidGeodesic.prototype
  21599. * @type {Cartographic}
  21600. * @readonly
  21601. */
  21602. start : {
  21603. get : function() {
  21604. return this._start;
  21605. }
  21606. },
  21607. /**
  21608. * Gets the final planetodetic point on the path.
  21609. * @memberof EllipsoidGeodesic.prototype
  21610. * @type {Cartographic}
  21611. * @readonly
  21612. */
  21613. end : {
  21614. get : function() {
  21615. return this._end;
  21616. }
  21617. },
  21618. /**
  21619. * Gets the heading at the initial point.
  21620. * @memberof EllipsoidGeodesic.prototype
  21621. * @type {Number}
  21622. * @readonly
  21623. */
  21624. startHeading : {
  21625. get : function() {
  21626. if (!defined(this._distance)) {
  21627. throw new DeveloperError('set end positions before getting startHeading');
  21628. }
  21629. return this._startHeading;
  21630. }
  21631. },
  21632. /**
  21633. * Gets the heading at the final point.
  21634. * @memberof EllipsoidGeodesic.prototype
  21635. * @type {Number}
  21636. * @readonly
  21637. */
  21638. endHeading : {
  21639. get : function() {
  21640. if (!defined(this._distance)) {
  21641. throw new DeveloperError('set end positions before getting endHeading');
  21642. }
  21643. return this._endHeading;
  21644. }
  21645. }
  21646. });
  21647. /**
  21648. * Sets the start and end points of the geodesic
  21649. *
  21650. * @param {Cartographic} start The initial planetodetic point on the path.
  21651. * @param {Cartographic} end The final planetodetic point on the path.
  21652. */
  21653. EllipsoidGeodesic.prototype.setEndPoints = function(start, end) {
  21654. if (!defined(start)) {
  21655. throw new DeveloperError('start cartographic position is required');
  21656. }
  21657. if (!defined(end)) {
  21658. throw new DeveloperError('end cartgraphic position is required');
  21659. }
  21660. computeProperties(this, start, end, this._ellipsoid);
  21661. };
  21662. /**
  21663. * Provides the location of a point at the indicated portion along the geodesic.
  21664. *
  21665. * @param {Number} fraction The portion of the distance between the initial and final points.
  21666. * @returns {Cartographic} The location of the point along the geodesic.
  21667. */
  21668. EllipsoidGeodesic.prototype.interpolateUsingFraction = function(fraction, result) {
  21669. return this.interpolateUsingSurfaceDistance(this._distance * fraction, result);
  21670. };
  21671. /**
  21672. * Provides the location of a point at the indicated distance along the geodesic.
  21673. *
  21674. * @param {Number} distance The distance from the inital point to the point of interest along the geodesic
  21675. * @returns {Cartographic} The location of the point along the geodesic.
  21676. *
  21677. * @exception {DeveloperError} start and end must be set before calling function interpolateUsingSurfaceDistance
  21678. */
  21679. EllipsoidGeodesic.prototype.interpolateUsingSurfaceDistance = function(distance, result) {
  21680. if (!defined(this._distance)) {
  21681. throw new DeveloperError('start and end must be set before calling function interpolateUsingSurfaceDistance');
  21682. }
  21683. var constants = this._constants;
  21684. var s = constants.distanceRatio + distance / constants.b;
  21685. var cosine2S = Math.cos(2.0 * s);
  21686. var cosine4S = Math.cos(4.0 * s);
  21687. var cosine6S = Math.cos(6.0 * s);
  21688. var sine2S = Math.sin(2.0 * s);
  21689. var sine4S = Math.sin(4.0 * s);
  21690. var sine6S = Math.sin(6.0 * s);
  21691. var sine8S = Math.sin(8.0 * s);
  21692. var s2 = s * s;
  21693. var s3 = s * s2;
  21694. var u8Over256 = constants.u8Over256;
  21695. var u2Over4 = constants.u2Over4;
  21696. var u6Over64 = constants.u6Over64;
  21697. var u4Over16 = constants.u4Over16;
  21698. var sigma = 2.0 * s3 * u8Over256 * cosine2S / 3.0 +
  21699. s * (1.0 - u2Over4 + 7.0 * u4Over16 / 4.0 - 15.0 * u6Over64 / 4.0 + 579.0 * u8Over256 / 64.0 -
  21700. (u4Over16 - 15.0 * u6Over64 / 4.0 + 187.0 * u8Over256 / 16.0) * cosine2S -
  21701. (5.0 * u6Over64 / 4.0 - 115.0 * u8Over256 / 16.0) * cosine4S -
  21702. 29.0 * u8Over256 * cosine6S / 16.0) +
  21703. (u2Over4 / 2.0 - u4Over16 + 71.0 * u6Over64 / 32.0 - 85.0 * u8Over256 / 16.0) * sine2S +
  21704. (5.0 * u4Over16 / 16.0 - 5.0 * u6Over64 / 4.0 + 383.0 * u8Over256 / 96.0) * sine4S -
  21705. s2 * ((u6Over64 - 11.0 * u8Over256 / 2.0) * sine2S + 5.0 * u8Over256 * sine4S / 2.0) +
  21706. (29.0 * u6Over64 / 96.0 - 29.0 * u8Over256 / 16.0) * sine6S +
  21707. 539.0 * u8Over256 * sine8S / 1536.0;
  21708. var theta = Math.asin(Math.sin(sigma) * constants.cosineAlpha);
  21709. var latitude = Math.atan(constants.a / constants.b * Math.tan(theta));
  21710. // Redefine in terms of relative argument of latitude.
  21711. sigma = sigma - constants.sigma;
  21712. var cosineTwiceSigmaMidpoint = Math.cos(2.0 * constants.sigma + sigma);
  21713. var sineSigma = Math.sin(sigma);
  21714. var cosineSigma = Math.cos(sigma);
  21715. var cc = constants.cosineU * cosineSigma;
  21716. var ss = constants.sineU * sineSigma;
  21717. var lambda = Math.atan2(sineSigma * constants.sineHeading, cc - ss * constants.cosineHeading);
  21718. var l = lambda - computeDeltaLambda(constants.f, constants.sineAlpha, constants.cosineSquaredAlpha,
  21719. sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint);
  21720. if (defined(result)) {
  21721. result.longitude = this._start.longitude + l;
  21722. result.latitude = latitude;
  21723. result.height = 0.0;
  21724. return result;
  21725. }
  21726. return new Cartographic(this._start.longitude + l, latitude, 0.0);
  21727. };
  21728. return EllipsoidGeodesic;
  21729. });
  21730. /*global define*/
  21731. define('Core/PolylinePipeline',[
  21732. './Cartesian3',
  21733. './Cartographic',
  21734. './defaultValue',
  21735. './defined',
  21736. './DeveloperError',
  21737. './Ellipsoid',
  21738. './EllipsoidGeodesic',
  21739. './IntersectionTests',
  21740. './isArray',
  21741. './Math',
  21742. './Matrix4',
  21743. './Plane'
  21744. ], function(
  21745. Cartesian3,
  21746. Cartographic,
  21747. defaultValue,
  21748. defined,
  21749. DeveloperError,
  21750. Ellipsoid,
  21751. EllipsoidGeodesic,
  21752. IntersectionTests,
  21753. isArray,
  21754. CesiumMath,
  21755. Matrix4,
  21756. Plane) {
  21757. 'use strict';
  21758. /**
  21759. * @private
  21760. */
  21761. var PolylinePipeline = {};
  21762. PolylinePipeline.numberOfPoints = function(p0, p1, minDistance) {
  21763. var distance = Cartesian3.distance(p0, p1);
  21764. return Math.ceil(distance / minDistance);
  21765. };
  21766. var cartoScratch = new Cartographic();
  21767. PolylinePipeline.extractHeights = function(positions, ellipsoid) {
  21768. var length = positions.length;
  21769. var heights = new Array(length);
  21770. for (var i = 0; i < length; i++) {
  21771. var p = positions[i];
  21772. heights[i] = ellipsoid.cartesianToCartographic(p, cartoScratch).height;
  21773. }
  21774. return heights;
  21775. };
  21776. var wrapLongitudeInversMatrix = new Matrix4();
  21777. var wrapLongitudeOrigin = new Cartesian3();
  21778. var wrapLongitudeXZNormal = new Cartesian3();
  21779. var wrapLongitudeXZPlane = new Plane(Cartesian3.ZERO, 0.0);
  21780. var wrapLongitudeYZNormal = new Cartesian3();
  21781. var wrapLongitudeYZPlane = new Plane(Cartesian3.ZERO, 0.0);
  21782. var wrapLongitudeIntersection = new Cartesian3();
  21783. var wrapLongitudeOffset = new Cartesian3();
  21784. var subdivideHeightsScratchArray = [];
  21785. function subdivideHeights(numPoints, h0, h1) {
  21786. var heights = subdivideHeightsScratchArray;
  21787. heights.length = numPoints;
  21788. var i;
  21789. if (h0 === h1) {
  21790. for (i = 0; i < numPoints; i++) {
  21791. heights[i] = h0;
  21792. }
  21793. return heights;
  21794. }
  21795. var dHeight = h1 - h0;
  21796. var heightPerVertex = dHeight / numPoints;
  21797. for (i = 0; i < numPoints; i++) {
  21798. var h = h0 + i*heightPerVertex;
  21799. heights[i] = h;
  21800. }
  21801. return heights;
  21802. }
  21803. var carto1 = new Cartographic();
  21804. var carto2 = new Cartographic();
  21805. var cartesian = new Cartesian3();
  21806. var scaleFirst = new Cartesian3();
  21807. var scaleLast = new Cartesian3();
  21808. var ellipsoidGeodesic = new EllipsoidGeodesic();
  21809. //Returns subdivided line scaled to ellipsoid surface starting at p1 and ending at p2.
  21810. //Result includes p1, but not include p2. This function is called for a sequence of line segments,
  21811. //and this prevents duplication of end point.
  21812. function generateCartesianArc(p0, p1, minDistance, ellipsoid, h0, h1, array, offset) {
  21813. var first = ellipsoid.scaleToGeodeticSurface(p0, scaleFirst);
  21814. var last = ellipsoid.scaleToGeodeticSurface(p1, scaleLast);
  21815. var numPoints = PolylinePipeline.numberOfPoints(p0, p1, minDistance);
  21816. var start = ellipsoid.cartesianToCartographic(first, carto1);
  21817. var end = ellipsoid.cartesianToCartographic(last, carto2);
  21818. var heights = subdivideHeights(numPoints, h0, h1);
  21819. ellipsoidGeodesic.setEndPoints(start, end);
  21820. var surfaceDistanceBetweenPoints = ellipsoidGeodesic.surfaceDistance / numPoints;
  21821. var index = offset;
  21822. start.height = h0;
  21823. var cart = ellipsoid.cartographicToCartesian(start, cartesian);
  21824. Cartesian3.pack(cart, array, index);
  21825. index += 3;
  21826. for (var i = 1; i < numPoints; i++) {
  21827. var carto = ellipsoidGeodesic.interpolateUsingSurfaceDistance(i * surfaceDistanceBetweenPoints, carto2);
  21828. carto.height = heights[i];
  21829. cart = ellipsoid.cartographicToCartesian(carto, cartesian);
  21830. Cartesian3.pack(cart, array, index);
  21831. index += 3;
  21832. }
  21833. return index;
  21834. }
  21835. /**
  21836. * Breaks a {@link Polyline} into segments such that it does not cross the &plusmn;180 degree meridian of an ellipsoid.
  21837. *
  21838. * @param {Cartesian3[]} positions The polyline's Cartesian positions.
  21839. * @param {Matrix4} [modelMatrix=Matrix4.IDENTITY] The polyline's model matrix. Assumed to be an affine
  21840. * transformation matrix, where the upper left 3x3 elements are a rotation matrix, and
  21841. * the upper three elements in the fourth column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  21842. * The matrix is not verified to be in the proper form.
  21843. * @returns {Object} An object with a <code>positions</code> property that is an array of positions and a
  21844. * <code>segments</code> property.
  21845. *
  21846. *
  21847. * @example
  21848. * var polylines = new Cesium.PolylineCollection();
  21849. * var polyline = polylines.add(...);
  21850. * var positions = polyline.positions;
  21851. * var modelMatrix = polylines.modelMatrix;
  21852. * var segments = Cesium.PolylinePipeline.wrapLongitude(positions, modelMatrix);
  21853. *
  21854. * @see PolygonPipeline.wrapLongitude
  21855. * @see Polyline
  21856. * @see PolylineCollection
  21857. */
  21858. PolylinePipeline.wrapLongitude = function(positions, modelMatrix) {
  21859. var cartesians = [];
  21860. var segments = [];
  21861. if (defined(positions) && positions.length > 0) {
  21862. modelMatrix = defaultValue(modelMatrix, Matrix4.IDENTITY);
  21863. var inverseModelMatrix = Matrix4.inverseTransformation(modelMatrix, wrapLongitudeInversMatrix);
  21864. var origin = Matrix4.multiplyByPoint(inverseModelMatrix, Cartesian3.ZERO, wrapLongitudeOrigin);
  21865. var xzNormal = Matrix4.multiplyByPointAsVector(inverseModelMatrix, Cartesian3.UNIT_Y, wrapLongitudeXZNormal);
  21866. var xzPlane = Plane.fromPointNormal(origin, xzNormal, wrapLongitudeXZPlane);
  21867. var yzNormal = Matrix4.multiplyByPointAsVector(inverseModelMatrix, Cartesian3.UNIT_X, wrapLongitudeYZNormal);
  21868. var yzPlane = Plane.fromPointNormal(origin, yzNormal, wrapLongitudeYZPlane);
  21869. var count = 1;
  21870. cartesians.push(Cartesian3.clone(positions[0]));
  21871. var prev = cartesians[0];
  21872. var length = positions.length;
  21873. for (var i = 1; i < length; ++i) {
  21874. var cur = positions[i];
  21875. // intersects the IDL if either endpoint is on the negative side of the yz-plane
  21876. if (Plane.getPointDistance(yzPlane, prev) < 0.0 || Plane.getPointDistance(yzPlane, cur) < 0.0) {
  21877. // and intersects the xz-plane
  21878. var intersection = IntersectionTests.lineSegmentPlane(prev, cur, xzPlane, wrapLongitudeIntersection);
  21879. if (defined(intersection)) {
  21880. // move point on the xz-plane slightly away from the plane
  21881. var offset = Cartesian3.multiplyByScalar(xzNormal, 5.0e-9, wrapLongitudeOffset);
  21882. if (Plane.getPointDistance(xzPlane, prev) < 0.0) {
  21883. Cartesian3.negate(offset, offset);
  21884. }
  21885. cartesians.push(Cartesian3.add(intersection, offset, new Cartesian3()));
  21886. segments.push(count + 1);
  21887. Cartesian3.negate(offset, offset);
  21888. cartesians.push(Cartesian3.add(intersection, offset, new Cartesian3()));
  21889. count = 1;
  21890. }
  21891. }
  21892. cartesians.push(Cartesian3.clone(positions[i]));
  21893. count++;
  21894. prev = cur;
  21895. }
  21896. segments.push(count);
  21897. }
  21898. return {
  21899. positions : cartesians,
  21900. lengths : segments
  21901. };
  21902. };
  21903. /**
  21904. * Subdivides polyline and raises all points to the specified height. Returns an array of numbers to represent the positions.
  21905. * @param {Cartesian3[]} positions The array of type {Cartesian3} representing positions.
  21906. * @param {Number|Number[]} [height=0.0] A number or array of numbers representing the heights of each position.
  21907. * @param {Number} [granularity = CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  21908. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the positions lie.
  21909. * @returns {Number[]} A new array of positions of type {Number} that have been subdivided and raised to the surface of the ellipsoid.
  21910. *
  21911. * @example
  21912. * var positions = Cesium.Cartesian3.fromDegreesArray([
  21913. * -105.0, 40.0,
  21914. * -100.0, 38.0,
  21915. * -105.0, 35.0,
  21916. * -100.0, 32.0
  21917. * ]);
  21918. * var surfacePositions = Cesium.PolylinePipeline.generateArc({
  21919. * positons: positions
  21920. * });
  21921. */
  21922. PolylinePipeline.generateArc = function(options) {
  21923. if (!defined(options)) {
  21924. options = {};
  21925. }
  21926. var positions = options.positions;
  21927. if (!defined(positions)) {
  21928. throw new DeveloperError('options.positions is required.');
  21929. }
  21930. var length = positions.length;
  21931. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  21932. var height = defaultValue(options.height, 0);
  21933. var hasHeightArray = isArray(height);
  21934. if (length < 1) {
  21935. return [];
  21936. } else if (length === 1) {
  21937. var p = ellipsoid.scaleToGeodeticSurface(positions[0], scaleFirst);
  21938. height = hasHeightArray ? height[0] : height;
  21939. if (height !== 0) {
  21940. var n = ellipsoid.geodeticSurfaceNormal(p, cartesian);
  21941. Cartesian3.multiplyByScalar(n, height, n);
  21942. Cartesian3.add(p, n, p);
  21943. }
  21944. return [p.x, p.y, p.z];
  21945. }
  21946. var minDistance = options.minDistance;
  21947. if (!defined(minDistance)) {
  21948. var granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  21949. minDistance = CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  21950. }
  21951. var numPoints = 0;
  21952. var i;
  21953. for (i = 0; i < length -1; i++) {
  21954. numPoints += PolylinePipeline.numberOfPoints(positions[i], positions[i+1], minDistance);
  21955. }
  21956. var arrayLength = (numPoints + 1) * 3;
  21957. var newPositions = new Array(arrayLength);
  21958. var offset = 0;
  21959. for (i = 0; i < length - 1; i++) {
  21960. var p0 = positions[i];
  21961. var p1 = positions[i + 1];
  21962. var h0 = hasHeightArray ? height[i] : height;
  21963. var h1 = hasHeightArray ? height[i + 1] : height;
  21964. offset = generateCartesianArc(p0, p1, minDistance, ellipsoid, h0, h1, newPositions, offset);
  21965. }
  21966. subdivideHeightsScratchArray.length = 0;
  21967. var lastPoint = positions[length - 1];
  21968. var carto = ellipsoid.cartesianToCartographic(lastPoint, carto1);
  21969. carto.height = hasHeightArray ? height[length - 1] : height;
  21970. var cart = ellipsoid.cartographicToCartesian(carto, cartesian);
  21971. Cartesian3.pack(cart, newPositions, arrayLength - 3);
  21972. return newPositions;
  21973. };
  21974. /**
  21975. * Subdivides polyline and raises all points to the specified height. Returns an array of new {Cartesian3} positions.
  21976. * @param {Cartesian3[]} positions The array of type {Cartesian3} representing positions.
  21977. * @param {Number|Number[]} [height=0.0] A number or array of numbers representing the heights of each position.
  21978. * @param {Number} [granularity = CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  21979. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the positions lie.
  21980. * @returns {Cartesian3[]} A new array of cartesian3 positions that have been subdivided and raised to the surface of the ellipsoid.
  21981. *
  21982. * @example
  21983. * var positions = Cesium.Cartesian3.fromDegreesArray([
  21984. * -105.0, 40.0,
  21985. * -100.0, 38.0,
  21986. * -105.0, 35.0,
  21987. * -100.0, 32.0
  21988. * ]);
  21989. * var surfacePositions = Cesium.PolylinePipeline.generateCartesianArc({
  21990. * positons: positions
  21991. * });
  21992. */
  21993. PolylinePipeline.generateCartesianArc = function(options) {
  21994. var numberArray = PolylinePipeline.generateArc(options);
  21995. var size = numberArray.length/3;
  21996. var newPositions = new Array(size);
  21997. for (var i = 0; i < size; i++) {
  21998. newPositions[i] = Cartesian3.unpack(numberArray, i*3);
  21999. }
  22000. return newPositions;
  22001. };
  22002. return PolylinePipeline;
  22003. });
  22004. /*global define*/
  22005. define('Core/WallGeometryLibrary',[
  22006. './Cartographic',
  22007. './defined',
  22008. './EllipsoidTangentPlane',
  22009. './Math',
  22010. './PolygonPipeline',
  22011. './PolylinePipeline',
  22012. './WindingOrder'
  22013. ], function(
  22014. Cartographic,
  22015. defined,
  22016. EllipsoidTangentPlane,
  22017. CesiumMath,
  22018. PolygonPipeline,
  22019. PolylinePipeline,
  22020. WindingOrder) {
  22021. 'use strict';
  22022. /**
  22023. * private
  22024. */
  22025. var WallGeometryLibrary = {};
  22026. function latLonEquals(c0, c1) {
  22027. return ((CesiumMath.equalsEpsilon(c0.latitude, c1.latitude, CesiumMath.EPSILON14)) && (CesiumMath.equalsEpsilon(c0.longitude, c1.longitude, CesiumMath.EPSILON14)));
  22028. }
  22029. var scratchCartographic1 = new Cartographic();
  22030. var scratchCartographic2 = new Cartographic();
  22031. function removeDuplicates(ellipsoid, positions, topHeights, bottomHeights) {
  22032. var length = positions.length;
  22033. if (length < 2) {
  22034. return;
  22035. }
  22036. var hasBottomHeights = defined(bottomHeights);
  22037. var hasTopHeights = defined(topHeights);
  22038. var hasAllZeroHeights = true;
  22039. var cleanedPositions = new Array(length);
  22040. var cleanedTopHeights = new Array(length);
  22041. var cleanedBottomHeights = new Array(length);
  22042. var v0 = positions[0];
  22043. cleanedPositions[0] = v0;
  22044. var c0 = ellipsoid.cartesianToCartographic(v0, scratchCartographic1);
  22045. if (hasTopHeights) {
  22046. c0.height = topHeights[0];
  22047. }
  22048. hasAllZeroHeights = hasAllZeroHeights && c0.height <= 0;
  22049. cleanedTopHeights[0] = c0.height;
  22050. if (hasBottomHeights) {
  22051. cleanedBottomHeights[0] = bottomHeights[0];
  22052. } else {
  22053. cleanedBottomHeights[0] = 0.0;
  22054. }
  22055. var index = 1;
  22056. for (var i = 1; i < length; ++i) {
  22057. var v1 = positions[i];
  22058. var c1 = ellipsoid.cartesianToCartographic(v1, scratchCartographic2);
  22059. if (hasTopHeights) {
  22060. c1.height = topHeights[i];
  22061. }
  22062. hasAllZeroHeights = hasAllZeroHeights && c1.height <= 0;
  22063. if (!latLonEquals(c0, c1)) {
  22064. cleanedPositions[index] = v1; // Shallow copy!
  22065. cleanedTopHeights[index] = c1.height;
  22066. if (hasBottomHeights) {
  22067. cleanedBottomHeights[index] = bottomHeights[i];
  22068. } else {
  22069. cleanedBottomHeights[index] = 0.0;
  22070. }
  22071. Cartographic.clone(c1, c0);
  22072. ++index;
  22073. } else if (c0.height < c1.height) {
  22074. cleanedTopHeights[index - 1] = c1.height;
  22075. }
  22076. }
  22077. if (hasAllZeroHeights || index < 2) {
  22078. return;
  22079. }
  22080. cleanedPositions.length = index;
  22081. cleanedTopHeights.length = index;
  22082. cleanedBottomHeights.length = index;
  22083. return {
  22084. positions: cleanedPositions,
  22085. topHeights: cleanedTopHeights,
  22086. bottomHeights: cleanedBottomHeights
  22087. };
  22088. }
  22089. var positionsArrayScratch = new Array(2);
  22090. var heightsArrayScratch = new Array(2);
  22091. var generateArcOptionsScratch = {
  22092. positions : undefined,
  22093. height : undefined,
  22094. granularity : undefined,
  22095. ellipsoid : undefined
  22096. };
  22097. /**
  22098. * @private
  22099. */
  22100. WallGeometryLibrary.computePositions = function(ellipsoid, wallPositions, maximumHeights, minimumHeights, granularity, duplicateCorners) {
  22101. var o = removeDuplicates(ellipsoid, wallPositions, maximumHeights, minimumHeights);
  22102. if (!defined(o)) {
  22103. return;
  22104. }
  22105. wallPositions = o.positions;
  22106. maximumHeights = o.topHeights;
  22107. minimumHeights = o.bottomHeights;
  22108. if (wallPositions.length >= 3) {
  22109. // Order positions counter-clockwise
  22110. var tangentPlane = EllipsoidTangentPlane.fromPoints(wallPositions, ellipsoid);
  22111. var positions2D = tangentPlane.projectPointsOntoPlane(wallPositions);
  22112. if (PolygonPipeline.computeWindingOrder2D(positions2D) === WindingOrder.CLOCKWISE) {
  22113. wallPositions.reverse();
  22114. maximumHeights.reverse();
  22115. minimumHeights.reverse();
  22116. }
  22117. }
  22118. var length = wallPositions.length;
  22119. var numCorners = length - 2;
  22120. var topPositions;
  22121. var bottomPositions;
  22122. var minDistance = CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  22123. var generateArcOptions = generateArcOptionsScratch;
  22124. generateArcOptions.minDistance = minDistance;
  22125. generateArcOptions.ellipsoid = ellipsoid;
  22126. if (duplicateCorners) {
  22127. var count = 0;
  22128. var i;
  22129. for (i = 0; i < length - 1; i++) {
  22130. count += PolylinePipeline.numberOfPoints(wallPositions[i], wallPositions[i+1], minDistance) + 1;
  22131. }
  22132. topPositions = new Float64Array(count * 3);
  22133. bottomPositions = new Float64Array(count * 3);
  22134. var generateArcPositions = positionsArrayScratch;
  22135. var generateArcHeights = heightsArrayScratch;
  22136. generateArcOptions.positions = generateArcPositions;
  22137. generateArcOptions.height = generateArcHeights;
  22138. var offset = 0;
  22139. for (i = 0; i < length - 1; i++) {
  22140. generateArcPositions[0] = wallPositions[i];
  22141. generateArcPositions[1] = wallPositions[i + 1];
  22142. generateArcHeights[0] = maximumHeights[i];
  22143. generateArcHeights[1] = maximumHeights[i + 1];
  22144. var pos = PolylinePipeline.generateArc(generateArcOptions);
  22145. topPositions.set(pos, offset);
  22146. generateArcHeights[0] = minimumHeights[i];
  22147. generateArcHeights[1] = minimumHeights[i + 1];
  22148. bottomPositions.set(PolylinePipeline.generateArc(generateArcOptions), offset);
  22149. offset += pos.length;
  22150. }
  22151. } else {
  22152. generateArcOptions.positions = wallPositions;
  22153. generateArcOptions.height = maximumHeights;
  22154. topPositions = new Float64Array(PolylinePipeline.generateArc(generateArcOptions));
  22155. generateArcOptions.height = minimumHeights;
  22156. bottomPositions = new Float64Array(PolylinePipeline.generateArc(generateArcOptions));
  22157. }
  22158. return {
  22159. bottomPositions: bottomPositions,
  22160. topPositions: topPositions,
  22161. numCorners: numCorners
  22162. };
  22163. };
  22164. return WallGeometryLibrary;
  22165. });
  22166. /*global define*/
  22167. define('Core/WallOutlineGeometry',[
  22168. './BoundingSphere',
  22169. './Cartesian3',
  22170. './ComponentDatatype',
  22171. './defaultValue',
  22172. './defined',
  22173. './DeveloperError',
  22174. './Ellipsoid',
  22175. './Geometry',
  22176. './GeometryAttribute',
  22177. './GeometryAttributes',
  22178. './IndexDatatype',
  22179. './Math',
  22180. './PrimitiveType',
  22181. './WallGeometryLibrary'
  22182. ], function(
  22183. BoundingSphere,
  22184. Cartesian3,
  22185. ComponentDatatype,
  22186. defaultValue,
  22187. defined,
  22188. DeveloperError,
  22189. Ellipsoid,
  22190. Geometry,
  22191. GeometryAttribute,
  22192. GeometryAttributes,
  22193. IndexDatatype,
  22194. CesiumMath,
  22195. PrimitiveType,
  22196. WallGeometryLibrary) {
  22197. 'use strict';
  22198. var scratchCartesian3Position1 = new Cartesian3();
  22199. var scratchCartesian3Position2 = new Cartesian3();
  22200. /**
  22201. * A description of a wall outline. A wall is defined by a series of points,
  22202. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  22203. *
  22204. * @alias WallOutlineGeometry
  22205. * @constructor
  22206. *
  22207. * @param {Object} options Object with the following properties:
  22208. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  22209. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  22210. * @param {Number[]} [options.maximumHeights] An array parallel to <code>positions</code> that give the maximum height of the
  22211. * wall at <code>positions</code>. If undefined, the height of each position in used.
  22212. * @param {Number[]} [options.minimumHeights] An array parallel to <code>positions</code> that give the minimum height of the
  22213. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  22214. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  22215. *
  22216. * @exception {DeveloperError} positions length must be greater than or equal to 2.
  22217. * @exception {DeveloperError} positions and maximumHeights must have the same length.
  22218. * @exception {DeveloperError} positions and minimumHeights must have the same length.
  22219. *
  22220. * @see WallGeometry#createGeometry
  22221. * @see WallGeometry#fromConstantHeight
  22222. *
  22223. * @example
  22224. * // create a wall outline that spans from ground level to 10000 meters
  22225. * var wall = new Cesium.WallOutlineGeometry({
  22226. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  22227. * 19.0, 47.0, 10000.0,
  22228. * 19.0, 48.0, 10000.0,
  22229. * 20.0, 48.0, 10000.0,
  22230. * 20.0, 47.0, 10000.0,
  22231. * 19.0, 47.0, 10000.0
  22232. * ])
  22233. * });
  22234. * var geometry = Cesium.WallOutlineGeometry.createGeometry(wall);
  22235. */
  22236. function WallOutlineGeometry(options) {
  22237. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  22238. var wallPositions = options.positions;
  22239. var maximumHeights = options.maximumHeights;
  22240. var minimumHeights = options.minimumHeights;
  22241. if (!defined(wallPositions)) {
  22242. throw new DeveloperError('options.positions is required.');
  22243. }
  22244. if (defined(maximumHeights) && maximumHeights.length !== wallPositions.length) {
  22245. throw new DeveloperError('options.positions and options.maximumHeights must have the same length.');
  22246. }
  22247. if (defined(minimumHeights) && minimumHeights.length !== wallPositions.length) {
  22248. throw new DeveloperError('options.positions and options.minimumHeights must have the same length.');
  22249. }
  22250. var granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  22251. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  22252. this._positions = wallPositions;
  22253. this._minimumHeights = minimumHeights;
  22254. this._maximumHeights = maximumHeights;
  22255. this._granularity = granularity;
  22256. this._ellipsoid = Ellipsoid.clone(ellipsoid);
  22257. this._workerName = 'createWallOutlineGeometry';
  22258. var numComponents = 1 + wallPositions.length * Cartesian3.packedLength + 2;
  22259. if (defined(minimumHeights)) {
  22260. numComponents += minimumHeights.length;
  22261. }
  22262. if (defined(maximumHeights)) {
  22263. numComponents += maximumHeights.length;
  22264. }
  22265. /**
  22266. * The number of elements used to pack the object into an array.
  22267. * @type {Number}
  22268. */
  22269. this.packedLength = numComponents + Ellipsoid.packedLength + 1;
  22270. }
  22271. /**
  22272. * Stores the provided instance into the provided array.
  22273. *
  22274. * @param {WallOutlineGeometry} value The value to pack.
  22275. * @param {Number[]} array The array to pack into.
  22276. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  22277. *
  22278. * @returns {Number[]} The array that was packed into
  22279. */
  22280. WallOutlineGeometry.pack = function(value, array, startingIndex) {
  22281. if (!defined(value)) {
  22282. throw new DeveloperError('value is required');
  22283. }
  22284. if (!defined(array)) {
  22285. throw new DeveloperError('array is required');
  22286. }
  22287. startingIndex = defaultValue(startingIndex, 0);
  22288. var i;
  22289. var positions = value._positions;
  22290. var length = positions.length;
  22291. array[startingIndex++] = length;
  22292. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  22293. Cartesian3.pack(positions[i], array, startingIndex);
  22294. }
  22295. var minimumHeights = value._minimumHeights;
  22296. length = defined(minimumHeights) ? minimumHeights.length : 0;
  22297. array[startingIndex++] = length;
  22298. if (defined(minimumHeights)) {
  22299. for (i = 0; i < length; ++i) {
  22300. array[startingIndex++] = minimumHeights[i];
  22301. }
  22302. }
  22303. var maximumHeights = value._maximumHeights;
  22304. length = defined(maximumHeights) ? maximumHeights.length : 0;
  22305. array[startingIndex++] = length;
  22306. if (defined(maximumHeights)) {
  22307. for (i = 0; i < length; ++i) {
  22308. array[startingIndex++] = maximumHeights[i];
  22309. }
  22310. }
  22311. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  22312. startingIndex += Ellipsoid.packedLength;
  22313. array[startingIndex] = value._granularity;
  22314. return array;
  22315. };
  22316. var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  22317. var scratchOptions = {
  22318. positions : undefined,
  22319. minimumHeights : undefined,
  22320. maximumHeights : undefined,
  22321. ellipsoid : scratchEllipsoid,
  22322. granularity : undefined
  22323. };
  22324. /**
  22325. * Retrieves an instance from a packed array.
  22326. *
  22327. * @param {Number[]} array The packed array.
  22328. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  22329. * @param {WallOutlineGeometry} [result] The object into which to store the result.
  22330. * @returns {WallOutlineGeometry} The modified result parameter or a new WallOutlineGeometry instance if one was not provided.
  22331. */
  22332. WallOutlineGeometry.unpack = function(array, startingIndex, result) {
  22333. if (!defined(array)) {
  22334. throw new DeveloperError('array is required');
  22335. }
  22336. startingIndex = defaultValue(startingIndex, 0);
  22337. var i;
  22338. var length = array[startingIndex++];
  22339. var positions = new Array(length);
  22340. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  22341. positions[i] = Cartesian3.unpack(array, startingIndex);
  22342. }
  22343. length = array[startingIndex++];
  22344. var minimumHeights;
  22345. if (length > 0) {
  22346. minimumHeights = new Array(length);
  22347. for (i = 0; i < length; ++i) {
  22348. minimumHeights[i] = array[startingIndex++];
  22349. }
  22350. }
  22351. length = array[startingIndex++];
  22352. var maximumHeights;
  22353. if (length > 0) {
  22354. maximumHeights = new Array(length);
  22355. for (i = 0; i < length; ++i) {
  22356. maximumHeights[i] = array[startingIndex++];
  22357. }
  22358. }
  22359. var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  22360. startingIndex += Ellipsoid.packedLength;
  22361. var granularity = array[startingIndex];
  22362. if (!defined(result)) {
  22363. scratchOptions.positions = positions;
  22364. scratchOptions.minimumHeights = minimumHeights;
  22365. scratchOptions.maximumHeights = maximumHeights;
  22366. scratchOptions.granularity = granularity;
  22367. return new WallOutlineGeometry(scratchOptions);
  22368. }
  22369. result._positions = positions;
  22370. result._minimumHeights = minimumHeights;
  22371. result._maximumHeights = maximumHeights;
  22372. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  22373. result._granularity = granularity;
  22374. return result;
  22375. };
  22376. /**
  22377. * A description of a walloutline. A wall is defined by a series of points,
  22378. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  22379. *
  22380. * @param {Object} options Object with the following properties:
  22381. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  22382. * @param {Number} [options.maximumHeight] A constant that defines the maximum height of the
  22383. * wall at <code>positions</code>. If undefined, the height of each position in used.
  22384. * @param {Number} [options.minimumHeight] A constant that defines the minimum height of the
  22385. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  22386. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  22387. * @returns {WallOutlineGeometry}
  22388. *
  22389. *
  22390. * @example
  22391. * // create a wall that spans from 10000 meters to 20000 meters
  22392. * var wall = Cesium.WallOutlineGeometry.fromConstantHeights({
  22393. * positions : Cesium.Cartesian3.fromDegreesArray([
  22394. * 19.0, 47.0,
  22395. * 19.0, 48.0,
  22396. * 20.0, 48.0,
  22397. * 20.0, 47.0,
  22398. * 19.0, 47.0,
  22399. * ]),
  22400. * minimumHeight : 20000.0,
  22401. * maximumHeight : 10000.0
  22402. * });
  22403. * var geometry = Cesium.WallOutlineGeometry.createGeometry(wall);
  22404. *
  22405. * @see WallOutlineGeometry#createGeometry
  22406. */
  22407. WallOutlineGeometry.fromConstantHeights = function(options) {
  22408. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  22409. var positions = options.positions;
  22410. if (!defined(positions)) {
  22411. throw new DeveloperError('options.positions is required.');
  22412. }
  22413. var minHeights;
  22414. var maxHeights;
  22415. var min = options.minimumHeight;
  22416. var max = options.maximumHeight;
  22417. var doMin = defined(min);
  22418. var doMax = defined(max);
  22419. if (doMin || doMax) {
  22420. var length = positions.length;
  22421. minHeights = (doMin) ? new Array(length) : undefined;
  22422. maxHeights = (doMax) ? new Array(length) : undefined;
  22423. for (var i = 0; i < length; ++i) {
  22424. if (doMin) {
  22425. minHeights[i] = min;
  22426. }
  22427. if (doMax) {
  22428. maxHeights[i] = max;
  22429. }
  22430. }
  22431. }
  22432. var newOptions = {
  22433. positions : positions,
  22434. maximumHeights : maxHeights,
  22435. minimumHeights : minHeights,
  22436. ellipsoid : options.ellipsoid
  22437. };
  22438. return new WallOutlineGeometry(newOptions);
  22439. };
  22440. /**
  22441. * Computes the geometric representation of a wall outline, including its vertices, indices, and a bounding sphere.
  22442. *
  22443. * @param {WallOutlineGeometry} wallGeometry A description of the wall outline.
  22444. * @returns {Geometry|undefined} The computed vertices and indices.
  22445. */
  22446. WallOutlineGeometry.createGeometry = function(wallGeometry) {
  22447. var wallPositions = wallGeometry._positions;
  22448. var minimumHeights = wallGeometry._minimumHeights;
  22449. var maximumHeights = wallGeometry._maximumHeights;
  22450. var granularity = wallGeometry._granularity;
  22451. var ellipsoid = wallGeometry._ellipsoid;
  22452. var pos = WallGeometryLibrary.computePositions(ellipsoid, wallPositions, maximumHeights, minimumHeights, granularity, false);
  22453. if (!defined(pos)) {
  22454. return;
  22455. }
  22456. var bottomPositions = pos.bottomPositions;
  22457. var topPositions = pos.topPositions;
  22458. var length = topPositions.length;
  22459. var size = length * 2;
  22460. var positions = new Float64Array(size);
  22461. var positionIndex = 0;
  22462. // add lower and upper points one after the other, lower
  22463. // points being even and upper points being odd
  22464. length /= 3;
  22465. var i;
  22466. for (i = 0; i < length; ++i) {
  22467. var i3 = i * 3;
  22468. var topPosition = Cartesian3.fromArray(topPositions, i3, scratchCartesian3Position1);
  22469. var bottomPosition = Cartesian3.fromArray(bottomPositions, i3, scratchCartesian3Position2);
  22470. // insert the lower point
  22471. positions[positionIndex++] = bottomPosition.x;
  22472. positions[positionIndex++] = bottomPosition.y;
  22473. positions[positionIndex++] = bottomPosition.z;
  22474. // insert the upper point
  22475. positions[positionIndex++] = topPosition.x;
  22476. positions[positionIndex++] = topPosition.y;
  22477. positions[positionIndex++] = topPosition.z;
  22478. }
  22479. var attributes = new GeometryAttributes({
  22480. position : new GeometryAttribute({
  22481. componentDatatype : ComponentDatatype.DOUBLE,
  22482. componentsPerAttribute : 3,
  22483. values : positions
  22484. })
  22485. });
  22486. var numVertices = size / 3;
  22487. size = 2 * numVertices - 4 + numVertices;
  22488. var indices = IndexDatatype.createTypedArray(numVertices, size);
  22489. var edgeIndex = 0;
  22490. for (i = 0; i < numVertices - 2; i += 2) {
  22491. var LL = i;
  22492. var LR = i + 2;
  22493. var pl = Cartesian3.fromArray(positions, LL * 3, scratchCartesian3Position1);
  22494. var pr = Cartesian3.fromArray(positions, LR * 3, scratchCartesian3Position2);
  22495. if (Cartesian3.equalsEpsilon(pl, pr, CesiumMath.EPSILON10)) {
  22496. continue;
  22497. }
  22498. var UL = i + 1;
  22499. var UR = i + 3;
  22500. indices[edgeIndex++] = UL;
  22501. indices[edgeIndex++] = LL;
  22502. indices[edgeIndex++] = UL;
  22503. indices[edgeIndex++] = UR;
  22504. indices[edgeIndex++] = LL;
  22505. indices[edgeIndex++] = LR;
  22506. }
  22507. indices[edgeIndex++] = numVertices - 2;
  22508. indices[edgeIndex++] = numVertices - 1;
  22509. return new Geometry({
  22510. attributes : attributes,
  22511. indices : indices,
  22512. primitiveType : PrimitiveType.LINES,
  22513. boundingSphere : new BoundingSphere.fromVertices(positions)
  22514. });
  22515. };
  22516. return WallOutlineGeometry;
  22517. });
  22518. /*global define*/
  22519. define('Workers/createWallOutlineGeometry',[
  22520. '../Core/defined',
  22521. '../Core/Ellipsoid',
  22522. '../Core/WallOutlineGeometry'
  22523. ], function(
  22524. defined,
  22525. Ellipsoid,
  22526. WallOutlineGeometry) {
  22527. 'use strict';
  22528. function createWallOutlineGeometry(wallGeometry, offset) {
  22529. if (defined(offset)) {
  22530. wallGeometry = WallOutlineGeometry.unpack(wallGeometry, offset);
  22531. }
  22532. wallGeometry._ellipsoid = Ellipsoid.clone(wallGeometry._ellipsoid);
  22533. return WallOutlineGeometry.createGeometry(wallGeometry);
  22534. }
  22535. return createWallOutlineGeometry;
  22536. });
  22537. }());